Non homogeneous dirichletbcs

Hi everybody, I am trying to impose non homogeneous dirichletbcs but I can’t understand how I can give my array a type (this is the error I think). I attach the code and the error generated.
CODE:

“”“GENERATE MESH AND TIME STEP”“”

side = np.pi
mesh = create_rectangle(MPI.COMM_WORLD, [np.array([0.0, 0.0]), np.array([side, side])], [10, 10])

t = 0
T = 10
num_steps = 200
dt = T / num_steps

“”“DEFINE THE FINITE ELEMENT FUNCTION SPACE”“”

v_cg2 = VectorElement(“Lagrange”, mesh.ufl_cell(), 2)
s_cg1 = FiniteElement(“Lagrange”, mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, v_cg2)
Q = FunctionSpace(mesh, s_cg1)
u = TrialFunction(V)
v = TestFunction(V)
p = TrialFunction(Q)
q = TestFunction(Q)

“”“DEFINE BOUNDARY CONDITIONS”“”

def bound_loop(dofs, f, xy): #x = 1, y = 0 mi dice quale delle due variabili voglio che sia nulla
array_2d = np.zeros((dofs, 2), dtype=PETSc.ScalarType)
for i in range(dofs):
if xy == 1:
array_2d = (0, f(i * side / dofs))
else:
array_2d = (f(i * side / dofs), 0)
return array_2d

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

right_wall_dofs = locate_dofs_geometrical(V, right_wall)
right_wall_D = bound_loop(len(right_wall_dofs), np.sin, 0)
right_bc = dirichletbc(right_wall_D, right_wall_dofs)

AND THE ERROR:

Traceback (most recent call last):
File “/opt/anaconda3/envs/provax/lib/python3.12/site-packages/dolfinx/fem/bcs.py”, line 150, in dirichletbc
dtype = value.dtype
^^^^^^^^^^^
AttributeError: ‘tuple’ object has no attribute ‘dtype’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/Users/niccolobaldi/Desktop/from_pf_to_tgv.py”, line 57, in
right_bc = dirichletbc(right_wall_D, right_wall_dofs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/anaconda3/envs/provax/lib/python3.12/site-packages/dolfinx/fem/bcs.py”, line 162, in dirichletbc
raise AttributeError(“Boundary condition value must have a dtype attribute.”)
AttributeError: Boundary condition value must have a dtype attribute.

Can everybody help me?
Thanks in advance

Please format your code with 3x` and ensure to include all imports and a specification of what version of dolfinx you are using.

```python

# add code here

```
from mpi4py import MPI
from petsc4py import PETSc
import numpy as np
import pyvista

from dolfinx.fem import Constant, Function, FunctionSpace, assemble_scalar, dirichletbc, form, locate_dofs_geometrical
from dolfinx.fem.petsc import assemble_matrix, assemble_vector, apply_lifting, create_vector, set_bc
from dolfinx.io import VTXWriter
from dolfinx.mesh import create_rectangle
from dolfinx.mesh import create_box
from dolfinx.plot import vtk_mesh
from ufl import (FacetNormal, FiniteElement, Identity, TestFunction, TrialFunction, VectorElement,
                 div, dot, ds, dx, inner, lhs, nabla_grad, rhs, sym)


"""GENERATE MESH AND TIME STEP"""

side = np.pi
mesh = create_rectangle(MPI.COMM_WORLD, [np.array([0.0, 0.0]), np.array([side, side])], [10, 10])

t = 0
T = 10
num_steps = 200
dt = T / num_steps

"""DEFINE THE FINITE ELEMENT FUNCTION SPACE"""

v_cg2 = VectorElement("Lagrange", mesh.ufl_cell(), 2)
s_cg1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, v_cg2)
Q = FunctionSpace(mesh, s_cg1)
u = TrialFunction(V)
v = TestFunction(V)
p = TrialFunction(Q)
q = TestFunction(Q)

"""DEFINE BOUNDARY CONDITIONS"""


def bound_loop(dofs, f, xy): #x = 1, y = 0 mi dice quale delle due variabili voglio che sia nulla
    array_2d = np.zeros((dofs, 2), dtype=PETSc.ScalarType)
    for i in range(dofs):
        if xy == 1:
            array_2d = (0, f(i * side / dofs))
        else:
            array_2d = (f(i * side / dofs), 0)
    return array_2d



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


right_wall_dofs = locate_dofs_geometrical(V, right_wall)
right_wall_D = bound_loop(len(right_wall_dofs), np.sin, 0)
right_bc = dirichletbc(right_wall_D, right_wall_dofs)

I’m using DOLFINx 0.7.3

your right_wall_D type is tuple, you can write as right_wall_D[0], also, need to define function space for dirichletbc. For your case, it should be Q

right_bc = dirichletbc(right_wall_D[0], right_wall_dofs, Q)

Above code works for me, but not 100% percent sure for the explanation, since I am also new for FEniCS.

Yes, it seems to work, thank you :slight_smile: . Otherwise I tried to print the result of my function and it was only a tuple and not an array of tuples (as I thought) so I slightly changed my code to give dirichletbc an array but now it doesn’t work again. So I was wondering what’s the right way to impose a sin function as non homogeneous boundary conditions.

def bound_loop(dofs, f, xy): #x = 1, y = 0 mi dice quale delle due variabili voglio che sia nulla
    array_2d = np.zeros((dofs, 2), dtype=PETSc.ScalarType)
    for i in range(dofs):
        if xy == 1:
            array_2d[i][0] = 0
            array_2d[i][1] = f(i * side / dofs)
        else:
            array_2d[i][0] = f(i * side / dofs)
            array_2d[i][1] = 0
    return array_2d

Hi, maybe you can write a loop, which define every boundary points with different x[0] value:

import numpy as np

right_bc = []
for i in range(len(right_wall_D)):
   right_wall_dofs_temp = np.array([  right_wall_dofs[i]  ],  dtype=np.int32)
   right_bc +=  [   dirichletbc(right_wall_D[i,0], right_wall_dofs_temp, Q)   ]

This right_bc is the combination for all bc in right wall. which is equal to

bc1=...
bc2=...
bc_all = [bc1, bc2]

To define the dirichletbc, only below input type is support:

    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<double>)
    2. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Constant<double>, dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace<double>)
    3. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double, double>, dofs: numpy.ndarray[numpy.int32])
    4. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double, double>, dofs: List[numpy.ndarray[numpy.int32][2]], V: dolfinx::fem::FunctionSpace<double>)

Now it works, thank you so much! :slight_smile: