Creating a linear and newton solver

Hi everyone,
Hope you are safe. I have a question regarding creating newton solver. Follow is my code:

pc = PETScPreconditioner("ilu")
pc.parameters["ilu"]["fill_level"] = 1

#Create Krylov linear solvers and set parameters

krylov_solver = PETScKrylovSolver("bicgstab", pc)
krylov_solver.parameters["relative_tolerance"] = 1.0e-6
krylov_solver.parameters["error_on_nonconvergence"] = False
krylov_solver.parameters["maximum_iterations"] = 250

#Create newton solver
newton_solver = NewtonSolver(krylov_solver, PETScFactory.instance())
newton_solver.parameters["relative_tolerance"] = 1.0e-6
newton_solver.parameters["absolute_tolerance"] = 1.0e-14
newton_solver.parameters["maximum_iterations"] = 10
newton_solver.parameters["error_on_nonconvergence"] = False

At the beginning, I got an error message for line 2 of my code:

RuntimeError Traceback (most recent call last)
in
2
3 pc = PETScPreconditioner(“ilu”)
----> 4 pc.parameters[“ilu”][“fill_level”] = 1
5
6 #Create Krylov linear solvers and set parameters

~/anaconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/parameter/init.py in getitem(self, key)
25 # return np
26 else:
—> 27 raise RuntimeError(“Invalid parameter: {}”.format(key))
28
29

RuntimeError: Invalid parameter: ilu

when I make line 2 as comments, following error appears:

TypeError Traceback (most recent call last)
in
12
13 #Create newton solver
—> 14 newton_solver = NewtonSolver(krylov_solver, PETScFactory.instance())
15 newton_solver.parameters[“relative_tolerance”] = 1.0e-6
16 newton_solver.parameters[“absolute_tolerance”] = 1.0e-14

TypeError: init(): incompatible constructor arguments. The following argument types are supported:
1. dolfin.cpp.nls.NewtonSolver()
2. dolfin.cpp.nls.NewtonSolver(arg0: MPICommWrapper)
3. dolfin.cpp.nls.NewtonSolver(arg0: MPICommWrapper, arg1: dolfin.cpp.la.GenericLinearSolver, arg2: dolfin.cpp.la.GenericLinearAlgebraFactory)

Invoked with: <dolfin.cpp.la.PETScKrylovSolver object at 0x7f496958f7f0>, <dolfin.cpp.la.PETScFactory object at 0x7f49696a5070>

I am using latest version of FEniCS, however, I could not find the solution. Your help would be highly appreciated.

The second error can be resolved by choosing the third function signature given in the error message:

newton_solver = NewtonSolver(MPI.comm_world,
                             krylov_solver, PETScFactory.instance())

I’m not sure what the best way to resolve the first error is, although you can use the petsc4py API to set arbitrary PETSc options.

Thanks very much David for your great help. Actually I have some main problems to convert my code to become compatible with newest version of FEniCS. Again Thanks a lot.