feat: integrate AMESim media models and editor UI
This commit is contained in:
1 parent
3e4f466114
commit
e7177ab03e
181 files changed
+7042
-25270
No files matched your search
@@ -0,0 +1,451 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import tarfile
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from app.simulation.examples.test_mql.system import CONNECTION_SPECS, GLOBAL_PARAMETERS
|
||||
from app.simulation.examples.test_mql.config import resolve_numeric_expression
|
||||
|
||||
|
||||
AMESIM_REFERENCE_PRESSURE_PA = 101_300.0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TestMqlPnl0001Spec:
|
||||
alias: str
|
||||
source_component: str
|
||||
source_port: str
|
||||
target_component: str
|
||||
target_port: str
|
||||
diameter_mm: float
|
||||
length_m: float
|
||||
relative_roughness: float
|
||||
polytropic_constant: float
|
||||
heat_transfer_coefficient: float
|
||||
external_temperature_k: float
|
||||
gas_type_index: int
|
||||
mode: int
|
||||
initial_temperature_k: float
|
||||
initial_gauge_pressure_pa: float
|
||||
|
||||
@property
|
||||
def initial_absolute_pressure_pa(self) -> float:
|
||||
return self.initial_gauge_pressure_pa + AMESIM_REFERENCE_PRESSURE_PA
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TestMqlPnl0002Spec:
|
||||
alias: str
|
||||
source_component: str
|
||||
source_port: str
|
||||
target_component: str
|
||||
target_port: str
|
||||
diameter_mm: float
|
||||
length_m: float
|
||||
relative_roughness: float
|
||||
polytropic_constant: float
|
||||
heat_transfer_coefficient: float
|
||||
external_temperature_k: float
|
||||
gas_type_index: int
|
||||
mode: int
|
||||
initial_center_temperature_k: float
|
||||
initial_center_gauge_pressure_pa: float
|
||||
|
||||
@property
|
||||
def initial_center_absolute_pressure_pa(self) -> float:
|
||||
return self.initial_center_gauge_pressure_pa + AMESIM_REFERENCE_PRESSURE_PA
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TestMqlPnl0003Spec:
|
||||
alias: str
|
||||
source_component: str
|
||||
source_port: str
|
||||
target_component: str
|
||||
target_port: str
|
||||
diameter_mm: float
|
||||
length_m: float
|
||||
relative_roughness: float
|
||||
polytropic_constant: float
|
||||
heat_transfer_coefficient: float
|
||||
external_temperature_k: float
|
||||
gas_type_index: int
|
||||
mode: int
|
||||
initial_temperature_1_k: float
|
||||
initial_gauge_pressure_1_pa: float
|
||||
initial_temperature_2_k: float
|
||||
initial_gauge_pressure_2_pa: float
|
||||
|
||||
@property
|
||||
def initial_absolute_pressure_1_pa(self) -> float:
|
||||
return self.initial_gauge_pressure_1_pa + AMESIM_REFERENCE_PRESSURE_PA
|
||||
|
||||
@property
|
||||
def initial_absolute_pressure_2_pa(self) -> float:
|
||||
return self.initial_gauge_pressure_2_pa + AMESIM_REFERENCE_PRESSURE_PA
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TestMqlPnl00rSpec:
|
||||
alias: str
|
||||
source_component: str
|
||||
source_port: str
|
||||
target_component: str
|
||||
target_port: str
|
||||
diameter_mm: float
|
||||
length_m: float
|
||||
relative_roughness: float
|
||||
gas_type_index: int
|
||||
|
||||
|
||||
def load_test_mql_pnl0001_specs(
|
||||
archive_path: str | Path,
|
||||
*,
|
||||
cir_member: str = "test_mql_.cir",
|
||||
) -> tuple[TestMqlPnl0001Spec, ...]:
|
||||
"""Load resolved PNL0001 geometry and initial states from the AMESim source."""
|
||||
with tarfile.open(archive_path) as archive:
|
||||
cir_file = archive.extractfile(cir_member)
|
||||
if cir_file is None:
|
||||
raise ValueError(f"Missing AMESim circuit member: {cir_member}")
|
||||
cir_text = cir_file.read().decode("latin1")
|
||||
|
||||
numeric_globals = {
|
||||
name: value
|
||||
for name, expression in GLOBAL_PARAMETERS.items()
|
||||
if (value := resolve_numeric_expression(expression, {})) is not None
|
||||
}
|
||||
connections = {
|
||||
str(connection["alias"]): connection
|
||||
for connection in CONNECTION_SPECS
|
||||
if connection["submodel"] == "PNL0001"
|
||||
}
|
||||
specs = []
|
||||
for block in re.findall(r"<LINE>.*?</LINE>", cir_text, flags=re.DOTALL):
|
||||
if _optional_text(block, "SUB_NAME") != "PNL0001":
|
||||
continue
|
||||
alias = _required_text(block, "ALIAS")
|
||||
connection = connections.get(alias)
|
||||
if connection is None:
|
||||
raise ValueError(f"PNL0001 line {alias!r} is absent from CONNECTION_SPECS")
|
||||
real_parameters = _parameter_expressions(block, "RPARAM")
|
||||
integer_parameters = _parameter_expressions(block, "IPARAM")
|
||||
state_values = _evar_values(block)
|
||||
specs.append(
|
||||
TestMqlPnl0001Spec(
|
||||
alias=alias,
|
||||
source_component=str(connection["source_component"]),
|
||||
source_port=str(connection["source_port"]),
|
||||
target_component=str(connection["target_component"]),
|
||||
target_port=str(connection["target_port"]),
|
||||
diameter_mm=_required_numeric(
|
||||
alias, "diam", real_parameters, numeric_globals
|
||||
),
|
||||
length_m=_required_numeric(alias, "le", real_parameters, numeric_globals),
|
||||
relative_roughness=_required_numeric(
|
||||
alias, "rr", real_parameters, numeric_globals
|
||||
),
|
||||
polytropic_constant=_required_numeric(
|
||||
alias, "k", real_parameters, numeric_globals
|
||||
),
|
||||
heat_transfer_coefficient=_required_numeric(
|
||||
alias, "kth", real_parameters, numeric_globals
|
||||
),
|
||||
external_temperature_k=_required_numeric(
|
||||
alias, "extemp", real_parameters, numeric_globals
|
||||
),
|
||||
gas_type_index=int(
|
||||
_required_numeric(alias, "gi", integer_parameters, numeric_globals)
|
||||
),
|
||||
mode=int(
|
||||
_required_numeric(alias, "mode", integer_parameters, numeric_globals)
|
||||
),
|
||||
initial_temperature_k=_required_numeric(
|
||||
alias, "t2", state_values, numeric_globals
|
||||
),
|
||||
initial_gauge_pressure_pa=_required_numeric(
|
||||
alias, "p2", state_values, numeric_globals
|
||||
),
|
||||
)
|
||||
)
|
||||
if set(connections) != {spec.alias for spec in specs}:
|
||||
missing = sorted(set(connections) - {spec.alias for spec in specs})
|
||||
raise ValueError(f"Missing PNL0001 parameter blocks: {missing}")
|
||||
return tuple(specs)
|
||||
|
||||
|
||||
def load_test_mql_pnl0002_specs(
|
||||
archive_path: str | Path,
|
||||
*,
|
||||
cir_member: str = "test_mql_.cir",
|
||||
) -> tuple[TestMqlPnl0002Spec, ...]:
|
||||
"""Load resolved PNL0002 geometry and center compliance initial state."""
|
||||
with tarfile.open(archive_path) as archive:
|
||||
cir_file = archive.extractfile(cir_member)
|
||||
if cir_file is None:
|
||||
raise ValueError(f"Missing AMESim circuit member: {cir_member}")
|
||||
cir_text = cir_file.read().decode("latin1")
|
||||
|
||||
numeric_globals = {
|
||||
name: value
|
||||
for name, expression in GLOBAL_PARAMETERS.items()
|
||||
if (value := resolve_numeric_expression(expression, {})) is not None
|
||||
}
|
||||
connections = {
|
||||
str(connection["alias"]): connection
|
||||
for connection in CONNECTION_SPECS
|
||||
if connection["submodel"] == "PNL0002"
|
||||
}
|
||||
specs = []
|
||||
for block in re.findall(r"<LINE>.*?</LINE>", cir_text, flags=re.DOTALL):
|
||||
if _optional_text(block, "SUB_NAME") != "PNL0002":
|
||||
continue
|
||||
alias = _required_text(block, "ALIAS")
|
||||
connection = connections.get(alias)
|
||||
if connection is None:
|
||||
raise ValueError(f"PNL0002 line {alias!r} is absent from CONNECTION_SPECS")
|
||||
real_parameters = _parameter_expressions(block, "RPARAM")
|
||||
integer_parameters = _parameter_expressions(block, "IPARAM")
|
||||
state_values = _ivar_values(block)
|
||||
specs.append(
|
||||
TestMqlPnl0002Spec(
|
||||
alias=alias,
|
||||
source_component=str(connection["source_component"]),
|
||||
source_port=str(connection["source_port"]),
|
||||
target_component=str(connection["target_component"]),
|
||||
target_port=str(connection["target_port"]),
|
||||
diameter_mm=_required_numeric(
|
||||
alias, "diam", real_parameters, numeric_globals
|
||||
),
|
||||
length_m=_required_numeric(alias, "le", real_parameters, numeric_globals),
|
||||
relative_roughness=_required_numeric(
|
||||
alias, "rr", real_parameters, numeric_globals
|
||||
),
|
||||
polytropic_constant=_required_numeric(
|
||||
alias, "k", real_parameters, numeric_globals
|
||||
),
|
||||
heat_transfer_coefficient=_required_numeric(
|
||||
alias, "kth", real_parameters, numeric_globals
|
||||
),
|
||||
external_temperature_k=_required_numeric(
|
||||
alias, "extemp", real_parameters, numeric_globals
|
||||
),
|
||||
gas_type_index=int(
|
||||
_required_numeric(alias, "gi", integer_parameters, numeric_globals)
|
||||
),
|
||||
mode=int(
|
||||
_required_numeric(alias, "mode", integer_parameters, numeric_globals)
|
||||
),
|
||||
initial_center_temperature_k=_required_numeric(
|
||||
alias, "tctr", state_values, numeric_globals
|
||||
),
|
||||
initial_center_gauge_pressure_pa=_required_numeric(
|
||||
alias, "pctr", state_values, numeric_globals
|
||||
),
|
||||
)
|
||||
)
|
||||
if set(connections) != {spec.alias for spec in specs}:
|
||||
missing = sorted(set(connections) - {spec.alias for spec in specs})
|
||||
raise ValueError(f"Missing PNL0002 parameter blocks: {missing}")
|
||||
return tuple(specs)
|
||||
|
||||
|
||||
def load_test_mql_pnl0003_specs(
|
||||
archive_path: str | Path,
|
||||
*,
|
||||
cir_member: str = "test_mql_.cir",
|
||||
) -> tuple[TestMqlPnl0003Spec, ...]:
|
||||
"""Load resolved PNL0003 geometry and both compliance initial states."""
|
||||
with tarfile.open(archive_path) as archive:
|
||||
cir_file = archive.extractfile(cir_member)
|
||||
if cir_file is None:
|
||||
raise ValueError(f"Missing AMESim circuit member: {cir_member}")
|
||||
cir_text = cir_file.read().decode("latin1")
|
||||
|
||||
numeric_globals = {
|
||||
name: value
|
||||
for name, expression in GLOBAL_PARAMETERS.items()
|
||||
if (value := resolve_numeric_expression(expression, {})) is not None
|
||||
}
|
||||
connections = {
|
||||
str(connection["alias"]): connection
|
||||
for connection in CONNECTION_SPECS
|
||||
if connection["submodel"] == "PNL0003"
|
||||
}
|
||||
specs = []
|
||||
for block in re.findall(r"<LINE>.*?</LINE>", cir_text, flags=re.DOTALL):
|
||||
if _optional_text(block, "SUB_NAME") != "PNL0003":
|
||||
continue
|
||||
alias = _required_text(block, "ALIAS")
|
||||
connection = connections.get(alias)
|
||||
if connection is None:
|
||||
raise ValueError(f"PNL0003 line {alias!r} is absent from CONNECTION_SPECS")
|
||||
real_parameters = _parameter_expressions(block, "RPARAM")
|
||||
integer_parameters = _parameter_expressions(block, "IPARAM")
|
||||
state_values = _evar_values(block)
|
||||
specs.append(
|
||||
TestMqlPnl0003Spec(
|
||||
alias=alias,
|
||||
source_component=str(connection["source_component"]),
|
||||
source_port=str(connection["source_port"]),
|
||||
target_component=str(connection["target_component"]),
|
||||
target_port=str(connection["target_port"]),
|
||||
diameter_mm=_required_numeric(
|
||||
alias, "diam", real_parameters, numeric_globals
|
||||
),
|
||||
length_m=_required_numeric(alias, "le", real_parameters, numeric_globals),
|
||||
relative_roughness=_required_numeric(
|
||||
alias, "rr", real_parameters, numeric_globals
|
||||
),
|
||||
polytropic_constant=_required_numeric(
|
||||
alias, "k", real_parameters, numeric_globals
|
||||
),
|
||||
heat_transfer_coefficient=_required_numeric(
|
||||
alias, "kth", real_parameters, numeric_globals
|
||||
),
|
||||
external_temperature_k=_required_numeric(
|
||||
alias, "extemp", real_parameters, numeric_globals
|
||||
),
|
||||
gas_type_index=int(
|
||||
_required_numeric(alias, "gi", integer_parameters, numeric_globals)
|
||||
),
|
||||
mode=int(
|
||||
_required_numeric(alias, "mode", integer_parameters, numeric_globals)
|
||||
),
|
||||
initial_temperature_1_k=_required_numeric(
|
||||
alias, "t1", state_values, numeric_globals
|
||||
),
|
||||
initial_gauge_pressure_1_pa=_required_numeric(
|
||||
alias, "p1", state_values, numeric_globals
|
||||
),
|
||||
initial_temperature_2_k=_required_numeric(
|
||||
alias, "t2", state_values, numeric_globals
|
||||
),
|
||||
initial_gauge_pressure_2_pa=_required_numeric(
|
||||
alias, "p2", state_values, numeric_globals
|
||||
),
|
||||
)
|
||||
)
|
||||
if set(connections) != {spec.alias for spec in specs}:
|
||||
missing = sorted(set(connections) - {spec.alias for spec in specs})
|
||||
raise ValueError(f"Missing PNL0003 parameter blocks: {missing}")
|
||||
return tuple(specs)
|
||||
|
||||
|
||||
def load_test_mql_pnl00r_specs(
|
||||
archive_path: str | Path,
|
||||
*,
|
||||
cir_member: str = "test_mql_.cir",
|
||||
) -> tuple[TestMqlPnl00rSpec, ...]:
|
||||
"""Load resolved PNL00R geometry from the AMESim source."""
|
||||
with tarfile.open(archive_path) as archive:
|
||||
cir_file = archive.extractfile(cir_member)
|
||||
if cir_file is None:
|
||||
raise ValueError(f"Missing AMESim circuit member: {cir_member}")
|
||||
cir_text = cir_file.read().decode("latin1")
|
||||
|
||||
numeric_globals = {
|
||||
name: value
|
||||
for name, expression in GLOBAL_PARAMETERS.items()
|
||||
if (value := resolve_numeric_expression(expression, {})) is not None
|
||||
}
|
||||
connections = {
|
||||
str(connection["alias"]): connection
|
||||
for connection in CONNECTION_SPECS
|
||||
if connection["submodel"] == "PNL00R"
|
||||
}
|
||||
specs = []
|
||||
for block in re.findall(r"<LINE>.*?</LINE>", cir_text, flags=re.DOTALL):
|
||||
if _optional_text(block, "SUB_NAME") != "PNL00R":
|
||||
continue
|
||||
alias = _required_text(block, "ALIAS")
|
||||
connection = connections.get(alias)
|
||||
if connection is None:
|
||||
raise ValueError(f"PNL00R line {alias!r} is absent from CONNECTION_SPECS")
|
||||
real_parameters = _parameter_expressions(block, "RPARAM")
|
||||
integer_parameters = _parameter_expressions(block, "IPARAM")
|
||||
specs.append(
|
||||
TestMqlPnl00rSpec(
|
||||
alias=alias,
|
||||
source_component=str(connection["source_component"]),
|
||||
source_port=str(connection["source_port"]),
|
||||
target_component=str(connection["target_component"]),
|
||||
target_port=str(connection["target_port"]),
|
||||
diameter_mm=_required_numeric(
|
||||
alias, "diam", real_parameters, numeric_globals
|
||||
),
|
||||
length_m=_required_numeric(alias, "le", real_parameters, numeric_globals),
|
||||
relative_roughness=_required_numeric(
|
||||
alias, "rr", real_parameters, numeric_globals
|
||||
),
|
||||
gas_type_index=int(
|
||||
_required_numeric(alias, "gi", integer_parameters, numeric_globals)
|
||||
),
|
||||
)
|
||||
)
|
||||
if set(connections) != {spec.alias for spec in specs}:
|
||||
missing = sorted(set(connections) - {spec.alias for spec in specs})
|
||||
raise ValueError(f"Missing PNL00R parameter blocks: {missing}")
|
||||
return tuple(specs)
|
||||
|
||||
|
||||
def _parameter_expressions(block: str, tag_name: str) -> dict[str, str]:
|
||||
parameters = {}
|
||||
for parameter_block in re.findall(
|
||||
rf"<{tag_name}>.*?</{tag_name}>",
|
||||
block,
|
||||
flags=re.DOTALL,
|
||||
):
|
||||
parameters[_required_text(parameter_block, "VARNAME")] = _required_text(
|
||||
parameter_block,
|
||||
"VALUE",
|
||||
)
|
||||
return parameters
|
||||
|
||||
|
||||
def _ivar_values(block: str) -> dict[str, str]:
|
||||
values = {}
|
||||
for variable_block in re.findall(r"<IVAR>.*?</IVAR>", block, flags=re.DOTALL):
|
||||
value = _optional_text(variable_block, "VALUE")
|
||||
if value:
|
||||
values[_required_text(variable_block, "VARNAME")] = value
|
||||
return values
|
||||
|
||||
|
||||
def _evar_values(block: str) -> dict[str, str]:
|
||||
values = {}
|
||||
for variable_block in re.findall(r"<EVAR>.*?</EVAR>", block, flags=re.DOTALL):
|
||||
value = _optional_text(variable_block, "VALUE")
|
||||
if value:
|
||||
values[_required_text(variable_block, "VARNAME")] = value
|
||||
return values
|
||||
|
||||
|
||||
def _required_numeric(
|
||||
alias: str,
|
||||
name: str,
|
||||
expressions: dict[str, str],
|
||||
variables: dict[str, float],
|
||||
) -> float:
|
||||
if name not in expressions:
|
||||
raise ValueError(f"Missing {name!r} on line {alias!r}")
|
||||
value = resolve_numeric_expression(expressions[name], variables)
|
||||
if value is None:
|
||||
raise ValueError(
|
||||
f"Cannot resolve {name!r}={expressions[name]!r} on line {alias!r}"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
def _required_text(block: str, tag_name: str) -> str:
|
||||
value = _optional_text(block, tag_name)
|
||||
if value is None:
|
||||
raise ValueError(f"Missing AMESim circuit element: {tag_name}")
|
||||
return value
|
||||
|
||||
|
||||
def _optional_text(block: str, tag_name: str) -> str | None:
|
||||
match = re.search(rf"<{tag_name}>(.*?)</{tag_name}>", block, flags=re.DOTALL)
|
||||
return match.group(1).strip() if match is not None else None
|
||||
Reference in new issue
Block a user