Coverage for src/beamme/mesh_creation_functions/beam_splinepy.py: 94%
34 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-30 18:48 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-30 18:48 +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"""Create a beam filament from a curve represented with splinepy."""
24import numpy as _np
26from beamme.core.conf import mpy as _mpy
27from beamme.mesh_creation_functions.beam_parametric_curve import (
28 create_beam_mesh_parametric_curve as _create_beam_mesh_parametric_curve,
29)
32def get_curve_function_and_jacobian_for_integration(curve, tol: float | None = None):
33 """Return function objects for evaluating the curve and the derivative.
34 These functions are used in the curve integration. It can happen that the
35 integration algorithm has to evaluate the curve outside of the defined
36 domain. This usually leads to errors in common spline/NURBS packages.
37 Therefore, we check for this evaluation outside of the parameter domain
38 here and perform a linear extrapolation.
40 Args
41 ----
42 curve: splinepy object
43 Curve that is used to describe the beam centerline.
44 tol: float
45 Tolerance for checking if point is close to the start or end of the
46 interval. If None is given, use the default tolerance from mpy.
48 Return
49 ----
50 (function, jacobian, curve_start, curve_end):
51 function:
52 Function for evaluating a position on the curve
53 jacobian:
54 Function for evaluating the tangent along the curve
55 curve_start:
56 Parameter coordinate for the start for the curve
57 curve_end:
58 Parameter coordinate for the end for the curve
59 """
61 if tol is None:
62 tol = _mpy.eps_pos
64 curve_start = curve.parametric_bounds[0][0]
65 curve_end = curve.parametric_bounds[1][0]
67 def eval_r(t):
68 """Evaluate the position along the curve."""
69 return curve.evaluate([[t]])[0]
71 def eval_rp(t):
72 """Evaluate the derivative along the curve."""
73 return curve.derivative([[t]], orders=[1])[0]
75 def function(t):
76 """Convert the curve to a function that can be used for beam
77 generation."""
79 if curve_start <= t <= curve_end:
80 return eval_r(t)
81 elif t < curve_start and _np.abs(t - curve_start) < tol:
82 diff = t - curve_start
83 return eval_r(curve_start) + diff * eval_rp(curve_start)
84 elif t > curve_end and _np.abs(t - curve_end) < tol:
85 diff = t - curve_end
86 return eval_r(curve_end) + diff * eval_rp(curve_end)
87 raise ValueError(
88 "Can not evaluate the curve function outside of the interval (plus tolerances).\n"
89 f"Abs diff start: {_np.abs(curve_start - t)}\nAbs diff end: {_np.abs(t - curve_end)}"
90 )
92 def jacobian(t):
93 """Convert the curve to a Jacobian function that can be used for
94 integration along the curve.
96 There is no tolerance here, since the integration algorithms
97 sometimes evaluates the derivative far outside the interval.
98 """
100 if curve_start <= t <= curve_end:
101 return eval_rp(t)
102 elif t < curve_start:
103 return eval_rp(curve_start)
104 elif curve_end < t:
105 return eval_rp(curve_end)
106 raise ValueError("Should not happen")
108 return function, jacobian, curve_start, curve_end
111def create_beam_mesh_from_splinepy(
112 mesh, beam_class, material, curve, *, tol=None, **kwargs
113):
114 """Generate a beam from a splinepy curve.
116 Args
117 ----
118 mesh: Mesh
119 Mesh that the curve will be added to.
120 beam_class: Beam
121 Class of beam that will be used for this line.
122 material: Material
123 Material for this line.
124 curve: splinepy object
125 Curve that is used to describe the beam centerline.
126 tol: float
127 Tolerance for checking if point is close to the start or end of the
128 interval. If None is given, use the default tolerance from mpy.
130 **kwargs (for all of them look into create_beam_mesh_function)
131 ----
132 n_el: int
133 Number of equally spaced beam elements along the line. Defaults to 1.
134 Mutually exclusive with l_el.
135 l_el: float
136 Desired length of beam elements. Mutually exclusive with n_el.
137 Be aware, that this length might not be achieved, if the elements are
138 warped after they are created.
140 Return:
141 Return value from create_beam_mesh_function
142 """
144 (
145 function,
146 jacobian,
147 curve_start,
148 curve_end,
149 ) = get_curve_function_and_jacobian_for_integration(curve, tol=tol)
151 # Create the beams
152 return _create_beam_mesh_parametric_curve(
153 mesh,
154 beam_class,
155 material,
156 function,
157 [curve_start, curve_end],
158 function_derivative=jacobian,
159 **kwargs,
160 )