Using petsc4py.PETSc.SNES directly

The copy lines are necessary because you have no guarantee that the solution vector used by PETSc (x in def F(self, snes, x, F)) shares the same memory as the dolfinx.fem.Function (self.u in that snippet). In practice, they probably won’t, especially if line search is enabled.

The call to .apply("") is typically needed.

The method .apply doesn’t seem to exist in dolfinx, aren’t you talking about dolfin legacy?

Yes, sorry. In this topic there are some posts that use dolfin and others that use dolfinx, so I got confused: I replied to the first question assuming you were using dolfinx, and to the second one as if you were using dolfin. If you are using dolfinx, see for instance dolfinx/python/test/unit/nls/test_newton.py at e0dcd43553ee97d8873f2a7c873de3cde653f795 · FEniCS/dolfinx · GitHub

Thanks for the clear explanation. I appreciate it!