mesh.Mesh#

class mesh.Mesh(coords=None, elems=None, prop=None, eltype=None)#

Bases: Geometry

A Mesh is a discrete geometric model defined by nodes and elements.

It consists of:

  • nodes: points in 3D space, stored as coordinates in an array of shape (n_coords, 3), where n_coords is the number of unique points;

  • elements: groups of nodes forming geometric entities (like lines or triangles), stored as indices into the nodes array with shape (n_elems, plexitude), where n_elems is the number of elements and plexitude is the number of nodes per element.

This structure allows compact storage by sharing nodes among elements and supports efficient algorithms that use connectivity information.

Parameters#

coords: Coords or other object.

Usually, a 2-dim Coords object holding the coordinates of all the nodes used in the Mesh geometry. See details below for different initialization methods.

elems: Elems or other object (n_elems, plexitude)

Usually, an Elems object, defining the elements of the geometry by indices into the coords Coords array. All values in elems should be in the range 0 <= value < n_coords. See details below for different initialization methods.

prop: int array_like, optional

1-dim int array with non-negative element property numbers. If provided, set_prop() will be called to assign the specified properties.

eltype: str or ElementType, optional

The element type of the geometric entities (elements). This is only needed if the element type has not yet been set in elems. See below.

A Mesh object can be initialized in many different ways, depending on the values passed for the coords and elems arguments.

  • Coords, Elems: This is the most obvious case: coords is a 2-dim Coords object holding the coordinates of all the nodes in the Mesh, and elems is a Elems object describing the geometric elements by indices into the coords.

  • Coords, : If a Coords is passed as first argument, but no elems, the result is a Mesh of points, with plexitude 1. The Connectivity will be constructed automatically.

  • object with to_mesh method: As a convenience, if another object is provided that has a to_mesh method and elems is not provided, the result of the to_mesh method will be used to initialize both coords and elems.

  • None: If neither coords nor elems are specified, but eltype is, a unit sized single element Mesh of the specified ElementType is created.

  • An empty Mesh can be created by providing an empty coords and elems array, and an eltype.

Setting the element type can also be done in different ways. If elems is an Elems object, it will normally already have a element type. If not, it can be done by passing it in the eltype parameter. In case you pass a simple array or list in the elems parameter, an element type is required. Finally, the user can specify an eltype to override the one in the Elems. It should however match the plexitude of the connectivity data.

eltype should be one of the ElementType instances or the name of such an instance. If required but not provided, the default type is used, which is based on the plexitude:

  • 1: Point

  • 2: Line2

  • 3: Tri3

  • 4: Quad4

  • 6: Wedge6

  • 8: Hex8

A properly initialized Mesh has the following attributes:

Attributes#

coords: Coords (n_coords,3)

A 2-dim Coords object holding the coordinates of all the nodes used to describe the Mesh geometry.

elems: Elems (n_elems,plexitude)

An Elems object, defining the connectivity of the elements of the geometry by indices into the coords Coords array. All values in elems should be in the range 0 <= value < n_coords.

The Elems also stores the element type of the Mesh.

prop: int array, optional

Element property numbers. See geometry.Geometry.prop attribute.

fields: dict

The Fields defined on the Mesh. See geometry.Geometry.fields attribute.

Important

The Mesh class inherits from geometry.Geometry and therefore has all the methods defined there readily available.

Note

The coords attribute of a Mesh can hold points that are not used or needed to describe the Geometry. They do not influence the result of Mesh operations, but use more memory than needed. If their number becomes large, you may want to free that memory by calling the compact() method. Also, before exporting a Mesh (e.g. to a numerical simulation program), you may want to compact the Mesh first.

Examples#

Create a Mesh with four points and two triangle elements of type ‘tri3’.

>>> coords = Coords([[0.0, 0.0, 0.0],
...                  [1.0, 0.0, 0.0],
...                  [1.0, 1.0, 0.0],
...                  [0.0, 1.0, 0.0]])
>>> elems = [[0,1,2], [0,2,3]]
>>> M = Mesh(coords,elems,eltype='tri3')
>>> print(M.report(full=True))
Mesh: n_nodes: 4, n_elems: 2, plexitude: 3, level: 2, eltype: tri3
  BBox: [0. 0. 0.], [1. 1. 0.]
  Size: [1. 1. 0.]
  Length: 4.0  Area: 1.0
  Coords: [[0. 0. 0.]
           [1. 0. 0.]
           [1. 1. 0.]
           [0. 1. 0.]]
  Elems: [[0 1 2]
          [0 2 3]]
>>> M.n_elems()
2
>>> M.n_coords()
4
>>> M.plexitude()
3
>>> M.level()
2
>>> M.eltype.name
'Tri3'

Generate an empty Mesh:

>>> print(Mesh([],[],eltype='tri3'))
Mesh: n_nodes: 0, n_elems: 0, plexitude: 3, level: 2, eltype: tri3
  BBox: [0. 0. 0.], [0. 0. 0.]
  Size: [0. 0. 0.]
  Length: 0.0  Area: 0.0

Example showing that coordinate transform methods defined for the Geometry class can be applied to Mesh objects:

>>> M1 = M.rotate_z(90.0)
>>> print(M1.coords)
[[ 0.  0.  0.]
 [ 0.  1.  0.]
 [-1.  1.  0.]
 [-1.  0.  0.]]