Helpers API
Utility functions for common mesh operations.
get_triangles()
Extract triangular elements from mesh for plotting.
gmshparser.helpers.get_triangles(mesh)
Return tuple (X, Y, T) of triangular data.
Data can be used effectively in matplotlib's triplot:
X, Y, T = get_triangles(mesh)
plt.triplot(X, Y, T)
Source code in gmshparser/helpers.py
| def get_triangles(mesh):
"""Return tuple (X, Y, T) of triangular data.
Data can be used effectively in matplotlib's `triplot`:
>>> X, Y, T = get_triangles(mesh)
>>> plt.triplot(X, Y, T)
"""
elements = {}
nodes = {}
node_ids = set()
for entity in mesh.get_element_entities():
eltype = entity.get_element_type()
if entity.get_dimension() == 2 and eltype == 2:
for element in entity.get_elements():
elid = element.get_tag()
elcon = element.get_connectivity()
elements[elid] = elcon
for c in elcon:
node_ids.add(c)
for entity in mesh.get_node_entities():
for node in entity.get_nodes():
nid = node.get_tag()
if nid not in node_ids:
continue
ncoords = node.get_coordinates()
nodes[nid] = ncoords
invP = {}
X = []
Y = []
for i, nid in enumerate(node_ids):
invP[nid] = i
X.append(nodes[nid][0])
Y.append(nodes[nid][1])
T = []
for element in elements.values():
T.append([invP[c] for c in element])
return X, Y, T
|
Example:
import gmshparser
import matplotlib.pyplot as plt
from gmshparser.helpers import get_triangles
mesh = gmshparser.parse("mesh.msh")
X, Y, T = get_triangles(mesh)
plt.triplot(X, Y, T)
plt.show()
get_quads()
Extract quadrilateral elements from mesh.
gmshparser.helpers.get_quads(mesh)
Return tuple (X, Y, Q) of quadrilateral data.
Extracts 4-node quadrilateral elements (element type 3) from mesh.
Data can be used with matplotlib's patches:
X, Y, Q = get_quads(mesh)
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, ax = plt.subplots()
for quad in Q:
coords = [[X[i], Y[i]] for i in quad]
polygon = patches.Polygon(coords, fill=False, edgecolor='black')
ax.add_patch(polygon)
Parameters
mesh : Mesh
Mesh object containing quadrilateral elements
Returns
X : list
List of x-coordinates of nodes
Y : list
List of y-coordinates of nodes
Q : list
List of quadrilateral connectivity, each entry is [n0, n1, n2, n3]
Source code in gmshparser/helpers.py
| def get_quads(mesh):
"""Return tuple (X, Y, Q) of quadrilateral data.
Extracts 4-node quadrilateral elements (element type 3) from mesh.
Data can be used with matplotlib's patches:
>>> X, Y, Q = get_quads(mesh)
>>> import matplotlib.pyplot as plt
>>> import matplotlib.patches as patches
>>> fig, ax = plt.subplots()
>>> for quad in Q:
>>> coords = [[X[i], Y[i]] for i in quad]
>>> polygon = patches.Polygon(coords, fill=False, edgecolor='black')
>>> ax.add_patch(polygon)
Parameters
----------
mesh : Mesh
Mesh object containing quadrilateral elements
Returns
-------
X : list
List of x-coordinates of nodes
Y : list
List of y-coordinates of nodes
Q : list
List of quadrilateral connectivity, each entry is [n0, n1, n2, n3]
"""
elements = {}
nodes = {}
node_ids = set()
for entity in mesh.get_element_entities():
eltype = entity.get_element_type()
if entity.get_dimension() == 2 and eltype == 3:
for element in entity.get_elements():
elid = element.get_tag()
elcon = element.get_connectivity()
elements[elid] = elcon
for c in elcon:
node_ids.add(c)
for entity in mesh.get_node_entities():
for node in entity.get_nodes():
nid = node.get_tag()
if nid not in node_ids:
continue
ncoords = node.get_coordinates()
nodes[nid] = ncoords
invP = {}
X = []
Y = []
for i, nid in enumerate(sorted(node_ids)):
invP[nid] = i
X.append(nodes[nid][0])
Y.append(nodes[nid][1])
Q = []
for element in elements.values():
Q.append([invP[c] for c in element])
return X, Y, Q
|
Example:
from gmshparser.helpers import get_quads
mesh = gmshparser.parse("quad_mesh.msh")
X, Y, Q = get_quads(mesh)
# Plot quads
for quad in Q:
x = [X[n-1] for n in quad] + [X[quad[0]-1]]
y = [Y[n-1] for n in quad] + [Y[quad[0]-1]]
plt.plot(x, y, 'k-')
get_elements_2d()
Extract all 2D elements (triangles and quads).
gmshparser.helpers.get_elements_2d(mesh)
Return 2D mesh elements (triangles and quads) for visualization.
Extracts all 2D elements from the mesh, supporting both triangular
(type 2) and quadrilateral (type 3) elements.
Parameters
mesh : Mesh
Mesh object containing 2D elements
Returns
dict
Dictionary with keys:
- 'nodes': dict mapping node_id to (x, y) coordinates
- 'triangles': list of triangle connectivity [n0, n1, n2]
- 'quads': list of quad connectivity [n0, n1, n2, n3]
- 'node_ids': list of all node IDs used
Source code in gmshparser/helpers.py
| def get_elements_2d(mesh):
"""Return 2D mesh elements (triangles and quads) for visualization.
Extracts all 2D elements from the mesh, supporting both triangular
(type 2) and quadrilateral (type 3) elements.
Parameters
----------
mesh : Mesh
Mesh object containing 2D elements
Returns
-------
dict
Dictionary with keys:
- 'nodes': dict mapping node_id to (x, y) coordinates
- 'triangles': list of triangle connectivity [n0, n1, n2]
- 'quads': list of quad connectivity [n0, n1, n2, n3]
- 'node_ids': list of all node IDs used
"""
triangles = []
quads = []
nodes = {}
node_ids = set()
# Extract elements
for entity in mesh.get_element_entities():
eltype = entity.get_element_type()
if entity.get_dimension() == 2:
for element in entity.get_elements():
elcon = element.get_connectivity()
for c in elcon:
node_ids.add(c)
if eltype == 2: # Triangle
triangles.append(elcon)
elif eltype == 3: # Quad
quads.append(elcon)
# Extract node coordinates
for entity in mesh.get_node_entities():
for node in entity.get_nodes():
nid = node.get_tag()
if nid in node_ids:
ncoords = node.get_coordinates()
nodes[nid] = (ncoords[0], ncoords[1])
return {
"nodes": nodes,
"triangles": triangles,
"quads": quads,
"node_ids": sorted(node_ids),
}
|
Example:
from gmshparser.helpers import get_elements_2d
mesh = gmshparser.parse("mixed_mesh.msh")
X, Y, triangles, quads = get_elements_2d(mesh)
print(f"Found {len(triangles)} triangles and {len(quads)} quads")
Utility Functions
parse_ints()
Parse space-separated integers from string.
parse_floats()
Parse space-separated floats from string.
See Also