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

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

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

66 # into a list for simplified usage 

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

68 pot_law_prefactor = [pot_law_prefactor] 

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

70 pot_law_exponent = [pot_law_exponent] 

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

72 pot_law_line_charge_density = [pot_law_line_charge_density] 

73 if ( 

74 isinstance(pot_law_line_charge_density_funcs, _Function) 

75 or pot_law_line_charge_density_funcs is None 

76 ): 

77 pot_law_line_charge_density_funcs = [pot_law_line_charge_density_funcs] 

78 

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

80 if ( 

81 not len(pot_law_prefactor) 

82 == len(pot_law_exponent) 

83 == len(pot_law_line_charge_density) 

84 ): 

85 raise ValueError( 

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

87 ) 

88 

89 self.pot_law_prefactor = pot_law_prefactor 

90 self.pot_law_exponent = pot_law_exponent 

91 self.pot_law_line_charge_density = pot_law_line_charge_density 

92 self.pot_law_line_charge_density_funcs = pot_law_line_charge_density_funcs 

93 

94 def create_header( 

95 self, 

96 *, 

97 potential_type: str, 

98 evaluation_strategy: str, 

99 cutoff_radius: float, 

100 regularization_type: str | None = None, 

101 regularization_separation: float, 

102 integration_segments: int, 

103 gauss_points: int, 

104 potential_reduction_length: float | int | None = None, 

105 automatic_differentiation: bool, 

106 choice_master_slave: str | None, 

107 runtime_output_interval_steps: int | None = None, 

108 runtime_output_every_iteration: bool, 

109 runtime_output_force: bool, 

110 runtime_output_moment: bool, 

111 runtime_output_uids: bool, 

112 runtime_output_per_ele_pair: bool, 

113 ) -> dict: 

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

115 

116 Args: 

117 potential_type: 

118 Type of applied potential. 

119 evaluation_strategy: 

120 Strategy to evaluate interaction potential. 

121 cutoff_radius: 

122 Neglect all contributions at separation larger than this cutoff 

123 radius. 

124 regularization_type: 

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

126 specified separation. 

127 regularization_separation: 

128 Use specified regularization type for separations smaller than 

129 this value. 

130 integration_segments: 

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

132 gauss_points: 

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

134 potential_reduction_length: 

135 Potential is smoothly decreased within this length when using the 

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

137 force. 

138 automatic_differentiation: 

139 Use automatic differentiation via FAD. 

140 choice_master_slave: 

141 Rule how to assign the role of master and slave to beam elements (if 

142 applicable). 

143 

144 runtime_output: 

145 If the output for beam potential should be written. 

146 runtime_output_interval_steps: 

147 Interval at which output is written. 

148 runtime_output_every_iteration: 

149 If output at every Newton iteration should be written. 

150 runtime_output_force: 

151 If the forces should be written. 

152 runtime_output_moment: 

153 If the moments should be written. 

154 runtime_output_uids: 

155 If the unique ids should be written. 

156 runtime_output_per_ele_pair: 

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

158 

159 Returns: 

160 Header for beam potential interactions. 

161 """ 

162 

163 header = { 

164 "beam_potential": { 

165 "type": potential_type, 

166 "strategy": evaluation_strategy, 

167 "potential_law_prefactors": self.pot_law_prefactor, 

168 "potential_law_exponents": self.pot_law_exponent, 

169 "automatic_differentiation": automatic_differentiation, 

170 "cutoff_radius": cutoff_radius, 

171 "n_integration_segments": integration_segments, 

172 "n_gauss_points": gauss_points, 

173 "potential_reduction_length": potential_reduction_length, 

174 } 

175 } 

176 

177 if regularization_type is not None: 

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

179 "type": regularization_type, 

180 "separation": regularization_separation, 

181 } 

182 

183 if choice_master_slave is not None: 

184 header["beam_potential"]["choice_master_slave"] = choice_master_slave 

185 

186 if runtime_output_interval_steps is not None: 

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

188 "interval_steps": runtime_output_interval_steps, 

189 "force": runtime_output_force, 

190 "moment": runtime_output_moment, 

191 "every_iteration": runtime_output_every_iteration, 

192 "write_force_moment_per_elementpair": runtime_output_per_ele_pair, 

193 "write_uids": runtime_output_uids, 

194 } 

195 

196 return header 

197 

198 def create_potential_charge_conditions( 

199 self, *, geometry_set: _GeometrySet 

200 ) -> list[_BoundaryCondition]: 

201 """Create potential charge conditions. 

202 

203 Args: 

204 geometry_set: 

205 Add potential charge condition to this set. 

206 

207 Returns: 

208 List of boundary conditions for potential charge. 

209 """ 

210 

211 bcs = [] 

212 

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

214 zip( 

215 self.pot_law_line_charge_density, self.pot_law_line_charge_density_funcs 

216 ) 

217 ): 

218 bc = _BoundaryCondition( 

219 geometry_set, 

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

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

222 ) 

223 

224 bcs.append(bc) 

225 

226 return bcs