Files
SPHLite/Python/sphlite_debug.py
T

263 lines
9.0 KiB
Python
Raw Normal View History

2026-07-02 18:47:38 +08:00
from __future__ import annotations
import ctypes
from pathlib import Path
2026-07-02 19:50:44 +08:00
PHASE_WATER = 1
PHASE_GAS = 2
PHASE_EXPLOSIVE = 3
PHASE_SOLID = 4
PHASE_SHELL = 5
PHASE_BOUNDARY = 6
PHASE_COUPLING = 7
PHASE_SOLID_FAILED = 8
PHASE_SHELL_FAILED = 9
STATUS_ACTIVE = 0
STATUS_INACTIVE = 1
STATUS_FAILED = 2
STATUS_RESERVED = 3
2026-07-02 18:47:38 +08:00
class SPHLiteError(RuntimeError):
pass
class SPHLite:
def __init__(self, library: str | Path):
self.library_path = Path(library)
self.lib = ctypes.CDLL(str(self.library_path))
self._configure()
def _configure(self) -> None:
self.lib.sphlite_initialize.restype = ctypes.c_int
self.lib.sphlite_finalize.restype = ctypes.c_int
self.lib.sphlite_read_inp.argtypes = [ctypes.c_char_p]
self.lib.sphlite_read_inp.restype = ctypes.c_int
self.lib.sphlite_build_model.restype = ctypes.c_int
self.lib.sphlite_run_explicit.restype = ctypes.c_int
self.lib.sphlite_get_particle_count.argtypes = [ctypes.POINTER(ctypes.c_int)]
self.lib.sphlite_get_particle_count.restype = ctypes.c_int
2026-07-02 19:50:44 +08:00
self.lib.sphlite_get_input_warning_count.argtypes = [ctypes.POINTER(ctypes.c_int)]
self.lib.sphlite_get_input_warning_count.restype = ctypes.c_int
self.lib.sphlite_set_output_directory.argtypes = [ctypes.c_char_p]
self.lib.sphlite_set_output_directory.restype = ctypes.c_int
self.lib.sphlite_set_output_interval.argtypes = [ctypes.c_int]
self.lib.sphlite_set_output_interval.restype = ctypes.c_int
self.lib.sphlite_write_output.argtypes = [ctypes.c_int, ctypes.c_double]
self.lib.sphlite_write_output.restype = ctypes.c_int
self.lib.sphlite_write_restart.argtypes = [ctypes.c_char_p]
self.lib.sphlite_write_restart.restype = ctypes.c_int
self.lib.sphlite_read_restart.argtypes = [ctypes.c_char_p]
self.lib.sphlite_read_restart.restype = ctypes.c_int
self.lib.sphlite_get_water_summary.argtypes = [
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
]
self.lib.sphlite_get_water_summary.restype = ctypes.c_int
self.lib.sphlite_get_water_dt_limits.argtypes = [
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
]
self.lib.sphlite_get_water_dt_limits.restype = ctypes.c_int
2026-07-02 19:50:44 +08:00
self.lib.sphlite_get_particle_state.argtypes = [
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_int),
]
self.lib.sphlite_get_particle_state.restype = ctypes.c_int
2026-07-02 18:47:38 +08:00
self.lib.sphlite_get_last_error.argtypes = [
ctypes.POINTER(ctypes.c_int),
ctypes.c_char_p,
ctypes.c_int,
]
self.lib.sphlite_get_last_error.restype = ctypes.c_int
def _check(self, ierr: int) -> None:
if ierr == 0:
return
code = ctypes.c_int(0)
buffer = ctypes.create_string_buffer(256)
rc = self.lib.sphlite_get_last_error(ctypes.byref(code), buffer, len(buffer))
if rc == 0:
raise SPHLiteError(buffer.value.decode("utf-8", errors="replace"))
raise SPHLiteError(f"SPHLite error {ierr}")
def initialize(self) -> None:
self._check(self.lib.sphlite_initialize())
def finalize(self) -> None:
self._check(self.lib.sphlite_finalize())
def read_inp(self, filename: str | Path) -> None:
encoded = str(Path(filename)).encode("utf-8")
self._check(self.lib.sphlite_read_inp(encoded))
def build_model(self) -> None:
self._check(self.lib.sphlite_build_model())
def particle_count(self) -> int:
count = ctypes.c_int(0)
self._check(self.lib.sphlite_get_particle_count(ctypes.byref(count)))
return int(count.value)
2026-07-02 19:50:44 +08:00
def warning_count(self) -> int:
count = ctypes.c_int(0)
self._check(self.lib.sphlite_get_input_warning_count(ctypes.byref(count)))
return int(count.value)
def particle_state(self) -> dict[str, list[float] | list[int]]:
count = self.particle_count()
int_array = ctypes.c_int * count
vec_array = ctypes.c_double * (3 * count)
real_array = ctypes.c_double * count
particle_id_ext = int_array()
phase = int_array()
status = int_array()
material_id = int_array()
section_id = int_array()
x = vec_array()
v = vec_array()
a = vec_array()
rho = real_array()
pressure = real_array()
internal_energy = real_array()
temperature = real_array()
h = real_array()
actual = ctypes.c_int(0)
self._check(self.lib.sphlite_get_particle_state(
count,
particle_id_ext,
phase,
status,
material_id,
section_id,
x,
v,
a,
rho,
pressure,
internal_energy,
temperature,
h,
ctypes.byref(actual),
))
return {
"particle_id_ext": list(particle_id_ext),
"phase": list(phase),
"status": list(status),
"material_id": list(material_id),
"section_id": list(section_id),
"x": list(x),
"v": list(v),
"a": list(a),
"rho": list(rho),
"pressure": list(pressure),
"internal_energy": list(internal_energy),
"temperature": list(temperature),
"h": list(h),
}
2026-07-02 18:47:38 +08:00
def run_explicit(self) -> None:
self._check(self.lib.sphlite_run_explicit())
2026-07-02 19:50:44 +08:00
def set_output_directory(self, directory: str | Path) -> None:
self._check(self.lib.sphlite_set_output_directory(str(directory).encode("utf-8")))
def set_output_interval(self, interval: int) -> None:
self._check(self.lib.sphlite_set_output_interval(int(interval)))
def write_output(self, step: int = 0, time: float = 0.0) -> None:
self._check(self.lib.sphlite_write_output(int(step), float(time)))
def write_restart(self, filename: str | Path) -> None:
self._check(self.lib.sphlite_write_restart(str(filename).encode("utf-8")))
def read_restart(self, filename: str | Path) -> None:
self._check(self.lib.sphlite_read_restart(str(filename).encode("utf-8")))
def water_summary(self) -> dict[str, float | list[float]]:
mass = ctypes.c_double(0.0)
momentum_type = ctypes.c_double * 3
momentum = momentum_type()
max_pressure = ctypes.c_double(0.0)
min_density = ctypes.c_double(0.0)
max_density = ctypes.c_double(0.0)
self._check(self.lib.sphlite_get_water_summary(
ctypes.byref(mass),
momentum,
ctypes.byref(max_pressure),
ctypes.byref(min_density),
ctypes.byref(max_density),
))
return {
"water_mass": float(mass.value),
"water_momentum": list(momentum),
"max_pressure": float(max_pressure.value),
"min_density": float(min_density.value),
"max_density": float(max_density.value),
}
def water_dt_limits(self) -> dict[str, float]:
dt_cfl = ctypes.c_double(0.0)
dt_accel = ctypes.c_double(0.0)
dt_viscous = ctypes.c_double(0.0)
self._check(self.lib.sphlite_get_water_dt_limits(
ctypes.byref(dt_cfl),
ctypes.byref(dt_accel),
ctypes.byref(dt_viscous),
))
return {
"dt_cfl": float(dt_cfl.value),
"dt_accel": float(dt_accel.value),
"dt_viscous": float(dt_viscous.value),
}
def inspect(self, filename: str | Path) -> dict[str, int | float | list[float]]:
2026-07-02 19:50:44 +08:00
self.read_inp(filename)
self.build_model()
summary: dict[str, int | float | list[float]] = {
"particles": self.particle_count(),
"warnings": self.warning_count(),
}
summary.update(self.water_summary())
summary.update(self.water_dt_limits())
return summary
2026-07-02 19:50:44 +08:00
def run(self, filename: str | Path) -> dict[str, int | float | list[float]]:
2026-07-02 19:50:44 +08:00
summary = self.inspect(filename)
self.run_explicit()
summary["particles"] = self.particle_count()
summary.update(self.water_summary())
summary.update(self.water_dt_limits())
2026-07-02 19:50:44 +08:00
return summary