Unknown ufl object type VectorElement

I have fenics 2019.1.0 version installed in ubuntu 22. I am running the Incompressible Navier-Stokes equations for channel flow (tutorial example), but getting the following error:
“Unknown ufl object type VectorElement”
which points to this line of code " V = VectorFunctionSpace(mesh, ‘P’, 2)"

Please what I’m I doing wrong?

Code:
from future import print_function

“import matplotlib.pyplot as plt”
from fenics import *
import numpy as np

T = 5.0 # final time
num_steps = 500 # number of time steps
dt = T / num_steps # time step size
mu = 1 # kinematic viscosity
rho = 1 # density

Create mesh and define function spaces

mesh = UnitSquareMesh(16, 16)
V = VectorFunctionSpace(mesh, ‘P’, 2)
Q = FunctionSpace(mesh, ‘P’, 1)

Define boundaries

inflow = ‘near(x[0], 0)’
outflow = ‘near(x[0], 1)’
walls = ‘near(x[1], 0) || near(x[1], 1)’

Define boundary conditions

bcu_noslip = DirichletBC(V, Constant((0, 0)), walls)

bcp_inflow = DirichletBC(Q, Constant(8), inflow)
bcp_outflow = DirichletBC(Q, Constant(0), outflow)

bcu = [bcu_noslip]

bcp = [bcp_inflow, bcp_outflow]

Define trial and test functions

u = TrialFunction(V)
v = TestFunction(V)
p = TrialFunction(Q)
q = TestFunction(Q)

Define functions for solutions at previous and current time steps

u_n = Function(V)
u_ = Function(V)
p_n = Function(Q)
p_ = Function(Q)

Define expressions used in variational forms

U = 0.5 * (u_n + u)
n = FacetNormal(mesh)
f = Constant((0, 0))
k = Constant(dt)
mu = Constant(mu)
rho = Constant(rho)

Define strain-rate tensor

def epsilon(u):
return sym(nabla_grad(u))

Define stress tensor

def sigma(u, p):
return 2 * mu * epsilon(u) - p * Identity(len(u))

Define variational problem for step 1

F1 = rho * dot((u - u_n) / k, v) * dx +
rho * dot(dot(u_n, nabla_grad(u_n)), v) * dx
+ inner(sigma(U, p_n), epsilon(v)) * dx
+ dot(p_n * n, v) * ds - dot(mu * nabla_grad(U) * n, v) * ds
- dot(f, v) * dx
a1 = lhs(F1)
L1 = rhs(F1)

Define variational problem for step 2

a2 = dot(nabla_grad(p), nabla_grad(q)) * dx
L2 = dot(nabla_grad(p_n), nabla_grad(q)) * dx - (1 / k) * div(u_) * q * dx

Define variational problem for step 3

a3 = dot(u, v) * dx
L3 = dot(u_, v) * dx - k * dot(nabla_grad(p_ - p_n), v) * dx

Assemble matrices

A1 = assemble(a1)
A2 = assemble(a2)
A3 = assemble(a3)

Apply boundary conditions to matrices

[bc.apply(A1) for bc in bcu]

[bc.apply(A2) for bc in bcp]

Time-stepping

t = 0
for n in range(num_steps):
# Update current time
t += dt

# Step 1: Tentative velocity step
b1 = assemble(L1)
# [bc.apply(b1) for bc in bcu]
solve(A1, u_.vector(), b1)

# Step 2: Pressure correction step
b2 = assemble(L2)
[bc.apply(b2) for bc in bcp]
solve(A2, p_.vector(), b2)

# Step 3: Velocity correction step
b3 = assemble(L3)
solve(A3, u_.vector(), b3)

# Plot solution
velocity = plot(u_)

# Compute error
u_e = Expression(('4*x[1]*(1.0 - x[1])', '0'), degree=2)
u_e = interpolate(u_e, V)
error = np.abs(np.array(u_e.vector()) - np.array(u_.vector())).max()
print('t = %.2f: error = %.3g' % (t, error))
print('max u:', np.array(u_.vector()).max())

# Update previous solution
u_n.assign(u_)
p_n.assign(p_)

Hold plot

plt.show()

interactive()

Thanks for helping in advance

How Did you install dolfin?

I installed it via command line using:
sudo apt install dolphin

Thanks.

I cannot reproduce your issue on a clean ubuntu 22.04 system:

 docker run -ti -v ${PWD}:/root/shared  --rm  ubuntu:22.04
apt-get update
apt-get install -y software-properties-common
add-apt-repository ppa:fenics-packages/fenics
apt-get install -y fenics
cd /root/shared/
cat mwe.py
python3 mwe55.py 

yielding

from fenics import *

mesh = UnitSquareMesh(16, 16)
V = VectorFunctionSpace(mesh, "P", 2)

and

 python3 -c "import dolfin; print(dolfin.__version__)"

yielding

2019.2.0.dev0

Please add full traceback of your error message, and please encapsulate all code or output as

```python
# add code here
```
```bash
Add terminal output here
```

Okay
Thank you very much, I tried with anaconda and it has worked now.
So thanks so much.

Hi I have a similar error:
I installed dolfin and

python3 -c "import dolfin; print(dolfin.__version__)"

yields

2019.2.0.dev0

but running the poisson.py from the tutorial:

"""
FEniCS tutorial demo program: Poisson equation with Dirichlet conditions.
Test problem is chosen to give an exact solution at all nodes of the mesh.

  -Laplace(u) = f    in the unit square
            u = u_D  on the boundary

  u_D = 1 + x^2 + 2y^2
    f = -6
"""

from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt

# Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)

# Define boundary condition
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)

def boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, u_D, boundary)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = dot(grad(u), grad(v))*dx
L = f*v*dx

# Compute solution
u = Function(V)
solve(a == L, u, bc)

# Plot solution and mesh
plot(u)
plot(mesh)

# Save solution to file in VTK format
vtkfile = File('poisson/solution.pvd')
vtkfile << u

# Compute error in L2 norm
error_L2 = errornorm(u_D, u, 'L2')

# Compute maximum error at vertices
vertex_values_u_D = u_D.compute_vertex_values(mesh)
vertex_values_u = u.compute_vertex_values(mesh)
import numpy as np
error_max = np.max(np.abs(vertex_values_u_D - vertex_values_u))

# Print errors
print('error_L2  =', error_L2)
print('error_max =', error_max)

# Hold plot
plt.show()

yields this output:

Unknown ufl object type FiniteElement
Traceback (most recent call last):
  File "/home/kb/Documents/fenics-tutorial/src/vol1/python/./poisson.py", line 18, in <module>
    V = FunctionSpace(mesh, 'P', 1)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 33, in __init__
    self._init_convenience(*args, **kwargs)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 100, in _init_convenience
    self._init_from_ufl(mesh, element, constrained_domain=constrained_domain)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 42, in _init_from_ufl
    ufc_element, ufc_dofmap = ffc_jit(element, form_compiler_parameters=None,
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py", line 50, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py", line 100, in ffc_jit
    return ffc.jit(ufl_form, parameters=p)
  File "/home/kb/.local/lib/python3.10/site-packages/ffc/jitcompiler.py", line 214, in jit
    kind, module_name = compute_jit_prefix(ufl_object, parameters)
  File "/home/kb/.local/lib/python3.10/site-packages/ffc/jitcompiler.py", line 156, in compute_jit_prefix
    error("Unknown ufl object type %s" % (ufl_object.__class__.__name__,))
  File "<string>", line 1, in <lambda>
  File "/home/kb/.local/lib/python3.10/site-packages/ufl/log.py", line 172, in error
    raise self._exception_type(self._format_raw(*message))
Exception: Unknown ufl object type FiniteElement

I would be thankful for any input.
Thanks and BR Philipp

1 Like

How Did you install dolfin?

i have same problem, using legacy fenics
code:

import random
from dolfin import *
from dolfin import FiniteElement, FunctionSpace

for i in range(1, 10):
mesh_file = f"result_{i}.xml"
domains_file = f"result_{i}physical_region.xml"
boundaries_file = f"result
{i}_facet_region.xml"

mesh = Mesh(mesh_file)
domains = MeshFunction("size_t", mesh, domains_file)
boundaries = MeshFunction("size_t", mesh, boundaries_file)

V = FunctionSpace(mesh, "Lagrange", 1)

g = Constant(0.0)

boundary_markers = boundaries.array()

boundary1_marker = 1

boundary2_marker = 2

boundary_subdomains = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
boundary_subdomains.set_all(0)
boundaries.array()[:] = boundary_subdomains.array()[:]

bc1 = DirichletBC(V, g, boundary_subdomains, boundary1_marker)
bc2 = DirichletBC(V, g, boundary_subdomains, boundary2_marker)

u = TrialFunction(V)
v = TestFunction(V)

k_expr = Expression("sin(x[0])*cos(x[1])", degree=2)
k = Function(V)
k.interpolate(k_expr)

f = Constant(1)
a = Constant(1) * inner(grad(u), grad(v)) * dx
L = f * v * dx

u_sol = Function(V)

k_val = random.uniform(-1, 1)
k_expr = Expression("sin(x[0])*cos(x[1]) * {}".format(k_val), degree=2)
k.interpolate(k_expr)
print("k = {}".format(k(0.5, 0.5)))

solve(a == L, u_sol, [bc1, bc2])

print("u = {}".format(u_sol(0.5, 0.5)))

file_u = File(f"u_{i}.pvd")
file_k = File(f"k_{i}.pvd")
file_u << u_sol
file_k << k

Error:

File /usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py:100, in FunctionSpace._init_convenience(self, mesh, family, degree, form_degree, constrained_domain, restriction) 93 def _init_convenience(self, mesh, family, degree, form_degree=None, 94 constrained_domain=None, restriction=None): 95 96 # Create UFL element 97 element = ufl.FiniteElement(family, mesh.ufl_cell(), degree, 98 form_degree=form_degree) → 100 self._init_from_ufl(mesh, element, constrained_domain=constrained_domain)

Exception: Unknown ufl object type FiniteElement

afaik I installed it that way:

sudo apt-get update
sudo apt-get install --no-install-recommends fenics

I’m working on a Ubuntu 22.04.2 LTS system

Why are you using

Is there an version inconsistency? What is the full traceback of your error message.

In the above

Uses a mixture of global and local packages.

So you would recommend to reinstall fenics?
Which way of installing is recommended?
I don’t have a preferred version.

Delete /home/kb/.local/lib/python3.10/site-packages. Never (ever) use pip to install package suites. It is unable to maintain version dependencies. Installation methods are summarised at GitHub - FEniCS/dolfinx: Next generation FEniCS problem solving environment.

sudo apt-get install fenics fenicsx

this solved the issue, thank you so much!

Hi everyone,

I am currently trying to make fenics work, however I have the following consistent problem when I compile the demo poisson.py code :

hwloc/linux: Ignoring PCI device with non-16bit domain.
Pass --enable-32bits-pci-domain to configure to support such devices
(warning: it would break the library ABI, don’t enable unless really needed).
Unknown ufl object type FiniteElement

So I have the same problem with unknown ufl but also this problem linked to hwloc. Does someone have an idea on how to fix this ? I am on Ubuntu and tried to install fenics as described on the installation page with Ubuntu packages.

Thanks, have a nice day

Please state what version of FEniCS (legacy or DOLFINx) and what release of any of those versions you are using (20xy.z.w for legacy and x.y.z) for DOLFINx.

If I am not mistaking, it is the FEniCS legacy 2019.2.0.dev0 version I am using

Possibly you are trying to use new ufl with the legacy dolfin.

Make sure you access ufl_legacy instead,
cf. Announcement: ufl_legacy and legacy dolfin

But if you are starting a new project, consider using dolfinx instead.

Let us know if ufl_legacy fixes your issue.

I think ufl_legacy fixed one problem, thank you !

But, with the following code :

from __future__ import print_function
from fenics import *
from ufl_legacy import *
import matplotlib.pyplot as plt

# Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)

# Define boundary condition
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)

def boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, u_D, boundary)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = dot(grad(u), grad(v))*dx
L = f*v*dx

# Compute solution
u = Function(V)
solve(a == L, u, bc)

# Plot solution and mesh
plt.plot(u)
plt.plot(mesh)

# Save solution to file in VTK format
vtkfile = File('poisson/solution.pvd')
vtkfile << u

# Compute error in L2 norm
error_L2 = errornorm(u_D, u, 'L2')

# Compute maximum error at vertices
vertex_values_u_D = u_D.compute_vertex_values(mesh)
vertex_values_u = u.compute_vertex_values(mesh)
import numpy as np
error_max = np.max(np.abs(vertex_values_u_D - vertex_values_u))

# Print errors
print('error_L2  =', error_L2)
print('error_max =', error_max)

# Hold plot
plt.show()

I still have the following error :

hwloc/linux: Ignoring PCI device with non-16bit domain.
Pass --enable-32bits-pci-domain to configure to support such devices
(warning: it would break the library ABI, don't enable unless really needed).
hwloc/linux: Ignoring PCI device with non-16bit domain.
Pass --enable-32bits-pci-domain to configure to support such devices
(warning: it would break the library ABI, don't enable unless really needed).
Traceback (most recent call last):
  File "/home/juvanderhaeg/Documents/Projet python/fen_test.py", line 19, in <module>
    V = FunctionSpace(mesh, 'P', 1)
TypeError: FunctionSpace.__init__() takes 3 positional arguments but 4 were given

You should be really careful with wildcard imports. There is no reason (and dangerous) to have two packages with wildcard import. Please remove from ufl_legacy import * or move it above the fenics import, as both packages are using the same namespace

3 Likes

Please, can you explain how have you solved it by Anaconda?