58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
# tests/test_tank.py
|
|
import numpy as np
|
|
import pytest
|
|
from tank_pipe.tank import Tank
|
|
|
|
|
|
GAMMA = 1.4
|
|
R_GAS = 287.0
|
|
|
|
|
|
def test_tank_initial_state_matches_ideal_gas():
|
|
"""
|
|
Given P, T, V construct a Tank; mass and U should match ideal gas:
|
|
rho = P / (R * T)
|
|
mass = rho * V
|
|
U = P * V / (gamma - 1) (since u=0 inside tank)
|
|
T = (U / mass) * (gamma - 1) / R (round-trip)
|
|
"""
|
|
tank = Tank(V=5.0, P_init=10e6, T_init=300.0, gamma=GAMMA, R_gas=R_GAS)
|
|
rho_expected = 10e6 / (R_GAS * 300.0)
|
|
assert abs(tank.rho - rho_expected) < 1e-9
|
|
assert abs(tank.mass - rho_expected * 5.0) < 1e-6
|
|
assert abs(tank.U - 10e6 * 5.0 / (GAMMA - 1)) < 1e-3
|
|
assert abs(tank.P - 10e6) < 1e-3
|
|
assert abs(tank.T - 300.0) < 1e-9
|
|
|
|
|
|
def test_tank_ghost_state_is_stagnation_conservative_vector():
|
|
"""
|
|
ghost_state() should return [rho, 0, P/(gamma-1)]
|
|
(u_ghost = 0 per spec §2.3, so total energy density equals
|
|
internal energy density = P/(gamma-1)).
|
|
"""
|
|
tank = Tank(V=5.0, P_init=10e6, T_init=300.0, gamma=GAMMA, R_gas=R_GAS)
|
|
g = tank.ghost_state()
|
|
assert g.shape == (3,)
|
|
assert abs(g[0] - tank.rho) < 1e-12
|
|
assert g[1] == 0.0
|
|
assert abs(g[2] - 10e6 / (GAMMA - 1)) < 1e-3
|
|
|
|
|
|
def test_tank_apply_flux_outflow_reduces_mass_energy_and_pressure():
|
|
"""
|
|
apply_flux(mdot, edot, dt, sign=-1) should subtract mdot*dt from mass
|
|
and edot*dt from U. Derived P should decrease correspondingly.
|
|
"""
|
|
tank = Tank(V=5.0, P_init=10e6, T_init=300.0, gamma=GAMMA, R_gas=R_GAS)
|
|
mass_before = tank.mass
|
|
U_before = tank.U
|
|
P_before = tank.P
|
|
mdot = 1.0 # kg/s
|
|
edot = 5e5 # J/s (enthalpy rate)
|
|
dt = 1e-3
|
|
tank.apply_flux(mdot=mdot, edot=edot, dt=dt, sign=-1)
|
|
assert abs(tank.mass - (mass_before - mdot * dt)) < 1e-12
|
|
assert abs(tank.U - (U_before - edot * dt)) < 1e-9
|
|
assert tank.P < P_before
|