Hello,
when solving a nonlinear problem (e.g. nonlinear Poisson Equation) within fenicsx, one can use the following code from the tutorial:
import ufl
import numpy as np
from mpi4py import MPI
from petsc4py import PETSc
from dolfinx import mesh, fem
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.nls.petsc import NewtonSolver
def q(u):
return 1 + u**2
domain = mesh.create_unit_square(MPI.COMM_WORLD, 5, 5)
x = ufl.SpatialCoordinate(domain)
u_ufl = 1 + x[0] + 2 * x[1]
f = - ufl.div(q(u_ufl) * ufl.grad(u_ufl))
V = fem.functionspace(domain, ("Lagrange", 1))
def u_exact(x):
return eval(str(u_ufl))
u_D = fem.Function(V)
u_D.interpolate(u_exact)
fdim = domain.topology.dim - 1
boundary_facets = mesh.locate_entities_boundary(domain, fdim, lambda x: np.full(x.shape[1], True, dtype=bool))
bc = fem.dirichletbc(u_D, fem.locate_dofs_topological(V, fdim, boundary_facets))
uh = fem.Function(V)
v = ufl.TestFunction(V)
F = q(uh) * ufl.dot(ufl.grad(uh), ufl.grad(v)) * ufl.dx - f * v * ufl.dx
problem = NonlinearProblem(F, uh, bcs=[bc])
solver = NewtonSolver(MPI.COMM_WORLD, problem)
# direct solver
ksp = PETSc.KSP().create()
option_prefix = ksp.getOptionsPrefix()
opts = PETSc.Options()
opts['ksp_type'] = "preonly"
opts['pc_type'] = "lu"
ksp.setFromOptions()
n, converged = solver.solve(uh)
print(f"Number of interations: {n:d}")
I wonder how it is possible to access the linearized systems that are solved within the NewtonSolver (e.g. to visualize A and RHS b). I found that in dolfinx/fem/petsc.py there is the function
def J(self, x: PETSc.Vec, A: PETSc.Mat) -> None:
"""Assemble the Jacobian matrix.
Args:
x: The vector containing the latest solution
"""
A.zeroEntries()
assemble_matrix_mat(A, self._a, self.bcs)
A.assemble()
which is called in every iteration within the NewtonSolver. However, I suspect that directly editing code here (e.g. adding A.view()) is not the proper way to do this. I tried using KSPMonitor but this did not work. Maybe one has to overwrite the NewtonSolver class in order to “expose” the linearized systems? Any help is greatly apprechiated.
Thank you.
cheers
domi