Unable to set PETSc Options with NonlinearProblem

Hi, I’m struggling to understand the PETSc options process, but from what I have found, something along the lines of what is present in this MWE should work to solve my error (ERROR 76).

I have a new error raised:

Traceback (most recent call last):
  File "/testfile.py", line 35, in <module>
    problem = dolfinx.fem.petsc.NonlinearProblem(F, u, bc)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/petsc.py", line 904, in __init__
    du = ufl.TrialFunction(V)
  File "/usr/lib/python3/dist-packages/ufl/argument.py", line 285, in TrialFunction
    return Argument(function_space, 1, part)
  File "/usr/lib/python3/dist-packages/ufl/argument.py", line 176, in __init__
    BaseArgument.__init__(self, function_space, number, part)
  File "/usr/lib/python3/dist-packages/ufl/argument.py", line 46, in __init__
    raise ValueError("Expecting a FunctionSpace.")
ValueError: Expecting a FunctionSpace.

Here is the MWE:

 
import fenics as fn
import dolfinx.fem.petsc
import numpy as np
import scipy.constants as sc
from scipy.interpolate import RegularGridInterpolator as rgi
import matplotlib.pyplot as plt 
from petsc4py import PETSc
from mpi4py import MPI


# Parameters
# in SI
# Material and environmental constants for Quartz and the laser setup
B = 0.02  # x-dimension of the box in meters
l = 0.02 # y-dimension of the box in meters
h = 0.01  # z-dimension of the box in meters (height of the material)

# Mesh Creation
mesh = fn.BoxMesh(MPI.COMM_WORLD, fn.Point(0, 0, 0), fn.Point(B, l, h), 20, 20, 20)

# Function Space Initialization
V = fn.FunctionSpace(mesh, "CG", 2)

# Variational Problem Definition
u = fn.TrialFunction(V)
v = fn.TestFunction(V)
f = fn.Constant(0)

bc = []

F = fn.dot(fn.grad(u), fn.grad(v))*fn.dx
a, L = fn.lhs(F), fn.rhs(F)

problem = dolfinx.fem.petsc.NonlinearProblem(F, u, bc)
solver = dolfinx.nls.petsc.NewtonSolver(mesh.comm, problem)
solver.error_on_nonconvergence = False
ksp = solver.krylov_solver
opts = PETSc.Options()
option_prefix = ksp.getOptionsPrefix()
opts[f"{option_prefix}ksp_type"] = "preonly"
opts[f"{option_prefix}pc_type"] = "lu"
opts[f"{option_prefix}pc_factor_mat_solver_type"]="mumps"
ksp.setFromOptions()

print(">SOLVING...\n...")
u = fn.Function(V) # new unknown function
fn.solve(a == L, u, bc)

I’m not sure where to begin when solving this, does anyone have some insight on where to start?

Thanks in advance!

You seem to be mixing legacy fenics and fenicsx objects. If you want to use the dolfinx.fem.petsc.NonlinearProblem, then the rest of your code must also be based on dolfinx.

So, anything related to import fenics as fn and fn in your code must be replaced by the appropriate dolfinx equivalent.

1 Like

Thanks for the fast response, Stein!

Ah, that makes sense to me. I had found some code snippets that were perhaps not the solution to my issue.

So, my priority is to simply set pc_type to "lu", and that’s about it. Is there a way to abbreviate this options-setting process to bypass the need to convert to dolfinx from fenics?

I guess this demo shows the procedure you’re looking for, right?

https://olddocs.fenicsproject.org/dolfin/2019.1.0/python/demos/cahn-hilliard/demo_cahn-hilliard.py.html