PETSC solver Fenics 2018

Hello,

I’m trying to update my code from fenics 2017.2 to the last version 2018. I have a problem with the PETScSNESSolver I used in 2017 :

problem = GeneralProblem(F, J, bnd)
solver = PETScSNESSolver(‘newtonls’)
solver.parameters[‘linear_solver’] = ‘lu’
solver.parameters[‘report’] = False
solver.init(problem, u.vector())
snes = solver.snes()

GeneralProblem is a class which inherit from NonLinearProblem.
Apparently PETScSNESSolver doesn’t have a method init anymore.
Anybody know how to use the PETScSNESSolver in Fenics 2018 ?

Thanks in advance.

The following example runs for me in 2018.1:

from dolfin import *

mesh = UnitSquareMesh(10,10)
V = FunctionSpace(mesh,"Lagrange",1)
u = Function(V)
v = TestFunction(V)
x = SpatialCoordinate(mesh)
f = x[0]*x[1]
res = (1.0+u*u)*inner(grad(u),grad(v))*dx + inner(u,v)*dx - inner(f,v)*dx
Dres = derivative(res,u)

class CustomNonlinearProblem(NonlinearProblem):
    def F(self,b,x):
        return assemble(res,tensor=b)
    def J(self,A,x):
        return assemble(Dres,tensor=A)

problem = CustomNonlinearProblem()
solver = PETScSNESSolver()
solver.solve(problem,u.vector())

I can also set the options

solver.parameters['linear_solver'] = 'lu'
solver.parameters['report'] = False

before solving without any errors.

3 Likes

That works for me too.
Thank you !