Read mesh from XDMF file (write_checkpoint)

Hi all,

I want to read a mesh from a output xdmf file as follow:

from fenics import *
mesh = UnitSquareMesh(10, 10)

V = FunctionSpace(mesh, 'P', 1)

u = Function(V)

XDMFFile("out.xdmf").write_checkpoint(u, "u", 0)

mesh2 = Mesh()

XDMFFile("out.xdmf").read(mesh2)

but this yields the following error:

Traceback (most recent call last):
  File "plot.py", line 10, in <module>
    XDMFFile("out.xdmf").read(mesh2)
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to recognise cell type.
*** Reason:  Unknown value "".
*** Where:   This error was encountered inside XDMFFile.cpp.
*** Process: 0
***
*** DOLFIN version: 2019.1.0
*** Git changeset:  74d7efe1e84d65e9433fd96c50f1d278fa3e3f3f
*** -------------------------------------------------------------------------

any idea how to read a mesh from a xdmf file created with write_checkpoint ?

Cheers
Rem

1 Like

Hi, I think to read the xdmf, you can refer to this post
https://fenicsproject.org/qa/11348/read-in-mesh-and-functins-from-xdmf-file/

run the following
from dolfin import *
mesh = Mesh()
filename = “mesh.xdmf”
f = XDMFFile(mpi_comm_world(), filename)
f.read(mesh, True)
plot(mesh)

To use read, you need to use write. However, you can combine the two as follows:

from fenics import *
mesh = UnitSquareMesh(10, 10)

V = FunctionSpace(mesh, 'P', 1)

u = Function(V)
u.vector()[:] = 2
with XDMFFile("out.xdmf") as outfile:
    outfile.write(mesh)
    outfile.write_checkpoint(u, "u", 0, append=True)

mesh2 = Mesh()
u2 = Function(V)
with XDMFFile("out.xdmf") as infile:
    infile.read(mesh2)
    infile.read_checkpoint(u2, "u")
print(u2.vector().get_local())
print(mesh.num_cells(), mesh2.num_cells())

Hi dokken!

I am using XDMFFile in c++ program, such as

  XDMFFile f0("f0.xdmf");
  for (std::size_t n = 0; n < N; n++)
  {
    double t = n*dt;
    f0.write(*u, t);
  }
  f0.close();

If N is too large and we terminate the program before f0.close();, how to show intermediate results in paraview?

I would then rather re-open and append to the file inside the loop to avoid issues with random shut-downs.

1 Like

Setting a parameter can solve this problem:

f0.parameters["flush_output"] = true;

It might be also effective in python.