# Questions about stress strain calculation

**URL:** <https://fenicsproject.discourse.group/t/questions-about-stress-strain-calculation/12031>\
**Category:** General\
**Created:** [August 25, 2023, 7:10am UTC](https://fenicsproject.discourse.group/t/questions-about-stress-strain-calculation/12031 "2023-08-25T07:10:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xx599](https://avatars.discourse-cdn.com/v4/letter/x/bc8723/32.png) [@xx599](https://fenicsproject.discourse.group/u/xx599)\
**Post date:** [August 25, 2023, 7:10am UTC](https://fenicsproject.discourse.group/t/questions-about-stress-strain-calculation/12031/1 "2023-08-25T07:10:51Z")

</div>

Hello everyone, I would like to ask some questions about stress strain calculation.

def epsilon(u):  
return sym(grad(u))  
def sigma(u):  
return 2.0_mu_epsilon(u) + lmbda\*tr(epsilon(u))\*Identity(2)

Is the matrix dimension calculated by the stress and strain functions here 2 \* 2?

Why did I output the ：np. shape of these two matrices during the calculation process and get () - an empty parenthesis

And I would like to ask what is the difference between Identity (2) and np. identity (2)? It seems to be related to the dimensionality of the matrix.

The reason for asking this question is because I want to extract the diagonal of the stress tensor through np. diagonal (sigma), but it has been unsuccessful.

---

<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:** [August 25, 2023, 7:21am UTC](https://fenicsproject.discourse.group/t/questions-about-stress-strain-calculation/12031/2 "2023-08-25T07:21:06Z")

</div>

Please follow the guidelines of:

> [@Read before posting: How do I get my question answered?](https://fenicsproject.discourse.group/t/read-before-posting-how-do-i-get-my-question-answered/21):
>
> Here are a few tips to help get your question answered: Phrase the title of your post as a question and make it meaningful. Check that your question hasn’t been asked before using the search functionality. Post a “minimum working code example” (MWE) to illustrate the problem: A MWE is the simplest piece of code that reproduces or explains the problem you have. It must be possible to take the MWE, paste it into a file and run it, without any extra modifications. Good MWEs can be as little as …

meaning:

1. Use proper formatting of code (3x` syntax, see: [How to Format Code in Markdown](https://www.freecodecamp.org/news/how-to-format-code-in-markdown/))
2. Make a minimal reproducible example, i.e. a code that people can run and reproduce your output. Usually, it is common to use one of the “built-in” meshes, such as an interval, square or cube mesh.
3. I would suggest looking at: [Form language — Unified Form Language (UFL) 2021.1.0 documentation](https://fenics.readthedocs.io/projects/ufl/en/latest/manual/form_language.html?highlight=Identity#literals-and-geometric-quantities)
4. Note that FEniCS uses UFL for the syntax of the variational form, thus you would have to use something along the lines of [ufl package — Unified Form Language (UFL) 2021.1.0 documentation](https://fenics.readthedocs.io/projects/ufl/en/latest/api-doc/ufl.html#ufl.tr) and project/interpolate this into an appropriate space, depending on what version of dolfin you are using (legacy or DOLFINx).

---

<div class="post-metadata">

**Author:** ![xx599](https://avatars.discourse-cdn.com/v4/letter/x/bc8723/32.png) [@xx599](https://fenicsproject.discourse.group/u/xx599)\
**Post date:** [August 26, 2023, 5:40am UTC](https://fenicsproject.discourse.group/t/questions-about-stress-strain-calculation/12031/3 "2023-08-26T05:40:45Z")

</div>

Thank you for your reply! Due to my lack of familiarity with forum usage, I apologize for any inconvenience caused. I would like to reiterate my question: I need to extract the diagonal part of the stress tensor (that is, extract the diagonal elements of the stress tensor as a diagonal matrix) during relevant mathematical calculations

But since the tensor storage in Fenics is in UFL format, I cannot use numpy to extract diagonal lines. What method should I use to achieve my goal?

Here is the code and error reports

> > > from dolfin import\*  
> > > import ufl  
> > > a = Identity(2)  
> > > b = ufl.diag(a)

Traceback (most recent call last):  
File “”, line 1, in   
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/operators.py”, line 282, in diag  
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/tensors.py”, line 297, in as\_matrix  
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/tensors.py”, line 251, in as\_tensor  
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/tensors.py”, line 203, in \_as\_list\_tensor  
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/tensors.py”, line 203, in   
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/tensors.py”, line 203, in \_as\_list\_tensor  
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/tensors.py”, line 203, in   
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/tensors.py”, line 206, in \_as\_list\_tensor  
File “/home/xx/.local/lib/python3.10/site-packages/fenics\_ufl-2019.1.0-py3.10.egg/ufl/constantvalue.py”, line 471, in as\_ufl  
ufl.log.UFLValueError: Invalid type conversion: 1 can not be converted to any UFL type.

---

<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:** [August 26, 2023, 6:18am UTC](https://fenicsproject.discourse.group/t/questions-about-stress-strain-calculation/12031/4 "2023-08-26T06:18:45Z")

</div>

> [@xx599](#):
>
> from dolfin import\*  
> import ufl  
> a = Identity(2)  
> b = ufl.diag(a)

This runs with no error for me.

```python
from dolfin import*
import ufl
a = Identity(2)
b = ufl.diag(a)
print(b.ufl_shape)

```

Note that this returns `(2, 2)`, as described in  
[https://fenics.readthedocs.io/projects/ufl/en/latest/api-doc/ufl.html?highlight=diag\_vector#ufl.diag](https://fenics.readthedocs.io/projects/ufl/en/latest/api-doc/ufl.html?highlight=diag_vector#ufl.diag)  
What you want is `diag_vector`, i.e.  
[https://fenics.readthedocs.io/projects/ufl/en/latest/api-doc/ufl.html?highlight=diag\_vector#ufl.diag\_vector](https://fenics.readthedocs.io/projects/ufl/en/latest/api-doc/ufl.html?highlight=diag_vector#ufl.diag_vector)

```python
from dolfin import*
import ufl
a = Identity(2)
b = ufl.diag_vector(a)
print(b.ufl_shape)

```

returning `(2,)` a vector.
