Coverage for src/meshpy/four_c/beam_potential.py: 87%
31 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2018-2025 MeshPy 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."""
25from meshpy.core.boundary_condition import BoundaryCondition as _BoundaryCondition
28class BeamPotential:
29 """Class which provides functions for the usage of beam to beam potential
30 interactions within 4C based on a potential law in form of a power law."""
32 def __init__(
33 self,
34 input_file,
35 *,
36 pot_law_prefactor=None,
37 pot_law_exponent=None,
38 pot_law_line_charge_density=None,
39 pot_law_line_charge_density_funcs=None,
40 ):
41 """Initialize object to enable beam potential interactions.
43 Args
44 ----
45 input_file:
46 Input file of current problem setup.
47 pot_law_prefactors: float, int, _np.array, list
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: float, int, _np.array, list
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: float, int, _np.array, list
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 self.input_file = input_file
67 # if only one potential law prefactor/exponent is present, convert it
68 # into a list for simplified usage
69 if isinstance(pot_law_prefactor, (float, int)):
70 pot_law_prefactor = [pot_law_prefactor]
71 if isinstance(pot_law_exponent, (float, int)):
72 pot_law_exponent = [pot_law_exponent]
73 if isinstance(pot_law_line_charge_density, (float, int)):
74 pot_law_line_charge_density = [pot_law_line_charge_density]
76 # check if same number of prefactors and exponents are provided
77 if (
78 not len(pot_law_prefactor)
79 == len(pot_law_exponent)
80 == len(pot_law_line_charge_density)
81 ):
82 raise ValueError(
83 "Number of potential law prefactors do not match potential law exponents!"
84 )
86 self.pot_law_prefactor = pot_law_prefactor
87 self.pot_law_exponent = pot_law_exponent
88 self.pot_law_line_charge_density = pot_law_line_charge_density
89 self.pot_law_line_charge_density_funcs = pot_law_line_charge_density_funcs
91 def add_header(
92 self,
93 *,
94 potential_type="volume",
95 cutoff_radius=None,
96 evaluation_strategy=None,
97 regularization_type=None,
98 regularization_separation=None,
99 integration_segments=1,
100 gauss_points=10,
101 potential_reduction_length=-1,
102 automatic_differentiation=False,
103 choice_master_slave=None,
104 option_overwrite=False,
105 ):
106 """Set the basic header options for beam potential interactions.
108 Args
109 ----
110 potential_type: string
111 Type of applied potential (volume, surface).
112 cutoff_radius: float
113 Neglect all contributions at separation larger than this cutoff
114 radius.
115 evaluation_strategy: string
116 Strategy to evaluate interaction potential.
117 regularization_type: string
118 Type of regularization to use for force law at separations below
119 specified separation (constant_extrapolation, linear_extrapolation).
120 regularization_separation: float
121 Use specified regularization type for separations smaller than
122 this value.
123 integration_segments: int
124 Number of integration segments to be used per beam element.
125 gauss_points: int
126 Number of Gauss points to be used per integration segment.
127 potential_reduction_length: float
128 Potential is smoothly decreased within this length when using the
129 single length specific (SBIP) approach to enable an axial pull off
130 force.
131 automatic_differentiation: bool
132 Use automatic differentiation via FAD.
133 choice_master_slave: string
134 Rule how to assign the role of master and slave to beam elements (if
135 applicable) (lower_eleGID_is_slave, higher_eleGID_is_slave).
136 option_overwrite: bool
137 If existing options should be overwritten. If this is false and an
138 option is already defined, and error will be thrown.
139 """
141 settings = {
142 "POT_LAW_PREFACTOR": " ".join(map(str, self.pot_law_prefactor)),
143 "POT_LAW_EXPONENT": " ".join(map(str, self.pot_law_exponent)),
144 "TYPE": potential_type,
145 "CUTOFF_RADIUS": cutoff_radius,
146 "STRATEGY": evaluation_strategy,
147 "N_INTEGRATION_SEGMENTS": integration_segments,
148 "N_GAUSS_POINTS": gauss_points,
149 "POTENTIAL_REDUCTION_LENGTH": potential_reduction_length,
150 "AUTOMATIC_DIFFERENTIATION": automatic_differentiation,
151 }
153 if regularization_type is not None:
154 settings = settings | {
155 "REGULARIZATION_TYPE": regularization_type,
156 "REGULARIZATION_SEPARATION": regularization_separation,
157 }
159 if choice_master_slave is not None:
160 settings = settings | {"CHOICE_MASTER_SLAVE": choice_master_slave}
162 self.input_file.add(
163 {"BEAM POTENTIAL": settings},
164 option_overwrite=option_overwrite,
165 )
167 def add_runtime_output(
168 self,
169 *,
170 output_beam_potential=True,
171 interval_steps=1,
172 every_iteration=False,
173 forces=True,
174 moments=True,
175 uids=True,
176 per_ele_pair=True,
177 option_overwrite=False,
178 ):
179 """Set the basic runtime output options for beam potential
180 interactions.
182 Args
183 ----
184 output_beam_potential: bool
185 If the output for beam potential should be written.
186 interval_steps: int
187 Interval at which output is written.
188 every_iteration: bool
189 If output at every Newton iteration should be written.
190 forces: bool
191 If the forces should be written.
192 moments: bool
193 If the moments should be written.
194 uids: bool
195 If the unique ids should be written.
196 per_ele_pair: bool
197 If the forces/moments should be written per element pair.
198 option_overwrite: bool
199 If existing options should be overwritten. If this is false and an
200 option is already defined, and error will be thrown.
201 """
203 self.input_file.add(
204 {
205 "BEAM POTENTIAL/RUNTIME VTK OUTPUT": {
206 "VTK_OUTPUT_BEAM_POTENTIAL": output_beam_potential,
207 "INTERVAL_STEPS": interval_steps,
208 "EVERY_ITERATION": every_iteration,
209 "FORCES": forces,
210 "MOMENTS": moments,
211 "WRITE_UIDS": uids,
212 "WRITE_FORCE_MOMENT_PER_ELEMENTPAIR": per_ele_pair,
213 }
214 },
215 option_overwrite=option_overwrite,
216 )
218 def add_potential_charge_condition(self, *, geometry_set=None):
219 """Add potential charge condition to geometry.
221 Args
222 ----
223 geometry_set:
224 Add potential charge condition to this set.
225 """
227 for i, (line_charge, func) in enumerate(
228 zip(
229 self.pot_law_line_charge_density, self.pot_law_line_charge_density_funcs
230 )
231 ):
232 if func != "none":
233 self.input_file.add(func)
235 bc = _BoundaryCondition(
236 geometry_set,
237 {"POTLAW": i + 1, "VAL": line_charge, "FUNCT": func},
238 bc_type="DESIGN LINE BEAM POTENTIAL CHARGE CONDITIONS",
239 )
241 self.input_file.add(bc)