Error occuring in fem.form

Dear all,

I recently bought a new computer and installed fenicsx (0.6.0) on WSL2 (Ubuntu) through anaconda.
I tried to run some old code on my new computer (same version of fenicsx as before), and while the first time it ran, it ran fine, but the second time when I changed a variable in my fem.form (e.g. dt) I got an error. The code I used here to generate the error is just an adapted version of the heat equation tutorial (I know it can be done more efficiently as is done in the real tutorial, but I have the same error for code where both the bilinear and linear form are time-dependent so this is the easiest similar case I could come up with. Update: I also get the exact same error when completely copy-pasting the tutorial):

import numpy as np

from mpi4py import MPI
from petsc4py import PETSc

from dolfinx import fem, mesh, io, plot

# Define temporal parameters
t = 0 # Start time
T = 1.0 # Final time
num_steps = 50     
dt = T / num_steps # time step size

# Define mesh
nx, ny = 50, 50
domain = mesh.create_rectangle(MPI.COMM_WORLD, [np.array([-2, -2]), np.array([2, 2])], 
                               [nx, ny], mesh.CellType.triangle)
V = fem.FunctionSpace(domain, ("CG", 1))

# Create initial condition
def initial_condition(x, a=5):
    return np.exp(-a*(x[0]**2+x[1]**2))
u_n = fem.Function(V)
u_n.name = "u_n"
u_n.interpolate(initial_condition)

# Create boundary condition
fdim = domain.topology.dim - 1
boundary_facets = mesh.locate_entities_boundary(
    domain, fdim, lambda x: np.full(x.shape[1], True, dtype=bool))
bc = fem.dirichletbc(PETSc.ScalarType(0), fem.locate_dofs_topological(V, fdim, boundary_facets), V)

xdmf = io.XDMFFile(domain.comm, "diffusion.xdmf", "w")
xdmf.write_mesh(domain)

# Define solution variable, and interpolate initial solution for visualization in Paraview
uh = fem.Function(V)
uh.name = "uh"
uh.interpolate(initial_condition)
xdmf.write_function(uh, t)


import ufl
u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
f = fem.Constant(domain, PETSc.ScalarType(0))

for i in range(num_steps):
    t += dt

    a = u * v * ufl.dx + dt*ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx 
    L = (u_n + dt * f) * v * ufl.dx

    bilinear_form = fem.form(a)
    linear_form = fem.form(L)

    A = fem.petsc.assemble_matrix(bilinear_form, bcs=[bc])
    A.assemble()
    b = fem.petsc.create_vector(linear_form)
    fem.petsc.set_bc(b, [bc])
    b.assemble()

    solver = PETSc.KSP().create(domain.comm)
    solver.setOperators(A)
    solver.setType(PETSc.KSP.Type.PREONLY)
    solver.getPC().setType(PETSc.PC.Type.LU)

    # Solve linear problem
    solver.solve(b, uh.vector)

    # Update solution at previous time step (u_n)
    u_n.x.array[:] = uh.x.array

    # Write solution to file
    xdmf.write_function(uh, t)
 
xdmf.close()


The error I got out is the following:

INFO:root:running build_ext
INFO:root:building 'libffcx_forms_1790fb3e8ead4c2bdbfebd6a45e3be2c80395974' extension
INFO:root:/home/rsmeets/anaconda3/envs/fenicsx/bin/x86_64-conda-linux-gnu-cc -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -fPIC -O2 -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -march -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/rsmeets/anaconda3/envs/fenicsx/include -DNDEBUG -D_FORTIFY_SOURCE -fPIC -I/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/ffcx/codegeneration -I/home/rsmeets/anaconda3/envs/fenicsx/include/python3.10 -c libffcx_forms_1790fb3e8ead4c2bdbfebd6a45e3be2c80395974.c -o ./libffcx_forms_1790fb3e8ead4c2bdbfebd6a45e3be2c80395974.o -O2 -g0
x86_64-conda-linux-gnu-cc: error: unrecognized command-line option '-march'
Traceback (most recent call last):
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/unixccompiler.py", line 185, in _compile
    self.spawn(compiler_so + cc_args + [src, '-o', obj] + extra_postargs)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/ccompiler.py", line 1041, in spawn
    spawn(cmd, dry_run=self.dry_run, **kwargs)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/spawn.py", line 70, in spawn
    raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/home/rsmeets/anaconda3/envs/fenicsx/bin/x86_64-conda-linux-gnu-cc' failed with exit code 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/cffi/ffiplatform.py", line 51, in _build
    dist.run_command('build_ext')
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/dist.py", line 989, in run_command
    super().run_command(command)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 988, in run_command
    cmd_obj.run()
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 88, in run
    _build_ext.run(self)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 345, in run
    self.build_extensions()
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 467, in build_extensions
    self._build_extensions_serial()
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 493, in _build_extensions_serial
    self.build_extension(ext)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 249, in build_extension
    _build_ext.build_extension(self, ext)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 548, in build_extension
    objects = self.compiler.compile(
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/ccompiler.py", line 600, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/setuptools/_distutils/unixccompiler.py", line 187, in _compile
    raise CompileError(msg)
distutils.errors.CompileError: command '/home/rsmeets/anaconda3/envs/fenicsx/bin/x86_64-conda-linux-gnu-cc' failed with exit code 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rsmeets/coding/biolin/minworkexample.py", line 53, in <module>
    bilinear_form = fem.form(a)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 176, in form
    return _create_form(form)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 171, in _create_form
    return _form(form)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 145, in _form
    ufcx_form, module, code = jit.ffcx_jit(mesh.comm, form,
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/jit.py", line 56, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/jit.py", line 204, in ffcx_jit
    r = ffcx.codegeneration.jit.compile_forms([ufl_object], options=p_ffcx, **p_jit)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/ffcx/codegeneration/jit.py", line 186, in compile_forms
    impl = _compile_objects(decl, forms, form_names, module_name, p, cache_dir,
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/ffcx/codegeneration/jit.py", line 270, in _compile_objects
    ffibuilder.compile(tmpdir=cache_dir, verbose=True, debug=cffi_debug)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/cffi/api.py", line 725, in compile
    return recompile(self, module_name, source, tmpdir=tmpdir,
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/cffi/recompiler.py", line 1564, in recompile
    outputfilename = ffiplatform.compile('.', ext,
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/cffi/ffiplatform.py", line 22, in compile
    outputfilename = _build(tmpdir, ext, compiler_verbose, debug)
  File "/home/rsmeets/anaconda3/envs/fenicsx/lib/python3.10/site-packages/cffi/ffiplatform.py", line 58, in _build
    raise VerificationError('%s: %s' % (e.__class__.__name__, e))
cffi.VerificationError: CompileError: command '/home/rsmeets/anaconda3/envs/fenicsx/bin/x86_64-conda-linux-gnu-cc' failed with exit code 1

I think the real error is in the third section and thus seems to have to do something with fem.form (I see these first two blocks almost always when there is an error with the third block). I have the same error with other code where I want to change parameters in fem.form. Has anyone encountered this problem before and/or knows how to solve it? I have tried deleting the environment with fenicsx and the package, and reinstalling but that did not work unfortunately.

Many thanks,
Robin

1 Like

I am sorry, I know that this may not be a big help, but the code runs without error on my PC via ubuntu WSL2 in Windows 11, everything in latest versions.

1 Like

Hi Belmont, thanks for replying :slight_smile:
Iā€™m pretty sure I have the exact same (Windows 11, WSL2, Ubuntu, latest versions), which is the more reason Iā€™m confused of what is happening haha

I ran your code in a the dolfinx tutorial docker image.
It is just a few commands and one can easily start using dolfinx.

docker pull ghcr.io/jorgensd/dolfinx-tutorial:v0.6.0.post0

Once the pull is complete, run the following command

docker run -ti -p 8888:8888 -v "$(pwd)":/root/ ghcr.io/jorgensd/dolfinx-tutorial:v0.6.0.post0
  1. Open a browser and access jupyter-lab where you can create a new notebook, paste your code and run it.
http://127.0.0.1:8888/

Your code runs without giving the errors that you mention.

1 Like

Hi Ampdes,
Thank you for your reply :slight_smile: . On my old laptop I did work through docker and never had this issue, and so I absolutely believe that this would work. But it should work on Windows 11 through Ubuntu WSL2 and anaconda as well right?

Iā€™ve also been having a similar issue, but I have no idea what is happening. Seems to have started with the installation of dolfinx v0.6.0 when I upgraded from v0.5.x

Iā€™m running dolfinx in a conda environment (can provide full package list if it is helpful) and working in VSCode. I was able to reproduce your error message once, but then if I re-run the exact same script in ā€œdebug modeā€, the error goes away for subsequent runs. How in the world does that happen? (Iā€™ve seen this happen for a few different scripts):

INITIAL RUN ERROR MESSAGE:

(MY_ENV) (base) usr@MACHINE:~/DIR$ /home/usr/anaconda3/envs/MY_ENV/bin/python "/home/usr/DIR/MWEfrompreviouspost.py"
x86_64-conda-linux-gnu-cc: error: unrecognized command-line option '-march'
Traceback (most recent call last):
  File "/home/usr/anaconda3/envs/MY_ENV/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 "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/_distutils/ccompiler.py", line 1007, in spawn
    spawn(cmd, dry_run=self.dry_run, **kwargs)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/_distutils/spawn.py", line 70, in spawn
    raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/home/usr/anaconda3/envs/MY_ENV/bin/x86_64-conda-linux-gnu-cc' failed with exit code 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/cffi/ffiplatform.py", line 51, in _build
    dist.run_command('build_ext')
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/dist.py", line 1217, in run_command
    super().run_command(command)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
    cmd_obj.run()
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 84, in run
    _build_ext.run(self)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 346, in run
    self.build_extensions()
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 466, in build_extensions
    self._build_extensions_serial()
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 492, in _build_extensions_serial
    self.build_extension(ext)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 246, in build_extension
    _build_ext.build_extension(self, ext)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 547, in build_extension
    objects = self.compiler.compile(
  File "/home/usr/anaconda3/envs/MY_ENV/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 "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/setuptools/_distutils/unixccompiler.py", line 188, in _compile
    raise CompileError(msg)
distutils.errors.CompileError: command '/home/usr/anaconda3/envs/MY_ENV/bin/x86_64-conda-linux-gnu-cc' failed with exit code 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/usr/DIR/MWEfrompreviouspost.py", line 18, in <module>
    V = fem.FunctionSpace(domain, ("CG", 1))
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/dolfinx/fem/function.py", line 487, in __init__
    (self._ufcx_element, self._ufcx_dofmap), module, code = jit.ffcx_jit(
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/dolfinx/jit.py", line 56, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/dolfinx/jit.py", line 206, in ffcx_jit
    r = ffcx.codegeneration.jit.compile_elements([ufl_object], options=p_ffcx, **p_jit)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/ffcx/codegeneration/jit.py", line 144, in compile_elements
    impl = _compile_objects(decl, elements, names, module_name, p, cache_dir,
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/ffcx/codegeneration/jit.py", line 270, in _compile_objects
    ffibuilder.compile(tmpdir=cache_dir, verbose=True, debug=cffi_debug)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/cffi/api.py", line 725, in compile
    return recompile(self, module_name, source, tmpdir=tmpdir,
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/cffi/recompiler.py", line 1564, in recompile
    outputfilename = ffiplatform.compile('.', ext,
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/cffi/ffiplatform.py", line 22, in compile
    outputfilename = _build(tmpdir, ext, compiler_verbose, debug)
  File "/home/usr/anaconda3/envs/MY_ENV/lib/python3.10/site-packages/cffi/ffiplatform.py", line 58, in _build
    raise VerificationError('%s: %s' % (e.__class__.__name__, e))
cffi.VerificationError: CompileError: command '/home/usr/anaconda3/envs/MY_ENV/bin/x86_64-conda-linux-gnu-cc' failed with exit code 1

Subsequent runs AFTER RUNNING IN DEBUG MODE, do not throw this same error. Very weird.

I also have a more complicated package that seems to keep throwing a similar error. The error seems to show up with the assembly of the RHS petsc vector, regardless of what that form looks like and even for forms that are assembled in a similar ā€œspaghetti codeā€ and run without throwing this error.

The error seems to show up at the creation of the RHS petsc vector

#...... SOME CODE ABOVE
self.b=create_vector(form(self.L_form))
#MORE CODE.....
1 Like

Thank you! That seems indeed awfully similar to my error under the same circumstances. Does running it in debug mode fix it? And if so, how do you run in debug mode? :slight_smile:

Very strange, but seriously, all Iā€™m doing is this after the error:

Iā€™m not necessarily proposing this as a ā€œfixā€, but just noting it as something Iā€™ve seen so far to help track down whatā€™s going on.

1 Like

This post also had some useful information. I noticed that I was having issues even importing some packages and certain code was throwing a PETSc Segmentation violation error in debug mode like this:

[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see https://petsc.org/release/faq/#valgrind and https://petsc.org/release/faq/
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run 
[0]PETSC ERROR: to get more information on the crash.
[0]PETSC ERROR: Run with -malloc_debug to check if memory corruption is causing the crash.
Abort(59) on node 0 (rank 0 in comm 0): application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0

The solution in the linked post helped me get rid of my last ā€˜ComplieErrorā€™.

1 Like

Thank you. I unfortunately did not manage to fix it and just decided to install docker and run it through there and that works. It must be something that goes wrong when installing it through anaconda but as it is all done automatically, Iā€™m not quite what happens.

In any way, for those encountering the same error and who are looking for a solution, my best advice right now is to just run it through docker

e.g. in powershell run the command

docker run --rm -ti -v [path of your wanted directory]:[place holder name directory] dolfinx/dolfinx:stable

and then run the command:
cd [place holder name directory]

In my case that looks like:

docker run --rm -ti -v \\wsl.localhost\Ubuntu\home\rsmeets\coding:/opt/project dolfinx/dolfinx:stable
cd /opt/project

Ofcourse you should install docker to do this, but it is quite easy and straightforward.

tldr: no fix, try docker