Coverage for src/beamme/core/node.py: 98%

54 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-11 12:17 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2018-2025 BeamMe 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 node in the Mesh.""" 

23 

24import numpy as _np 

25 

26from beamme.core.base_mesh_item import BaseMeshItem as _BaseMeshItem 

27from beamme.core.rotation import Rotation as _Rotation 

28 

29 

30class Node(_BaseMeshItem): 

31 """This object represents one node in the mesh.""" 

32 

33 def __init__(self, coordinates, *, is_middle_node=False, **kwargs): 

34 super().__init__(**kwargs) 

35 

36 # Coordinates of this node. 

37 self.coordinates = _np.array(coordinates) 

38 

39 # If this node is at the end of a line or curve (by default only those 

40 # nodes are checked for overlapping nodes). 

41 self.is_end_node = False 

42 

43 # If the node is in the middle of a beam element. 

44 self.is_middle_node = is_middle_node 

45 

46 # Lists with the objects that this node is linked to. 

47 self.element_link = [] 

48 self.node_sets_link = [] 

49 self.element_partner_index = None 

50 self.mesh = None 

51 

52 # If this node is replaced, store a link to the remaining node. 

53 self.master_node = None 

54 

55 def get_master_node(self): 

56 """Return the master node of this node. 

57 

58 If the node has not been replaced, this object is returned. 

59 """ 

60 

61 if self.master_node is None: 

62 return self 

63 else: 

64 return self.master_node.get_master_node() 

65 

66 def replace_with(self, master_node): 

67 """Replace this node with another node object.""" 

68 

69 # Check that the two nodes have the same type. 

70 if not isinstance(self, type(master_node)): 

71 raise TypeError( 

72 "A node can only be replaced by a node with the same type. " 

73 + f"Got {type(self)} and {type(master_node)}" 

74 ) 

75 

76 # Replace the links to this node in the referenced objects. 

77 self.mesh.replace_node(self, master_node) 

78 for element in self.element_link: 

79 element.replace_node(self, master_node) 

80 for node_set in self.node_sets_link: 

81 node_set.replace_node(self, master_node) 

82 

83 # Set link to master node. 

84 self.master_node = master_node.get_master_node() 

85 

86 def unlink(self): 

87 """Reset the links to elements, node sets and global indices.""" 

88 self.element_link = [] 

89 self.node_sets_link = [] 

90 self.mesh = None 

91 self.i_global = None 

92 

93 def rotate(self, *args, **kwargs): 

94 """Don't do anything for a standard node, as this node can not be 

95 rotated.""" 

96 

97 def dump_to_list(self): 

98 """Return a list with the legacy string representing this node.""" 

99 

100 return { 

101 "id": self.i_global + 1, 

102 "COORD": self.coordinates, 

103 "data": {"type": "NODE"}, 

104 } 

105 

106 

107class NodeCosserat(Node): 

108 """This object represents a Cosserat node in the mesh, i.e., it contains 

109 three positions and three rotations.""" 

110 

111 def __init__( 

112 self, 

113 coordinates, 

114 rotation: _Rotation, 

115 *, 

116 arc_length: float | None = None, 

117 **kwargs, 

118 ): 

119 super().__init__(coordinates, **kwargs) 

120 

121 # Rotation of this node. 

122 self.rotation = rotation.copy() 

123 

124 # Arc length along the filament that this beam is a part of 

125 self.arc_length = arc_length 

126 

127 def rotate(self, rotation, *, origin=None, only_rotate_triads=False): 

128 """Rotate this node. 

129 

130 By default the node is rotated around the origin (0,0,0), if the 

131 keyword argument origin is given, it is rotated around that 

132 point. If only_rotate_triads is True, then only the rotation is 

133 affected, the position of the node stays the same. 

134 """ 

135 

136 self.rotation = rotation * self.rotation 

137 

138 # Rotate the positions (around origin). 

139 if not only_rotate_triads: 

140 if origin is not None: 

141 self.coordinates = self.coordinates - origin 

142 self.coordinates = rotation * self.coordinates 

143 if origin is not None: 

144 self.coordinates = self.coordinates + origin 

145 

146 

147class ControlPoint(Node): 

148 """This object represents a control point with a weight in the mesh.""" 

149 

150 def __init__(self, coordinates, weight, **kwargs): 

151 super().__init__(coordinates, **kwargs) 

152 

153 # Weight of this node 

154 self.weight = weight 

155 

156 def dump_to_list(self): 

157 """Return a list with the legacy string representing this control 

158 point.""" 

159 

160 return { 

161 "id": self.i_global + 1, 

162 "COORD": self.coordinates, 

163 "data": {"type": "CP", "weight": self.weight}, 

164 }