Finite element meshes in pyFormex.
This module defines the Mesh class, which can be used to describe discrete geometrical models like those used in Finite Element models. It also contains some useful functions to create such models.
Classes defined in module mesh
A mesh is a discrete geometrical model defined by nodes and elements.
In the Mesh geometrical data model, coordinates of all points are gathered in a single twodimensional array ‘coords’ with shape (ncoords,3) and the individual geometrical elements are described by indices into the ‘elems’ array. This model has some advantages over the Formex data model, where all points of all element are stored by their coordinates:
The downside is that geometry generating algorithms are far more complex and possibly slower.
In pyFormex we therefore mostly use the Formex data model when creating geometry, but when we come to the point of exporting the geometry to file (and to other programs), a Mesh data model may be more adequate.
The Mesh data model has at least the following attributes:
If eltype is None, a default eltype is derived from the plexitude, by calling the defaultEltype function. For plexitudes without default type, or if the default type is not the wanted element type, the user should specify the element type himself.
A Mesh can be initialized by its attributes (coords,elems,prop,eltype) or by a single geometric object that provides a toMesh() method.
Methods
Create or destroy the property array for the Mesh.
A property array is a rank-1 integer array with dimension equal to the number of elements in the Mesh. You can specify a single value or a list/array of integer values. If the number of passed values is less than the number of elements, they wil be repeated. If you give more, they will be ignored.
If a value None is given, the properties are removed from the Mesh.
Return the properties as a numpy array (ndarray)
Return the highest property value used, or None
Return a list with unique property values.
Return a copy using the same data arrays
Convert a Mesh to a Formex.
The Formex inherits the element property numbers and eltype from the Mesh. Node property numbers however can not be translated to the Formex data model.
Return the number of edges.
This returns the number of rows that would be in getEdges(), without actually constructing the edges. The edges are not fused!
Return the centroids of all elements of the Mesh.
The centroid of an element is the point whose coordinates are the mean values of all points of the element. The return value is a Coords object with nelems points.
Get the coords data.
Get the elems data.
Get the entities of a lower dimensionality.
If the element type is defined in the elements module, this returns a Connectivity table with the entities of a lower dimensionality. The full list of entities with increasing dimensionality 0,1,2,3 is:
['points', 'edges', 'faces', 'cells' ]
If level is negative, the dimensionality returned is relative to that of the caller. If it is positive, it is taken absolute. Thus, for a Mesh with a 3D element type, getLowerEntities(-1) returns the faces, while for a 2D element type, it returns the edges. For bothe meshes however, getLowerEntities(+1) returns the edges.
By default, all entities for all elements are returned and common entities will appear multiple times. Specifying unique=True will return only the unique ones.
The return value may be an empty table, if the element type does not have the requested entities (e.g. the ‘point’ type). If the eltype is not defined, or the requested entity level is outside the range 0..3, the return value is None.
Get the entities of a lower dimensionality.
If the element type is defined in the elements module, this returns a Connectivity table with the entities of a lower dimensionality. The full list of entities with increasing dimensionality 0,1,2,3 is:
['points', 'edges', 'faces', 'cells' ]
If level is negative, the dimensionality returned is relative to that of the caller. If it is positive, it is taken absolute. Thus, for a Mesh with a 3D element type, getLowerEntities(-1) returns the faces, while for a 2D element type, it returns the edges. For both meshes however, getLowerEntities(+1) returns the edges.
By default, all entities for all elements are returned and common entities will appear multiple times. Specifying unique=True will return only the unique ones.
The return value may be an empty table, if the element type does not have the requested entities (e.g. the ‘point’ type). If the eltype is not defined, or the requested entity level is outside the range 0..3, the return value is None.
Return the nodal coordinates of the Mesh.
This returns only those points that are effectively used in the connectivity table. For a compacted Mesh, it is equal to the coords attribute.
Return the set of unique node numbers in the Mesh.
This returns only the node numbers that are effectively used in the connectivity table. For a compacted Mesh, it is equal to the range(self.nelems).
Return the edges of the elements.
This is a convenient function to create a table with the element edges. It is equivalent to `self.getLowerEntities(1,unique)`
Return the faces of the elements.
This is a convenient function to create a table with the element faces. It is equivalent to `self.getLowerEntities(2,unique)`
Return a Mesh where all elements have been reversed.
Reversing an element means reversing the order of its points. This is equivalent to:
Mesh(self.coords,self.elems[:,::-1])
Returns the angles in Deg or Rad between the edges of a mesh.
The returned angles are shaped as (nelems, n1faces, n1vertices), where n1faces are the number of faces in 1 element and the number of vertices in 1 face.
Return the border of the Mesh.
This returns a Connectivity table with the border of the Mesh. The border entities are of a lower hierarchical level than the mesh itself. These entities become part of the border if they are connected to only one element.
If return_indices==True, it returns also an (nborder,2) index for inverse lookup of the higher entity (column 0) and its local border part number (column 1).
The returned Connectivity can be used together with the Mesh.coords to construct a Mesh of the border geometry. See also getBorderMesh().
Return a Mesh with the border elements.
Returns a Mesh representing the border of the Mesh. The returned Mesh is of the next lower hierarchical level. If the Mesh has property numbers, the border elements inherit the property of the element to which they belong.
By default, the resulting Mesh is compacted. Compaction can be switched off by setting compact=False.
_For each element index in the list elsel,
it returns the list of neighbor elements (connected by one node at least). If elsel is None, the neighbors of all elements are calculated, but it is computationally expensive for big meshes.
Create a report on the Mesh shape and size.
The report contains the number of nodes, number of elements, plexitude, bbox and size.
Fuse the nodes of a Meshes.
All nodes that are within the tolerance limits of each other are merged into a single node.
The merging operation can be tuned by specifying extra arguments that will be passed to Coords:fuse().
Remove unconnected nodes and renumber the mesh.
Returns a mesh where all nodes that are not used in any element have been removed, and the nodes are renumbered to a compacter scheme.
Return a Mesh only holding the selected elements.
Returns a Mesh (or subclass) with only the selected elements.
See cselect for the complementary operation.
Return a mesh without the selected elements.
Returns a Mesh with all but the selected elements.
This is the complimentary operation of select.
Create nodes from the existing nodes of a mesh.
nodsel is a local node selector as in selectNodes() Returns the mean coordinates of the points in the selector as (nelems*nnod,3) array of coordinates, where nnod is the length of the node selector.
Add new nodes to elements.
newcoords is an (nelems,nnod,3) or`(nelems*nnod,3)` array of coordinates. Each element gets exactly nnod extra nodes from this array. The result is a Mesh with plexitude self.nplex() + nnod.
Add new nodes to elements by averaging existing ones.
nodsel is a local node selector as in selectNodes() Returns a Mesh where the mean coordinates of the points in the selector are added to each element, thus increasing the plexitude by the length of the items in the selector. The new element type should be set to correct value.
Return a mesh with subsets of the original nodes.
nodsel is an object that can be converted to a 1-dim or 2-dim array. Examples are a tuple of local node numbers, or a list of such tuples all having the same length. Each row of nodsel holds a list of local node numbers that should be retained in the new connectivity table.
Return a Mesh which holds only the elements with property val.
val is either a single integer, or a list/array of integers. The return value is a Mesh holding all the elements that have the property val, resp. one of the values in val. The returned Mesh inherits the matching properties.
If the Mesh has no properties, a copy with all elements is returned.
Return a Mesh without the elements with property val.
This is the complementary method of Mesh.withProp(). val is either a single integer, or a list/array of integers. The return value is a Mesh holding all the elements that do not have the property val, resp. one of the values in val. The returned Mesh inherits the matching properties.
If the Mesh has no properties, a copy with all elements is returned.
Partition a Mesh according to its propery values.
Returns a dict with the property values as keys and the corresponding partitions as values. Each value is a Mesh instance. It the Mesh has no props, an empty dict is returned.
Convert a Mesh to another element type.
Converting a Mesh from one element type to another can only be done if both element types are of the same dimensionality. Thus, 3D elements can only be converted to 3D elements.
The conversion is done by splitting the elements in smaller parts and/or by adding new nodes to the elements.
Not all conversions between elements of the same dimensionality are possible. The possible conversion strategies are implemented in a table. New strategies may be added however.
The return value is a Mesh of the requested element type, representing the same geometry (possibly approximatively) as the original mesh.
If the requested conversion is not implemented, an error is raised.
Split a mesh in n parts, distributing the elements randomly.
Returns a list of n Mesh objects, constituting together the same Mesh as the original. The elements are randomly distributed over the subMeshes.
By default, the Meshes are compacted. Compaction may be switched off for efficiency reasons.
Convert choosing randomly between choices
Reduce degenerate elements to lower plexitude elements.
This will try to reduce the degenerate elements of the mesh to elements of a lower plexitude. If a target element type is given, only the matching recuce scheme is tried. Else, all the target element types for which a reduce scheme from the Mesh eltype is available, will be tried.
The result is a list of Meshes of which the last one contains the elements that could not be reduced and may be empty. Property numbers propagate to the children.
Split a Mesh in degenerate and non-degenerate elements.
If autofix is True, the degenerate elements will be tested against known degeneration patterns, and the matching elements will be transformed to non-degenerate elements of a lower plexitude.
The return value is a list of Meshes. The first holds the non-degenerate elements of the original Mesh. The last holds the remaining degenerate elements. The intermediate Meshes, if any, hold elements of a lower plexitude than the original. These may still contain degenerate elements.
Renumber the nodes of a Mesh in the specified order.
order is an index with length equal to the number of nodes. The index specifies the node number that should come at this position. Thus, the order values are the old node numbers on the new node number positions.
order can also be a predefined value that will generate the node index automatically:
Extrude a Mesh in one of the axes directions.
Returns a new Mesh obtained by extruding the given Mesh over n steps of length step in direction of axis dir. The returned Mesh has double plexitude of the original.
This function is usually used to extrude points into lines, lines into surfaces and surfaces into volumes. By default it will try to fix the connectivity ordering where appropriate. If autofix is switched off, the connectivities are merely stacked, and the user may have to fix it himself.
Currently, this function correctly transforms: point1 to line2, line2 to quad4, tri3 to wedge6, quad4 to hex8.
Revolve a Mesh around an axis.
Returns a new Mesh obtained by revolving the given Mesh over an angle around an axis in n steps, while extruding the mesh from one step to the next.
This function is usually used to extrude points into lines, lines into surfaces and surfaces into volumes. By default it will try to fix the connectivity ordering where appropriate. If autofix is switched off, the connectivities are merely stacked, and the user may have to fix it himself.
Currently, this function correctly transforms: point1 to line2, line2 to quad4, tri3 to wedge6, quad4 to hex8.
Sweep a mesh along a path, creating an extrusion
Returns a new Mesh obtained by sweeping the given Mesh over a path. The returned Mesh has double plexitude of the original. The operation is similar to the extrude() method, but the path can be any 3D curve.
This function is usually used to extrude points into lines, lines into surfaces and surfaces into volumes. By default it will try to fix the connectivity ordering where appropriate. If autofix is switched off, the connectivities are merely stacked, and the user may have to fix it himself.
Currently, this function correctly transforms: point1 to line2, line2 to quad4, tri3 to wedge6, quad4 to hex8.
Flag elements having nodal coordinates between min and max.
This function is very convenient in clipping a Mesh in a specified direction. It returns a 1D integer array flagging (with a value 1 or True) the elements having nodal coordinates in the required range. Use where(result) to get a list of element numbers passing the test. Or directly use clip() or cclip() to create the clipped Mesh
The test plane can be defined in two ways, depending on the value of dir. If dir == 0, 1 or 2, it specifies a global axis and min and max are the minimum and maximum values for the coordinates along that axis. Default is the 0 (or x) direction.
Else, dir should be compaitble with a (3,) shaped array and specifies the direction of the normal on the planes. In this case, min and max are points and should also evaluate to (3,) shaped arrays.
nodes specifies which nodes are taken into account in the comparisons. It should be one of the following:
The default (‘all’) will flag all the elements that have all their nodes between the planes x=min and x=max, i.e. the elements that fall completely between these planes. One of the two clipping planes may be left unspecified.
Return a Mesh with all the elements where t>0.
t should be a 1-D integer array with length equal to the number of elements of the Mesh. The resulting Mesh will contain all elements where t > 0.
This is the complement of clip, returning a Mesh where t<=0.
Return the Mesh clipped at plane (p,n).
This is a convenience function returning the part of the Mesh at one side of the plane (p,n)
area of elements
For surface element the faces’ area is returned. For volume elements the sum of the faces’areas is returned.
Return the signed volume of all the mesh elements
For a ‘tet4’ tetraeder Mesh, the volume of the elements is calculated as 1/3 * surface of base * height.
For other Mesh types the volumes are calculated by first splitting the elements into tetraeder elements.
The return value is an array of float values with length equal to the number of elements. If the Mesh conversion to tetraeder does not succeed, the return value is None.
Return the total volume of a Mesh.
If the Mesh can not be converted to tet4 type, 0 is returned
Returns the equiAngleSkew of the elements, a mesh quality parameter .
It quantifies the skewness of the elements: normalize difference between the worst angle in each element and the ideal angle (angle in the face of an equiangular element, qe).
Functions defined in module mesh
Return a rotation matrix for rotating vector vec1 to vec2
The rotation matrix will be such that the plane of vec2 and the rotated upvec will be parallel to the original upvec.
This function is like arraytools.rotMatrix(), but allows the specification of vec1. The returned matrix should be used in postmultiplication to the Coords.
Sweep a Coords object along a path, returning a series of copies.
origin and normal define the local path position and direction on the mesh.
At each point of the curve, a copy of the Coords object is created, with its origin in the curve’s point, and its normal along the curve’s direction. In case of a PolyLine, directions are pointing to the next point by default. If avgdir==True, average directions are taken at the intermediate points. Missing end directions can explicitely be set by enddir, and are by default taken along the last segment. If the curve is closed, endpoints are treated as any intermediate point, and the user should normally not specify enddir.
At each point of the curve, the original Coords object can be scaled in x and y direction by specifying scalex and scaley. The number of values specified in scalex and scaly should be equal to the number of points on the curve.
The return value is a sequence of the transformed Coords objects.
Default element type for a mesh with given plexitude.
For the most common cases of plexitudes, we define a default element type. The full list of default types can be found in plugins.mesh._default_eltype.
Merge all the nodes of a list of node sets.
Each item in nodes is a Coords array. The return value is a tuple with:
The merging operation can be tuned by specifying extra arguments that will be passed to Coords.fuse().
Merge all the nodes of a list of Meshes.
Each item in meshes is a Mesh instance. The return value is a tuple with:
The merging operation can be tuned by specifying extra arguments that will be passed to Coords:fuse(). Setting fuse=False will merely concatenate all the mesh.coords, but not fuse them.
Connect two meshes to form a hypermesh.
mesh1 and mesh2 are two meshes with same topology (shape). The two meshes are connected by a higher order mesh with n elements in the direction between the two meshes. n1 and n2 are node selection indices permitting a permutation of the nodes of the base sets in their appearance in the hypermesh. This can e.g. be used to achieve circular numbering of the hypermesh.
currently works for Quad8 only.
Connect two Quad8 meshes to form a Hex20 mesh.