# Smart way to define Neumann boundary conditions for an elasticity-problem

**URL:** <https://fenicsproject.discourse.group/t/smart-way-to-define-neumann-boundary-conditions-for-an-elasticity-problem/5549>\
**Category:** variational formulation\
**Created:** [April 9, 2021, 10:23am UTC](https://fenicsproject.discourse.group/t/smart-way-to-define-neumann-boundary-conditions-for-an-elasticity-problem/5549 "2021-04-09T10:23:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![a-latyshev](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/a-latyshev/32/7135_2.png) [@a-latyshev](https://fenicsproject.discourse.group/u/a-latyshev)\
**Post date:** [April 9, 2021, 10:23am UTC](https://fenicsproject.discourse.group/t/smart-way-to-define-neumann-boundary-conditions-for-an-elasticity-problem/5549/1 "2021-04-09T10:23:13Z")

</div>

Hello everyone,

I’m a new user of Fenics and I can’t find a simple way to define boundary conditions. I have a linear elasticity problem with pure Neumann boundary conditions. I apply exact expressions of stresses \sigma\_{ij} on the external boundary. So, when we talk about the linear term in the weak formulation \int\_{ \partial\Omega } (\sigma \cdot n) \cdot v, I’m going to substitute exacte functions for each component of tensor.

The best code would be

```auto
n = FacetNormal(mesh)
L = dot( dot( sigma, n ), v )*ds

```

but I don’t understand how to define a tensor of Expressions. Of cause, I can calculate each term of the final sum, but maybe you know a “smarter” way for this?

In general, we use Expressions to define Dirichlet conditions, for example

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

```

but in my case the functions aren’t expressed as polynomials, so there will be a small error after the interpolation in the FE space. I would like to apply exacte values of the functions instead of their FE representations.  
How can I do it?

And the third question. Can we use a python (instead of C++) expression of functions in the Expression?

Thank you for your attention!

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [April 9, 2021, 12:07pm UTC](https://fenicsproject.discourse.group/t/smart-way-to-define-neumann-boundary-conditions-for-an-elasticity-problem/5549/2 "2021-04-09T12:07:25Z")

</div>

For applying Neumann BCs, it’s possible to avoid `Expression`s altogether. You can access the spatial coordinate associated with a given `mesh` in UFL via

```python
x = SpatialCoordinate(mesh)

```

This can then be used to define components of a tensor using the `as_tensor` function, e.g.,

```python
s00 = # Something in terms of `x`
s01 = # ...
s11 = # ...
sigma_BC = as_tensor([[s00,s01],
                      [s01,s11]])

```

(Or, if you are doing verification starting from an exact displacement solution, you could define the displacement in terms of `x`, then plug it into the definition of \sigma to get `sigma_BC`, letting UFL handle all the spatial derivatives.) This is sufficient for a Neumann BC that enters into the variational formulation through a `ds` integral. The one limitation of this approach is that functions defined this way cannot be `interpolate`d or used to define a `DirichletBC`.
