The MeshParser class#

What it does#

Based on the gmsh API, the MeshParser class is used to parse the data extracted from the .msh file provided by the user. Unless you know what you are doing, do not access and modify this class.

Documentation#

class watlab.utils.MeshParser(msh_mesh)#

Extracts information from .msh files to make them compatible with Watlab.

This class mainly uses the GMSH API to extract mesh data. Watlab requires information on cells, nodes, regions, and other mesh-related attributes. This class should not be used directly by end users; it is intended to be initialized internally by Watlab when a mesh file needs to be processed.

Parameters:

msh_mesh (str) – Path to a GMSH .msh file containing the mesh.

Example

from watlab.utils.meshParser import MeshParser

parser = MeshParser("my_mesh.msh")
__sort_elements_by_tag(tags, elements)#

Sort elements based on their tags.

This private method sorts a set of elements according to their corresponding tags, ensuring that the elements and tags arrays are aligned in ascending order.

Parameters:
  • tags (numpy.ndarray) – Array of element tags of shape (n,) or (n, 1).

  • elements (numpy.ndarray) – Array of elements to be sorted, of shape (n, dim).

Returns:

  • elements (numpy.ndarray) – Sorted array of elements.

  • tags (numpy.ndarray) – Sorted array of tags.

Return type:

tuple(numpy.ndarray, numpy.ndarray)

Example

parser = MeshParser("my_mesh.msh")
sorted_elements, sorted_tags = parser._MeshParser__sort_elements_by_tag(tags, elements)
print(sorted_elements.shape)
print(sorted_tags.shape)
extract_cells()#

Extract cells from the GMSH mesh.

This method retrieves all triangular mesh cells (elements of type 2) along with their tags using the internal GMSH mesh object. It uses the private __parse_elements method to reshape the connectivity and tags arrays.

Returns:

  • cells (numpy.ndarray) – Array of cell connectivity indices of shape (n_cells, 3).

  • tags_cells (numpy.ndarray) – Array of cell tags of shape (n_cells,).

Return type:

tuple(numpy.ndarray, numpy.ndarray)

Raises:

Exception – If the mesh does not contain any cells (triangular elements of type 2).

Example

parser = MeshParser("my_mesh.msh")
cells, tags_cells = parser.extract_cells()
print(cells.shape)       # (number_of_cells, 3)
print(tags_cells.shape)  # (number_of_cells,)
extract_cells_barycenters()#

Extract the barycenters (geometric centers) of all cells in the GMSH mesh.

This method computes the barycenters for each triangular cell (2D elements) in the mesh using the internal GMSH mesh object.

Returns:

A numpy array of shape (n_cells, 3) containing the barycenter coordinates (x, y, z) for each cell in the mesh.

Return type:

numpy.ndarray

Example

parser = MeshParser("my_mesh.msh")
barycenters = parser.extract_cells_barycenters()
print(barycenters.shape)  # (number_of_cells, 3)
print(barycenters[:5])    # coordinates of the first 5 cell barycenters
extract_cells_by_physical_group(dim) dict#

Extract cells for each entity of a physical group and organize them in a dictionary.

This method collects all elements (cells) belonging to each physical group of the specified dimension and returns them in a dictionary where the keys are the physical group tags and the values are arrays of element tags.

Parameters:

dim (int) – Dimension of the physical groups to extract (0 = points, 1 = edges, 2 = surfaces).

Returns:

A dictionary mapping each physical group tag to a numpy array of element tags.

Return type:

dict

Example

parser = MeshParser("my_mesh.msh")
surface_cells = parser.extract_cells_by_physical_group(2)
for group_tag, cell_tags in surface_cells.items():
    print(f"Group {group_tag} has {len(cell_tags)} elements")
extract_edges()#

Extract edges (interfaces) from the GMSH mesh along with their tags and the cells on either side of each edge.

This method retrieves all mesh edges from triangular cells, ensures a consistent positive orientation, and identifies the left and right cells for each edge. Edge tags and connectivity are returned both sorted and unsorted.

Returns:

  • edgeNodes (numpy.ndarray) – Array of nodes composing each edge, oriented positively, shape (n_edges, 2).

  • edgeCells (numpy.ndarray) – Array of left and right cell tags for each edge, shape (n_edges, 2).

  • tags (numpy.ndarray) – Array of unique edge tags (unsorted), shape (n_edges,).

  • edgeTags (numpy.ndarray) – Array of all edge tags (sorted), shape (n_edges,).

Return type:

tuple(numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray)

Raises:

Exception – If any edge orientation is different from -1 or 1.

Example

parser = MeshParser("my_mesh.msh")
edgeNodes, edgeCells, tags, edgeTags = parser.extract_edges()
print(edgeNodes.shape)  # (number_of_edges, 2)
print(edgeCells.shape)  # (number_of_edges, 2)
print(tags.shape)       # (number_of_edges,)
print(edgeTags.shape)   # (number_of_edges,)
extract_nodes()#

Extracts nodes from the GMSH mesh

This method retrieves all mesh nodes along with their tags using the internal GMSH mesh object. It calls the private __parse_elements method to reshape the nodes and tags arrays.

Returns:

  • nodes (numpy.ndarray) – Array of node coordinates of shape (n_nodes, 3).

  • tags (numpy.ndarray) – Array of node tags of shape (n_nodes,).

Return type:

tuple(numpy.ndarray, numpy.ndarray)

Example

parser = MeshParser("my_mesh.msh")
nodes, tags = parser.extract_nodes()
print(nodes.shape)  # (number_of_nodes, 3)
print(tags.shape)   # (number_of_nodes,)
extract_physical_groups(dim) dict#

Extract physical groups of a given dimension from the GMSH mesh with their names.

Physical groups are used in GMSH to label entities such as points (0D), edges (1D), or surfaces (2D) with a meaningful name. This method returns a dictionary mapping the group tag to its name for the specified dimension.

Parameters:

dim (int) – Dimension of the physical groups to extract (0 = points, 1 = edges, 2 = surfaces).

Returns:

A dictionary where keys are group tags and values are group names.

Return type:

dict

Example

parser = MeshParser("my_mesh.msh")
edge_groups = parser.extract_physical_groups(1)
print(edge_groups)  # {tag1: "river", tag2: "boundary", ...}