修复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

+52 -3
View File
@@ -82,12 +82,24 @@ class AmesimPneumaticVolume(DynamicComponent):
gas: AmesimPneumaticGas = HELIUM_PNEUMATIC_GAS,
p0: float = 101_325.0,
T0: float = 293.15,
heat_transfer_coefficient: float = 0.0,
heat_transfer_area: float = 0.0,
external_temperature_k: float = 293.15,
) -> None:
if volume <= 0.0:
raise ValueError("volume must be positive.")
if heat_transfer_coefficient < 0.0:
raise ValueError("heat_transfer_coefficient must be non-negative.")
if heat_transfer_area < 0.0:
raise ValueError("heat_transfer_area must be non-negative.")
if external_temperature_k <= 0.0:
raise ValueError("external_temperature_k must be positive.")
super().__init__(name=name)
self.volume = volume
self.gas = gas
self.heat_transfer_coefficient = heat_transfer_coefficient
self.heat_transfer_area = heat_transfer_area
self.external_temperature = external_temperature_k
rho0 = gas.density(p0, T0)
m0 = rho0 * volume
U0 = m0 * gas.specific_internal_energy(T0)
@@ -103,8 +115,20 @@ class AmesimPneumaticVolume(DynamicComponent):
gas: AmesimPneumaticGas = HELIUM_PNEUMATIC_GAS,
p0: float = 101_325.0,
T0: float = 293.15,
heat_transfer_coefficient: float = 0.0,
heat_transfer_area: float = 0.0,
external_temperature_k: float = 293.15,
) -> "AmesimPneumaticVolume":
return cls(name=name, volume=liters_to_m3(volume_liters), gas=gas, p0=p0, T0=T0)
return cls(
name=name,
volume=liters_to_m3(volume_liters),
gas=gas,
p0=p0,
T0=T0,
heat_transfer_coefficient=heat_transfer_coefficient,
heat_transfer_area=heat_transfer_area,
external_temperature_k=external_temperature_k,
)
def get_state_vector(self) -> list[float]:
return self.state.as_vector()
@@ -118,6 +142,14 @@ class AmesimPneumaticVolume(DynamicComponent):
def volume_rate_m3_s(self) -> float:
return 0.0
def thermal_energy_flow_w(self, temperature_k: float | None = None) -> float:
temperature = self.properties().T if temperature_k is None else temperature_k
return (
self.heat_transfer_coefficient
* self.heat_transfer_area
* (self.external_temperature - temperature)
)
def gas_mass_g(self) -> float:
return kg_to_g(self.state.m)
@@ -139,7 +171,10 @@ class AmesimPneumaticVolume(DynamicComponent):
return ThermodynamicProperties(p=p, T=T, rho=rho, u=u, h=h)
def derivatives(self, inlet_h: float, m_flow: float) -> VolumeState:
return VolumeState(m=m_flow, U=m_flow * inlet_h)
return VolumeState(
m=m_flow,
U=m_flow * inlet_h + self.thermal_energy_flow_w(),
)
def derivatives_from_two_connections(
self,
@@ -151,6 +186,7 @@ class AmesimPneumaticVolume(DynamicComponent):
internal_h: float,
volume_rate_m3_s: float | None = None,
) -> VolumeState:
properties = self.properties()
inlet_h_a = self.connection_inlet_enthalpy(
port_m_flow=port_a_m_flow,
connected_h=connected_h_a,
@@ -166,7 +202,8 @@ class AmesimPneumaticVolume(DynamicComponent):
U=(
port_a_m_flow * inlet_h_a
+ port_b_m_flow * inlet_h_b
- self.properties().p * (
+ self.thermal_energy_flow_w(properties.T)
- properties.p * (
self.volume_rate_m3_s()
if volume_rate_m3_s is None
else volume_rate_m3_s
@@ -186,6 +223,9 @@ class AmesimVariablePneumaticVolume(AmesimPneumaticVolume):
p0: float = 101_325.0,
T0: float = 293.15,
external_volume: float = 0.0,
heat_transfer_coefficient: float = 0.0,
heat_transfer_area: float = 0.0,
external_temperature_k: float = 293.15,
) -> None:
if dead_volume <= 0.0:
raise ValueError("dead_volume must be positive.")
@@ -200,6 +240,9 @@ class AmesimVariablePneumaticVolume(AmesimPneumaticVolume):
gas=gas,
p0=p0,
T0=T0,
heat_transfer_coefficient=heat_transfer_coefficient,
heat_transfer_area=heat_transfer_area,
external_temperature_k=external_temperature_k,
)
@classmethod
@@ -211,6 +254,9 @@ class AmesimVariablePneumaticVolume(AmesimPneumaticVolume):
p0: float = 101_325.0,
T0: float = 293.15,
external_volume_liters: float = 0.0,
heat_transfer_coefficient: float = 0.0,
heat_transfer_area: float = 0.0,
external_temperature_k: float = 293.15,
) -> "AmesimVariablePneumaticVolume":
return cls(
name=name,
@@ -219,6 +265,9 @@ class AmesimVariablePneumaticVolume(AmesimPneumaticVolume):
p0=p0,
T0=T0,
external_volume=liters_to_m3(external_volume_liters),
heat_transfer_coefficient=heat_transfer_coefficient,
heat_transfer_area=heat_transfer_area,
external_temperature_k=external_temperature_k,
)
def volume_rate_m3_s(self) -> float:
@@ -48,7 +48,7 @@ class _DarcyPipeResistanceMixin:
if upper > 1.0e3:
raise ValueError("unable to bracket pneumatic pipe resistance flow")
lower = 0.0
for _ in range(80):
for _ in range(48):
middle = 0.5 * (lower + upper)
if self._darcy_pressure_drop(
middle,
@@ -255,15 +255,19 @@ class AmesimPnl0001Pipe(_DarcyPipeResistanceMixin, DynamicComponent):
connected_h_2: float,
) -> VolumeState:
internal = self.properties()
inlet_h_1 = self.connection_inlet_enthalpy(
port_m_flow=port_1_m_flow,
connected_h=connected_h_1,
internal_h=internal.h,
# PNL0001 is a fixed-volume distributed line store. Its transported
# energy variable therefore follows specific internal energy, not the
# chamber-style stagnation enthalpy contract. For this ideal gas,
# h = gamma * u. Outflow always carries the local u.
inlet_u_1 = (
connected_h_1 / self.gas.gamma
if port_1_m_flow > 0.0
else internal.u
)
inlet_h_2 = self.connection_inlet_enthalpy(
port_m_flow=port_2_m_flow,
connected_h=connected_h_2,
internal_h=internal.h,
inlet_u_2 = (
connected_h_2 / self.gas.gamma
if port_2_m_flow > 0.0
else internal.u
)
heat_flow = (
self.heat_transfer_coefficient
@@ -272,7 +276,7 @@ class AmesimPnl0001Pipe(_DarcyPipeResistanceMixin, DynamicComponent):
)
return VolumeState(
m=port_1_m_flow + port_2_m_flow,
U=port_1_m_flow * inlet_h_1 + port_2_m_flow * inlet_h_2 + heat_flow,
U=port_1_m_flow * inlet_u_1 + port_2_m_flow * inlet_u_2 + heat_flow,
)
+15 -10
View File
@@ -10,8 +10,9 @@ class SolveIVPConfig:
t_stop: float = 20.0
method: str = "BDF"
rtol: float = 1e-6
atol: float = 1e-8
atol: float = 1e-10
max_step: float = 1e-3
first_step: float | None = None
@dataclass(frozen=True)
@@ -91,12 +92,16 @@ def integrate_ode(
except ImportError:
return _runge_kutta_4(rhs, initial_state, config, t_eval)
return solve_ivp(
fun=rhs,
t_span=(config.t_start, config.t_stop),
y0=initial_state,
method=config.method,
rtol=config.rtol,
atol=config.atol,
t_eval=t_eval,
)
solve_options = {
"fun": rhs,
"t_span": (config.t_start, config.t_stop),
"y0": initial_state,
"method": config.method,
"rtol": config.rtol,
"atol": config.atol,
"max_step": config.max_step,
"t_eval": t_eval,
}
if config.first_step is not None:
solve_options["first_step"] = config.first_step
return solve_ivp(**solve_options)
@@ -559,6 +559,8 @@ def format_test_mql_full_state_comparison_summary(
f" - mass_derivative_kg_s={chamber_diagnostic.mass_derivative_kg_s}",
f" - port_a_energy_flow_w={chamber_diagnostic.port_a_energy_flow_w}",
f" - boundary_work_w={chamber_diagnostic.boundary_work_w}",
f" - thermal_energy_flow_w="
f"{chamber_diagnostic.thermal_energy_flow_w}",
f" - energy_derivative_w={chamber_diagnostic.energy_derivative_w}",
]
)
+9 -1
View File
@@ -3870,6 +3870,7 @@ class TestMqlVariableChamberRhsDiagnostic:
port_a_energy_flow_w: float
port_b_energy_flow_w: float
boundary_work_w: float
thermal_energy_flow_w: float
energy_derivative_w: float
@@ -4120,6 +4121,9 @@ class TestMqlFullStateClosure:
port_a_energy_flow = port_a_mass_flow * port_a_inlet_h
port_b_energy_flow = port_b_mass_flow * port_b_inlet_h
boundary_work = -chamber_properties.p * chamber.volume_rate_m3_s()
thermal_energy_flow = chamber.thermal_energy_flow_w(
chamber_properties.T
)
return TestMqlVariableChamberRhsDiagnostic(
chamber_alias=chamber_alias,
piston_alias=piston_alias,
@@ -4133,8 +4137,12 @@ class TestMqlFullStateClosure:
port_a_energy_flow_w=port_a_energy_flow,
port_b_energy_flow_w=port_b_energy_flow,
boundary_work_w=boundary_work,
thermal_energy_flow_w=thermal_energy_flow,
energy_derivative_w=(
port_a_energy_flow + port_b_energy_flow + boundary_work
port_a_energy_flow
+ port_b_energy_flow
+ boundary_work
+ thermal_energy_flow
),
)
@@ -229,6 +229,9 @@ def _build_chamber(
gas=gas,
p0=initial_pressure_pa,
T0=_component_temperature(component),
heat_transfer_coefficient=component.parameter_value("kth"),
heat_transfer_area=component.parameter_value("sth"),
external_temperature_k=_component_temperature(component),
)
return AmesimPneumaticVolume.from_liters(
name=component.alias,
@@ -236,6 +239,9 @@ def _build_chamber(
gas=gas,
p0=initial_pressure_pa,
T0=_component_temperature(component),
heat_transfer_coefficient=component.parameter_value("kth"),
heat_transfer_area=component.parameter_value("sth"),
external_temperature_k=_component_temperature(component),
)