Mesh

(Source code, png, hires.png, pdf)

../../_images/Mesh.png
class Mesh(*args)

Mesh.

Available constructors:

Mesh(dim=1)

Mesh(vertices)

Mesh(vertices, simplices)

Parameters:

dim : int, dim \geq 0

The dimension of the vertices. By default, it creates only one vertex of dimension dim with components equal to 0.

vertices : 2-d sequence of float

Vertices’ coordinates in \Rset^{dim}.

simplices : 2-d sequence of int

List of simplices defining the topology of the mesh. The simplex [i_1, \dots, i_{dim+1}] connects the vertices of indices (i_1, \dots, i_{dim+1}) in \Rset^{dim}. In dimension 1, a simplex is an interval [i_1, i_2]; in dimension 2, it is a triangle [i_1, i_2, i_3].

See also

RegularGrid

Examples

>>> import openturns as ot
>>> # Define the vertices of the mesh
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.5, 1.0]]
>>> # Define the simplices of the mesh
>>> simplices = [[0, 1, 2], [1, 2, 3]]
>>> # Create the mesh of dimension 2
>>> mesh2d = ot.Mesh(vertices, simplices)

Methods

ImportFromMSHFile(fileName) Import mesh from FreeFem 2-d mesh files.
checkPointInSimplex(point, index) Check if a point is inside a simplex.
checkPointInSimplexWithCoordinates(point, index) Check if a point is inside a simplex and returns its barycentric coordinates.
computeP1Gram() Compute the P1 Lagrange finite element gram matrix of the mesh.
computeSimplexVolume(index) Compute the volume of a given simplex.
contains(point) Check if the given point is inside of the domain.
draw() Draw the mesh.
draw1D() Draw the mesh of dimension 1.
draw2D() Draw the mesh of dimension 2.
draw3D(*args) Draw the bidimensional projection of the mesh.
exportToVTKFile(fileName) Export the mesh to a VTK file.
getClassName() Accessor to the object’s name.
getDescription() Get the description of the vertices.
getDimension() Get the dimension of the domain.
getId() Accessor to the object’s id.
getLowerBound() Get the lower bound of the domain.
getName() Accessor to the object’s name.
getNearestVertex(*args) Get the nearest vertex of a given point.
getNearestVertexIndex(*args) Get the index of the nearest vertex of a given point.
getNumericalVolume() Get the volume of the domain.
getShadowedId() Accessor to the object’s shadowed id.
getSimplex(index) Get the simplex of a given index.
getSimplices() Get the simplices of the mesh.
getSimplicesNumber() Get the number of simplices of the mesh.
getUpperBound() Get the upper bound of the domain.
getVertex(index) Get the vertex of a given index.
getVertices() Get the vertices of the mesh.
getVerticesNumber() Get the number of vertices of the mesh.
getVerticesToSimplicesMap()
getVisibility() Accessor to the object’s visibility state.
getVolume() Get the geometric volume of the domain.
hasName() Test if the object is named.
hasVisibleName() Test if the object has a distinguishable name.
isEmpty() Test whether the domain is empty or not.
isNumericallyEmpty() Check if the domain is numerically empty.
isRegular() Check if the mesh is regular (only for 1-d meshes).
isValid() Check the mesh validity.
numericallyContains(point) Check if the given point is inside of the discretization of the domain.
setName(name) Accessor to the object’s name.
setShadowedId(id) Accessor to the object’s shadowed id.
setSimplices(simplices) Set the simplices of the mesh.
setVertex(index, vertex) Set a vertex of a given index.
setVertices(vertices) Set the vertices of the mesh.
setVisibility(visible) Accessor to the object’s visibility state.
streamToVTKFormat() Give a VTK representation of the mesh.
__init__(*args)
static ImportFromMSHFile(fileName)

Import mesh from FreeFem 2-d mesh files.

Parameters:

MSHFile : str

A MSH ASCII file.

Returns:

mesh : Mesh

Mesh defined in the file MSHFile.

checkPointInSimplex(point, index)

Check if a point is inside a simplex.

Parameters:

point : sequence of float

Point of dimension dim, the dimension of the vertices of the mesh.

index : int

Integer characterizes one simplex of the mesh.

Returns:

isInside : bool

Flag telling whether point is inside the simplex of index index.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> simplex = [[0, 1, 2]]
>>> mesh2d = ot.Mesh(vertices, simplex)
>>> # Create a point A inside the simplex
>>> pointA = [0.6, 0.3]
>>> print(mesh2d.checkPointInSimplex(pointA, 0))
True
>>> # Create a point B outside the simplex
>>> pointB = [1.1, 0.6]
>>> print(mesh2d.checkPointInSimplex(pointB, 0))
False
checkPointInSimplexWithCoordinates(point, index)

Check if a point is inside a simplex and returns its barycentric coordinates.

Parameters:

point : sequence of float

Point of dimension dim, the dimension of the vertices of the mesh.

index : int

Integer characterizes one simplex of the mesh.

Returns:

isInside : bool

Flag telling whether point is inside the simplex of index index.

coordinates : NumericalPoint

The barycentric coordinates of the given point wrt the vertices of the simplex

.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> simplex = [[0, 1, 2]]
>>> mesh2d = ot.Mesh(vertices, simplex)
>>> # Create a point A inside the simplex
>>> pointA = [0.6, 0.3]
>>> print(mesh2d.checkPointInSimplexWithCoordinates(pointA, 0))
[True, class=NumericalPoint name=Unnamed dimension=3 values=[0.4,0.3,0.3]]
>>> # Create a point B outside the simplex
>>> pointB = [1.1, 0.6]
>>> print(mesh2d.checkPointInSimplexWithCoordinates(pointB, 0))
[False, class=NumericalPoint name=Unnamed dimension=3 values=[-0.1,0.5,0.6]]
computeP1Gram()

Compute the P1 Lagrange finite element gram matrix of the mesh.

Returns:

gram : CovarianceMatrix

P1 Lagrange finite element gram matrix of the mesh.

Notes

The P1 Lagrange finite element space associated to a mesh with vertices (\vect{x}_i)_{i=1,\hdots,n} is the space of piecewise-linear functions generated by the functions (\phi_i)_{i=1,\hdots,n}, where \phi_i(\vect{x_i})=1, \phi_i(\vect{x_j})=0 for j\neq i and the restriction of \phi_i to any simplex is an affine function. The vertices that are not included into at least one simplex are not taken into account.

The gram matrix of the mesh is defined as the symmetric positive definite matrix \mat{K} whose generic element K_{i,j} is given by:

\forall i,j=1,\hdots,n,\quad K_{i,j}=\int_{\cD}\phi_i(\vect{x})\phi_j(\vect{x})\di{\vect{x}}

This method is used in several algorithms related to stochastic process representation such as the Karhunen-Loeve decomposition.

Examples

>>> import openturns as ot
>>> # Define the vertices of the mesh
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.5, 1.0]]
>>> # Define the simplices of the mesh
>>> simplices = [[0, 1, 2], [1, 2, 3]]
>>> # Create the mesh of dimension 2
>>> mesh2d = ot.Mesh(vertices, simplices)
>>> print(mesh2d.computeP1Gram())
[[ 0.0416667 0.0208333 0.0208333 0         ]
 [ 0.0208333 0.0625    0.03125   0.0104167 ]
 [ 0.0208333 0.03125   0.0625    0.0104167 ]
 [ 0         0.0104167 0.0104167 0.0208333 ]]
computeSimplexVolume(index)

Compute the volume of a given simplex.

Parameters:

index : int

Integer characterizes one simplex of the mesh.

Returns:

volume : float

Volume of the simplex of index index.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> simplex = [[0, 1, 2]]
>>> mesh2d = ot.Mesh(vertices, simplex)
>>> print(mesh2d.computeSimplexVolume(0))
0.5
contains(point)

Check if the given point is inside of the domain.

Parameters:

point : sequence of float

Point with the same dimension as the current domain’s dimension.

Returns:

isInside : bool

Flag telling whether the given point is inside of the domain.

draw()

Draw the mesh.

Returns:

graph : Graph

If the dimension of the mesh is 1, it draws the corresponding interval, using the draw1D() method; if the dimension is 2, it draws the triangular simplices, using the draw2D() method; if the dimension is 3, it projects the simplices on the plane of the two first components, using the draw3D() method with its default parameters, superposing the simplices.

draw1D()

Draw the mesh of dimension 1.

Returns:

graph : Graph

Draws the line linking the vertices of the mesh when the mesh is of dimension 1.

Examples

>>> import openturns as ot
>>> vertices = [[0.5], [1.5], [2.1], [2.7]]
>>> simplices = [[0, 1], [1, 2], [2, 3]]
>>> mesh1d = ot.Mesh(vertices, simplices)
>>> # Create a graph
>>> aGraph = mesh1d.draw1D()
>>> # Draw the mesh
>>> aGraph.draw('mesh1D')
draw2D()

Draw the mesh of dimension 2.

Returns:

graph : Graph

Draws the edges of each simplex, when the mesh is of dimension 2.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.5, 1.0]]
>>> simplices = [[0, 1, 2], [1, 2, 3]]
>>> mesh2d = ot.Mesh(vertices, simplices)
>>> # Create a graph
>>> aGraph = mesh2d.draw2D()
>>> # Draw the mesh
>>> aGraph.draw('mesh2D')
draw3D(*args)

Draw the bidimensional projection of the mesh.

Available usages:

draw3D(drawEdge=True, thetaX=0.0, thetaY=0.0, thetaZ=0.0, shading=False, rho=1.0)

draw3D(drawEdge, rotation, shading, rho)

Parameters:

drawEdge : bool

Tells if the edge of each simplex has to be drawn.

thetaX : float

Gives the value of the rotation along the X axis in radian.

thetaY : float

Gives the value of the rotation along the Y axis in radian.

thetaZ : float

Gives the value of the rotation along the Z axis in radian.

rotation : SquareMatrix

Operates a rotation on the mesh before its projection of the plane of the two first components.

shading : bool

Enables to give a visual perception of depth and orientation.

rho : float, 0 \leq \rho \leq 1

Contraction factor of the simplices. If \rho < 1, all the simplices are contracted and appear deconnected: some holes are created, which enables to see inside the mesh. If \rho = 1, the simplices keep their initial size and appear connected. If \rho = 0, each simplex is reduced to its gravity center.

Returns:

graph : Graph

Draws the bidimensional projection of the mesh on the (x,y) plane.

Examples

>>> import openturns as ot
>>> from math import cos, sin, pi
>>> vertices = [[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0],
...             [0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0],
...             [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]
>>> simplices = [[0, 1, 2, 4], [3, 5, 6, 7],[1, 2, 3, 6],
...              [1, 2, 4, 6], [1, 3, 5, 6], [1, 4, 5, 6]]
>>> mesh3d = ot.Mesh(vertices, simplices)
>>> # Create a graph
>>> aGraph = mesh3d.draw3D()
>>> # Draw the mesh
>>> aGraph.draw('mesh3D_1')
>>> rotation = ot.SquareMatrix(3)
>>> rotation[0, 0] = cos(pi / 3.0)
>>> rotation[0, 1] = sin(pi / 3.0)
>>> rotation[1, 0] = -sin(pi / 3.0)
>>> rotation[1, 1] = cos(pi / 3.0)
>>> rotation[2, 2] = 1.0
>>> # Create a graph
>>> aGraph = mesh3d.draw3D(True, rotation, True, 1.0)
>>> # Draw the mesh
>>> aGraph.draw('mesh3D_2')
exportToVTKFile(fileName)

Export the mesh to a VTK file.

Parameters:

myVTKFile.vtk : str

Name of the created file which contains the mesh and the associated random values that can be visualized with the open source software Paraview.

getClassName()

Accessor to the object’s name.

Returns:

class_name : str

The object class name (object.__class__.__name__).

getDescription()

Get the description of the vertices.

Returns:

description : str

Description of the vertices.

Examples

>>> import openturns as ot
>>> mesh = ot.Mesh()
>>> vertices = ot.NumericalSample([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]])
>>> vertices.setDescription(['X', 'Y'])
>>> mesh.setVertices(vertices)
>>> print(mesh.getDescription())
[X,Y]
getDimension()

Get the dimension of the domain.

Returns:

dim : int

Dimension of the domain.

getId()

Accessor to the object’s id.

Returns:

id : int

Internal unique identifier.

getLowerBound()

Get the lower bound of the domain.

Returns:

lower : NumericalPoint

The lower bound of an axes-aligned bounding box of the domain.

getName()

Accessor to the object’s name.

Returns:

name : str

The name of the object.

getNearestVertex(*args)

Get the nearest vertex of a given point.

Parameters:

point : sequence of float

Point of dimension dim, the dimension of the vertices of the mesh.

Returns:

vertex : NumericalPoint

Coordinates of the nearest vertex of point.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> simplices = [[0, 1, 2]]
>>> mesh2d = ot.Mesh(vertices, simplices)
>>> point = [0.9, 0.4]
>>> print(mesh2d.getNearestVertex(point))
[1,0]
getNearestVertexIndex(*args)

Get the index of the nearest vertex of a given point.

Parameters:

point : sequence of float

Point of dimension dim, the dimension of the vertices of the mesh.

Returns:

index : int

Index of the simplex the nearest of point according to the Euclidean norm.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> simplices = [[0, 1, 2]]
>>> mesh2d = ot.Mesh(vertices, simplices)
>>> point = [0.9, 0.4]
>>> print(mesh2d.getNearestVertexIndex(point))
1
getNumericalVolume()

Get the volume of the domain.

Returns:

volume : float

Volume of the underlying mesh which is the discretization of the domain. For now, by default, it is equal to the geometrical volume.

getShadowedId()

Accessor to the object’s shadowed id.

Returns:

id : int

Internal unique identifier.

getSimplex(index)

Get the simplex of a given index.

Parameters:

index : int

Index characterizing one simplex of the mesh.

Returns:

indices : Indices

Indices defining the simplex of index index. The simplex [i_1, \dots, i_{n+1}] relies the vertices of index (i_1, \dots, i_{n+1}) in \Rset^{dim}. In dimension 1, a simplex is an interval [i_1, i_2]; in dimension 2, it is a triangle [i_1, i_2, i_3].

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.5, 1.0]]
>>> simplices = [[0, 1, 2], [1, 2, 3]]
>>> mesh2d = ot.Mesh(vertices, simplices)
>>> print(mesh2d.getSimplex(0))
[0,1,2]
>>> print(mesh2d.getSimplex(1))
[1,2,3]
getSimplices()

Get the simplices of the mesh.

Returns:

indicesCollection : collection of Indices

List of indices defining all the simplices. The simplex [i_1, \dots, i_{n+1}] relies the vertices of index (i_1, \dots, i_{n+1}) in \Rset^{dim}. In dimension 1, a simplex is an interval [i_1, i_2]; in dimension 2, it is a triangle [i_1, i_2, i_3].

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [1.5, 1.0]]
>>> simplices = [[0, 1, 2], [1, 2, 3]]
>>> mesh2d = ot.Mesh(vertices, simplices)
>>> print(mesh2d.getSimplices())
[[0,1,2],[1,2,3]]
getSimplicesNumber()

Get the number of simplices of the mesh.

Returns:

number : int

Number of simplices of the mesh.

getUpperBound()

Get the upper bound of the domain.

Returns:

upper : NumericalPoint

The upper bound of an axes-aligned bounding box of the domain.

getVertex(index)

Get the vertex of a given index.

Parameters:

index : int

Index characterizing one vertex of the mesh.

Returns:

vertex : NumericalPoint

Coordinates in \Rset^{dim} of the vertex of index index, where dim is the dimension of the vertices of the mesh.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> simplices = [[0, 1, 2]]
>>> mesh2d = ot.Mesh(vertices, simplices)
>>> print(mesh2d.getVertex(1))
[1,0]
>>> print(mesh2d.getVertex(0))
[0,0]
getVertices()

Get the vertices of the mesh.

Returns:

vertices : NumericalSample

Coordinates in \Rset^{dim} of the vertices, where dim is the dimension of the vertices of the mesh.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> simplices = [[0, 1, 2]]
>>> mesh2d = ot.Mesh(vertices, simplices)
>>> print(mesh2d.getVertices())
0 : [ 0 0 ]
1 : [ 1 0 ]
2 : [ 1 1 ]
getVerticesNumber()

Get the number of vertices of the mesh.

Returns:

number : int

Number of vertices of the mesh.

getVisibility()

Accessor to the object’s visibility state.

Returns:

visible : bool

Visibility flag.

getVolume()

Get the geometric volume of the domain.

Returns:

volume : float

Geometrical volume of the domain.

hasName()

Test if the object is named.

Returns:

hasName : bool

True if the name is not empty.

hasVisibleName()

Test if the object has a distinguishable name.

Returns:

hasVisibleName : bool

True if the name is not empty and not the default one.

isEmpty()

Test whether the domain is empty or not.

Returns:

isInside : bool

True if the interior of the geometric domain is empty.

isNumericallyEmpty()

Check if the domain is numerically empty.

Returns:

isInside : bool

Flag telling whether the domain is numerically empty, i.e. if its numerical volume is inferior or equal to \epsilon (defined in the ResourceMap: \epsilon = DomainImplementation-SmallVolume).

Examples

>>> import openturns as ot
>>> domain = ot.Domain([1.0, 2.0], [1.0, 2.0]) 
>>> print(domain.isNumericallyEmpty())
True
isRegular()

Check if the mesh is regular (only for 1-d meshes).

Returns:

isRegular : bool

Tells if the mesh is regular or not.

Examples

>>> import openturns as ot
>>> vertices = [[0.5], [1.5], [2.4], [3.5]]
>>> simplices = [[0, 1], [1, 2], [2, 3]]
>>> mesh1d = ot.Mesh(vertices, simplices)
>>> print(mesh1d.isRegular())
False
>>> vertices = [[0.5], [1.5], [2.5], [3.5]]
>>> mesh1d = ot.Mesh(vertices, simplices)
>>> print(mesh1d.isRegular())
True
isValid()

Check the mesh validity.

Returns:

validity : bool

Tells if the mesh is valid i.e. if there is non-overlaping simplices, no unused vertex, no simplices with duplicate vertices and no coincident vertices.

numericallyContains(point)

Check if the given point is inside of the discretization of the domain.

Parameters:

point : sequence of float

Point with the same dimension as the current domain’s dimension.

Returns:

isInside : bool

Flag telling whether the point is inside the discretized domain associated to the domain. For now, by default, the discretized domain is equal to the geometrical domain.

setName(name)

Accessor to the object’s name.

Parameters:

name : str

The name of the object.

setShadowedId(id)

Accessor to the object’s shadowed id.

Parameters:

id : int

Internal unique identifier.

setSimplices(simplices)

Set the simplices of the mesh.

Parameters:

indices : 2-d sequence of int

List of indices defining all the simplices. The simplex [i_1, \dots, i_{n+1}] relies the vertices of index (i_1, \dots, i_{n+1}) in \Rset^{dim}. In dimension 1, a simplex is an interval [i_1, i_2]; in dimension 2, it is a triangle [i_1, i_2, i_3].

Examples

>>> import openturns as ot
>>> mesh = ot.Mesh()
>>> simplices = [[0, 1, 2], [1, 2, 3]]
>>> mesh.setSimplices(simplices)
setVertex(index, vertex)

Set a vertex of a given index.

Parameters:

index : int

Index of the vertex to set.

vertex : sequence of float

Cordinates in \Rset^{dim} of the vertex of index index, where dim is the dimension of the vertices of the mesh.

Examples

>>> import openturns as ot
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> simplices = [[0, 1, 2]]
>>> mesh = ot.Mesh(vertices, simplices)
>>> vertex = [0.0, 0.5]
>>> mesh.setVertex(0, vertex)
>>> print(mesh.getVertices())
0 : [ 0   0.5 ]
1 : [ 1   0   ]
2 : [ 1   1   ]
setVertices(vertices)

Set the vertices of the mesh.

Parameters:

vertices : 2-d sequence of float

Cordinates in \Rset^{dim} of the vertices, where dim is the dimension of the vertices of the mesh.

Examples

>>> import openturns as ot
>>> mesh = ot.Mesh()
>>> vertices = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
>>> mesh.setVertices(vertices)
setVisibility(visible)

Accessor to the object’s visibility state.

Parameters:

visible : bool

Visibility flag.

streamToVTKFormat()

Give a VTK representation of the mesh.

Returns:

stream : str

VTK representation of the mesh.