Skip to content

Helpers API

gmshparser.helpers contains two groups of functions: visualization adapters for 2D meshes and low-level line parsers used by section parsers.

get_triangles()

gmshparser.helpers.get_triangles(mesh)

Return (X, Y, triangles) for three-node surface triangles.

Both :func:gmshparser.read meshes and compatibility :func:gmshparser.parse meshes are accepted. Connectivity is converted to zero-based indices into X and Y.

Source code in gmshparser/helpers.py
def get_triangles(mesh: Any) -> tuple[list[float], list[float], list[list[int]]]:
    """Return ``(X, Y, triangles)`` for three-node surface triangles.

    Both :func:`gmshparser.read` meshes and compatibility
    :func:`gmshparser.parse` meshes are accepted. Connectivity is converted to
    zero-based indices into ``X`` and ``Y``.
    """
    return _indexed_cells(mesh, element_type=2)

Returns (X, Y, triangles). Triangle connectivity contains zero-based indices into X and Y.

import matplotlib.pyplot as plt
from gmshparser.helpers import get_triangles

X, Y, triangles = get_triangles(mesh)
plt.triplot(X, Y, triangles)

Only two-dimensional Gmsh type-2 elements are included.

get_quads()

gmshparser.helpers.get_quads(mesh)

Return (X, Y, quads) for four-node surface quadrilaterals.

Both modern and compatibility mesh objects are accepted. Connectivity is converted to zero-based indices into X and Y.

Source code in gmshparser/helpers.py
def get_quads(mesh: Any) -> tuple[list[float], list[float], list[list[int]]]:
    """Return ``(X, Y, quads)`` for four-node surface quadrilaterals.

    Both modern and compatibility mesh objects are accepted. Connectivity is
    converted to zero-based indices into ``X`` and ``Y``.
    """
    return _indexed_cells(mesh, element_type=3)

Returns (X, Y, quads). Quad connectivity contains zero-based indices into X and Y.

from matplotlib.patches import Polygon
from gmshparser.helpers import get_quads

X, Y, quads = get_quads(mesh)
for quad in quads:
    coordinates = [(X[index], Y[index]) for index in quad]
    axes.add_patch(Polygon(coordinates, fill=False))

Only two-dimensional Gmsh type-3 elements are included.

get_elements_2d()

gmshparser.helpers.get_elements_2d(mesh)

Return node coordinates, triangles, and quadrilaterals for plotting.

Connectivity in triangles and quads retains the original Gmsh node tags. The function accepts both the modern and compatibility APIs.

Source code in gmshparser/helpers.py
def get_elements_2d(mesh: Any) -> dict[str, Any]:
    """Return node coordinates, triangles, and quadrilaterals for plotting.

    Connectivity in ``triangles`` and ``quads`` retains the original Gmsh node
    tags. The function accepts both the modern and compatibility APIs.
    """
    triangles: list[list[int]] = []
    quads: list[list[int]] = []
    node_ids: set[int] = set()

    for dimension, element_type, _, connectivity in _elements(mesh):
        if dimension != 2:
            continue

        node_ids.update(connectivity)
        if element_type == 2:
            triangles.append(connectivity)
        elif element_type == 3:
            quads.append(connectivity)

    coordinates = dict(_nodes(mesh))
    nodes = {
        tag: (coordinates[tag][0], coordinates[tag][1]) for tag in sorted(node_ids)
    }

    return {
        "nodes": nodes,
        "triangles": triangles,
        "quads": quads,
        "node_ids": sorted(node_ids),
    }

Returns a dictionary rather than coordinate arrays:

from gmshparser.helpers import get_elements_2d

data = get_elements_2d(mesh)

nodes = data["nodes"]
triangles = data["triangles"]
quads = data["quads"]
node_ids = data["node_ids"]
  • nodes maps original Gmsh node tags to (x, y) coordinates.
  • triangles and quads preserve original node tags in their connectivity.
  • node_ids is the sorted list of referenced node tags.

Do not unpack this helper as (X, Y, triangles, quads) and do not apply a -1 offset to its node tags.

Line parsers

gmshparser.helpers.parse_ints(io)

Parse one required line from io as integers.

Source code in gmshparser/helpers.py
def parse_ints(io: TextIO) -> list[int]:
    """Parse one required line from *io* as integers."""
    line = read_required_line(io, "an integer record")
    return [int(value) for value in line.strip().split()]

gmshparser.helpers.parse_floats(io)

Parse one required line from io as floating-point values.

Source code in gmshparser/helpers.py
def parse_floats(io: TextIO) -> list[float]:
    """Parse one required line from *io* as floating-point values."""
    line = read_required_line(io, "a floating-point record")
    return [float(value) for value in line.strip().split()]

These functions read one line from a text stream and convert its space-separated values. They are primarily parser-development utilities rather than application-level mesh APIs.

See also