Compile a form with ufl.Coefficients

I would like to compile a form and provide the functions to be assembled later on at assembly time. Is this possible? I read from the ufl documentation that I should use Coefficient but I can’t compile a form with coefficients.

MWE:

from dolfinx import fem, mesh, cpp
import ufl
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

points_side = 4
domain = mesh.create_unit_square(comm, points_side, points_side,
                               mesh.CellType.quadrilateral,
                               )
v = fem.functionspace(domain, ("Lagrange", 1))
f   = fem.Function(v)
c = ufl.Coefficient(v) # will be f later on
l_ufl = c*c*ufl.dx
l = fem.form(l_ufl) # AttributeError: 'Coefficient' object has no attribute '_cpp_object'

This is currently being worked on in Data independent form compilation for Python interface by jorgensd · Pull Request #3263 · FEniCS/dolfinx · GitHub

For the time being, just use f instead of c, and update the values stored in f later on in the code.

1 Like