Skip to content

Commit dd0d9ed

Browse files
MateusStanoclaude
andcommitted
BUG: rebuild a loaded phase's canonical states from its dynamics
A saved phase kept only its dynamics' name and state names, and loading rebuilt it as bare dynamics without its to_canonical rule. A phase that rebuilds canonical states from its own then read them back frozen at their start-of-phase values. Keep a table of the dynamics RocketPy ships and use the saved name to get them back, as long as their states still match. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 0a72660 commit dd0d9ed

4 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎rocketpy/simulation/helpers/dynamics.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,16 @@ def __repr__(self):
456456
CANONICAL_STATE_NAMES,
457457
PARACHUTE_POST_PROCESS_VARS,
458458
)
459+
460+
# Every kind of phase RocketPy ships, by name, so a saved flight gets back the
461+
# dynamics each of its phases was flown with. Add new dynamics here.
462+
BUILT_IN_DYNAMICS = {
463+
dynamics.name: dynamics
464+
for dynamics in (
465+
RAIL_DYNAMICS,
466+
SOLID_PROPULSION_DYNAMICS,
467+
SIX_DOF_DYNAMICS,
468+
THREE_DOF_DYNAMICS,
469+
PARACHUTE_DYNAMICS,
470+
)
471+
}

‎rocketpy/simulation/solution.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44

55
from .helpers.dynamics import (
6+
BUILT_IN_DYNAMICS,
67
CANONICAL_INDEX,
78
CANONICAL_STATE_NAMES,
89
SIX_DOF_DYNAMICS,
@@ -258,12 +259,18 @@ def from_dict(cls, data):
258259
the dynamics to a flight needs a running simulation, so the phase
259260
cannot be post-processed again.
260261
"""
261-
return cls(
262-
# A derivative is code and cannot be saved, so the phase keeps its
263-
# name and states but cannot be flown or post-processed again.
264-
_PhaseDynamics(
262+
# The dynamics is code, so it was saved by name. A built-in one is
263+
# used again, so the canonical states it rebuilds are rebuilt here too.
264+
dynamics = BUILT_IN_DYNAMICS.get(data.get("dynamics"))
265+
if dynamics is None or list(dynamics.states) != data["state_names"]:
266+
# Unknown, or changed since the flight was saved: its own states
267+
# can still be read by name, and every other canonical state is
268+
# held at its start-of-phase value.
269+
dynamics = _PhaseDynamics(
265270
data.get("dynamics") or "unknown", None, data["state_names"]
266-
),
271+
)
272+
return cls(
273+
dynamics,
267274
data.get("start_canonical"),
268275
t_start=data.get("t_start"),
269276
name=data.get("name"),

‎tests/unit/simulation/test_dynamics.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import pytest
88

99
from rocketpy.simulation.flight import Flight
10+
from rocketpy.simulation.helpers import dynamics as dynamics_module
1011
from rocketpy.simulation.helpers.dynamics import (
12+
BUILT_IN_DYNAMICS,
1113
CANONICAL_STATE_NAMES,
1214
FULL_POST_PROCESS_VARS,
1315
PARACHUTE_DYNAMICS,
@@ -82,6 +84,16 @@ def test_dynamics_names_and_states():
8284
assert PARACHUTE_DYNAMICS.post_process_vars == ("ax", "ay", "az", "R1", "R2", "R3")
8385

8486

87+
def test_every_shipped_dynamics_is_found_by_name():
88+
"""A saved flight finds its dynamics by name, so none may be left out."""
89+
shipped = [
90+
value
91+
for value in vars(dynamics_module).values()
92+
if isinstance(value, _PhaseDynamics)
93+
]
94+
assert {dynamics.name: dynamics for dynamics in shipped} == BUILT_IN_DYNAMICS
95+
96+
8597
# ---------------------------------------------------------------------------
8698
# Canonicalization
8799
# ---------------------------------------------------------------------------

‎tests/unit/simulation/test_solution.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytest
1010

1111
from rocketpy.simulation.helpers.dynamics import (
12+
BUILT_IN_DYNAMICS,
1213
CANONICAL_INDEX,
1314
CANONICAL_STATE_NAMES,
1415
FULL_POST_PROCESS_VARS,
@@ -514,6 +515,43 @@ def test_from_dict_unknown_dynamics_name_tolerated():
514515
assert restored["vz"][:, 1].tolist() == [0.0]
515516

516517

518+
def test_from_dict_finds_built_in_dynamics_by_name():
519+
solution = build_mixed_solution()
520+
restored = Solution.from_dict(solution.to_dict())
521+
assert restored.phases[0].dynamics is SIX_DOF_DYNAMICS
522+
523+
524+
def test_from_dict_ignores_a_built_in_whose_states_changed():
525+
"""A built-in phase saved with other states than it has now is not used."""
526+
data = build_mixed_solution().to_dict()
527+
data["phases"][1]["dynamics"] = "six_dof"
528+
restored = Solution.from_dict(data)
529+
assert restored.phases[1].dynamics is not SIX_DOF_DYNAMICS
530+
assert restored.phases[1].dynamics.states == DESCENT_DYNAMICS.states
531+
532+
533+
def test_rebuilt_canonical_states_survive_save_and_load(monkeypatch):
534+
"""A phase with states of its own rebuilds the canonical ones after loading."""
535+
536+
def to_canonical(values):
537+
values["z"], values["vz"] = values["h"], values["v"]
538+
return [values[name] for name in CANONICAL_STATE_NAMES]
539+
540+
altitude = _PhaseDynamics(
541+
"altitude", stub_derivative, ("h", "v"), to_canonical=to_canonical
542+
)
543+
monkeypatch.setitem(BUILT_IN_DYNAMICS, "altitude", altitude)
544+
solution = Solution()
545+
solution._start_phase(altitude, start_canonical=tuple([0.0] * 13))
546+
solution._append([0.0, 100.0, 50.0])
547+
solution._append([1.0, 150.0, 40.0])
548+
549+
restored = Solution.from_dict(solution.to_dict())
550+
assert restored.phases[0].dynamics is altitude
551+
assert restored["z"][:, 1].tolist() == [100.0, 150.0]
552+
assert np.array_equal(restored.canonical_array, solution.canonical_array)
553+
554+
517555
def test_phase_starts_that_disagree_with_the_rows_are_rejected():
518556
phase = _PhaseSolution(SIX_DOF_DYNAMICS, tuple([0.0] * 13), start=0)
519557
later = _PhaseSolution(SIX_DOF_DYNAMICS, tuple([0.0] * 13), start=9)

0 commit comments

Comments
 (0)