Hello,
I have been working on a pipeline to combine custom kernels with custom assembler. As a part of sanity tests, I am trying to manually compile a standard UFL formulation using ffcx_jit, and then to import it and assemble. However, apparently I am doing something incorrectly, because I receive the PETSC runtime SEGV error. The minimal code is the following:
import numpy
import dolfinx
import ufl
import numba.core.typing.cffi_utils as cffi_support
from dolfinx.fem import Function, functionspace
from dolfinx.fem import (dirichletbc, locate_dofs_geometrical)
from dolfinx.fem.petsc import assemble_matrix as petsc_assemble_matrix
from dolfiny.function import unroll_dofs
from dolfinx.jit import ffcx_jit
from mpi4py import MPI
mesh = dolfinx.mesh.create_box(MPI.COMM_WORLD, [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]], [2,2,2])
V = functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim, )))
u = Function(V)
u_left = Function(V)
u_right = Function(V)
left_dofs = locate_dofs_geometrical(V, lambda x: numpy.isclose(x[0], 0.0))
right_dofs = locate_dofs_geometrical(V, lambda x: numpy.isclose(x[0], 1.0))
bcs = [dirichletbc(u_left, left_dofs), dirichletbc(u_right, right_dofs)]
# Trial and test functions
du = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
# Functions
u = Function(V)
# Kinematics
d = 3
Id = ufl.Identity(d)
F = Id + ufl.grad(u)
C = F.T * F
# Invariants of deformation tensors
Ic = ufl.tr(C)
J = ufl.det(F)
# Elasticity parameters
E = 10.0
nu = 0.3
mu = E / (2 * (1 + nu))
lmbda = E * nu / ((1 + nu) * (1 - 2 * nu))
# Stored strain energy density (compressible neo-Hookean model)
psi = (mu / 2) * (Ic - 3) - mu * ufl.ln(J) + (lmbda / 2) * (J - 1) ** 2
# Total potential energy
Pi = psi * ufl.dx(mesh, metadata={"quadrature_degree": 2})
# First variation of Pi (directional derivative about u in the direction of v)
R = -ufl.derivative(Pi, u, v)
# Compute Jacobian of F
K = -ufl.derivative(R, u, du)
ufc_form_K, module_K, code_k = ffcx_jit(mesh.comm, K)
ffc_K_ptr = module_K.ffi.addressof(ufc_form_K)
# Prepare dolfinx.Form based on provided kernels
cells = numpy.arange(mesh.topology.index_map(mesh.topology.dim).size_local, dtype=numpy.int32)
ptrA = module_K.ffi.cast("intptr_t", ffc_K_ptr)
integrals_a = {dolfinx.fem.IntegralType.cell: [(-1, ptrA, cells)]}
_a = dolfinx.cpp.fem.Form_float64([V._cpp_object, V._cpp_object], integrals_a, [], [], False, {}, None)
a = dolfinx.fem.Form(_a)
A_dolfinx_asm = petsc_assemble_matrix(a, bcs)
Thank you in advance for helping me to understand where my mistake is.