# Dashes in jit compiled functionspace name

**URL:** https://fenicsproject.discourse.group/t/dashes-in-jit-compiled-functionspace-name/6710
**Category:** Errors
**Created:** [September 29, 2021, 4:29pm UTC](https://fenicsproject.discourse.group/t/dashes-in-jit-compiled-functionspace-name/6710 "2021-09-29T16:29:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MatsievskiySV](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MatsievskiySV](https://fenicsproject.discourse.group/u/MatsievskiySV)
#### Post date: [September 29, 2021, 4:29pm UTC](https://fenicsproject.discourse.group/t/dashes-in-jit-compiled-functionspace-name/6710/1 "2021-09-29T16:29:26Z")

</div>

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

```auto
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

```auto
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

```bash
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}.

---

<div class="post-metadata">

### Author: ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)
#### Post date: [September 29, 2021, 6:01pm UTC](https://fenicsproject.discourse.group/t/dashes-in-jit-compiled-functionspace-name/6710/2 "2021-09-29T18:01:38Z")

</div>

The modification

```python
#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.

---

<div class="post-metadata">

### Author: ![IgorBaratta](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/igorbaratta/32/579_2.png) [@IgorBaratta](https://fenicsproject.discourse.group/u/IgorBaratta)
#### Post date: [September 29, 2021, 6:12pm UTC](https://fenicsproject.discourse.group/t/dashes-in-jit-compiled-functionspace-name/6710/3 "2021-09-29T18:12:36Z")

</div>

There’s an open issue on dolfinx

> <https://github.com/FEniCS/dolfinx/issues/1577>
>
> \`F1.split()\` and \`ufl.split(F1)\` differ in behavior during JIT compilation. 
> 
> …A minimal working example is shown below.
> \`\`\`python3
> import ufl
> import dolfinx
> from mpi4py import MPI
> 
> comm = MPI.COMM\_WORLD
> 
> mesh = dolfinx.UnitSquareMesh(comm, 5, 5)
> 
> \# Define problem function space
> FE = ufl.FiniteElement("Lagrange", ufl.triangle, 1)
> ME = ufl.MixedElement(\[FE, FE\])
> VQ = dolfinx.FunctionSpace(mesh, ME)
> 
> \# Create dolfinx function
> F1 = dolfinx.Function(VQ)
> \`\`\`
> 
> The following code fails to compile:
> \`\`\`python3
> u1, v1 = F1.split()
> M = ufl.inner(u1, u1) \* ufl.dx
> cpp\_form = dolfinx.fem.Form(M)
> \`\`\`
> \`u1.\_\_str\_\_() = 'f\_2-0'\` which is not suitable for jit compilation.
> \`\`\`bash
> libffcx\_forms\_97fce81b4941ed21178dfa25742d8014a024a0cb.c: In function ‘functionspace\_form\_libffcx\_forms\_97fce81b4941ed21178dfa25742d8014a024a0cb\_0’:
> libffcx\_forms\_97fce81b4941ed21178dfa25742d8014a024a0cb.c:874:44: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘\_\_attribute\_\_’ before ‘-’ token
> 874 | static ufc\_function\_space functionspace\_f\_2-0 =
> | ^
> libffcx\_forms\_97fce81b4941ed21178dfa25742d8014a024a0cb.c:875:1: error: expected expression before ‘{’ token
> \`\`\`
> 
> 
> However the following code works:
> \`\`\`python3
> u1, v1 = ufl.split(F1)
> M = ufl.inner(u1, u1) \* ufl.dx
> cpp\_form = dolfinx.fem.Form(M)
> \`\`\`
> \`u1.\_\_str\_\_() = f\_2\[0\]\`

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

---

<div class="post-metadata">

### Author: ![MatsievskiySV](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MatsievskiySV](https://fenicsproject.discourse.group/u/MatsievskiySV)
#### Post date: [September 29, 2021, 7:08pm UTC](https://fenicsproject.discourse.group/t/dashes-in-jit-compiled-functionspace-name/6710/4 "2021-09-29T19:08:20Z")

</div>

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

```python
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)

```

---

<div class="post-metadata">

### Author: ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)
#### Post date: [September 29, 2021, 7:27pm UTC](https://fenicsproject.discourse.group/t/dashes-in-jit-compiled-functionspace-name/6710/5 "2021-09-29T19:27:14Z")

</div>

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

```python
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 `Form`s in UFL and `phi_re_out` and `phi_im_out` for file output.

---

<div class="post-metadata">

### Author: ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)
#### Post date: [September 29, 2021, 8:51pm UTC](https://fenicsproject.discourse.group/t/dashes-in-jit-compiled-functionspace-name/6710/6 "2021-09-29T20:51:41Z")

</div>

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