|
9 | 9 | import pytest |
10 | 10 |
|
11 | 11 | from rocketpy.simulation.helpers.dynamics import ( |
| 12 | + BUILT_IN_DYNAMICS, |
12 | 13 | CANONICAL_INDEX, |
13 | 14 | CANONICAL_STATE_NAMES, |
14 | 15 | FULL_POST_PROCESS_VARS, |
@@ -514,6 +515,43 @@ def test_from_dict_unknown_dynamics_name_tolerated(): |
514 | 515 | assert restored["vz"][:, 1].tolist() == [0.0] |
515 | 516 |
|
516 | 517 |
|
| 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 | + |
517 | 555 | def test_phase_starts_that_disagree_with_the_rows_are_rejected(): |
518 | 556 | phase = _PhaseSolution(SIX_DOF_DYNAMICS, tuple([0.0] * 13), start=0) |
519 | 557 | later = _PhaseSolution(SIX_DOF_DYNAMICS, tuple([0.0] * 13), start=9) |
|
0 commit comments