-
-
Notifications
You must be signed in to change notification settings - Fork 265
ENH: reproducible Monte Carlo via per-simulation-index seeding #1054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thc1006
wants to merge
2
commits into
RocketPy-Team:develop
Choose a base branch
from
thc1006:enh/reproducible-montecarlo-seeding
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+393
−18
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
tests/integration/simulation/test_monte_carlo_determinism.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| """End-to-end determinism tests for ``MonteCarlo.simulate(random_seed=...)``. | ||
|
|
||
| With a fixed ``random_seed`` the generated random *inputs* are reproducible and | ||
| identical across serial and parallel execution and across any number of workers. | ||
| Each simulation index draws from its own child stream spawned from the run's root | ||
| seed, and ``SeedSequence.spawn`` is prefix-stable, so index ``i`` maps to the same | ||
| seed regardless of the worker that runs it. (The seed-handling helpers themselves | ||
| are unit tested in ``tests/unit/simulation/test_monte_carlo_determinism``.) | ||
|
|
||
| The trajectory integration (``Flight``) is stubbed: worker invariance is a | ||
| property of the *input sampling*, which happens before ``Flight`` is built, so a | ||
| stub keeps the runs fast while still driving the real serial and parallel loops. | ||
| Stubbing the module-level ``Flight`` symbol reaches the parallel workers only | ||
| under the ``fork`` start method, so the worker-invariance test skips otherwise and | ||
| is marked ``slow`` to match the other Monte Carlo multiprocessing tests. | ||
|
|
||
| A dedicated numpy-only rocket is used so *all* randomness flows through the seeded | ||
| numpy generator. List-valued stochastic attributes are sampled with the standard | ||
| library ``random.choice`` (an unseeded global generator) which ``random_seed`` | ||
| does not govern; the fixture drops the only such attribute (a multi-element | ||
| ``thrust_source``) so the inputs are byte-for-byte reproducible from the seed. | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| import pytest | ||
|
|
||
| import rocketpy.simulation.monte_carlo as mc_module | ||
| from rocketpy.simulation import MonteCarlo | ||
| from rocketpy.stochastic import StochasticRocket, StochasticSolidMotor | ||
|
|
||
|
|
||
| class _StubFlight: | ||
| """Minimal stand-in for ``Flight`` that skips trajectory integration.""" | ||
|
|
||
| def __init__(self, **kwargs): # accepts and ignores MonteCarlo's Flight kwargs | ||
| pass | ||
|
|
||
| def __getattr__(self, name): | ||
| return 0.0 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def stochastic_calisto_numpy_only( | ||
| cesaroni_m1670, | ||
| calisto_robust, | ||
| stochastic_nose_cone, | ||
| stochastic_trapezoidal_fins, | ||
| stochastic_tail, | ||
| stochastic_rail_buttons, | ||
| stochastic_main_parachute, | ||
| stochastic_drogue_parachute, | ||
| ): | ||
| """A ``StochasticRocket`` whose randomness flows entirely through numpy. | ||
|
|
||
| Mirrors the shared ``stochastic_calisto`` fixture but gives the solid motor a | ||
| single ``thrust_source`` instead of a multi-element list, so no attribute is | ||
| sampled through the unseeded standard-library ``random.choice``. | ||
| """ | ||
| motor = StochasticSolidMotor( | ||
| solid_motor=cesaroni_m1670, | ||
| burn_out_time=(4, 0.1), | ||
| grains_center_of_mass_position=0.001, | ||
| grain_density=50, | ||
| grain_separation=1 / 1000, | ||
| grain_initial_height=1 / 1000, | ||
| grain_initial_inner_radius=0.375 / 1000, | ||
| grain_outer_radius=0.375 / 1000, | ||
| total_impulse=(6500, 1000), | ||
| throat_radius=0.5 / 1000, | ||
| nozzle_radius=0.5 / 1000, | ||
| nozzle_position=0.001, | ||
| ) | ||
| rocket = StochasticRocket( | ||
| rocket=calisto_robust, | ||
| radius=0.0127 / 2000, | ||
| mass=(15.426, 0.5, "normal"), | ||
| inertia_11=(6.321, 0), | ||
| inertia_22=0.01, | ||
| inertia_33=0.01, | ||
| center_of_mass_without_motor=0, | ||
| ) | ||
| rocket.add_motor(motor, position=0.001) | ||
| rocket.add_nose(stochastic_nose_cone, position=(1.134, 0.001)) | ||
| rocket.add_trapezoidal_fins(stochastic_trapezoidal_fins, position=(0.001, "normal")) | ||
| rocket.add_tail(stochastic_tail) | ||
| rocket.set_rail_buttons( | ||
| stochastic_rail_buttons, lower_button_position=(-0.618, 0.001, "normal") | ||
| ) | ||
| rocket.add_parachute(parachute=stochastic_main_parachute) | ||
| rocket.add_parachute(parachute=stochastic_drogue_parachute) | ||
| return rocket | ||
|
|
||
|
|
||
| def _read_inputs_by_index(input_file): | ||
| """Read a ``.inputs.txt`` file into ``{index: raw_json_line}``.""" | ||
| by_index = {} | ||
| with open(input_file, mode="r", encoding="utf-8") as rows: | ||
| for line in rows: | ||
| line = line.strip() | ||
| if not line: | ||
| continue | ||
| by_index[json.loads(line)["index"]] = line | ||
| return by_index | ||
|
|
||
|
|
||
| def _simulate_inputs( | ||
| monkeypatch, tmp_path, environment, rocket, flight, tag, **simulate_kwargs | ||
| ): | ||
| """Run a Monte Carlo with a stubbed ``Flight`` and return inputs by index.""" | ||
| monkeypatch.setattr(mc_module, "Flight", _StubFlight) | ||
| montecarlo = MonteCarlo( | ||
| filename=str(tmp_path / tag), | ||
| environment=environment, | ||
| rocket=rocket, | ||
| flight=flight, | ||
| ) | ||
| montecarlo.simulate(**simulate_kwargs) | ||
| return _read_inputs_by_index(montecarlo.input_file) | ||
|
|
||
|
|
||
| def test_serial_inputs_are_reproducible( | ||
| monkeypatch, | ||
| tmp_path, | ||
| stochastic_environment, | ||
| stochastic_calisto_numpy_only, | ||
| stochastic_flight, | ||
| ): | ||
| """Two serial runs with the same seed yield byte-identical inputs per index. | ||
|
|
||
| This drives the serial ``simulate`` path end to end; the flexible seed types | ||
| are covered by the unit test of ``__root_seed_sequence``. | ||
| """ | ||
| models = (stochastic_environment, stochastic_calisto_numpy_only, stochastic_flight) | ||
| run_a = _simulate_inputs( | ||
| monkeypatch, tmp_path, *models, "a", number_of_simulations=3, random_seed=7 | ||
| ) | ||
| run_b = _simulate_inputs( | ||
| monkeypatch, tmp_path, *models, "b", number_of_simulations=3, random_seed=7 | ||
| ) | ||
| assert sorted(run_a) == list(range(3)) | ||
| assert run_a == run_b | ||
|
|
||
|
|
||
| @pytest.mark.slow | ||
| def test_inputs_are_worker_invariant( | ||
| monkeypatch, | ||
| tmp_path, | ||
| stochastic_environment, | ||
| stochastic_calisto_numpy_only, | ||
| stochastic_flight, | ||
| ): | ||
| """serial == parallel(2) == parallel(4): inputs are bit-identical per index.""" | ||
| multiprocess = pytest.importorskip("multiprocess") | ||
| if multiprocess.get_start_method() != "fork": | ||
| pytest.skip( | ||
| "stub-based parallel determinism test requires the 'fork' start method" | ||
| ) | ||
|
|
||
| models = (stochastic_environment, stochastic_calisto_numpy_only, stochastic_flight) | ||
| common = {"number_of_simulations": 8, "random_seed": 314159} | ||
|
|
||
| serial = _simulate_inputs(monkeypatch, tmp_path, *models, "serial", **common) | ||
| par2 = _simulate_inputs( | ||
| monkeypatch, tmp_path, *models, "par2", parallel=True, n_workers=2, **common | ||
| ) | ||
| par4 = _simulate_inputs( | ||
| monkeypatch, tmp_path, *models, "par4", parallel=True, n_workers=4, **common | ||
| ) | ||
|
|
||
| expected = list(range(8)) | ||
| assert sorted(serial) == expected | ||
| assert sorted(par2) == expected | ||
| assert sorted(par4) == expected | ||
| for index in expected: | ||
| assert serial[index] == par2[index], f"serial vs parallel(2) differ at {index}" | ||
| assert serial[index] == par4[index], f"serial vs parallel(4) differ at {index}" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you double check if after running this test, no files are left dangling on the test file system. If that is the case, using a
try-finallyblock to remove those (as done in a few otherMonteCarlotests) is an option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review. On the dangling files: I checked, and nothing gets left behind. The test passes
filename=tmp_path / tagintoMonteCarlo, so the.inputs.txt,.outputs.txtand.errors.txtall land under pytest's per-testtmp_pathand get cleaned up with it. I reran it and confirmed the repo and working directory stay clean, so there's nothing here for atry/finallyto remove.The other MonteCarlo tests need that cleanup because their fixture uses a fixed
filename="monte_carlo_test", which writes into the cwd. This one keeps everything insidetmp_pathinstead. I'm happy to match the try/finally style if you'd prefer consistency across the file, but the tmp_path route seemed cleaner to me. Your call.