公开PNRP17并接通实时气动机械耦合
This commit is contained in:
1 parent
503394f3ed
commit
1f4027573f
18 files changed
+693
-29
No files matched your search
@@ -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()
|
||||
Reference in new issue
Block a user