Coverage for src/beamme/core/nurbs_patch.py: 97%

67 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 NURBS patches for the mesh.""" 

23 

24from typing import Iterator as _Iterator 

25 

26import numpy as _np 

27 

28from beamme.core.conf import bme as _bme 

29from beamme.core.element import Element as _Element 

30from beamme.core.material import ( 

31 MaterialSolidBase as _MaterialSolidBase, 

32) 

33 

34 

35class NURBSPatch(_Element): 

36 """A base class for a NURBS patch.""" 

37 

38 # A list of valid material types for this element 

39 valid_materials = [_MaterialSolidBase] 

40 

41 def __init__( 

42 self, 

43 knot_vectors, 

44 polynomial_orders, 

45 material=None, 

46 nodes=None, 

47 data=None, 

48 ): 

49 super().__init__(nodes=nodes, material=material, data=data) 

50 

51 # Knot vectors 

52 self.knot_vectors = knot_vectors 

53 

54 # Polynomial degrees 

55 self.polynomial_orders = polynomial_orders 

56 

57 # Set numbers for elements 

58 self.i_nurbs_patch = None 

59 

60 def get_nurbs_dimension(self) -> int: 

61 """Determine the number of dimensions of the NURBS structure. 

62 

63 Returns: 

64 Number of dimensions of the NURBS object. 

65 """ 

66 n_knots = len(self.knot_vectors) 

67 n_polynomial = len(self.polynomial_orders) 

68 if not n_knots == n_polynomial: 

69 raise ValueError( 

70 "The variables n_knots and polynomial_orders should have " 

71 f"the same length. Got {n_knots} and {n_polynomial}" 

72 ) 

73 return n_knots 

74 

75 def get_number_of_control_points_per_dir(self) -> list[int]: 

76 """Determine the number of control points in each parameter direction 

77 of the patch. 

78 

79 Returns: 

80 List of control points per direction. 

81 """ 

82 n_dim = len(self.knot_vectors) 

83 n_cp_per_dim = [] 

84 for i_dim in range(n_dim): 

85 knot_vector_size = len(self.knot_vectors[i_dim]) 

86 polynomial_order = self.polynomial_orders[i_dim] 

87 n_cp_per_dim.append(knot_vector_size - polynomial_order - 1) 

88 return n_cp_per_dim 

89 

90 def get_number_elements(self) -> int: 

91 """Determine the number of elements in this patch by checking the 

92 amount of nonzero knot spans in the knot vector. 

93 

94 Returns: 

95 Number of elements for this patch. 

96 """ 

97 

98 num_elements_dir = _np.zeros(len(self.knot_vectors), dtype=int) 

99 

100 for i_dir in range(len(self.knot_vectors)): 

101 for i_knot in range(len(self.knot_vectors[i_dir]) - 1): 

102 if ( 

103 abs( 

104 self.knot_vectors[i_dir][i_knot] 

105 - self.knot_vectors[i_dir][i_knot + 1] 

106 ) 

107 > _bme.eps_knot_vector 

108 ): 

109 num_elements_dir[i_dir] += 1 

110 

111 total_num_elements = _np.prod(num_elements_dir) 

112 

113 return total_num_elements 

114 

115 def _check_material(self) -> None: 

116 """Check if the linked material is valid for this type of NURBS solid 

117 element.""" 

118 for material_type in type(self).valid_materials: 

119 if isinstance(self.material, material_type): 

120 return 

121 raise TypeError( 

122 f"NURBS solid of type {type(self)} can not have a material of" 

123 f" type {type(self.material)}!" 

124 ) 

125 

126 

127class NURBSSurface(NURBSPatch): 

128 """A patch of a NURBS surface.""" 

129 

130 def __init__(self, *args, **kwargs): 

131 super().__init__(*args, **kwargs) 

132 

133 def _get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]: 

134 """Return a tuple with the knot spans for this patch.""" 

135 

136 p, q = self.polynomial_orders 

137 kv_u, kv_v = self.knot_vectors[:2] 

138 return ( 

139 (u, v) 

140 for v in range(q, len(kv_v) - q - 1) 

141 for u in range(p, len(kv_u) - p - 1) 

142 ) 

143 

144 def _get_ids_ctrlpts(self, knot_span_u: int, knot_span_v: int) -> list[int]: 

145 """Compute the global indices of the control points that influence the 

146 element defined by the given knot span.""" 

147 

148 p, q = self.polynomial_orders 

149 ctrlpts_size_u = len(self.knot_vectors[0]) - p - 1 

150 id_u = knot_span_u - p 

151 id_v = knot_span_v - q 

152 

153 return [ 

154 ctrlpts_size_u * (id_v + j) + id_u + i 

155 for j in range(q + 1) 

156 for i in range(p + 1) 

157 ] 

158 

159 

160class NURBSVolume(NURBSPatch): 

161 """A patch of a NURBS volume.""" 

162 

163 def __init__(self, *args, **kwargs): 

164 super().__init__(*args, **kwargs) 

165 

166 def _get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]: 

167 """Return a tuple with the knot spans for this patch.""" 

168 

169 p, q, r = self.polynomial_orders 

170 kv_u, kv_v, kv_w = self.knot_vectors 

171 return ( 

172 (u, v, w) 

173 for w in range(r, len(kv_w) - r - 1) 

174 for v in range(q, len(kv_v) - q - 1) 

175 for u in range(p, len(kv_u) - p - 1) 

176 ) 

177 

178 def _get_ids_ctrlpts( 

179 self, knot_span_u: int, knot_span_v: int, knot_span_w: int 

180 ) -> list[int]: 

181 """Compute the global indices of the control points that influence the 

182 element defined by the given knot span.""" 

183 

184 p, q, r = self.polynomial_orders 

185 id_u = knot_span_u - p 

186 id_v = knot_span_v - q 

187 id_w = knot_span_w - r 

188 size_u = len(self.knot_vectors[0]) - p - 1 

189 size_v = len(self.knot_vectors[1]) - q - 1 

190 

191 return [ 

192 size_u * size_v * (id_w + k) + size_u * (id_v + j) + id_u + i 

193 for k in range(r + 1) 

194 for j in range(q + 1) 

195 for i in range(p + 1) 

196 ]