From 1d70fbfb01b21fc7f1655ff9913b267444b1d92b Mon Sep 17 00:00:00 2001 From: David Rundus <114828622+rundvd@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:11:18 -0600 Subject: [PATCH] BUG: fix fin_flutter_analysis for flights without wind Without wind the Mach number is exactly 0 until liftoff, so the safety factor divided by zero and the spline fit raised a ValueError. Leave those points out. Just after liftoff the safety factor is very large and a spline through it oscillates below zero, so interpolate it with pchip. The values at the samples do not change. --- rocketpy/utilities.py | 21 +++++++++---- tests/unit/test_utilities.py | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/rocketpy/utilities.py b/rocketpy/utilities.py index 97ebe3f45..96c832b68 100644 --- a/rocketpy/utilities.py +++ b/rocketpy/utilities.py @@ -336,7 +336,8 @@ def fin_flutter_analysis( considering the variation of the speed of sound with altitude. safety_factor : rocketpy.Function The Safety Factor for the fin flutter as a function of time, defined as - the flutter Mach Number divided by the freestream Mach Number. + the flutter Mach Number divided by the freestream Mach Number. The times + at which the freestream Mach Number is zero are left out. """ found_fin = False surface_area = None @@ -415,10 +416,20 @@ def _flutter_safety_factor(flight, flutter_mach): rocketpy.Function The safety factor for the fin flutter analysis. """ - safety_factor = flutter_mach / flight.mach_number - safety_factor.set_title("Fin Flutter Safety Factor") - safety_factor.set_outputs("Safety Factor") - return safety_factor + time, mach = flight.mach_number[:, 0], flight.mach_number[:, 1] + # Without wind, Mach is zero until liftoff and the ratio is undefined there + defined = mach > 0 + safety_factor = flutter_mach[defined, 1] / mach[defined] + # The ratio is very large just after liftoff, where a spline oscillates and + # can become negative. pchip stays between the neighboring samples. + return Function( + np.column_stack((time[defined], safety_factor)), + inputs="Time (s)", + outputs="Safety Factor", + interpolation="pchip", + extrapolation="constant", + title="Fin Flutter Safety Factor", + ) def _flutter_plots(flight, flutter_mach, safety_factor, *, filename=None): diff --git a/tests/unit/test_utilities.py b/tests/unit/test_utilities.py index a6ed1f3eb..5c51fab4c 100644 --- a/tests/unit/test_utilities.py +++ b/tests/unit/test_utilities.py @@ -118,6 +118,64 @@ def test_fin_flutter_analysis(flight_calisto_custom_wind): assert np.isclose(safety_factor(np.inf), 61.669562809629035, atol=5e-3) +def test_fin_flutter_analysis_without_wind(flight_calisto_robust): + """Without wind the Mach number is zero on the pad, where the safety factor + is undefined. Those points are left out, and the rest is the ratio of the + flutter and the freestream Mach numbers. + + Parameters + ---------- + flight_calisto_robust : Flight + A Flight object with a rocket with fins, in an atmosphere without wind. + """ + mach = flight_calisto_robust.mach_number + assert mach[0, 1] == 0 + + flutter_mach, safety_factor = utilities.fin_flutter_analysis( + fin_thickness=2 / 1000, + shear_modulus=10e9, + flight=flight_calisto_robust, + see_prints=False, + see_graphs=False, + ) + + time = safety_factor[:, 0] + assert len(time) == np.count_nonzero(mach[:, 1]) + assert safety_factor[:, 1] == pytest.approx( + np.array(flutter_mach(time)) / np.array(mach(time)), rel=1e-9 + ) + assert safety_factor(10) == pytest.approx(flutter_mach(10) / mach(10), rel=5e-3) + # The interpolation should not take the curve below its lowest sample + dense_time = np.linspace(0, time[-1], 20001) + assert min(safety_factor(dense_time)) >= safety_factor[:, 1].min() * (1 - 1e-9) + + +@patch("matplotlib.pyplot.show") +def test_fin_flutter_analysis_without_wind_output( + mock_show, flight_calisto_robust, capsys +): + """Tests the prints and graphs for a flight without wind. + + Parameters + ---------- + mock_show : mock + Mock of matplotlib.pyplot.show function. + flight_calisto_robust : Flight + A Flight object with a rocket with fins, in an atmosphere without wind. + """ + _, safety_factor = utilities.fin_flutter_analysis( + fin_thickness=2 / 1000, + shear_modulus=10e9, + flight=flight_calisto_robust, + see_prints=True, + see_graphs=True, + ) + + min_safety_factor = safety_factor[:, 1].min() + assert f"Min safety factor={min_safety_factor:.3f}" in capsys.readouterr().out + mock_show.assert_called() + + def test_calculate_stall_wind_velocity_returns_value(flight_calisto_custom_wind): """Regression: the stall wind velocity must be returned (it was previously only logged at INFO level and the method returned None, losing the value).