STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_layer1_validation_refusal.py
Go to the documentation of this file.
1"""A design whose validation replay the chamber solver refuses is invalid, not a crash.
2
3The chamber solver says a solve did not close in two ways: a ``ValueError`` from the
4root find, and a ``RuntimeError`` from its own checks on the root it found ("Solution
5validation failed: Isp is non-positive"). Layer 1's validation replay caught only the
6first, so a smoke search whose last candidate replayed onto a non-physical root --
7which is what CI's runners produced, and this machine did not -- took the whole
8optimizer down. Here the replay is made to land on that refusal deliberately.
9"""
10
11from __future__ import annotations
12
13import copy
14from pathlib import Path
15
16import pytest
17
18ROOT = Path(__file__).resolve().parents[1]
19REFUSAL = "Solution validation failed:\nIsp is non-positive: -78.52 s"
20
21
22@pytest.fixture(scope="module")
23def cfg():
24 from engine.pipeline.io import load_config
25
26 return load_config(str(ROOT / "configs/canonical/impinging.yaml"))
27
28
29def _smoke(cfg, monkeypatch, error: Exception):
31 from engine.core.runner import PintleEngineRunner
32
33 validating = {"on": False}
34
35 def progress(stage: str, _fraction: float, _message: str) -> None:
36 if stage == "Layer 1: Validation":
37 validating["on"] = True
38
39 real = PintleEngineRunner.evaluate
40
41 def evaluate(self, *a, **k):
42 if validating["on"]:
43 raise error
44 return real(self, *a, **k)
45
46 monkeypatch.setattr(PintleEngineRunner, "evaluate", evaluate)
47 monkeypatch.setattr(L1, "_get_num_workers", lambda c: 1)
48 base = copy.deepcopy(cfg)
49 req = base.design_requirements.model_dump()
50 req["layer1_random_seed"] = 0
51 pcfg = {
52 "mode": "optimizer_controlled",
53 "max_lox_pressure_psi": float(req["max_lox_tank_pressure_psi"]),
54 "max_fuel_pressure_psi": float(req["max_fuel_tank_pressure_psi"]),
55 }
56 _, results = L1.run_layer1_optimization(
57 copy.deepcopy(base),
58 PintleEngineRunner(copy.deepcopy(base)),
59 req,
60 target_burn_time=float(req.get("target_burn_time", 6.0)),
61 tolerances={"thrust": 0.10, "apogee": 0.15},
62 pressure_config=pcfg,
63 update_progress=progress,
64 layer1_smoke=True,
65 layer1_max_iterations=1,
66 layer1_cma_restarts=1,
67 )
68 assert validating["on"], "the run never reached validation"
69 return results["performance"]
70
71
73 perf = _smoke(cfg, monkeypatch, RuntimeError(REFUSAL))
74 assert perf["layer1_validation_used_last_good_bundle"] is True
75 assert perf["validation_replay_ok"] is False
76 assert perf["pressure_candidate_valid"] is False
77 assert any("Isp is non-positive" in r for r in perf["failure_reasons"])
78
79
81 """Only the solver's own refusals mean "this design does not close"; a bug is a bug."""
82 with pytest.raises(RuntimeError, match="something else entirely"):
83 _smoke(cfg, monkeypatch, RuntimeError("something else entirely"))
test_a_refused_replay_reports_the_design_invalid(cfg, monkeypatch)
_smoke(cfg, monkeypatch, Exception error)