# Normal vector of just one of the boundaries

**URL:** <https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855>\
**Category:** variational formulation\
**Created:** [March 16, 2022, 11:58am UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855 "2022-03-16T11:58:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [March 16, 2022, 11:58am UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/1 "2022-03-16T11:58:17Z")

</div>

Dear FEniCSx community,

I am trying to compute a stress vector in one of the boundaries of my geometry. In order to achieve this, I need to define the stress tensor and the normal vector associated to that boundary, and take the dot product of both variables.

The problem is that if I define the normal vector as usual, i.e., `n=FacetNormal(mesh)`, I get the normal vector of all the boundaries. Therefore, if I take the dot product of the stress tensor and the normal vector, I get the stress vector over all the boundaries.

Is it possible to define the normal vector of, for example, the left boundary?

Here is a minimal example:

```auto
# Import
import dolfinx
from ufl import Identity, div, dot, ds, dx, inner, lhs, grad, nabla_grad, rhs, sym, FacetNormal
from ufl import FiniteElement, VectorElement, TrialFunctions, TestFunctions, MixedElement
from mpi4py import MPI

# Mesh
mesh = dolfinx.UnitSquareMesh(MPI.COMM_WORLD, 10, 10)
n = FacetNormal(mesh)

# Function spaces
v_cg2 = VectorElement('CG', mesh.ufl_cell(), 2)
p_cg1 = FiniteElement('CG', mesh.ufl_cell(), 1)
mel = MixedElement([v_cg2, p_cg1])
V = dolfinx.FunctionSpace(mesh, v_cg2)
Q = dolfinx.FunctionSpace(mesh, p_cg1)

u_n = dolfinx.fem.Function(V)
p_n = dolfinx.fem.Function(Q)

# Stress tensor
mu = 0.01
def sigma(u, p):
    return 2*mu*sym(nabla_grad(u)) - p*Identity(u.geometric_dimension())

# Stress vector
stress = dot(sigma(u_n,p_n),n)

```

Many thanks in advance.

---

<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:** [March 16, 2022, 12:34pm UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/2 "2022-03-16T12:34:05Z")

</div>

What do you want to use `stress` for later on in your code?  
If you want to integrate the stress over a subset of a boundary, you can just restrict the integration measure `ds`

---

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [March 16, 2022, 12:45pm UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/3 "2022-03-16T12:45:20Z")

</div>

The problem is that `stress` over one of the boundaries is what I need (it is the gradient of my cost function, so I just need to transform it into numpy array and use scipy.minimize). Therefore, I do not need to integrate it.

---

<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:** [March 16, 2022, 12:48pm UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/4 "2022-03-16T12:48:52Z")

</div>

How are you going to transform this into a numpy array?

I would suggest using a projection into a suitable function space, which you can do as:

> <https://github.com/jorgensd/dolfinx_mpc/blob/master/python/dolfinx_mpc/utils/utils.py#L44-L141>

which shows you how to get a projection of the facet normal over a certain boundary marked with a mesh tag.  
You can of course adapt this to take in your stress and project in to a suitable space.

---

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [March 17, 2022, 2:47pm UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/5 "2022-03-17T14:47:48Z")

</div>

Thank you very much for your reply.  
I have adapted the function facet\_normal\_approximation as you suggested.

Now, when I compute the stress vector as `stress = dot(sigma(u_n,p_n),nn)` I get the object `<class 'ufl.tensoralgebra.Dot'>`.

Do you know how I can transform it into a numpy array?

Many thanks again.

---

<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:** [March 17, 2022, 3:36pm UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/6 "2022-03-17T15:36:05Z")

</div>

As I asked previously, what do you want that numpy array to contain? Is it values at degrees of freedom?

---

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [March 17, 2022, 3:38pm UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/7 "2022-03-17T15:38:28Z")

</div>

Yes, that is exactly what I need.

---

<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:** [March 17, 2022, 3:41pm UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/8 "2022-03-17T15:41:19Z")

</div>

I would:

1. Project the expression `stress` into a suitable function space
2. Get the dof coordinates of the suitable space by using “FunctionSpace.tabulate\_dof\_coordinates()”
3. Find what dofs are on your chosen boundary by using `locate_dofs_topological` or `locate_dofs_geometrical`.
4. Extract the dof coordinates at these dofs and the corresponding values from the projected function

---

<div class="post-metadata">

**Author:** ![charithjeewantha](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/charithjeewantha/32/3948_2.png) [@charithjeewantha](https://fenicsproject.discourse.group/u/charithjeewantha)\
**Post date:** [November 21, 2022, 8:15pm UTC](https://fenicsproject.discourse.group/t/normal-vector-of-just-one-of-the-boundaries/7855/9 "2022-11-21T20:15:53Z")

</div>

@JLorenteMacias Could you please share your full code for calculating the stresses in this unit square…?
