-
-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathtest_flight.py
More file actions
492 lines (430 loc) · 14.7 KB
/
test_flight.py
File metadata and controls
492 lines (430 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
from unittest.mock import patch
import matplotlib as plt
import numpy as np
import pytest
from rocketpy import Flight, Parachute
plt.rcParams.update({"figure.max_open_warning": 0})
@pytest.mark.parametrize(
"flight_fixture", ["flight_calisto_robust", "flight_calisto_robust_solid_eom"]
)
@patch("matplotlib.pyplot.show")
# pylint: disable=unused-argument
def test_all_info(mock_show, request, flight_fixture):
"""Test that the flight class is working as intended. This basically calls
the all_info() method and checks if it returns None. It is not testing if
the values are correct, but whether the method is working without errors.
Parameters
----------
mock_show : unittest.mock.MagicMock
Mock object to replace matplotlib.pyplot.show
request : _pytest.fixtures.FixtureRequest
Request object to access the fixture dynamically.
flight_fixture : str
Name of the flight fixture to be tested.
"""
flight = request.getfixturevalue(flight_fixture)
assert flight.all_info() is None
@pytest.mark.slow
@patch("matplotlib.pyplot.show")
@pytest.mark.parametrize("solver_method", ["RK45", "DOP853", "Radau", "BDF"])
# RK23 is unstable and requires a very low tolerance to work
# pylint: disable=unused-argument
def test_all_info_different_solvers(
mock_show, calisto_robust, example_spaceport_env, solver_method
):
"""Test that the flight class is working as intended with different solver
methods. This basically calls the all_info() method and checks if it returns
None. It is not testing if the values are correct, but whether the method is
working without errors.
Parameters
----------
mock_show : unittest.mock.MagicMock
Mock object to replace matplotlib.pyplot.show
calisto_robust : rocketpy.Rocket
Rocket to be simulated. See the conftest.py file for more info.
example_spaceport_env : rocketpy.Environment
Environment to be simulated. See the conftest.py file for more info.
solver_method : str
The solver method to be used in the simulation.
"""
test_flight = Flight(
environment=example_spaceport_env,
rocket=calisto_robust,
rail_length=5.2,
inclination=85,
heading=0,
terminate_on_apogee=False,
ode_solver=solver_method,
)
assert test_flight.all_info() is None
@patch("matplotlib.pyplot.show")
def test_hybrid_motor_flight(
mock_show, flight_calisto_hybrid_modded
): # pylint: disable=unused-argument
"""Test the flight of a rocket with a hybrid motor. This test only validates
that a flight simulation can be performed with a hybrid motor; it does not
validate the results.
Parameters
----------
mock_show : unittest.mock.MagicMock
Mock object to replace matplotlib.pyplot.show
flight_calisto_hybrid_modded : rocketpy.Flight
Sample Flight to be tested. See the conftest.py file for more info.
"""
assert flight_calisto_hybrid_modded.all_info() is None
@patch("matplotlib.pyplot.show")
def test_liquid_motor_flight(
mock_show, flight_calisto_liquid_modded
): # pylint: disable=unused-argument
"""Test the flight of a rocket with a liquid motor. This test only validates
that a flight simulation can be performed with a liquid motor; it does not
validate the results.
Parameters
----------
mock_show : unittest.mock.MagicMock
Mock object to replace matplotlib.pyplot.show
flight_calisto_liquid_modded : rocketpy.Flight
Sample Flight to be tested. See the conftest.py file for more info.
"""
assert flight_calisto_liquid_modded.all_info() is None
@pytest.mark.slow
@patch("matplotlib.pyplot.show")
def test_time_overshoot(
mock_show, calisto_robust, example_spaceport_env
): # pylint: disable=unused-argument
"""Test the time_overshoot parameter of the Flight class. This basically
calls the all_info() method for a simulation without time_overshoot and
checks if it returns None. It is not testing if the values are correct,
just if the flight simulation is not breaking.
Parameters
----------
calisto_robust : rocketpy.Rocket
The rocket to be simulated. In this case, the fixture rocket is used.
See the conftest.py file for more information.
example_spaceport_env : rocketpy.Environment
The environment to be simulated. In this case, the fixture environment
is used. See the conftest.py file for more information.
"""
test_flight = Flight(
rocket=calisto_robust,
environment=example_spaceport_env,
rail_length=5.2,
inclination=85,
heading=0,
time_overshoot=False,
)
assert test_flight.all_info() is None
@patch("matplotlib.pyplot.show")
def test_simpler_parachute_triggers(
mock_show, example_plain_env, calisto_robust
): # pylint: disable=unused-argument
"""Tests different types of parachute triggers. This is important to ensure
the code is working as intended, since the parachute triggers can have very
different format definitions. It will add 3 parachutes using different
triggers format and check if the parachute events are being at the correct
altitude
Parameters
----------
mock_show : unittest.mock.MagicMock
Mock object to replace matplotlib.pyplot.show
example_plain_env : rocketpy.Environment
Environment to be simulated. See the conftest.py file for more info.
calisto_robust : rocketpy.Rocket
Rocket to be simulated. See the conftest.py file for more info.
"""
calisto_robust.parachutes = []
_ = calisto_robust.add_parachute(
"Main",
cd_s=10.0,
trigger=400,
sampling_rate=105,
lag=0,
)
_ = calisto_robust.add_parachute(
"Drogue2",
cd_s=5.5,
trigger=lambda pressure, height, state: height < 800 and state[5] < 0,
sampling_rate=105,
lag=0,
)
_ = calisto_robust.add_parachute(
"Drogue",
cd_s=1.0,
trigger="apogee",
sampling_rate=105,
lag=0,
)
test_flight = Flight(
rocket=calisto_robust,
environment=example_plain_env,
rail_length=5,
inclination=85,
heading=0,
)
assert (
abs(test_flight.z(test_flight.parachute_events[0][0]) - test_flight.apogee) <= 1
)
assert (
abs(
test_flight.z(test_flight.parachute_events[1][0])
- (800 + example_plain_env.elevation)
)
<= 1
)
assert (
abs(
test_flight.z(test_flight.parachute_events[2][0])
- (400 + example_plain_env.elevation)
)
<= 1
)
assert calisto_robust.all_info() is None
assert test_flight.all_info() is None
@patch("matplotlib.pyplot.show")
def test_rolling_flight( # pylint: disable=unused-argument
mock_show,
example_plain_env,
cesaroni_m1670,
calisto,
calisto_nose_cone,
calisto_tail,
calisto_main_chute,
calisto_drogue_chute,
):
test_rocket = calisto
test_rocket.set_rail_buttons(0.082, -0.618)
test_rocket.add_motor(cesaroni_m1670, position=-1.373)
test_rocket.add_trapezoidal_fins(
4,
span=0.100,
root_chord=0.120,
tip_chord=0.040,
position=-1.04956,
cant_angle=0.5,
)
calisto.add_surfaces(calisto_nose_cone, 1.160)
calisto.add_surfaces(calisto_tail, -1.313)
calisto.parachutes.append(calisto_main_chute)
calisto.parachutes.append(calisto_drogue_chute)
test_flight = Flight(
rocket=test_rocket,
environment=example_plain_env,
rail_length=5.2,
inclination=85,
heading=0,
)
assert test_flight.all_info() is None
@patch("matplotlib.pyplot.show")
def test_eccentricity_on_flight( # pylint: disable=unused-argument
mock_show,
example_plain_env,
cesaroni_m1670,
calisto,
calisto_nose_cone,
calisto_trapezoidal_fins,
calisto_tail,
):
test_rocket = calisto
test_rocket.set_rail_buttons(0.082, -0.618)
test_rocket.add_motor(cesaroni_m1670, position=-1.373)
calisto.add_surfaces(calisto_trapezoidal_fins, -1.04956)
calisto.add_surfaces(calisto_nose_cone, 1.160)
calisto.add_surfaces(calisto_tail, -1.313)
calisto.add_cm_eccentricity(x=-0.01, y=-0.01)
test_flight = Flight(
rocket=test_rocket,
environment=example_plain_env,
rail_length=5.2,
inclination=85,
heading=0,
terminate_on_apogee=True,
)
assert test_flight.all_info() is None
@patch("matplotlib.pyplot.show")
def test_air_brakes_flight(
mock_show, flight_calisto_air_brakes
): # pylint: disable=unused-argument
"""Test the flight of a rocket with air brakes. This test only validates
that a flight simulation can be performed with air brakes; it does not
validate the results.
Parameters
----------
mock_show : unittest.mock.MagicMock
Mock object to replace matplotlib.pyplot.show
flight_calisto_air_brakes_clamp_on : rocketpy.Flight
Flight object to be tested. See the conftest.py file for more info
regarding this pytest fixture.
"""
test_flight = flight_calisto_air_brakes
air_brakes = test_flight.rocket.air_brakes[0]
assert air_brakes.plots.all() is None
assert air_brakes.prints.all() is None
@patch("matplotlib.pyplot.show")
def test_initial_solution(
mock_show, example_plain_env, calisto_robust
): # pylint: disable=unused-argument
"""Tests the initial_solution option of the Flight class. This test simply
simulates the flight using the initial_solution option and checks if the
all_info method returns None.
Parameters
----------
mock_show : unittest.mock.MagicMock
Mock object to replace matplotlib.pyplot.show
example_plain_env : rocketpy.Environment
Environment to be simulated. See the conftest.py file for more info.
calisto_robust : rocketpy.Rocket
Rocket to be simulated. See the conftest.py file for more info.
"""
test_flight = Flight(
rocket=calisto_robust,
environment=example_plain_env,
rail_length=5,
inclination=85,
heading=0,
rtol=1e-8,
atol=1e-6,
verbose=True,
initial_solution=[
0.0,
0.0,
0.0,
1.5e3,
10,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
],
)
assert test_flight.all_info() is None
@patch("matplotlib.pyplot.show")
def test_empty_motor_flight(
mock_show, example_plain_env, calisto_motorless
): # pylint: disable=unused-argument
flight = Flight(
rocket=calisto_motorless,
environment=example_plain_env,
rail_length=5,
initial_solution=[ # a random flight starting at apogee
22.945995194368354,
277.80976806186936,
353.29457980509113,
3856.1112773441596,
12.737953434495966,
15.524649322067267,
-0.00011874766384947776,
-0.06086838708814366,
0.019695167217632138,
-0.35099420532705555,
-0.9338841410225396,
0.25418555574446716,
0.03632002739509155,
2.0747266017020563,
],
)
assert flight.all_info() is None
def test_freestream_speed_at_apogee(example_plain_env, calisto):
"""
Asserts that a rocket at apogee has a free stream speed of near 0.0 m/s
in all directions given that the environment doesn't have any wind. Any
speed values comes from coriolis effect.
"""
# NOTE: this rocket doesn't move in x or z direction. There's no wind.
hard_atol = 1e-12
soft_atol = 1e-6
test_flight = Flight(
environment=example_plain_env,
rocket=calisto,
rail_length=5.2,
inclination=90,
heading=0,
terminate_on_apogee=False,
atol=13 * [hard_atol],
)
assert np.isclose(
test_flight.stream_velocity_x(test_flight.apogee_time),
0.4641492104717301,
atol=hard_atol,
)
assert np.isclose(
test_flight.stream_velocity_y(test_flight.apogee_time), 0.0, atol=hard_atol
)
# NOTE: stream_velocity_z has a higher error due to apogee detection estimation
assert np.isclose(
test_flight.stream_velocity_z(test_flight.apogee_time), 0.0, atol=soft_atol
)
assert np.isclose(
test_flight.free_stream_speed(test_flight.apogee_time),
0.4641492104717798,
atol=hard_atol,
)
assert np.isclose(
test_flight.apogee_freestream_speed, 0.4641492104717798, atol=hard_atol
)
def test_rocket_csys_equivalence(
flight_calisto_robust, flight_calisto_nose_to_tail_robust
):
"""Test the equivalence of the rocket coordinate systems between two
different flight simulations.
Parameters
----------
flight_calisto_robust : rocketpy.Flight
Flight object to be tested. See the conftest.py file for more info.
flight_calisto_nose_to_tail_robust : rocketpy.Flight
Flight object to be tested. See the conftest.py file for more info.
"""
assert np.isclose(
flight_calisto_robust.apogee, flight_calisto_nose_to_tail_robust.apogee
)
assert np.isclose(
flight_calisto_robust.apogee_time,
flight_calisto_nose_to_tail_robust.apogee_time,
)
assert np.isclose(
flight_calisto_robust.x_impact,
flight_calisto_nose_to_tail_robust.x_impact,
atol=1e-3,
)
assert np.isclose(
flight_calisto_robust.y_impact,
flight_calisto_nose_to_tail_robust.y_impact,
)
assert np.allclose(
flight_calisto_robust.initial_solution,
flight_calisto_nose_to_tail_robust.initial_solution,
)
def test_opening_shock_recorded_during_flight(calisto, example_plain_env):
"""
Testing if the opening shock is being saved correctly during simulations.
"""
# Defining test parachute
calisto.parachutes = []
target_coeff = 1.75
main_chute = Parachute(
name="Main Test",
cd_s=5.0,
trigger="apogee",
sampling_rate=100,
opening_shock_coefficient=target_coeff,
)
calisto.parachutes.append(main_chute)
# Simulating
flight = Flight(
rocket=calisto,
environment=example_plain_env,
rail_length=5,
inclination=85,
heading=0,
terminate_on_apogee=False,
)
# Analysing results
assert len(flight.parachute_events) > 0, "No parachute event registered!"
event_time, flown_chute = flight.parachute_events[0]
assert flown_chute.opening_shock_force is not None
assert flown_chute.opening_shock_force > 0
assert flown_chute.opening_shock_coefficient == target_coeff