Coverage for src/beamme/four_c/beam_potential.py: 86%
35 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-08 11:03 +0000
« 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 file includes functions to ease the creation of input files using beam
23interaction potentials."""
25import numpy as _np
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
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."""
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.
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 """
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]
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 )
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
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_source_target: str | None,
107 two_half_pass: bool,
108 runtime_output_interval_steps: int | None = None,
109 runtime_output_every_iteration: bool,
110 runtime_output_force: bool,
111 runtime_output_moment: bool,
112 runtime_output_uids: bool,
113 runtime_output_per_ele_pair: bool,
114 ) -> dict:
115 """Set the basic header options for beam potential interactions.
117 Args:
118 potential_type:
119 Type of applied potential.
120 evaluation_strategy:
121 Strategy to evaluate interaction potential.
122 cutoff_radius:
123 Neglect all contributions at separation larger than this cutoff
124 radius.
125 regularization_type:
126 Type of regularization to use for force law at separations below
127 specified separation.
128 regularization_separation:
129 Use specified regularization type for separations smaller than
130 this value.
131 integration_segments:
132 Number of integration segments to be used per beam element.
133 gauss_points:
134 Number of Gauss points to be used per integration segment.
135 potential_reduction_length:
136 Potential is smoothly decreased within this length when using the
137 single length specific (SBIP) approach to enable an axial pull off
138 force.
139 automatic_differentiation:
140 Use automatic differentiation via FAD.
141 two_half_pass:
142 Whether to use the two half pass approach.
143 choice_source_target:
144 Rule how to assign the role of source and target to beam elements (if
145 applicable).
147 runtime_output:
148 If the output for beam potential should be written.
149 runtime_output_interval_steps:
150 Interval at which output is written.
151 runtime_output_every_iteration:
152 If output at every Newton iteration should be written.
153 runtime_output_force:
154 If the forces should be written.
155 runtime_output_moment:
156 If the moments should be written.
157 runtime_output_uids:
158 If the unique ids should be written.
159 runtime_output_per_ele_pair:
160 If the forces/moments should be written per element pair.
162 Returns:
163 Header for beam potential interactions.
164 """
166 header = {
167 "beam_potential": {
168 "type": potential_type,
169 "strategy": evaluation_strategy,
170 "potential_law_prefactors": self.pot_law_prefactor,
171 "potential_law_exponents": self.pot_law_exponent,
172 "automatic_differentiation": automatic_differentiation,
173 "cutoff_radius": cutoff_radius,
174 "n_integration_segments": integration_segments,
175 "n_gauss_points": gauss_points,
176 "potential_reduction_length": potential_reduction_length,
177 "two_half_pass": two_half_pass,
178 }
179 }
181 if regularization_type is not None:
182 header["beam_potential"]["regularization"] = {
183 "type": regularization_type,
184 "separation": regularization_separation,
185 }
187 if choice_source_target is not None:
188 header["beam_potential"]["choice_source_target"] = choice_source_target
190 if runtime_output_interval_steps is not None:
191 header["beam_potential"]["runtime_output"] = {
192 "interval_steps": runtime_output_interval_steps,
193 "force": runtime_output_force,
194 "moment": runtime_output_moment,
195 "every_iteration": runtime_output_every_iteration,
196 "write_force_moment_per_elementpair": runtime_output_per_ele_pair,
197 "write_uids": runtime_output_uids,
198 }
200 return header
202 def create_potential_charge_conditions(
203 self, *, geometry_set: _GeometrySet
204 ) -> list[_BoundaryCondition]:
205 """Create potential charge conditions.
207 Args:
208 geometry_set:
209 Add potential charge condition to this set.
211 Returns:
212 List of boundary conditions for potential charge.
213 """
215 bcs = []
217 for i, (line_charge, func) in enumerate(
218 zip(
219 self.pot_law_line_charge_density, self.pot_law_line_charge_density_funcs
220 )
221 ):
222 bc = _BoundaryCondition(
223 geometry_set,
224 {"POTLAW": i + 1, "VAL": line_charge, "FUNCT": func},
225 bc_type="DESIGN LINE BEAM POTENTIAL CHARGE CONDITIONS",
226 )
228 bcs.append(bc)
230 return bcs