Fenicsx method, which is equaivalent to dofmap().dofs() in fenics

Hi fenics community,

I need to port some python code from fenics 2019 to fenicsx 0.5.1. Im having a hard time finding a method in fenicsx, which gives me a output like FunctionSpace.dofmap().dofs() in fenics. Could you give me a hint? Here is a MWE in fenics:

from dolfin import *

mesh = UnitSquareMesh(3,3)

elementShape = triangle

FE_vector=VectorElement('CG', elementShape, 2)
FE_scalar=FiniteElement('CG', elementShape, 2)
FE_scalar_p=FiniteElement('CG', elementShape, 2)

MixedList = [FE_vector, FE_scalar, FE_scalar_p]
element = MixedElement(MixedList)
VMixed = FunctionSpace(mesh,element)

dofs = VMixed.dofmap().dofs()
print(dofs)

Thanks alot in Advance,
Tobias

Consider

import dolfinx
import ufl
from mpi4py import MPI
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 3, 3)

elementShape = ufl.triangle

FE_vector = ufl.VectorElement('CG', elementShape, 2)
FE_scalar = ufl.FiniteElement('CG', elementShape, 2)
FE_scalar_p = ufl.FiniteElement('CG', elementShape, 2)

MixedList = [FE_vector, FE_scalar, FE_scalar_p]
element = ufl.MixedElement(MixedList)
VMixed = dolfinx.fem.FunctionSpace(mesh, element)

dofs = VMixed.dofmap.list.array

print(dofs)

Hi dokken,

thanks for your response.
I have a following question about your solution:
The shapes of the dofs-array in fenics and fenicsx differ.
In fenics i get:


While in fenicsx:
Screenshot from 2022-08-29 11-33-56
In fenics the dofs array has a length of 196, while in fenicsx it has the size 432.
How can i only get the values, which correspond to the values i get in fenics?

Thank you for your Help,
Tobias

The old dofs gives you the global indices of all dofs owned by the current process, i.e.

from dolfin import *
from mpi4py import MPI as _MPI
mesh = UnitSquareMesh(3, 3)

elementShape = triangle

FE_vector = VectorElement('CG', elementShape, 2)
FE_scalar = FiniteElement('CG', elementShape, 2)
FE_scalar_p = FiniteElement('CG', elementShape, 2)

MixedList = [FE_vector, FE_scalar, FE_scalar_p]
element = MixedElement(MixedList)
VMixed = FunctionSpace(mesh, element)

dofs = VMixed.dofmap().dofs()
print(MPI.comm_world.allreduce(len(dofs), op=_MPI.SUM))
print(dofs)

executed with 3 MPI processes yields

96
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67]
196
[68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129]
196
[130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195]

In DOLFINx, you can get something similar by calling:

from IPython import embed
import dolfinx
import ufl
from mpi4py import MPI
import numpy as np
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 3, 3)

elementShape = ufl.triangle

FE_vector = ufl.VectorElement('CG', elementShape, 2)
FE_scalar = ufl.FiniteElement('CG', elementShape, 2)
FE_scalar_p = ufl.FiniteElement('CG', elementShape, 2)

MixedList = [FE_vector, FE_scalar, FE_scalar_p]
element = ufl.MixedElement(MixedList)
VMixed = dolfinx.fem.FunctionSpace(mesh, element)
local_range = VMixed.dofmap.index_map.local_range
dofs = np.arange(*local_range)
print(MPI.COMM_WORLD.rank, dofs)
1 Like

Thanks, that solved my Problem.

Hello! I’ve been translating a piece of code I had in fenics to dolfinx and I encountered some problems with the .dofmap().dofs() translation. The thing is that when using ‘VectorElement(‘Real’, mesh.ufl_cell(), 0, 3)’, fenics gives me 3 dof when calling V.sub(0).dofmap().dofs(), but dolfinx gives 0 when calling the analogous ‘V.sub(0).dofmap.list.array’. Is it possible to obtain the same output with dolfinx?

See Get velocity field at a boundary - #2 by dokken

But please that real elements are not supported in DOLFINX yet