Saving multiple functions in one file to visualize it

Hi, I’d like to visualize multiple functions using a single xdmf file. I tried

with XDMFFile(MPI.comm_world, 'filename.xdmf') as file:
    file.write(u, 0.)
    file.write(rho, 0.)

where u is a vector field and rho is a scalar field.
I want to check coloring contour on a deformed shape with the vector field u, but there are two issues:
First, I cannot find a scalar field
image

Second, there is another vtk composite blocking the fields I want to see.

How can I visualize as I want?

Plus, is there any way to visualize dolfin results with HyperView?

I’d like to visualize multiple functions using a single xdmf file

Try

with XDMFFile(MPI.comm_world, 'filename.xdmf') as file:
    file.parameters.update(
    {
        "functions_share_mesh": True,
        "rewrite_function_mesh": False
    })
    file.write(u, 0.)
    file.write(rho, 0.)
4 Likes

@bhaveshshrimali OMG… Thank you soooooo much! :smiley:

And if I want to save the save two variables in different time-step and to memorize all of them? If I insert this expression in a while loop it overwrites the solutions each time.
Thank you in advance

write allows you pass a time stamp as well, if that’s what you are referring to, namely

while t < T:
    # do something
    file.write(u, t)
    file.write(rho, t)
    t += dt

I tried in this way but it gives me the following error:
Segmentation fault (core dumped)

What I made wrong?

Use the Xdmf3ReaderT to open the files. (There should be three options) it would also help if you supply the version of Paraview you are using.

I am using Paraview 5.9.1

I have two different problems.
1)

with XDMFFile(MPI.comm_world, ‘filename.xdmf’) as file:
file.parameters.update(
{
“functions_share_mesh”: True,
“rewrite_function_mesh”: False
})
file.write(u, 0.)
file.write(rho, 0.)

while t < T:
# do something
file.write(u, t)
file.write(rho, t)
t += dt

It does not even finish the first iteration and it gives me the error: Segmentation fault (core dumped)

filexdmf = XDMFFile(MPI.comm_world, “risultati.xdmf”)
file.write(u, 0.)
file.write(rho, 0.)

while t < T:
# do something
file.write(u, t)
file.write(rho, t)
t += dt

Everything works fine, but I cannot open the ParaView file (even with Xdmf3ReaderT) because it crashes

I cannot reproduce this behavior with the following MWE:

from dolfin import *

mesh = UnitSquareMesh(10,10)
V = FunctionSpace(mesh, "CG", 1)
u = Function(V)
u.rename("u", "")
rho = Function(V)
rho.rename("rho", "")
with XDMFFile(MPI.comm_world, "filename.xdmf") as file:
    file.parameters.update(
    {
    "functions_share_mesh": True,
    "rewrite_function_mesh": False
    })
    file.write(u, 0.)
    file.write(rho, 0.)
    dt = 0.1
    T = 0.5
    t = dt
    while t < T:
        u.vector()[:] = t
        # do something
        file.write(u, t)
        rho.vector()[:] = 0.5*t
        file.write(rho, t)
        t += dt

using the docker image quay.io/fenicsproject/stable:current and Paraview 5.9.0.

For further comments on this issue, please do the following:

  1. Create a minimal working code example (similar to the snippet I have above) producing the error (and include the error message
  2. Format code using 3x` encapsulation
  3. Specify which version of dolfin (and how you installed it).
2 Likes

Hello,

I am looking to reproduce this multi-function saving in dolfinx. If I understand correctly, I need to run both write_mesh and write_function? What if some functions are defined on refined versions of the meshes used for other functions? Can they still be saved in the same file?

Many thanks!

I would not save multiple meshes to the same file (I do not think it is supported).
You should save functions for the unrefined mesh to one file, and functions on the refined mesh in another file.

All right, that makes sense thank you.

Hi,

How is it possible to write two functions in one file in the new version of Dolfinx?

Using the previous solution, I got this error:
AttributeError: 'XDMFFile' object has no attribute 'parameters'.

Thanks

Seems that in the new version it is not necessary to update the parameters. Is this function precoded?

Without a minimal code example it is hard to give you any pointers. In general the XDMFFile code is completely rewritten, so that to save a function it has to be associated with a mesh, see dolfinx/XDMFFile.h at main · FEniCS/dolfinx · GitHub

1 Like

Thanks @dokken for the link! I was talking about your code above (May '21).

In that case, this part of the code is no longer needed:

file.parameters.update(
    {
    "functions_share_mesh": True,
    "rewrite_function_mesh": False
    })

Solved! Thanks