Coverage for src/meshpy/mesh_creation_functions/beam_nurbs.py: 94%
35 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"""Create a beam filament from a NURBS curve represented with splinepy."""
24import numpy as _np
26from meshpy.core.conf import mpy as _mpy
27from meshpy.mesh_creation_functions.beam_curve import (
28 create_beam_mesh_curve as _create_beam_mesh_curve,
29)
32def get_nurbs_curve_function_and_jacobian_for_integration(curve, tol=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 NURBS packages. Therefore,
37 we check for this evaluation outside of the parameter domain here and
38 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 NURBS curve
57 curve_end:
58 Parameter coordinate for the end for the NURBS curve
59 """
61 if tol is None:
62 tol = _mpy.eps_pos
64 knot_vector = curve.knot_vectors[0]
65 curve_start = _np.min(knot_vector)
66 curve_end = _np.max(knot_vector)
68 def eval_r(t):
69 """Evaluate the position along the curve."""
70 return curve.evaluate([[t]])[0]
72 def eval_rp(t):
73 """Evaluate the derivative along the curve."""
74 return curve.derivative([[t]], orders=[1])[0]
76 def function(t):
77 """Convert the curve to a function that can be used for beam
78 generation."""
80 if curve_start <= t <= curve_end:
81 return eval_r(t)
82 elif t < curve_start and _np.abs(t - curve_start) < tol:
83 diff = t - curve_start
84 return eval_r(curve_start) + diff * eval_rp(curve_start)
85 elif t > curve_end and _np.abs(t - curve_end) < tol:
86 diff = t - curve_end
87 return eval_r(curve_end) + diff * eval_rp(curve_end)
88 raise ValueError(
89 "Can not evaluate the curve function outside of the interval (plus tolerances).\n"
90 f"Abs diff start: {_np.abs(curve_start - t)}\nAbs diff end: {_np.abs(t - curve_end)}"
91 )
93 def jacobian(t):
94 """Convert the spline to a Jacobian function that can be used for curve
95 generation.
97 There is no tolerance here, since the integration algorithms
98 sometimes evaluate the derivative far outside the interval.
99 """
101 if curve_start <= t <= curve_end:
102 return eval_rp(t)
103 elif t < curve_start:
104 return eval_rp(curve_start)
105 elif curve_end < t:
106 return eval_rp(curve_end)
107 raise ValueError("Should not happen")
109 return function, jacobian, curve_start, curve_end
112def create_beam_mesh_from_nurbs(
113 mesh, beam_class, material, curve, *, tol=None, **kwargs
114):
115 """Generate a beam from a NURBS curve.
117 Args
118 ----
119 mesh: Mesh
120 Mesh that the curve will be added to.
121 beam_class: Beam
122 Class of beam that will be used for this line.
123 material: Material
124 Material for this line.
125 curve: splinepy object
126 Curve that is used to describe the beam centerline.
127 tol: float
128 Tolerance for checking if point is close to the start or end of the
129 interval. If None is given, use the default tolerance from mpy.
131 **kwargs (for all of them look into create_beam_mesh_function)
132 ----
133 n_el: int
134 Number of equally spaced beam elements along the line. Defaults to 1.
135 Mutually exclusive with l_el.
136 l_el: float
137 Desired length of beam elements. Mutually exclusive with n_el.
138 Be aware, that this length might not be achieved, if the elements are
139 warped after they are created.
141 Return:
142 Return value from create_beam_mesh_function
143 """
145 (
146 function,
147 jacobian,
148 curve_start,
149 curve_end,
150 ) = get_nurbs_curve_function_and_jacobian_for_integration(curve, tol=tol)
152 # Create the beams
153 return _create_beam_mesh_curve(
154 mesh,
155 beam_class,
156 material,
157 function,
158 [curve_start, curve_end],
159 function_derivative=jacobian,
160 **kwargs,
161 )