# How to restrict solutions or stresses to subdomains?

**URL:** <https://fenicsproject.discourse.group/t/how-to-restrict-solutions-or-stresses-to-subdomains/6300>\
**Category:** I/O\
**Created:** [July 27, 2021, 7:22am UTC](https://fenicsproject.discourse.group/t/how-to-restrict-solutions-or-stresses-to-subdomains/6300 "2021-07-27T07:22:45Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [July 27, 2021, 9:18am UTC](https://fenicsproject.discourse.group/t/how-to-restrict-solutions-or-stresses-to-subdomains/6300/2 "2021-07-27T09:18:14Z")

</div>

> [@steve](#):
>
> ```auto
> V = FunctionSpace(mesh, 'P', 1) # Why is this line necessary?
> von_Mises = project(von_Mises, V)
> 
> ```

Please note that you should not use `CG` 1 function spaces for Von Mises stresses, as the stresses are a DG 0 function (if the displacement is CG 1. I.e.

```python
VDG = FunctionSpace(mesh, "DG", 0)
von_Mises = project(von_Mises, V)

```

You can compute the stresses on a subdomain by projecting on a subdomain, as follows:

```python
s = sigma(u) - (1./3)*tr(sigma(u))*Identity(d)
von_Mises = sqrt(3./2*inner(s, s))
u, v = TrialFunction(VDG), TestFunction(VDG)
a = inner(u, v)*dx
L = inner(von_Mises, v)*dx(1)
stress = Function(VDG)
solve(a==L, stress)

```

would only compute the stresses on domain 1, and be zero everywhere else.  
This can be optimized in a similar fashion to [How to extract values in a surface of 3D mesh to another 2D mesh? - #4 by dokken](https://fenicsproject.discourse.group/t/how-to-extract-values-in-a-surface-of-3d-mesh-to-another-2d-mesh/5043/4)  
where you would use `dx(1)` in the definition of `a`, then assemble it into `A`, and use `ident_zeros`.

Another solution would be to use MeshViews

---

_[View the full topic](https://fenicsproject.discourse.group/t/how-to-restrict-solutions-or-stresses-to-subdomains/6300)._
