Saving mesh and mesh_tags using adios4dolfinx

Suppose I have a mesh created by

mesh, cell_tags, facet_tags = model_to_mesh(gmsh.model, MPI.COMM_WORLD, 0,gdim=2)

Then I save the mesh and the facet_tags as follows:

import adios4dolfinx as adx
from pathlib import Path

mesh_to_save  = mesh
facet_to_save = facet_tags

facet_to_save.name = "my_facet_tag"
checkpoint_file    = Path("function_checkpoint.bp")
adx.write_mesh(mesh_to_save, checkpoint_file, engine="BP4")
adx.write_meshtags(checkpoint_file,mesh_to_save,facet_to_save, engine="BP4")

Next I import the mesh and the facet tags as follows:

new_mesh = adx.read_mesh(MPI.COMM_WORLD, checkpoint_file, "BP4", dolfinx.mesh.GhostMode.none)
new_ft   = adx.read_meshtags(checkpoint_file, new_mesh, meshtag_name="my_facet_tag", engine="BP4" )

For these adios4dolfinx codes, I used the details mentioned in:
adios4dolfinx/tests/test_meshtags.py at main Ā· jorgensd/adios4dolfinx Ā· GitHub

My question
Is there a difference between mesh vs new_mesh or facet_tags vs new_ft?
Just by the looks of it, both should be the same, but Iā€™m worried whether adios4dolfinx is doing any sort of changes when saving the file.

When I ran this code the results I got looks like:
image
image

You can notice that the indexing at the end is differentā€¦

Appreciate your help

Adios does not do any sort changes, But it uses the dolfinx mesh, which is repartitioned when read in. This has been explained in:

I have just made a checkpointing function that writes the data in the same order as the input mesh, see:

This means that you can read your mesh and meshtags from an XDMFFile, and only read the function from the checkpoint.

1 Like

@dokken Thank you so much for the quick fix.
So in this one, may I know how to read and write the mesh tagsā€¦

I checked the post you shared as well as: adios4dolfinx/tests/test_original_checkpoint.py at dokken/original_mesh_checkpointing Ā· jorgensd/adios4dolfinx Ā· GitHub

But I didnā€™t see the meshtag read/write command in it.

The whole point of that checkpoint is that your mesh (and preferrably meshtag) should come from another file (say xdmf).

I do not see much motivation in adding a write_meshtags_to_original_mesh founction, as either the meshtag is read from a file (along with the mesh) or it is generated during simulation (and thus belongs with the partitioned mesh).

1 Like

Mhā€¦ Iā€™m sorry but Iā€™m a bit confused nowā€¦

If the mesh is generated by a function like this:
mesh, cell_tags, facet_tags =dolfinx.io.gmshio.model_to_mesh(gmsh.model, MPI.COMM_WORLD, 0,gdim=2)

and if we want to reuse both mesh and the facet_tags,
can we only save the mesh using xdmf and extract out the mesh tags from that file?

Using model_to_mesh allready restructures data.

I Also wouldnt rely on a Gmsh.model to be consistent over time, as meshing algorithms can change.

For your code, i would either:

  1. generate the mesh with Gmsh, read it with gmshio.model_to_mesh and save it as an xdmf as a separate code. Then in your main script, load mesh and meshtags from xdmf, and use the new checkpointing function to write/read functions to/from this mesh(defined in the xdmf file).
  2. store the mesh, meshtags and function in a checkpoint with write/checkpoint
1 Like

Ah did you mean:

mesh, cell_tags, facet_tags =dolfinx.io.gmshio.model_to_mesh(gmsh.model, MPI.COMM_WORLD, 0,gdim=2)

with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "xdmf_mesh_save", "w") as xdmf:
        xdmf.write_mesh(mesh)

This gives me a .h5 file as well.

Then to upload and reuse the saved file:

with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "xdmf_mesh_save", "r") as xdmf_upload:
    mesh_from_xdmf = xdmf_upload.read_mesh()
  1. Can you please elaborate on what you meant by ā€œuse the new checkpointing function to write/read functions to/from this meshā€
  2. Also is there a recommended way to extract out the mesh tags from mesh_from_xdmf

Imagine the following pipeline

mesh, cell_tags, facet_tags =dolfinx.io.gmshio.model_to_mesh(gmsh.model, MPI.COMM_WORLD, 0,gdim=2)

with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "mesh.xdmf", "w") as xdmf:
        xdmf.write_mesh(mesh)
        xdmf.write_meshtags(cell_tags)
        xdmf.write_meshtags(facet_tags)

V = dolfinx.fem.functionspace(...)
u = dolfinx.fem.Function(V)
adios4dolfinx.write_function_on_input_mesh(u, "checkpoint.bp", engine="BP4", time=0.0)

Then in another program (or at a later stage in the current program), you call

with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "mesh.xdmf", "r") as xdmf:
        mesh = xdmf.read_mesh()
        cell_tags = xdmf.read_meshtags(name= "Cell tags")
        facet_tags = xdmf.read_meshtags(name= "Facet tags")

V = dolfinx.fem.functionspace(...)
u = dolfinx.fem.Function(V)
adios4dolfinx.read_function(u, "checkpoint.bp", engine="BP4", time=0.0)
1 Like

@dokken
Thank you so much for the example code. I really appreciate it.
So in hereā€¦

  1. You are writing the function to a separate file. Am I correct? That is not to the one we used to save the mesh tags.

  2. I saw what you have written in here: Writing standalone function. But Iā€™m a little confused whether it creates its own mesh and save the function or somehow include the function in the xdmf file we saved earlier.
    Iā€™m asking this question because, if we just used adios4dolfinx.write_function then it was written on the mesh that was saved using adios4dolfinx.write_mesh

  3. Also, if we want to use the write_function_on_input_mesh function, do we have to re install the entire docker image and install adios4dolfinx again or is there a better way to get that function (Iā€™m sorry but Iā€™m still learning how to mauver the git repository.)

With the example above, the file with the mesh and meshtags are different files. This is so you can visualize the mesh in paraview.
M

If you store the function with Ā«write on input mesh", you do not need to store the mesh.

If you use ā€œwrite_checkpointā€ you have to store the mesh with write_mesh.

1 Like

Just reinstall adios4dolfinx (git clone + python3 -m pip install./adios4dolfinx).

1 Like

@dokken Thank you so much for your continued support. I really appreciate it.

And just for the completeness of this postā€¦

I think xdmf.write_meshtags(facet_tags) needs to be changed to: xdmf.write_meshtags(mesh.geometry, facet_tags)

For newer versions of dolfnx it should be
xdmf.write_meshtags(facet_tags, mesh.geometry)

Ah I see. How about the read_meshtagsā€¦?

Please read the documentation:
https://docs.fenicsproject.org/dolfinx/v0.7.3/python/generated/dolfinx.io.html#dolfinx.io.XDMFFile

1 Like

Thanks for the link. I will go through them. I appreciate it.