Can't define the test and trial functions for RT element

I’m trying to define the acoustic eigenvalue problem on RT elements of lowest order.

(div u, div v) = lambda (u,v)

RTel = VectorElement("RT", mesh.ufl_cell(), 1)
print('Constructing the space ...')
space = FunctionSpace(mesh, RTel)
print('done ...')

'''Trial and test functions'''
print('The trial and test functions...')
u = TrialFunctions(space)
v = TestFunctions(space)

print('done...\n')

'''Left hand side'''
print('\t Constructing the bilinear forms')
a1 = (dot(div(u),div(v)))*dx

'''Right hand side'''
b = inner(u,v)*dx

I get the following msg:
ufl.log.UFLException: Don’t know how to split tensor valued mixed functions without flattened index space.

I don’t understand where is the problem

An RT element should not be a vector element, as the RT basis is already a vector, Ref:

If you use vector RT you would get some tensor space (this is not supported). If you want multiple RT elements, use ufl.MixedElement([RTelement,RTelement]) Where RTelement=FiniteElement("RT", mesh.ufl_cell(), 1)

1 Like

I did what you recommended
" RTel = FiniteElement(“RT”, mesh.ufl_cell(), 1)
space = MixedElement([RTel, RTel])
"
and got the following error:

TypeError: \ When constructing an Argument, TestFunction or TrialFunction, you
must to provide a FunctionSpace and not a FiniteElement. The
FiniteElement class provided by ufl only represents an abstract finite
element space and is only used in standalone .ufl files, while the
FunctionSpace provides a full discrete function space over a given
mesh and should be used in dolfin programs in Python.

Fr another attempt:
I added the mesh to the mixed space " space = MixedElement(mesh,[RTel, RTel]) " and got the following error:
cells = tuple(sorted(set(element.cell() for element in elements) - set([None])))
AttributeError: ‘dolfin.cpp.generation.UnitSquareMesh’ object has no attribute ‘cell’

The space should be
V = fem.FunctionSpace(mesh, mixed_el)
Not

But looking at your initial post, do you really want multiple RT elements, or just a single one for a vector field?

I need just a single vector field.

I have implemented the V = fem.FunctionSpace(mesh, mixed_el) but I got the error:

File “fenics_rt_acoustic.py”, line 93, in
a1 = (dot(div(u),div(v)))*dx
File “/home/linda/miniconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/operators.py”, line 398, in div
f = as_ufl(f)
File “/home/linda/miniconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/constantvalue.py”, line 471, in as_ufl
raise UFLValueError(“Invalid type conversion: %s can not be converted”
ufl.log.UFLValueError: Invalid type conversion: (ListTensor(Indexed(Argument(FunctionSpace(Mesh(VectorElement(FiniteElement(‘Lagrange’, triangle, 1), dim=2), 0), MixedElement(FiniteElement(‘Raviart-Thomas’, triangle, 1), FiniteElement(‘Raviart-Thomas’, triangle, 1))), 1, None), MultiIndex((FixedIndex(0),))), Indexed(Argument(FunctionSpace(Mesh(VectorElement(FiniteElement(‘Lagrange’, triangle, 1), dim=2), 0), MixedElement(FiniteElement(‘Raviart-Thomas’, triangle, 1), FiniteElement(‘Raviart-Thomas’, triangle, 1))), 1, None), MultiIndex((FixedIndex(1),)))), ListTensor(Indexed(Argument(FunctionSpace(Mesh(VectorElement(FiniteElement(‘Lagrange’, triangle, 1), dim=2), 0), MixedElement(FiniteElement(‘Raviart-Thomas’, triangle, 1), FiniteElement(‘Raviart-Thomas’, triangle, 1))), 1, None), MultiIndex((FixedIndex(2),))), Indexed(Argument(FunctionSpace(Mesh(VectorElement(FiniteElement(‘Lagrange’, triangle, 1), dim=2), 0), MixedElement(FiniteElement(‘Raviart-Thomas’, triangle, 1), FiniteElement(‘Raviart-Thomas’, triangle, 1))), 1, None), MultiIndex((FixedIndex(3),))))) can not be converted to any UFL type.

Then just use V = fem.FunctionSpace(mesh, ("RT", 1)).

I get the following error if I do this:

Traceback (most recent call last):
File “fenics_rt_acoustic.py”, line 67, in
space = FunctionSpace(mesh, (“RT”, 1))
File “/home/linda/miniconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/function/functionspace.py”, line 31, in init
self._init_from_ufl(*args, **kwargs)
File “/home/linda/miniconda3/envs/fenicsproject/lib/python3.8/site-packages/dolfin/function/functionspace.py”, line 39, in _init_from_ufl
ufl.FunctionSpace.init(self, mesh.ufl_domain(), element)
File “/home/linda/miniconda3/envs/fenicsproject/lib/python3.8/site-packages/ufl/functionspace.py”, line 57, in init
if element.cell() != domain_cell:
AttributeError: ‘tuple’ object has no attribute ‘cell’

Cannot reproduce this with:

import dolfinx
from mpi4py import MPI
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V = dolfinx.fem.FunctionSpace(mesh, ("RT", 1))
print(V)

I had the old fenics installed, i’m installing now fenicx but having some trouble with the pakages.
I’ll try to resolve the issue first.
Would it be possible to build the problem on the old fenics=2019.1.0

I managed to install the package but I don’t know how to impose Dirichlet zero BC and I get the below error.

Blockquote
from dolfinx.mesh import create_unit_square
from dolfinx import mesh, plot, fem
from dolfinx.fem import locate_dofs_geometrical
import pyvista
print(pyvista.global_theme.jupyter_backend)
from ufl import TrialFunction, TestFunction, dot, div, inner ,dx
import numpy as np
import petsc4py as PETSc
from petsc4py.PETSc import ScalarType
import matplotlib.pyplot as plt
import math
from mpi4py import MPI
domain = create_unit_square(MPI.COMM_WORLD,
num_elemnts, num_elemnts,
mesh.CellType.triangle)
tdim = domain.topology.dim
topology, cell_types, geometry = plot.create_vtk_mesh(domain, tdim)
grid = pyvista.UnstructuredGrid(topology, cell_types, geometry)
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True)
plotter.view_xy()
plotter.show()
‘’‘The function spaces’‘’
print(‘Constructing the space …’)
space = fem.FunctionSpace(domain, (“RT”, 1))
print(‘done …\n’)
‘’‘Trial and test functions’‘’
print(‘The trial and test functions…’)
u = TrialFunction(space)
v = TestFunction(space)
print(‘done…\n’)
‘’‘The operators and the bilinear form’‘’
print(‘Constructing the bilinear forms…’)
‘’‘Left hand side’‘’
a1 = (dot(div(u),div(v)))*dx
‘’‘Right hand side’‘’
b = inner(u,v)*dx
print(‘done…\n’)
print(‘Impossing the BC…’)
‘’’ Create facet to cell connectivity required to determine boundary facets’‘’
tdim = domain.topology.dim
fdim = tdim - 1
domain.topology.create_connectivity(fdim, tdim)
boundary_facets = mesh.exterior_facet_indices(domain.topology)
boundary_dofs = fem.locate_dofs_topological(domain, fdim, boundary_facets)
bc = fem.dirichletbc(ScalarType(0), boundary_dofs, space)

I get the following error:
boundary_dofs = fem.locate_dofs_topological(domain, fdim, boundary_facets)
File “/home/linda/miniconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/fem/bcs.py”, line 89, in locate_dofs_topological
raise TypeError
TypeError

Please use proper encapsulation (3x`) for your code, i.e.

```python
import dolfinx
# add code here

```

It is quite clear that locate_dofs_topological doesn’t take in the mesh, but the function space space, i.e.

should be

boundary_dofs = fem.locate_dofs_topological(space, fdim, boundary_facets)

I Have managed to impose the Dirichlet BC, but I’m not sure if I did it correctly and would apprentice your input.

	print('Impossing the BC...')
	# Create facet to cell connectivity required to determine boundary facets
	tdim = domain.topology.dim
	fdim = tdim - 1
	domain.topology.create_connectivity(fdim, tdim)
	boundary_facets = mesh.exterior_facet_indices(domain.topology)
	boundary_dofs = fem.locate_dofs_topological(space, fdim, boundary_facets)
	ubc = fem.Function(space)
	ubc.vector.set(0.0)
	bc = fem.dirichletbc(ubc, boundary_dofs)

Moreover, I would like to solve a generalized eigenvalue problem Ax = \lambda Bx.Thus, I need to impose zero on the BC of B. Previously in fenics I use to do bcs.zero(B).
How can I do this now?

bc.zero would zero out the rows associated with a boundary condition.
You can use petsc for this,
Like:
A.zeroRowsLocal(bc.dof_indices()[0], 1.)
(As shown in oasisx/assembly_bcs.py at 63fd8fd7458dcfca957e4420943d052c98845a3c · ComputationalPhysiology/oasisx · GitHub)
and documented in: MatZeroRowsLocal — PETSc 3.19.2 documentation

Ref old code
https://bitbucket.org/fenics-project/dolfin/src/8060a84e8ada0df018772497534092727b06bd56/dolfin/fem/DirichletBC.cpp#lines-305
https://bitbucket.org/fenics-project/dolfin/src/8060a84e8ada0df018772497534092727b06bd56/dolfin/la/PETScMatrix.cpp#lines-403
https://bitbucket.org/fenics-project/dolfin/src/8060a84e8ada0df018772497534092727b06bd56/dolfin/la/PETScMatrix.h#lines-182

Thank you so much… I managed to solve the problem and get the correct results