Dirichletbc incompatible arguments: needs a float32 functionspace?

Hi there,

I have been trying to move my fenics code to dolfinx, but have run into an issue with my Dirichlet boundary conditions. Looking at the documentation, the arguments and process have changed, but I’m not sure how to overcome this argument error. Here is a MWE made to illustrate my problem:



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

crystal_mesh = mesh.create_unit_cube(MPI.COMM_WORLD, 10, 10, 10)
V = fem.functionspace(crystal_mesh, ("CG", 2))

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

facets = mesh.locate_entities_boundary(crystal_mesh, dim=2, marker=bound)
const_value = fem.Constant(crystal_mesh, np.array([100], dtype=np.float32))
sur_dof = fem.locate_dofs_topological(V, entity_dim = 2, entities=facets)
dbc = fem.dirichletbc(const_value, dofs = sur_dof, V = V)
```

This results in:

```
Traceback (most recent call last):
  File "MWE.py", line 230, in dirichletbc
    bc = bctype(_value, dofs, V)
TypeError: __init__(): incompatible function arguments. The following argument types are supported:
    1. __init__(self, g: ndarray[dtype=float32, writable=False, order='C'], dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C'], V: dolfinx.cpp.fem.FunctionSpace_float32) -> None
    2. __init__(self, g: dolfinx.cpp.fem.Constant_float32, dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C'], V: dolfinx.cpp.fem.FunctionSpace_float32) -> None
    3. __init__(self, g: dolfinx.cpp.fem.Function_float32, dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C']) -> None
    4. __init__(self, g: dolfinx.cpp.fem.Function_float32, dofs: collections.abc.Sequence[ndarray[dtype=int32, writable=False, shape=(*), order='C']], V: dolfinx.cpp.fem.FunctionSpace_float32) -> None

Invoked with types: dolfinx.cpp.fem.DirichletBC_float32, dolfinx.cpp.fem.Constant_float32, ndarray, dolfinx.fem.function.FunctionSpace

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "MWE.py", line 15, in <module>
    dbc = fem.dirichletbc(const_value, dofs = sur_dof, V = V)
  File "/home/leroux/.conda/envs/leroux_fenicsx/lib/python3.13/site-packages/dolfinx/fem/bcs.py", line 232, in dirichletbc
    bc = bctype(_value, dofs, V._cpp_object)
TypeError: __init__(): incompatible function arguments. The following argument types are supported:
    1. __init__(self, g: ndarray[dtype=float32, writable=False, order='C'], dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C'], V: dolfinx.cpp.fem.FunctionSpace_float32) -> None
    2. __init__(self, g: dolfinx.cpp.fem.Constant_float32, dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C'], V: dolfinx.cpp.fem.FunctionSpace_float32) -> None
    3. __init__(self, g: dolfinx.cpp.fem.Function_float32, dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C']) -> None
    4. __init__(self, g: dolfinx.cpp.fem.Function_float32, dofs: collections.abc.Sequence[ndarray[dtype=int32, writable=False, shape=(*), order='C']], V: dolfinx.cpp.fem.FunctionSpace_float32) -> None

Invoked with types: dolfinx.cpp.fem.DirichletBC_float32, dolfinx.cpp.fem.Constant_float32, ndarray, dolfinx.cpp.fem.FunctionSpace_float64

```

My -very limited- understanding tells me that I need a way to create a float32 functionspace (which I only see documentation for a deprecated  functionspace_float32()) in order to fulfill argument type 2 listed in the error.

I assume this is a trivial mistake on my part where I misunderstand some data shape/type/structure but I have bene stumped on this one for a little bit.


Thanks in advance :)

Hello @AnfoDao , I made a mistake. At first I thought it was due to scalar-type and float-type getting mixed together, but it is not only that. It seems that dirichlet BC is getting an error when imposing “np.float32”. However, when changing to “np.float64” it does not. A first quick fix would be:

V = fem.functionspace(crystal_mesh, ("CG", 2, (1,)))
const_value = fem.Constant(crystal_mesh, np.array([100], dtype=np.float64))

A second quick fix:

const_value = fem.Constant(crystal_mesh, 100.0)

Either way, the function space and the dirichlet BC value should have the same type/shape.

1 Like

You’re a lifesaver, thank you very much!

Note for others who may run into this issue:

Bizarrely, when I attempted to implement this outside of the MWE, I was receiving an “Unsupported dtype” error when using your second method since my array of DBC values are all of integer form (ints aren’t accepted in fem.Constant()), so I opted to keep the np array float-ification, but needed to apply float64 instead of float32 like in your first solution example. Doing so didn’t require the (1,) argument inside the functionspace creation, however.

1 Like