from __future__ import annotations import argparse import sys from pathlib import Path def write_state_050_compat(source: Path, target: Path, npart: int) -> None: lines = source.read_text(encoding="utf-8").splitlines() solid_material_count = 1 solid_051_lines = 1 + 6 * solid_material_count + 8 + 9 * npart solid_start = len(lines) - solid_051_lines if solid_start <= 0: raise AssertionError("could not locate Solid restart block") lines[0] = "SPHLITE_RESTART 0 5 0 STATE_0_5" def skip_solid_line(relative_line: int) -> bool: material_end = 1 + 6 * solid_material_count if relative_line in (material_end + 6, material_end + 7): return True particle_start = material_end + 9 if relative_line < particle_start: return False particle_line = (relative_line - particle_start) % 9 return particle_line in (5, 6, 7) output: list[str] = [] for index, line in enumerate(lines): relative_line = index - solid_start + 1 if relative_line > 0 and skip_solid_line(relative_line): continue output.append(line) target.write_text("\n".join(output) + "\n", encoding="utf-8") 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 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") solver = SPHLite(args.library) 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") if not hasattr(solver.lib, "sphlite_get_gas_summary"): raise AssertionError("Python wrapper is missing Gas C ABI diagnostics") if not hasattr(solver.lib, "sphlite_get_gas_extended_summary"): raise AssertionError("Python wrapper is missing Gas extended C ABI diagnostics") 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_get_explosive_release_balance"): raise AssertionError("Python wrapper is missing Explosive release C ABI") if not hasattr(solver.lib, "sphlite_set_ignition_point"): raise AssertionError("Python wrapper is missing Explosive ignition C ABI") if not hasattr(solver.lib, "sphlite_get_solid_summary"): raise AssertionError("Python wrapper is missing Solid C ABI diagnostics") if not hasattr(solver.lib, "sphlite_get_solid_particle_state"): raise AssertionError("Python wrapper is missing Solid particle state C ABI") if not hasattr(solver.lib, "sphlite_get_solid_diagnostics"): raise AssertionError("Python wrapper is missing Solid 0.5.1 diagnostics C ABI") if solver.version() != (0, 5, 1): raise AssertionError("Python version mismatch") 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}") state = solver.particle_state() if state["phase"][0] != PHASE_SOLID: raise AssertionError("unexpected particle phase") solid_state = solver.solid_particle_state() if len(solid_state["solid_values"]) != 22 * count: raise AssertionError("solid particle state value layout mismatch") if solid_state["solid_failure_status"][0] != STATUS_ACTIVE: raise AssertionError("solid particle failure status mismatch") solid_summary = solver.inspect(args.input) if solid_summary["particles"] != 4: raise AssertionError("solid inspect summary mismatch") if "water_mass" not in solid_summary or "dt_cfl" not in solid_summary: raise AssertionError("inspect helper did not include Water diagnostics") if "gas_volume" not in solid_summary or "gas_dtcrit" not in solid_summary: raise AssertionError("inspect helper did not include Gas diagnostics") 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") if "explosive_particles" not in solid_summary or "explosive_lambda_range" not in solid_summary: raise AssertionError("inspect helper did not include Explosive diagnostics") if "explosive_release_mass_values" not in solid_summary: raise AssertionError("inspect helper did not include Explosive release balance") if solid_summary["solid_particles"] != 4 or solid_summary["solid_mass"] <= 0.0: raise AssertionError("inspect helper did not include Solid diagnostics") if solid_summary["solid_dt_limits"][0] <= 0.0: raise AssertionError("inspect helper did not include Solid dt diagnostics") if "solid_max_strain_rate" not in solid_summary: raise AssertionError("inspect helper did not include Solid 0.5.1 diagnostics") if "solid_reference_neighbor_min" not in solid_summary: raise AssertionError("inspect helper did not include Solid reference quality") 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) restart_summary = solver.solid_summary() if restart_summary["solid_particles"] != 4: raise AssertionError("Solid restart summary mismatch") restart_050 = root / "build-codex-unix" / "python_restart_050.tmp" write_state_050_compat(restart, restart_050, count) solver.read_restart(restart_050) compat_summary = solver.solid_summary() if compat_summary["solid_particles"] != 4: raise AssertionError("Solid 0.5.0 restart summary mismatch") compat_state = solver.solid_particle_state() if len(compat_state["solid_values"]) != 22 * count: raise AssertionError("Solid 0.5.0 restart particle state mismatch") solver.run_explicit() finally: solver.finalize() 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) 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() 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") 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") finally: gas.finalize() 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") explosive_balance = explosive.explosive_release_balance() if explosive_balance["explosive_release_mass_values"][0] <= 0.0: raise AssertionError("explosive release balance mass mismatch") if explosive_balance["explosive_release_energy_values"][0] <= 0.0: raise AssertionError("explosive release balance 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() 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() return 0 if __name__ == "__main__": raise SystemExit(main())