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

50 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-09-29 11:30 +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 

98class NodeCosserat(Node): 

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

100 three positions and three rotations.""" 

101 

102 def __init__( 

103 self, 

104 coordinates, 

105 rotation: _Rotation, 

106 *, 

107 arc_length: float | None = None, 

108 **kwargs, 

109 ): 

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

111 

112 # Rotation of this node. 

113 self.rotation = rotation.copy() 

114 

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

116 self.arc_length = arc_length 

117 

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

119 """Rotate this node. 

120 

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

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

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

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

125 """ 

126 

127 self.rotation = rotation * self.rotation 

128 

129 # Rotate the positions (around origin). 

130 if not only_rotate_triads: 

131 if origin is not None: 

132 self.coordinates = self.coordinates - origin 

133 self.coordinates = rotation * self.coordinates 

134 if origin is not None: 

135 self.coordinates = self.coordinates + origin 

136 

137 

138class ControlPoint(Node): 

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

140 

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

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

143 

144 # Weight of this node 

145 self.weight = weight