Files
SPHLite/tools/testing/test_python_debug.py
T

169 lines
7.2 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-03 09:55:48 +08:00
if not hasattr(solver.lib, "sphlite_get_gas_summary"):
raise AssertionError("Python wrapper is missing Gas C ABI diagnostics")
2026-07-03 11:04:59 +08:00
if not hasattr(solver.lib, "sphlite_get_gas_extended_summary"):
raise AssertionError("Python wrapper is missing Gas extended C ABI diagnostics")
2026-07-03 12:32:14 +08:00
if not hasattr(solver.lib, "sphlite_get_explosive_summary"):
raise AssertionError("Python wrapper is missing Explosive C ABI diagnostics")
if not hasattr(solver.lib, "sphlite_set_ignition_point"):
raise AssertionError("Python wrapper is missing Explosive ignition C ABI")
2026-07-03 11:50:07 +08:00
if solver.version() != (0, 4, 0):
2026-07-02 22:33:21 +08:00
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-03 09:55:48 +08:00
if "gas_volume" not in solid_summary or "gas_dtcrit" not in solid_summary:
raise AssertionError("inspect helper did not include Gas diagnostics")
2026-07-03 11:04:59 +08:00
if "gas_source_mass" not in solid_summary or "gas_density_range" not in solid_summary:
raise AssertionError("inspect helper did not include Gas extended diagnostics")
2026-07-03 12:32:14 +08:00
if "explosive_particles" not in solid_summary or "explosive_lambda_range" not in solid_summary:
raise AssertionError("inspect helper did not include Explosive 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()
2026-07-03 09:55:48 +08:00
gas = SPHLite(args.library)
gas.initialize()
try:
gas_input = root / "tests" / "data" / "gas_ideal_minimal_030.inp"
gas_summary = gas.inspect(gas_input)
if gas_summary["particles"] != 4:
raise AssertionError("gas inspect particle count mismatch")
if gas_summary["gas_volume"] <= 0.0:
raise AssertionError("gas summary volume mismatch")
if gas_summary["gas_max_pressure"] <= 0.0:
raise AssertionError("gas summary pressure mismatch")
if gas_summary["gas_equivalent_radius"] <= 0.0:
raise AssertionError("gas bubble radius mismatch")
if gas_summary["gas_dtcrit"] <= 0.0:
raise AssertionError("gas dt mismatch")
2026-07-03 11:04:59 +08:00
if gas_summary["gas_mass"] <= 0.0:
raise AssertionError("gas extended mass mismatch")
if gas_summary["gas_source_energy"] != 0.0:
raise AssertionError("gas source summary default mismatch")
2026-07-03 09:55:48 +08:00
finally:
gas.finalize()
2026-07-03 12:32:14 +08:00
explosive = SPHLite(args.library)
explosive.initialize()
try:
explosive_input = root / "tests" / "data" / "explosive_conversion_phase_040.inp"
explosive.read_inp(explosive_input)
explosive.set_ignition_point(0.0, 0.0, 0.0, 0.0)
explosive.build_model()
explosive.run_explicit()
explosive_summary = explosive.explosive_summary()
if explosive_summary["explosive_particles"] <= 0:
raise AssertionError("explosive summary particle count mismatch")
if explosive_summary["explosive_reacted_particles"] <= 0:
raise AssertionError("explosive summary reacted count mismatch")
if explosive_summary["explosive_lambda_range"][1] <= 0.0:
raise AssertionError("explosive summary lambda mismatch")
if explosive_summary["explosive_released_totals"][1] <= 0.0:
raise AssertionError("explosive summary energy mismatch")
finally:
explosive.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())