Monitoring the number of iterations of the nonlinear solver

Dear FEniCS community,

I need to monitor the number of iterations (progress information regarding the linear/nonlinear solvers I used).
Could you please guide me?
The FEniCS version: FEniCS 2019.2.0.dev0
IDE: Spyder 3.2.6

Note: I tried almost everything I could find on the FEniCS discourses but couldn’t find a solution. As a last resort, I’m asking you.

Thank you very much

Since you have not specified the way you are solving your nonlinear problem, I will suggest three different ways to get number of iterations.

For example, if you use NonlinearVariationalProblem, you can get number of iterations in a following way:

J = derivative(F, u)
problem = NonlinearVariationalProblem(F, u, bc, J)
solver = NonlinearVariationalSolver(problem)
number_of_iterations, convergence = solver.solve()

However, I would suggest you to design custom Newton solver, as described here.
Then you can get number iterations in a following way:

J = derivative(F, u)
problem = Problem(J, F, bcs)
custom_solver = CustomSolver()
no_iterations, convergence = custom_solver.solve(problem, u.vector())

Alternatively, you can use PETScSNESSolver(), where you get number of iterations similarly

J = derivative(F, u)
problem = NonlinearVariationalProblem(F, u, bc, J)
solver = PETScSNESSolver()
number_of_iterations, convergence = solver.solve()

The simplest way would be the first one, however designing your own solver, or using PETScSNESSolver() provides some options which could help to improve convergence.

3 Likes

Thank you very much for your time and your detailed explanations.
This worked for my problem.
I’m also grateful for the other solver information. I have noted them as well.
Sincerely