Problem with VectorFunctionSpace in FEniCS

Hi everyone!

I had a old code in FEniCS and I need some plots from this code but it doesn`t work…

The code is:

from fenics import *
from mshr import *
import numpy as np
from dolfin import *
#from dolfin_adjoint import *
import matplotlib.pyplot as plt
from petsc4py import PETSc
import math
from mpi4py import MPI

# Create mesh
y_h=40
point_cy=320
channel = Rectangle(Point(-40.0, -y_h), Point(50.0, y_h))
cylinder = Circle(Point(0.0, 0.0), 0.5,point_cy)


fx=[]
fy=[]
cp=[]
nr=[]


   
domain = channel - cylinder
mesh = generate_mesh(domain, 35)
cell_markers = MeshFunction("bool", mesh, mesh.topology().dim())
for c in cells(mesh):
    if c.midpoint().y() < 9 and c.midpoint().y() > -9 and c.midpoint().x() < 24 and c.midpoint().x() > -15:
                cell_markers[c] = True
    else:
                cell_markers[c] = False

mesh = refine(mesh, cell_markers)

cell_markers = MeshFunction("bool", mesh, mesh.topology().dim())

for c in cells(mesh):
    
    if c.midpoint().y() < 3 and c.midpoint().y() > -3 and c.midpoint().x() < 12 and c.midpoint().x() > -9:
                
                cell_markers[c] = True
    else:
                cell_markers[c] = False
    

mesh = refine(mesh, cell_markers)
V = VectorFunctionSpace(mesh, 'CG', 2)

and the error is:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[5], line 1
----> 1 V = VectorFunctionSpace(mesh, 'CG', 1)
      2 u=Function(V)
      3 shape_numpy=np.load('shape_sensi.npy')

File /usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py:245, in VectorFunctionSpace(mesh, family, degree, dim, form_degree, constrained_domain, restriction)
    241 element = ufl.VectorElement(family, mesh.ufl_cell(), degree,
    242                             form_degree=form_degree, dim=dim)
    244 # Return (Py)DOLFIN FunctionSpace
--> 245 return FunctionSpace(mesh, element, constrained_domain=constrained_domain)

File /usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py:31, in FunctionSpace.__init__(self, *args, **kwargs)
     29     pass
     30 elif len(args) == 2:
---> 31     self._init_from_ufl(*args, **kwargs)
     32 else:
     33     self._init_convenience(*args, **kwargs)

File /usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py:42, in FunctionSpace._init_from_ufl(self, mesh, element, constrained_domain)
     39 ufl.FunctionSpace.__init__(self, mesh.ufl_domain(), element)
     41 # Compile dofmap and element
---> 42 ufc_element, ufc_dofmap = ffc_jit(element, form_compiler_parameters=None,
     43                                   mpi_comm=mesh.mpi_comm())
     44 ufc_element = cpp.fem.make_ufc_finite_element(ufc_element)
     46 # Create DOLFIN element and dofmap

File /usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py:50, in mpi_jit_decorator.<locals>.mpi_jit(*args, **kwargs)
     48 # Just call JIT compiler when running in serial
     49 if MPI.size(mpi_comm) == 1:
---> 50     return local_jit(*args, **kwargs)
     52 # Default status (0 == ok, 1 == fail)
     53 status = 0

File /usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py:98, in ffc_jit(ufl_form, form_compiler_parameters)
     93 @mpi_jit_decorator
     94 def ffc_jit(ufl_form, form_compiler_parameters=None):
     95 
     96     # Prepare form compiler parameters with overrides from dolfin and kwargs
     97     p = ffc.default_jit_parameters()
---> 98     p.update(dict(parameters["form_compiler"]))
     99     p.update(form_compiler_parameters or {})
    100     return ffc.jit(ufl_form, parameters=p)

File /usr/lib/petsc/lib/python3/dist-packages/dolfin/parameter/__init__.py:27, in __getitem__(self, key)
     23     return p
     24     # np = cpp.parameter.Parameters(p)
     25     # return np
     26 else:
---> 27     raise RuntimeError("Invalid parameter: {}".format(key))

RuntimeError: Invalid parameter: form_compiler

My version is 2019.2.0.dev0

Thank you so much!

How did you install fenics?

To me this seems related to: RuntimeError: Invalid parameter: form_compiler - #18 by dparsons

In this way:

sudo apt update
sudo apt install fenics

What Ubuntu version are you running? Pinging @dparsons as he is the master of these things.

Your code sample runs fine for me. Which debian version are you using? Have you updated packages recently? apt update; apt upgrade. Sometimes you need to push library upgrades with apt dist-upgrade.

I think I had problem because I have Fenicsx and fenics_legady in the same machine. I uninstalled fenics.

Now, I’m trying to run the code in this docker:

docker run -ti -p 127.0.0.1:8000:8000 -v $(pwd):/root/shared -w /root/shared ghcr.io/scientificcomputing/fenics-gmsh:2024-05-30

But now, Rectangle function dosen’t work for mesh generation

I think legacy fenics and dolfinx should be possible to use from apt on the same machine (Drew can correct me if I am wrong).

We do not ship mshr with the docker images, as mshr has been deprecated for over 5 years (and not been maintained).

OK, How can I modify these lines?:

y_h=40

point_cy=320

channel = Rectangle(Point(-40.0, -y_h), Point(50.0, y_h))

cylinder = Circle(Point(0.0, 0.0), 0.5,point_cy)

fx=[]

fy=[]

cp=[]

nr=[]

domain = channel - cylinder

mesh = generate_mesh(domain, 35)

cell_markers = MeshFunction("bool", mesh, mesh.topology().dim())

Thnak you

See for instance

or the equivalent with the gmsh api equivalent that is part of Test problem 2: Flow past a cylinder (DFG 2D-3 benchmark) — FEniCSx tutorial