This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to set error_on_nonconvergence for NonlinearVariationalSolver in C++

0 votes

I'm trying to set the "error_on_nonconvergence" flag as follows in C++ code:

    auto ProblemPhi = std::make_shared<NonlinearVariationalProblem>(F1, phi, dbcPhi, J1);
    auto SolverPhi  = std::make_shared<NonlinearVariationalSolver>(ProblemPhi);
    std::pair<std::size_t, bool> phiResult;
...
    SolverPhi->parameters["error_on_nonconvergence"] = "false"; 
....
    phiResult = SolverPhi->solve();

This compiles, but then throws an exception when run:

*** Error: Unable to access parameter.
*** Reason: Parameter "nonlinear_variational_solver.error_on_nonconvergence" not defined.
*** Where: This error was encountered inside Parameters.cpp.
*** Process: 0


*** DOLFIN version: 2017.1.0
*** Git changeset: 3d1f687ec9ee39afc0fe6e01800431995b42ad04

I couldn't find this in the docs or any demo. How does one access the parameters (in C++) for NonlinearVariationalSolver properly?

Thx! -JW

asked Jun 26, 2017 by jwinkle FEniCS Novice (450 points)
edited Jun 27, 2017 by jwinkle

1 Answer

0 votes
 
Best answer

Update: (copied from my response in fenics-support google group, after solving the issue):

I had to dig around in the source files (NonlinearVariationalSolver.cpp in dolfin/dolfin/fem/) and the proper syntax is:

SolverPhi->parameters("newton_solver")["error_on_nonconvergence"] = false; 

Thus, this differs from python in that the newton_solver parameters are returned via function call (with string argument "newton_solver") instead of (what appears) as a key-value extraction directly in python.

To give a further explanation for future reference, the Parameters class is attached to the base class Variable, of which both NonlinearVariationalSolver and NewtonSolver are derived classes. In writing-out this response, I have just found there is a proper explanation in the source file "Parameters.cpp" (note the 's' at the end, located in dolfin/dolfin/parameter/), which says:

/// Parameter sets may be nested as follows:
  ///   Parameters q("nested_parameters");
  ///   p.add(q);

/// Nested parameters may then be accessed by  ///
  ///   p("nested_parameters")["..."]

/// Parameters may be nested at arbitrary depths.

This may exist somewhere in the documentation for Parameters, but it would be helpful to add this to the documentation of NonlinearVariationalSolver and/or NewtonSolver. The confusing part was that the object newton_solver is private in NonlinearVariationalSolver, but yet one still needs access to the parameters of the NewtonSolver class from the NonlinearVariationalSolver class. In any case these access rules make it clear.

Cheers,
James W.

answered Jun 30, 2017 by jwinkle FEniCS Novice (450 points)
edited Jul 3, 2017 by jwinkle
...