Files
SPHLite/Python/sphlite_debug.py
T

931 lines
35 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-03 18:46:18 +08:00
from sphlite_solid import configure_solid_api
from sphlite_solid import solid_diagnostics as _solid_diagnostics
from sphlite_solid import solid_particle_state as _solid_particle_state
from sphlite_solid import solid_reference_quality as _solid_reference_quality
from sphlite_solid import solid_summary as _solid_summary
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-04 09:59:52 +08:00
COUPLING_REGION_CONTACT = 1
COUPLING_REGION_CONTINUUM = 2
COUPLING_EXPORT_PARTICLE = 1
COUPLING_EXPORT_FACET = 2
COUPLING_EXPORT_BOTH = 3
COUPLING_IMPORT_FORCE = 1
COUPLING_IMPORT_FACET_LOAD = 2
COUPLING_IMPORT_MOTION = 3
2026-07-04 11:43:00 +08:00
COUPLING_LOAD_PARTICLE_FORCE = 1
COUPLING_LOAD_FACET_FORCE = 2
COUPLING_LOAD_PRESSURE = 3
COUPLING_LOAD_TRACTION = 4
COUPLING_DISTRIBUTION_UNIFORM = 1
COUPLING_DISTRIBUTION_AREA = 2
COUPLING_DISTRIBUTION_MASS = 3
COUPLING_CONTACT_STATE_NONE = 0
COUPLING_CONTACT_STATE_IMPORT = 1
2026-07-04 09:59:52 +08:00
COUPLING_PARTICLE_SCALAR_COUNT = 7
COUPLING_PARTICLE_VECTOR_COUNT = 3
2026-07-04 11:43:00 +08:00
COUPLING_DIAGNOSTIC_VALUE_COUNT = 27
COUPLING_DIAGNOSTIC_COUNT_COUNT = 12
COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT = 1
COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT = 7
2026-07-04 09:59:52 +08:00
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:
2026-07-03 16:21:51 +08:00
int_p = ctypes.POINTER(ctypes.c_int)
double_p = ctypes.POINTER(ctypes.c_double)
2026-07-02 18:47:38 +08:00
self.lib.sphlite_initialize.restype = ctypes.c_int
self.lib.sphlite_finalize.restype = ctypes.c_int
2026-07-03 16:21:51 +08:00
self.lib.sphlite_get_version.argtypes = [int_p, int_p, int_p]
2026-07-02 22:33:21 +08:00
self.lib.sphlite_get_version.restype = ctypes.c_int
2026-07-02 18:47:38 +08:00
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
2026-07-03 16:21:51 +08:00
self.lib.sphlite_get_water_summary.argtypes = [double_p] * 5
self.lib.sphlite_get_water_summary.restype = ctypes.c_int
2026-07-03 16:21:51 +08:00
self.lib.sphlite_get_water_dt_limits.argtypes = [double_p] * 3
self.lib.sphlite_get_water_dt_limits.restype = ctypes.c_int
2026-07-03 16:21:51 +08:00
self.lib.sphlite_get_gas_summary.argtypes = [double_p] * 4
2026-07-03 09:55:48 +08:00
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
2026-07-03 11:04:59 +08:00
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
2026-07-03 12:32:14 +08:00
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
2026-07-03 13:52:49 +08:00
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
2026-07-03 18:46:18 +08:00
configure_solid_api(self.lib)
2026-07-03 12:32:14 +08:00
2026-07-04 09:59:52 +08:00
self.lib.sphlite_coupling_get_region_count.argtypes = [
ctypes.POINTER(ctypes.c_int)
]
self.lib.sphlite_coupling_get_region_count.restype = ctypes.c_int
self.lib.sphlite_coupling_get_region_info.argtypes = [
ctypes.c_int,
ctypes.c_char_p,
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_int),
]
self.lib.sphlite_coupling_get_region_info.restype = ctypes.c_int
2026-07-04 11:43:00 +08:00
self.lib.sphlite_coupling_get_region_modes.argtypes = [
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int),
]
self.lib.sphlite_coupling_get_region_modes.restype = ctypes.c_int
2026-07-04 09:59:52 +08:00
self.lib.sphlite_coupling_export_particles.argtypes = [
ctypes.c_int,
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_int),
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
]
self.lib.sphlite_coupling_export_particles.restype = ctypes.c_int
self.lib.sphlite_coupling_apply_particle_forces.argtypes = [
ctypes.c_int,
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_double),
]
self.lib.sphlite_coupling_apply_particle_forces.restype = ctypes.c_int
2026-07-04 11:43:00 +08:00
self.lib.sphlite_coupling_apply_facet_pressures.argtypes = [
ctypes.c_int,
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_double),
ctypes.c_int,
ctypes.c_int,
]
self.lib.sphlite_coupling_apply_facet_pressures.restype = ctypes.c_int
self.lib.sphlite_coupling_apply_facet_tractions.argtypes = [
ctypes.c_int,
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_double),
ctypes.c_int,
ctypes.c_int,
]
self.lib.sphlite_coupling_apply_facet_tractions.restype = ctypes.c_int
self.lib.sphlite_coupling_apply_facet_resultants.argtypes = [
ctypes.c_int,
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_double),
ctypes.c_int,
ctypes.c_int,
]
self.lib.sphlite_coupling_apply_facet_resultants.restype = ctypes.c_int
self.lib.sphlite_coupling_apply_contact_state.argtypes = [
ctypes.c_int,
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.c_int,
]
self.lib.sphlite_coupling_apply_contact_state.restype = ctypes.c_int
self.lib.sphlite_coupling_get_load_balance.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),
ctypes.POINTER(ctypes.c_double),
]
self.lib.sphlite_coupling_get_load_balance.restype = ctypes.c_int
self.lib.sphlite_coupling_get_contact_diagnostics.argtypes = [
ctypes.POINTER(ctypes.c_int),
ctypes.c_int,
ctypes.POINTER(ctypes.c_double),
ctypes.c_int,
]
self.lib.sphlite_coupling_get_contact_diagnostics.restype = ctypes.c_int
2026-07-04 09:59:52 +08:00
self.lib.sphlite_coupling_get_diagnostics.argtypes = [
ctypes.POINTER(ctypes.c_double),
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
ctypes.c_int,
]
self.lib.sphlite_coupling_get_diagnostics.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())
2026-07-02 22:33:21 +08:00
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)
2026-07-02 18:47:38 +08:00
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),
}
2026-07-03 09:55:48 +08:00
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),
}
2026-07-03 11:04:59 +08:00
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),
}
2026-07-03 12:32:14 +08:00
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),
}
2026-07-03 13:52:49 +08:00
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),
}
2026-07-03 16:21:51 +08:00
def solid_summary(self) -> dict[str, int | float | list[float]]:
2026-07-03 18:46:18 +08:00
return _solid_summary(self)
2026-07-03 18:26:41 +08:00
def solid_particle_state(self) -> dict[str, list[float] | list[int]]:
2026-07-03 18:46:18 +08:00
return _solid_particle_state(self)
2026-07-03 18:26:41 +08:00
def solid_diagnostics(self) -> dict[str, float]:
2026-07-03 18:46:18 +08:00
return _solid_diagnostics(self)
2026-07-03 18:26:41 +08:00
def solid_reference_quality(self) -> dict[str, int | float]:
2026-07-03 18:46:18 +08:00
return _solid_reference_quality(self)
2026-07-03 16:21:51 +08:00
2026-07-04 09:59:52 +08:00
def coupling_region_count(self) -> int:
count = ctypes.c_int(0)
self._check(self.lib.sphlite_coupling_get_region_count(ctypes.byref(count)))
return int(count.value)
def coupling_region_info(self, region_index: int) -> dict[str, int | str]:
buffer = ctypes.create_string_buffer(128)
region_type = ctypes.c_int(0)
export_mode = ctypes.c_int(0)
import_mode = ctypes.c_int(0)
enabled = ctypes.c_int(0)
particle_count = ctypes.c_int(0)
facet_count = ctypes.c_int(0)
2026-07-04 11:43:00 +08:00
load_mode = ctypes.c_int(0)
distribution_mode = ctypes.c_int(0)
contact_state_mode = ctypes.c_int(0)
2026-07-04 09:59:52 +08:00
self._check(self.lib.sphlite_coupling_get_region_info(
int(region_index),
buffer,
len(buffer),
ctypes.byref(region_type),
ctypes.byref(export_mode),
ctypes.byref(import_mode),
ctypes.byref(enabled),
ctypes.byref(particle_count),
ctypes.byref(facet_count),
))
2026-07-04 11:43:00 +08:00
self._check(self.lib.sphlite_coupling_get_region_modes(
int(region_index),
ctypes.byref(load_mode),
ctypes.byref(distribution_mode),
ctypes.byref(contact_state_mode),
))
2026-07-04 09:59:52 +08:00
return {
"name": buffer.value.decode("utf-8", errors="replace"),
"region_type": int(region_type.value),
"export_mode": int(export_mode.value),
"import_mode": int(import_mode.value),
2026-07-04 11:43:00 +08:00
"load_mode": int(load_mode.value),
"distribution_mode": int(distribution_mode.value),
"contact_state_mode": int(contact_state_mode.value),
2026-07-04 09:59:52 +08:00
"enabled": int(enabled.value),
"particle_count": int(particle_count.value),
"facet_count": int(facet_count.value),
}
def coupling_particle_export(self, region_index: int) -> dict[str, list[float] | list[int]]:
info = self.coupling_region_info(region_index)
capacity = max(1, int(info["particle_count"]))
int_array = ctypes.c_int * capacity
scalar_array = ctypes.c_double * (COUPLING_PARTICLE_SCALAR_COUNT * capacity)
vector_array = ctypes.c_double * (3 * COUPLING_PARTICLE_VECTOR_COUNT * capacity)
ext_ids = int_array()
internal_indices = int_array()
phases = int_array()
statuses = int_array()
materials = int_array()
sections = int_array()
scalar_values = scalar_array()
vector_values = vector_array()
count = ctypes.c_int(0)
self._check(self.lib.sphlite_coupling_export_particles(
int(region_index),
capacity,
ctypes.byref(count),
ext_ids,
internal_indices,
phases,
statuses,
materials,
sections,
scalar_values,
vector_values,
))
actual = int(count.value)
return {
"ext_ids": list(ext_ids)[:actual],
"internal_indices": list(internal_indices)[:actual],
"phases": list(phases)[:actual],
"statuses": list(statuses)[:actual],
"materials": list(materials)[:actual],
"sections": list(sections)[:actual],
"scalar_values": list(scalar_values)[: COUPLING_PARTICLE_SCALAR_COUNT * actual],
"vector_values": list(vector_values)[: 3 * COUPLING_PARTICLE_VECTOR_COUNT * actual],
}
def coupling_apply_particle_forces(
self,
region_index: int,
export_indices: list[int],
forces: list[float],
) -> None:
count = len(export_indices)
if len(forces) != 3 * count:
raise ValueError("coupling force vector size mismatch")
index_array = (ctypes.c_int * max(1, count))(*export_indices)
force_array = (ctypes.c_double * max(1, 3 * count))(*forces)
self._check(self.lib.sphlite_coupling_apply_particle_forces(
int(region_index),
count,
index_array,
force_array,
))
2026-07-04 11:43:00 +08:00
def coupling_apply_facet_pressures(
self,
region_index: int,
facet_ids: list[int],
pressures: list[float],
distribution_mode: int,
coupling_step: int,
) -> None:
count = len(facet_ids)
if len(pressures) != count:
raise ValueError("coupling pressure vector size mismatch")
id_array = (ctypes.c_int * max(1, count))(*facet_ids)
pressure_array = (ctypes.c_double * max(1, count))(*pressures)
self._check(self.lib.sphlite_coupling_apply_facet_pressures(
int(region_index),
count,
id_array,
pressure_array,
int(distribution_mode),
int(coupling_step),
))
def coupling_apply_facet_tractions(
self,
region_index: int,
facet_ids: list[int],
tractions: list[float],
distribution_mode: int,
coupling_step: int,
) -> None:
count = len(facet_ids)
if len(tractions) != 3 * count:
raise ValueError("coupling traction vector size mismatch")
id_array = (ctypes.c_int * max(1, count))(*facet_ids)
traction_array = (ctypes.c_double * max(1, 3 * count))(*tractions)
self._check(self.lib.sphlite_coupling_apply_facet_tractions(
int(region_index),
count,
id_array,
traction_array,
int(distribution_mode),
int(coupling_step),
))
def coupling_apply_facet_resultants(
self,
region_index: int,
facet_ids: list[int],
resultants: list[float],
distribution_mode: int,
coupling_step: int,
) -> None:
count = len(facet_ids)
if len(resultants) != 3 * count:
raise ValueError("coupling resultant vector size mismatch")
id_array = (ctypes.c_int * max(1, count))(*facet_ids)
resultant_array = (ctypes.c_double * max(1, 3 * count))(*resultants)
self._check(self.lib.sphlite_coupling_apply_facet_resultants(
int(region_index),
count,
id_array,
resultant_array,
int(distribution_mode),
int(coupling_step),
))
def coupling_apply_contact_state(
self,
region_index: int,
facet_ids: list[int],
normals: list[float],
gaps: list[float],
penetrations: list[float],
contact_pressures: list[float],
coupling_step: int,
) -> None:
count = len(facet_ids)
if (
len(normals) != 3 * count
or len(gaps) != count
or len(penetrations) != count
or len(contact_pressures) != count
):
raise ValueError("coupling contact state vector size mismatch")
id_array = (ctypes.c_int * max(1, count))(*facet_ids)
normal_array = (ctypes.c_double * max(1, 3 * count))(*normals)
gap_array = (ctypes.c_double * max(1, count))(*gaps)
penetration_array = (ctypes.c_double * max(1, count))(*penetrations)
pressure_array = (ctypes.c_double * max(1, count))(*contact_pressures)
self._check(self.lib.sphlite_coupling_apply_contact_state(
int(region_index),
count,
id_array,
normal_array,
gap_array,
penetration_array,
pressure_array,
int(coupling_step),
))
def coupling_load_balance(self) -> dict[str, float | list[float]]:
vector_type = ctypes.c_double * 3
input_force = vector_type()
distributed_force = vector_type()
input_moment = vector_type()
distributed_moment = vector_type()
force_error = ctypes.c_double(0.0)
moment_error = ctypes.c_double(0.0)
self._check(self.lib.sphlite_coupling_get_load_balance(
input_force,
distributed_force,
ctypes.byref(force_error),
input_moment,
distributed_moment,
ctypes.byref(moment_error),
))
return {
"coupling_input_force": list(input_force),
"coupling_distributed_force": list(distributed_force),
"coupling_force_balance_error": float(force_error.value),
"coupling_input_moment": list(input_moment),
"coupling_distributed_moment": list(distributed_moment),
"coupling_moment_balance_error": float(moment_error.value),
}
def coupling_contact_diagnostics(self) -> dict[str, float | int]:
count_array = ctypes.c_int * COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT
value_array = ctypes.c_double * COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT
counts = count_array()
values = value_array()
self._check(self.lib.sphlite_coupling_get_contact_diagnostics(
counts,
COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT,
values,
COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT,
))
return {
"coupling_active_contacts": int(counts[0]),
"coupling_skipped_contacts": int(counts[1]),
"coupling_stale_loads": int(counts[2]),
"coupling_repeated_imports": int(counts[3]),
"coupling_nonfinite_contact_imports": int(counts[4]),
"coupling_bad_facet_areas": int(counts[5]),
"coupling_invalid_weights": int(counts[6]),
"coupling_max_penetration": float(values[0]),
}
2026-07-04 09:59:52 +08:00
def coupling_diagnostics(self) -> dict[str, float | int | list[float]]:
value_array = ctypes.c_double * COUPLING_DIAGNOSTIC_VALUE_COUNT
count_array = ctypes.c_int * COUPLING_DIAGNOSTIC_COUNT_COUNT
values = value_array()
counts = count_array()
self._check(self.lib.sphlite_coupling_get_diagnostics(
values,
COUPLING_DIAGNOSTIC_VALUE_COUNT,
counts,
COUPLING_DIAGNOSTIC_COUNT_COUNT,
))
values_list = list(values)
return {
"coupling_force": values_list[0:3],
"coupling_moment": values_list[3:6],
"coupling_current_impulse": values_list[6:9],
"coupling_cumulative_impulse": values_list[9:12],
"coupling_momentum_exchange": values_list[12:15],
"coupling_work": values_list[15],
"coupling_cumulative_work": values_list[16],
"coupling_energy_exchange": values_list[17],
"coupling_max_force": values_list[18],
"coupling_max_acceleration": values_list[19],
"coupling_max_velocity_error": values_list[20],
"coupling_max_position_error": values_list[21],
2026-07-04 11:43:00 +08:00
"coupling_force_balance_error": values_list[22],
"coupling_moment_balance_error": values_list[23],
"coupling_max_pressure": values_list[24],
"coupling_max_traction": values_list[25],
"coupling_max_penetration": values_list[26],
2026-07-04 09:59:52 +08:00
"coupling_stale_loads": int(counts[0]),
"coupling_skipped_particles": int(counts[1]),
"coupling_nonfinite_imports": int(counts[2]),
"coupling_imported_forces": int(counts[3]),
"coupling_step_load_consumed": int(counts[4]),
2026-07-04 11:43:00 +08:00
"coupling_repeated_imports": int(counts[5]),
"coupling_duplicate_facets": int(counts[6]),
"coupling_invalid_weights": int(counts[7]),
"coupling_active_contacts": int(counts[8]),
"coupling_skipped_contacts": int(counts[9]),
"coupling_nonfinite_contact_imports": int(counts[10]),
"coupling_bad_facet_areas": int(counts[11]),
2026-07-04 09:59:52 +08:00
}
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())
2026-07-03 09:55:48 +08:00
summary.update(self.gas_summary())
summary.update(self.gas_bubble_metrics())
summary.update(self.gas_dt_limits())
2026-07-03 11:04:59 +08:00
summary.update(self.gas_extended_summary())
summary.update(self.gas_source_summary())
2026-07-03 12:32:14 +08:00
summary.update(self.explosive_summary())
2026-07-03 13:52:49 +08:00
summary.update(self.explosive_release_balance())
2026-07-03 16:21:51 +08:00
summary.update(self.solid_summary())
2026-07-04 09:59:52 +08:00
coupling_regions = self.coupling_region_count()
summary["coupling_regions"] = coupling_regions
summary["coupling_region_info"] = [
self.coupling_region_info(index) for index in range(1, coupling_regions + 1)
]
summary.update(self.coupling_diagnostics())
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-03 09:55:48 +08:00
summary.update(self.gas_summary())
summary.update(self.gas_bubble_metrics())
summary.update(self.gas_dt_limits())
2026-07-03 11:04:59 +08:00
summary.update(self.gas_extended_summary())
summary.update(self.gas_source_summary())
2026-07-03 12:32:14 +08:00
summary.update(self.explosive_summary())
2026-07-03 13:52:49 +08:00
summary.update(self.explosive_release_balance())
2026-07-03 16:21:51 +08:00
summary.update(self.solid_summary())
2026-07-04 09:59:52 +08:00
summary.update(self.coupling_diagnostics())
2026-07-02 19:50:44 +08:00
return summary