Dirichletbc incompatible constructor arguments

Hello all

I am trying to solve a mixed problem in the unkowns u and \theta on the unit interval. At the implementation of Dirichlet boundary conditions for the function u, I obtain a TypeError, however I have no clue how to fix it.

A MWE is included below. It seems to me there is a problem with a dolfinx.cpp.fem.Function_float64 that should be a dolfinx.cpp.fem.DirichletBC_float64?

Any pointers or tips are highly appreciated.
Many thanks in advance!

from dolfinx import fem, mesh
from mpi4py import MPI
import ufl
import numpy as np

nx = 10
domain = mesh.create_unit_interval(MPI.COMM_SELF,nx)
tdim = domain.topology.dim
fdim = tdim-1
domain.topology.create_connectivity(fdim,tdim)

# boundary of unit interval
def is_boundary(x):
    return np.logical_or.reduce((is_left(x), is_right(x)))

def is_left(x):
    return np.isclose(x[0],0.)

def is_right(x):
    return np.isclose(x[0],1.)

# function spaces, elements and related objects
u_element = ufl.VectorElement('CG', domain.ufl_cell(),1)
theta_element = ufl.FiniteElement('CG', domain.ufl_cell(),1)
V = fem.FunctionSpace(domain, ufl.MixedElement([u_element, theta_element]))
Vu, Vu_to_V = V.sub(0).collapse()
Vtheta, Vtheta_to_V = V.sub(1).collapse()

# boundary condition
boundary_facets_all = mesh.locate_entities_boundary(domain, fdim, is_boundary)
dofsu = fem.locate_dofs_topological(Vu, fdim, boundary_facets_all)
u_D = fem.Function(Vu)
u_D.interpolate(lambda x : x[0])
bcsu = fem.dirichletbc(u_D, dofsu, Vu)

The error looks like this

TypeError                                 Traceback (most recent call last)
File /usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/fem/bcs.py:125, in DirichletBCMetaClass.__init__(self, value, dofs, V)
    124 try:
--> 125     super().__init__(_value, dofs, V)  # type: ignore
    126 except TypeError:

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 0x7f242c1359b0>, array([ 0, 10], dtype=int32), FunctionSpace(Mesh(VectorElement(Basix element (P, interval, 1, gll_warped, unset, False), 1), 0), VectorElement(FiniteElement('Lagrange', interval, 1), dim=1))

should be

dofsu = fem.locate_dofs_topological((V.sub(0), Vu), fdim, boundary_facets_all)
u_D = fem.Function(Vu)
u_D.interpolate(lambda x : x[0])
bcsu = fem.dirichletbc(u_D, dofsu, Vu.sub(0))

see: https://github.com/FEniCS/dolfinx/blob/main/python/demo/demo_stokes.py#L455-L457

2 Likes