Error while creating MINI element for velocity function space

I am trying to use MINI elements to solve the DFG2D-3 benchmark from the tutorial. I made the mesh from triangular elements as MINI does not work with quadrilaterals.
First I tried this:

V1 = VectorElement("Lagrange", mesh.ufl_cell(), 1)
B  = VectorElement("Bubble", mesh.ufl_cell(), 3)
P2 = V1 + B
P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, P2)
Q = FunctionSpace(mesh, P1)

fdim = mesh.topology.dim - 1

# Define boundary conditions
class InletVelocity():
    def __init__(self, t):
        self.t = t

    def __call__(self, x):
        values = np.zeros((gdim, x.shape[1]),dtype=PETSc.ScalarType)
        values[0] = 4 * 1.5 * np.sin(self.t * np.pi/8) * x[1] * (0.41 - x[1])/(0.41**2)
        return values

# Inlet
u_inlet = Function(V)
inlet_velocity = InletVelocity(t)
u_inlet.interpolate(inlet_velocity)
bcu_inflow = dirichletbc(u_inlet, locate_dofs_topological(V, fdim, ft.find(inlet_marker)))
# Walls
#u_nonslip = np.array((0,) * mesh.geometry.dim, dtype=PETSc.ScalarType)
u_nonslip = Function(V)
u_nonslip.x.set(0.)
bcu_walls = dirichletbc(u_nonslip, locate_dofs_topological(V, fdim, ft.find(wall_marker)), V)
# Obstacle
bcu_obstacle = dirichletbc(u_nonslip, locate_dofs_topological(V, fdim, ft.find(obstacle_marker)), V)
bcu = [bcu_inflow, bcu_obstacle, bcu_walls]
# Outlet
bcp_outlet = dirichletbc(PETSc.ScalarType(0), locate_dofs_topological(Q, fdim, ft.find(outlet_marker)), Q)
bcp = [bcp_outlet]

This produces an error:

Traceback (most recent call last):
  File "/Users/varunkumar/opt/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/bcs.py", line 125, in __init__
    super().__init__(_value, dofs, V)  # type: ignore
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. dolfinx.cpp.fem.DirichletBC_float64(g: numpy.ndarray[numpy.float64], dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
    2. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Constant<double>, dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
    3. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: numpy.ndarray[numpy.int32])
    4. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: List[numpy.ndarray[numpy.int32][2]], V: dolfinx::fem::FunctionSpace)

Invoked with: <dolfinx.cpp.fem.Function_float64 object at 0x142cd8ef0>, array([   8,    9,   20,   21,   42,   43,   52,   53,   58,   59,   84,
         85,   88,   89,  112,  113,  120,  121,  146,  147,  154,  155,
        180,  181,  184,  185,  214,  215,  218,  219,  240,  241,  248,
        249,  278,  279,  282,  283,  308,  309,  312,  313,  344,  345,
        348,  349,  378,  379,  382,  383,  416,  417,  420,  421,  462,
        463,  466,  467,  518,  519,  522,  523,  574,  575,  578,  579,
        632,  633,  636,  637,  702,  703,  706,  707,  782,  783,  820,
        821,  876,  877,  916,  917,  970,  971, 1024, 1025, 1086, 1087,
       1136, 1137, 1206, 1207, 1260, 1261, 1336, 1337, 1398, 1399, 1478,
       1479, 1540, 1541, 1618, 1619, 1688, 1689, 1784, 1785, 1872, 1873,
       1982, 1983, 2082, 2083, 2192, 2193, 2298, 2299, 2416, 2417, 2522,
       2523, 2650, 2651, 2768, 2769, 2898, 2899, 3010, 3011, 3016, 3017,
       3258, 3259, 3270, 3271, 3508, 3509, 3522, 3523, 3636, 3637, 3646,
       3647, 3650, 3651, 3784, 3785, 4038, 4039, 4042, 4043, 4046, 4047,
       4170, 4171, 4316, 4317], dtype=int32), FunctionSpace(Mesh(VectorElement(Basix element (P, triangle, 3, equispaced, unset, False), 2), 0), EnrichedElement(VectorElement(FiniteElement('Lagrange', triangle, 1), dim=2), VectorElement(FiniteElement('Bubble', triangle, 3), dim=2)))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/varunkumar/Downloads/FEniCSx v0.6/DFG2D-3-Crank.py", line 151, in <module>
    bcu_walls = dirichletbc(u_nonslip, locate_dofs_topological(V, fdim, ft.find(wall_marker)), V)
  File "/Users/varunkumar/opt/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/bcs.py", line 183, in dirichletbc
    return formcls(value, dofs, V)
  File "/Users/varunkumar/opt/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/bcs.py", line 127, in __init__
    super().__init__(_value, dofs, V._cpp_object)  # type: ignore
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. dolfinx.cpp.fem.DirichletBC_float64(g: numpy.ndarray[numpy.float64], dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
    2. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Constant<double>, dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
    3. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: numpy.ndarray[numpy.int32])
    4. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: List[numpy.ndarray[numpy.int32][2]], V: dolfinx::fem::FunctionSpace)

Invoked with: <dolfinx.cpp.fem.Function_float64 object at 0x142cd8ef0>, array([   8,    9,   20,   21,   42,   43,   52,   53,   58,   59,   84,
         85,   88,   89,  112,  113,  120,  121,  146,  147,  154,  155,
        180,  181,  184,  185,  214,  215,  218,  219,  240,  241,  248,
        249,  278,  279,  282,  283,  308,  309,  312,  313,  344,  345,
        348,  349,  378,  379,  382,  383,  416,  417,  420,  421,  462,
        463,  466,  467,  518,  519,  522,  523,  574,  575,  578,  579,
        632,  633,  636,  637,  702,  703,  706,  707,  782,  783,  820,
        821,  876,  877,  916,  917,  970,  971, 1024, 1025, 1086, 1087,
       1136, 1137, 1206, 1207, 1260, 1261, 1336, 1337, 1398, 1399, 1478,
       1479, 1540, 1541, 1618, 1619, 1688, 1689, 1784, 1785, 1872, 1873,
       1982, 1983, 2082, 2083, 2192, 2193, 2298, 2299, 2416, 2417, 2522,
       2523, 2650, 2651, 2768, 2769, 2898, 2899, 3010, 3011, 3016, 3017,
       3258, 3259, 3270, 3271, 3508, 3509, 3522, 3523, 3636, 3637, 3646,
       3647, 3650, 3651, 3784, 3785, 4038, 4039, 4042, 4043, 4046, 4047,
       4170, 4171, 4316, 4317], dtype=int32), <dolfinx.cpp.fem.FunctionSpace object at 0x142cc94f0>

Then I tried this from The FEniCS Book:

V1 = VectorFunctionSpace(mesh, ("Lagrange", 1))
B  = VectorFunctionSpace(mesh, ("Bubble", 3))
V = V1 + B
P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
Q = FunctionSpace(mesh, P1)

This produces an error:

Traceback (most recent call last):
  File "/Users/varunkumar/Downloads/FEniCSx v0.6/DFG2D-3-Crank.py", line 113, in <module>
    V = V1 + B
TypeError: unsupported operand type(s) for +: 'FunctionSpace' and 'FunctionSpace'

Any suggestions on what’s wrong would be very helpful. :blush:

this should be:
bcu_walls = dirichletbc(u_nonslip, locate_dofs_topological(V, fdim, ft.find(wall_marker)))
Similar to

1 Like

Please do not post unrelated questions in other topics. You already have asked this question at: Hp-version of DG-FEM - #2 by st0907