Skip to content

Commit 0a1a5a4

Browse files
committed
BUG: fly the parachute the simulation sampled
StochasticParachute.create_object() builds a Parachute from a complete draw. StochasticRocket.create_object() threw it away and built a second one from six of its ten fields, so radius, height, porosity and the drag coefficient never left last_rnd_dict. Parachute re-derived radius from cd_s and the default drag coefficient, and height fell back to that radius. Randomizing the geometry was not required to hit it. A parachute configured with an explicit radius of 2.0 flew 1.519 in every simulation, because the value was dropped on the way to add_parachute rather than sampled away. Attaching the object _create_parachute already built fixes both, and stops Parachute.__init__ running twice per parachute per simulation. The second run drew the initial pressure noise from the global NumPy RNG again (#1091). Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent f40f18e commit 0a1a5a4

2 files changed

Lines changed: 104 additions & 9 deletions

File tree

‎rocketpy/stochastic/stochastic_rocket.py‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -794,14 +794,9 @@ def create_object(self):
794794
)
795795

796796
for parachute in self.parachutes:
797-
parachute = self._create_parachute(parachute)
798-
rocket.add_parachute(
799-
name=parachute.name,
800-
cd_s=parachute.cd_s,
801-
trigger=parachute.trigger,
802-
sampling_rate=parachute.sampling_rate,
803-
lag=parachute.lag,
804-
noise=parachute.noise,
805-
)
797+
# The sampled object itself. Passing a subset of its fields back to
798+
# add_parachute built a second one, which re-derived radius and
799+
# height from cd_s and the default drag coefficient.
800+
rocket.parachutes.append(self._create_parachute(parachute))
806801

807802
return rocket

‎tests/unit/stochastic/test_stochastic_rocket.py‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from rocketpy.rocket.parachute import Parachute
12
from rocketpy.rocket.rocket import Rocket
3+
from rocketpy.stochastic import StochasticParachute, StochasticRocket
24

35

46
def test_str(stochastic_calisto):
@@ -23,3 +25,101 @@ class creates a StochasticCalisto object from the randomly generated
2325
"""
2426
obj = stochastic_calisto.create_object()
2527
assert isinstance(obj, Rocket)
28+
29+
30+
def test_sampled_parachute_geometry_reaches_the_created_rocket(
31+
stochastic_calisto, calisto_main_chute
32+
):
33+
"""The sampled Parachute used to be discarded and a second one built from
34+
six of its ten fields, so radius, height, porosity and the drag coefficient
35+
never left `last_rnd_dict`. Parachute re-derived radius from cd_s and the
36+
default drag coefficient, and height fell back to that radius.
37+
"""
38+
stochastic_calisto.parachutes = []
39+
stochastic_calisto.add_parachute(
40+
StochasticParachute(
41+
parachute=calisto_main_chute,
42+
cd_s=0.1,
43+
radius=0.3,
44+
height=0.2,
45+
porosity=0.01,
46+
drag_coefficient=0.2,
47+
)
48+
)
49+
stochastic_calisto._set_stochastic(42)
50+
51+
rocket = stochastic_calisto.create_object()
52+
53+
built = rocket.parachutes[0]
54+
sampled = stochastic_calisto.last_rnd_dict["parachutes"][0]
55+
for field in ("cd_s", "radius", "height", "porosity", "drag_coefficient"):
56+
assert getattr(built, field) == sampled[field], (
57+
f"the rocket flies a {field} the run never sampled"
58+
)
59+
60+
61+
def test_the_parachute_is_attached_exactly_once(
62+
stochastic_calisto, stochastic_main_parachute, stochastic_drogue_parachute
63+
):
64+
"""The control. Without it the test above would pass on a create_object
65+
that attaches nothing at all, which would silently fly every Monte Carlo
66+
rocket without its parachutes."""
67+
stochastic_calisto.parachutes = []
68+
for parachute in (stochastic_main_parachute, stochastic_drogue_parachute):
69+
stochastic_calisto.add_parachute(parachute)
70+
stochastic_calisto._set_stochastic(42)
71+
72+
rocket = stochastic_calisto.create_object()
73+
74+
assert len(rocket.parachutes) == 2
75+
assert [p.name for p in rocket.parachutes] == [
76+
stochastic_main_parachute.obj.name,
77+
stochastic_drogue_parachute.obj.name,
78+
]
79+
80+
81+
def test_a_parachute_is_built_once_per_simulation(
82+
stochastic_calisto, stochastic_main_parachute, monkeypatch
83+
):
84+
"""Building it twice drew the initial pressure noise from the global NumPy
85+
RNG twice, which is state no seed here controls. See #1091."""
86+
built = []
87+
real = Parachute.__init__
88+
89+
def counting(self, *args, **kwargs):
90+
built.append(self)
91+
return real(self, *args, **kwargs)
92+
93+
stochastic_calisto.parachutes = []
94+
stochastic_calisto.add_parachute(stochastic_main_parachute)
95+
stochastic_calisto._set_stochastic(42)
96+
monkeypatch.setattr(Parachute, "__init__", counting)
97+
98+
stochastic_calisto.create_object()
99+
100+
assert len(built) == 1
101+
102+
103+
def test_configured_geometry_survives_without_being_randomized(calisto_robust):
104+
"""The wider case. Dropping the four fields did not need anyone to
105+
randomize them: a parachute built with an explicit radius flew a radius
106+
re-derived from cd_s instead, in every Monte Carlo simulation."""
107+
chute = calisto_robust.add_parachute(
108+
"geometric",
109+
cd_s=10.0,
110+
trigger="apogee",
111+
sampling_rate=105,
112+
lag=1.5,
113+
radius=2.0,
114+
height=1.5,
115+
porosity=0.05,
116+
drag_coefficient=1.4,
117+
)
118+
stochastic = StochasticRocket(rocket=calisto_robust, mass=(14.426, 0.5))
119+
stochastic.parachutes = []
120+
stochastic.add_parachute(StochasticParachute(chute, cd_s=(10.0, 0.5)))
121+
stochastic._set_stochastic(42)
122+
123+
flown = stochastic.create_object().parachutes[0]
124+
125+
assert (flown.radius, flown.height, flown.porosity) == (2.0, 1.5, 0.05)

0 commit comments

Comments
 (0)