完善MECMAS21参数配置与分组交互

This commit is contained in:
ljz committed 2026-08-03 23:51:27 +08:00
1 parent 32f21f08ff
commit be54070855
28 files changed
+3418 -202

No files matched your search

+87 -1
View File
@@ -23,6 +23,8 @@ SYSTEM_XML_V2_SCHEMA_PATH = (
Path(__file__).resolve().parent.parent / "schemas" / "system-simulation-v2.xsd"
)
SUPPORTED_SOLVER_METHODS = {"RK45", "RK23", "DOP853", "Radau", "BDF", "LSODA"}
AMESIM_PARAMETER_ENCODING_VERSION = "1"
MECMAS21_LEGACY_BINARY_PARAMETERS = ("useFriction", "strib")
@dataclass(frozen=True)
@@ -139,6 +141,7 @@ class SystemXmlDocument:
schema_version: str
unit_system: str
medium_reference_version: str | None
amesim_parameter_encoding_version: str | None
simulation: SystemXmlSimulation
components: tuple[SystemXmlComponent, ...]
connections: tuple[SystemXmlConnection, ...]
@@ -153,6 +156,10 @@ class SystemXmlDocument:
}
if self.medium_reference_version is not None:
result["mediumReferenceVersion"] = self.medium_reference_version
if self.amesim_parameter_encoding_version is not None:
result["amesimParameterEncodingVersion"] = (
self.amesim_parameter_encoding_version
)
return result
def as_project_data(self) -> dict[str, object]:
@@ -176,6 +183,7 @@ class SystemXmlDocument:
return {
"name": self.name,
"mediumReferenceVersion": 1,
"amesimParameterEncodingVersion": 1,
"nodes": [
{
"id": component.id,
@@ -292,11 +300,19 @@ def validate_system_xml_document(
document, property_model_issues = _normalize_amesim_gas_property_models(
document
)
document = replace(document, medium_reference_version="1")
document, parameter_encoding_issues = (
_normalize_legacy_amesim_parameter_encoding(document)
)
document = replace(
document,
medium_reference_version="1",
amesim_parameter_encoding_version=AMESIM_PARAMETER_ENCODING_VERSION,
)
issues = tuple(
[
*compatibility_issues,
*property_model_issues,
*parameter_encoding_issues,
*_semantic_issues(document),
]
)
@@ -350,6 +366,9 @@ def _parse_validated_root(root: etree._Element) -> SystemXmlDocument:
schema_version=str(root.get("schemaVersion")),
unit_system=str(root.get("unitSystem")),
medium_reference_version=root.get("mediumReferenceVersion"),
amesim_parameter_encoding_version=root.get(
"amesimParameterEncodingVersion"
),
simulation=simulation,
components=components,
connections=connections,
@@ -392,6 +411,73 @@ def _parse_component(element: etree._Element) -> SystemXmlComponent:
)
def _normalize_legacy_amesim_parameter_encoding(
document: SystemXmlDocument,
) -> tuple[SystemXmlDocument, tuple[ValidationIssue, ...]]:
"""Upgrade legacy app-local MECMAS21 0/1 codes to AMESim 1/2 codes."""
if document.amesim_parameter_encoding_version is not None:
return document, ()
normalized_components: list[SystemXmlComponent] = []
issues: list[ValidationIssue] = []
for component in document.components:
if component.model_type != "amesim_mecmas21":
normalized_components.append(component)
continue
changed_parameters: list[str] = []
normalized_parameters: list[SystemXmlParameter] = []
parameter_names: set[str] = set()
for parameter in component.parameters:
parameter_names.add(parameter.name)
value = parameter.value
if parameter.name in MECMAS21_LEGACY_BINARY_PARAMETERS:
if value == 0.0:
value = 1.0
changed_parameters.append(parameter.name)
elif value == 1.0:
value = 2.0
changed_parameters.append(parameter.name)
normalized_parameters.append(replace(parameter, value=value))
for name in MECMAS21_LEGACY_BINARY_PARAMETERS:
if name not in parameter_names:
normalized_parameters.append(
SystemXmlParameter(name=name, value=2.0, line=component.line)
)
changed_parameters.append(name)
normalized_components.append(
replace(component, parameters=tuple(normalized_parameters))
)
if changed_parameters:
issues.append(
ValidationIssue(
layer="semantic",
code="AMESIM_PARAMETER_ENCODING_MIGRATED",
message=(
f"Component '{component.id}' used legacy MECMAS21 "
"option semantics; normalized "
+ ", ".join(changed_parameters)
+ " to canonical AMESim 1/2 codes."
),
severity="warning",
path=f"/System/Components/Component[@id='{component.id}']",
line=component.line,
)
)
return (
replace(
document,
components=tuple(normalized_components),
amesim_parameter_encoding_version=AMESIM_PARAMETER_ENCODING_VERSION,
),
tuple(issues),
)
def _normalize_legacy_amesim_gas_references(
document: SystemXmlDocument,
) -> tuple[SystemXmlDocument, list[ValidationIssue]]: