公开PNRP17并接通实时气动机械耦合

This commit is contained in:
huojiarong committed 2026-08-02 14:51:31 +00:00
1 parent 503394f3ed
commit 1f4027573f
18 files changed
+693 -29

No files matched your search

@@ -18,16 +18,15 @@ class AmesimComponentMigrationMatrixTests(unittest.TestCase):
self.assertEqual(missing, [])
def test_unsupported_domains_are_not_marked_as_public_candidates(self) -> None:
def test_pnrp17_is_marked_as_the_first_public_cross_domain_component(self) -> None:
text = MATRIX_PATH.read_text(encoding="utf-8")
unsupported = {
"PNRP17",
}
row = next(
line for line in text.splitlines() if "| `PNRP17` |" in line
)
for family in unsupported:
with self.subTest(family=family):
row = next(line for line in text.splitlines() if f"`{family}`" in line)
self.assertIn("暂不公开", row)
self.assertIn("第一版公开", row)
self.assertIn("`amesim_pnrp17`", row)
self.assertNotIn("暂不公开", row)
if __name__ == "__main__":
+1
View File
@@ -27,6 +27,7 @@ from tests.test_system_xml_protocol import physical_port
GAS_INDEXED_MODEL_TYPES = (
"amesim_pnch023",
"amesim_pnch012",
"amesim_pnrp17",
"amesim_pnor001",
"amesim_pnvo001_fixed",
"amesim_pnvo001",
+1 -1
View File
@@ -69,7 +69,7 @@ class AmesimHeliumPengRobinsonMediumTests(unittest.TestCase):
def test_definition_maps_local_selector_to_amesim_fluid_and_eos_codes(
self,
) -> None:
self.assertEqual(LIBRARY.version, "0.2.0")
self.assertEqual(LIBRARY.version, "0.3.0")
self.assertEqual(AMESIM_HELIUM_PENG_ROBINSON_PROPERTY_MODEL, 0)
self.assertEqual(AMESIM_HELIUM_PROPERTY_MODELS[0].value, 0)
self.assertEqual(AMESIM_HELIUM_PROPERTY_MODELS[0].eos_type, 6)
+205
View File
@@ -0,0 +1,205 @@
from __future__ import annotations
from math import pi
import unittest
from app.main import (
ReactFlowProjectPayload,
build_reactflow_system_xml,
compile_reactflow_network,
run_system_xml_simulation,
)
from app.simulation.components.amesim.mechanical.pistons import AmesimPnrp17
from app.simulation.core.medium import IdealGasMedium
from app.system_xml import validate_system_xml_document
from tests.test_amesim_mechanical_xml import MECMAS21_DEFAULTS, mechanical_port
from tests.test_generic_system_xml_simulation import component_node, physical_edge
from tests.test_system_xml_protocol import physical_port
def _pneumatic_port(name: str, side: str) -> dict[str, str]:
return physical_port(name, "bidirectional", side)
def _zero_force_node(name: str, side: str) -> dict[str, object]:
return component_node(
name,
"amesim_f000",
[mechanical_port("port_1", side)],
)
def _mass_node(name: str) -> dict[str, object]:
parameters = dict(MECMAS21_DEFAULTS)
parameters["mass"] = 2.0
return component_node(
name,
"amesim_mecmas21",
[
mechanical_port("port_1", "left"),
mechanical_port("port_2", "right"),
],
parameters,
)
def pnrp17_coupled_project() -> ReactFlowProjectPayload:
return ReactFlowProjectPayload(
name="amesim-pnrp17-coupled-smoke",
nodes=[
component_node(
"chamber_1",
"amesim_pnch012",
[
_pneumatic_port("port_1", "left"),
_pneumatic_port("port_2", "right"),
_pneumatic_port("port_3", "left"),
_pneumatic_port("port_4", "right"),
],
{
"gi": 0.0,
"cvol0": 0.01,
"kth": 0.0,
"sth": 0.1,
"extemp": 300.0,
"p0": 200000.0,
"T0": 300.0,
"vol1": 0.0,
"vol2": 0.0,
"vol3": 0.0,
"vol4": 0.0,
"dvol1": 0.0,
"dvol2": 0.0,
"dvol3": 0.0,
"dvol4": 0.0,
},
),
component_node(
"piston_1",
"amesim_pnrp17",
[
_pneumatic_port("port_1", "left"),
mechanical_port("port_2", "right"),
mechanical_port("port_3", "left"),
mechanical_port("port_4", "left"),
mechanical_port("port_5", "right"),
],
{"gi": 0.0, "dp": 0.1, "dr": 0.02, "x0": 0.0},
),
_mass_node("piston_mass"),
_mass_node("cylinder_mass"),
_zero_force_node("piston_free", "left"),
_zero_force_node("cylinder_free", "right"),
_zero_force_node("piston_mass_free", "left"),
_zero_force_node("cylinder_mass_free", "right"),
component_node(
"boundary_1",
"amesim_pnpl01",
[_pneumatic_port("port_1", "left")],
),
component_node(
"boundary_2",
"amesim_pnpl01",
[_pneumatic_port("port_1", "left")],
),
component_node(
"boundary_4",
"amesim_pnpl01",
[_pneumatic_port("port_1", "left")],
),
],
edges=[
physical_edge("edge-pneumatic", "chamber_1", "port_3", "piston_1", "port_1"),
physical_edge("edge-boundary-1", "chamber_1", "port_1", "boundary_1", "port_1"),
physical_edge("edge-boundary-2", "chamber_1", "port_2", "boundary_2", "port_1"),
physical_edge("edge-boundary-4", "chamber_1", "port_4", "boundary_4", "port_1"),
physical_edge("edge-piston-mass", "piston_1", "port_2", "piston_mass", "port_1"),
physical_edge("edge-piston-free", "piston_1", "port_5", "piston_free", "port_1"),
physical_edge("edge-piston-mass-free", "piston_mass", "port_2", "piston_mass_free", "port_1"),
physical_edge("edge-cylinder-mass", "piston_1", "port_3", "cylinder_mass", "port_1"),
physical_edge("edge-cylinder-free", "piston_1", "port_4", "cylinder_free", "port_1"),
physical_edge("edge-cylinder-mass-free", "cylinder_mass", "port_2", "cylinder_mass_free", "port_1"),
],
simulation={
"t_start": 0.0,
"t_stop": 0.005,
"step": 0.0025,
"max_step": 0.0005,
"method": "BDF",
},
)
class AmesimPnrp17Tests(unittest.TestCase):
def test_component_equations_match_pnrp17_geometry_and_signs(self) -> None:
medium = IdealGasMedium()
piston = AmesimPnrp17(
"piston_1",
medium,
gi=0.0,
dp=0.2,
dr=0.1,
x0=0.05,
)
piston.port_1.p = 300000.0
piston.port_2.x = piston.port_5.x = 0.3
piston.port_2.v = piston.port_5.v = 0.4
piston.port_3.x = piston.port_4.x = 0.1
piston.port_3.v = piston.port_4.v = -0.2
pressure_force = piston.pressure_force
piston.port_5.f = 10.0
piston.port_2.f = -pressure_force - piston.port_5.f
piston.port_4.f = 20.0
piston.port_3.f = pressure_force - piston.port_4.f
expected_area = pi * (0.2**2 - 0.1**2) / 4.0
self.assertAlmostEqual(piston.effective_area, expected_area)
self.assertAlmostEqual(piston.chamber_length, 0.25)
self.assertAlmostEqual(piston.chamber_volume, expected_area * 0.25)
self.assertAlmostEqual(piston.chamber_volume_flow, expected_area * 0.6)
for residual in piston.pressure_flow_equation_residuals():
self.assertAlmostEqual(residual.value, 0.0, msg=residual.id)
self.assertEqual(
piston.pneumatic_volume_outputs(),
{"port_1": (piston.chamber_volume, piston.chamber_volume_flow)},
)
def test_rod_diameter_cannot_exceed_piston_diameter(self) -> None:
with self.assertRaisesRegex(ValueError, "dr must not exceed"):
AmesimPnrp17(
"piston_1",
IdealGasMedium(),
dp=0.05,
dr=0.06,
)
def test_cross_domain_system_xml_compiles_and_simulates(self) -> None:
project = pnrp17_coupled_project()
xml = build_reactflow_system_xml(project)
report = validate_system_xml_document(xml)
self.assertTrue(report.valid, report.as_dict())
network = compile_reactflow_network(project)
self.assertTrue(network.pressure_flow_structure_dict()["isSquare"])
result = run_system_xml_simulation(xml)
self.assertTrue(result["success"], result["message"])
self.assertEqual(result["series"]["time"], [0.0, 0.0025, 0.005])
piston_volume = result["series"]["piston_1.volume"]
chamber_volume = result["series"]["chamber_1.vol"]
self.assertAlmostEqual(piston_volume[0], 0.0, places=12)
self.assertGreater(piston_volume[-1], piston_volume[0])
for chamber_value, piston_value in zip(chamber_volume, piston_volume):
self.assertAlmostEqual(chamber_value, 0.01 + piston_value, places=10)
self.assertGreater(result["series"]["piston_mass.x"][-1], 0.0)
self.assertLess(result["series"]["cylinder_mass.x"][-1], 0.0)
self.assertGreater(result["diagnostics"]["pneumaticVolume"]["propagations"], 0)
self.assertEqual(
result["diagnostics"]["pneumaticVolume"]["last"]["outputPorts"],
["piston_1.port_1"],
)
if __name__ == "__main__":
unittest.main()
+8 -1
View File
@@ -49,7 +49,7 @@ class ComponentCatalogTests(unittest.TestCase):
self.assertEqual(library["label"], "AMESim 组件库")
self.assertEqual([category["id"] for category in library["categories"]], ["media", "storage", "flow", "junctions", "boundary", "signals", "mechanical"])
self.assertEqual(set(components), {"amesim_ideal_air_medium", "amesim_helium_medium", "amesim_pnpl01", "amesim_step0", "amesim_ud00", "amesim_f000", "amesim_forc", "amesim_mecmas21", "amesim_lstp00a", "amesim_lmechn1", "amesim_pnch023", "amesim_pnch012", "amesim_pnor001", "amesim_pnvo001_fixed", "amesim_pnvo001", "amesim_pnl00r", "amesim_pnl0001", "amesim_pnl0002", "amesim_pnl0003", "amesim_pn3node2", "amesim_p4node2"})
self.assertEqual(set(components), {"amesim_ideal_air_medium", "amesim_helium_medium", "amesim_pnpl01", "amesim_step0", "amesim_ud00", "amesim_f000", "amesim_forc", "amesim_mecmas21", "amesim_lstp00a", "amesim_lmechn1", "amesim_pnrp17", "amesim_pnch023", "amesim_pnch012", "amesim_pnor001", "amesim_pnvo001_fixed", "amesim_pnvo001", "amesim_pnl00r", "amesim_pnl0001", "amesim_pnl0002", "amesim_pnl0003", "amesim_pn3node2", "amesim_p4node2"})
for model_type, component in components.items():
with self.subTest(model_type=model_type):
self.assertEqual(component["symbol"], model_type)
@@ -180,6 +180,13 @@ class ComponentCatalogTests(unittest.TestCase):
}
self.assertEqual(components["amesim_lmechn1"]["category"]["id"], "mechanical")
self.assertEqual([port["name"] for port in components["amesim_lmechn1"]["ports"]], [f"port_{index}" for index in range(1, 10)])
pnrp_parameters = {
parameter["name"]: parameter
for parameter in components["amesim_pnrp17"]["parameters"]
}
self.assertEqual(components["amesim_pnrp17"]["category"]["id"], "mechanical")
self.assertEqual([port["name"] for port in components["amesim_pnrp17"]["ports"]], ["port_1", "port_3", "port_4", "port_2", "port_5"])
self.assertEqual(pnrp_parameters["dp"]["unit"], "m")
self.assertEqual(lmechn_parameters["v1"]["maximum"], 8.0)
self.assertEqual(components["amesim_pnvo001"]["category"]["id"], "flow")
self.assertEqual([port["name"] for port in components["amesim_pnvo001"]["ports"]], ["res", "port_2", "port_3"])
+2
View File
@@ -28,6 +28,8 @@ class ComponentInterfaceTests(unittest.TestCase):
("p", "effort", "equal"),
("m_flow", "flow", "sumToZero"),
("h_outflow", "stream", "streamMix"),
("volume", "signal", "directed"),
("volume_flow", "signal", "directed"),
],
)
+2
View File
@@ -41,6 +41,7 @@ class ComponentRegistryTests(unittest.TestCase):
"amesim_mecmas21",
"amesim_lstp00a",
"amesim_lmechn1",
"amesim_pnrp17",
"amesim_pnch023",
"amesim_pnch012",
"amesim_pnor001",
@@ -78,6 +79,7 @@ class ComponentRegistryTests(unittest.TestCase):
self.assertEqual(models["amesim_mecmas21"].library.id, "amesim")
self.assertEqual(models["amesim_lstp00a"].library.id, "amesim")
self.assertEqual(models["amesim_lmechn1"].library.id, "amesim")
self.assertEqual(models["amesim_pnrp17"].library.id, "amesim")
self.assertEqual(models["amesim_pnch023"].library.id, "amesim")
self.assertEqual(models["amesim_pnch012"].library.id, "amesim")
self.assertEqual(models["amesim_pnor001"].library.id, "amesim")