Input and Output Facilities¶
solvcon.io.gmsh
¶
-
class
solvcon.io.gmsh.
Gmsh
(stream, load=False)¶ Gmsh mesh object. Indices nodes and elements in Gmsh is 1-based (Fortran convention), but 0-based (C convention) indices are used throughout SOLVCON. However, physics groups are using 0-based index.
-
__init__
(stream, load=False)¶ >>> # sample data. >>> from io import BytesIO >>> data = b"""$MeshFormat ... 2.2 0 8 ... $EndMeshFormat ... $Nodes ... 3 ... 1 -1 0 0 ... 2 1 0 0 ... 3 0 1 0 ... $EndNodes ... $Elements ... 1 ... 1 2 2 1 22 1 2 3 ... $EndElements ... $PhysicalNames ... 1 ... 2 1 "lower" ... $EndPhysicalNames ... $Periodic ... 1 ... 0 1 3 ... 1 ... 1 3 ... $EndPeriodic"""
Creation of the object doesn’t load data:
>>> gmsh = Gmsh(BytesIO(data)) >>> None is gmsh.ndim True >>> gmsh.load() >>> gmsh.ndim 2 >>> gmsh.stream.close() # it's a good habit :-)
We can request to load data on creation by setting load=True. Note the stream will be closed after creation+loading. The default behavior is different to
load()
.>>> gmsh = Gmsh(BytesIO(data), load=True) >>> gmsh.ndim 2 >>> gmsh.stream.closed True
-
nnode
¶ Number of nodes that is useful for SOLVCON.
-
ncell
¶ Number of cells that is useful for SOLVCON and interior.
-
load
(close=False)¶ Load mesh data from storage.
>>> # sample data. >>> from io import BytesIO >>> data = b"""$MeshFormat ... 2.2 0 8 ... $EndMeshFormat ... $Nodes ... 3 ... 1 -1 0 0 ... 2 1 0 0 ... 3 0 1 0 ... $EndNodes ... $Elements ... 1 ... 1 2 2 1 22 1 2 3 ... $EndElements ... $PhysicalNames ... 1 ... 2 1 "lower" ... $EndPhysicalNames ... $Periodic ... 1 ... 0 1 3 ... 1 ... 1 3 ... $EndPeriodic"""
Load the mesh data after creation of the object. Note the stream is left opened after loading.
>>> stream = BytesIO(data) >>> gmsh = Gmsh(stream) >>> gmsh.load() >>> stream.closed False >>> stream.close() # it's a good habit :-)
We can ask
load()
to close the stream after loading by using close=True:>>> gmsh = Gmsh(BytesIO(data)) >>> gmsh.load(close=True) >>> gmsh.stream.closed True
Private Helpers for Loading and Parsing the Mesh File
These private methods are documented for demonstrating the data structure of the loaded meshes. Do not rely on their implementation.
-
static
_check_meta
(stream)¶ Load and check the meta data of the mesh. It doesn’t return anything to be stored.
>>> from io import BytesIO >>> stream = BytesIO(b"""$MeshFormat ... 2.2 0 8 ... $EndMeshFormat""") >>> stream.readline() == b'$MeshFormat\n' True >>> Gmsh._check_meta(stream) {} >>> stream.readline() == b'' True
-
static
_load_nodes
(stream)¶ Load node coordinates of the mesh data. Because of the internal data structure of Python, Numpy, and SOLVCON, the loaded
nodes
are using the 0-based index.>>> import io >>> stream = io.BytesIO(b"""$Nodes ... 3 ... 1 -1 0 0 ... 2 1 0 0 ... 3 0 1 0 ... $EndNodes""") # a triangle. >>> stream.readline() == b'$Nodes\n' True >>> Gmsh._load_nodes(stream) {'nodes': array([[-1., 0., 0.], [ 1., 0., 0.], [ 0., 1., 0.]])} >>> stream.readline() == b'' True
-
classmethod
_load_elements
(stream, nodes)¶ Load element definition of the mesh data. The node indices defined for each element are still 1-based. It returns
cltpn
,eldim
,elems
,elgeo
,elgrp
,ndim
,ndmap
, andusnds
for storage.>>> from numpy import array >>> nodes = array([[-1., 0., 0.], [ 1., 0., 0.], [ 0., 1., 0.]]) >>> import io >>> stream = io.BytesIO(b"""$Elements ... 1 ... 1 2 2 1 22 1 2 3 ... $EndElements""") # a triangle. >>> stream.readline() == b'$Elements\n' True >>> sorted(Gmsh._load_elements( ... stream, nodes).items()) [('cltpn', array([3], dtype=int32)), ('eldim', array([2], dtype=int32)), ('elems', array([[ 3, 1, 2, 3, -1, -1, -1, -1, -1]], dtype=int32)), ('elgeo', array([22], dtype=int32)), ('elgrp', array([1], dtype=int32)), ('ndim', 2), ('ndmap', array([0, 1, 2], dtype=int32)), ('usnds', array([0, 1, 2], dtype=int32))] >>> stream.readline() == b'' True
-
static
_load_physics
(stream)¶ Load physics groups of the mesh data. Return
physics
for storage.>>> from io import BytesIO >>> stream = BytesIO(b"""$PhysicalNames ... 1 ... 2 1 "lower" ... $EndPhysicalNames""") >>> stream.readline() == b'$PhysicalNames\n' True >>> res = Gmsh._load_physics(stream) >>> stream.readline() == b'' True
-
static
_load_periodic
(stream)¶ Load periodic definition of the mesh data. Return
periodics
for storage.>>> from io import BytesIO >>> stream = BytesIO(b"""$Periodic ... 1 ... 0 1 3 ... 1 ... 1 3 ... $EndPeriodic""") # a triangle. >>> stream.readline() == b'$Periodic\n' True >>> res = Gmsh._load_periodic(stream) >>> stream.readline() == b'' True
Mesh Definition and Data Attributes
-
ELMAP
¶ Element definition map. The key is Gmsh element type ID. The value is a 4-tuple: (i) dimension, (ii) number of total nodes, (iii) SOLVCON cell type ID, and (iv) SOLVCON cell node ordering.
-
stream
¶ Input stream (
file
) of the mesh data.
-
ndim
¶ Number of dimension of this mesh (py:class:int). Stored by
_load_elements()
.
-
nodes
¶ of Gmsh nodes, 3). Note for even two-dimensional meshes the array still stores three-dimensional coordinates. Stored by
_load_nodes()
.
-
usnds
¶ Indices (0-based) of the nodes really useful for SOLVCON (
numpy.ndarray
). Stored by_load_elements()
.
-
ndmap
¶ A mapping array from Gmsh node indices (0-based) to SOLVCON node indices (0-based) (
numpy.ndarray
). Stored by_load_elements()
.
-
cltpn
¶ SOLVCON cell type ID for each Gmsh element (py:class:numpy.ndarray). Stored by
_load_elements()
.
-
elgrp
¶ Physics group number of each Gmsh element; the first tag (
numpy.ndarray
). Stored by_load_elements()
.
-
elgeo
¶ Geometrical gropu number of each Gmsh element; the second tag (
numpy.ndarray
). Stored by_load_elements()
.
-
eldim
¶ Dimension of each Gmsh element (
numpy.ndarray
). Stored by_load_elements()
.
-
elems
¶ Gmsh node indices (1-based) of each Gmsh element (
numpy.ndarray
). Stored by_load_elements()
.
-
intels
¶ Indices (0-based) of the elements inside the domain (
numpy.ndarray
). Stored by_parse_physics()
.
-
physics
¶ Physics groups as a
list
of 3-tuples: (i) dimension, (ii) index (0-based), and (iii) name. If a physics group has the same dimension as the mesh, it is an interior group. Otherwise, the physics group must have one less dimension than the mesh, and it must be used as the boundary definition. Stored by_load_physics()
and then processed by_parse_physics()
.
-
periodics
¶ Periodic relation
list
. Each item is adict
:. Stored by_load_periodic()
.
-