# How do I use a Python function to define Dirichlet boundary conditions which are not known in closed form?

**URL:** <https://fenicsproject.discourse.group/t/how-do-i-use-a-python-function-to-define-dirichlet-boundary-conditions-which-are-not-known-in-closed-form/4088>\
**Category:** Uncategorized\
**Created:** [September 13, 2020, 4:32pm UTC](https://fenicsproject.discourse.group/t/how-do-i-use-a-python-function-to-define-dirichlet-boundary-conditions-which-are-not-known-in-closed-form/4088 "2020-09-13T16:32:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![frenzie](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@frenzie](https://fenicsproject.discourse.group/u/frenzie)\
**Post date:** [September 13, 2020, 4:32pm UTC](https://fenicsproject.discourse.group/t/how-do-i-use-a-python-function-to-define-dirichlet-boundary-conditions-which-are-not-known-in-closed-form/4088/1 "2020-09-13T16:32:34Z")

</div>

Hello all! Hope you’re having a good one.

To illustrate my question I will use the 2d Poisson equation as a toy example, and will use the [example code](https://github.com/hplgit/fenics-tutorial/blob/master/pub/python/vol1/ft01_poisson.py), which I also include here:

```
from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt

# Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)

# Define boundary condition
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)

def boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, u_D, boundary)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = dot(grad(u), grad(v))*dx
L = f*v*dx

# Compute solution
u = Function(V)
solve(a == L, u, bc)

# Plot solution and mesh
plot(u)
plt.show()

```

Suppose now, however, that we do not know the closed form of the Dirichlet boundary conditions, but that we have a python function

```
def py_fun(x,y):
    return 1 + x **2 + 2*y** 2

```

(this is for illustration purposes only, here we assume that in general we do **not** know the closed form of py\_fun(x,y). This function may give some interpolation from numerically calculated data, e.g.)

How would we change the

```
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)

```

part in order to implement the boundary conditions to be taken from py\_fun(x,y)?

[This topic](https://fenicsproject.discourse.group/t/how-to-apply-time-varying-boundary-conditions-coming-from-a-function/2386/3) might be related, but it only imposes constant boundary conditions.

Any input will be very helpful!

Thanks! 🙂

EDIT: So, as per dokken’s suggestion, writing

```
class MyExpression0(UserExpression):
def eval(self, value, x):
   value[0] = py_fun(x[0],x[1])

u_D = MyExpression0()

```

Does the job.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [September 13, 2020, 5:00pm UTC](https://fenicsproject.discourse.group/t/how-do-i-use-a-python-function-to-define-dirichlet-boundary-conditions-which-are-not-known-in-closed-form/4088/2 "2020-09-13T17:00:54Z")

</div>

Have a look at UserExpressions, for instance:

> [@Update Expression for DirichletBC](https://fenicsproject.discourse.group/t/update-expression-for-dirichletbc/2412/2):
>
> This is because the class of this two objects differs, and is handled differently by the DirichletBC. You can see this with the following code: import ufl print(isinstance(Coeff(T), ufl.Coefficient)) print(isinstance(2\*Coeff(T), ufl.Coefficient)) which returns True False This means that the latter input is treated as something that has to be projected internally in the DirichletBC, and is thus not prone to updates. What you should do here is to add a scaling parameter to your UserExpression…

Here, you can substitute self.T with your function, which has to be evaluated at x[0],x[1],x[2]
