Dashes in jit compiled functionspace name

This is a MWE of the program that produces jit C++ code with dashes in Functionspace name.

import dolfinx
import dolfinx.io
from mpi4py import MPI
import ufl
from ufl import inner, grad, dx
import numpy as np

mesh = dolfinx.UnitSquareMesh(MPI.COMM_WORLD, 100, 100)
mesh.topology.create_connectivity(mesh.topology.dim-1,
                                  mesh.topology.dim)

Hcurl = ufl.FiniteElement("Nedelec 1st kind H(curl)", mesh.ufl_cell(), 1)
H1 = ufl.FiniteElement("Lagrange", mesh.ufl_cell(), 1)

V = dolfinx.FunctionSpace(mesh, H1)
u, v = ufl.TrialFunctions(V), ufl.TestFunctions(V)
f = dolfinx.Function(V)
f.interpolate(lambda x: x[0]**2 + 0*x[1])
f.x.scatter_forward()
u_bc = dolfinx.Function(V)

bc_facets = np.where(
    np.array(dolfinx.cpp.mesh.compute_boundary_facets(mesh.topology)) == 1)[0]
bc_dofs = dolfinx.fem.locate_dofs_topological(V,
                                              mesh.topology.dim-1,
                                              bc_facets)

with u_bc.vector.localForm() as loc:
    loc.setValues(bc_dofs, np.full(len(bc_dofs), 0))
bc = dolfinx.DirichletBC(u_bc, bc_dofs)

V = dolfinx.FunctionSpace(mesh, ufl.MixedElement(H1, H1))
phi_re, phi_im = ufl.TrialFunctions(V)
v_re, v_im = ufl.TestFunctions(V)

a_p = 0
a_p += inner(grad(v_re), grad(phi_re)) * dx
a_p += inner(v_re, phi_re) * dx
a_p += inner(grad(v_im), grad(phi_im)) * dx
a_p += inner(v_im, phi_im) * dx
L_p = inner(v_re, f) * dx

solution = dolfinx.fem.LinearProblem(a_p, L_p, bcs=[bc],
                                       petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
phi_re, phi_im = solution.solve().split()

V = dolfinx.FunctionSpace(mesh, ufl.VectorElement(H1, degree=2))
u, v = ufl.TrialFunction(V), ufl.TestFunction(V)

a_p = inner(u, v) * dx
L_p = inner(v, grad(phi_re)) * dx

solution = dolfinx.fem.LinearProblem(a_p, L_p, bcs=[bc],
                                       petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
solution.solve()

This code throws error

libffcx_forms_0654d9cebf8e356abd5838dcfa5a86679b549387.c:1002:44: error: expect
ed ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘-’ token
 1002 | static ufc_function_space functionspace_f_5-0 =
      |                                            ^
libffcx_forms_0654d9cebf8e356abd5838dcfa5a86679b549387.c:1003:1: error: expecte
d expression before ‘{’ token
 1003 | {
      | ^
libffcx_forms_0654d9cebf8e356abd5838dcfa5a86679b549387.c:1012:13: error: ‘funct
ionspace_f_5’ undeclared (first use in this function); did you mean ‘functionsp
ace_v_0’?
 1012 |     return &functionspace_f_5-0;
      |             ^~~~~~~~~~~~~~~~~
      |             functionspace_v_0

It looks like generated C++ variable name contains dash, which is interpreted as a minus sign.

Run this code using command

docker run -ti -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $(pwd):/root/shared -w /root/shared --rm dolfinx/dolfinx python3 mwe.py

Clarification: this program is a simplified version of a Maxwell equation solver for irrotational electric field. Program is solving the equation \nabla^2\Phi=f and then calculates the electric field from the equation -\nabla\Phi=\vec{E}_{div}.

The modification

#phi_re, phi_im = solution.solve().split()
phi_re, phi_im = ufl.split(solution.solve())

runs without error, although it still seems like there is a bug in FFCx that is triggered by the original MWE.

1 Like

There’s an open issue on dolfinx

For now, I’d recommend you to use the ufl.split function as suggested by @kamensky .

1 Like

Is there a way to write these functions to a file? The following fails:

phi_re, phi_im = ufl.split(solution.solve())

with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "out.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
    xdmf.write_function(phi_re)
    xdmf.write_function(phi_im)

Assuming the bug doesn’t affect file output, you could instead do something like

phi = solution.solve()
phi_re, phi_im = ufl.split(phi)
phi_re_out, phi_im_out = phi.split()

then use phi_re and phi_im to define Forms in UFL and phi_re_out and phi_im_out for file output.

1 Like

The bug should not affect file output, as ffcx is not involved there.

1 Like