Skip to content

Commit e8a1bd6

Browse files
committed
BUG: leave the rocket body's stream where it was, and isolate each collection
Moving the body to child zero broke every fixed-seed baseline for mass, radius and the body inputs, and nothing about the nested-component fix needed that. The body keeps the seed as given now: stochastic_calisto under seed 42 reads mass=14.906007947 on develop and the same here. Components were also addressed by one global traversal index, so adding a fin moved every motor, rail button, parachute and air brake. Each collection has a root of its own now, spawned from the same seed, so an unrelated component in one of them leaves the others where they were. The source scan compares the two sets both ways. A collection left in the reseed after create_object stops using it still spawns a child and moves every stream after it, which the subset check let through. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 7d5b404 commit e8a1bd6

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

‎rocketpy/stochastic/stochastic_rocket.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,28 @@ def _set_stochastic(self, seed=None):
189189
"""Set the stochastic attributes for Components, positions and
190190
inputs.
191191
192-
Each component takes its own child of a ``SeedSequence`` root, spawned
193-
in a fixed order, so components stay independent and one seed still
194-
reproduces the rocket.
192+
Each component takes its own child of its collection's root, so
193+
components stay independent and one seed still reproduces the rocket.
194+
The body keeps the seed as given, and a collection has a root of its
195+
own, so adding a fin does not move every parachute.
195196
196197
Parameters
197198
----------
198199
seed : int, optional
199200
Seed for the random number generator.
200201
"""
201-
root = np.random.SeedSequence(seed)
202-
super()._set_stochastic(_seed_sequence_to_int(root.spawn(1)[0]))
202+
super()._set_stochastic(seed)
203+
names = self._stochastic_collections()
204+
roots = dict(zip(names, np.random.SeedSequence(seed).spawn(len(names))))
203205
for name in self._POSITIONED_COLLECTIONS:
204-
setattr(self, name, self.__reset_components(getattr(self, name), root))
206+
setattr(
207+
self, name, self.__reset_components(getattr(self, name), roots[name])
208+
)
205209
for name in self._PLAIN_COLLECTIONS:
206210
for component in getattr(self, name):
207-
component._set_stochastic(_seed_sequence_to_int(root.spawn(1)[0]))
211+
component._set_stochastic(
212+
_seed_sequence_to_int(roots[name].spawn(1)[0])
213+
)
208214

209215
def __reset_components(self, components, root):
210216
"""Creates a new Components whose stochastic structures

‎tests/unit/stochastic/test_stochastic_rocket_seeding.py‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ def test_the_reseed_covers_every_collection_create_object_uses(stochastic_calist
102102
declared = set(type(rocket)._stochastic_collections())
103103

104104
assert iterated, "found no collections in create_object, so the scan is broken"
105-
assert iterated <= declared, (
106-
f"create_object samples these but the reseed never reaches them: "
107-
f"{sorted(iterated - declared)}"
108-
)
105+
# Both directions. One left in the reseed after create_object stopped using
106+
# it still spawns a child and moves every stream that follows.
107+
assert iterated == declared, {
108+
"sampled but never reseeded": sorted(iterated - declared),
109+
"reseeded but never sampled": sorted(declared - iterated),
110+
}
109111

110112

111113
def test_an_air_brake_answers_to_the_seed(
@@ -137,3 +139,23 @@ def drawn(seed):
137139

138140
assert drawn(1234) == first, "the same seed drew a different air brake"
139141
assert drawn(1235) != first, "a different seed drew the same air brake"
142+
143+
144+
def test_adding_a_surface_leaves_the_other_collections_alone(
145+
stochastic_calisto, calisto_main_chute, stochastic_nose_cone
146+
):
147+
"""Each collection has a root of its own, so an unrelated component in one
148+
of them does not move the streams in the others."""
149+
stochastic_calisto.parachutes = []
150+
stochastic_calisto.add_parachute(
151+
StochasticParachute(parachute=calisto_main_chute, cd_s=0.1, lag=0.2)
152+
)
153+
154+
def parachute_draw():
155+
stochastic_calisto._set_stochastic(5)
156+
return _drawn(stochastic_calisto.parachutes[0])
157+
158+
before = parachute_draw()
159+
stochastic_calisto.add_nose(stochastic_nose_cone, position=1.1)
160+
161+
assert parachute_draw() == before

0 commit comments

Comments
 (0)