Add coupling output restart and APIs

This commit is contained in:
Codex
2026-07-04 09:59:52 +08:00
parent 5be6b178bc
commit 5d9933d014
22 changed files with 2636 additions and 89 deletions
+192
View File
@@ -24,6 +24,22 @@ STATUS_INACTIVE = 1
STATUS_FAILED = 2
STATUS_RESERVED = 3
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
COUPLING_PARTICLE_SCALAR_COUNT = 7
COUPLING_PARTICLE_VECTOR_COUNT = 3
COUPLING_DIAGNOSTIC_VALUE_COUNT = 22
COUPLING_DIAGNOSTIC_COUNT_COUNT = 5
class SPHLiteError(RuntimeError):
pass
@@ -133,6 +149,51 @@ class SPHLite:
self.lib.sphlite_get_explosive_release_balance.restype = ctypes.c_int
configure_solid_api(self.lib)
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
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
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
self.lib.sphlite_get_particle_state.argtypes = [
ctypes.c_int,
ctypes.POINTER(ctypes.c_int),
@@ -456,6 +517,130 @@ class SPHLite:
def solid_reference_quality(self) -> dict[str, int | float]:
return _solid_reference_quality(self)
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)
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),
))
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),
"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,
))
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],
"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]),
}
def inspect(self, filename: str | Path) -> dict[str, int | float | list[float]]:
self.read_inp(filename)
self.build_model()
@@ -473,6 +658,12 @@ class SPHLite:
summary.update(self.explosive_summary())
summary.update(self.explosive_release_balance())
summary.update(self.solid_summary())
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
def run(self, filename: str | Path) -> dict[str, int | float | list[float]]:
@@ -489,4 +680,5 @@ class SPHLite:
summary.update(self.explosive_summary())
summary.update(self.explosive_release_balance())
summary.update(self.solid_summary())
summary.update(self.coupling_diagnostics())
return summary