Mesh generation#

Mesh generation is not the primary objective of the library, but it is equipped with the most essential stuff. These are mainly for testing and representation purposes, although what they do, they do it pretty good.

sigmaepsilon.mesh.triang.triangulate(*args, points: ndarray | None = None, size: tuple | None = None, shape: tuple | None = None, origo: tuple | None = None, backend: str = 'mpl', random: bool = False, triangles: Iterable | None = None, triobj=None, return_lines: bool = False, **kwargs)[source]#

Crates a triangulation using different backends.

Parameters:
  • points (numpy.ndarray, Optional) – A 2d array of coordinates of a flat surface. This or shape must be provided.

  • size (tuple, Optional) – A 2-tuple, containg side lengths of a rectangular domain. Should be provided alongside shape. Must be provided if points is None.

  • shape (tuple or int, Optional) – A 2-tuple, describing subdivisions along coordinate directions, or the number of base points used for triangulation. Should be provided alongside size.

  • origo (numpy.ndarray, Optional) – 1d float array, specifying the origo of the mesh.

  • backend (str, Optional) – The backend to use. It can be ‘mpl’ (matplotlib), ‘pv’ (pyvista), or ‘scipy’. Default is ‘mpl’.

  • random (bool, Optional) – If True, and points are provided by shape, it creates random points in the region defined by size. Default is False.

  • triangles (numpy.ndarray) – 2d integer array of vertex indices. If both points and triangles are specified, the only thing this function does is to create a triangulation object according to the argument backend.

  • triobj (object) – An object representing a triangulation. It can be - a result of a call to matplotlib.tri.Triangulation - a Delaunay object from scipy - an instance of pyvista.PolyData In this case, the function can be used to transform between the supported backends.

  • return_lines (bool, Optional) – If True the function return the unique edges of the triangulation and the indices of the edges for each triangle.

Returns:

  • numpy.ndarray – A 2d float array of coordinates of points.

  • numpy.ndarray – A 2d integer array representing the topology.

  • MeshLike – An object representing the triangulation, according to the specified backend.

Examples

Triangulate a rectangle of size 800x600 with a subdivision of 10x10

>>> from sigmaepsilon.mesh import triangulate
>>> coords, topo, triobj = triangulate(size=(800, 600), shape=(10, 10))
...

Triangulate a rectangle of size 800x600 with a number of 100 randomly distributed points

>>> coords, topo, triobj = triangulate(size=(800, 600), shape=100, random=True)
...
sigmaepsilon.mesh.grid.grid(size: Tuple[float] | None = None, shape: int | Tuple[int] | None = None, eshape: str | Tuple[int] | None = None, shift: Iterable | None = None, start: int = 0, bins: Iterable | None = None, centralize: bool = False, path: Iterable | None = None, **kwargs) Tuple[ndarray, ndarray][source]#

Crates a 1d, 2d or 3d grid for different patterns and returnes the raw data. If you want a more high level mesh object, consider using the Grid class, which calls this method to generate a PolyData instance.

Parameters:
  • size (Tuple[float], Optional) – A 2-tuple, containg side lengths of a rectangular domain. Should be provided alongside shape. Default is None.

  • shape (Union[int, Tuple[int]], Optional) – An integer or a tuple of integers, describing number of cells along coordinate directions. Should be provided alongside size. Default is None.

  • eshape (str or Tuple, Optional) –

    This specifies element shape. Supported strings are thw following:

    • ’Q4’ : 4-noded quadrilateral

    • ’Q9’ : 9-noded quadrilateral

    • ’H8’ : 8-noded hexagon

    • ’H27’ : 27-noded hexagon

  • shift (numpy.ndarray, Optional) – 1d float array, specifying a translation.

  • start (int, Optional) – Starting value for node numbering. Default is 0.

  • bins (numpy.ndarray, Optional) – Numpy array or an iterable of numy arrays.

  • centralize (bool, Optional) – If True, the returned coordinates are centralized.

  • path (Iterable, Optional) – A 1d iterable used to rewire the nodes of the resulting grid. If provided, the j-th node of the i-th cell becomes topo[i, path[j]]. Default is None.

Notes

1) The returned topology may not be compliant with vtk. If you want to use the results of this call to build a vtk model, you have to account for this (for instance through the ‘path’ parameter). Optionally, you can use the dedicated grid generation routines of this module. 2) If you’d rather get the result as a PolyData, use the Grid class.

Returns:

  • numpy.ndarray – A numpy float array of coordinates.

  • numpy.ndarray – A numpy integer array describing the topology.

Examples

Create a simple hexagonal mesh of 8 x 6 x 2 cells

>>> from sigmaepsilon.mesh import grid
>>> size = 80, 60, 20
>>> shape = 8, 6, 2
>>> mesh = (coords, topo) = grid(size=size, shape=shape, eshape='H8')

Create a mesh of 4-noded quadrilaterals

>>> gridparams = {
>>>     'size' : (1200, 600),
>>>     'shape' : (30, 15),
>>>     'eshape' : (2, 2),
>>>     'start' : 0
>>> }
>>> coordsQ4, topoQ4 = grid(**gridparams)

The same mesh with 6-noded quadrialterals, 2 in x direction, 3 in y direction

>>> gridparams['eshape'] = (2, 3)
>>> coordsQ4, topoQ4 = grid(**gridparams)

See also

Grid, gridQ4(), gridQ9(), gridH8(), gridH27()

sigmaepsilon.mesh.grid.gridH27(*args, **kwargs) Tuple[ndarray, ndarray][source]#

Customized version of grid dedicated to H27 elements. It returns a topology with vtk-compliant node numbering.

In terms of parameters, this function have to be called the same way grid would be called, except the parameter eshape being obsolete.

sigmaepsilon.mesh.grid.gridH8(*args, **kwargs) Tuple[ndarray, ndarray][source]#

Customized version of grid dedicated to H8 elements. It returns a topology with vtk-compliant node numbering.

In terms of parameters, this function have to be called the same way grid would be called, except the parameter eshape being obsolete.

sigmaepsilon.mesh.grid.gridQ4(*args, **kwargs) Tuple[ndarray, ndarray][source]#

Customized version of grid() dedicated to Q4 elements. It returns a topology with vtk-compliant node numbering.

In terms of parameters, this function have to be called the same way grid would be called, except the parameter eshape being obsolete.

Example

Creating a mesh of 4-noded quadrilaterals

>>> from sigmaepsilon.mesh.grid import gridQ4
>>> gridparams = {
>>>     'size' : (1200, 600),
>>>     'shape' : (30, 15),
>>>     'origo' : (0, 0),
>>>     'start' : 0
>>> }
>>> coordsQ4, topoQ4 = gridQ4(**gridparams)
sigmaepsilon.mesh.grid.gridQ8(*args, **kwargs) Tuple[ndarray, ndarray][source]#

Customized version of grid dedicated to Q8 elements. It returns a topology with vtk-compliant node numbering.

In terms of parameters, this function have to be called the same way grid would be called, except the parameter eshape being obsolete.

sigmaepsilon.mesh.grid.gridQ9(*args, **kwargs) Tuple[ndarray, ndarray][source]#

Customized version of grid dedicated to Q9 elements. It returns a topology with vtk-compliant node numbering.

In terms of parameters, this function have to be called the same way grid would be called, except the parameter eshape being obsolete.

sigmaepsilon.mesh.grid.knngridL2(*args, max_distance: float | None = None, k: int = 3, X: ndarray | None = None, **kwargs) Tuple[ndarray, ndarray][source]#

Returns a KNN grid of L2 lines. First a grid of points is created using :func:grid, then points are connected based on a KNN-tree.

Parameters:
  • *args (tuple, Optional) – Positional arguments forwarded to :func:grid.

  • max_distance (float, Optional) – Maximum distance allowed. Default is None.

  • k (int, Optional) – Number of neighbours for a given point.

  • X (numpy.ndarray, Optional) – Coordinates of a pointcloud. If provided, args and kwargs are ignored. Default is None.

  • **kwargs (dict, Optional) – Keyword arguments forwarded to :func:grid.

See also

k_nearest_neighbours(), knn_to_lines()