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

69 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-28 15:20 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2018-2026 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 collections.abc import Iterator as _Iterator 

26 

27import numpy as _np 

28import pyvista as _pv 

29 

30from beamme.core.conf import bme as _bme 

31from beamme.core.element import Element as _Element 

32 

33 

34class NURBSPatch(_Element): 

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

36 

37 # Generic VTK cell type for NURBS elements - this will not show the correct topology in vtk. 

38 vtk_cell_type = _pv.CellType.POLYGON 

39 

40 # Type of this element. 

41 element_type = _bme.element_type.nurbs 

42 

43 def __init__(self, knot_vectors, polynomial_orders, material=None, nodes=None): 

44 super().__init__(nodes=nodes, material=material) 

45 

46 # Knot vectors 

47 self.knot_vectors = knot_vectors 

48 

49 # Polynomial degrees 

50 self.polynomial_orders = polynomial_orders 

51 

52 def get_nurbs_dimension(self) -> int: 

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

54 

55 Returns: 

56 Number of dimensions of the NURBS object. 

57 """ 

58 n_knots = len(self.knot_vectors) 

59 n_polynomial = len(self.polynomial_orders) 

60 if not n_knots == n_polynomial: 

61 raise ValueError( 

62 "The variables n_knots and polynomial_orders should have " 

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

64 ) 

65 return n_knots 

66 

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

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

69 patch. 

70 

71 Returns: 

72 List of control points per direction. 

73 """ 

74 n_dim = len(self.knot_vectors) 

75 n_cp_per_dim = [] 

76 for i_dim in range(n_dim): 

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

78 polynomial_order = self.polynomial_orders[i_dim] 

79 n_cp_per_dim.append(knot_vector_size - polynomial_order - 1) 

80 return n_cp_per_dim 

81 

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

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

84 direction. 

85 

86 Returns: 

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

88 each parameter direction. 

89 """ 

90 non_empty_knot_spans_indices: list[list[int]] = [ 

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

92 ] 

93 

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

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

96 if ( 

97 abs( 

98 self.knot_vectors[i_dir][i_knot] 

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

100 ) 

101 > _bme.eps_knot_vector 

102 ): 

103 non_empty_knot_spans_indices[i_dir].append(i_knot) 

104 return non_empty_knot_spans_indices 

105 

106 def get_number_of_elements(self) -> int: 

107 """Determine the number of elements in this patch by checking the amount of 

108 nonzero knot spans in the knot vector. 

109 

110 Returns: 

111 Number of elements for this patch. 

112 """ 

113 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices() 

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

115 total_num_elements = _np.prod(num_elements_dir) 

116 return total_num_elements 

117 

118 @_abstractmethod 

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

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

121 

122 @_abstractmethod 

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

124 """Compute the global indices of the control points that influence the element 

125 defined by the given knot span.""" 

126 

127 

128class NURBSSurface(NURBSPatch): 

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

130 

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

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

133 

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

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

136 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices() 

137 return ( 

138 (u, v) 

139 for v in non_empty_knot_spans_indices[1] 

140 for u in non_empty_knot_spans_indices[0] 

141 ) 

142 

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

144 """Compute the global indices of the control points that influence the element 

145 defined by the given knot span.""" 

146 p, q = self.polynomial_orders 

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

148 id_u = knot_span_u - p 

149 id_v = knot_span_v - q 

150 

151 return [ 

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

153 for j in range(q + 1) 

154 for i in range(p + 1) 

155 ] 

156 

157 

158class NURBSVolume(NURBSPatch): 

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

160 

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

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

163 

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

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

166 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices() 

167 return ( 

168 (u, v, w) 

169 for w in non_empty_knot_spans_indices[2] 

170 for v in non_empty_knot_spans_indices[1] 

171 for u in non_empty_knot_spans_indices[0] 

172 ) 

173 

174 def get_ids_ctrlpts( 

175 self, knot_span_u: int, knot_span_v: int, knot_span_w: int 

176 ) -> list[int]: 

177 """Compute the global indices of the control points that influence the element 

178 defined by the given knot span.""" 

179 p, q, r = self.polynomial_orders 

180 id_u = knot_span_u - p 

181 id_v = knot_span_v - q 

182 id_w = knot_span_w - r 

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

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

185 

186 return [ 

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

188 for k in range(r + 1) 

189 for j in range(q + 1) 

190 for i in range(p + 1) 

191 ]