NewtonSolver in v11

Hello, I am trying to run the hyperelasticity demo from Hyperelasticity — Computational Mechanics Numerical Tours with FEniCSx , but I get the following error message, related to NewtonSolver in NonlinearProblem:

Traceback (most recent call last):
  File "/home/claire/Downloads/hyperelasticity.py", line 224, in <module>
    solver = nls.petsc.NewtonSolver(mesh.comm, problem)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/claire/anaconda3/envs/fenicsx-env-v11/lib/python3.11/site-packages/dolfinx/nls/petsc.py", line 54, in __init__
    self._A = create_matrix(problem.a)
                            ^^^^^^^^^
AttributeError: 'NonlinearProblem' object has no attribute 'a'. Did you mean: 'A'?
Exception ignored in: <function NewtonSolver.__del__ at 0x7fc392df0720>
Traceback (most recent call last):
  File "/home/claire/anaconda3/envs/fenicsx-env-v11/lib/python3.11/site-packages/dolfinx/nls/petsc.py", line 62, in __del__
    for obj in filter(lambda obj: obj is not None, (self._A, self._b)):
                                                    ^^^^^^^
AttributeError: 'NewtonSolver' object has no attribute '_A'

I am not sure how to solve this. I have already tried editing lines below line 210:

petsc_options = {
    "snes_type": "newtonls",
    "snes_linesearch_type": "none",
    "snes_stol": np.sqrt(np.finfo(dolfinx.default_real_type).eps) * 1e-2,
    "snes_atol": 0,
    "snes_rtol": 0,
    "ksp_type": "preonly",
    "pc_type": "lu",
    "pc_factor_mat_solver_type": "mumps",
    "snes_monitor": None,
}
problem = fem.petsc.NonlinearProblem(Residual, u, bcs=bcs,  petsc_options_prefix="demo_hyperelasticity_",  petsc_options=petsc_options)

but I still get the error above.

Many thanks in advance for your help !

Kind regards,

Claire

As far as what i know, the referenced hyperelasticity demo corresonding to dolfinx version 0.8. For version V11 it might not be compatible, which is why the possible error.

From the small snippet, i noticed that you have to replace the definition of problem as,

from dolfinx.fem.petsc import NewtonSolverNonlinearProblem
problem = fem.petsc.NewtonSolverNonlinearProblem(Residual, u, bcs,)
solver = nls.petsc.NewtonSolver(mesh.comm, problem)

Also when i tried, there is another modification subjected to v11,

# u.vector.set(0.0) --> to be replaced as 
u.x.petsc_vec.set(0.0)

I hope this works.

The new NonlinearProblem class has been simplified so that it builds its own petsc solver, thus there is no need to use NewtonSolver. Just call problem.solve() on instance you build in your edited code, then access convergence status and number of solves etc. by

problem.solve()
num_its = problem.solver.getIterationNumber()
converged = problem.solver.getConvergedReason()

The class that was called NonlinearProblem in the old versions has been moved to NewtonSolverNonlinearProblem, so you can either use that class with a NewtonSolver, or just use the new simplified interface.