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.