Hi! I am new to FEniCSx and before jumping into my more complex real problem, I was using a toy model when I run into an error using one of the solvers. I am using the version fenics-dolfinx 0.9.0 and fenics-ufl 2024.2.0 and I installed them using conda-forge on my macOS Ventura13.1 (chip M1). My code is the following:
import pygmsh
import meshio
import dolfinx
import dolfinx.fem.petsc
from ufl import TestFunction, TrialFunction, interpolate, dx, grad, inner
import numpy as np
import matplotlib.pyplot as plt
from mpi4py import MPI
from petsc4py import PETSc
# %% Create mesh
# Generate the spherical surface mesh with pygmsh
with pygmsh.geo.Geometry() as geom:
radius = 1.0 # Radius of the sphere
# Add a sphere with a smaller mesh size for higher point density
sphere = geom.add_ball([0, 0, 0], radius, mesh_size=0.05) # Smaller mesh_size -> more points
mesh = geom.generate_mesh(dim=2) # Generate 2D surface mesh
# Filter out mixed cells and retain only triangles
triangle_cells = mesh.cells_dict["triangle"]
mesh = meshio.Mesh(
points=mesh.points,
cells=[("triangle", triangle_cells)],
)
# Save the filtered mesh in XDMF format
meshio.write("SphericalSurfaceMesh.xdmf", mesh)
# %% Load the mesh and integrate the system
# Load the filtered mesh into FEniCSx
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "SphericalSurfaceMesh.xdmf", "r") as infile:
fenics_mesh = infile.read_mesh(name="Grid")
cell_radius = 75
Du = dolfinx.fem.Constant(fenics_mesh, 0.08/(cell_radius**2))
coordinates = fenics_mesh.geometry.x # Nodal coordinates
x, y, z = coordinates[:, 0], coordinates[:, 1], coordinates[:, 2]
# Define the function space for the scalar field u
V = dolfinx.fem.functionspace(fenics_mesh, ("CG", 1))
# Define trial and test functions
u = TrialFunction(V)
v_test_u = TestFunction(V)
# Define the bilinear and linear forms
a_u = Du*inner(grad(u), grad(v_test_u))*dx
A_u = dolfinx.fem.petsc.assemble_matrix(dolfinx.fem.form(a_u))
# Update RHS using the current values of u, v, w
f_u = 1.
L_u = dolfinx.fem.assemble_vector(dolfinx.fem.form(inner(f_u, v_test_u)*dx))
problem_u = dolfinx.fem.petsc.LinearProblem(A_u, L_u, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
# Solve
uh = problem_u.solve()
The error I run to is the following:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
File ~/miniconda3/envs/fenicsx-env/lib/python3.13/site-packages/spyder_kernels/customize/utils.py:209, in exec_encapsulate_locals(code_ast, globals, locals, exec_fun, filename)
207 if filename is None:
208 filename = "<stdin>"
--> 209 exec_fun(compile(code_ast, filename, "exec"), globals, None)
210 finally:
211 if use_locals_hack:
212 # Cleanup code
File ~/Desktop/FEMSphere_v3.py:56
53 f_u = 1.
54 L_u = dolfinx.fem.assemble_vector(dolfinx.fem.form(inner(f_u, v_test_u)*dx))
---> 56 problem_u = dolfinx.fem.petsc.LinearProblem(A_u, L_u, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
58 # Solve
59 uh = problem_u.solve()
File ~/miniconda3/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/petsc.py:788, in LinearProblem.__init__(self, a, L, bcs, u, petsc_options, form_compiler_options, jit_options)
754 """Initialize solver for a linear variational problem.
755
756 Args:
(...)
780 "mumps"})
781 """
782 self._a = _create_form(
783 a,
784 dtype=PETSc.ScalarType,
785 form_compiler_options=form_compiler_options,
786 jit_options=jit_options,
787 )
--> 788 self._A = create_matrix(self._a)
789 self._L = _create_form(
790 L,
791 dtype=PETSc.ScalarType,
792 form_compiler_options=form_compiler_options,
793 jit_options=jit_options,
794 )
795 self._b = create_vector(self._L)
File ~/miniconda3/envs/fenicsx-env/lib/python3.13/site-packages/dolfinx/fem/petsc.py:176, in create_matrix(a, mat_type)
160 """Create a PETSc matrix that is compatible with a bilinear form.
161
162 Note:
(...)
173 A PETSc matrix with a layout that is compatible with ``a``.
174 """
175 if mat_type is None:
--> 176 return _cpp.fem.petsc.create_matrix(a._cpp_object)
177 else:
178 return _cpp.fem.petsc.create_matrix(a._cpp_object, mat_type)
AttributeError: 'petsc4py.PETSc.Mat' object has no attribute '_cpp_object'
I’ve tried to use some other objects such as CSR Matrices, but seems that the only objects that the solver can use is ‘petsc4py.PETSc.Mat’. I did not see this question posted anywhere, so I thought on asking myself.