from __future__ import annotations import ctypes from pathlib import Path 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 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_get_version.argtypes = [ ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ] self.lib.sphlite_get_version.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 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 self.lib.sphlite_get_gas_summary.argtypes = [ ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ] self.lib.sphlite_get_gas_summary.restype = ctypes.c_int self.lib.sphlite_get_gas_bubble_metrics.argtypes = [ ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ] self.lib.sphlite_get_gas_bubble_metrics.restype = ctypes.c_int self.lib.sphlite_get_gas_dt_limits.argtypes = [ ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ] self.lib.sphlite_get_gas_dt_limits.restype = ctypes.c_int self.lib.sphlite_get_gas_extended_summary.argtypes = [ ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ] self.lib.sphlite_get_gas_extended_summary.restype = ctypes.c_int self.lib.sphlite_get_gas_source_summary.argtypes = [ ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ] self.lib.sphlite_get_gas_source_summary.restype = ctypes.c_int self.lib.sphlite_set_ignition_point.argtypes = [ ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ] self.lib.sphlite_set_ignition_point.restype = ctypes.c_int self.lib.sphlite_get_explosive_summary.argtypes = [ 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), ] self.lib.sphlite_get_explosive_summary.restype = ctypes.c_int self.lib.sphlite_get_explosive_release_balance.argtypes = [ ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double), ] self.lib.sphlite_get_explosive_release_balance.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 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 version(self) -> tuple[int, int, int]: major = ctypes.c_int(0) minor = ctypes.c_int(0) patch = ctypes.c_int(0) self._check(self.lib.sphlite_get_version( ctypes.byref(major), ctypes.byref(minor), ctypes.byref(patch), )) return int(major.value), int(minor.value), int(patch.value) 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) 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), } def run_explicit(self) -> None: self._check(self.lib.sphlite_run_explicit()) 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 gas_summary(self) -> dict[str, float | list[float]]: volume = ctypes.c_double(0.0) center_type = ctypes.c_double * 3 center = center_type() mean_pressure = ctypes.c_double(0.0) max_pressure = ctypes.c_double(0.0) self._check(self.lib.sphlite_get_gas_summary( ctypes.byref(volume), center, ctypes.byref(mean_pressure), ctypes.byref(max_pressure), )) return { "gas_volume": float(volume.value), "gas_center": list(center), "gas_mean_pressure": float(mean_pressure.value), "gas_max_pressure": float(max_pressure.value), } def gas_bubble_metrics(self) -> dict[str, float | list[float]]: equivalent_radius = ctypes.c_double(0.0) radius_rate = ctypes.c_double(0.0) center_type = ctypes.c_double * 3 center = center_type() self._check(self.lib.sphlite_get_gas_bubble_metrics( ctypes.byref(equivalent_radius), ctypes.byref(radius_rate), center, )) return { "gas_equivalent_radius": float(equivalent_radius.value), "gas_radius_rate": float(radius_rate.value), "gas_bubble_center": list(center), } def gas_dt_limits(self) -> dict[str, float]: dt_cfl = ctypes.c_double(0.0) dt_accel = ctypes.c_double(0.0) dtcrit = ctypes.c_double(0.0) self._check(self.lib.sphlite_get_gas_dt_limits( ctypes.byref(dt_cfl), ctypes.byref(dt_accel), ctypes.byref(dtcrit), )) return { "gas_dt_cfl": float(dt_cfl.value), "gas_dt_accel": float(dt_accel.value), "gas_dtcrit": float(dtcrit.value), } def gas_extended_summary(self) -> dict[str, float | list[float]]: mass = ctypes.c_double(0.0) momentum_type = ctypes.c_double * 3 range_type = ctypes.c_double * 2 limits_type = ctypes.c_double * 3 momentum = momentum_type() density_range = range_type() dt_limits = limits_type() self._check(self.lib.sphlite_get_gas_extended_summary( ctypes.byref(mass), momentum, density_range, dt_limits, )) return { "gas_mass": float(mass.value), "gas_momentum": list(momentum), "gas_density_range": list(density_range), "gas_dt_limits": list(dt_limits), } def gas_source_summary(self) -> dict[str, float | list[float]]: mass_source = ctypes.c_double(0.0) energy_source = ctypes.c_double(0.0) momentum_type = ctypes.c_double * 3 momentum_source = momentum_type() self._check(self.lib.sphlite_get_gas_source_summary( ctypes.byref(mass_source), momentum_source, ctypes.byref(energy_source), )) return { "gas_source_mass": float(mass_source.value), "gas_source_momentum": list(momentum_source), "gas_source_energy": float(energy_source.value), } def set_ignition_point(self, x: float, y: float, z: float, time: float) -> None: self._check(self.lib.sphlite_set_ignition_point( float(x), float(y), float(z), float(time), )) def explosive_summary(self) -> dict[str, int | float | list[float]]: particle_count = ctypes.c_int(0) reacted_count = ctypes.c_int(0) lambda_type = ctypes.c_double * 3 released_type = ctypes.c_double * 5 time_type = ctypes.c_double * 2 lambda_range = lambda_type() released_totals = released_type() detonation_range = time_type() self._check(self.lib.sphlite_get_explosive_summary( ctypes.byref(particle_count), ctypes.byref(reacted_count), lambda_range, released_totals, detonation_range, )) return { "explosive_particles": int(particle_count.value), "explosive_reacted_particles": int(reacted_count.value), "explosive_lambda_range": list(lambda_range), "explosive_released_totals": list(released_totals), "explosive_detonation_time_range": list(detonation_range), } def explosive_release_balance(self) -> dict[str, float | list[float]]: mass_type = ctypes.c_double * 4 energy_type = ctypes.c_double * 2 momentum_type = ctypes.c_double * 3 mass_values = mass_type() energy_values = energy_type() momentum_values = momentum_type() self._check(self.lib.sphlite_get_explosive_release_balance( mass_values, energy_values, momentum_values)) return { "explosive_release_mass_values": list(mass_values), "explosive_release_energy_values": list(energy_values), "explosive_release_momentum_values": list(momentum_values), } def inspect(self, filename: str | Path) -> dict[str, int | float | list[float]]: 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()) summary.update(self.gas_summary()) summary.update(self.gas_bubble_metrics()) summary.update(self.gas_dt_limits()) summary.update(self.gas_extended_summary()) summary.update(self.gas_source_summary()) summary.update(self.explosive_summary()) summary.update(self.explosive_release_balance()) return summary def run(self, filename: str | Path) -> dict[str, int | float | list[float]]: summary = self.inspect(filename) self.run_explicit() summary["particles"] = self.particle_count() summary.update(self.water_summary()) summary.update(self.water_dt_limits()) summary.update(self.gas_summary()) summary.update(self.gas_bubble_metrics()) summary.update(self.gas_dt_limits()) summary.update(self.gas_extended_summary()) summary.update(self.gas_source_summary()) summary.update(self.explosive_summary()) summary.update(self.explosive_release_balance()) return summary