Skip to content

Commit 74fc8ad

Browse files
authored
BUG: draw each declared eccentricity once per simulation (#1168)
#1167 declared the eccentricities that add_cp_eccentricity and add_thrust_eccentricity install, so dict_generator draws them. _create_eccentricities then drew them a second time and overwrote the first value. The exported inputs matched the applied ones only because the second write wins, and the extra draw moved every component position create_object places after it. Read what has been drawn already, and draw only a half the caller left out, which is not a declared input and so never reaches dict_generator. The docstring correction is the explanation #1167 landed with, which named the wrong symptom: the value did vary between simulations, what a fixed seed failed to do was reproduce it. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent df06506 commit 74fc8ad

2 files changed

Lines changed: 56 additions & 6 deletions

File tree

‎rocketpy/stochastic/stochastic_rocket.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,10 +734,16 @@ def _create_parachute(self, stochastic_parachute):
734734
return parachute
735735

736736
def _create_eccentricities(self, stochastic_x, stochastic_y, eccentricity):
737-
x_rnd = self._randomize_position(stochastic_x)
738-
self.last_rnd_dict[eccentricity + "_x"] = x_rnd
739-
y_rnd = self._randomize_position(stochastic_y)
740-
self.last_rnd_dict[eccentricity + "_y"] = y_rnd
737+
# A half that was given is a declared input, so dict_generator has drawn
738+
# it already; drawing again would spend a second value out of the same
739+
# stream and move every component position that follows.
740+
def drawn_once(name, stochastic):
741+
if name not in self.last_rnd_dict:
742+
self.last_rnd_dict[name] = self._randomize_position(stochastic)
743+
return self.last_rnd_dict[name]
744+
745+
x_rnd = drawn_once(eccentricity + "_x", stochastic_x)
746+
y_rnd = drawn_once(eccentricity + "_y", stochastic_y)
741747
return x_rnd, y_rnd
742748

743749
def create_object(self):

‎tests/unit/stochastic/test_stochastic_rocket.py‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ def test_an_eccentricity_added_after_init_is_still_drawn(calisto, add_them, name
212212
"""``dict_generator`` walks the declared inputs, and these arrive later.
213213
214214
The list is built in ``__init__``, so a distribution installed by an
215-
``add_*`` method afterwards was set on the instance and never drawn from:
216-
every simulation used the same value, with nothing to say so.
215+
``add_*`` method afterwards was never re-validated on a reseed and stayed
216+
bound to the unseeded generator: a fixed seed did not reproduce it.
217217
"""
218218
stochastic = StochasticRocket(rocket=calisto, radius=0.0127 / 2)
219219
getattr(stochastic, add_them)(x=(0.0, 0.001), y=(0.0, 0.001))
@@ -236,3 +236,47 @@ def drawn(seed):
236236

237237
assert drawn(7) == drawn(7)
238238
assert drawn(7) != drawn(8)
239+
240+
241+
def test_a_declared_eccentricity_is_not_drawn_a_second_time(calisto):
242+
"""``create_object`` applies the draw ``dict_generator`` already made.
243+
244+
A second draw spends another value out of the same stream, which moves
245+
every component position ``create_object`` places after it.
246+
"""
247+
stochastic = StochasticRocket(rocket=calisto, radius=0.0127 / 2)
248+
stochastic.add_cp_eccentricity(x=(0.0, 0.01), y=(0.0, 0.01))
249+
stochastic.add_thrust_eccentricity(x=(0.0, 0.01), y=(0.0, 0.01))
250+
251+
stochastic._set_stochastic(42)
252+
declared = next(stochastic.dict_generator())
253+
expected = {name: declared[name] for name in declared if "eccentricity" in name}
254+
assert len(expected) == 4
255+
256+
stochastic._set_stochastic(42)
257+
rocket = stochastic.create_object()
258+
259+
applied = {
260+
"cp_eccentricity_x": rocket.cp_eccentricity_x,
261+
"cp_eccentricity_y": rocket.cp_eccentricity_y,
262+
"thrust_eccentricity_x": rocket.thrust_eccentricity_x,
263+
"thrust_eccentricity_y": rocket.thrust_eccentricity_y,
264+
}
265+
assert applied == expected
266+
assert {name: stochastic.last_rnd_dict[name] for name in expected} == expected
267+
268+
269+
def test_an_eccentricity_half_that_was_left_out_is_still_drawn(calisto):
270+
"""Only a half that was given is a declared input, so the other is not.
271+
272+
``create_object`` has to keep drawing it, and keep reporting it, or the
273+
inputs it writes stop describing the rocket it built.
274+
"""
275+
stochastic = StochasticRocket(rocket=calisto, radius=0.0127 / 2)
276+
stochastic.add_cp_eccentricity(x=(0.0, 0.01))
277+
278+
stochastic._set_stochastic(42)
279+
rocket = stochastic.create_object()
280+
281+
assert "cp_eccentricity_y" in stochastic.last_rnd_dict
282+
assert stochastic.last_rnd_dict["cp_eccentricity_y"] == rocket.cp_eccentricity_y

0 commit comments

Comments
 (0)