Files
SPHLite/Python/sphlite_debug.py
T

202 lines
6.8 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_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 inspect(self, filename: str | Path) -> dict[str, int]:
self.read_inp(filename)
self.build_model()
return {"particles": self.particle_count(), "warnings": self.warning_count()}
def run(self, filename: str | Path) -> dict[str, int]:
summary = self.inspect(filename)
self.run_explicit()
summary["particles"] = self.particle_count()
return summary