12. elements
— Definition of elements for the Mesh
model¶
This modules provides local numbering schemes of element
connectivities for Mesh
models. It allows a consistent
numbering throughout pyFormex.
When interfacing with other programs, one should be aware
that conversions may be necessary. Conversions to/from external programs
are done by the interface modules.
The module defines the ElementType
class and a whole slew of its
instances, which are the element types used in pyFormex.
Here is also the definition of the Elems
class, which is a
specialisation of the Connectivity
using an
ElementType instance as the eltype. The Elems class is one of the basic
data holders in the Mesh
model.
12.1. Classes defined in module elements¶
-
class
elements.
ElementType
(name, doc, ndim, vertices, edges=None, faces=None, **kargs)[source]¶ Element types for
Mesh
models.ElementType instances store all data that define a particular geometrical entity type and that can not be derived from the plexitude. The class is mostly a storage of provided (and sanitized) data. All succesfully created elements are stored in the class-owned
register
, from where they can be looked up by their name.Parameters: - name (str) – The name of the element. Case is ignored. It is stored in the ElementType instance in capitalized form. The key in the register is the name in all lower case. Usually the name has a numeric last part equal to the plexitude of the element, but this is not a requirement.
- doc (str) – A short description of the element.
- ndim (int) –
The dimensionality (level) of the element (0..3):
- 0: point
- 1: line
- 2: surface
- 3: volume
- vertices (float array_like) – The natural coordinates of the nodes (nplex,3). This also defines the plexitude (the number of nodes) of the element and the local node numbering: range(nplex). The vertices of the elements are usually defined in a unit space [0,1] in each axis direction.
- edges (
Elems
) – A connectivity table listing the edges of the element with local node numbers. The eltype of the Elems should be an ElementType instance of level 1. This parameter should be provided if and only if the element is of level 2 or 3. The edges should be the conceptually correct edges of the element. If the element contains edges of different plexitudes, they should be specified with the highest plexitude and degenerate elements should be used for the lower plexitude edges. - faces (
Elems
) – A connectivity table listing the faces of the element with local node numbers. The eltype of the Elems should be an ElementType instance of level 2. This parameter should be provided if and only iff the element is of level 3. The faces should be the conceptually correct faces of the element. If the element contains faces of different plexitudes, they should be specified with the highest plexitude and degenerate elements should be used for the lower plexitude faces. - *kargs (keyword arguments) – Other keyword arguments are stored in the ElementType as is. These can also be set after initialization by direct assignment to the instance’s attribute. Below are some predefined values used by pyFormex.
- reversed (int array_like) – The order of the nodes for the reversed element. Reversing a line element reverses its parametric direction. Reversing a surface element reverses the direction of the positive normal. Reversing a volume element turns the element inside out (which could possibly be used to represent holes in space).
- drawgl2faces (
Elems
) – A connectivity defining the entities to be used in rendering the element. This should only be provided if it the rendered geometry should be different from the element itself or from the elementfaces
. This is used to draw approximate renderings of elements for which there is no correct functionality available: linear approximations for higher order elements, triangular subdivisions for quadrilaterals. Note that although the name suggests faces, this connectivity can be of any level. - drawgl2edges (
Elems
) – A connectivity defining the entities to be used in wire rendering of the element. This is likedrawgl2faces
but defines the edges to be rendered in the wireframe and smoothwire and flatwire rendering modes. - conversions (dict) – A dict holding the possible strategies for conversion of the element to other element types. The key is the target element name, possibly extended with an extra string to discriminate between multiple strategies leading to the same element. The value is a list of actions to undertake to get from the initial element type to the target element type. See more details in the section Element type conversion below.
- extruded (dict) – A dict with data for how to extrude an element to a higher level element. Extrusion increases the level of the element with one, by creating 1 or 2 new planes of nodes in a new direction. The key in the dict is the degree of the extrusion: 1 or 2. The value is a tuple (eltype, nodes), where eltype is the target element type (an existing ElementType instance) and nodes is a list of nodes numbers as they will appear in the new node planes. If nodes is left out of the tuple, they will be ordered exactly like in the original element.
- degenerate (dict) – A dict with data for how to replace a degenerate element with a reduced element. A degenerate element has one or more coinciding nodes. The reduced elements replaces coinciding nodes with a single node yielding an element with lower plexitude. The keys in the dict are reduced element types. The values are lists of coinciding node conditions and matching reduction schemes: see the section Degenerate element reduction below for more details.
Notes
The ordering of the
vertices
defines a fixed local numbering scheme of the nodes in the element. The ordering of the items inedges
andfaces
attributes specify the local numbering of the edges and faces. For solid elements, it is guaranteed that the vertices of all faces are numbered in a consecutive order spinning positively around the outward normal on the face.Some of the parameters of an ElementType are instances of
Elems
, but Elems instances contain themselves anElementType
instance. Therefore care should be taken not to define circular dependencies. If the ElementType instances are created in order of increasing level, there is no problem with theedges
andfaces
parameters, as these are of a lower level than the element itself, and will have been defined before. For other attributes however this might not always be the case. These attributes can then be defined by direct assignment, after the needed ElementTypes have been initialized.List of elements
The list of available element types can be found from:
>>> ElementType.listall() Available Element Types: 0-dimensional elements: ['point'] 1-dimensional elements: ['line2', 'line3', 'line4'] 2-dimensional elements: ['tri3', 'tri6', 'quad4', 'quad6', 'quad8', 'quad9', 'quad12'] 3-dimensional elements: ['tet4', 'tet10', 'tet14', 'tet15', 'wedge6', 'hex8', 'hex16', 'hex20', 'hex27', 'octa', 'icosa']
Element type conversion
Element type conversion in pyFormex is a powerful feature to transform Mesh type objects. While mostly used to change the element type, there are also conversion types that refine the Mesh.
Available conversion methods are defined in an attribute conversion of the input element type. This attribute should be a dictionary, where the keys are the name of the conversion method and the values describe what steps need be taken to achieve this conversion. The method name should be the name of the target element, optionally followed by a suffix to discriminate between different methods yielding the same target element type. The suffix should always start with a ‘-‘. The part starting at the ‘-‘ will be stripped of to set the final target element name.
E.g., a ‘line3’ element type is a quadratic line element through three points. There are two available methods to convert it to ‘line2’ (straight line segments betwee two points), named named ‘line2’, resp. ‘line2-2’. The first will transform a ‘line3’ element in a single ‘line2’ between the two endpoints (i.e. the chord of the quadratic element); the second will replace each ‘line3’ with two straight segments: from first to central node, and from central node to end node.
The values in the dictionary are a list of execution steps to be performed in the conversion. Each step is a tuple of a single character defining the type of the step, and the data needed by this type of step. The steps are executed one by one to go from the source element type to the target.
Currently, the following step types are defined:
Type Data ‘s’ (select) connectivity list of selected nodes ‘a’ (average) list of tuples of nodes to be averaged ‘v’ (via) string with name of intermediate element type ‘x’ (execute) the name of a proper conversion function ‘r’ (random) list of conversion method names The operation of these methods is as follows:
- ‘s’ (select): This is the most common conversion type. It selects a set of nodes of the input element, and creates one or more new elements with these nodes. The data field is a list of tuples defining for each created element which node numbers from the source element should be included. This method is typically used to reduce the plexitude of the element.
- ‘a’ (average): Creates new nodes, the position of which is computed as an average of existing nodes. The data field is a list of tuples with the numbers of the nodes that should be averaged for each new node. The resulting new nodes are added in order at the end of the existing nodes. If this order is not the proper local node numbering, an ‘s’ step should follow to put the (old and new) nodes in the proper order. This method will usually increase the plexitude of the elements.
- ‘v’ (via): The conversion is made via an intermediate element type. The initial element type is first converted to this intermediate type and the result is then transformed to the target type.
- ‘x’ (execute): Calls a function to do the conversion. The function takes the input Mesh as argument and returns the converted Mesh. Currently this function should be a global function in the mesh module. Its name is specified as data in the conversion rule.
- ‘r’ (random): Chooses a random method between a list of alternatives. The data field is a list of conversion method names defined for the same element (and thus inside the same dictionary). While this could be considered an amusement (e.g. used in the Carpetry example), there are serious applications for this, e.g. when transforming a Mesh of squares or rectangles into a Mesh of triangles, by adding one diagonal in each element. Results with such a Mesh may however be different dependent on the choice of diagonal. The converted Mesh has directional preferences, not present in the original. The Quad4 to Tri3 conversion therefore has the choice to use either ‘up’ or ‘down’ diagonals. But a better choice is often the ‘random’ method, which will put the diagonals in a random direction, thus reducing the effect.
Degenerate element reduction
Each element can have an attribute
degenerate
, defining some rules of how the element can be reduced to a lower plexitude element in case it becomes degenerate. An element is said to be degenerate if the same node number is used more than once in the connectivity of a single element. Reduction of degenerate elements is usually only done if the element can be reduced to another element of the same level. For example, if two node of a quadrilateral element are coinciding, it could be replaced with a triangle. Both are level 2 elements. When the triangle has two coinciding nodes however, the element is normally not reduced to a line (level 1), but rather completely removed from the (level 2) model. However, nothing prohibits cross-level reductions.The
degenerate
attribute of an element is a dict where the key is a target element and the corresponding value is a list of reduction rules. Each rule is a tuple of two items: a set of conditions and a node selector to reduce the element. The conditions items is itself a tuple of any number of conditions, where each condition is a tuple of two node indices. If these nodes are equal, the condition is met. If all conditions in a rule are met, the reduction rule is applied. The second item in a rule, the node selector, is an index specifying the indices of the nodes that should be retained in the new (reduced) elements.As an example, take the Line3 element, which has 3 nodes defining a curved line element. Node 1 is the middle node, and nodes 0 and 2 are the end nodes. The element has four ways of being degenerate: nodes 0 and 1 are the same, nodes 1 and 2 are the same, nodes 0 and 2 are the same, and all three nodes are the same. For the first two of them, a reduction scheme is defined, reducing the element to a straight line segment (Line2) between the two end points:
Line3.degenerate = { Line2: [(((0, 1), ), (0, 2)), (((1, 2), ), (0, 2)), ], }
In this case each of the reduction rules contains only a single condition, but there exist cases where multiple conditions have to be net at the same time, which is why the condition (0, 1) is itself enclosed in a tuple. But what about the other degenerate cases. If both end points coincide, it is not clear what to do: reduce to a line segment between the coincident end points, or between an end point and the middle. Here pyFormex made the choice to not reduce such case and leave the degenerate Line3 element. But the user could add a rule to e.g. reduce the case to a line segment between end point and middle point:
Line3.degenerate[Line2].append((((0, 2), ), (0, 1)))
Also the case of three coinciding points is left unhandled. But the user could reduce such cases to a Point:
Line3.degenerate[Point] = [(((0, 1), (1, 2)), (0, ))]
Here we need two conditions to check that nodes 0, 1 and 2 are equal. However, in this case the user probably also wants the third degenerate case (nodes 0 and 2 are equal) to be reduced to a Point. So he could just use:
Line3.degenerate[Point] = [(((0, 2), ), (0, ))]
-
register
¶ This is a class attribute collecting all the created ElementType instances with their name in lower case as key.
Type: dict
Examples
>>> print(list(ElementType.register.keys())) ['point', 'line2', 'line3', 'line4', 'tri3', 'tri6', 'quad4', 'quad6', 'quad8', 'quad9', 'quad12', 'tet4', 'tet10', 'tet14', 'tet15', 'wedge6', 'hex8', 'hex16', 'hex20', 'hex27', 'octa', 'icosa'] >>> print(ElementType.default) {1: Point, 2: Line2, 3: Tri3, 4: Quad4, 6: Wedge6, 8: Hex8}
-
nvertices
()¶ Return the plexitude of the element
-
nnodes
()¶ Return the plexitude of the element
-
getEntities
(level)[source]¶ Return the type and connectivity table of some element entities.
Parameters: level (int) – The level of the entities to return. If negative, it is a value relative to the level of the caller. If non-negative, it specifies the absolute level. Thus, for an ElementType of level 3, getEntities(-1) returns the faces, while for a level 2 ElementType, it returns the edges. In both cases however, getEntities(1) returns the edges. Returns: Elems
|Connectivity
– The connectivity table and element type of the entities of the specified level. The type is normally Elems. If the requested entity level is outside the range 0..ndim, an empty Connectivity is returned.Examples
>>> Tri3.getEntities(0) Elems([[0], [1], [2]], eltype=Point) >>> Tri3.getEntities(1) Elems([[0, 1], [1, 2], [2, 0]], eltype=Line2) >>> Tri3.getEntities(2) Elems([[0, 1, 2]], eltype=Tri3) >>> Tri3.getEntities(3) Connectivity([], shape=(0, 1))
-
getDrawFaces
(quadratic=False)[source]¶ Return the local connectivity for drawing the element’s faces
-
getDrawEdges
(quadratic=False)[source]¶ Return the local connectivity for drawing the element’s edges
-
toMesh
()[source]¶ Convert the element type to a Mesh.
Returns a Mesh with a single element of natural size.
-
toFormex
()[source]¶ Convert the element type to a Formex.
Returns a Formex with a single element of natural size.
-
name
()[source]¶ Return the lowercase name of the element.
For compatibility, name() returns the lower case version of the ElementType’s name. To get the real name, use the attribute _name or format the ElementType as a string.
-
family
()[source]¶ Return the element family name.
The element family name is the first part of the name that consists only of lower case letter.
-
classmethod
list
(ndim=None, types=False)[source]¶ Return a list of available ElementTypes.
Parameters: Returns: list – A list of ElementType names (default) or instances.
Examples
>>> ElementType.list() ['point', 'line2', 'line3', 'line4', 'tri3', 'tri6', 'quad4', 'quad6', 'quad8', 'quad9', 'quad12', 'tet4', 'tet10', 'tet14', 'tet15', 'wedge6', 'hex8', 'hex16', 'hex20', 'hex27', 'octa', 'icosa'] >>> ElementType.list(ndim=1) ['line2', 'line3', 'line4'] >>> ElementType.list(ndim=1, types=True) [Line2, Line3, Line4]
-
classmethod
listall
(lower=False)[source]¶ Print all available element types.
Prints a list of the names of all available element types, grouped by their dimensionality.
-
static
get
(eltype=None, nplex=None)[source]¶ Find the ElementType matching an element name and/or plexitude.
Parameters: - eltype (
ElementType
| str | None) – The element type or name. If not provided andnplex
is provided, the default element type for that plexitude will be used, if it exists. If a name, it should be the name of one of the existing ElementType instances (case insensitive). - nplex (int) – The plexitude of the element. If provided and an eltype was provided, it should match the eltype plexitude. If no eltype was provided, the default element type for this plexitude is returned.
Returns: ElementType
– The ElementType matching the provided eltype and/or nplexRaises: ValueError
– If neithername
nornplex
can resolve into an element type.Examples
>>> ElementType.get('tri3') Tri3 >>> ElementType.get(nplex=2).name() 'line2' >>> ElementType.get('QUAD4') Quad4
- eltype (
-
class
elements.
Elems
[source]¶ A Connectivity where the eltype is an ElementType subclass.
This is used to store the connectivity of a
Mesh
instance. It is also used to store some subitems in theElementType
.>>> C = Elems([[0,1,2],[0,1,3],[0,5,3]],'tri3') >>> C Elems([[0, 1, 2], [0, 1, 3], [0, 5, 3]], eltype=Tri3) >>> sel = C.levelSelector(1) >>> sel Elems([[0, 1], [1, 2], [2, 0]], eltype=Line2) >>> C.selectNodes(sel) Elems([[0, 1], [1, 2], [2, 0], [0, 1], [1, 3], [3, 0], [0, 5], [5, 3], [3, 0]], eltype=Line2) >>> C.selectNodes(Elems([[0,1],[0,2]],eltype=Line2)) Elems([[0, 1], [0, 2], [0, 1], [0, 3], [0, 5], [0, 3]], eltype=Line2) >>> C.selectNodes([[0,1],[0,2]]) Connectivity([[0, 1], [0, 2], [0, 1], [0, 3], [0, 5], [0, 3]]) >>> hi, lo = C.insertLevel(1) >>> hi Connectivity([[0, 1, 2], [0, 3, 4], [5, 6, 4]]) >>> lo Elems([[0, 1], [1, 2], [2, 0], [1, 3], [3, 0], [0, 5], [5, 3]], eltype=Line2) >>> hi, lo = C.insertLevel([[0,1],[1,2],[2,0]]) >>> hi Connectivity([[0, 1, 2], [0, 3, 4], [5, 6, 4]]) >>> lo Connectivity([[0, 1], [1, 2], [2, 0], [1, 3], [3, 0], [0, 5], [5, 3]]) >>> C = Elems([[0,1,2,3]],'quad4') >>> hi, lo = C.insertLevel([[0,1,2],[1,2,3],[0,1,1],[0,0,1],[1,0,0]]) >>> hi Connectivity([[0, 1, 2, 2, 2]]) >>> lo Connectivity([[0, 1, 2], [1, 2, 3], [0, 1, 1]])
-
levelSelector
(level)[source]¶ Return a selector for lower level entities.
Parameters: level (int) – An specifying one of the hierarchical levels of element entities. See Element.getEntities()
.Returns: Elems
– A new Elems object with shape(self.nelems*selector.nelems,selector.nplex)
.
-
selectNodes
(selector)[source]¶ Return a Connectivity with subsets of the nodes.
Parameters: selector (int | int array_like) – A single int specifies a relative or absolute hierarchical level of element entities (See the Element class). A 2-dim int array selector is then constructed automatically from
self.eltype.getEntities(selector)
.Else, it is a 2-dim int array like (often a
Connectivity
or anotherElems
. Each row of selector holds a list of the local node numbers that should be retained in the new Connectivity table.As an example, if the Elems is plex-3 representing triangles, a selector [[0,1],[1,2],[2,0]] would extract the edges of the triangle. The same would be obtained with
selector=-1
orselector=1
.Returns: Elems
|Connectivity
– An Elems or Connectivity object with eltype equal to that of the selector. This means that if the selector has an eltype that is one of the elements defined inelements
, the return type will be Elems, else Connectivity. The shape is(self.nelems*selector.nelems,selector.nplex)
. Duplicate elements created by the selector are retained.
-
insertLevel
(selector, permutations='all')[source]¶ Insert an extra hierarchical level in a Connectivity table.
A Connectivity table identifies higher hierarchical entities in function of lower ones. This method inserts an extra level in the hierarchy. For example, if you have volumes defined in function of points, you can insert an intermediate level of edges, or faces. Each element may generate multiple instances of the intermediate level.
Parameters: - selector (int | int array_like) –
A single int specifies a relative or absolute hierarchical level of element entities (See the Element class). A 2-dim int array selector is then constructed automatically from
self.eltype.getEntities(selector)
.Else, it is a 2-dim int array like (often a
Connectivity
or anotherElems
. Each row of selector holds a list of the local node numbers that should be retained in the new Connectivity table. - permutations (str) –
Defines which permutations of the row data are allowed while still considering the rows equal. Equal rows in the intermediate level are collapsed into single items. Possible values are:
- ’none’: no permutations are allowed: rows must match the same date at the same positions.
- ’roll’: rolling is allowed. Rows that can be transformed into each other by rolling are considered equal;
- ’all’: any permutation of the same data will be considered an equal row. This is the default.
Returns: - hi (
Connectivity
) – A Connecivity defining the original elements in function of the intermediate level ones. - lo (
Elems
|Connectivity
) – An Elems or Connectivity object with eltype equal to that of the selector. This means that if the selector has an eltype that is one of the elements defined inelements
, the return type will be Elems, else Connectivity. - The resulting node numbering of the created intermediate entities
- (the lo return value) respects the numbering order of the original
- elements and the applied the selector, but in case of collapsing
- duplicate rows, it is undefined which of the collapsed sequences is
- returned.
- Because the precise order of the data in the collapsed rows is lost,
- it is in general not possible to restore the exact original table
- from the two result tables.
- See however
mesh.Mesh.getBorder()
for an application where an - inverse operation is possible, because the border only contains
- unique rows.
- See also
mesh.Mesh.combine()
, which is an almost inverse operation - for the general case, if the selector is complete.
- The resulting rows may however be permutations of the original.
- selector (int | int array_like) –
-
reduceDegenerate
(target=None, return_indices=False)[source]¶ Reduce degenerate elements to lower plexitude elements.
This will try to reduce the degenerate elements of the Elems to lower plexitude elements. This is only possible if the ElementType has an attribute
degenerate
containing the proper reduction rules.Parameters: - target (str, optional) – Target element name. If provided, only reductions to that element type are performed. Else, all the target element types for which a reduction scheme is available, will be tried.
- return_indices (bool, optional) – If True, also returns the indices of the elements in the input Elems that are contained in each of the parts returned.
Returns: - conn (list of Elems instances) – A list of Elems of which the first one contains the originally
non-degenerate elements, the next one(s) contain the reduced
elements (per reduced element type) and the last one contains
elements that could not be reduced (this may be absent).
In the following cases a list with only the original is returned:
- there are no degenerate elements in the Elems;
- the ElementType does not have any reduction scheme defined;
- the ElementType does not have a reduction scheme fot the target.
- ind (list of indices, optional) – Only returned if
return_indices
is True: the indices of the elements ofself
contained in each item inconn
.
Note
If the Elems is part of a
Mesh
, you should use themesh.Mesh.splitDegenerate()
method instead, as that will preserve the property numbers in the resulting Meshes.The returned reduced Elems may still be degenerate for their own element type.
See also
mesh.Mesh.splitDegenerate()
- split mesh in non-degenerate and reduced
Examples
>>> Elems([[0,1,2],[0,1,3]],eltype=Line3).reduceDegenerate() [Elems([[0, 1, 2], [0, 1, 3]], eltype=Line3)] >>> C = Elems([[0,1,2],[0,1,1],[0,3,2]],eltype='line3') >>> C.reduceDegenerate() [Elems([[0, 1, 2], [0, 3, 2]], eltype=Line3), Elems([[0, 1]], eltype=Line2)] >>> C = Elems([[0,1,2],[0,1,1],[0,3,2],[1,1,1],[0,0,2]],eltype=Line3) >>> C.reduceDegenerate() [Elems([[0, 1, 2], [0, 3, 2]], eltype=Line3), Elems([[1, 1], [0, 2], [0, 1]], eltype=Line2)] >>> conn, ind = C.reduceDegenerate(return_indices=True) >>> conn [Elems([[0, 1, 2], [0, 3, 2]], eltype=Line3), Elems([[1, 1], [0, 2], [0, 1]], eltype=Line2)] >>> ind [array([0, 2]), array([3, 4, 1])]
-
extrude
(nnod, degree)[source]¶ Extrude an Elems to a higher level Elems.
Parameters: - nnod (int) – Node increment for each new node plane. It should be higher than the highest node number in self.
- degree (int) – Number of node planes to add. This is also the degree of the extrusion. Currently it is limited to 1 or 2.
- extrusion adds degree planes of nodes, each with a node (The) –
- nnod, to the original Elems and then selects (increment) –
- target nodes from it as defined by the (the) –
- value. (self.eltype.extruded[degree]) –
Returns: Elems – An Elems for the extruded element.
Examples
>>> a = Elems([[0,1],[1,2]],eltype=Line2).extrude(3,1) >>> print(a) [[0 1 4 3] [1 2 5 4]] >>> print(a.eltype.name()) quad4 >>> a = Elems([[0,1,2],[0,2,3]],eltype=Line3).extrude(4,2) >>> print(a) [[ 0 2 10 8 1 6 9 4 5] [ 0 3 11 8 2 7 10 4 6]] >>> print(a.eltype.name()) quad9
-