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

68 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-08 11:03 +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 typing 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 def __init__(self, knot_vectors, polynomial_orders, material=None, nodes=None): 

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

42 

43 # Knot vectors 

44 self.knot_vectors = knot_vectors 

45 

46 # Polynomial degrees 

47 self.polynomial_orders = polynomial_orders 

48 

49 def get_nurbs_dimension(self) -> int: 

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

51 

52 Returns: 

53 Number of dimensions of the NURBS object. 

54 """ 

55 n_knots = len(self.knot_vectors) 

56 n_polynomial = len(self.polynomial_orders) 

57 if not n_knots == n_polynomial: 

58 raise ValueError( 

59 "The variables n_knots and polynomial_orders should have " 

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

61 ) 

62 return n_knots 

63 

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

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

66 of the patch. 

67 

68 Returns: 

69 List of control points per direction. 

70 """ 

71 n_dim = len(self.knot_vectors) 

72 n_cp_per_dim = [] 

73 for i_dim in range(n_dim): 

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

75 polynomial_order = self.polynomial_orders[i_dim] 

76 n_cp_per_dim.append(knot_vector_size - polynomial_order - 1) 

77 return n_cp_per_dim 

78 

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

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

81 direction. 

82 

83 Returns: 

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

85 each parameter direction. 

86 """ 

87 

88 non_empty_knot_spans_indices: list[list[int]] = [ 

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

90 ] 

91 

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

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

94 if ( 

95 abs( 

96 self.knot_vectors[i_dir][i_knot] 

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

98 ) 

99 > _bme.eps_knot_vector 

100 ): 

101 non_empty_knot_spans_indices[i_dir].append(i_knot) 

102 return non_empty_knot_spans_indices 

103 

104 def get_number_of_elements(self) -> int: 

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

106 amount of nonzero knot spans in the knot vector. 

107 

108 Returns: 

109 Number of elements for this patch. 

110 """ 

111 

112 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices() 

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

114 total_num_elements = _np.prod(num_elements_dir) 

115 return total_num_elements 

116 

117 @_abstractmethod 

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

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

120 

121 @_abstractmethod 

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

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

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

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 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 

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

146 

147 p, q = self.polynomial_orders 

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

149 id_u = knot_span_u - p 

150 id_v = knot_span_v - q 

151 

152 return [ 

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

154 for j in range(q + 1) 

155 for i in range(p + 1) 

156 ] 

157 

158 

159class NURBSVolume(NURBSPatch): 

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

161 

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

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

164 

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

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

167 

168 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices() 

169 return ( 

170 (u, v, w) 

171 for w in non_empty_knot_spans_indices[2] 

172 for v in non_empty_knot_spans_indices[1] 

173 for u in non_empty_knot_spans_indices[0] 

174 ) 

175 

176 def get_ids_ctrlpts( 

177 self, knot_span_u: int, knot_span_v: int, knot_span_w: int 

178 ) -> list[int]: 

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

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

181 

182 p, q, r = self.polynomial_orders 

183 id_u = knot_span_u - p 

184 id_v = knot_span_v - q 

185 id_w = knot_span_w - r 

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

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

188 

189 return [ 

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

191 for k in range(r + 1) 

192 for j in range(q + 1) 

193 for i in range(p + 1) 

194 ]