修正test_mql事件窗口插值诊断
This commit is contained in:
1 parent
331435e78a
commit
01b606df30
5 files changed
+103
-3
No files matched your search
@@ -70,6 +70,24 @@ class AmesimPneumaticGas:
|
||||
)
|
||||
)
|
||||
|
||||
def pressure_transport_enthalpy(
|
||||
self,
|
||||
pressure: float,
|
||||
temperature: float,
|
||||
reference_pressure: float = 101_300.0,
|
||||
reference_temperature: float = 298.15,
|
||||
) -> float:
|
||||
"""Convert AMESim reference enthalpy to the absolute-energy state basis."""
|
||||
return (
|
||||
self.pressure_reference_enthalpy(
|
||||
pressure,
|
||||
temperature,
|
||||
reference_pressure,
|
||||
reference_temperature,
|
||||
)
|
||||
+ self.cp * reference_temperature
|
||||
)
|
||||
|
||||
def temperature_from_internal_energy(self, specific_internal_energy: float) -> float:
|
||||
if self.cv <= 0.0:
|
||||
raise ValueError("cv must be positive.")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from bisect import bisect_left
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import UTC, datetime
|
||||
from math import nextafter, sqrt
|
||||
@@ -492,6 +493,8 @@ class TestMqlPnvoEventWindowSampleDiagnostic:
|
||||
data_paths: tuple[str, ...]
|
||||
python_values_by_data_path: dict[str, float]
|
||||
amesim_values_by_data_path: dict[str, float]
|
||||
amesim_sample_left_time_s: float
|
||||
amesim_sample_right_time_s: float
|
||||
pnl0001_rhs_diagnostics: tuple[TestMqlPnl0001LineRhsDiagnostic, ...] = field(
|
||||
default_factory=tuple
|
||||
)
|
||||
@@ -517,6 +520,10 @@ class TestMqlPnvoEventWindowSampleDiagnostic:
|
||||
TestMqlPnch012EnergyEquationDiagnostic, ...
|
||||
] = field(default_factory=tuple)
|
||||
|
||||
@property
|
||||
def amesim_values_are_interpolated(self) -> bool:
|
||||
return self.amesim_sample_left_time_s != self.amesim_sample_right_time_s
|
||||
|
||||
def abs_error(self, data_path: str) -> float:
|
||||
return abs(
|
||||
self.python_values_by_data_path[data_path]
|
||||
@@ -929,6 +936,27 @@ def _series_one_sided_differences_at(
|
||||
return backward, forward
|
||||
|
||||
|
||||
def _series_sample_bracket(
|
||||
*,
|
||||
times: tuple[float, ...] | list[float],
|
||||
time_s: float,
|
||||
) -> tuple[float, float]:
|
||||
if not times:
|
||||
raise ValueError("at least one sample time is required")
|
||||
index = bisect_left(times, time_s)
|
||||
tolerance = 1.0e-12 * max(1.0, abs(time_s))
|
||||
for candidate_index in (index - 1, index):
|
||||
if 0 <= candidate_index < len(times):
|
||||
candidate_time = times[candidate_index]
|
||||
if abs(candidate_time - time_s) <= tolerance:
|
||||
return candidate_time, candidate_time
|
||||
if index <= 0:
|
||||
return times[0], times[0]
|
||||
if index >= len(times):
|
||||
return times[-1], times[-1]
|
||||
return times[index - 1], times[index]
|
||||
|
||||
|
||||
def _reference_enthalpy_flow_for_node_port(
|
||||
*,
|
||||
gas: AmesimPneumaticGas,
|
||||
@@ -2157,12 +2185,18 @@ def run_test_mql_pnvo_event_window_diagnostic(
|
||||
time_s=sample_time,
|
||||
),
|
||||
)
|
||||
(
|
||||
amesim_sample_left_time_s,
|
||||
amesim_sample_right_time_s,
|
||||
) = _series_sample_bracket(times=amesim_results.times, time_s=sample_time)
|
||||
sample_diagnostics.append(
|
||||
TestMqlPnvoEventWindowSampleDiagnostic(
|
||||
time_s=sample_time,
|
||||
data_paths=data_paths,
|
||||
python_values_by_data_path=python_sample_values,
|
||||
amesim_values_by_data_path=amesim_sample_values,
|
||||
amesim_sample_left_time_s=amesim_sample_left_time_s,
|
||||
amesim_sample_right_time_s=amesim_sample_right_time_s,
|
||||
pnl0001_rhs_diagnostics=pnl0001_rhs_diagnostics,
|
||||
pnl0001_pressure_loss_diagnostics=pnl0001_pressure_loss_diagnostics,
|
||||
pnvo_flow_parameter_diagnostics=pnvo_flow_parameter_diagnostics,
|
||||
@@ -2224,7 +2258,17 @@ def format_test_mql_pnvo_event_window_summary(
|
||||
if diagnostic.sample_diagnostics:
|
||||
lines.append("Sample comparisons:")
|
||||
for sample in diagnostic.sample_diagnostics:
|
||||
lines.append(f" t={sample.time_s}")
|
||||
if sample.amesim_values_are_interpolated:
|
||||
amesim_sample_source = (
|
||||
"linear-interpolation@"
|
||||
f"{sample.amesim_sample_left_time_s}"
|
||||
f"..{sample.amesim_sample_right_time_s}"
|
||||
)
|
||||
else:
|
||||
amesim_sample_source = f"exact@{sample.amesim_sample_left_time_s}"
|
||||
lines.append(
|
||||
f" t={sample.time_s} (amesim={amesim_sample_source})"
|
||||
)
|
||||
for data_path in sample.data_paths:
|
||||
lines.append(
|
||||
f" - {data_path}: "
|
||||
|
||||
Reference in new issue
Block a user