17 def mock_evaluate(time_array, P_tank_O, P_tank_F, *args, **kwargs):
25 if drift_type ==
"stable":
26 decay = np.linspace(1.0, 0.76, n)
27 Pc_hist = Pc_initial * decay
28 elif drift_type ==
"extreme_drift":
29 decay = np.linspace(1.0, 0.40, n)
30 Pc_hist = Pc_initial * decay
32 Pc_hist = np.full(n, Pc_initial)
36 "F": np.full(n, 1000.0),
37 "mdot_O": np.full(n, 1.0),
38 "mdot_F": np.full(n, 0.5),
39 "MR": np.full(n, 2.0),
40 "stability_score": np.full(n, 0.9),
41 "chugging_stability_margin": np.full(n, 1.0)
59 print(
"\n=== Testing Layer 2 Chamber Pressure Constraint ===")
61 config_mock = MagicMock()
62 config_mock.ablative_cooling =
None
63 config_mock.graphite_insert =
None
66 with patch(
"engine.optimizer.layers.layer2_pressure.PintleEngineRunner")
as MockRunner, \
67 patch(
"engine.optimizer.layers.layer2_pressure.logging.getLogger")
as mock_get_logger:
69 mock_logger = MagicMock()
70 mock_get_logger.return_value = mock_logger
73 instance = MockRunner.return_value
75 instance.evaluate.return_value = {
"Pc": 3e6}
78 print(
"\n--- Test Case 1: Stable Pressure (Safe) ---")
82 def capture_case1(eval_num, obj, best):
83 objs_case1.append(obj)
87 optimized_config=config_mock,
88 initial_lox_pressure_pa=3e6,
89 initial_fuel_pressure_pa=2e6,
91 target_apogee_m=1000.0,
92 rocket_dry_mass_kg=100.0,
93 max_lox_tank_capacity_kg=50.0,
94 max_fuel_tank_capacity_kg=50.0,
95 target_burn_time=10.0,
99 objective_callback=capture_case1
106 for call
in mock_logger.info.call_args_list:
107 if "Pc_stab=0.00" in str(call):
112 print(
"PASS: Verified Pc_stab=0.00 for stable pressure.")
114 print(f
"FAIL: Did not find zero Pc penalty (Objs: {objs_case1}).")
117 print(
"\n--- Test Case 2: Extreme Drift (>25%) ---")
118 mock_logger.reset_mock()
122 def capture_case2(eval_num, obj, best):
123 objs_case2.append(obj)
127 optimized_config=config_mock,
128 initial_lox_pressure_pa=3e6,
129 initial_fuel_pressure_pa=2e6,
131 target_apogee_m=1000.0,
132 rocket_dry_mass_kg=100.0,
133 max_lox_tank_capacity_kg=50.0,
134 max_fuel_tank_capacity_kg=50.0,
135 target_burn_time=10.0,
138 optimal_of_ratio=2.0,
139 objective_callback=capture_case2
144 found_penalty =
False
151 if any(obj > 200.0
for obj
in objs_case2):
155 print(f
"PASS: Verified high Pc_stab penalty (Max obj: {max(objs_case2)}).")
158 for call
in mock_logger.info.call_args_list:
160 if "Pc_stab=" in msg
and "Pc_stab=0.00" not in msg:
163 print(
"PASS: Verified high Pc_stab penalty via logs.")
165 print(f
"FAIL: Did not find high Pc penalty (Objs: {objs_case2}).")
168 print(
"\n--- Test Case 3: Severe Pressure Drop (<75% of Initial) ---")
169 mock_logger.reset_mock()
173 def mock_generate_low_pressure(*args, **kwargs):
174 return np.full(20, 1.0e6)
177 def capture_case3(eval_num, obj, best):
178 objs_case3.append(obj)
180 with patch(
"engine.optimizer.layers.layer2_pressure.generate_pressure_curve_from_segments", side_effect=mock_generate_low_pressure):
183 optimized_config=config_mock,
184 initial_lox_pressure_pa=3e6,
185 initial_fuel_pressure_pa=3e6,
187 target_apogee_m=1000.0,
188 rocket_dry_mass_kg=100.0,
189 max_lox_tank_capacity_kg=50.0,
190 max_fuel_tank_capacity_kg=50.0,
191 target_burn_time=10.0,
194 optimal_of_ratio=2.0,
195 objective_callback=capture_case3
200 found_pruning =
False
206 if any(obj > 200.0
for obj
in objs_case3):
210 print(f
"PASS: Verified high penalty for low pressure (generation constraints bypassed by mock).")
212 print(f
"FAIL: Did not find high penalty for low pressure (Objs: {objs_case3}).")