Skip to content

Commit 76cab5a

Browse files
authored
BUG: build Monte Carlo flights with the configuration they were given (#1164)
__run_single_simulation writes the Flight constructor out by hand and stops at time_overshoot. StochasticFlight.create_object passes nine more: max_time, the two time steps, rtol, atol, name, equations_of_motion, ode_solver and simulation_mode. So a Monte Carlo run ignored the max time, the tolerances, the solver and the equations of motion the caller had set, and reset the simulation mode to the constructor default. The same rocket, environment and flight therefore flew differently under MonteCarlo than under StochasticFlight.create_object. #1070 added StochasticFlight's handling of these; this path never picked it up. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent f604534 commit 76cab5a

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎rocketpy/simulation/monte_carlo.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,19 @@ def __run_single_simulation(self):
471471
initial_solution=self.flight.initial_solution,
472472
terminate_on_apogee=self.flight.terminate_on_apogee,
473473
time_overshoot=self.flight.time_overshoot,
474+
# The rest of what StochasticFlight.create_object passes. Left out
475+
# here, a run ignored the max_time, tolerances, solver, equations of
476+
# motion and simulation mode the caller had set, which is what #1070
477+
# added StochasticFlight's own handling of them for.
478+
max_time=self.flight.max_time,
479+
max_time_step=self.flight.obj.max_time_step,
480+
min_time_step=self.flight.obj.min_time_step,
481+
rtol=self.flight.obj.rtol,
482+
atol=self.flight.obj.atol,
483+
name=self.flight.obj.name,
484+
equations_of_motion=self.flight.obj.equations_of_motion,
485+
ode_solver=self.flight.obj.ode_solver,
486+
simulation_mode=self.flight.obj.simulation_mode,
474487
)
475488

476489
def estimate_confidence_interval(

‎tests/unit/simulation/test_monte_carlo.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import csv
22
import json
33
import pathlib
4+
import types
45
from collections import namedtuple
56

67
import matplotlib as plt
@@ -550,3 +551,48 @@ def test_simulate_convergence_runs_until_max_when_not_converging():
550551
assert mc.num_of_loaded_sims == 200
551552
assert all(width > 0.5 for width in history)
552553
assert len(history) == 4 # 200 / 50 batches
554+
555+
556+
def test_a_monte_carlo_flight_keeps_the_configuration_it_was_given(monkeypatch):
557+
"""A run must build the same ``Flight`` ``StochasticFlight`` would.
558+
559+
Monte Carlo wrote out the constructor by hand and stopped at
560+
``time_overshoot``, so ``max_time``, the tolerances, the solver, the
561+
equations of motion and the simulation mode were silently reset to their
562+
defaults. #1070 added StochasticFlight's handling of exactly those.
563+
"""
564+
base = types.SimpleNamespace(
565+
max_time_step=0.5,
566+
min_time_step=0.01,
567+
rtol=1e-9,
568+
atol=1e-9,
569+
name="named",
570+
equations_of_motion="solid_propulsion",
571+
ode_solver="RK23",
572+
simulation_mode="native",
573+
)
574+
stochastic_flight = types.SimpleNamespace(
575+
obj=base,
576+
max_time=123.0,
577+
initial_solution=None,
578+
terminate_on_apogee=True,
579+
time_overshoot=False,
580+
_randomize_rail_length=lambda: 5.0,
581+
_randomize_inclination=lambda: 84.0,
582+
_randomize_heading=lambda: 133.0,
583+
)
584+
analysis = object.__new__(MonteCarlo)
585+
analysis.flight = stochastic_flight
586+
analysis.rocket = types.SimpleNamespace(create_object=lambda: "rocket")
587+
analysis.environment = types.SimpleNamespace(create_object=lambda: "environment")
588+
monkeypatch.setattr("rocketpy.simulation.monte_carlo.Flight", types.SimpleNamespace)
589+
590+
flight = MonteCarlo._MonteCarlo__run_single_simulation(analysis)
591+
592+
assert flight.max_time == 123.0
593+
assert (flight.rtol, flight.atol) == (1e-9, 1e-9)
594+
assert (flight.max_time_step, flight.min_time_step) == (0.5, 0.01)
595+
assert flight.ode_solver == "RK23"
596+
assert flight.equations_of_motion == "solid_propulsion"
597+
assert flight.simulation_mode == "native"
598+
assert flight.name == "named"

0 commit comments

Comments
 (0)