From 7039a562ec43220d3997be021d7f9a1444f1d65c Mon Sep 17 00:00:00 2001 From: Mohit Ak Date: Thu, 8 Oct 2026 02:29:17 -0400 Subject: [PATCH 1/2] BUG: fix the sign of Flight.potential_energy Flight.potential_energy computed mu * m * (1/(z + R) - 1/R), which is negative for z > 0 and decreases as the rocket climbs. Swap the two terms so the potential energy relative to sea level is positive and grows with altitude. Flight.total_energy inherits the fix. Fixes #1202 --- rocketpy/simulation/flight.py | 2 +- tests/unit/simulation/test_flight.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/rocketpy/simulation/flight.py b/rocketpy/simulation/flight.py index 425ca815e..4ade434c0 100644 --- a/rocketpy/simulation/flight.py +++ b/rocketpy/simulation/flight.py @@ -3802,7 +3802,7 @@ def potential_energy(self): return ( standard_gravitational_parameter * total_mass - * (1 / (self.z + self.env.earth_radius) - 1 / self.env.earth_radius) + * (1 / self.env.earth_radius - 1 / (self.z + self.env.earth_radius)) ) # Total Mechanical Energy diff --git a/tests/unit/simulation/test_flight.py b/tests/unit/simulation/test_flight.py index a486d8f05..9008a6b22 100644 --- a/tests/unit/simulation/test_flight.py +++ b/tests/unit/simulation/test_flight.py @@ -512,6 +512,31 @@ def test_max_values(flight_calisto_robust): assert pytest.approx(285.94948, rel=rtol) == test.max_speed +def test_potential_energy_increases_with_altitude(flight_calisto): + """Test that the potential energy grows as the rocket climbs. Between + burn out and apogee the mass is constant, so the change in potential + energy must match m * g * dz. + + Parameters + ---------- + flight_calisto : rocketpy.Flight + Flight object to be tested. See the conftest.py file for more info + regarding this pytest fixture. + """ + test = flight_calisto + burn_out_time = test.rocket.motor.burn_out_time + apogee_time = test.apogee_time + mass = test.rocket.total_mass(apogee_time) + delta_z = test.z(apogee_time) - test.z(burn_out_time) + initial_potential_energy = test.potential_energy(burn_out_time) + final_potential_energy = test.potential_energy(apogee_time) + delta_potential_energy = final_potential_energy - initial_potential_energy + expected_delta = mass * 9.80665 * delta_z + + assert delta_z > 0 + assert pytest.approx(expected_delta, rel=1e-2) == delta_potential_energy + + @pytest.mark.parametrize( "flight_time_attr", ["t_initial", "out_of_rail_time", "apogee_time", "t_final"], From 16a823787ae1c721fd78822b60f41f62bda9cede Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Sun, 15 Nov 2026 23:25:23 -0300 Subject: [PATCH 2/2] TST: pin potential energy reference and total energy Use Environment.standard_g instead of a literal, check that the potential energy is zero at sea level and positive above it, and that total_energy is kinetic plus potential energy. The docstring now states the convention. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XDW574BkSfcYpK6FQ4VCNe --- rocketpy/simulation/flight.py | 5 +++-- tests/unit/simulation/test_flight.py | 24 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/rocketpy/simulation/flight.py b/rocketpy/simulation/flight.py index 4ade434c0..8113bb51f 100644 --- a/rocketpy/simulation/flight.py +++ b/rocketpy/simulation/flight.py @@ -3791,8 +3791,9 @@ def kinetic_energy(self): # Potential Energy @funcify_method("Time (s)", "Potential Energy (J)", "spline", "constant") def potential_energy(self): - """Potential energy as a Function of time in relation to sea - level.""" + """Gravitational potential energy as a Function of time, given by + ``mu * m * (1 / R - 1 / (R + z))``. It is zero at sea level and + positive above it, increasing with altitude.""" # Constants # TODO: this constant should come from Environment. standard_gravitational_parameter = 3.986004418e14 diff --git a/tests/unit/simulation/test_flight.py b/tests/unit/simulation/test_flight.py index 9008a6b22..fe18717db 100644 --- a/tests/unit/simulation/test_flight.py +++ b/tests/unit/simulation/test_flight.py @@ -531,12 +531,34 @@ def test_potential_energy_increases_with_altitude(flight_calisto): initial_potential_energy = test.potential_energy(burn_out_time) final_potential_energy = test.potential_energy(apogee_time) delta_potential_energy = final_potential_energy - initial_potential_energy - expected_delta = mass * 9.80665 * delta_z + expected_delta = mass * test.env.standard_g * delta_z assert delta_z > 0 assert pytest.approx(expected_delta, rel=1e-2) == delta_potential_energy +def test_potential_energy_reference_and_total_energy(flight_calisto): + """Test that the potential energy is zero at sea level and positive + above it, and that the total energy is the kinetic plus the potential + energy. + + Parameters + ---------- + flight_calisto : rocketpy.Flight + Flight object to be tested. See the conftest.py file for more info + regarding this pytest fixture. + """ + test = flight_calisto + apogee_time = test.apogee_time + + assert test.z(test.t_initial) == pytest.approx(0) + assert test.potential_energy(test.t_initial) == pytest.approx(0, abs=1e-6) + assert test.potential_energy(apogee_time) > 0 + assert test.total_energy(apogee_time) == pytest.approx( + test.kinetic_energy(apogee_time) + test.potential_energy(apogee_time) + ) + + @pytest.mark.parametrize( "flight_time_attr", ["t_initial", "out_of_rail_time", "apogee_time", "t_final"],