Files
SPHLite/tools/testing/test_python_debug.py
T

113 lines
4.1 KiB
Python
Raw Normal View History

2026-07-02 18:47:38 +08:00
from __future__ import annotations
import argparse
import sys
from pathlib import Path
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--library", required=True)
parser.add_argument("--input", required=True)
args = parser.parse_args()
root = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(root / "Python"))
from sphlite_debug import PHASE_SOLID, PHASE_WATER, STATUS_ACTIVE, SPHLite, SPHLiteError
2026-07-02 18:47:38 +08:00
2026-07-02 23:53:17 +08:00
wrapper_source = (root / "python" / "sphlite_debug.py").read_text(encoding="utf-8")
if "ctypes.CDLL" not in wrapper_source:
raise AssertionError("Python wrapper is not loading the C ABI library")
if "sphft_" in wrapper_source:
raise AssertionError("Python wrapper directly references Fortran symbols")
2026-07-02 18:47:38 +08:00
solver = SPHLite(args.library)
2026-07-02 19:50:44 +08:00
if PHASE_SOLID != 4 or STATUS_ACTIVE != 0:
raise AssertionError("Python constants are not locked")
if not hasattr(solver.lib, "sphlite_get_water_summary"):
raise AssertionError("Python wrapper is not using the C ABI library")
2026-07-02 22:33:21 +08:00
if solver.version() != (0, 2, 1):
raise AssertionError("Python version mismatch")
2026-07-02 19:50:44 +08:00
2026-07-02 18:47:38 +08:00
solver.initialize()
try:
solver.read_inp(args.input)
solver.build_model()
count = solver.particle_count()
if count != 4:
raise AssertionError(f"expected 4 particles, got {count}")
2026-07-02 19:50:44 +08:00
state = solver.particle_state()
if state["phase"][0] != PHASE_SOLID:
raise AssertionError("unexpected particle phase")
solid_summary = solver.inspect(args.input)
if solid_summary["particles"] != 4:
raise AssertionError("solid inspect summary mismatch")
2026-07-02 23:53:17 +08:00
if "water_mass" not in solid_summary or "dt_cfl" not in solid_summary:
raise AssertionError("inspect helper did not include Water diagnostics")
2026-07-02 19:50:44 +08:00
solver.set_output_directory(root / "build-codex-unix")
solver.write_output(0, 0.0)
restart = root / "build-codex-unix" / "python_restart.tmp"
solver.write_restart(restart)
solver.read_restart(restart)
2026-07-02 18:47:38 +08:00
solver.run_explicit()
finally:
solver.finalize()
2026-07-02 19:50:44 +08:00
water = SPHLite(args.library)
water.initialize()
try:
water_input = root / "tests" / "data" / "water_minimal_020.inp"
water_summary = water.inspect(water_input)
if water_summary["particles"] != 4:
raise AssertionError("water inspect particle count mismatch")
if water_summary["water_mass"] <= 0.0:
raise AssertionError("water summary mass mismatch")
if len(water_summary["water_momentum"]) != 3:
raise AssertionError("water summary momentum shape mismatch")
if (
water_summary["dt_cfl"] <= 0.0
or water_summary["dt_accel"] <= 0.0
or water_summary["dt_viscous"] <= 0.0
):
raise AssertionError("water dt limits mismatch")
water_state = water.particle_state()
if water_state["phase"][0] != PHASE_WATER:
raise AssertionError("unexpected water particle phase")
run_summary = water.run(water_input)
2026-07-02 23:53:17 +08:00
if run_summary["particles"] != 4:
raise AssertionError("water run particle count mismatch")
if run_summary["water_mass"] <= 0.0 or run_summary["dt_cfl"] <= 0.0:
raise AssertionError("water run summary mismatch")
finally:
water.finalize()
bad_water = SPHLite(args.library)
bad_water.initialize()
try:
try:
bad_water.inspect(root / "tests" / "data" / "water_missing_eos_020.inp")
except SPHLiteError:
pass
else:
raise AssertionError("missing Water parameter input did not raise")
finally:
bad_water.finalize()
2026-07-02 19:50:44 +08:00
missing = SPHLite(args.library)
missing.initialize()
try:
try:
missing.read_inp(root / "tests" / "data" / "does_not_exist.inp")
except SPHLiteError:
pass
else:
raise AssertionError("missing input did not raise")
finally:
missing.finalize()
2026-07-02 18:47:38 +08:00
return 0
if __name__ == "__main__":
raise SystemExit(main())