Coverage for src/meshpy/four_c/beam_potential.py: 88%
33 statements
« prev ^ index » next coverage.py v7.9.0, created at 2025-06-13 04:26 +0000
« prev ^ index » next coverage.py v7.9.0, created at 2025-06-13 04:26 +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
26from meshpy.core.mesh import Mesh as _Mesh
29class BeamPotential:
30 """Class which provides functions for the usage of beam to beam potential
31 interactions within 4C based on a potential law in form of a power law."""
33 def __init__(
34 self,
35 input_file,
36 mesh,
37 *,
38 pot_law_prefactor=None,
39 pot_law_exponent=None,
40 pot_law_line_charge_density=None,
41 pot_law_line_charge_density_funcs=None,
42 ):
43 """Initialize object to enable beam potential interactions.
45 Args
46 ----
47 input_file:
48 Input file of current problem setup.
49 mesh:
50 Mesh object of current problem setup.
51 pot_law_prefactors: float, int, _np.array, list
52 Prefactors of a potential law in form of a power law. Same number
53 of prefactors and exponents/line charge densities/functions must be
54 provided!
55 pot_law_exponent: float, int, _np.array, list
56 Exponents of a potential law in form of a power law. Same number
57 of exponents and prefactors/line charge densities/functions must be
58 provided!
59 pot_law_line_charge_density: float, int, _np.array, list
60 Line charge densities of a potential law in form of a power law.
61 Same number of line charge densities and prefactors/exponents/functions
62 must be provided!
63 pot_law_line_charge_density_funcs:
64 Functions for line charge densities of a potential law in form of a
65 power law. Same number of functions and prefactors/exponents/line
66 charge densities must be provided!
67 """
69 self.input_file = input_file
70 self.mesh = mesh
72 # if only one potential law prefactor/exponent is present, convert it
73 # into a list for simplified usage
74 if isinstance(pot_law_prefactor, (float, int)):
75 pot_law_prefactor = [pot_law_prefactor]
76 if isinstance(pot_law_exponent, (float, int)):
77 pot_law_exponent = [pot_law_exponent]
78 if isinstance(pot_law_line_charge_density, (float, int)):
79 pot_law_line_charge_density = [pot_law_line_charge_density]
81 # check if same number of prefactors and exponents are provided
82 if (
83 not len(pot_law_prefactor)
84 == len(pot_law_exponent)
85 == len(pot_law_line_charge_density)
86 ):
87 raise ValueError(
88 "Number of potential law prefactors do not match potential law exponents!"
89 )
91 self.pot_law_prefactor = pot_law_prefactor
92 self.pot_law_exponent = pot_law_exponent
93 self.pot_law_line_charge_density = pot_law_line_charge_density
94 self.pot_law_line_charge_density_funcs = pot_law_line_charge_density_funcs
96 def add_header(
97 self,
98 *,
99 potential_type="volume",
100 cutoff_radius=None,
101 evaluation_strategy=None,
102 regularization_type=None,
103 regularization_separation=None,
104 integration_segments=1,
105 gauss_points=10,
106 potential_reduction_length=-1,
107 automatic_differentiation=False,
108 choice_master_slave=None,
109 ):
110 """Set the basic header options for beam potential interactions.
112 Args
113 ----
114 potential_type: string
115 Type of applied potential (volume, surface).
116 cutoff_radius: float
117 Neglect all contributions at separation larger than this cutoff
118 radius.
119 evaluation_strategy: string
120 Strategy to evaluate interaction potential.
121 regularization_type: string
122 Type of regularization to use for force law at separations below
123 specified separation (constant_extrapolation, linear_extrapolation).
124 regularization_separation: float
125 Use specified regularization type for separations smaller than
126 this value.
127 integration_segments: int
128 Number of integration segments to be used per beam element.
129 gauss_points: int
130 Number of Gauss points to be used per integration segment.
131 potential_reduction_length: float
132 Potential is smoothly decreased within this length when using the
133 single length specific (SBIP) approach to enable an axial pull off
134 force.
135 automatic_differentiation: bool
136 Use automatic differentiation via FAD.
137 choice_master_slave: string
138 Rule how to assign the role of master and slave to beam elements (if
139 applicable) (lower_eleGID_is_slave, higher_eleGID_is_slave).
140 """
142 settings = {
143 "POT_LAW_PREFACTOR": " ".join(map(str, self.pot_law_prefactor)),
144 "POT_LAW_EXPONENT": " ".join(map(str, self.pot_law_exponent)),
145 "TYPE": potential_type,
146 "CUTOFF_RADIUS": cutoff_radius,
147 "STRATEGY": evaluation_strategy,
148 "N_INTEGRATION_SEGMENTS": integration_segments,
149 "N_GAUSS_POINTS": gauss_points,
150 "POTENTIAL_REDUCTION_LENGTH": potential_reduction_length,
151 "AUTOMATIC_DIFFERENTIATION": automatic_differentiation,
152 }
154 if regularization_type is not None:
155 settings = settings | {
156 "REGULARIZATION_TYPE": regularization_type,
157 "REGULARIZATION_SEPARATION": regularization_separation,
158 }
160 if choice_master_slave is not None:
161 settings = settings | {"CHOICE_MASTER_SLAVE": choice_master_slave}
163 self.input_file.add(
164 {"BEAM POTENTIAL": settings},
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 ):
178 """Set the basic runtime output options for beam potential
179 interactions.
181 Args
182 ----
183 output_beam_potential: bool
184 If the output for beam potential should be written.
185 interval_steps: int
186 Interval at which output is written.
187 every_iteration: bool
188 If output at every Newton iteration should be written.
189 forces: bool
190 If the forces should be written.
191 moments: bool
192 If the moments should be written.
193 uids: bool
194 If the unique ids should be written.
195 per_ele_pair: bool
196 If the forces/moments should be written per element pair.
197 """
199 self.input_file.add(
200 {
201 "BEAM POTENTIAL/RUNTIME VTK OUTPUT": {
202 "VTK_OUTPUT_BEAM_POTENTIAL": output_beam_potential,
203 "INTERVAL_STEPS": interval_steps,
204 "EVERY_ITERATION": every_iteration,
205 "FORCES": forces,
206 "MOMENTS": moments,
207 "WRITE_UIDS": uids,
208 "WRITE_FORCE_MOMENT_PER_ELEMENTPAIR": per_ele_pair,
209 }
210 }
211 )
213 def add_potential_charge_condition(self, *, geometry_set=None):
214 """Add potential charge condition to geometry.
216 Args
217 ----
218 geometry_set:
219 Add potential charge condition to this set.
220 """
222 for i, (line_charge, func) in enumerate(
223 zip(
224 self.pot_law_line_charge_density, self.pot_law_line_charge_density_funcs
225 )
226 ):
227 if func != "none":
228 self.mesh.add(func)
230 bc = _BoundaryCondition(
231 geometry_set,
232 {"POTLAW": i + 1, "VAL": line_charge, "FUNCT": func},
233 bc_type="DESIGN LINE BEAM POTENTIAL CHARGE CONDITIONS",
234 )
236 self.mesh.add(bc)