90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
import pytest
|
|
|
|
from cylinder.gas_cylinder import (
|
|
DEFAULT_FLUID,
|
|
DEFAULT_P_INIT,
|
|
DEFAULT_T_INIT,
|
|
DEFAULT_VOLUME,
|
|
HighPressureGasCylinder,
|
|
)
|
|
|
|
|
|
def test_default_high_pressure_cylinder_state_matches_inputs():
|
|
cylinder = HighPressureGasCylinder()
|
|
|
|
assert cylinder.fluid == DEFAULT_FLUID
|
|
assert cylinder.V == DEFAULT_VOLUME
|
|
assert abs(cylinder.P - DEFAULT_P_INIT) / DEFAULT_P_INIT < 1e-8
|
|
assert abs(cylinder.T - DEFAULT_T_INIT) < 1e-8
|
|
assert cylinder.mass > 0
|
|
assert cylinder.U > 0
|
|
|
|
|
|
def test_cylinder_accepts_configurable_fluid_pressure_volume_temperature():
|
|
cylinder = HighPressureGasCylinder(
|
|
fluid="Nitrogen",
|
|
P_init=2.0e6,
|
|
V=50e-3,
|
|
T_init=300.0,
|
|
)
|
|
|
|
assert cylinder.fluid == "Nitrogen"
|
|
assert abs(cylinder.P - 2.0e6) / 2.0e6 < 1e-8
|
|
assert abs(cylinder.T - 300.0) < 1e-8
|
|
assert abs(cylinder.V - 50e-3) < 1e-15
|
|
assert cylinder.mass > 0
|
|
|
|
|
|
def test_cylinder_state_summary_omits_unused_thermodynamic_helpers():
|
|
cylinder = HighPressureGasCylinder()
|
|
summary = cylinder.state_summary()
|
|
|
|
assert set(summary) == {
|
|
"fluid",
|
|
"V",
|
|
"mass",
|
|
"rho",
|
|
"P",
|
|
"T",
|
|
"U",
|
|
"u",
|
|
"h",
|
|
}
|
|
|
|
|
|
def test_cylinder_ghost_state_is_stagnant_conservative_vector():
|
|
cylinder = HighPressureGasCylinder()
|
|
ghost = cylinder.ghost_state()
|
|
|
|
assert ghost.shape == (3,)
|
|
assert abs(ghost[0] - cylinder.rho) < 1e-12
|
|
assert ghost[1] == 0.0
|
|
assert abs(ghost[2] - cylinder.rho * cylinder.specific_internal_energy) < 1e-6
|
|
|
|
|
|
def test_cylinder_apply_flux_outflow_reduces_mass_energy_and_pressure():
|
|
cylinder = HighPressureGasCylinder()
|
|
mass_before = cylinder.mass
|
|
U_before = cylinder.U
|
|
P_before = cylinder.P
|
|
|
|
mdot = 1e-3
|
|
edot = mdot * cylinder.h
|
|
dt = 1.0
|
|
cylinder.apply_flux(mdot=mdot, edot=edot, dt=dt, sign=-1)
|
|
|
|
assert abs(cylinder.mass - (mass_before - mdot * dt)) < 1e-12
|
|
assert abs(cylinder.U - (U_before - edot * dt)) < 1e-6
|
|
assert cylinder.P < P_before
|
|
|
|
|
|
def test_cylinder_rejects_invalid_inputs():
|
|
with pytest.raises(ValueError):
|
|
HighPressureGasCylinder(fluid="")
|
|
with pytest.raises(ValueError):
|
|
HighPressureGasCylinder(P_init=0.0)
|
|
with pytest.raises(ValueError):
|
|
HighPressureGasCylinder(V=0.0)
|
|
with pytest.raises(ValueError):
|
|
HighPressureGasCylinder(T_init=0.0)
|