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.
>>> # sample data.
>>> import StringIO
>>> data = """$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(StringIO.StringIO(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(StringIO.StringIO(data), load=True)
>>> gmsh.ndim
2
>>> gmsh.stream.closed
True
Number of nodes that is useful for SOLVCON.
Number of cells that is useful for SOLVCON and interior.
Load mesh data from storage.
>>> # sample data.
>>> import StringIO
>>> data = """$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 = StringIO.StringIO(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(StringIO.StringIO(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.
Load and check the meta data of the mesh. It doesn’t return anything to be stored.
>>> import StringIO
>>> stream = StringIO.StringIO("""$MeshFormat
... 2.2 0 8
... $EndMeshFormat""")
>>> stream.readline()
'$MeshFormat\n'
>>> Gmsh._check_meta(stream)
{}
>>> stream.readline()
''
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 StringIO
>>> stream = StringIO.StringIO("""$Nodes
... 3
... 1 -1 0 0
... 2 1 0 0
... 3 0 1 0
... $EndNodes""") # a triangle.
>>> stream.readline()
'$Nodes\n'
>>> Gmsh._load_nodes(stream)
{'nodes': array([[-1., 0., 0.], [ 1., 0., 0.], [ 0., 1., 0.]])}
>>> stream.readline()
''
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, and usnds for storage.
>>> from numpy import array
>>> nodes = array([[-1., 0., 0.], [ 1., 0., 0.], [ 0., 1., 0.]])
>>> import StringIO
>>> stream = StringIO.StringIO("""$Elements
... 1
... 1 2 2 1 22 1 2 3
... $EndElements""") # a triangle.
>>> stream.readline()
'$Elements\n'
>>> 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()
''
Load physics groups of the mesh data. Return physics for storage.
>>> import StringIO
>>> stream = StringIO.StringIO("""$PhysicalNames
... 1
... 2 1 "lower"
... $EndPhysicalNames""")
>>> stream.readline()
'$PhysicalNames\n'
>>> Gmsh._load_physics(stream)
{'physics': ['1', '2 1 "lower"']}
>>> stream.readline()
''
Load periodic definition of the mesh data. Return periodics for storage.
>>> import StringIO
>>> stream = StringIO.StringIO("""$Periodic
... 1
... 0 1 3
... 1
... 1 3
... $EndPeriodic""") # a triangle.
>>> stream.readline()
'$Periodic\n'
>>> Gmsh._load_periodic(stream)
{'periodics': [{'ndim': 0,
'stag': 1,
'nodes': array([[1, 3]], dtype=int32),
'mtag': 3}]}
>>> stream.readline()
''
Mesh Definition and Data Attributes
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.
Input stream (file) of the mesh data.
Number of dimension of this mesh (py:class:int). Stored by _load_elements().
Three-dimensional coordinates of all nodes of Gmsh nodes, 3). Note for even two-dimensional meshes the array still stores three-dimensional coordinates. Stored by _load_nodes().
Indices (0-based) of the nodes really useful for SOLVCON (numpy.ndarray). Stored by _load_elements().
A mapping array from Gmsh node indices (0-based) to SOLVCON node indices (0-based) (numpy.ndarray). Stored by _load_elements().
SOLVCON cell type ID for each Gmsh element (py:class:numpy.ndarray). Stored by _load_elements().
Physics group number of each Gmsh element; the first tag (numpy.ndarray). Stored by _load_elements().
Geometrical gropu number of each Gmsh element; the second tag (numpy.ndarray). Stored by _load_elements().
Dimension of each Gmsh element (numpy.ndarray). Stored by _load_elements().
Gmsh node indices (1-based) of each Gmsh element (numpy.ndarray). Stored by _load_elements().
Indices (0-based) of the elements inside the domain (numpy.ndarray). Stored by _parse_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().
Periodic relation list. Each item is a dict:. Stored by _load_periodic().