AttributeError: 'Mesh' object has no attribute 'geometric_dimension'

Hi,everyone
I am a newcomer to FEniCSx,I encountered the following error when building variational forms using UFL:
AttributeError: ‘Mesh’ object has no attribute ‘geometric_dimension’
code are follows:
msh= mesh.create_rectangle(comm=MPI.COMM_WORLD,
** points=((0.0, 0.0), (2.0, 1.0)), n=(32, 16),**
** cell_type=mesh.CellType.triangle,)**
P2= ufl.VectorElement(“Lagrange”, “triangle”, 2)
P1 = ufl.FiniteElement(“Lagrange”, “triangle”, 1)
TH = ufl.MixedElement([P2, P1,P1])
W = ufl.FunctionSpace(msh, TH)
(u,v,w)=ufl.TrialFunctions(W)
(u1,v1,w1)=ufl.TestFunctions(W)
*b=inner(u,u1)dx
thanks you all

Please make a minimal reproducible example, using markdown formatting (i.e.

```python
# code here
```

sorry,the python code are follows:

msh= mesh.create_rectangle(comm=MPI.COMM_WORLD,
                            points=((0.0, 0.0), (2.0, 1.0)), n=(32, 16),
                            cell_type=mesh.CellType.triangle,)
P2= ufl.VectorElement("RT", "triangle", 2)
P1 = ufl.FiniteElement("DG", "triangle", 1)
TH = ufl.MixedElement([P2, P1 ,P2])
W = ufl.FunctionSpace(msh,TH)
u,v,w=ufl.TrialFunctions(W)
u1,v1,w1=ufl.TestFunctions(W)
b=inner(u,u1)*dx

Use

from dolfinx import mesh
import ufl
from mpi4py import MPI
msh= mesh.create_rectangle(comm=MPI.COMM_WORLD,
                            points=((0.0, 0.0), (2.0, 1.0)), n=(32, 16),
                            cell_type=mesh.CellType.triangle,)
P2= ufl.VectorElement("RT", "triangle", 2)
P1 = ufl.FiniteElement("DG", "triangle", 1)
TH = ufl.MixedElement([P2, P1 ,P2])
W = ufl.FunctionSpace(msh,TH)
u,v,w=ufl.TrialFunctions(W)
u1,v1,w1=ufl.TestFunctions(W)
b=ufl.inner(u,u1)*ufl.dx(domain=msh)

Thank you very much,Can you help me explain why?
I really want to delve deeper into learning Fenicsx, but I have made no progress at all.
Thank you very much again.

It is quite a corner case, where your form only consists of sub functions, and no coefficients or constants that have the mesh easily attached. UFL then struggles with finding the geometrical dimension of the problem.

1 Like

The variational form (b) is just an example I gave in my study.I tried the gradient operator again.Get the same error as before.I added a constant according to your instructions(c) but it does not solve problem.I have been fooled by myself, really trouble you.Thanks. :sob:

grad dx rot seems to have the same problem.

msh= mesh.create_rectangle(comm=MPI.COMM_WORLD,
                            points=((0.0, 0.0), (2.0, 1.0)), n=(32, 16),
                            cell_type=mesh.CellType.triangle,)
P2= ufl.VectorElement("RT", "triangle", 2)
P1 = ufl.FiniteElement("DG", "triangle", 1)
TH = ufl.MixedElement([P2, P1 ,P2])
W = ufl.FunctionSpace(msh,TH)
u,v,w=ufl.TrialFunctions(W)
u1,v1,w1=ufl.TestFunctions(W)
c=ufl.Constant(W)
b=c*inner(grad(u),grad(u1))*ufl.dx(domain=msh)

I may know where the problem lies.
RT element is Raviart-Thomas element. It seems to have provided finite elements for vector fields.So it can only be used ufl.FiniteElement rather than ufl.VectorElement.
meanwhile W =fem.FunctionSpace(msh,TH) replace W = ufl.FunctionSpace(msh,TH)

from dolfinx import fem
msh= mesh.create_rectangle(comm=MPI.COMM_WORLD,
                            points=((0.0, 0.0), (2.0, 1.0)), n=(32, 16),
                            cell_type=mesh.CellType.triangle,)
P2= ufl.FiniteElement("RT", "triangle", 2)
P1 = ufl.FiniteElement("DG", "triangle", 1)
TH = ufl.MixedElement([P2, P1 ,P2])
W =fem.FunctionSpace(msh,TH)
u,v,w=ufl.TrialFunctions(W)
u1,v1,w1=ufl.TestFunctions(W)
b=inner(grad(u),grad(u1))*ufl.dx(domain=msh)

Is my explanation correct? Although it cannot be explained from the source code.

Actually, you should not make a vector element of a RT space, as the basis of an RT function space is already vector valued. This would create a tensor function space.

I’m suprised that DOLFINx/basix doesn’t complain about this.
@mscroggs any ideas?

This should throw an exception. Perhaps we removed the exception by mistake while refactoring some of UFL/Basix.

I’ll look into this and re-add the exception if it’s gone.

I seem to have encountered a similar error while dealing with boundary conditions, and I am unable to resolve it.
The python code are as follows:

import numpy as np
import ufl
from dolfinx import fem, io, mesh, plot
from mpi4py import MPI
from petsc4py.PETSc import ScalarType
from petsc4py import PETSc 
msh = mesh.create_rectangle(comm=MPI.COMM_WORLD,
                            points=((0.0, 0.0), (2.0, 1.0)), n=(32, 16),
                            cell_type=mesh.CellType.triangle)

u_element=ufl.FiniteElement("RT",ufl.triangle,1)
v_element=ufl.VectorElement("DG",ufl.triangle,1)
Vh=fem.FunctionSpace(msh,u_element) 
Dh=fem.FunctionSpace(msh,v_element)
print("Vh(FinitElement(RT)) dofs is ",Vh.dofmap.bs)   #  OUTPUT is  [Vh(FinitElement(RT)) dofs is  1]
print("Dh(VectorElement(DG)) dofs is",Dh.dofmap.bs)   #  OUTPUT is  [Dh(VectorElement(DG)) dofs is 2]

facets = mesh.locate_entities_boundary(msh, dim=1,
                                       marker=lambda x: np.logical_or(np.isclose(x[0], 0.0),
                                                                      np.isclose(x[0], 2.0)))

#--------------------------------------------------------------------
no_slip=np.zeros(2, dtype=PETSc.ScalarType)
dofs2 = fem.locate_dofs_topological(V=Dh, entity_dim=1, entities=facets)
bc2 = fem.dirichletbc(no_slip, dofs=dofs2, V=Dh)
#--------------------------------------------------------------------
no_slip=np.zeros(2, dtype=PETSc.ScalarType)
dofs = fem.locate_dofs_topological(V=Vh, entity_dim=1, entities=facets)
bc = fem.dirichletbc(no_slip, dofs=dofs, V=Vh)

error:
RuntimeError: Creating a DirichletBC using a Constant is not supported when the Constant size is not equal to the block size of the constrained (sub-)space. Use a fem::Function to create the fem::DirichletBC.
As you can see,the output: Vh.dofmap.bs =1 ,but Vh is a vector function space.
Perhaps it is precisely for this reason that the Constant size is not equal to the block size of the constrained (sub-)space