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

74 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-21 12:57 +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 abc import abstractmethod as _abstractmethod 

25from typing import Iterator as _Iterator 

26 

27import numpy as _np 

28 

29from beamme.core.conf import bme as _bme 

30from beamme.core.element import Element as _Element 

31from beamme.core.material import ( 

32 MaterialSolidBase as _MaterialSolidBase, 

33) 

34 

35 

36class NURBSPatch(_Element): 

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

38 

39 # A list of valid material types for this element 

40 valid_materials = [_MaterialSolidBase] 

41 

42 def __init__( 

43 self, 

44 knot_vectors, 

45 polynomial_orders, 

46 material=None, 

47 nodes=None, 

48 data=None, 

49 ): 

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

51 

52 # Knot vectors 

53 self.knot_vectors = knot_vectors 

54 

55 # Polynomial degrees 

56 self.polynomial_orders = polynomial_orders 

57 

58 # Set numbers for elements 

59 self.i_nurbs_patch = None 

60 

61 def get_nurbs_dimension(self) -> int: 

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

63 

64 Returns: 

65 Number of dimensions of the NURBS object. 

66 """ 

67 n_knots = len(self.knot_vectors) 

68 n_polynomial = len(self.polynomial_orders) 

69 if not n_knots == n_polynomial: 

70 raise ValueError( 

71 "The variables n_knots and polynomial_orders should have " 

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

73 ) 

74 return n_knots 

75 

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

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

78 of the patch. 

79 

80 Returns: 

81 List of control points per direction. 

82 """ 

83 n_dim = len(self.knot_vectors) 

84 n_cp_per_dim = [] 

85 for i_dim in range(n_dim): 

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

87 polynomial_order = self.polynomial_orders[i_dim] 

88 n_cp_per_dim.append(knot_vector_size - polynomial_order - 1) 

89 return n_cp_per_dim 

90 

91 def get_non_empty_knot_span_indices(self) -> list[list[int]]: 

92 """Determine the indices of the non-empty knot spans in each parameter 

93 direction. 

94 

95 Returns: 

96 List of lists with the indices of the non-empty knot spans in 

97 each parameter direction. 

98 """ 

99 

100 non_empty_knot_spans_indices: list[list[int]] = [ 

101 [] for _ in range(self.get_nurbs_dimension()) 

102 ] 

103 

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

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

106 if ( 

107 abs( 

108 self.knot_vectors[i_dir][i_knot] 

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

110 ) 

111 > _bme.eps_knot_vector 

112 ): 

113 non_empty_knot_spans_indices[i_dir].append(i_knot) 

114 return non_empty_knot_spans_indices 

115 

116 def get_number_of_elements(self) -> int: 

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

118 amount of nonzero knot spans in the knot vector. 

119 

120 Returns: 

121 Number of elements for this patch. 

122 """ 

123 

124 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices() 

125 num_elements_dir = [len(indices) for indices in non_empty_knot_spans_indices] 

126 total_num_elements = _np.prod(num_elements_dir) 

127 return total_num_elements 

128 

129 def _check_material(self) -> None: 

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

131 element.""" 

132 for material_type in type(self).valid_materials: 

133 if isinstance(self.material, material_type): 

134 return 

135 raise TypeError( 

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

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

138 ) 

139 

140 @_abstractmethod 

141 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]: 

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

143 

144 @_abstractmethod 

145 def get_ids_ctrlpts(self, *args) -> list[int]: 

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

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

148 

149 

150class NURBSSurface(NURBSPatch): 

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

152 

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

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

155 

156 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]: 

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

158 

159 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices() 

160 return ( 

161 (u, v) 

162 for v in non_empty_knot_spans_indices[1] 

163 for u in non_empty_knot_spans_indices[0] 

164 ) 

165 

166 def get_ids_ctrlpts(self, knot_span_u: int, knot_span_v: int) -> list[int]: 

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

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

169 

170 p, q = self.polynomial_orders 

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

172 id_u = knot_span_u - p 

173 id_v = knot_span_v - q 

174 

175 return [ 

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

177 for j in range(q + 1) 

178 for i in range(p + 1) 

179 ] 

180 

181 

182class NURBSVolume(NURBSPatch): 

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

184 

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

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

187 

188 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]: 

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

190 

191 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices() 

192 return ( 

193 (u, v, w) 

194 for w in non_empty_knot_spans_indices[2] 

195 for v in non_empty_knot_spans_indices[1] 

196 for u in non_empty_knot_spans_indices[0] 

197 ) 

198 

199 def get_ids_ctrlpts( 

200 self, knot_span_u: int, knot_span_v: int, knot_span_w: int 

201 ) -> list[int]: 

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

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

204 

205 p, q, r = self.polynomial_orders 

206 id_u = knot_span_u - p 

207 id_v = knot_span_v - q 

208 id_w = knot_span_w - r 

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

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

211 

212 return [ 

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

214 for k in range(r + 1) 

215 for j in range(q + 1) 

216 for i in range(p + 1) 

217 ]