How to open a result?

I am new to fenics and want to do a quite simple task, but having some troubles.

I solved a lot of pressure/velocity fields in a permanent regime and saved them in XMDF/H5 format.
I have only saved the velocity field (2 dimensional).

Now, I want to open it in python to generate a visualization (yes, I want to do it with python since I have an issue with the files to open them all in paraview).

However, I don’t know how to open a file. I’ve tried some internet found solutions, but they didn’t work.
I would appreciate it if someone could share a bit of code, so I could open those files. It would also be nice if you could tell me how to get the coordinates (x,y) and velocity (U) from this file.

Many thanks

Are you using legacy fenics or fenicsx?

If you are using legacy fenics, the 2 ways to do this are

  1. Saving the solution in an XDMFFile with write_checkpoint and reading it back in with read_checkpoint. Some useful info can be found in this post.
  2. Saving the solution in a HDF5File with the 'w' (write) argument, which can then be read back into python using the 'r' (read) argument. Some relevant info is in this post.

For dolfinx, the best way I’ve found is to use meshio to read the DOF arrays. e.g.

import meshio

mesh_meshio = meshio.read(xdmf_filename)
u = mesh_meshio.point_data["name"]

or

u = mesh_meshio.cell_data["name"]

for DG0 functions.

If you need to construct a dolfinx function from this, you will need to reorder the cell indices to match the dolfinx ordering.

def permute_array(array, perm_indices):
    output = array.copy()
    for i in range(len(array)):
        output[i] = array[perm_indices[i]]
    return output

CG1 = dolfinx.fem.VectorFunctionSpace(mesh, ("CG", 1))
u_dolfinx = dolfinx.fem.Function(CG1)
u_dolfinx.x.array[:] = permute_array(u, mesh.geometry.input_global_indices).flatten()

or for DG0 functions

DG0 = dolfinx.fem.VectorFunctionSpace(mesh, ("DG", 0))
u_dolfinx = dolfinx.fem.Function(DG0)
u_dolfinx.x.array[:] = permute_array(u, mesh.topology.original_cell_index).flatten()

Please note that this will only work on a single process.

1 Like

Thank you. This is the simplest solution Ive found.