# Plotting multiple functions in same XDMF file

**URL:** <https://fenicsproject.discourse.group/t/plotting-multiple-functions-in-same-xdmf-file/14334>\
**Category:** dolfinx\
**Tags:** io, dolfinx\
**Created:** [April 19, 2024, 12:04am UTC](https://fenicsproject.discourse.group/t/plotting-multiple-functions-in-same-xdmf-file/14334 "2024-04-19T00:04:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![VenuVGR](https://avatars.discourse-cdn.com/v4/letter/v/f04885/32.png) [@VenuVGR](https://fenicsproject.discourse.group/u/VenuVGR)\
**Post date:** [April 19, 2024, 12:04am UTC](https://fenicsproject.discourse.group/t/plotting-multiple-functions-in-same-xdmf-file/14334/1 "2024-04-19T00:04:24Z")

</div>

Hello, I am trying to write out multiple functions in the same file in dolfinx XDMFFile format. Please find MWE below. When I open the XDMF file in Paraview 5.12, I am only able to see the second function being plotted, (array for function 1 not available). I also tried the “Filters → Extract block” as suggested here: [multiple functions](https://fenicsproject.discourse.group/t/error-in-paraview-visualization-of-xdmf-file-with-multiple-solution-variables/6873) but still only able to visualize function 1.

```auto
from mpi4py import MPI
import dolfinx as dlx
import numpy as np

comm = MPI.COMM_WORLD
rank = comm.rank
nproc = comm.size

msh = dlx.mesh.create_unit_square(comm, 10, 10, dlx.mesh.CellType.quadrilateral)
Vh_m = dlx.fem.FunctionSpace(msh, ("Lagrange", 1))

f1 = dlx.fem.Function(Vh_m,name='f1')
f1.interpolate(
    lambda x: np.log(2 + 7 * (((x[0] - 0.5) **2 + (x[1] - 0.5)** 2) ** 0.5 > 0.2))
)
f1.x.scatter_forward()

f2 = dlx.fem.Function(Vh_m,name='f2')
f2.interpolate(
    lambda x: np.log(10 + 3* (((x[0] - 0.5) **2 + (x[1] - 0.5)** 2) ** 0.5 > 0.2))
)
f2.x.scatter_forward()

with dlx.io.XDMFFile(
    msh.comm,
    "output.xdmf".format(nproc),
    "w",
) as file:
    file.write_mesh(msh)
    file.write_function(f1, 0)
    file.write_function(f2, 1)

```

Some other links which may have relevant information but I am not able to get it to work:

> [@Issues visualizing multiple fields stored in single XDMF file \[dolfinx\]](https://fenicsproject.discourse.group/t/issues-visualizing-multiple-fields-stored-in-single-xdmf-file-dolfinx/11265):
>
> Hey all, I am experiencing some issues trying to visualize vector fields in ParaView 5.11.1 when multiple functions are stored in a single XDMF file. Maybe this turns out to be a ParaView question, but my impression is that something is wrong with how I write the XDMF files. Consider the following MWE that creates and writes 2 vector fields: import dolfinx import numpy as np from dolfinx import fem, io from mpi4py import MPI from petsc4py import PETSc mesh = dolfinx.mesh.create\_unit\_square(…

Time dependent: [Time-dependent mesh using XDMF File in DOLFINx - #10 by rsz7ncf24p](https://fenicsproject.discourse.group/t/time-dependent-mesh-using-xdmf-file-in-dolfinx/11826/10)

---

<div class="post-metadata">

**Author:** ![Jesus\_Vellojin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jesus_vellojin/32/6485_2.png) [@Jesus\_Vellojin](https://fenicsproject.discourse.group/u/Jesus_Vellojin)\
**Post date:** [April 19, 2024, 3:02am UTC](https://fenicsproject.discourse.group/t/plotting-multiple-functions-in-same-xdmf-file/14334/2 "2024-04-19T03:02:26Z")

</div>

Hi.

Welcome to the FEniCS community!

Changing `file.write_function(f2, 1)` by `file.write_function(f2, 0)` gives the desired result.

Consider the following MWE (adapted from your code):

```auto
from mpi4py import MPI
import dolfinx as dlx
import numpy as np

comm = MPI.COMM_WORLD
rank = comm.rank
nproc = comm.size

msh = dlx.mesh.create_unit_square(comm, 10, 10, dlx.mesh.CellType.quadrilateral)
Vh_m = dlx.fem.FunctionSpace(msh, ("Lagrange", 1))

f1 = dlx.fem.Function(Vh_m,name='f1')
f1.interpolate(
    lambda x: np.sin(x[0]*x[1])
)
f1.x.scatter_forward()

f2 = dlx.fem.Function(Vh_m,name='f2')
f2.interpolate(
    lambda x: 1/np.sqrt((x[0]-0.5)**2 + (x[1]-0.5)**2 + 1.)
)
f2.x.scatter_forward()

with dlx.io.XDMFFile(
    msh.comm,
    "outputmutiple.xdmf".format(nproc),
    "w",
) as file:
    file.write_mesh(msh)
    file.write_function(f1, 0)
    file.write_function(f2, 0)

```

Which produces

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/8/8d440044a995592d8e679bd9e6290f523c350d84.jpeg)

---

<div class="post-metadata">

**Author:** ![VenuVGR](https://avatars.discourse-cdn.com/v4/letter/v/f04885/32.png) [@VenuVGR](https://fenicsproject.discourse.group/u/VenuVGR)\
**Post date:** [April 19, 2024, 4:18am UTC](https://fenicsproject.discourse.group/t/plotting-multiple-functions-in-same-xdmf-file/14334/3 "2024-04-19T04:18:41Z")

</div>

Thank you so much for the quick help. It works now.
