Skip to content

Commit d8663a4

Browse files
authored
ENH: Add optional max time to stochastic_flight and create stochastic_flight objects with attributes in base flight (#1070)
* Add max_time option to stochastic_flight * Update: use default attributes in flight object for stochastic flights * Add tests to check attributes * Refactor implementation
1 parent 550c90e commit d8663a4

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

rocketpy/stochastic/stochastic_flight.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class StochasticFlight(StochasticModel):
3232
time_overshoot : bool
3333
If False, the simulation will run at the time step defined by the controller
3434
sampling rate. Be aware that this will make the simulation run much slower.
35+
max_time : int, float
36+
The maximum time of the flight simulation. If the flight simulation
37+
reaches this time, it will terminate. This attribute can not be randomized.
3538
"""
3639

3740
def __init__(
@@ -43,6 +46,7 @@ def __init__(
4346
initial_solution=None,
4447
terminate_on_apogee=None,
4548
time_overshoot=None,
49+
max_time=None,
4650
):
4751
"""Initializes the Stochastic Flight class.
4852
@@ -70,6 +74,9 @@ def __init__(
7074
time_overshoot : bool
7175
If False, the simulation will run at the time step defined by the controller
7276
sampling rate. Be aware that this will make the simulation run much slower.
77+
max_time : int, float
78+
The maximum time of the flight simulation. If the flight simulation
79+
reaches this time, it will terminate. This attribute can not be randomized.
7380
"""
7481
if terminate_on_apogee is not None:
7582
assert isinstance(terminate_on_apogee, bool), (
@@ -78,6 +85,9 @@ def __init__(
7885
if time_overshoot is not None:
7986
if not isinstance(time_overshoot, bool):
8087
raise TypeError("`time_overshoot` must be a boolean")
88+
if max_time is not None:
89+
if not isinstance(max_time, (int, float)):
90+
raise TypeError("`max_time` must be a number")
8191
super().__init__(
8292
flight,
8393
rail_length=rail_length,
@@ -87,6 +97,10 @@ def __init__(
8797

8898
self.initial_solution = initial_solution
8999
self.terminate_on_apogee = terminate_on_apogee
100+
if max_time is None:
101+
self.max_time = flight.max_time
102+
else:
103+
self.max_time = max_time
90104
if time_overshoot is None:
91105
self.time_overshoot = flight.time_overshoot
92106
else:
@@ -135,12 +149,21 @@ def create_object(self):
135149
generated_dict = next(self.dict_generator())
136150
# TODO: maybe we should use generated_dict["rail_length"] instead
137151
return Flight(
152+
rocket=self.obj.rocket,
138153
environment=self.obj.env,
139154
rail_length=self._randomize_rail_length(),
140-
rocket=self.obj.rocket,
141155
inclination=generated_dict["inclination"],
142156
heading=generated_dict["heading"],
143157
initial_solution=self.initial_solution,
144158
terminate_on_apogee=self.terminate_on_apogee,
159+
max_time=self.max_time,
160+
max_time_step=self.obj.max_time_step,
161+
min_time_step=self.obj.min_time_step,
162+
rtol=self.obj.rtol,
163+
atol=self.obj.atol,
145164
time_overshoot=self.time_overshoot,
165+
name=self.obj.name,
166+
equations_of_motion=self.obj.equations_of_motion,
167+
ode_solver=self.obj.ode_solver,
168+
simulation_mode=self.obj.simulation_mode,
146169
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
11
from rocketpy.simulation.flight import Flight
2+
from rocketpy.stochastic import StochasticFlight
23

34

45
def test_stochastic_flight_create_object(stochastic_flight):
56
obj = stochastic_flight.create_object()
67
assert isinstance(obj, Flight)
8+
9+
10+
def test_stochastic_flight_inherited_attributes(calisto_robust, example_spaceport_env):
11+
flight = Flight(
12+
rocket=calisto_robust,
13+
environment=example_spaceport_env,
14+
rail_length=5.2,
15+
max_time_step=10,
16+
min_time_step=0.01,
17+
rtol=1e-4,
18+
atol=1e-6,
19+
name="FlightName",
20+
equations_of_motion="solid_propulsion",
21+
ode_solver="BDF",
22+
simulation_mode="3 DOF",
23+
)
24+
stochastic_flight = StochasticFlight(flight=flight)
25+
26+
obj = stochastic_flight.create_object()
27+
assert flight.max_time_step == obj.max_time_step
28+
assert flight.min_time_step == obj.min_time_step
29+
assert flight.rtol == obj.rtol
30+
assert flight.atol == obj.atol
31+
assert flight.name == obj.name
32+
assert flight.equations_of_motion == obj.equations_of_motion
33+
assert flight.ode_solver == obj.ode_solver
34+
assert flight.simulation_mode == obj.simulation_mode
35+
36+
37+
def test_stochastic_flight_optional_attributes(flight_calisto_robust):
38+
stochastic_flight = StochasticFlight(
39+
flight=flight_calisto_robust,
40+
terminate_on_apogee=True,
41+
time_overshoot=True,
42+
max_time=987.6,
43+
)
44+
obj = stochastic_flight.create_object()
45+
assert obj.terminate_on_apogee is True
46+
assert obj.time_overshoot is True
47+
assert obj.max_time == 987.6

0 commit comments

Comments
 (0)