FEniCS / ffcx

Next generation FEniCS Form Compiler for finite element forms
https://fenicsproject.org
Other
152 stars 41 forks source link

Compiling certain forms multiple times fails #551

Closed jpdean closed 1 year ago

jpdean commented 1 year ago

The following code

import ufl
import ffcx.codegeneration.jit

for i in range(10):
    print("i = ", i)
    fe = ufl.FiniteElement("Lagrange", ufl.triangle, 1)
    f = ufl.Coefficient(fe)
    g = ufl.Coefficient(fe)  # Commenting this line fixes the issue

    forms = [f * ufl.dx]

    compiled_forms, module, code = ffcx.codegeneration.jit.compile_forms(
        forms, options={"scalar_type": "double"})

fails after five loops with the error

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
  999 | static ufcx_function_space functionspace_w_{10} =

from the compiler. I'm not sure why the generated code is being incorrectly written. Strangely, the error does not occur if the line g = ufl.Coefficient(fe) is removed, despite g not being in the form.

chrisrichardson commented 1 year ago

Yes, this is occuring because the coefficient number has gone above 9. For whatever reason (maybe LaTeX compatibility) the coefficients are labelled w_1, w_2, ... w_9, w_{10}, w_{11}... etc. and the curly brackets are confusing the python string formatting. I guess this can be worked around in ffcx by scrubbing the coefficient name before using it. But maybe the problem originates in ufl.

chrisrichardson commented 1 year ago

https://github.com/FEniCS/ufl/tree/chris/coeff-name

chrisrichardson commented 1 year ago

Fixed by FEniCS/ufl#142