181 lines
6.6 KiB
Python
181 lines
6.6 KiB
Python
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(
|
|
projectSchemaVersion=1,
|
|
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_3", "left"),
|
|
mechanical_port("port_2", "left"),
|
|
mechanical_port("port_4", "right"),
|
|
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_catalog_ports_follow_amesim_pnrp17_icon_sides(self) -> None:
|
|
self.assertEqual(
|
|
tuple((port.name, port.side) for port in AmesimPnrp17.DISPLAY.ports),
|
|
(
|
|
("port_1", "left"),
|
|
("port_3", "left"),
|
|
("port_2", "left"),
|
|
("port_4", "right"),
|
|
("port_5", "right"),
|
|
),
|
|
)
|
|
|
|
|
|
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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|