Coverage for src/meshpy/core/element_volume.py: 98%

43 statements  

« 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 file defines the base volume element in MeshPy.""" 

23 

24import numpy as _np 

25import vtk as _vtk 

26 

27from meshpy.core.element import Element as _Element 

28from meshpy.core.vtk_writer import add_point_data_node_sets as _add_point_data_node_sets 

29 

30 

31class VolumeElement(_Element): 

32 """A base class for a volume element.""" 

33 

34 # This class variables stores the information about the element shape in 

35 # vtk. And the connectivity to the nodes. 

36 vtk_cell_type = None 

37 vtk_topology: list = [] 

38 

39 def __init__(self, nodes=None, string_pre_nodes="", string_post_nodes="", **kwargs): 

40 super().__init__(nodes=nodes, material=None, **kwargs) 

41 self.string_pre_nodes = string_pre_nodes 

42 self.string_post_nodes = string_post_nodes 

43 

44 def dump_to_list(self): 

45 """Return a list with the items representing this object (usually a 

46 single item).""" 

47 

48 # String with the node ids. 

49 nodes_string = " ".join(str(node.i_global) for node in self.nodes) 

50 

51 return [ 

52 f"{self.i_global} {self.string_pre_nodes} {nodes_string} {self.string_post_nodes}" 

53 ] 

54 

55 def get_vtk(self, vtk_writer_beam, vtk_writer_solid, **kwargs): 

56 """Add the representation of this element to the VTK writer as a 

57 quad.""" 

58 

59 # Check that the element has a valid vtk cell type. 

60 if self.vtk_cell_type is None: 

61 raise TypeError(f"vtk_cell_type for {type(self)} not set!") 

62 

63 # Dictionary with cell data. 

64 cell_data = {} 

65 

66 # Dictionary with point data. 

67 point_data = {} 

68 

69 # Array with nodal coordinates. 

70 coordinates = _np.zeros([len(self.nodes), 3]) 

71 for i, node in enumerate(self.nodes): 

72 coordinates[i, :] = node.coordinates 

73 

74 # Add the node sets connected to this element. 

75 _add_point_data_node_sets(point_data, self.nodes) 

76 

77 # Add cell to writer. 

78 indices = vtk_writer_solid.add_points(coordinates, point_data=point_data) 

79 vtk_writer_solid.add_cell( 

80 self.vtk_cell_type, indices[self.vtk_topology], cell_data=cell_data 

81 ) 

82 

83 

84class VolumeWEDGE6(VolumeElement): 

85 """A WEDGE6 volume element.""" 

86 

87 vtk_cell_type = _vtk.vtkWedge 

88 vtk_topology = list(range(6)) 

89 

90 

91class VolumeHEX8(VolumeElement): 

92 """A HEX8 volume element.""" 

93 

94 vtk_cell_type = _vtk.vtkHexahedron 

95 vtk_topology = list(range(8)) 

96 

97 

98class VolumeTET4(VolumeElement): 

99 """A TET4 volume element.""" 

100 

101 vtk_cell_type = _vtk.vtkTetra 

102 vtk_topology = list(range(4)) 

103 

104 

105class VolumeTET10(VolumeElement): 

106 """A TET10 volume element.""" 

107 

108 vtk_cell_type = _vtk.vtkQuadraticTetra 

109 vtk_topology = list(range(10)) 

110 

111 

112class VolumeHEX20(VolumeElement): 

113 """A HEX20 volume element.""" 

114 

115 vtk_cell_type = _vtk.vtkQuadraticHexahedron 

116 vtk_topology = [ 

117 0, 

118 1, 

119 2, 

120 3, 

121 4, 

122 5, 

123 6, 

124 7, 

125 8, 

126 9, 

127 10, 

128 11, 

129 16, 

130 17, 

131 18, 

132 19, 

133 12, 

134 13, 

135 14, 

136 15, 

137 ] 

138 

139 

140class VolumeHEX27(VolumeElement): 

141 """A HEX27 volume element.""" 

142 

143 vtk_cell_type = _vtk.vtkTriQuadraticHexahedron 

144 vtk_topology = [ 

145 0, 

146 1, 

147 2, 

148 3, 

149 4, 

150 5, 

151 6, 

152 7, 

153 8, 

154 9, 

155 10, 

156 11, 

157 16, 

158 17, 

159 18, 

160 19, 

161 12, 

162 13, 

163 14, 

164 15, 

165 24, 

166 22, 

167 21, 

168 23, 

169 20, 

170 25, 

171 26, 

172 ]