FEniCS / dolfinx

Next generation FEniCS problem solving environment
https://fenicsproject.org
GNU Lesser General Public License v3.0
731 stars 177 forks source link

ffcx for Function name string including spaces #2396

Closed cmaurini closed 1 year ago

cmaurini commented 1 year ago

Function name strings including spaces leads to a ffcx error and a strange error message. See the MWE below.

I have this issue with version 0.5.1

from dolfinx import fem, mesh
from ufl import dx
from mpi4py import MPI

msh = mesh.create_rectangle(
    comm=MPI.COMM_WORLD,
    points=((0.0, 0.0), (2.0, 1.0)),
    n=(1, 1),
    cell_type=mesh.CellType.triangle,
)
V = fem.FunctionSpace(msh, ("Lagrange", 1))
u_1 = fem.Function(V,name="without_space")
u_2 = fem.Function(V,name="with space")

# Works
fem.assemble_scalar(fem.form(2 * u_1 * dx))

# Does not work, with a incomprehensible error message
fem.assemble_scalar(fem.form(3 * u_2 * dx))
jhale commented 1 year ago

Could you post the error message?

On a broader note, we should perhaps throw an exception if string contains spaces...

cmaurini commented 1 year ago

Yes I think that a clear exception would be perfect. It is a bad idea to put spaces in names, but it it may happen. That's the second time I take 20 mins to figure out the issue in student codes. Here the error message.


libffcx_forms_04c39657d35cd0902d6ef26b99320c5d1fd53c04.c:1116:46: error: expected ';' at end of declaration
static ufcx_function_space functionspace_with space =
                                             ^
                                             ;
libffcx_forms_04c39657d35cd0902d6ef26b99320c5d1fd53c04.c:1126:29: error: expected ';' after return statement
  return &functionspace_with space;
                            ^
                            ;
2 errors generated.
Traceback (most recent call last):
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/unixccompiler.py", line 186, in _compile
    self.spawn(compiler_so + cc_args + [src, '-o', obj] + extra_postargs)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/ccompiler.py", line 1007, in spawn
    spawn(cmd, dry_run=self.dry_run, **kwargs)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/spawn.py", line 70, in spawn
    raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/opt/homebrew/bin/clang' failed with exit code 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/cffi/ffiplatform.py", line 51, in _build
    dist.run_command('build_ext')
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/dist.py", line 1217, in run_command
    super().run_command(command)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
    cmd_obj.run()
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 84, in run
    _build_ext.run(self)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 346, in run
    self.build_extensions()
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 466, in build_extensions
    self._build_extensions_serial()
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 492, in _build_extensions_serial
    self.build_extension(ext)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 246, in build_extension
    _build_ext.build_extension(self, ext)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 547, in build_extension
    objects = self.compiler.compile(
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/ccompiler.py", line 599, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/setuptools/_distutils/unixccompiler.py", line 188, in _compile
    raise CompileError(msg)
distutils.errors.CompileError: command '/opt/homebrew/bin/clang' failed with exit code 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/maurini/Documents/codes/FFT-FEM/test.py", line 19, in <module>
    fem.assemble_scalar(fem.form(3 * u_2 * dx))
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 166, in form
    return _create_form(form)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 161, in _create_form
    return _form(form)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 135, in _form
    ufcx_form, module, code = jit.ffcx_jit(mesh.comm, form,
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/dolfinx/jit.py", line 56, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/dolfinx/jit.py", line 204, in ffcx_jit
    r = ffcx.codegeneration.jit.compile_forms([ufl_object], parameters=p_ffcx, **p_jit)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/ffcx/codegeneration/jit.py", line 168, in compile_forms
    impl = _compile_objects(decl, forms, form_names, module_name, p, cache_dir,
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/ffcx/codegeneration/jit.py", line 252, in _compile_objects
    ffibuilder.compile(tmpdir=cache_dir, verbose=True, debug=cffi_debug)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/cffi/api.py", line 725, in compile
    return recompile(self, module_name, source, tmpdir=tmpdir,
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/cffi/recompiler.py", line 1564, in recompile
    outputfilename = ffiplatform.compile('.', ext,
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/cffi/ffiplatform.py", line 22, in compile
    outputfilename = _build(tmpdir, ext, compiler_verbose, debug)
  File "/Users/maurini/opt/anaconda3/envs/fenicsx-0.5.1/lib/python3.10/site-packages/cffi/ffiplatform.py", line 58, in _build
    raise VerificationError('%s: %s' % (e.__class__.__name__, e))
cffi.VerificationError: CompileError: command '/opt/homebrew/bin/clang' failed with exit code 1```
jorgensd commented 1 year ago

Could you post the error message?

On a broader note, we should perhaps throw an exception if string contains spaces...

I think it is a general mistake that we use the function names in the generated code.

As we renumber the coefficients in the signature, we should just use this number, i.e. f_0, f_1 etc

cmaurini commented 1 year ago

yet, it is useful when one try to look at the generated code

michalhabera commented 1 year ago

See https://github.com/FEniCS/ffcx/blob/main/ffcx/codegeneration/form.py#L111, which means function names must be a valid C identifier.

For forms, it is not really required we put function names into C object names, since we fetch them anyway with a string, but this is in place for expressions. Anyway, I think, as the code suggests, ufcx_function_space generation should be rewritten. I just made a simple check, https://github.com/FEniCS/ffcx/pull/539

garth-wells commented 1 year ago

@michalhabera does https://github.com/FEniCS/ffcx/pull/539 resolve this issue?

michalhabera commented 1 year ago

Yes, in a sense that an exception saying "Function name {name} must be a valid object identifier." will be thrown from ffcx.