Gmsh-backend#

Reads and writes Gmsh .msh files through the Gmsh Python API (pip install io4dolfinx[gmsh]), reusing dolfinx.io.gmsh. Meshes, mesh tags (physical groups), point data ($NodeData) and cell data ($ElementData) are supported, with time-dependent data stored as the steps of a view. Complex data is split into the views real_{name} and imag_{name}, as dolfinx.io.XDMFFile does. All file access happens on rank 0. Requires DOLFINx 0.11 or later.

Gmsh interface to io4dolfinx.

Reads and writes Gmsh .msh files (MSH 2.2 and 4.1, ASCII and binary) through the Gmsh Python API, reusing dolfinx.io.gmsh for the geometry, the topology and the node ordering. All file access happens on rank 0. Only the element types of dolfinx.io.gmsh are supported, and DOLFINx 0.11 or later is required.

In DOLFINx terms:

  • As in dolfinx.io.gmsh.model_to_mesh(), the cells are the elements of the highest dimension in a physical group, and every cell is in exactly one physical group. Node i has node tag i + 1. An MSH file holds a single mesh, so meshes are not time dependent.

  • Point and cell data are post-processing views ($NodeData/$ElementData) keyed by node and element tags. The steps of a view are its time series. Complex data is split into the views real_{name} and imag_{name}, as in dolfinx.io.XDMFFile.

  • Mesh tags are physical groups, with the physical tag as the tag value.

SPDX License identifier: MIT

Copyright: Jørgen S. Dokken and Simula Research Laboratory

io4dolfinx.backends.gmsh.backend.get_default_backend_args(arguments: dict[str, Any] | None) → dict[str, Any][source]#

Get default backend arguments given a set of input arguments.

Parameters:

arguments – Input backend arguments

Returns:

  • "gdim": Geometric dimension of a mesh read from file, 3 as in dolfinx.io.gmsh.read_from_msh()

  • "dtype": Floating point type of the coordinates and data read from file, np.float64

  • "binary": Write binary instead of ASCII MSH. None keeps the encoding of an existing file and writes a new one as ASCII.

Return type:

The arguments, with defaults for

io4dolfinx.backends.gmsh.backend.read_cell_data(filename: Path | str, name: str, comm: Comm, time: str | float | None, backend_args: dict[str, Any] | None) → tuple[ndarray[tuple[Any, ...], dtype[int64]], ndarray][source]#

Read cell data ($ElementData) from a Gmsh file.

Parameters:
  • filename – Path to file

  • name – Name of cell data

  • comm – Communicator to launch IO on.

  • time – Time of the step to read, None for the first step

  • backend_args – Arguments to backend, "dtype" sets the type of the data, or of its real and imaginary parts for complex data

Returns:

  • The nodes of the cells with data, as zero-based node indices in DOLFINx ordering, on rank 0

  • The data of these cells, of shape (num_cells, num_components), on rank 0. Complex data, stored as the views real_{name} and imag_{name}, is combined.

Return type:

Tuple containing

io4dolfinx.backends.gmsh.backend.read_function_names(filename: Path | str, comm: Comm, backend_args: dict[str, Any] | None) → list[str][source]#

Read the names of all point and cell data in a file.

Parameters:
  • filename – Path to file

  • comm – MPI communicator to launch IO on.

  • backend_args – Arguments to backend

Returns:

The names of the views in the file, on all ranks. A complex pair of views real_{name} and imag_{name} is listed as name.

io4dolfinx.backends.gmsh.backend.read_mesh_data(filename: Path | str, comm: Comm, time: str | float | None = None, read_from_partition: bool = False, backend_args: dict[str, Any] | None = None) → ReadMeshData[source]#

Read mesh data from a Gmsh file.

As in dolfinx.io.gmsh.model_to_mesh(), the cells are the elements of the highest dimension in a physical group, and every cell must be in exactly one physical group. The cells must be of one type, and node tags contiguous. All data is read on rank 0.

Parameters:
  • filename – Path to file to read from

  • comm – MPI communicator to distribute the mesh over

  • time – Ignored, as an MSH file holds one mesh

  • read_from_partition – Read mesh with partition from file (not supported)

  • backend_args – Arguments to backend, see get_default_backend_args()

Returns:

Internal data structure for the mesh data read from file

io4dolfinx.backends.gmsh.backend.read_meshtags_data(filename: str | Path, comm: Comm, name: str, backend_args: dict[str, Any] | None = None) → MeshTagsData[source]#

Read mesh tags from the physical groups of a Gmsh file.

Parameters:
  • filename – Path to file to read from

  • comm – MPI communicator used in storage

  • name – "cell", "facet", "ridge" or "peak" for all physical groups of that codimension, as in dolfinx.io.gmsh.model_to_mesh(). Otherwise the physical group called name, or the groups called f"{name}_{tag}", as written by write_meshtags().

  • backend_args – Arguments to backend

Returns:

Internal data structure for the mesh tags read from file, with the physical tag as value. All data is on rank 0.

io4dolfinx.backends.gmsh.backend.read_point_data(filename: Path | str, name: str, comm: Comm, time: str | float | None, backend_args: dict[str, Any] | None) → tuple[ndarray, int][source]#

Read point data ($NodeData) from a Gmsh file.

Parameters:
  • filename – Path to file

  • name – Name of point data

  • comm – Communicator to launch IO on.

  • time – Time of the step to read, None for the first step

  • backend_args – Arguments to backend, "dtype" sets the type of the data, or of its real and imaginary parts for complex data

Returns:

  • The data of shape (num_nodes, num_components), ordered by node tag, on rank 0. Nodes without data get zeros. Complex data, stored as the views real_{name} and imag_{name}, is combined.

  • The global start of the data on this process, 0

Return type:

Tuple containing

io4dolfinx.backends.gmsh.backend.read_timestamps(filename: Path | str, comm: Comm, function_name: str, backend_args: dict[str, Any] | None) → ndarray[tuple[Any, ...], dtype[float64 | str]][source]#

Read the times of the steps of point or cell data.

Parameters:
  • filename – Path to file to read from

  • comm – MPI communicator used in storage

  • function_name – Name of the point or cell data

  • backend_args – Arguments to backend

Returns:

The time of each step, on all ranks

io4dolfinx.backends.gmsh.backend.write_data(filename: Path | str, array_data: ArrayData, comm: Comm, time: str | float | None, mode: FileMode, backend_args: dict[str, Any] | None)[source]#

Write point or cell data to a Gmsh file, as a step of the view named after it.

Point data is written as $NodeData and cell data as $ElementData, keyed by global index + 1. Complex data is split into the views real_{name} and imag_{name}, as dolfinx.io.XDMFFile does. The mesh must have been written to the file with write_mesh(), from the same mesh.

Parameters:
  • filename – Path to file

  • array_data – Data to write to file

  • comm – MPI communicator to open the file with

  • time – Time of the step, None for 0

  • mode – FileMode.append adds the step, or replaces the step at the same time; FileMode.write replaces all steps of the data

  • backend_args – Arguments to backend, "binary" selects binary MSH

io4dolfinx.backends.gmsh.backend.write_mesh(filename: Path | str, comm: Comm, mesh: MeshData, backend_args: dict[str, Any] | None = None, mode: FileMode = FileMode.write, time: float = 0.0)[source]#

Write a mesh to a Gmsh file.

Nodes and cells get the tag one above their global index. The cells form a single entity in the physical group with tag 1, the default cell marker, as every cell must be in a physical group (see dolfinx.io.gmsh.model_to_mesh()). Cell tags written with write_meshtags() replace it.

Parameters:
  • filename – Path to file to write to, with suffix .msh

  • comm – MPI communicator used in storage

  • mesh – Internal data structure for the mesh data to save to file

  • backend_args – Arguments to backend, "binary" selects binary MSH

  • mode – Only FileMode.write, as an MSH file holds one mesh

  • time – Ignored, as an MSH file holds one mesh

io4dolfinx.backends.gmsh.backend.write_meshtags(filename: str | Path, comm: Comm, data: MeshTagsData, backend_args: dict[str, Any] | None = None)[source]#

Write mesh tags to a Gmsh file as physical groups.

Each tag value becomes a physical group with the value as tag, named f"{data.name}_{value}". Gmsh physical tags are positive and unique per dimension, so tag values must be positive.

Cell tags replace the marker of the cells they cover, moving them to the physical group of their value, while other cells keep their marker, e.g. the default marker 1 of write_mesh(). Markers no cell has any more are removed. Entities of lower dimension are added as new elements, and only one set of mesh tags can be written per lower dimension.

Parameters:
  • filename – Path to file to write to, holding a mesh written with write_mesh()

  • comm – MPI communicator used in storage

  • data – Internal data structure for the mesh tags to save to file

  • backend_args – Arguments to backend, "binary" selects binary MSH