Coverage for src/meshpy/core/element.py: 89%
61 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2018-2025 MeshPy Authors
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22"""This module implements the class that represents one element in the Mesh."""
24from typing import List as _List
26from meshpy.core.base_mesh_item import BaseMeshItem as _BaseMeshItem
27from meshpy.core.node import Node as _Node
28from meshpy.utils.environment import fourcipp_is_available as _fourcipp_is_available
31class Element(_BaseMeshItem):
32 """A base class for an FEM element in the mesh."""
34 def __init__(self, nodes=None, material=None, **kwargs):
35 super().__init__(data=None, **kwargs)
37 # List of nodes that are connected to the element.
38 if nodes is None:
39 self.nodes = []
40 else:
41 self.nodes = nodes
43 # Material of this element.
44 self.material = material
46 # VTK cell data for this element.
47 self.vtk_cell_data = {}
49 @classmethod
50 def from_legacy_string(cls, nodes: _List[_Node], input_line: str):
51 """Create an element from a legacy string."""
53 if _fourcipp_is_available():
54 raise ValueError(
55 "Port this functionality to create the element from the dict "
56 "representing the element, not the legacy string."
57 "TODO: pass the nodes array here, so we can directly link to the nodes"
58 )
60 # Import solid element classes for creation of the element.
61 from meshpy.core.element_volume import VolumeHEX8 as _VolumeHEX8
62 from meshpy.core.element_volume import VolumeHEX20 as _VolumeHEX20
63 from meshpy.core.element_volume import VolumeHEX27 as _VolumeHEX27
64 from meshpy.core.element_volume import VolumeTET4 as _VolumeTET4
65 from meshpy.core.element_volume import VolumeTET10 as _VolumeTET10
66 from meshpy.core.element_volume import VolumeWEDGE6 as _VolumeWEDGE6
67 from meshpy.four_c.element_volume import SolidRigidSphere as _SolidRigidSphere
69 # Split up input line and get pre node string.
70 line_split = input_line.split()
71 string_pre_nodes = " ".join(line_split[1:3])
73 # Get a list of the element nodes.
74 # This is only here because we need the pre and post strings - can be
75 # removed when moving on from the legacy format.
76 dummy = []
77 for i, item in enumerate(line_split[3:]):
78 if item.isdigit():
79 dummy.append(int(item) - 1)
80 else:
81 break
82 else:
83 raise ValueError(
84 f'The input line:\n"{input_line}"\ncould not be converted to a solid element!'
85 )
87 # Get the post node string
88 string_post_nodes = " ".join(line_split[3 + i :])
90 # Depending on the number of nodes chose which solid element to return.
91 n_nodes = len(nodes)
92 match n_nodes:
93 case 8:
94 return _VolumeHEX8(
95 nodes=nodes,
96 string_pre_nodes=string_pre_nodes,
97 string_post_nodes=string_post_nodes,
98 )
99 case 4:
100 return _VolumeTET4(
101 nodes=nodes,
102 string_pre_nodes=string_pre_nodes,
103 string_post_nodes=string_post_nodes,
104 )
105 case 10:
106 return _VolumeTET10(
107 nodes=nodes,
108 string_pre_nodes=string_pre_nodes,
109 string_post_nodes=string_post_nodes,
110 )
111 case 20:
112 return _VolumeHEX20(
113 nodes=nodes,
114 string_pre_nodes=string_pre_nodes,
115 string_post_nodes=string_post_nodes,
116 )
117 case 27:
118 return _VolumeHEX27(
119 nodes=nodes,
120 string_pre_nodes=string_pre_nodes,
121 string_post_nodes=string_post_nodes,
122 )
123 case 6:
124 return _VolumeWEDGE6(
125 nodes=nodes,
126 string_pre_nodes=string_pre_nodes,
127 string_post_nodes=string_post_nodes,
128 )
129 case 1:
130 return _SolidRigidSphere(
131 nodes=nodes,
132 string_pre_nodes=string_pre_nodes,
133 string_post_nodes=string_post_nodes,
134 )
135 case _:
136 raise TypeError(
137 f"Could not find a element type for {string_pre_nodes}, with {n_nodes} nodes"
138 )
140 def flip(self):
141 """Reverse the nodes of this element.
143 This is usually used when reflected.
144 """
145 raise NotImplementedError(
146 f"The flip method is not implemented for {self.__class__}"
147 )
149 def replace_node(self, old_node, new_node):
150 """Replace old_node with new_node."""
152 # Look for old_node and replace it. If it is not found, throw error.
153 for i, node in enumerate(self.nodes):
154 if node == old_node:
155 self.nodes[i] = new_node
156 break
157 else:
158 raise ValueError(
159 "The node that should be replaced is not in the current element"
160 )
162 def dump_element_specific_section(self, yaml_dict):
163 """Add information of this element to specific section (e.g. STRUCTURE
164 KNOTVECTORS for NURBS elements)."""
166 def get_vtk(self, vtk_writer_beam, vtk_writer_solid, **kwargs):
167 """Add representation of this element to the vtk_writers for solid and
168 beam."""
169 raise NotImplementedError("VTK output has to be implemented in the class!")