Coverage for src/beamme/four_c/beam_potential.py: 87%

39 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 file includes functions to ease the creation of input files using beam 

23interaction potentials.""" 

24 

25import numpy as _np 

26 

27from beamme.core.boundary_condition import BoundaryCondition as _BoundaryCondition 

28from beamme.core.function import Function as _Function 

29from beamme.core.geometry_set import GeometrySet as _GeometrySet 

30 

31 

32class BeamPotential: 

33 """Class which provides functions for the usage of beam to beam potential 

34 interactions within 4C based on a potential law in form of a power law.""" 

35 

36 def __init__( 

37 self, 

38 *, 

39 pot_law_prefactor: float | int | list | _np.ndarray, 

40 pot_law_exponent: float | int | list | _np.ndarray, 

41 pot_law_line_charge_density: float | int | list | _np.ndarray, 

42 pot_law_line_charge_density_funcs: _Function | list | _np.ndarray | None, 

43 ): 

44 """Initialize object to enable beam potential interactions. 

45 

46 Args: 

47 pot_law_prefactors: 

48 Prefactors of a potential law in form of a power law. Same number 

49 of prefactors and exponents/line charge densities/functions must be 

50 provided! 

51 pot_law_exponent: 

52 Exponents of a potential law in form of a power law. Same number 

53 of exponents and prefactors/line charge densities/functions must be 

54 provided! 

55 pot_law_line_charge_density: 

56 Line charge densities of a potential law in form of a power law. 

57 Same number of line charge densities and prefactors/exponents/functions 

58 must be provided! 

59 pot_law_line_charge_density_funcs: 

60 Functions for line charge densities of a potential law in form of a 

61 power law. Same number of functions and prefactors/exponents/line 

62 charge densities must be provided! 

63 """ 

64 # if only one potential law prefactor/exponent is present, convert it 

65 # into a list for simplified usage 

66 if isinstance(pot_law_prefactor, (float, int)): 

67 pot_law_prefactor = [pot_law_prefactor] 

68 if isinstance(pot_law_exponent, (float, int)): 

69 pot_law_exponent = [pot_law_exponent] 

70 if isinstance(pot_law_line_charge_density, (float, int)): 

71 pot_law_line_charge_density = [pot_law_line_charge_density] 

72 if ( 

73 isinstance(pot_law_line_charge_density_funcs, _Function) 

74 or pot_law_line_charge_density_funcs is None 

75 ): 

76 pot_law_line_charge_density_funcs = [pot_law_line_charge_density_funcs] 

77 

78 # check if same number of prefactors and exponents are provided 

79 if ( 

80 not len(pot_law_prefactor) 

81 == len(pot_law_exponent) 

82 == len(pot_law_line_charge_density) 

83 ): 

84 raise ValueError( 

85 "Number of potential law prefactors do not match potential law exponents or potential line charge density!" 

86 ) 

87 

88 self.pot_law_prefactor = pot_law_prefactor 

89 self.pot_law_exponent = pot_law_exponent 

90 self.pot_law_line_charge_density = pot_law_line_charge_density 

91 self.pot_law_line_charge_density_funcs = pot_law_line_charge_density_funcs 

92 

93 def create_header( 

94 self, 

95 *, 

96 potential_type: str, 

97 evaluation_strategy: str, 

98 cutoff_radius: float, 

99 regularization_type: str | None = None, 

100 regularization_separation: float, 

101 integration_segments: int, 

102 gauss_points: int, 

103 potential_reduction_length: float | int | None = None, 

104 potential_reduction_function: str | None = None, 

105 potential_reduction_endpoint_moment_compensation: bool = False, 

106 potential_reduction_adaptive_n_gauss_points: int | None = None, 

107 automatic_differentiation: bool, 

108 choice_source_target: str | None, 

109 two_half_pass: bool, 

110 runtime_output_interval_steps: int | None = None, 

111 runtime_output_every_iteration: bool, 

112 runtime_output_force: bool, 

113 runtime_output_moment: bool, 

114 runtime_output_uids: bool, 

115 runtime_output_per_ele_pair: bool, 

116 ) -> dict: 

117 """Set the basic header options for beam potential interactions. 

118 

119 Args: 

120 potential_type: 

121 Type of applied potential. 

122 evaluation_strategy: 

123 Strategy to evaluate interaction potential. 

124 cutoff_radius: 

125 Neglect all contributions at separation larger than this cutoff 

126 radius. 

127 regularization_type: 

128 Type of regularization to use for force law at separations below 

129 specified separation. 

130 regularization_separation: 

131 Use specified regularization type for separations smaller than 

132 this value. 

133 integration_segments: 

134 Number of integration segments to be used per beam element. 

135 gauss_points: 

136 Number of Gauss points to be used per integration segment. 

137 potential_reduction_length: 

138 Potential is smoothly decreased within this length when using the 

139 single length specific (SBIP) approach to enable an axial pull off 

140 force. 

141 potential_reduction_function: 

142 Function to be used for potential reduction formulation. 

143 potential_reduction_endpoint_moment_compensation: 

144 If the potential reduction should be compensated at the endpoints of 

145 the beam elements to avoid a distributed moment contribution. 

146 potential_reduction_adaptive_n_gauss_points: 

147 Number of Gauss points for element pairs where potential reduction is 

148 effective. 

149 automatic_differentiation: 

150 Use automatic differentiation via FAD. 

151 two_half_pass: 

152 Whether to use the two half pass approach. 

153 choice_source_target: 

154 Rule how to assign the role of source and target to beam elements (if 

155 applicable). 

156 

157 runtime_output: 

158 If the output for beam potential should be written. 

159 runtime_output_interval_steps: 

160 Interval at which output is written. 

161 runtime_output_every_iteration: 

162 If output at every Newton iteration should be written. 

163 runtime_output_force: 

164 If the forces should be written. 

165 runtime_output_moment: 

166 If the moments should be written. 

167 runtime_output_uids: 

168 If the unique ids should be written. 

169 runtime_output_per_ele_pair: 

170 If the forces/moments should be written per element pair. 

171 

172 Returns: 

173 Header for beam potential interactions. 

174 """ 

175 header = { 

176 "beam_potential": { 

177 "type": potential_type, 

178 "strategy": evaluation_strategy, 

179 "potential_law_prefactors": self.pot_law_prefactor, 

180 "potential_law_exponents": self.pot_law_exponent, 

181 "automatic_differentiation": automatic_differentiation, 

182 "cutoff_radius": cutoff_radius, 

183 "n_integration_segments": integration_segments, 

184 "n_gauss_points": gauss_points, 

185 "potential_reduction_length": potential_reduction_length, 

186 "two_half_pass": two_half_pass, 

187 "potential_reduction_endpoint_moment_compensation": potential_reduction_endpoint_moment_compensation, 

188 } 

189 } 

190 

191 if regularization_type is not None: 

192 header["beam_potential"]["regularization"] = { 

193 "type": regularization_type, 

194 "separation": regularization_separation, 

195 } 

196 

197 if potential_reduction_function is not None: 

198 header["beam_potential"]["potential_reduction_function"] = ( 

199 potential_reduction_function 

200 ) 

201 

202 if potential_reduction_adaptive_n_gauss_points is not None: 

203 header["beam_potential"]["potential_reduction_adaptive_n_gauss_points"] = ( 

204 potential_reduction_adaptive_n_gauss_points 

205 ) 

206 

207 if choice_source_target is not None: 

208 header["beam_potential"]["choice_source_target"] = choice_source_target 

209 

210 if runtime_output_interval_steps is not None: 

211 header["beam_potential"]["runtime_output"] = { 

212 "interval_steps": runtime_output_interval_steps, 

213 "force": runtime_output_force, 

214 "moment": runtime_output_moment, 

215 "every_iteration": runtime_output_every_iteration, 

216 "write_force_moment_per_elementpair": runtime_output_per_ele_pair, 

217 "write_uids": runtime_output_uids, 

218 } 

219 

220 return header 

221 

222 def create_potential_charge_conditions( 

223 self, *, geometry_set: _GeometrySet 

224 ) -> list[_BoundaryCondition]: 

225 """Create potential charge conditions. 

226 

227 Args: 

228 geometry_set: 

229 Add potential charge condition to this set. 

230 

231 Returns: 

232 List of boundary conditions for potential charge. 

233 """ 

234 bcs = [] 

235 

236 for i, (line_charge, func) in enumerate( 

237 zip( 

238 self.pot_law_line_charge_density, self.pot_law_line_charge_density_funcs 

239 ) 

240 ): 

241 bc = _BoundaryCondition( 

242 geometry_set, 

243 {"POTLAW": i + 1, "VAL": line_charge, "FUNCT": func}, 

244 bc_type="DESIGN LINE BEAM POTENTIAL CHARGE CONDITIONS", 

245 ) 

246 

247 bcs.append(bc) 

248 

249 return bcs