修复test_mql事件后积分步长控制

This commit is contained in:
huojiarong committed 2026-07-22 02:58:59 +00:00
1 parent a0855018d5
commit 2120901909
12 files changed
+222 -27

No files matched your search

+25
View File
@@ -79,6 +79,31 @@ class AmesimPneumaticComponentsTest(unittest.TestCase):
)
self.assertAlmostEqual(volume.port_a.p, volume.port_b.p)
def test_volume_applies_amesim_heat_exchange_to_energy_derivative(self) -> None:
volume = AmesimPneumaticVolume.from_liters(
name="heated_chamber",
volume_liters=15.0,
p0=100000.0,
T0=300.0,
heat_transfer_coefficient=1500.0,
heat_transfer_area=0.7,
external_temperature_k=293.15,
)
props = volume.properties()
derivative = volume.derivatives_from_two_connections(
port_a_m_flow=0.0,
connected_h_a=props.h,
port_b_m_flow=0.0,
connected_h_b=props.h,
internal_h=props.h,
)
self.assertAlmostEqual(
derivative.U,
1500.0 * 0.7 * (293.15 - 300.0),
)
def test_variable_volume_tracks_external_volume(self) -> None:
volume = AmesimVariablePneumaticVolume.from_liters(
name="pn_c1_8",
+2 -1
View File
@@ -63,7 +63,8 @@ class AmesimPnl0001PipeTests(unittest.TestCase):
self.assertAlmostEqual(derivative.m, 0.1)
self.assertAlmostEqual(
derivative.U,
0.2 * (internal.h + 1000.0) - 0.1 * internal.h,
0.2 * (internal.h + 1000.0) / self.pipe.gas.gamma
- 0.1 * internal.u,
)
+68
View File
@@ -0,0 +1,68 @@
import sys
import types
import unittest
from unittest.mock import patch
from PythonModels.core.solver import SolveIVPConfig, integrate_ode
class IntegrateOdeTests(unittest.TestCase):
def test_scipy_solver_receives_step_size_controls(self) -> None:
calls: list[dict[str, object]] = []
def fake_solve_ivp(**kwargs):
calls.append(kwargs)
return object()
scipy_module = types.ModuleType("scipy")
integrate_module = types.ModuleType("scipy.integrate")
integrate_module.solve_ivp = fake_solve_ivp
scipy_module.integrate = integrate_module
with patch.dict(
sys.modules,
{"scipy": scipy_module, "scipy.integrate": integrate_module},
):
integrate_ode(
rhs=lambda _time, state: state,
initial_state=[1.0],
config=SolveIVPConfig(
t_start=0.0,
t_stop=1.0,
max_step=1.0e-4,
first_step=1.0e-8,
),
t_eval=[0.0, 1.0],
)
self.assertEqual(calls[0]["max_step"], 1.0e-4)
self.assertEqual(calls[0]["first_step"], 1.0e-8)
def test_scipy_solver_omits_unset_first_step(self) -> None:
calls: list[dict[str, object]] = []
def fake_solve_ivp(**kwargs):
calls.append(kwargs)
return object()
scipy_module = types.ModuleType("scipy")
integrate_module = types.ModuleType("scipy.integrate")
integrate_module.solve_ivp = fake_solve_ivp
scipy_module.integrate = integrate_module
with patch.dict(
sys.modules,
{"scipy": scipy_module, "scipy.integrate": integrate_module},
):
integrate_ode(
rhs=lambda _time, state: state,
initial_state=[1.0],
config=SolveIVPConfig(t_start=0.0, t_stop=1.0),
)
self.assertEqual(calls[0]["max_step"], 1.0e-3)
self.assertNotIn("first_step", calls[0])
if __name__ == "__main__":
unittest.main()
@@ -89,7 +89,8 @@ class RunTestMqlFullStateComparisonScriptTests(unittest.TestCase):
chamber_diagnostic.energy_derivative_w,
chamber_diagnostic.port_a_energy_flow_w
+ chamber_diagnostic.port_b_energy_flow_w
+ chamber_diagnostic.boundary_work_w,
+ chamber_diagnostic.boundary_work_w
+ chamber_diagnostic.thermal_energy_flow_w,
)
self.assertTrue(csv_summary_exists)
self.assertIn("Mode: Python 132 full-state closure comparison", summary)
@@ -112,6 +113,7 @@ class RunTestMqlFullStateComparisonScriptTests(unittest.TestCase):
self.assertIn("piston_alias=pn_brp2_8", summary)
self.assertIn("mass_derivative_kg_s=", summary)
self.assertIn("boundary_work_w=", summary)
self.assertIn("thermal_energy_flow_w=", summary)
self.assertIn("final_python=", summary)
self.assertIn("final_amesim=", summary)
self.assertIn("python.press@pn_c1_8", csv_header)
+3
View File
@@ -55,6 +55,9 @@ class TestMqlPneumaticAssemblyTests(unittest.TestCase):
self.assertAlmostEqual(fixed_chamber.volume, 0.057)
self.assertAlmostEqual(variable_chamber.dead_volume, 0.015)
self.assertAlmostEqual(variable_chamber.volume, 0.015)
self.assertAlmostEqual(variable_chamber.heat_transfer_coefficient, 1500.0)
self.assertAlmostEqual(variable_chamber.heat_transfer_area, 0.7)
self.assertAlmostEqual(variable_chamber.external_temperature, 293.15)
self.assertIs(fixed_chamber.gas, HELIUM_PNEUMATIC_GAS)
self.assertIs(variable_chamber.gas, HELIUM_PNEUMATIC_GAS)
self.assertAlmostEqual(fixed_chamber.properties().T, 293.15)
+23 -1
View File
@@ -709,7 +709,8 @@ class TestMqlPn3P4NodeChamberSegmentTests(unittest.TestCase):
diagnostic.energy_derivative_w,
diagnostic.port_a_energy_flow_w
+ diagnostic.port_b_energy_flow_w
+ diagnostic.boundary_work_w,
+ diagnostic.boundary_work_w
+ diagnostic.thermal_energy_flow_w,
)
def test_full_state_closure_applies_step_controls_to_variable_orifices(self) -> None:
@@ -731,6 +732,27 @@ class TestMqlPn3P4NodeChamberSegmentTests(unittest.TestCase):
open_snapshot.p4_port3_remote_node_to_primary_line_flow,
0.0,
)
node_inlet_h = (
open_snapshot.p4_port3_remote_balance.port_2_enthalpy_flow_w
/ open_snapshot.p4_port3_remote_node_to_primary_line_flow
)
self.assertAlmostEqual(
node_inlet_h,
open_snapshot.p4_port3_remote_orifice_line_port_2.h,
)
rhs = closure.rhs_at(0.04, state)
line = closure.pneumatic_closure.components.p4_port3_remote_primary_line
expected_line_derivative = line.derivatives_from_connections(
port_1_m_flow=(
open_snapshot.p4_port3_remote_chamber_to_line_flow
),
connected_h_1=open_snapshot.p4_port3_remote_primary_chamber.h,
port_2_m_flow=(
open_snapshot.p4_port3_remote_node_to_primary_line_flow
),
connected_h_2=node_inlet_h,
)
self.assertAlmostEqual(rhs[45], expected_line_derivative.U)
self.assertAlmostEqual(
self.system.pneumatic_assembly.variable_orifices[
"pn_morifice_1"