# How to set a dirichlet boundary condition, which depends on a Function?

Hello,
I want to specify a Dirichlet boundary condition, which depends on a Function object rather than a constant (e.g. ScalarType(0), as is often the case in the standard examples I found). The following MWE produces a dtype error. My question is how this could be done properly.
Thank you.

``````import numpy
from ufl import (SpatialCoordinate, FiniteElement, MixedElement,
split, TestFunctions)
from dolfinx import fem, mesh
from dolfinx.fem import FunctionSpace, Function
from mpi4py import MPI
from petsc4py.PETSc import ScalarType

# Geometry and mesh
x_left  = -0.1
x_right = +0.1
NumberOfCells = int(1e3)

msh = mesh.create_interval(MPI.COMM_WORLD, NumberOfCells, points=(x_left, x_right))

x = SpatialCoordinate(msh)
CG1_elem = FiniteElement("CG", msh.ufl_cell(), 1)
ME_elem  = MixedElement([CG1_elem, CG1_elem])
ME = FunctionSpace(msh, ME_elem)

u_test, v_test = TestFunctions(ME)

w = Function(ME)

u, v = split(w)

# Left boundary
facet_l_bdry = mesh.locate_entities_boundary(msh, dim=0, marker=lambda x: numpy.isclose(x, x_left))
dof_l_bdry = fem.locate_dofs_topological(V=ME.sub(1), entity_dim=0, entities=facet_l_bdry)
f = 2*u
PoissonEq_dirichlet_bc_l = fem.dirichletbc(value=2*u, dofs=dof_l_bdry, V=ME.sub(1))
``````

Interpolate `2*u` into the suitable function space (for instance with dolfinx.fem.Expression) or simply by scaling all inputs of `u` by 2, i.e. `u.x.array[:]*=2`. and set is as shown in Test problem 2: Flow past a cylinder (DFG 2D-3 benchmark) — FEniCSx tutorial

``````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)))
``````

Hi dokken,
thank you for your answer. Maybe my MWE was too obscure (and also not really a MWE, sorry for that). My problem is that I am not able to provide any Function object in a Dirichlet boundary condition. I tried to further reduce the MWE:

``````import numpy as np
from ufl import SpatialCoordinate, FiniteElement
from dolfinx import fem, mesh
from dolfinx.fem import FunctionSpace, Function
from mpi4py import MPI

# Geometry and mesh
x_left = -0.1
x_right = +0.1
NumberOfCells = int(10)
msh = mesh.create_interval(MPI.COMM_WORLD, NumberOfCells, points=(x_left, x_right))
x = SpatialCoordinate(msh)
s_cg1 = FiniteElement("CG", msh.ufl_cell(), 1)
V = FunctionSpace(msh, s_cg1)

# left boundary
facet_l_bdry = mesh.locate_entities_boundary(msh, dim=0, marker=lambda x: np.isclose(x, x_left))
dof_l_bdry = fem.locate_dofs_topological(V=V, entity_dim=0, entities=facet_l_bdry)
u = Function(V)
PoissonEq_dirichlet_bc_l = fem.dirichletbc(value=u, dofs=dof_l_bdry, V=V)
``````

which still throws a TypeError.