Prepare SPHLite 0.2.0 development baseline

This commit is contained in:
Codex
2026-07-02 19:50:44 +08:00
parent ad31c2d5cc
commit ed1f5a5ab2
36 changed files with 3122 additions and 267 deletions
+135
View File
@@ -3,6 +3,21 @@ 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
@@ -27,6 +42,43 @@ class SPHLite:
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_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,
@@ -62,5 +114,88 @@ class SPHLite:
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 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