Hello everyone,

i’ trying to solve the following nonlinear equation using dolfinx, and i keep getting the error that Newton solver did not converge because maximum number of iterations is reached.

Equation:

i defined c as a constant and g as a given Function on my domain.

Code defining the nonlinear problem:

```
F = ufl.inner(u*u/c,v)*ufl.dx - ufl.inner(ufl.dot(ufl.grad(g),ufl.grad(u)),v)*ufl.dx
problem = fem.petsc.NonlinearProblem(F, u, bcu)
from dolfinx import nls
solver = nls.petsc.NewtonSolver(MPI.COMM_WORLD, problem)
solver.convergence_criterion = "residual"
solver.atol = 1e-6
solver.rtol = 1e-6
solver.report = True
from petsc4py import PETSc
ksp = solver.krylov_solver
opts = PETSc.Options()
option_prefix = ksp.getOptionsPrefix()
opts[f"{option_prefix}ksp_type"] = "cg"
opts[f"{option_prefix}pc_type"] = "gamg"
opts[f"{option_prefix}pc_factor_mat_solver_type"] = "mumps"
ksp.setFromOptions()
from dolfinx import log
log.set_log_level(log.LogLevel.INFO)
n, converged = solver.solve(u)
assert(converged)
print(f"Number of interations: {n:d}")
```

the Error i get:

```
2022-04-20 21:16:55.998 ( 29.616s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 0: r (abs) = 0.060699 (tol = 1e-06) r (rel) = inf(tol = 1e-06)
2022-04-20 21:16:56.000 ( 29.619s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:16:56.025 ( 29.644s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 1: r (abs) = 0.600538 (tol = 1e-06) r (rel) = 1949.02(tol = 1e-06)
2022-04-20 21:16:56.028 ( 29.647s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:16:59.059 ( 32.677s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 2: r (abs) = 471024 (tol = 1e-06) r (rel) = 1.52869e+09(tol = 1e-06)
2022-04-20 21:16:59.060 ( 32.679s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:16:59.070 ( 32.689s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 3: r (abs) = 1.12878e+06 (tol = 1e-06) r (rel) = 3.66342e+09(tol = 1e-06)
2022-04-20 21:16:59.073 ( 32.691s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.634 ( 38.253s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 4: r (abs) = 1.13335e+06 (tol = 1e-06) r (rel) = 3.67824e+09(tol = 1e-06)
2022-04-20 21:17:04.637 ( 38.255s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.669 ( 38.287s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 5: r (abs) = 1.2926e+08 (tol = 1e-06) r (rel) = 4.19508e+11(tol = 1e-06)
2022-04-20 21:17:04.670 ( 38.289s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.677 ( 38.296s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 6: r (abs) = 5.30945e+10 (tol = 1e-06) r (rel) = 1.72316e+14(tol = 1e-06)
2022-04-20 21:17:04.679 ( 38.297s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.690 ( 38.308s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 7: r (abs) = 5.44664e+12 (tol = 1e-06) r (rel) = 1.76769e+16(tol = 1e-06)
2022-04-20 21:17:04.692 ( 38.311s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.705 ( 38.324s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 8: r (abs) = 6.0341e+14 (tol = 1e-06) r (rel) = 1.95835e+18(tol = 1e-06)
2022-04-20 21:17:04.708 ( 38.327s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.740 ( 38.359s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 9: r (abs) = 1.14422e+17 (tol = 1e-06) r (rel) = 3.71353e+20(tol = 1e-06)
2022-04-20 21:17:04.743 ( 38.362s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.768 ( 38.386s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 10: r (abs) = 5.19606e+17 (tol = 1e-06) r (rel) = 1.68636e+21(tol = 1e-06)
2022-04-20 21:17:04.770 ( 38.389s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.792 ( 38.410s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 11: r (abs) = 6.84943e+17 (tol = 1e-06) r (rel) = 2.22296e+21(tol = 1e-06)
2022-04-20 21:17:04.794 ( 38.413s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.810 ( 38.428s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 12: r (abs) = 2.11929e+18 (tol = 1e-06) r (rel) = 6.87807e+21(tol = 1e-06)
2022-04-20 21:17:04.811 ( 38.430s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.825 ( 38.444s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 13: r (abs) = 3.17833e+24 (tol = 1e-06) r (rel) = 1.03152e+28(tol = 1e-06)
2022-04-20 21:17:04.828 ( 38.446s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.845 ( 38.464s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 14: r (abs) = 8.03616e+23 (tol = 1e-06) r (rel) = 2.60811e+27(tol = 1e-06)
2022-04-20 21:17:04.848 ( 38.467s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.870 ( 38.488s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 15: r (abs) = 9.51312e+25 (tol = 1e-06) r (rel) = 3.08745e+29(tol = 1e-06)
2022-04-20 21:17:04.873 ( 38.491s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.904 ( 38.523s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 16: r (abs) = 7.54087e+26 (tol = 1e-06) r (rel) = 2.44736e+30(tol = 1e-06)
2022-04-20 21:17:04.907 ( 38.525s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.928 ( 38.546s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 17: r (abs) = 1.92042e+27 (tol = 1e-06) r (rel) = 6.23267e+30(tol = 1e-06)
2022-04-20 21:17:04.930 ( 38.549s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.941 ( 38.560s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 18: r (abs) = 1.22528e+27 (tol = 1e-06) r (rel) = 3.97661e+30(tol = 1e-06)
2022-04-20 21:17:04.946 ( 38.564s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:04.969 ( 38.587s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 19: r (abs) = 3.46014e+29 (tol = 1e-06) r (rel) = 1.12298e+33(tol = 1e-06)
2022-04-20 21:17:04.971 ( 38.590s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.002 ( 38.621s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 20: r (abs) = 6.40238e+30 (tol = 1e-06) r (rel) = 2.07787e+34(tol = 1e-06)
2022-04-20 21:17:05.005 ( 38.624s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.025 ( 38.643s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 21: r (abs) = 2.47446e+31 (tol = 1e-06) r (rel) = 8.03076e+34(tol = 1e-06)
2022-04-20 21:17:05.028 ( 38.646s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.048 ( 38.666s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 22: r (abs) = 1.74874e+31 (tol = 1e-06) r (rel) = 5.67548e+34(tol = 1e-06)
2022-04-20 21:17:05.051 ( 38.669s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.069 ( 38.688s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 23: r (abs) = 2.1068e+31 (tol = 1e-06) r (rel) = 6.83756e+34(tol = 1e-06)
2022-04-20 21:17:05.081 ( 38.700s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.095 ( 38.714s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 24: r (abs) = 2.10729e+31 (tol = 1e-06) r (rel) = 6.83912e+34(tol = 1e-06)
2022-04-20 21:17:05.104 ( 38.722s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.122 ( 38.741s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 25: r (abs) = 1.176e+32 (tol = 1e-06) r (rel) = 3.81665e+35(tol = 1e-06)
2022-04-20 21:17:05.124 ( 38.742s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.139 ( 38.757s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 26: r (abs) = 1.86258e+32 (tol = 1e-06) r (rel) = 6.04495e+35(tol = 1e-06)
2022-04-20 21:17:05.141 ( 38.760s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.165 ( 38.783s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 27: r (abs) = 5.69039e+34 (tol = 1e-06) r (rel) = 1.8468e+38(tol = 1e-06)
2022-04-20 21:17:05.170 ( 38.789s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.186 ( 38.805s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 28: r (abs) = 8.46494e+39 (tol = 1e-06) r (rel) = 2.74727e+43(tol = 1e-06)
2022-04-20 21:17:05.188 ( 38.807s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.202 ( 38.820s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 29: r (abs) = 8.46507e+39 (tol = 1e-06) r (rel) = 2.74731e+43(tol = 1e-06)
2022-04-20 21:17:05.214 ( 38.832s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.235 ( 38.854s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 30: r (abs) = 8.46744e+39 (tol = 1e-06) r (rel) = 2.74808e+43(tol = 1e-06)
2022-04-20 21:17:05.238 ( 38.856s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.269 ( 38.887s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 31: r (abs) = 3.58916e+41 (tol = 1e-06) r (rel) = 1.16485e+45(tol = 1e-06)
2022-04-20 21:17:05.275 ( 38.894s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.290 ( 38.908s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 32: r (abs) = 1.56324e+49 (tol = 1e-06) r (rel) = 5.07344e+52(tol = 1e-06)
2022-04-20 21:17:05.292 ( 38.911s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.306 ( 38.924s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 33: r (abs) = 1.96084e+56 (tol = 1e-06) r (rel) = 6.36384e+59(tol = 1e-06)
2022-04-20 21:17:05.307 ( 38.926s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.316 ( 38.935s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 34: r (abs) = 1.04376e+57 (tol = 1e-06) r (rel) = 3.38748e+60(tol = 1e-06)
2022-04-20 21:17:05.319 ( 38.938s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.326 ( 38.945s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 35: r (abs) = 1.04376e+57 (tol = 1e-06) r (rel) = 3.38748e+60(tol = 1e-06)
2022-04-20 21:17:05.328 ( 38.946s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.338 ( 38.957s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 36: r (abs) = 1.40747e+57 (tol = 1e-06) r (rel) = 4.56789e+60(tol = 1e-06)
2022-04-20 21:17:05.341 ( 38.960s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.355 ( 38.973s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 37: r (abs) = 2.87834e+57 (tol = 1e-06) r (rel) = 9.34154e+60(tol = 1e-06)
2022-04-20 21:17:05.357 ( 38.976s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.371 ( 38.990s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 38: r (abs) = 1.89304e+58 (tol = 1e-06) r (rel) = 6.14379e+61(tol = 1e-06)
2022-04-20 21:17:05.373 ( 38.992s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.382 ( 39.000s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 39: r (abs) = 1.81924e+60 (tol = 1e-06) r (rel) = 5.90429e+63(tol = 1e-06)
2022-04-20 21:17:05.384 ( 39.003s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.395 ( 39.013s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 40: r (abs) = 1.81942e+60 (tol = 1e-06) r (rel) = 5.90486e+63(tol = 1e-06)
2022-04-20 21:17:05.397 ( 39.015s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.407 ( 39.026s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 41: r (abs) = 1.83654e+60 (tol = 1e-06) r (rel) = 5.96044e+63(tol = 1e-06)
2022-04-20 21:17:05.410 ( 39.028s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.420 ( 39.039s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 42: r (abs) = 1.10833e+62 (tol = 1e-06) r (rel) = 3.59706e+65(tol = 1e-06)
2022-04-20 21:17:05.423 ( 39.041s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.442 ( 39.061s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 43: r (abs) = 2.59137e+63 (tol = 1e-06) r (rel) = 8.41019e+66(tol = 1e-06)
2022-04-20 21:17:05.444 ( 39.062s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.455 ( 39.073s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 44: r (abs) = 5.05397e+63 (tol = 1e-06) r (rel) = 1.64025e+67(tol = 1e-06)
2022-04-20 21:17:05.459 ( 39.077s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.469 ( 39.088s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 45: r (abs) = 5.29098e+63 (tol = 1e-06) r (rel) = 1.71717e+67(tol = 1e-06)
2022-04-20 21:17:05.472 ( 39.090s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.491 ( 39.109s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 46: r (abs) = 1.42779e+67 (tol = 1e-06) r (rel) = 4.63384e+70(tol = 1e-06)
2022-04-20 21:17:05.494 ( 39.112s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.514 ( 39.133s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 47: r (abs) = 2.86324e+68 (tol = 1e-06) r (rel) = 9.29255e+71(tol = 1e-06)
2022-04-20 21:17:05.517 ( 39.136s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.533 ( 39.151s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 48: r (abs) = 2.86324e+68 (tol = 1e-06) r (rel) = 9.29255e+71(tol = 1e-06)
2022-04-20 21:17:05.536 ( 39.154s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.554 ( 39.172s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 49: r (abs) = 2.92111e+68 (tol = 1e-06) r (rel) = 9.48037e+71(tol = 1e-06)
2022-04-20 21:17:05.556 ( 39.175s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.
2022-04-20 21:17:05.573 ( 39.191s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 50: r (abs) = 3.95506e+68 (tol = 1e-06) r (rel) = 1.2836e+72(tol = 1e-06)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [13], in <cell line: 32>()
29 from dolfinx import log
31 log.set_log_level(log.LogLevel.INFO)
---> 32 n, converged = solver.solve(u)
33 assert(converged)
34 print(f"Number of interations: {n:d}")
File /usr/local/dolfinx-complex/lib/python3.8/dist-packages/dolfinx/nls/petsc.py:41, in NewtonSolver.solve(self, u)
38 def solve(self, u: fem.Function):
39 """Solve non-linear problem into function u. Returns the number
40 of iterations and if the solver converged."""
---> 41 n, converged = super().solve(u.vector)
42 u.x.scatter_forward()
43 return n, converged
RuntimeError: Newton solver did not converge because maximum number of iterations reached
```

Could someone help me with that or gives me tips on how this could be solved, i would really appreciate it.

Thanks.