DirichletBC not working

Good afternoon,
Working with dolfinx 0.3.0,
i tried following the tutorial’s example with that code:

import numpy as np
from dolfinx import fem, cpp, io, mesh, generation
from mpi4py import MPI
import ufl
from petsc4py import PETSc

L, W, H=0.303, 0.19, 0.064
NX, NY, NZ = 30, 19, 6

points=[np.array([0, 0, 0]), np.array([L, W, H])]
domain=generation.BoxMesh(MPI.COMM_WORLD, points, [NX,NY,NZ], cell_type=cpp.mesh.CellType.hexahedron)
dim=domain.topology.dim

def bottom(x):
    return np.isclose(x[2], 0, atol=1e-3)

U=fem.VectorFunctionSpace(domain, ("CG", 1))


bcdofs=fem.locate_dofs_geometrical(U, bottom)
u_0=np.array((0,)*dim, dtype=PETSc.ScalarType)
bc=fem.DirichletBC(u_0, bcdofs, U)

But i get :

Traceback (most recent call last):
  File "/mnt/c/Users/stavfe/Documents/Optimisation/opti.py", line 92, in <module>
    bc=fem.DirichletBC(u_0, bcdofs, U)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfinx/fem/dirichletbc.py", line 147, in __init__
    raise NotImplementedError
NotImplementedError

Is it because i need dolfinx 0.4 to for it to work?
What can be a replacement for Dolfinx 0.3?
Thanks in advance.

Yes, you need DOLFINx >= 0.4 for usage of Constants as input to DirichletBC.

A workaround is to use a function, and interpolate the constant value onto that function.

u_0 = dolfinx.fem.Function(V)
def interpolater(x):
    values = np.zeros((tdim, x.shape[1]))
    values[0] = ... # Change first value of constant
    values[1] = ... # Change second value of constant
    ...
    return values
u_0.interpolate(interpolator)
bc = fem.DirichletBC(u_0, bc_dofs)
1 Like