Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions rocketpy/simulation/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -3802,7 +3803,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
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/simulation/test_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,53 @@ 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 * 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"],
Expand Down
Loading