Poisson’s equation in UFL#
This page illustrates how to:
Bring a problem written in plain UFL, as for the DOLFINx C++ interface, into sumfem with
sumfem.symbolic.UFLResidualBind it to a mesh with
sumfem.discrete.discretize()Solve it with a
sumfem.solvers.Solver
The problem#
We solve Poisson’s equation on the unit square \(\Omega\),
whose weak form is: find \(u\) with \(u = u_D\) on \(\partial\Omega\) such that
for all test functions \(v\) that vanish on \(\partial\Omega\). With \(u_D = 1 + x^2 + 2y^2\) and \(f = -6\), the exact solution is \(u = u_D\).
Three layers#
sumfem.symbolic describes domains, fields and residuals
symbolically, without mesh data, so a model is compiled once and solved on
any mesh. sumfem.discrete binds it to a mesh, and
sumfem.solvers solves it.
The model in plain UFL#
The UFL file of a DOLFINx C++ program writes the weak form, without data,
with a ufl.Mesh, a ufl.FunctionSpace on it, the
unknown as a ufl.Coefficient, a test function and the source as
a ufl.Constant:
cell = "triangle"
mesh = ufl.Mesh(element("Lagrange", cell, 1, shape=(2,)))
V = ufl.FunctionSpace(mesh, element("Lagrange", cell, 2))
uh = ufl.Coefficient(V)
v = ufl.TestFunction(V)
f = ufl.Constant(mesh)
F = ufl.inner(ufl.grad(uh), ufl.grad(v)) * ufl.dx - f * v * ufl.dx
UFLResidual turns the residual
into a sumfem model. The unknown becomes a field, and the constant a named
parameter of the model, "poisson.f", which can be set later. names gives
the variable name of each, and of the mesh, whose symbolic domain is then
"square". A Jacobian and a preconditioner can be given too; without them,
the Jacobian is derived.
The mesh#
So far, we have not considered an actual discrete domain: a
dolfinx.mesh.Mesh with its
MeshTags. In sumfem we store
these in a sumfem.discrete.MeshData, a dataclass that also names
the tags: for each entity dimension, a marker name maps to tag values. It is
created from locator functions, from existing mesh tags or from a Gmsh model,
see Meshes and their markers. Here the whole boundary is marked "Boundary" by a
locator, a function of the coordinates as
dolfinx.mesh.locate_entities() takes:
square = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 16, 16)
marked = discrete.MeshData.from_locators(
square, {1: {"Boundary": lambda x: np.full(x.shape[1], True)}}, boundary=True
)
Discretize and solve#
DomainMaps pairs each symbolic domain
with its MeshData, and discretize
creates one function for every field and parameter of the given components.
The result, a Discretization,
holds them all.
u = poisson.field
domain = u.domain
domain_maps = discrete.DomainMaps({domain: marked})
components = [poisson]
disc = discrete.discretize(components, domain_maps)
Parameters are set by object or by name:
disc.set({"poisson.f": -6.0})
Indexing the discretization gives the discrete counterpart of a symbolic
object: the DOLFINx function of a field, or the discrete system of a model,
compiled on first use. A DirichletBC
is given by the field, the marker name and the value, here a
UFL expression of the
spatial coordinate of the domain.
x = ufl.SpatialCoordinate(domain)
u_D = 1 + x[0] ** 2 + 2 * x[1] ** 2
bcs = [symbolic.DirichletBC(u, "Boundary", u_D)]
solver = solvers.Solver(disc[poisson], bcs)
solver.solve()
print(f"Newton iterations: {solver.iterations}")
Newton iterations: 1
Check the solution#
The exact solution is quadratic, so P2 elements reproduce it to round-off.
error takes the DOLFINx
function and the exact solution as a function of the mesh’s coordinates.
L2 error: 1.84e-14
The same symbolic model can be discretized again on another mesh, without writing it again; only the forms are compiled for the new mesh’s cell. A model as a component writes the same model as a component, which other models can extend.