Files
pipe-system-simulation-test/tests/test_pipe.py
T
2026-06-03 15:41:04 +08:00

60 lines
1.9 KiB
Python

# tests/test_pipe.py
import numpy as np
import pytest
from pipe import Pipe
GAMMA = 1.4
R_GAS = 287.0
def test_pipe_uniform_initialization_all_cells_identical():
"""
Initial state: uniform P, T, u=0 across all N cells.
All cells should have identical W. primitives() should round-trip
back to u=0 and P=P_init.
"""
pipe = Pipe(L=1.0, D=5e-3, N=20,
P_init=1e5, T_init=300.0,
gamma=GAMMA, R_gas=R_GAS)
# All cells identical
for i in range(1, pipe.N):
assert np.allclose(pipe.W[:, i], pipe.W[:, 0], rtol=1e-14)
rho, u, P, a = pipe.primitives()
rho_expected = 1e5 / (R_GAS * 300.0)
assert np.allclose(u, 0.0)
assert np.allclose(P, 1e5, rtol=1e-10)
assert np.allclose(rho, rho_expected, rtol=1e-10)
# Sound speed a = sqrt(gamma * P / rho)
a_expected = np.sqrt(GAMMA * 1e5 / rho_expected)
assert np.allclose(a, a_expected, rtol=1e-10)
# Geometric sanity
assert pipe.dx == 1.0 / 20
assert pipe.x_centers.shape == (20,)
assert abs(pipe.x_centers[0] - 0.025) < 1e-14
assert abs(pipe.x_centers[-1] - 0.975) < 1e-14
def test_pipe_uniform_state_plus_matching_boundary_flux_is_static():
"""
If all cells have identical W (so all internal HLL fluxes are
identical to the physical flux F(W) = [0, P, 0] for u=0 state),
and we pass in boundary fluxes equal to [0, P, 0] as well, then
every difference (flux[i+1] - flux[i]) is zero, so W must not
change after one step. Verify to machine precision.
"""
pipe = Pipe(L=1.0, D=5e-3, N=20,
P_init=1e5, T_init=300.0,
gamma=GAMMA, R_gas=R_GAS)
W_before = pipe.W.copy()
# Boundary flux matching the static interior: [rho*u, rho*u^2+P, u*(E+P)]
# with u=0 -> [0, P, 0]
flux_boundary = np.array([0.0, 1e5, 0.0])
pipe.step(flux_boundary, flux_boundary, dt=1e-5)
assert np.allclose(pipe.W, W_before, atol=1e-6, rtol=1e-12)