Dear community,
similar questions on the forum
there have been several questions on how to read xdmf files (that store fenicsx functions and meshes) in order to visualize them in pyvista. With this I mean creating a plot of the mesh or function (see e.g. here); or creating a gif of a time-dependent function (e.g. here).
- Read exported solution back into fenicsx about how to do the above in parallel. It is suggested to use the adios package. Can I achieve my goal without this package in certain cases - e.g. if I am fine without parallel computing?
- there are discussions about this for legacy fenicsx, e.g. Loading xdmf data back in - #4 by dokken or Access function in time dependent XDMFFile - #6 by RemDelaporteMathurin or Save solution and load it in the next iteration
- somebody wanted to read only the last time-step of a time-dependent function from an xdmf file, here: Read solution of last time step from file but this post was created before the publication of the adios package, so it also does not answer my question.
Saving mesh and function data in memory-efficient ways (i.e. by only saving raw function and mesh values) is important for me to be able to reproduce plots and gifs efficiently, without recomputing solutions.
my appraoch fails
My naive approach is to do the following:
from dolfinx.fem.petsc import LinearProblem
from mpi4py import MPI
import numpy as np
import ufl
from dolfinx import fem
from ufl import dx, grad, inner
from basix.ufl import element
from dolfinx.fem import dirichletbc, functionspace, locate_dofs_topological
from dolfinx.mesh import create_unit_square, locate_entities_boundary
# mesh, spaces, BC
msh = create_unit_square(MPI.COMM_WORLD, 10,10)
P1 = element("Lagrange", "triangle", 1)
XV = functionspace(msh, P1)
facetsV = locate_entities_boundary(msh, dim=1,
marker=lambda x: np.logical_or.reduce((
np.isclose(x[0], 0.0),
np.isclose(x[0], 1.0),
np.isclose(x[1], 0.0),
np.isclose(x[1], 1.0))))
dofsV = locate_dofs_topological(XV, entity_dim=1, entities=facetsV)
BCs = [dirichletbc(0.0,dofsV, XV)]
# variational problem
u = ufl.TrialFunction(XV)
v = ufl.TestFunction(XV)
x = ufl.SpatialCoordinate(msh)
f = 1
a = inner(grad(u), grad(v)) * dx
L = inner(f, v) * dx
problem = LinearProblem(a, L, bcs=BCs, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
# xdmf file
from dolfinx import io
xdmf = io.XDMFFile(msh.comm, "test.xdmf", "w") # Open XDMF file for writing
xdmf.write_mesh(msh)
xdmf.write_function(uh)
xdmf.close()
# read test.xdmf
import pyvista as pv
reader = pv.get_reader("test.xdmf")
grid = reader.read()
# plot using pyvista
p = pv.Plotter()
p.add_mesh(grid)
p.show()
but the code (in particular the line grid = reader.read()
) returns
[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see https://petsc.org/release/faq/#valgrind and https://petsc.org/release/faq/
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run
[0]PETSC ERROR: to get more information on the crash.
[0]PETSC ERROR: Run with -malloc_debug to check if memory corruption is causing the crash.
Abort(59) on node 0 (rank 0 in comm 0): application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0
which I cant explain, as .read()
seems to be well-implemented, see here. A similar approach for a time-dependent problem (in order to create a snapshot or gif of a function) fails as well.
This means, that at the moment I can only produce visualizations directly inside the same file that also produces the function i want to visualize.
my question:
Is the best practice to use adios, or can it be avoided in simpler cases (serial, Lagrange elements, etc)?
Or do professional fenicsx users - who have a similar workflow consisting of saving solutions as raw data in order to create visualisations later on - stick to paraview?