# Plotting 3D piecewise constant stresses in paraview

**URL:** <https://fenicsproject.discourse.group/t/plotting-3d-piecewise-constant-stresses-in-paraview/11798>\
**Category:** General\
**Created:** [July 21, 2023, 4:09pm UTC](https://fenicsproject.discourse.group/t/plotting-3d-piecewise-constant-stresses-in-paraview/11798 "2023-07-21T16:09:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![magnus](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@magnus](https://fenicsproject.discourse.group/u/magnus)\
**Post date:** [July 21, 2023, 4:09pm UTC](https://fenicsproject.discourse.group/t/plotting-3d-piecewise-constant-stresses-in-paraview/11798/1 "2023-07-21T16:09:27Z")

</div>

Newbie to fenicsx here,  
I’m trying to plot the Von Mises stresses found in the ‘The equations of linear elasticity’ tutorial in Paraview. The pyplot works fine in JupyterNotebooks, and when I save the Deformations into the xdmf file like this:

```auto
with io.XDMFFile(domain.comm, "out_test/deformation.xdmf", "w") as xdmf:
    xdmf.write_mesh(domain)
    uh.name = "Deformation"
    xdmf.write_function(uh)

```

Everything works fine in Paraview:

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/7/73176446ed17a51b9e714e4ac3d837b7cadce22b.png)

However when I try to save the stresses as well like this:

```auto
s = sigma(uh) -1./3*ufl.tr(sigma(uh))*ufl.Identity(len(uh))
von_Mises = ufl.sqrt(3./2*ufl.inner(s, s))
V_von_mises = fem.FunctionSpace(domain, ("DG", 0))
stress_expr = fem.Expression(von_Mises, V_von_mises.element.interpolation_points())
stresses = fem.Function(V_von_mises)
stresses.interpolate(stress_expr)

with io.XDMFFile(domain.comm, "out_test/deformation.xdmf", "w") as xdmf:
    xdmf.write_mesh(domain)
    uh.name = "Deformation"
    xdmf.write_function(uh)
    stresses.name = "stresses"
    xdmf.write_function(stresses)

```

(Bearing in mind this is the only change to the ipynb I made) Suddenly Paraview freaks out and doesn’t let me view either Deformation or stresses properly, they only appear as (partial):

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/9/930cde6d55c17bb8c58b87735d646f0776b87ad0.png)

Is it something to do with the stresses in a different FunctionSpace to the Deformation?

---

<div class="post-metadata">

**Author:** ![magnus](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@magnus](https://fenicsproject.discourse.group/u/magnus)\
**Post date:** [July 21, 2023, 4:12pm UTC](https://fenicsproject.discourse.group/t/plotting-3d-piecewise-constant-stresses-in-paraview/11798/2 "2023-07-21T16:12:32Z")

</div>

NB: correction, I also changed

```auto
L = ufl.dot(f, v) * ufl.dx + ufl.dot(T, v) * ds

```

to

```auto
L = ufl.inner(f, v) * ufl.dx + ufl.inner(T, v) * ds

```

as Jupyternotebooks doesn’t seem to like the complex numbers when using dot. This is why I get real\_ and imag\_ parts to both variable.

---

<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 21, 2023, 5:03pm UTC](https://fenicsproject.discourse.group/t/plotting-3d-piecewise-constant-stresses-in-paraview/11798/3 "2023-07-21T17:03:42Z")

</div>

Use the extract block paraview filter to access each of the functions you have saved to the file.

Note that the stresses (if they are not in DG 0) are interpolated into (Lagrange, 1).

---

<div class="post-metadata">

**Author:** ![magnus](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@magnus](https://fenicsproject.discourse.group/u/magnus)\
**Post date:** [July 22, 2023, 11:39am UTC](https://fenicsproject.discourse.group/t/plotting-3d-piecewise-constant-stresses-in-paraview/11798/4 "2023-07-22T11:39:54Z")

</div>

Thank you for the quick response!  
Was struggling to get ExtractBlock to work but it turns out I was using Xdmf3ReaderS rather than Xdmf3ReaderT, switching to the correct one fixed it, thank you.
