Skip to content

Commit 45a891e

Browse files
ENH: Refactor Flight class to improve time node handling and sensor/controllers (#843)
* ENH: Refactor Flight class to improve time node handling and sensor/controller processing TST: add tests for overshootable controllers TST: fix slow tests fix merge conflicts * fix tests fix slow tests update CHANGELOG Update rocketpy/simulation/flight.py fix lint
1 parent a25fd25 commit 45a891e

File tree

8 files changed

+680
-341
lines changed

8 files changed

+680
-341
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Attention: The newest changes should be on top -->
4949

5050
### Changed
5151

52-
-
52+
- ENH: Refactor Flight class to improve time node handling and sensor/controllers [#843](https://github.com/RocketPy-Team/RocketPy/pull/843)
5353

5454
### Fixed
5555

rocketpy/plots/flight_plots.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,16 @@ def energy_data(self, *, filename=None): # pylint: disable=too-many-statements
720720
ax2.grid()
721721

722722
ax3 = plt.subplot(413)
723+
# Handle both array-based and callable-based Functions
724+
thrust_power = self.flight.thrust_power
725+
if callable(thrust_power.source):
726+
# For callable sources, discretize based on speed
727+
thrust_power = thrust_power.set_discrete_based_on_model(
728+
self.flight.speed, mutate_self=False
729+
)
723730
ax3.plot(
724-
self.flight.thrust_power[:, 0],
725-
self.flight.thrust_power[:, 1],
731+
thrust_power[:, 0],
732+
thrust_power[:, 1],
726733
label="|Thrust Power|",
727734
)
728735
ax3.set_xlim(0, self.flight.rocket.motor.burn_out_time)
@@ -734,9 +741,16 @@ def energy_data(self, *, filename=None): # pylint: disable=too-many-statements
734741
ax3.grid()
735742

736743
ax4 = plt.subplot(414)
744+
# Handle both array-based and callable-based Functions
745+
drag_power = self.flight.drag_power
746+
if callable(drag_power.source):
747+
# For callable sources, discretize based on speed
748+
drag_power = drag_power.set_discrete_based_on_model(
749+
self.flight.speed, mutate_self=False
750+
)
737751
ax4.plot(
738-
self.flight.drag_power[:, 0],
739-
-self.flight.drag_power[:, 1],
752+
drag_power[:, 0],
753+
-drag_power[:, 1],
740754
label="|Drag Power|",
741755
)
742756
ax4.set_xlim(

rocketpy/simulation/flight.py

Lines changed: 625 additions & 329 deletions
Large diffs are not rendered by default.

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def pytest_collection_modifyitems(config, items):
7676
item.add_marker(skip_slow)
7777

7878

79+
# TODO: move this to Environment fixtures when possible
7980
@pytest.fixture
8081
def merra2_file_path(tmp_path): # pylint: disable=too-many-statements
8182
"""

tests/fixtures/flight/flight_fixtures.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,22 @@ def flight_calisto_air_brakes(calisto_air_brakes_clamp_on, example_plain_env):
246246
)
247247

248248

249+
@pytest.fixture
250+
def flight_calisto_air_brakes_time_overshoot(
251+
calisto_air_brakes_clamp_on, example_plain_env
252+
):
253+
"""Same as flight_calisto_air_brakes but with time_overshoot=True."""
254+
return Flight(
255+
rocket=calisto_air_brakes_clamp_on,
256+
environment=example_plain_env,
257+
rail_length=5.2,
258+
inclination=85,
259+
heading=0,
260+
time_overshoot=True,
261+
terminate_on_apogee=True,
262+
)
263+
264+
249265
@pytest.fixture
250266
def flight_calisto_with_sensors(calisto_with_sensors, example_plain_env):
251267
"""A rocketpy.Flight object of the Calisto rocket. This uses the calisto

tests/integration/environment/test_environment.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,8 @@ def test_nam_atmosphere(mock_show, example_spaceport_env): # pylint: disable=un
160160
@pytest.mark.slow
161161
@patch("matplotlib.pyplot.show")
162162
def test_rap_atmosphere(mock_show, example_spaceport_env): # pylint: disable=unused-argument
163-
today = date.today()
164163
now = datetime.now(timezone.utc)
165-
example_spaceport_env.set_date((today.year, today.month, today.day, now.hour))
164+
example_spaceport_env.set_date((now.year, now.month, now.day, now.hour))
166165
example_spaceport_env.set_atmospheric_model(type="Forecast", file="RAP")
167166
assert example_spaceport_env.all_info() is None
168167

@@ -281,7 +280,7 @@ def test_merra2_full_specification_compliance(merra2_file_path, example_plain_en
281280
# Input: 9806.65 m2/s2
282281
# Expected: 1000.0 m
283282
print(f"Calculated Elevation: {env.elevation} m")
284-
assert abs(env.elevation - 1000.0) < 1e-6, (
283+
assert abs(env.elevation - 1000.0) < 1e-4, (
285284
f"Failed to convert PHIS (m2/s2) to meters. Got {env.elevation}, expected 1000.0"
286285
)
287286

tests/integration/simulation/test_flight.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_liquid_motor_flight(mock_show, flight_calisto_liquid_modded): # pylint
102102

103103
@pytest.mark.slow
104104
@patch("matplotlib.pyplot.show")
105-
def test_time_overshoot(mock_show, calisto_robust, example_spaceport_env): # pylint: disable=unused-argument
105+
def test_time_overshoot_false(mock_show, calisto_robust, example_spaceport_env): # pylint: disable=unused-argument
106106
"""Test the time_overshoot parameter of the Flight class. This basically
107107
calls the all_info() method for a simulation without time_overshoot and
108108
checks if it returns None. It is not testing if the values are correct,
@@ -293,6 +293,19 @@ def test_air_brakes_flight(mock_show, flight_calisto_air_brakes): # pylint: dis
293293
assert air_brakes.prints.all() is None
294294

295295

296+
@patch("matplotlib.pyplot.show")
297+
def test_air_brakes_flight_with_overshoot(
298+
mock_show, flight_calisto_air_brakes_time_overshoot
299+
): # pylint: disable=unused-argument
300+
"""
301+
Same as test_air_brakes_flight but with time_overshoot=True.
302+
"""
303+
test_flight = flight_calisto_air_brakes_time_overshoot
304+
air_brakes = test_flight.rocket.air_brakes[0]
305+
assert air_brakes.plots.all() is None
306+
assert air_brakes.prints.all() is None
307+
308+
296309
@patch("matplotlib.pyplot.show")
297310
def test_initial_solution(mock_show, example_plain_env, calisto_robust): # pylint: disable=unused-argument
298311
"""Tests the initial_solution option of the Flight class. This test simply

tests/integration/test_plots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ def test_compare_flights(mock_show, mock_figure_show, calisto, example_plain_env
7474
)
7575

7676
calisto.set_rail_buttons(-0.5, 0.2)
77-
inclinations = [60, 70, 80, 90]
78-
headings = [0, 45, 90, 180]
77+
inclinations = [60, 90]
78+
headings = [0, 180]
7979
flights = []
80-
# Create (4 * 4) = 16 different flights to be compared
80+
# Create (2 * 2) = 4 different flights to be compared
8181
for heading in headings:
8282
for inclination in inclinations:
8383
flight = Flight(

0 commit comments

Comments
 (0)