Store function with meshio

Coming back to FEniCS after a while, I forgot how to store mesh functions with meshio. Here’s what I tried, but the order of the points is messed up:

import numpy as np
import meshio
import meshzoo
from dolfin import FunctionSpace, Mesh, MeshEditor, Expression, project

points, cells = meshzoo.disk(6, 10)

editor = MeshEditor()
mesh = Mesh()
# topological and geometrical dimension 2
editor.open(mesh, "triangle", 2, 2, 1)
editor.init_vertices(len(points))
editor.init_cells(len(cells))
for k, point in enumerate(points):
    editor.add_vertex(k, point[:2])
for k, cell in enumerate(cells.astype(np.uintp)):
    editor.add_cell(k, cell)
editor.close()
V = FunctionSpace(mesh, "CG", 1)

f1 = Expression("x[0]", element=V.ufl_element())
e1 = project(f1, V).vector().get_local()

meshio.Mesh(
    V.mesh().coordinates(),
    {"triangle": V.mesh().cells()},
    point_data={"e1": e1},
).write("out.vtk")

sh
If there’s a better way of creating a mesh from points and cells, let me know, too!

1 Like

I would suggest saving your data to xdmf, as explained here: https://jsdokken.com/converted_files/tutorial_pygmsh.html#second

I’ve also made a similar version coming from cell-sets (pygmsh)

and

That doesn’t show how to store point_data, does it?

Sorry Nico, I was a bit to fast reading through your post. How about using the vertex_to_dof_map?

from dolfin import *
import meshio
mesh = UnitSquareMesh(5, 5)
V = FunctionSpace(mesh, "CG", 1)

f1 = Expression("x[0]", element=V.ufl_element())
e1 = project(f1, V).vector().get_local()
v_to_d = vertex_to_dof_map(V)
# vertex_to_dof_map
meshio.Mesh(
    V.mesh().coordinates(),
    {"triangle": V.mesh().cells()},
    point_data={"e1": e1[v_to_d]},
).write("out.vtk")

For higher order spaces where there is no 1-to-1 map, I would use:

dolfin.Function.compute_vertex_values()

which would interpolate to vertices of the mesh.

1 Like

That’s what I was looking for. Thanks!