From 5d9933d014319c16fdad95ffc2ea75b0e7b6830e Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 4 Jul 2026 09:59:52 +0800 Subject: [PATCH] Add coupling output restart and APIs --- CMakeLists.txt | 69 +++++ Python/sphlite_debug.py | 192 +++++++++++++ include/sphlite.hpp | 158 +++++++++++ include/sphlite_c_api.h | 71 +++++ src/app/sph_main.c | 52 ++++ .../sph_coupling_interface.f90 | 254 ++++++++++++++++- src/interface/sph_api.f90 | 206 ++++++++++++++ src/interface/sph_c_api.c | 262 ++++++++++++++++++ src/output/sph_output_csv.f90 | 107 ++++++- src/output/sph_restart.f90 | 202 +++++++++++++- src/solid/sph_solid_restart.f90 | 3 +- tests/interface/test_coupling_c_api_080.c | 133 +++++++++ tests/interface/test_coupling_cpp_api_080.cpp | 82 ++++++ .../module/test_coupling_fortran_api_080.f90 | 130 +++++++++ tests/module/test_coupling_output_080.f90 | 199 +++++++++++++ .../test_coupling_restart_071_compat.f90 | 88 ++++++ tests/module/test_coupling_restart_080.f90 | 177 ++++++++++++ tests/module/test_output.f90 | 12 +- tests/module/test_solid_output.f90 | 17 +- todo.md | 138 ++++----- tools/testing/test_coupling_cli_080.py | 97 +++++++ tools/testing/test_coupling_python_080.py | 76 +++++ 22 files changed, 2636 insertions(+), 89 deletions(-) create mode 100644 tests/interface/test_coupling_c_api_080.c create mode 100644 tests/interface/test_coupling_cpp_api_080.cpp create mode 100644 tests/module/test_coupling_fortran_api_080.f90 create mode 100644 tests/module/test_coupling_output_080.f90 create mode 100644 tests/module/test_coupling_restart_071_compat.f90 create mode 100644 tests/module/test_coupling_restart_080.f90 create mode 100644 tools/testing/test_coupling_cli_080.py create mode 100644 tools/testing/test_coupling_python_080.py diff --git a/CMakeLists.txt b/CMakeLists.txt index b4102a5..808693a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,15 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") #define SPHLITE_FC_sphft_get_solid_state sphft_get_solid_state_ #define SPHLITE_FC_sphft_get_solid_diagnostics sphft_get_solid_diagnostics_ #define SPHLITE_FC_sphft_get_solid_reference_quality sphft_get_solid_reference_quality_ +#define SPHLITE_FC_sphft_coupling_get_region_count sphft_coupling_get_region_count_ +#define SPHLITE_FC_sphft_coupling_get_region_info sphft_coupling_get_region_info_ +#define SPHLITE_FC_sphft_coupling_export_particles sphft_coupling_export_particles_ +#define SPHLITE_FC_sphft_coupling_export_facets sphft_coupling_export_facets_ +#define SPHLITE_FC_sphft_coupling_apply_particle_forces sphft_coupling_apply_particle_forces_ +#define SPHLITE_FC_sphft_coupling_apply_facet_loads sphft_coupling_apply_facet_loads_ +#define SPHLITE_FC_sphft_coupling_apply_motion_boundary sphft_coupling_apply_motion_boundary_ +#define SPHLITE_FC_sphft_coupling_clear_imports sphft_coupling_clear_imports_ +#define SPHLITE_FC_sphft_coupling_get_diagnostics sphft_coupling_get_diagnostics_ #endif ") endif() @@ -228,6 +237,30 @@ set_target_properties(test_coupling_noop_regression_080 PROPERTIES Fortran_MODUL add_test(NAME coupling_noop_regression_080 COMMAND test_coupling_noop_regression_080) set_tests_properties(coupling_noop_regression_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_coupling_output_080 tests/module/test_coupling_output_080.f90) +target_link_libraries(test_coupling_output_080 PRIVATE sphlite_core) +set_target_properties(test_coupling_output_080 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_output_080 COMMAND test_coupling_output_080) +set_tests_properties(coupling_output_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_restart_080 tests/module/test_coupling_restart_080.f90) +target_link_libraries(test_coupling_restart_080 PRIVATE sphlite_core) +set_target_properties(test_coupling_restart_080 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_restart_080 COMMAND test_coupling_restart_080) +set_tests_properties(coupling_restart_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_restart_071_compat tests/module/test_coupling_restart_071_compat.f90) +target_link_libraries(test_coupling_restart_071_compat PRIVATE sphlite_core) +set_target_properties(test_coupling_restart_071_compat PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_restart_071_compat COMMAND test_coupling_restart_071_compat) +set_tests_properties(coupling_restart_071_compat PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_fortran_api_080 tests/module/test_coupling_fortran_api_080.f90) +target_link_libraries(test_coupling_fortran_api_080 PRIVATE sphlite_core) +set_target_properties(test_coupling_fortran_api_080 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_fortran_api_080 COMMAND test_coupling_fortran_api_080) +set_tests_properties(coupling_fortran_api_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_input_gas tests/module/test_input_gas.f90) target_link_libraries(test_input_gas PRIVATE sphlite_core) set_target_properties(test_input_gas PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") @@ -666,6 +699,15 @@ set_tests_properties(explosive_release_c_api PROPERTIES WORKING_DIRECTORY "${CMA add_test(NAME solid_c_api COMMAND test_c_api) set_tests_properties(solid_c_api PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_coupling_c_api_080 tests/interface/test_coupling_c_api_080.c) +target_include_directories(test_coupling_c_api_080 PRIVATE include) +target_compile_definitions(test_coupling_c_api_080 PRIVATE + SPHLITE_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data" +) +target_link_libraries(test_coupling_c_api_080 PRIVATE sphlite_c) +add_test(NAME coupling_c_api_080 COMMAND test_coupling_c_api_080) +set_tests_properties(coupling_c_api_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_cpp_api tests/interface/test_cpp_api.cpp) target_include_directories(test_cpp_api PRIVATE include) target_compile_definitions(test_cpp_api PRIVATE @@ -677,6 +719,15 @@ set_tests_properties(cpp_api PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURC add_test(NAME explosive_release_cpp_api COMMAND test_cpp_api) set_tests_properties(explosive_release_cpp_api PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_coupling_cpp_api_080 tests/interface/test_coupling_cpp_api_080.cpp) +target_include_directories(test_coupling_cpp_api_080 PRIVATE include) +target_compile_definitions(test_coupling_cpp_api_080 PRIVATE + SPHLITE_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data" +) +target_link_libraries(test_coupling_cpp_api_080 PRIVATE sphlite_c) +add_test(NAME coupling_cpp_api_080 COMMAND test_coupling_cpp_api_080) +set_tests_properties(coupling_cpp_api_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_restart tests/module/test_restart.f90) target_link_libraries(test_restart PRIVATE sphlite_core) set_target_properties(test_restart PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") @@ -973,6 +1024,15 @@ add_test( ) set_tests_properties(solid_python_051 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_test( + NAME coupling_python_080 + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/test_coupling_python_080.py" + --library "$" + --input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/coupling_shell_region_080.inp" +) +set_tests_properties(coupling_python_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_test( NAME cli COMMAND "${Python3_EXECUTABLE}" @@ -1038,6 +1098,15 @@ add_test( ) set_tests_properties(solid_cli_051 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_test( + NAME coupling_cli_080 + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/test_coupling_cli_080.py" + --exe "$" + --input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/coupling_shell_region_080.inp" +) +set_tests_properties(coupling_cli_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_test( NAME source_size COMMAND "${Python3_EXECUTABLE}" diff --git a/Python/sphlite_debug.py b/Python/sphlite_debug.py index f73b680..c44ac5c 100644 --- a/Python/sphlite_debug.py +++ b/Python/sphlite_debug.py @@ -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 diff --git a/include/sphlite.hpp b/include/sphlite.hpp index c36f3ad..faabe2d 100644 --- a/include/sphlite.hpp +++ b/include/sphlite.hpp @@ -144,6 +144,41 @@ struct SolidReferenceQuality { int correction_status; }; +struct CouplingRegionInfo { + std::string name; + int region_type; + int export_mode; + int import_mode; + int enabled; + int particle_count; + int facet_count; +}; + +struct CouplingParticleExport { + std::vector ext_ids; + std::vector internal_indices; + std::vector phases; + std::vector statuses; + std::vector materials; + std::vector sections; + std::vector scalar_values; + std::vector vector_values; +}; + +struct CouplingFacetExport { + std::vector facet_ids; + std::vector particle_ids; + std::vector materials; + std::vector sections; + std::vector vector_values; + std::vector area_values; +}; + +struct CouplingDiagnostics { + std::vector values; + std::vector counts; +}; + class Error : public std::runtime_error { public: explicit Error(const std::string &message) : std::runtime_error(message) {} @@ -397,6 +432,129 @@ inline void read_restart(const std::string &filename) check(sphlite_read_restart(filename.c_str())); } +inline int coupling_region_count() +{ + int count = 0; + check(sphlite_coupling_get_region_count(&count)); + return count; +} + +inline CouplingRegionInfo coupling_region_info(int region_index) +{ + char name[128]; + CouplingRegionInfo info = {"", 0, 0, 0, 0, 0, 0}; + check(sphlite_coupling_get_region_info(region_index, name, + static_cast(sizeof(name)), &info.region_type, &info.export_mode, + &info.import_mode, &info.enabled, &info.particle_count, + &info.facet_count)); + info.name = std::string(name); + return info; +} + +inline CouplingParticleExport coupling_export_particles(int region_index) +{ + CouplingRegionInfo info = coupling_region_info(region_index); + int capacity = info.particle_count > 0 ? info.particle_count : 1; + CouplingParticleExport result; + result.ext_ids.resize(capacity); + result.internal_indices.resize(capacity); + result.phases.resize(capacity); + result.statuses.resize(capacity); + result.materials.resize(capacity); + result.sections.resize(capacity); + result.scalar_values.resize(SPHLITE_COUPLING_PARTICLE_SCALAR_COUNT * capacity); + result.vector_values.resize(3 * SPHLITE_COUPLING_PARTICLE_VECTOR_COUNT * capacity); + int count = 0; + check(sphlite_coupling_export_particles(region_index, capacity, &count, + result.ext_ids.data(), result.internal_indices.data(), + result.phases.data(), result.statuses.data(), result.materials.data(), + result.sections.data(), result.scalar_values.data(), + result.vector_values.data())); + result.ext_ids.resize(count); + result.internal_indices.resize(count); + result.phases.resize(count); + result.statuses.resize(count); + result.materials.resize(count); + result.sections.resize(count); + result.scalar_values.resize(SPHLITE_COUPLING_PARTICLE_SCALAR_COUNT * count); + result.vector_values.resize(3 * SPHLITE_COUPLING_PARTICLE_VECTOR_COUNT * count); + return result; +} + +inline CouplingFacetExport coupling_export_facets(int region_index) +{ + CouplingRegionInfo info = coupling_region_info(region_index); + int capacity = info.facet_count > 0 ? info.facet_count : 1; + CouplingFacetExport result; + result.facet_ids.resize(capacity); + result.particle_ids.resize(capacity); + result.materials.resize(capacity); + result.sections.resize(capacity); + result.vector_values.resize(3 * SPHLITE_COUPLING_FACET_VECTOR_COUNT * capacity); + result.area_values.resize(capacity); + int count = 0; + check(sphlite_coupling_export_facets(region_index, capacity, &count, + result.facet_ids.data(), result.particle_ids.data(), + result.materials.data(), result.sections.data(), + result.vector_values.data(), result.area_values.data())); + result.facet_ids.resize(count); + result.particle_ids.resize(count); + result.materials.resize(count); + result.sections.resize(count); + result.vector_values.resize(3 * SPHLITE_COUPLING_FACET_VECTOR_COUNT * count); + result.area_values.resize(count); + return result; +} + +inline void coupling_apply_particle_forces(int region_index, + const std::vector &export_indices, + const std::vector &forces) +{ + if (forces.size() != 3 * export_indices.size()) { + throw Error("coupling force vector size mismatch"); + } + check(sphlite_coupling_apply_particle_forces(region_index, + static_cast(export_indices.size()), export_indices.data(), + forces.data())); +} + +inline void coupling_apply_facet_loads(int region_index, + const std::vector &facet_ids, + const std::vector &loads) +{ + if (loads.size() != 3 * facet_ids.size()) { + throw Error("coupling facet load vector size mismatch"); + } + check(sphlite_coupling_apply_facet_loads(region_index, + static_cast(facet_ids.size()), facet_ids.data(), loads.data())); +} + +inline void coupling_apply_motion_boundary(int region_index, + int export_index, + int target_kind, + int mode, + const double target[3]) +{ + check(sphlite_coupling_apply_motion_boundary(region_index, export_index, + target_kind, mode, target)); +} + +inline void coupling_clear_imports() +{ + check(sphlite_coupling_clear_imports()); +} + +inline CouplingDiagnostics coupling_diagnostics() +{ + CouplingDiagnostics diagnostics; + diagnostics.values.resize(SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT); + diagnostics.counts.resize(SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT); + check(sphlite_coupling_get_diagnostics(diagnostics.values.data(), + static_cast(diagnostics.values.size()), diagnostics.counts.data(), + static_cast(diagnostics.counts.size()))); + return diagnostics; +} + class Session { public: Session() : active_(true) { initialize(); } diff --git a/include/sphlite_c_api.h b/include/sphlite_c_api.h index d1e4d33..a88bf43 100644 --- a/include/sphlite_c_api.h +++ b/include/sphlite_c_api.h @@ -35,6 +35,29 @@ extern "C" { #define SPHLITE_SOLID_DIAGNOSTIC_VALUE_COUNT 3 #define SPHLITE_SOLID_REFERENCE_VALUE_COUNT 1 +#define SPHLITE_COUPLING_REGION_CONTACT 1 +#define SPHLITE_COUPLING_REGION_CONTINUUM 2 + +#define SPHLITE_COUPLING_EXPORT_PARTICLE 1 +#define SPHLITE_COUPLING_EXPORT_FACET 2 +#define SPHLITE_COUPLING_EXPORT_BOTH 3 + +#define SPHLITE_COUPLING_IMPORT_FORCE 1 +#define SPHLITE_COUPLING_IMPORT_FACET_LOAD 2 +#define SPHLITE_COUPLING_IMPORT_MOTION 3 + +#define SPHLITE_COUPLING_MOTION_NONE 0 +#define SPHLITE_COUPLING_MOTION_VELOCITY 1 +#define SPHLITE_COUPLING_MOTION_POSITION 2 +#define SPHLITE_COUPLING_MOTION_DIRECT 1 +#define SPHLITE_COUPLING_MOTION_PENALTY 2 + +#define SPHLITE_COUPLING_PARTICLE_SCALAR_COUNT 7 +#define SPHLITE_COUPLING_PARTICLE_VECTOR_COUNT 3 +#define SPHLITE_COUPLING_FACET_VECTOR_COUNT 3 +#define SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT 22 +#define SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT 5 + SPHLITE_API int sphlite_initialize(void); SPHLITE_API int sphlite_finalize(void); SPHLITE_API int sphlite_get_version(int *major, int *minor, int *patch); @@ -120,6 +143,54 @@ SPHLITE_API int sphlite_get_solid_reference_quality(int *reference_neighbor_min, int *correction_status, double *values, int value_capacity); +SPHLITE_API int sphlite_coupling_get_region_count(int *count); +SPHLITE_API int sphlite_coupling_get_region_info(int region_index, + char *name, + int name_capacity, + int *region_type, + int *export_mode, + int *import_mode, + int *enabled, + int *particle_count, + int *facet_count); +SPHLITE_API int sphlite_coupling_export_particles(int region_index, + int capacity, + int *count, + int *ext_ids, + int *internal_indices, + int *phases, + int *statuses, + int *materials, + int *sections, + double *scalar_values, + double *vector_values); +SPHLITE_API int sphlite_coupling_export_facets(int region_index, + int capacity, + int *count, + int *facet_ids, + int *particle_ids, + int *materials, + int *sections, + double *vector_values, + double *area_values); +SPHLITE_API int sphlite_coupling_apply_particle_forces(int region_index, + int force_count, + const int *export_indices, + const double *forces); +SPHLITE_API int sphlite_coupling_apply_facet_loads(int region_index, + int load_count, + const int *facet_ids, + const double *loads); +SPHLITE_API int sphlite_coupling_apply_motion_boundary(int region_index, + int export_index, + int target_kind, + int mode, + const double *target); +SPHLITE_API int sphlite_coupling_clear_imports(void); +SPHLITE_API int sphlite_coupling_get_diagnostics(double *values, + int value_capacity, + int *counts, + int count_capacity); #ifdef __cplusplus } diff --git a/src/app/sph_main.c b/src/app/sph_main.c index 0eaaeb0..5f48ca4 100644 --- a/src/app/sph_main.c +++ b/src/app/sph_main.c @@ -145,6 +145,9 @@ int main(int argc, char **argv) double solid_reference_values[SPHLITE_SOLID_REFERENCE_VALUE_COUNT] = {0.0}; int solid_reference_neighbor_min = 0; int solid_correction_status = 0; + int coupling_regions = 0; + double coupling_values[SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT] = {0.0}; + int coupling_counts[SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT] = {0}; int run_mode = 0; int check_mode = 0; const char *input = NULL; @@ -365,6 +368,20 @@ int main(int argc, char **argv) (void)sphlite_finalize(); return 1; } + ierr = sphlite_coupling_get_region_count(&coupling_regions); + if (ierr != 0) { + sphlite_print_error(); + (void)sphlite_finalize(); + return 1; + } + ierr = sphlite_coupling_get_diagnostics(coupling_values, + SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT, coupling_counts, + SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT); + if (ierr != 0) { + sphlite_print_error(); + (void)sphlite_finalize(); + return 1; + } printf("version: %d.%d.%d\n", major, minor, patch); printf("input: %s\n", input); @@ -444,6 +461,41 @@ int main(int argc, char **argv) printf("solid_dt_cfl: %.16e\n", solid_values[13]); printf("solid_dt_accel: %.16e\n", solid_values[14]); printf("solid_dtcrit: %.16e\n", solid_values[15]); + printf("coupling_regions: %d\n", coupling_regions); + for (i = 1; i <= coupling_regions; ++i) { + char coupling_name[128]; + int region_type = 0; + int export_mode = 0; + int import_mode = 0; + int enabled = 0; + int region_particles = 0; + int region_facets = 0; + ierr = sphlite_coupling_get_region_info(i, coupling_name, + (int)sizeof(coupling_name), ®ion_type, &export_mode, + &import_mode, &enabled, ®ion_particles, ®ion_facets); + if (ierr != 0) { + sphlite_print_error(); + (void)sphlite_finalize(); + return 1; + } + printf("coupling_region_%d_name: %s\n", i, coupling_name); + printf("coupling_region_%d_type: %d\n", i, region_type); + printf("coupling_region_%d_export_mode: %d\n", i, export_mode); + printf("coupling_region_%d_import_mode: %d\n", i, import_mode); + printf("coupling_region_%d_enabled: %d\n", i, enabled); + printf("coupling_region_%d_particles: %d\n", i, region_particles); + printf("coupling_region_%d_facets: %d\n", i, region_facets); + } + printf("coupling_force_x: %.16e\n", coupling_values[0]); + printf("coupling_force_y: %.16e\n", coupling_values[1]); + printf("coupling_force_z: %.16e\n", coupling_values[2]); + printf("coupling_impulse_x: %.16e\n", coupling_values[6]); + printf("coupling_impulse_y: %.16e\n", coupling_values[7]); + printf("coupling_impulse_z: %.16e\n", coupling_values[8]); + printf("coupling_work: %.16e\n", coupling_values[15]); + printf("coupling_energy_exchange: %.16e\n", coupling_values[17]); + printf("coupling_nonfinite_imports: %d\n", coupling_counts[2]); + printf("coupling_stale_loads: %d\n", coupling_counts[0]); printf("status: ok\n"); ierr = sphlite_finalize(); diff --git a/src/coupling_interface/sph_coupling_interface.f90 b/src/coupling_interface/sph_coupling_interface.f90 index 6b3d21d..00bbb4e 100644 --- a/src/coupling_interface/sph_coupling_interface.f90 +++ b/src/coupling_interface/sph_coupling_interface.f90 @@ -10,7 +10,9 @@ module sph_coupling_interface DB_EXPORT_BOTH => SPH_COUPLING_EXPORT_BOTH, & db_coupling_region_count => coupling_region_count, & db_coupling_region_name => coupling_region_name, & + db_coupling_region_type => coupling_region_type, & db_coupling_region_export_mode => coupling_region_export_mode, & + db_coupling_region_import_mode => coupling_region_import_mode, & db_coupling_region_enabled => coupling_region_enabled, & db_coupling_region_particle_count => coupling_region_particle_count, & db_coupling_region_particles => coupling_region_particles, & @@ -73,6 +75,11 @@ module sph_coupling_interface real(rk) :: coupling_max_position_error = 0.0_rk integer :: coupling_step_load_consumed = 0 + interface sph_coupling_get_diagnostics + module procedure sph_coupling_get_basic_diagnostics + module procedure sph_coupling_get_diagnostic_values + end interface + contains subroutine sph_coupling_reset() @@ -183,6 +190,57 @@ contains ierr = SPH_SUCCESS end subroutine sph_coupling_get_particle_count + subroutine sph_coupling_get_region_count(count, ierr) + integer, intent(out) :: count + integer, intent(out) :: ierr + + count = db_coupling_region_count + ierr = SPH_SUCCESS + end subroutine sph_coupling_get_region_count + + subroutine sph_coupling_get_region_info(region_index, name, region_type, & + export_mode, import_mode, enabled, particle_count, facet_count, ierr) + integer, intent(in) :: region_index + character(len=*), intent(out) :: name + integer, intent(out) :: region_type + integer, intent(out) :: export_mode + integer, intent(out) :: import_mode + integer, intent(out) :: enabled + integer, intent(out) :: particle_count + integer, intent(out) :: facet_count + integer, intent(out) :: ierr + + name = ' ' + region_type = 0 + export_mode = 0 + import_mode = 0 + enabled = 0 + particle_count = 0 + facet_count = 0 + ierr = SPH_SUCCESS + if (region_index < 1 .or. region_index > db_coupling_region_count) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + name = db_coupling_region_name(region_index) + region_type = db_coupling_region_type(region_index) + export_mode = db_coupling_region_export_mode(region_index) + import_mode = db_coupling_region_import_mode(region_index) + enabled = db_coupling_region_enabled(region_index) + if (enabled == 0) return + if (export_mode /= DB_EXPORT_FACET) then + particle_count = db_coupling_region_particle_count(region_index) + end if + if (export_mode /= DB_EXPORT_PARTICLE) then + call sph_coupling_count_shell_facets(region_index, facet_count, ierr) + if (ierr /= SPH_SUCCESS) then + facet_count = 0 + ierr = SPH_SUCCESS + end if + end if + end subroutine sph_coupling_get_region_info + subroutine sph_coupling_get_region_particle_count(region_name, count, ierr) character(len=*), intent(in) :: region_name integer, intent(out) :: count @@ -277,6 +335,46 @@ contains end do end subroutine sph_coupling_export_region_particles + subroutine sph_coupling_export_particles(region_index, capacity, count, & + ext_ids, internal_indices, phases, statuses, materials, sections, & + scalar_values, vector_values, ierr) + integer, intent(in) :: region_index + integer, intent(in) :: capacity + integer, intent(out) :: count + integer, intent(out) :: ext_ids(capacity) + integer, intent(out) :: internal_indices(capacity) + integer, intent(out) :: phases(capacity) + integer, intent(out) :: statuses(capacity) + integer, intent(out) :: materials(capacity) + integer, intent(out) :: sections(capacity) + real(rk), intent(out) :: scalar_values(SPH_COUPLING_PARTICLE_SCALAR_COUNT, & + capacity) + real(rk), intent(out) :: vector_values(SPH_NDIM, & + SPH_COUPLING_PARTICLE_VECTOR_COUNT, capacity) + integer, intent(out) :: ierr + + count = 0 + if (region_index < 1 .or. region_index > db_coupling_region_count) then + if (capacity > 0) then + ext_ids = 0 + internal_indices = 0 + phases = 0 + statuses = 0 + materials = 0 + sections = 0 + scalar_values = 0.0_rk + vector_values = 0.0_rk + end if + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_coupling_export_region_particles( & + db_coupling_region_name(region_index), capacity, count, ext_ids, & + internal_indices, phases, statuses, materials, sections, & + scalar_values, vector_values, ierr) + end subroutine sph_coupling_export_particles + subroutine sph_coupling_export_particle(index, ext_id, position, velocity, ierr) integer, intent(in) :: index integer, intent(out) :: ext_id @@ -417,6 +515,40 @@ contains end do end subroutine sph_coupling_export_region_facets + subroutine sph_coupling_export_facets(region_index, capacity, count, & + facet_ids, particle_ids, materials, sections, vector_values, & + area_values, ierr) + integer, intent(in) :: region_index + integer, intent(in) :: capacity + integer, intent(out) :: count + integer, intent(out) :: facet_ids(capacity) + integer, intent(out) :: particle_ids(capacity) + integer, intent(out) :: materials(capacity) + integer, intent(out) :: sections(capacity) + real(rk), intent(out) :: vector_values(SPH_NDIM, & + SPH_COUPLING_FACET_VECTOR_COUNT, capacity) + real(rk), intent(out) :: area_values(capacity) + integer, intent(out) :: ierr + + count = 0 + if (region_index < 1 .or. region_index > db_coupling_region_count) then + if (capacity > 0) then + facet_ids = 0 + particle_ids = 0 + materials = 0 + sections = 0 + vector_values = 0.0_rk + area_values = 0.0_rk + end if + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_coupling_export_region_facets( & + db_coupling_region_name(region_index), capacity, count, facet_ids, & + particle_ids, materials, sections, vector_values, area_values, ierr) + end subroutine sph_coupling_export_facets + subroutine sph_coupling_export_facet(index, node_ids, ierr) integer, intent(in) :: index integer, intent(out) :: node_ids(3) @@ -559,6 +691,89 @@ contains SPH_COUPLING_MOTION_POSITION, target_position, ierr) end subroutine sph_coupling_import_motion_position + subroutine sph_coupling_apply_particle_forces(region_index, force_count, & + export_indices, forces, ierr) + integer, intent(in) :: region_index + integer, intent(in) :: force_count + integer, intent(in) :: export_indices(force_count) + real(rk), intent(in) :: forces(SPH_NDIM,force_count) + integer, intent(out) :: ierr + integer :: i + + ierr = SPH_SUCCESS + if (region_index < 1 .or. region_index > db_coupling_region_count) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (force_count < 0) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling force count is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + do i = 1, force_count + call sph_coupling_import_force_by_region_index( & + db_coupling_region_name(region_index), export_indices(i), & + forces(:,i), ierr) + if (ierr /= SPH_SUCCESS) return + end do + end subroutine sph_coupling_apply_particle_forces + + subroutine sph_coupling_apply_facet_loads(region_index, load_count, & + facet_ids, loads, ierr) + integer, intent(in) :: region_index + integer, intent(in) :: load_count + integer, intent(in) :: facet_ids(load_count) + real(rk), intent(in) :: loads(SPH_NDIM,load_count) + integer, intent(out) :: ierr + integer :: i + + ierr = SPH_SUCCESS + if (region_index < 1 .or. region_index > db_coupling_region_count) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (load_count < 0) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling facet load count is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + do i = 1, load_count + call sph_coupling_import_region_facet_load( & + db_coupling_region_name(region_index), facet_ids(i), loads(:,i), ierr) + if (ierr /= SPH_SUCCESS) return + end do + end subroutine sph_coupling_apply_facet_loads + + subroutine sph_coupling_apply_motion_boundary(region_index, export_index, & + target_kind, mode, target, ierr) + integer, intent(in) :: region_index + integer, intent(in) :: export_index + integer, intent(in) :: target_kind + integer, intent(in) :: mode + real(rk), intent(in) :: target(SPH_NDIM) + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (region_index < 1 .or. region_index > db_coupling_region_count) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (target_kind == SPH_COUPLING_MOTION_VELOCITY) then + call sph_coupling_import_motion_velocity( & + db_coupling_region_name(region_index), export_index, mode, target, ierr) + else if (target_kind == SPH_COUPLING_MOTION_POSITION) then + call sph_coupling_import_motion_position( & + db_coupling_region_name(region_index), export_index, mode, target, ierr) + else + call sph_error_set(SPH_ERROR_ARGUMENT, & + 'coupling motion target kind is invalid') + ierr = SPH_ERROR_ARGUMENT + end if + end subroutine sph_coupling_apply_motion_boundary + subroutine sph_coupling_get_import_diagnostics(total_force, motion_count, & skipped_count, nonfinite_count, stale_count, epoch, ierr) real(rk), intent(out) :: total_force(SPH_NDIM) @@ -742,6 +957,41 @@ contains step_load_consumed = coupling_step_load_consumed end subroutine sph_coupling_get_extended_diagnostics + subroutine sph_coupling_get_diagnostic_values(value_capacity, values, & + count_capacity, counts, ierr) + integer, intent(in) :: value_capacity + real(rk), intent(out) :: values(value_capacity) + integer, intent(in) :: count_capacity + integer, intent(out) :: counts(count_capacity) + integer, intent(out) :: ierr + + if (value_capacity < 22 .or. count_capacity < 5) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling diagnostics buffer is too small') + ierr = SPH_ERROR_ARGUMENT + return + end if + values = 0.0_rk + counts = 0 + values(1:3) = coupling_current_force + values(4:6) = coupling_current_moment + values(7:9) = coupling_current_impulse + values(10:12) = coupling_cumulative_impulse + values(13:15) = coupling_momentum_exchange + values(16) = coupling_current_work + values(17) = coupling_cumulative_work + values(18) = coupling_energy_exchange + values(19) = coupling_max_external_force + values(20) = coupling_max_acceleration + values(21) = coupling_max_velocity_error + values(22) = coupling_max_position_error + counts(1) = coupling_stale_load_count + counts(2) = coupling_skipped_particle_count + counts(3) = coupling_nonfinite_import_count + counts(4) = coupling_imported_force_count + counts(5) = coupling_step_load_consumed + ierr = SPH_SUCCESS + end subroutine sph_coupling_get_diagnostic_values + subroutine sph_coupling_import_particle_force(index, force, ierr) integer, intent(in) :: index real(rk), intent(in) :: force(SPH_NDIM) @@ -778,7 +1028,7 @@ contains coupling_momentum(:) = coupling_momentum(:) + load(:) end subroutine sph_coupling_import_facet_load - subroutine sph_coupling_get_diagnostics(work, energy, momentum) + subroutine sph_coupling_get_basic_diagnostics(work, energy, momentum) real(rk), intent(out) :: work real(rk), intent(out) :: energy real(rk), intent(out) :: momentum(SPH_NDIM) @@ -786,7 +1036,7 @@ contains work = coupling_work energy = coupling_energy momentum(:) = coupling_momentum(:) - end subroutine sph_coupling_get_diagnostics + end subroutine sph_coupling_get_basic_diagnostics subroutine sph_coupling_find_region(region_name, region, ierr) character(len=*), intent(in) :: region_name diff --git a/src/interface/sph_api.f90 b/src/interface/sph_api.f90 index 718f190..ac1db17 100644 --- a/src/interface/sph_api.f90 +++ b/src/interface/sph_api.f90 @@ -1134,3 +1134,209 @@ subroutine sphft_read_restart_chars(chars, count, ierr) end do call sph_read_restart(filename(1:n), ierr) end subroutine sphft_read_restart_chars + +subroutine sphft_coupling_get_region_count(count, ierr) + use sph_coupling_interface, only: sph_coupling_get_region_count + implicit none + integer, intent(out) :: count + integer, intent(out) :: ierr + + call sph_coupling_get_region_count(count, ierr) +end subroutine sphft_coupling_get_region_count + +subroutine sphft_coupling_get_region_info(region_index, chars, capacity, & + actual, region_type, export_mode, import_mode, enabled, particle_count, & + facet_count, ierr) + use sph_coupling_interface, only: sph_coupling_get_region_info + implicit none + integer, intent(in) :: region_index + integer, intent(out) :: chars(*) + integer, intent(in) :: capacity + integer, intent(out) :: actual + integer, intent(out) :: region_type + integer, intent(out) :: export_mode + integer, intent(out) :: import_mode + integer, intent(out) :: enabled + integer, intent(out) :: particle_count + integer, intent(out) :: facet_count + integer, intent(out) :: ierr + character(len=64) :: name + integer :: i + integer :: n + + actual = 0 + if (capacity > 0) chars(1) = 0 + call sph_coupling_get_region_info(region_index, name, region_type, & + export_mode, import_mode, enabled, particle_count, facet_count, ierr) + if (ierr /= 0) return + n = len_trim(name) + actual = n + if (capacity <= 0) return + if (n > capacity - 1) n = capacity - 1 + do i = 1, n + chars(i) = ichar(name(i:i)) + end do + chars(n + 1) = 0 +end subroutine sphft_coupling_get_region_info + +subroutine sphft_coupling_export_particles(region_index, capacity, count, & + ext_ids, internal_indices, phases, statuses, materials, sections, & + scalar_values, vector_values, ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_export_particles, & + SPH_COUPLING_PARTICLE_SCALAR_COUNT, SPH_COUPLING_PARTICLE_VECTOR_COUNT + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: capacity + integer, intent(out) :: count + integer, intent(out) :: ext_ids(capacity) + integer, intent(out) :: internal_indices(capacity) + integer, intent(out) :: phases(capacity) + integer, intent(out) :: statuses(capacity) + integer, intent(out) :: materials(capacity) + integer, intent(out) :: sections(capacity) + real(rk), intent(out) :: scalar_values(SPH_COUPLING_PARTICLE_SCALAR_COUNT, capacity) + real(rk), intent(out) :: vector_values(SPH_NDIM, SPH_COUPLING_PARTICLE_VECTOR_COUNT, capacity) + integer, intent(out) :: ierr + + call sph_coupling_export_particles(region_index, capacity, count, ext_ids, & + internal_indices, phases, statuses, materials, sections, scalar_values, & + vector_values, ierr) +end subroutine sphft_coupling_export_particles + +subroutine sphft_coupling_export_facets(region_index, capacity, count, & + facet_ids, particle_ids, materials, sections, vector_values, area_values, & + ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_export_facets, & + SPH_COUPLING_FACET_VECTOR_COUNT + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: capacity + integer, intent(out) :: count + integer, intent(out) :: facet_ids(capacity) + integer, intent(out) :: particle_ids(capacity) + integer, intent(out) :: materials(capacity) + integer, intent(out) :: sections(capacity) + real(rk), intent(out) :: vector_values(SPH_NDIM, SPH_COUPLING_FACET_VECTOR_COUNT, capacity) + real(rk), intent(out) :: area_values(capacity) + integer, intent(out) :: ierr + + call sph_coupling_export_facets(region_index, capacity, count, facet_ids, & + particle_ids, materials, sections, vector_values, area_values, ierr) +end subroutine sphft_coupling_export_facets + +subroutine sphft_coupling_apply_particle_forces(region_index, force_count, & + export_indices, forces, ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_apply_particle_forces + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: force_count + integer, intent(in) :: export_indices(force_count) + real(rk), intent(in) :: forces(SPH_NDIM, force_count) + integer, intent(out) :: ierr + + call sph_coupling_apply_particle_forces(region_index, force_count, & + export_indices, forces, ierr) +end subroutine sphft_coupling_apply_particle_forces + +subroutine sphft_coupling_apply_facet_loads(region_index, load_count, & + facet_ids, loads, ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_apply_facet_loads + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: load_count + integer, intent(in) :: facet_ids(load_count) + real(rk), intent(in) :: loads(SPH_NDIM, load_count) + integer, intent(out) :: ierr + + call sph_coupling_apply_facet_loads(region_index, load_count, facet_ids, & + loads, ierr) +end subroutine sphft_coupling_apply_facet_loads + +subroutine sphft_coupling_apply_motion_boundary(region_index, export_index, & + target_kind, mode, target, ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_apply_motion_boundary + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: export_index + integer, intent(in) :: target_kind + integer, intent(in) :: mode + real(rk), intent(in) :: target(SPH_NDIM) + integer, intent(out) :: ierr + + call sph_coupling_apply_motion_boundary(region_index, export_index, & + target_kind, mode, target, ierr) +end subroutine sphft_coupling_apply_motion_boundary + +subroutine sphft_coupling_clear_imports(ierr) + use sph_coupling_interface, only: sph_coupling_clear_imports + implicit none + integer, intent(out) :: ierr + + call sph_coupling_clear_imports(ierr) +end subroutine sphft_coupling_clear_imports + +subroutine sphft_coupling_get_diagnostics(value_capacity, values, & + count_capacity, counts, ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_error, only: SPH_ERROR_ARGUMENT, sph_error_set + use sph_coupling_interface, only: sph_coupling_get_extended_diagnostics + implicit none + integer, intent(in) :: value_capacity + real(rk), intent(out) :: values(value_capacity) + integer, intent(in) :: count_capacity + integer, intent(out) :: counts(count_capacity) + integer, intent(out) :: ierr + real(rk) :: total_force(SPH_NDIM) + real(rk) :: total_moment(SPH_NDIM) + real(rk) :: current_impulse(SPH_NDIM) + real(rk) :: cumulative_impulse(SPH_NDIM) + real(rk) :: momentum_exchange(SPH_NDIM) + real(rk) :: current_work + real(rk) :: cumulative_work + real(rk) :: energy_exchange + real(rk) :: max_force + real(rk) :: max_acceleration + real(rk) :: max_velocity_error + real(rk) :: max_position_error + integer :: stale_count + integer :: skipped_count + integer :: nonfinite_count + integer :: imported_force_count + integer :: step_load_consumed + + if (value_capacity < 22 .or. count_capacity < 5) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling diagnostics buffer is too small') + ierr = SPH_ERROR_ARGUMENT + return + end if + values = 0.0_rk + counts = 0 + call sph_coupling_get_extended_diagnostics(total_force, total_moment, & + current_impulse, cumulative_impulse, current_work, cumulative_work, & + momentum_exchange, energy_exchange, max_force, max_acceleration, & + max_velocity_error, max_position_error, stale_count, skipped_count, & + nonfinite_count, imported_force_count, step_load_consumed, ierr) + if (ierr /= 0) return + values(1:3) = total_force + values(4:6) = total_moment + values(7:9) = current_impulse + values(10:12) = cumulative_impulse + values(13:15) = momentum_exchange + values(16) = current_work + values(17) = cumulative_work + values(18) = energy_exchange + values(19) = max_force + values(20) = max_acceleration + values(21) = max_velocity_error + values(22) = max_position_error + counts(1) = stale_count + counts(2) = skipped_count + counts(3) = nonfinite_count + counts(4) = imported_force_count + counts(5) = step_load_consumed +end subroutine sphft_coupling_get_diagnostics diff --git a/src/interface/sph_c_api.c b/src/interface/sph_c_api.c index 6c408ce..4496758 100644 --- a/src/interface/sph_c_api.c +++ b/src/interface/sph_c_api.c @@ -1,6 +1,7 @@ #include "sphlite_c_api.h" #include "sphlite_fortran_mangle.h" +#include #include #include #include @@ -101,6 +102,86 @@ extern void SPHLITE_FC_sphft_get_explosive_release_balance( double *energy_values, double *momentum_values, int *ierr); +extern void SPHLITE_FC_sphft_coupling_get_region_count( + int *count, int *ierr); +extern void SPHLITE_FC_sphft_coupling_get_region_info( + const int *region_index, + int *chars, + const int *capacity, + int *actual, + int *region_type, + int *export_mode, + int *import_mode, + int *enabled, + int *particle_count, + int *facet_count, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_export_particles( + const int *region_index, + const int *capacity, + int *count, + int *ext_ids, + int *internal_indices, + int *phases, + int *statuses, + int *materials, + int *sections, + double *scalar_values, + double *vector_values, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_export_facets( + const int *region_index, + const int *capacity, + int *count, + int *facet_ids, + int *particle_ids, + int *materials, + int *sections, + double *vector_values, + double *area_values, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_apply_particle_forces( + const int *region_index, + const int *force_count, + const int *export_indices, + const double *forces, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_apply_facet_loads( + const int *region_index, + const int *load_count, + const int *facet_ids, + const double *loads, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_apply_motion_boundary( + const int *region_index, + const int *export_index, + const int *target_kind, + const int *mode, + const double *target, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_clear_imports(int *ierr); +extern void SPHLITE_FC_sphft_coupling_get_diagnostics( + const int *value_capacity, + double *values, + const int *count_capacity, + int *counts, + int *ierr); + +static int sphlite_values_are_finite(const double *values, int count) +{ + int i; + + if (values == NULL) { + return 0; + } + for (i = 0; i < count; ++i) { + if (!isfinite(values[i])) { + return 0; + } + } + return 1; +} + static int sphlite_call_path( const char *path, void (*fortran_call)(const int *chars, const int *count, int *ierr)) @@ -453,3 +534,184 @@ int sphlite_get_explosive_release_balance(double *mass_values, energy_values, momentum_values, &ierr); return ierr; } + +int sphlite_coupling_get_region_count(int *count) +{ + int ierr = 0; + + if (count == NULL) { + return -1; + } + SPHLITE_FC_sphft_coupling_get_region_count(count, &ierr); + return ierr; +} + +int sphlite_coupling_get_region_info(int region_index, + char *name, + int name_capacity, + int *region_type, + int *export_mode, + int *import_mode, + int *enabled, + int *particle_count, + int *facet_count) +{ + int ierr = 0; + int actual = 0; + int *chars; + int i; + + if (region_index <= 0 || name == NULL || name_capacity <= 1 || + region_type == NULL || export_mode == NULL || import_mode == NULL || + enabled == NULL || particle_count == NULL || facet_count == NULL) { + return -1; + } + chars = (int *)malloc((size_t)name_capacity * sizeof(int)); + if (chars == NULL) { + return -1; + } + SPHLITE_FC_sphft_coupling_get_region_info(®ion_index, chars, + &name_capacity, &actual, region_type, export_mode, import_mode, + enabled, particle_count, facet_count, &ierr); + if (ierr == 0) { + for (i = 0; i < name_capacity; ++i) { + if (chars[i] == 0) { + name[i] = '\0'; + break; + } + name[i] = (char)chars[i]; + } + name[name_capacity - 1] = '\0'; + } + free(chars); + return ierr; +} + +int sphlite_coupling_export_particles(int region_index, + int capacity, + int *count, + int *ext_ids, + int *internal_indices, + int *phases, + int *statuses, + int *materials, + int *sections, + double *scalar_values, + double *vector_values) +{ + int ierr = 0; + + if (region_index <= 0 || capacity < 0 || count == NULL || + ext_ids == NULL || internal_indices == NULL || phases == NULL || + statuses == NULL || materials == NULL || sections == NULL || + scalar_values == NULL || vector_values == NULL) { + return -1; + } + SPHLITE_FC_sphft_coupling_export_particles(®ion_index, &capacity, + count, ext_ids, internal_indices, phases, statuses, materials, + sections, scalar_values, vector_values, &ierr); + return ierr; +} + +int sphlite_coupling_export_facets(int region_index, + int capacity, + int *count, + int *facet_ids, + int *particle_ids, + int *materials, + int *sections, + double *vector_values, + double *area_values) +{ + int ierr = 0; + + if (region_index <= 0 || capacity < 0 || count == NULL || + facet_ids == NULL || particle_ids == NULL || materials == NULL || + sections == NULL || vector_values == NULL || area_values == NULL) { + return -1; + } + SPHLITE_FC_sphft_coupling_export_facets(®ion_index, &capacity, + count, facet_ids, particle_ids, materials, sections, vector_values, + area_values, &ierr); + return ierr; +} + +int sphlite_coupling_apply_particle_forces(int region_index, + int force_count, + const int *export_indices, + const double *forces) +{ + int ierr = 0; + + if (region_index <= 0 || force_count < 0) { + return -1; + } + if (force_count > 0 && (export_indices == NULL || forces == NULL || + !sphlite_values_are_finite(forces, 3 * force_count))) { + return -1; + } + SPHLITE_FC_sphft_coupling_apply_particle_forces(®ion_index, + &force_count, export_indices, forces, &ierr); + return ierr; +} + +int sphlite_coupling_apply_facet_loads(int region_index, + int load_count, + const int *facet_ids, + const double *loads) +{ + int ierr = 0; + + if (region_index <= 0 || load_count < 0) { + return -1; + } + if (load_count > 0 && (facet_ids == NULL || loads == NULL || + !sphlite_values_are_finite(loads, 3 * load_count))) { + return -1; + } + SPHLITE_FC_sphft_coupling_apply_facet_loads(®ion_index, &load_count, + facet_ids, loads, &ierr); + return ierr; +} + +int sphlite_coupling_apply_motion_boundary(int region_index, + int export_index, + int target_kind, + int mode, + const double *target) +{ + int ierr = 0; + + if (region_index <= 0 || export_index <= 0 || target == NULL || + !sphlite_values_are_finite(target, 3)) { + return -1; + } + SPHLITE_FC_sphft_coupling_apply_motion_boundary(®ion_index, + &export_index, &target_kind, &mode, target, &ierr); + return ierr; +} + +int sphlite_coupling_clear_imports(void) +{ + int ierr = 0; + + SPHLITE_FC_sphft_coupling_clear_imports(&ierr); + return ierr; +} + +int sphlite_coupling_get_diagnostics(double *values, + int value_capacity, + int *counts, + int count_capacity) +{ + int ierr = 0; + + if (values == NULL || counts == NULL || + value_capacity < SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT || + count_capacity < SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT) { + return -1; + } + SPHLITE_FC_sphft_coupling_get_diagnostics(&value_capacity, values, + &count_capacity, counts, &ierr); + return ierr; +} diff --git a/src/output/sph_output_csv.f90 b/src/output/sph_output_csv.f90 index e7614e2..23d2aee 100644 --- a/src/output/sph_output_csv.f90 +++ b/src/output/sph_output_csv.f90 @@ -16,7 +16,11 @@ module sph_output_csv solid_max_softening, solid_dt_cfl, solid_dt_accel, solid_dtcrit, & solid_max_strain_rate, solid_max_acceleration, & solid_internal_force_residual, solid_reference_neighbor_min, & - solid_correction_det_min, solid_correction_status_max + solid_correction_det_min, solid_correction_status_max, & + coupling_region_count, coupling_region_enabled, & + coupling_region_export_mode, coupling_region_particle_count, & + coupling_region_particles, SPH_COUPLING_EXPORT_PARTICLE, & + SPH_COUPLING_EXPORT_FACET, SPH_PHASE_SHELL use sph_gas, only: sph_gas_get_source_summary use sph_explosive_state, only: & explosive_detonation_time, explosive_reaction_progress, & @@ -42,6 +46,10 @@ module sph_output_csv blast_shell_total_force, blast_load_impulse, & blast_load_momentum_input, blast_load_work, blast_load_energy_input, & blast_load_max_particle_force, blast_load_max_acceleration + use sph_coupling_interface, only: coupling_external_force, & + coupling_motion_flag, coupling_current_force, coupling_current_impulse, & + coupling_current_work, coupling_energy_exchange, & + coupling_nonfinite_import_count, coupling_stale_load_count use sph_solid_state, only: solid_stress, solid_plastic_strain, solid_damage, & solid_softening, solid_strain_rate_eq, solid_reference_neighbor_count, & solid_correction_status, solid_failure_status @@ -98,9 +106,12 @@ contains real(rk) :: blast_solid_force(3) real(rk) :: blast_shell_force(3) real(rk) :: blast_shell_moment(3) + real(rk) :: coupling_force(3) integer :: solid_reference_neighbors integer :: solid_correction_state integer :: solid_failure + integer :: coupling_motion_value + integer :: coupling_region_value ierr = SPH_SUCCESS open(unit=SPH_OUTPUT_UNIT, file=trim(filename), status='unknown', & @@ -121,8 +132,9 @@ contains 'blast_solid_fx,blast_solid_fy,blast_solid_fz,' // & 'blast_shell_fx,blast_shell_fy,blast_shell_fz,' // & 'blast_shell_mx,blast_shell_my,blast_shell_mz,' // & + 'coupling_force_x,coupling_force_y,coupling_force_z,' // & 'solid_failure_status,solid_reference_neighbor_count,' // & - 'solid_correction_status' + 'solid_correction_status,coupling_motion_flag,coupling_region_id' do i = 1, npart explosive_td = 0.0_rk explosive_lambda = 0.0_rk @@ -139,9 +151,12 @@ contains blast_solid_force = 0.0_rk blast_shell_force = 0.0_rk blast_shell_moment = 0.0_rk + coupling_force = 0.0_rk solid_reference_neighbors = 0 solid_correction_state = 0 solid_failure = 0 + coupling_motion_value = 0 + coupling_region_value = 0 if (allocated(explosive_detonation_time)) explosive_td = explosive_detonation_time(i) if (allocated(explosive_reaction_progress)) explosive_lambda = & explosive_reaction_progress(i) @@ -172,7 +187,14 @@ contains if (allocated(blast_shell_external_moment)) then blast_shell_moment = blast_shell_external_moment(:,i) end if - write(SPH_OUTPUT_UNIT, '(5(I0,","),36(ES24.16,","),2(I0,","),I0)') & + if (allocated(coupling_external_force)) then + coupling_force = coupling_external_force(:,i) + end if + if (allocated(coupling_motion_flag)) then + coupling_motion_value = coupling_motion_flag(i) + end if + coupling_region_value = sph_output_coupling_region_id(i) + write(SPH_OUTPUT_UNIT, '(5(I0,","),39(ES24.16,","),4(I0,","),I0)') & particle_id_ext(i), particle_phase(i), particle_status(i), & material_id(i), section_id(i), x(1,i), x(2,i), x(3,i), & v(1,i), v(2,i), v(3,i), rho(i), pressure(i), temperature(i), h(i), & @@ -181,8 +203,9 @@ contains solid_stress_value(:), solid_plastic, solid_damage_value, & solid_softening_value, solid_strain_rate_value, & solid_acceleration_norm, blast_solid_force, blast_shell_force, & - blast_shell_moment, solid_failure, solid_reference_neighbors, & - solid_correction_state + blast_shell_moment, coupling_force, solid_failure, & + solid_reference_neighbors, solid_correction_state, & + coupling_motion_value, coupling_region_value end do close(unit=SPH_OUTPUT_UNIT) end subroutine sph_output_write_csv @@ -322,6 +345,7 @@ contains explosive_source_momentum_total(3) call sph_output_write_solid_summary() call sph_output_write_blast_summary() + call sph_output_write_coupling_summary() close(unit=SPH_OUTPUT_UNIT) end subroutine sph_output_write_summary @@ -466,4 +490,77 @@ contains write(metric, '(A,"_",I0)') trim(base_name), region end subroutine sph_output_blast_metric_name + subroutine sph_output_write_coupling_summary() + integer :: particle_total + integer :: facet_total + + call sph_output_coupling_counts(particle_total, facet_total) + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'coupling_regions', & + coupling_region_count + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'coupling_particles', particle_total + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'coupling_facets', facet_total + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_force_x', & + coupling_current_force(1) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_force_y', & + coupling_current_force(2) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_force_z', & + coupling_current_force(3) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_impulse_x', & + coupling_current_impulse(1) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_impulse_y', & + coupling_current_impulse(2) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_impulse_z', & + coupling_current_impulse(3) + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_work', & + coupling_current_work + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_energy_exchange', & + coupling_energy_exchange + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'coupling_nonfinite_imports', & + coupling_nonfinite_import_count + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'coupling_stale_loads', & + coupling_stale_load_count + end subroutine sph_output_write_coupling_summary + + subroutine sph_output_coupling_counts(particle_total, facet_total) + integer, intent(out) :: particle_total + integer, intent(out) :: facet_total + integer :: region + integer :: item + integer :: node_index + + particle_total = 0 + facet_total = 0 + do region = 1, coupling_region_count + if (coupling_region_enabled(region) == 0) cycle + if (coupling_region_export_mode(region) /= SPH_COUPLING_EXPORT_PARTICLE) then + do item = 1, coupling_region_particle_count(region) + node_index = coupling_region_particles(item, region) + if (node_index >= 1 .and. node_index <= npart) then + if (particle_phase(node_index) == SPH_PHASE_SHELL) facet_total = facet_total + 1 + end if + end do + end if + if (coupling_region_export_mode(region) /= SPH_COUPLING_EXPORT_FACET) then + particle_total = particle_total + coupling_region_particle_count(region) + end if + end do + end subroutine sph_output_coupling_counts + + integer function sph_output_coupling_region_id(particle_index) + integer, intent(in) :: particle_index + integer :: region + integer :: item + + sph_output_coupling_region_id = 0 + do region = 1, coupling_region_count + if (coupling_region_enabled(region) == 0) cycle + do item = 1, coupling_region_particle_count(region) + if (coupling_region_particles(item, region) == particle_index) then + sph_output_coupling_region_id = region + return + end if + end do + end do + end function sph_output_coupling_region_id + end module sph_output_csv diff --git a/src/output/sph_restart.f90 b/src/output/sph_restart.f90 index d39c5a4..1e85c50 100644 --- a/src/output/sph_restart.f90 +++ b/src/output/sph_restart.f90 @@ -3,7 +3,8 @@ module sph_restart use sph_error, only: SPH_SUCCESS, SPH_ERROR_IO, SPH_ERROR_INPUT, sph_error_set use sph_control, only: current_time, current_step, output_interval, output_directory, & output_enabled, safety_factor - use sph_database, only: npart, SPH_MAX_MATERIALS, sph_db_allocate, & + use sph_database, only: npart, SPH_MAX_MATERIALS, SPH_MAX_REGIONS, & + SPH_MAX_SET_MEMBERS, sph_db_allocate, & particle_id_ext, particle_phase, particle_status, material_id, & section_id, x0, x, v, a, vhalf, mass, rho, & volume, pressure, internal_energy, temperature, h, water_particle_count, & @@ -18,7 +19,15 @@ module sph_restart solid_internal_energy_total, solid_max_von_mises, & solid_min_plastic_strain, solid_max_plastic_strain, solid_min_damage, & solid_max_damage, solid_min_softening, solid_max_softening, & - solid_dt_cfl, solid_dt_accel, solid_dtcrit + solid_dt_cfl, solid_dt_accel, solid_dtcrit, & + coupling_region_count, coupling_region_name, coupling_region_set_name, & + coupling_region_surface_name, coupling_region_material_name, & + coupling_region_section_name, coupling_region_type, & + coupling_region_export_mode, coupling_region_import_mode, & + coupling_region_enabled, coupling_region_phase_filter, & + coupling_region_material_filter, coupling_region_section_filter, & + coupling_region_internal_id, coupling_region_particle_count, & + coupling_region_particles, coupling_region_disabled_count use sph_water, only: water_rho0, water_c0, water_gamma, water_p0, water_b, & water_visc_alpha, water_visc_beta, water_visc_eta2, water_body_force, & sph_water_update_diagnostics @@ -61,6 +70,14 @@ module sph_restart use sph_solid_restart, only: sph_solid_restart_write, sph_solid_restart_read, & sph_solid_restart_clear use sph_solid_dynamics, only: sph_solid_update_diagnostics + use sph_coupling_interface, only: sph_coupling_reset, & + sph_coupling_allocate_imports, coupling_external_force, & + coupling_motion_velocity, coupling_motion_position, & + coupling_motion_flag, coupling_motion_mode, & + coupling_cumulative_impulse, coupling_momentum_exchange, & + coupling_cumulative_work, coupling_energy_exchange, & + coupling_stale_load_count, coupling_skipped_particle_count, & + coupling_nonfinite_import_count, coupling_imported_force_count use sph_neighbor, only: neighbor_cell_size, neighbor_cell_dims, neighbor_num_cells, & sph_neighbor_rebuild implicit none @@ -88,7 +105,7 @@ contains end if write(SPH_RESTART_UNIT, '(A,1X,I0,1X,I0,1X,I0,1X,A)') 'SPHLITE_RESTART', & - SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_5_1' + SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_8' write(SPH_RESTART_UNIT, *) current_time, current_step, npart write(SPH_RESTART_UNIT, *) output_interval, output_enabled, safety_factor write(SPH_RESTART_UNIT, '(A)') trim(output_directory) @@ -142,6 +159,11 @@ contains end if call sph_restart_write_explosive_state() call sph_solid_restart_write(SPH_RESTART_UNIT) + call sph_restart_write_coupling_state(ierr) + if (ierr /= SPH_SUCCESS) then + close(unit=SPH_RESTART_UNIT) + return + end if close(unit=SPH_RESTART_UNIT) end subroutine sph_restart_write @@ -263,7 +285,8 @@ contains trim(state_version) == 'STATE_0_4' .or. & trim(state_version) == 'STATE_0_4_1' .or. & trim(state_version) == 'STATE_0_5' .or. & - trim(state_version) == 'STATE_0_5_1') then + trim(state_version) == 'STATE_0_5_1' .or. & + trim(state_version) == 'STATE_0_8') then read(SPH_RESTART_UNIT, *, iostat=ios) gas_eos_type, gas_rho0, gas_gamma, & gas_e0, gas_p0, gas_jwl_a, gas_jwl_b, gas_jwl_r1, gas_jwl_r2, & gas_jwl_omega, gas_pinf @@ -324,7 +347,8 @@ contains trim(state_version) == 'STATE_0_4' .or. & trim(state_version) == 'STATE_0_4_1' .or. & trim(state_version) == 'STATE_0_5' .or. & - trim(state_version) == 'STATE_0_5_1') then + trim(state_version) == 'STATE_0_5_1' .or. & + trim(state_version) == 'STATE_0_8') then has_gas_table = .true. has_gas_sources = .true. call sph_restart_read_gas_material_table(ios) @@ -338,9 +362,11 @@ contains if (trim(state_version) == 'STATE_0_4' .or. & trim(state_version) == 'STATE_0_4_1' .or. & trim(state_version) == 'STATE_0_5' .or. & - trim(state_version) == 'STATE_0_5_1') has_explosive_state = .true. + trim(state_version) == 'STATE_0_5_1' .or. & + trim(state_version) == 'STATE_0_8') has_explosive_state = .true. if (trim(state_version) == 'STATE_0_5' .or. & - trim(state_version) == 'STATE_0_5_1') has_solid_state = .true. + trim(state_version) == 'STATE_0_5_1' .or. & + trim(state_version) == 'STATE_0_8') has_solid_state = .true. else if (trim(state_version) /= 'STATE_0_2') then call sph_error_set(SPH_ERROR_INPUT, 'unsupported restart state version') ierr = SPH_ERROR_INPUT @@ -391,6 +417,15 @@ contains else if (ierr == SPH_SUCCESS) then call sph_solid_restart_clear(ierr) end if + if (ierr == SPH_SUCCESS .and. trim(state_version) == 'STATE_0_8') then + call sph_restart_read_coupling_state(ios) + if (ios /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Coupling state') + ierr = SPH_ERROR_INPUT + end if + else if (ierr == SPH_SUCCESS) then + call sph_restart_clear_coupling_state(ierr) + end if if (ierr == SPH_SUCCESS) then call sph_neighbor_rebuild(ierr) if (ierr == SPH_SUCCESS) call sph_water_update_diagnostics(ierr) @@ -491,7 +526,8 @@ contains ios = 0 has_041_state = trim(state_version) == 'STATE_0_4_1' .or. & trim(state_version) == 'STATE_0_5' .or. & - trim(state_version) == 'STATE_0_5_1' + trim(state_version) == 'STATE_0_5_1' .or. & + trim(state_version) == 'STATE_0_8' call sph_explosive_clear_material_table() read(SPH_RESTART_UNIT, *, iostat=ios) table_count if (ios /= 0) then @@ -625,4 +661,154 @@ contains call sph_explosive_allocate_state(ierr) end subroutine sph_restart_clear_explosive_state + subroutine sph_restart_write_coupling_state(ierr) + integer, intent(out) :: ierr + integer :: region + integer :: i + + ierr = SPH_SUCCESS + call sph_coupling_allocate_imports(ierr) + if (ierr /= SPH_SUCCESS) return + write(SPH_RESTART_UNIT, '(A)') 'SPHLITE_COUPLING_STATE' + write(SPH_RESTART_UNIT, *) coupling_region_count, coupling_region_disabled_count + do region = 1, coupling_region_count + write(SPH_RESTART_UNIT, '(A)') trim(coupling_region_name(region)) + write(SPH_RESTART_UNIT, '(A)') trim(coupling_region_set_name(region)) + write(SPH_RESTART_UNIT, '(A)') trim(coupling_region_surface_name(region)) + write(SPH_RESTART_UNIT, '(A)') trim(coupling_region_material_name(region)) + write(SPH_RESTART_UNIT, '(A)') trim(coupling_region_section_name(region)) + write(SPH_RESTART_UNIT, *) coupling_region_type(region), & + coupling_region_export_mode(region), coupling_region_import_mode(region), & + coupling_region_enabled(region), coupling_region_phase_filter(region), & + coupling_region_material_filter(region), coupling_region_section_filter(region), & + coupling_region_internal_id(region), coupling_region_particle_count(region) + if (coupling_region_particle_count(region) > 0) then + write(SPH_RESTART_UNIT, *) & + coupling_region_particles(1:coupling_region_particle_count(region), region) + end if + end do + write(SPH_RESTART_UNIT, *) coupling_cumulative_impulse(:), & + coupling_momentum_exchange(:) + write(SPH_RESTART_UNIT, *) coupling_cumulative_work, coupling_energy_exchange + write(SPH_RESTART_UNIT, *) coupling_stale_load_count, & + coupling_skipped_particle_count, coupling_nonfinite_import_count, & + coupling_imported_force_count + write(SPH_RESTART_UNIT, *) npart + do i = 1, npart + write(SPH_RESTART_UNIT, *) coupling_external_force(:,i), & + coupling_motion_velocity(:,i), coupling_motion_position(:,i), & + coupling_motion_flag(i), coupling_motion_mode(i) + end do + end subroutine sph_restart_write_coupling_state + + subroutine sph_restart_read_coupling_state(ios) + integer, intent(out) :: ios + character(len=64) :: marker + integer :: region + integer :: i + integer :: cache_count + integer :: local_ierr + integer :: imported_force_count + + ios = 0 + call sph_restart_clear_coupling_state(local_ierr) + if (local_ierr /= SPH_SUCCESS) then + ios = 1 + return + end if + read(SPH_RESTART_UNIT, '(A)', iostat=ios) marker + if (ios /= 0) return + if (trim(marker) /= 'SPHLITE_COUPLING_STATE') then + ios = 1 + return + end if + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_region_count, & + coupling_region_disabled_count + if (ios /= 0) return + if (coupling_region_count < 0 .or. coupling_region_count > SPH_MAX_REGIONS) then + ios = 1 + return + end if + do region = 1, coupling_region_count + read(SPH_RESTART_UNIT, '(A)', iostat=ios) coupling_region_name(region) + if (ios /= 0) return + read(SPH_RESTART_UNIT, '(A)', iostat=ios) coupling_region_set_name(region) + if (ios /= 0) return + read(SPH_RESTART_UNIT, '(A)', iostat=ios) coupling_region_surface_name(region) + if (ios /= 0) return + read(SPH_RESTART_UNIT, '(A)', iostat=ios) coupling_region_material_name(region) + if (ios /= 0) return + read(SPH_RESTART_UNIT, '(A)', iostat=ios) coupling_region_section_name(region) + if (ios /= 0) return + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_region_type(region), & + coupling_region_export_mode(region), coupling_region_import_mode(region), & + coupling_region_enabled(region), coupling_region_phase_filter(region), & + coupling_region_material_filter(region), coupling_region_section_filter(region), & + coupling_region_internal_id(region), coupling_region_particle_count(region) + if (ios /= 0) return + if (coupling_region_particle_count(region) < 0 .or. & + coupling_region_particle_count(region) > SPH_MAX_SET_MEMBERS) then + ios = 1 + return + end if + if (coupling_region_particle_count(region) > 0) then + read(SPH_RESTART_UNIT, *, iostat=ios) & + coupling_region_particles(1:coupling_region_particle_count(region), region) + if (ios /= 0) return + end if + end do + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_cumulative_impulse(:), & + coupling_momentum_exchange(:) + if (ios /= 0) return + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_cumulative_work, & + coupling_energy_exchange + if (ios /= 0) return + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_stale_load_count, & + coupling_skipped_particle_count, coupling_nonfinite_import_count, & + imported_force_count + if (ios /= 0) return + read(SPH_RESTART_UNIT, *, iostat=ios) cache_count + if (ios /= 0) return + if (cache_count /= npart) then + ios = 1 + return + end if + call sph_coupling_allocate_imports(local_ierr) + if (local_ierr /= SPH_SUCCESS) then + ios = 1 + return + end if + coupling_imported_force_count = imported_force_count + do i = 1, npart + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_external_force(:,i), & + coupling_motion_velocity(:,i), coupling_motion_position(:,i), & + coupling_motion_flag(i), coupling_motion_mode(i) + if (ios /= 0) return + end do + end subroutine sph_restart_read_coupling_state + + subroutine sph_restart_clear_coupling_state(ierr) + integer, intent(out) :: ierr + + call sph_coupling_reset() + coupling_region_count = 0 + coupling_region_name = ' ' + coupling_region_set_name = ' ' + coupling_region_surface_name = ' ' + coupling_region_material_name = ' ' + coupling_region_section_name = ' ' + coupling_region_type = 0 + coupling_region_export_mode = 0 + coupling_region_import_mode = 0 + coupling_region_enabled = 0 + coupling_region_phase_filter = 0 + coupling_region_material_filter = 0 + coupling_region_section_filter = 0 + coupling_region_internal_id = 0 + coupling_region_particle_count = 0 + coupling_region_particles = 0 + coupling_region_disabled_count = 0 + ierr = SPH_SUCCESS + end subroutine sph_restart_clear_coupling_state + end module sph_restart diff --git a/src/solid/sph_solid_restart.f90 b/src/solid/sph_solid_restart.f90 index 8fecf7b..845dd91 100644 --- a/src/solid/sph_solid_restart.f90 +++ b/src/solid/sph_solid_restart.f90 @@ -95,7 +95,8 @@ contains logical :: has_051_state ios = 0 - has_051_state = trim(state_version) == 'STATE_0_5_1' + has_051_state = trim(state_version) == 'STATE_0_5_1' .or. & + trim(state_version) == 'STATE_0_8' call sph_solid_clear_material_table() read(unit, *, iostat=ios) solid_material_seen, table_count if (ios /= 0) return diff --git a/tests/interface/test_coupling_c_api_080.c b/tests/interface/test_coupling_c_api_080.c new file mode 100644 index 0000000..9488d87 --- /dev/null +++ b/tests/interface/test_coupling_c_api_080.c @@ -0,0 +1,133 @@ +#include "sphlite_c_api.h" + +#include +#include + +static int fail(const char *message) +{ + fprintf(stderr, "%s\n", message); + return 1; +} + +int main(void) +{ + char path[1024]; + char name[128]; + int ierr; + int region_count = 0; + int region_type = 0; + int export_mode = 0; + int import_mode = 0; + int enabled = 0; + int particle_count = 0; + int facet_count = 0; + int ext_ids[3] = {0, 0, 0}; + int internal_indices[3] = {0, 0, 0}; + int phases[3] = {0, 0, 0}; + int statuses[3] = {0, 0, 0}; + int materials[3] = {0, 0, 0}; + int sections[3] = {0, 0, 0}; + int facet_ids[3] = {0, 0, 0}; + int facet_particles[3] = {0, 0, 0}; + int indices[1] = {1}; + int facets[1] = {1}; + double particle_scalars[SPHLITE_COUPLING_PARTICLE_SCALAR_COUNT * 3] = {0.0}; + double particle_vectors[3 * SPHLITE_COUPLING_PARTICLE_VECTOR_COUNT * 3] = {0.0}; + double facet_vectors[3 * SPHLITE_COUPLING_FACET_VECTOR_COUNT * 3] = {0.0}; + double facet_areas[3] = {0.0, 0.0, 0.0}; + double forces[3] = {2.0, 0.0, 0.0}; + double loads[3] = {0.0, 3.0, 0.0}; + double target[3] = {0.0, 0.0, 1.0}; + double diagnostics[SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT] = {0.0}; + int counts[SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT] = {0}; + + if (sphlite_coupling_get_region_count(NULL) != -1) { + return fail("C coupling null region count did not return -1"); + } + if (sphlite_coupling_get_region_info(1, NULL, (int)sizeof(name), + ®ion_type, &export_mode, &import_mode, &enabled, &particle_count, + &facet_count) != -1) { + return fail("C coupling null region name did not return -1"); + } + if (sphlite_coupling_get_region_info(1, name, 1, ®ion_type, + &export_mode, &import_mode, &enabled, &particle_count, + &facet_count) != -1) { + return fail("C coupling small name capacity did not return -1"); + } + if (sphlite_coupling_export_particles(1, 3, &particle_count, NULL, + internal_indices, phases, statuses, materials, sections, + particle_scalars, particle_vectors) != -1) { + return fail("C coupling null particle export buffer did not return -1"); + } + if (sphlite_coupling_get_diagnostics(diagnostics, + SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT - 1, counts, + SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT) != -1) { + return fail("C coupling small diagnostics buffer did not return -1"); + } + forces[0] = HUGE_VAL; + if (sphlite_coupling_apply_particle_forces(1, 1, indices, forces) != -1) { + return fail("C coupling nonfinite force did not return -1"); + } + forces[0] = 2.0; + + ierr = sphlite_initialize(); + if (ierr != 0) return fail("C coupling initialize failed"); + snprintf(path, sizeof(path), "%s/coupling_shell_region_080.inp", + SPHLITE_TEST_DATA_DIR); + ierr = sphlite_read_inp(path); + if (ierr != 0) return fail("C coupling read input failed"); + ierr = sphlite_build_model(); + if (ierr != 0) return fail("C coupling build model failed"); + + ierr = sphlite_coupling_get_region_count(®ion_count); + if (ierr != 0 || region_count != 2) return fail("C coupling region count mismatch"); + ierr = sphlite_coupling_get_region_info(2, name, (int)sizeof(name), + ®ion_type, &export_mode, &import_mode, &enabled, &particle_count, + &facet_count); + if (ierr != 0) return fail("C coupling region info failed"); + if (export_mode != SPHLITE_COUPLING_EXPORT_BOTH || + import_mode != SPHLITE_COUPLING_IMPORT_FORCE || + enabled != 1 || particle_count != 3 || facet_count != 3) { + return fail("C coupling region info content mismatch"); + } + ierr = sphlite_coupling_get_region_info(99, name, (int)sizeof(name), + ®ion_type, &export_mode, &import_mode, &enabled, &particle_count, + &facet_count); + if (ierr == 0) return fail("C coupling illegal region did not fail"); + + ierr = sphlite_coupling_export_particles(2, 3, &particle_count, ext_ids, + internal_indices, phases, statuses, materials, sections, + particle_scalars, particle_vectors); + if (ierr != 0 || particle_count != 3 || ext_ids[0] != 1) { + return fail("C coupling particle export mismatch"); + } + ierr = sphlite_coupling_export_particles(2, 2, &particle_count, ext_ids, + internal_indices, phases, statuses, materials, sections, + particle_scalars, particle_vectors); + if (ierr == 0) return fail("C coupling small particle export did not fail"); + ierr = sphlite_coupling_export_facets(1, 3, &facet_count, facet_ids, + facet_particles, materials, sections, facet_vectors, facet_areas); + if (ierr != 0 || facet_count != 3 || facet_ids[0] != 1 || + facet_areas[0] <= 0.0) { + return fail("C coupling facet export mismatch"); + } + + ierr = sphlite_coupling_apply_particle_forces(2, 1, indices, forces); + if (ierr != 0) return fail("C coupling particle force import failed"); + ierr = sphlite_coupling_apply_facet_loads(1, 1, facets, loads); + if (ierr != 0) return fail("C coupling facet load import failed"); + ierr = sphlite_coupling_apply_motion_boundary(2, 2, + SPHLITE_COUPLING_MOTION_VELOCITY, SPHLITE_COUPLING_MOTION_DIRECT, + target); + if (ierr != 0) return fail("C coupling motion import failed"); + ierr = sphlite_coupling_get_diagnostics(diagnostics, + SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT, counts, + SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT); + if (ierr != 0 || counts[3] != 2) return fail("C coupling diagnostics mismatch"); + ierr = sphlite_coupling_clear_imports(); + if (ierr != 0) return fail("C coupling clear imports failed"); + + ierr = sphlite_finalize(); + if (ierr != 0) return fail("C coupling finalize failed"); + return 0; +} diff --git a/tests/interface/test_coupling_cpp_api_080.cpp b/tests/interface/test_coupling_cpp_api_080.cpp new file mode 100644 index 0000000..2def56a --- /dev/null +++ b/tests/interface/test_coupling_cpp_api_080.cpp @@ -0,0 +1,82 @@ +#include "sphlite.hpp" + +#include +#include + +int main() +{ + if (SPHLITE_COUPLING_EXPORT_BOTH != 3 || + SPHLITE_COUPLING_IMPORT_FORCE != 1) { + return 1; + } + + sphlite::Session session; + std::string input = + std::string(SPHLITE_TEST_DATA_DIR) + "/coupling_shell_region_080.inp"; + sphlite::read_inp(input); + sphlite::build_model(); + + if (sphlite::coupling_region_count() != 2) { + return 1; + } + sphlite::CouplingRegionInfo info = sphlite::coupling_region_info(2); + if (info.name != "SHELL_BOTH" || + info.export_mode != SPHLITE_COUPLING_EXPORT_BOTH || + info.import_mode != SPHLITE_COUPLING_IMPORT_FORCE || + info.particle_count != 3 || info.facet_count != 3) { + return 1; + } + try { + (void)sphlite::coupling_region_info(99); + return 1; + } catch (const sphlite::Error &) { + } + + sphlite::CouplingParticleExport particles = sphlite::coupling_export_particles(2); + if (particles.ext_ids.size() != 3 || particles.ext_ids[0] != 1 || + particles.scalar_values.size() != 3 * SPHLITE_COUPLING_PARTICLE_SCALAR_COUNT || + particles.vector_values.size() != 9 * SPHLITE_COUPLING_PARTICLE_VECTOR_COUNT) { + return 1; + } + sphlite::CouplingFacetExport facets = sphlite::coupling_export_facets(1); + if (facets.facet_ids.size() != 3 || facets.facet_ids[0] != 1 || + facets.area_values[0] <= 0.0) { + return 1; + } + + std::vector indices(1, 1); + std::vector forces; + forces.push_back(2.0); + forces.push_back(0.0); + forces.push_back(0.0); + sphlite::coupling_apply_particle_forces(2, indices, forces); + std::vector facet_ids(1, 1); + std::vector loads; + loads.push_back(0.0); + loads.push_back(3.0); + loads.push_back(0.0); + sphlite::coupling_apply_facet_loads(1, facet_ids, loads); + double target[3] = {0.0, 0.0, 1.0}; + sphlite::coupling_apply_motion_boundary(2, 2, + SPHLITE_COUPLING_MOTION_VELOCITY, SPHLITE_COUPLING_MOTION_DIRECT, + target); + sphlite::CouplingDiagnostics diagnostics = sphlite::coupling_diagnostics(); + if (diagnostics.values.size() != SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT || + diagnostics.counts.size() != SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT || + diagnostics.counts[3] != 2) { + return 1; + } + try { + std::vector bad_forces(2, 0.0); + sphlite::coupling_apply_particle_forces(2, indices, bad_forces); + return 1; + } catch (const sphlite::Error &) { + } + sphlite::coupling_clear_imports(); + diagnostics = sphlite::coupling_diagnostics(); + if (diagnostics.counts[0] < 1) { + return 1; + } + session.close(); + return 0; +} diff --git a/tests/module/test_coupling_fortran_api_080.f90 b/tests/module/test_coupling_fortran_api_080.f90 new file mode 100644 index 0000000..c9fef34 --- /dev/null +++ b/tests/module/test_coupling_fortran_api_080.f90 @@ -0,0 +1,130 @@ +program test_coupling_fortran_api_080 + use sph_precision, only: rk, SPH_NDIM + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model + use sph_coupling_interface, only: sph_coupling_get_region_count, & + sph_coupling_get_region_info, sph_coupling_export_particles, & + sph_coupling_export_facets, sph_coupling_apply_particle_forces, & + sph_coupling_apply_facet_loads, sph_coupling_apply_motion_boundary, & + sph_coupling_clear_imports, sph_coupling_get_diagnostics, & + SPH_COUPLING_PARTICLE_SCALAR_COUNT, & + SPH_COUPLING_PARTICLE_VECTOR_COUNT, SPH_COUPLING_FACET_VECTOR_COUNT, & + SPH_COUPLING_MOTION_VELOCITY, SPH_COUPLING_MOTION_DIRECT + implicit none + + integer :: ierr + + call check_coupling_fortran_api() + +contains + + subroutine check_coupling_fortran_api() + integer :: region_count + character(len=64) :: name + integer :: region_type + integer :: export_mode + integer :: import_mode + integer :: enabled + integer :: particle_count + integer :: facet_count + integer :: ext_ids(3) + integer :: internal_indices(3) + integer :: phases(3) + integer :: statuses(3) + integer :: materials(3) + integer :: sections(3) + real(rk) :: scalar_values(SPH_COUPLING_PARTICLE_SCALAR_COUNT,3) + real(rk) :: particle_vectors(SPH_NDIM,SPH_COUPLING_PARTICLE_VECTOR_COUNT,3) + integer :: facet_ids(3) + integer :: facet_particles(3) + real(rk) :: facet_vectors(SPH_NDIM,SPH_COUPLING_FACET_VECTOR_COUNT,3) + real(rk) :: facet_areas(3) + integer :: indices(1) + integer :: facets(1) + real(rk) :: forces(SPH_NDIM,1) + real(rk) :: loads(SPH_NDIM,1) + real(rk) :: target(SPH_NDIM) + real(rk) :: diag_values(22) + integer :: diag_counts(5) + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for coupling Fortran API') + call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr) + call assert_ok(ierr, 'read failed for coupling Fortran API') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for coupling Fortran API') + + call sph_coupling_get_region_count(region_count, ierr) + call assert_ok(ierr, 'Fortran API region count failed') + if (region_count /= 2) call fail('Fortran API region count mismatch') + call sph_coupling_get_region_info(2, name, region_type, export_mode, & + import_mode, enabled, particle_count, facet_count, ierr) + call assert_ok(ierr, 'Fortran API region info failed') + if (trim(name) /= 'SHELL_BOTH') call fail('Fortran API region name mismatch') + if (enabled /= 1 .or. particle_count /= 3 .or. facet_count /= 3) then + call fail('Fortran API region info count mismatch') + end if + call sph_coupling_get_region_info(99, name, region_type, export_mode, & + import_mode, enabled, particle_count, facet_count, ierr) + if (ierr == SPH_SUCCESS) call fail('Fortran API invalid region did not fail') + + call sph_coupling_export_particles(2, 3, particle_count, ext_ids, & + internal_indices, phases, statuses, materials, sections, scalar_values, & + particle_vectors, ierr) + call assert_ok(ierr, 'Fortran API particle export failed') + if (particle_count /= 3 .or. ext_ids(1) /= 1) then + call fail('Fortran API particle export content mismatch') + end if + call sph_coupling_export_facets(1, 3, facet_count, facet_ids, & + facet_particles, materials, sections, facet_vectors, facet_areas, ierr) + call assert_ok(ierr, 'Fortran API facet export failed') + if (facet_count /= 3 .or. facet_ids(1) /= 1) then + call fail('Fortran API facet export content mismatch') + end if + call sph_coupling_export_particles(2, 2, particle_count, ext_ids(1:2), & + internal_indices(1:2), phases(1:2), statuses(1:2), materials(1:2), & + sections(1:2), scalar_values(:,1:2), particle_vectors(:,:,1:2), ierr) + if (ierr == SPH_SUCCESS) call fail('Fortran API small export did not fail') + + indices(1) = 1 + forces(:,1) = (/2.0_rk, 0.0_rk, 0.0_rk/) + call sph_coupling_apply_particle_forces(2, 1, indices, forces, ierr) + call assert_ok(ierr, 'Fortran API particle force import failed') + facets(1) = 1 + loads(:,1) = (/0.0_rk, 3.0_rk, 0.0_rk/) + call sph_coupling_apply_facet_loads(1, 1, facets, loads, ierr) + call assert_ok(ierr, 'Fortran API facet load import failed') + target = (/1.0_rk, 0.0_rk, 0.0_rk/) + call sph_coupling_apply_motion_boundary(2, 2, SPH_COUPLING_MOTION_VELOCITY, & + SPH_COUPLING_MOTION_DIRECT, target, ierr) + call assert_ok(ierr, 'Fortran API motion import failed') + call sph_coupling_get_diagnostics(22, diag_values, 5, diag_counts, ierr) + call assert_ok(ierr, 'Fortran API diagnostics failed') + if (diag_counts(4) /= 2) call fail('Fortran API import count mismatch') + call sph_coupling_get_diagnostics(21, diag_values(1:21), 5, diag_counts, ierr) + if (ierr == SPH_SUCCESS) call fail('Fortran API small diagnostics did not fail') + call sph_coupling_clear_imports(ierr) + call assert_ok(ierr, 'Fortran API clear imports failed') + call sph_coupling_get_diagnostics(22, diag_values, 5, diag_counts, ierr) + call assert_ok(ierr, 'Fortran API diagnostics after clear failed') + if (diag_counts(1) < 1) call fail('Fortran API stale count missing') + + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for coupling Fortran API') + end subroutine check_coupling_fortran_api + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + + if (ierr_value /= SPH_SUCCESS) call fail(message) + end subroutine assert_ok + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_coupling_fortran_api_080 diff --git a/tests/module/test_coupling_output_080.f90 b/tests/module/test_coupling_output_080.f90 new file mode 100644 index 0000000..ba91568 --- /dev/null +++ b/tests/module/test_coupling_output_080.f90 @@ -0,0 +1,199 @@ +program test_coupling_output_080 + use sph_precision, only: rk, SPH_NDIM + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, & + sph_build_model, sph_set_output_directory, sph_write_output + use sph_coupling_interface, only: sph_coupling_apply_particle_forces + implicit none + + integer :: ierr + + call check_coupling_output_fields() + +contains + + subroutine check_coupling_output_fields() + integer :: indices(1) + real(rk) :: forces(SPH_NDIM,1) + + call delete_output_pair(80) + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for coupling output') + call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr) + call assert_ok(ierr, 'read failed for coupling output') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for coupling output') + indices(1) = 1 + forces(:,1) = (/3.0_rk, 4.0_rk, 5.0_rk/) + call sph_coupling_apply_particle_forces(2, 1, indices, forces, ierr) + call assert_ok(ierr, 'coupling force import failed for output') + call sph_set_output_directory('build-codex-unix', ierr) + call assert_ok(ierr, 'coupling output directory failed') + call sph_write_output(80, 0.0_rk, ierr) + call assert_ok(ierr, 'coupling output write failed') + call check_particle_csv('build-codex-unix/sphlite_00000080.csv') + call check_summary_csv('build-codex-unix/sphlite_00000080_summary.csv') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for coupling output') + end subroutine check_coupling_output_fields + + subroutine check_particle_csv(filename) + character(len=*), intent(in) :: filename + character(len=1024) :: header + integer :: unit + integer :: ios + integer :: id + integer :: phase + integer :: status + integer :: mat + integer :: sec + integer :: failure + integer :: reference_neighbors + integer :: correction_status + integer :: motion_flag + integer :: region_id + real(rk) :: pos(3) + real(rk) :: vel(3) + real(rk) :: scalar_values(4) + real(rk) :: explosive_values(6) + real(rk) :: stress_values(6) + real(rk) :: solid_values(5) + real(rk) :: blast_values(9) + real(rk) :: coupling_force(3) + + unit = 84 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('coupling particle CSV open failed') + read(unit, '(A)', iostat=ios) header + if (ios /= 0) call fail('coupling particle CSV header read failed') + if (index(header, 'coupling_force_x,coupling_force_y,coupling_force_z') <= 0) then + call fail('coupling force particle CSV columns missing') + end if + if (index(header, 'coupling_motion_flag,coupling_region_id') <= 0) then + call fail('coupling motion particle CSV columns missing') + end if + read(unit, *, iostat=ios) id, phase, status, mat, sec, pos, vel, & + scalar_values, explosive_values, stress_values, solid_values, & + blast_values, coupling_force, failure, reference_neighbors, & + correction_status, motion_flag, region_id + close(unit=unit) + if (ios /= 0) call fail('coupling particle CSV row read failed') + if (id /= 1) call fail('coupling output id mismatch') + if (maxval(abs(coupling_force - (/3.0_rk, 4.0_rk, 5.0_rk/))) > & + 1.0e-12_rk) call fail('coupling output force mismatch') + if (region_id <= 0) call fail('coupling output region id missing') + if (motion_flag /= 0) call fail('coupling output motion flag default mismatch') + end subroutine check_particle_csv + + subroutine check_summary_csv(filename) + character(len=*), intent(in) :: filename + + call assert_summary_int(filename, 'coupling_regions', 2) + call assert_summary_int(filename, 'coupling_particles', 3) + call assert_summary_int(filename, 'coupling_facets', 6) + call assert_summary_present(filename, 'coupling_force_x') + call assert_summary_present(filename, 'coupling_force_y') + call assert_summary_present(filename, 'coupling_force_z') + call assert_summary_present(filename, 'coupling_impulse_x') + call assert_summary_present(filename, 'coupling_impulse_y') + call assert_summary_present(filename, 'coupling_impulse_z') + call assert_summary_present(filename, 'coupling_work') + call assert_summary_present(filename, 'coupling_energy_exchange') + call assert_summary_int(filename, 'coupling_nonfinite_imports', 0) + call assert_summary_int(filename, 'coupling_stale_loads', 0) + end subroutine check_summary_csv + + subroutine assert_summary_present(filename, metric) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + character(len=256) :: line + integer :: unit + integer :: ios + logical :: found + + found = .false. + unit = 85 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('coupling summary CSV open failed') + do + read(unit, '(A)', iostat=ios) line + if (ios /= 0) exit + if (index(line, trim(metric) // ',') == 1) found = .true. + end do + close(unit=unit) + if (.not. found) call fail('coupling summary metric missing') + end subroutine assert_summary_present + + subroutine assert_summary_int(filename, metric, expected) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + integer, intent(in) :: expected + integer :: actual + + call read_summary_int(filename, metric, actual) + if (actual /= expected) call fail('coupling summary integer mismatch') + end subroutine assert_summary_int + + subroutine read_summary_int(filename, metric, value) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + integer, intent(out) :: value + character(len=256) :: line + integer :: unit + integer :: ios + integer :: start + + unit = 86 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('coupling summary integer open failed') + do + read(unit, '(A)', iostat=ios) line + if (ios /= 0) exit + if (index(line, trim(metric) // ',') == 1) then + start = len_trim(metric) + 2 + read(line(start:), *, iostat=ios) value + close(unit=unit) + if (ios /= 0) call fail('coupling summary integer parse failed') + return + end if + end do + close(unit=unit) + call fail('coupling summary integer missing') + end subroutine read_summary_int + + subroutine delete_output_pair(step_value) + integer, intent(in) :: step_value + character(len=256) :: filename + + write(filename, '("build-codex-unix/sphlite_",I8.8,".csv")') step_value + call delete_file_if_exists(filename) + write(filename, '("build-codex-unix/sphlite_",I8.8,"_summary.csv")') step_value + call delete_file_if_exists(filename) + end subroutine delete_output_pair + + subroutine delete_file_if_exists(filename) + character(len=*), intent(in) :: filename + logical :: exists + integer :: ios + + inquire(file=trim(filename), exist=exists) + if (.not. exists) return + open(unit=87, file=trim(filename), status='old', iostat=ios) + if (ios == 0) close(unit=87, status='delete') + end subroutine delete_file_if_exists + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + + if (ierr_value /= SPH_SUCCESS) call fail(message) + end subroutine assert_ok + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_coupling_output_080 diff --git a/tests/module/test_coupling_restart_071_compat.f90 b/tests/module/test_coupling_restart_071_compat.f90 new file mode 100644 index 0000000..5fa410d --- /dev/null +++ b/tests/module/test_coupling_restart_071_compat.f90 @@ -0,0 +1,88 @@ +program test_coupling_restart_071_compat + use sph_precision, only: rk + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & + sph_advance_explicit, sph_write_restart, sph_read_restart + use sph_coupling_interface, only: sph_coupling_get_region_count, & + coupling_external_force + implicit none + + integer :: ierr + + call check_legacy_restart_clears_coupling() + +contains + + subroutine check_legacy_restart_clears_coupling() + integer :: region_count + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for coupling compat restart') + call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr) + call assert_ok(ierr, 'read failed for coupling compat restart') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for coupling compat restart') + call sph_write_restart('build-codex-unix/restart_coupling_080_seed.tmp', ierr) + call assert_ok(ierr, 'coupling compat seed restart write failed') + call write_071_compat_restart('build-codex-unix/restart_coupling_080_seed.tmp', & + 'build-codex-unix/restart_coupling_071_compat.tmp') + call sph_read_restart('build-codex-unix/restart_coupling_071_compat.tmp', ierr) + call assert_ok(ierr, 'read coupling legacy restart failed') + call sph_coupling_get_region_count(region_count, ierr) + call assert_ok(ierr, 'legacy restart region count query failed') + if (region_count /= 0) call fail('legacy restart did not clear coupling regions') + if (allocated(coupling_external_force)) then + if (maxval(abs(coupling_external_force)) > 0.0_rk) then + call fail('legacy restart left coupling force cache') + end if + end if + call sph_advance_explicit(1.0e-7_rk, ierr) + call assert_ok(ierr, 'legacy restart continuation failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for coupling legacy restart') + end subroutine check_legacy_restart_clears_coupling + + subroutine write_071_compat_restart(source_filename, target_filename) + character(len=*), intent(in) :: source_filename + character(len=*), intent(in) :: target_filename + character(len=512) :: line + integer :: in_unit + integer :: out_unit + integer :: ios + + in_unit = 97 + out_unit = 98 + open(unit=in_unit, file=trim(source_filename), status='old', action='read', & + iostat=ios) + if (ios /= 0) call fail('open coupling 0.8 restart seed failed') + open(unit=out_unit, file=trim(target_filename), status='unknown', & + action='write', iostat=ios) + if (ios /= 0) call fail('open coupling legacy restart target failed') + read(in_unit, '(A)', iostat=ios) line + if (ios /= 0) call fail('read coupling restart seed header failed') + write(out_unit, '(A)') 'SPHLITE_RESTART 0 5 1 STATE_0_5_1' + do + read(in_unit, '(A)', iostat=ios) line + if (ios /= 0) exit + if (trim(line) == 'SPHLITE_COUPLING_STATE') exit + write(out_unit, '(A)') trim(line) + end do + close(unit=in_unit) + close(unit=out_unit) + end subroutine write_071_compat_restart + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + + if (ierr_value /= SPH_SUCCESS) call fail(message) + end subroutine assert_ok + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_coupling_restart_071_compat diff --git a/tests/module/test_coupling_restart_080.f90 b/tests/module/test_coupling_restart_080.f90 new file mode 100644 index 0000000..dfffc36 --- /dev/null +++ b/tests/module/test_coupling_restart_080.f90 @@ -0,0 +1,177 @@ +program test_coupling_restart_080 + use sph_precision, only: rk, SPH_NDIM + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & + sph_advance_explicit, sph_write_restart, sph_read_restart, & + sph_get_last_error + use sph_coupling_interface, only: coupling_external_force, & + coupling_motion_flag, coupling_motion_mode, coupling_motion_position, & + coupling_imported_force_count, coupling_cumulative_impulse, & + coupling_cumulative_work, coupling_momentum_exchange, & + coupling_energy_exchange, sph_coupling_get_region_count, & + sph_coupling_get_region_info, sph_coupling_apply_particle_forces, & + sph_coupling_apply_facet_loads, sph_coupling_apply_motion_boundary, & + SPH_COUPLING_MOTION_POSITION, SPH_COUPLING_MOTION_DIRECT + implicit none + + integer :: ierr + + call check_coupling_restart_round_trip() + +contains + + subroutine check_coupling_restart_round_trip() + integer :: indices(1) + integer :: facets(1) + real(rk) :: forces(SPH_NDIM,1) + real(rk) :: loads(SPH_NDIM,1) + real(rk) :: target(SPH_NDIM) + integer :: region_count + character(len=64) :: name + integer :: region_type + integer :: export_mode + integer :: import_mode + integer :: enabled + integer :: particle_count + integer :: facet_count + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for coupling restart') + call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr) + call assert_ok(ierr, 'read failed for coupling restart') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for coupling restart') + indices(1) = 1 + forces(:,1) = (/1.0_rk, 2.0_rk, 3.0_rk/) + call sph_coupling_apply_particle_forces(2, 1, indices, forces, ierr) + call assert_ok(ierr, 'restart particle force import failed') + facets(1) = 1 + loads(:,1) = (/0.0_rk, 0.0_rk, 5.0_rk/) + call sph_coupling_apply_facet_loads(1, 1, facets, loads, ierr) + call assert_ok(ierr, 'restart facet load import failed') + target = (/0.11_rk, 0.22_rk, 0.33_rk/) + call sph_coupling_apply_motion_boundary(2, 2, SPH_COUPLING_MOTION_POSITION, & + SPH_COUPLING_MOTION_DIRECT, target, ierr) + call assert_ok(ierr, 'restart motion import failed') + coupling_cumulative_impulse = (/0.1_rk, 0.2_rk, 0.3_rk/) + coupling_momentum_exchange = (/0.4_rk, 0.5_rk, 0.6_rk/) + coupling_cumulative_work = 0.7_rk + coupling_energy_exchange = 0.8_rk + call sph_write_restart('build-codex-unix/restart_coupling_080.tmp', ierr) + call assert_ok(ierr, 'coupling restart write failed') + call assert_restart_state_version('build-codex-unix/restart_coupling_080.tmp', & + 'STATE_0_8') + + call scramble_coupling_state() + call sph_read_restart('build-codex-unix/restart_coupling_080.tmp', ierr) + call assert_ok(ierr, 'coupling restart read failed') + call sph_coupling_get_region_count(region_count, ierr) + call assert_ok(ierr, 'coupling restart region count query failed') + if (region_count /= 2) call fail('coupling restart region count mismatch') + call sph_coupling_get_region_info(1, name, region_type, export_mode, & + import_mode, enabled, particle_count, facet_count, ierr) + call assert_ok(ierr, 'coupling restart region info query failed') + if (trim(name) /= 'SHELL_FACETS') call fail('coupling restart region name mismatch') + if (enabled /= 1 .or. particle_count /= 0 .or. facet_count /= 3) then + call fail('coupling restart facet region mismatch') + end if + call sph_coupling_get_region_info(2, name, region_type, export_mode, & + import_mode, enabled, particle_count, facet_count, ierr) + call assert_ok(ierr, 'coupling restart both-region info query failed') + if (trim(name) /= 'SHELL_BOTH') call fail('coupling restart both region mismatch') + if (particle_count /= 3 .or. facet_count /= 3) then + call fail('coupling restart both region counts mismatch') + end if + call assert_close(coupling_external_force(1,1), 1.0_rk, & + 'coupling restart force x mismatch') + call assert_close(coupling_external_force(2,1), 2.0_rk, & + 'coupling restart force y mismatch') + call assert_close(coupling_external_force(3,1), 8.0_rk, & + 'coupling restart facet load cache mismatch') + if (coupling_motion_flag(2) /= SPH_COUPLING_MOTION_POSITION) then + call fail('coupling restart motion flag mismatch') + end if + if (coupling_motion_mode(2) /= SPH_COUPLING_MOTION_DIRECT) then + call fail('coupling restart motion mode mismatch') + end if + call assert_close(coupling_motion_position(3,2), 0.33_rk, & + 'coupling restart motion target mismatch') + if (coupling_imported_force_count /= 2) then + call fail('coupling restart imported force count mismatch') + end if + call assert_close(coupling_cumulative_impulse(2), 0.2_rk, & + 'coupling restart cumulative impulse mismatch') + call assert_close(coupling_momentum_exchange(3), 0.6_rk, & + 'coupling restart momentum exchange mismatch') + call assert_close(coupling_cumulative_work, 0.7_rk, & + 'coupling restart cumulative work mismatch') + call assert_close(coupling_energy_exchange, 0.8_rk, & + 'coupling restart energy exchange mismatch') + call sph_advance_explicit(1.0e-7_rk, ierr) + call assert_ok(ierr, 'coupling restart continuation failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for coupling restart') + end subroutine check_coupling_restart_round_trip + + subroutine scramble_coupling_state() + coupling_external_force = -999.0_rk + coupling_motion_flag = -999 + coupling_motion_mode = -999 + coupling_motion_position = -999.0_rk + coupling_cumulative_impulse = -999.0_rk + coupling_momentum_exchange = -999.0_rk + coupling_cumulative_work = -999.0_rk + coupling_energy_exchange = -999.0_rk + coupling_imported_force_count = -999 + end subroutine scramble_coupling_state + + subroutine assert_restart_state_version(filename, expected_state) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: expected_state + character(len=64) :: tag + character(len=32) :: state_version + integer :: major + integer :: minor + integer :: patch + integer :: ios + + open(unit=96, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('coupling restart header open failed') + read(96, *, iostat=ios) tag, major, minor, patch, state_version + close(unit=96) + if (ios /= 0) call fail('coupling restart header read failed') + if (trim(tag) /= 'SPHLITE_RESTART') call fail('coupling restart header tag mismatch') + if (trim(state_version) /= trim(expected_state)) then + call fail('coupling restart state version mismatch') + end if + end subroutine assert_restart_state_version + + subroutine assert_ok(ierr_value, message) + integer, intent(in) :: ierr_value + character(len=*), intent(in) :: message + integer :: code + character(len=256) :: last_message + + if (ierr_value /= SPH_SUCCESS) then + call sph_get_last_error(code, last_message, ierr) + write(*, '(A,": ",A)') trim(message), trim(last_message) + call fail(message) + end if + end subroutine assert_ok + + subroutine assert_close(actual, expected, message) + real(rk), intent(in) :: actual + real(rk), intent(in) :: expected + character(len=*), intent(in) :: message + + if (abs(actual - expected) > 1.0e-12_rk) call fail(message) + end subroutine assert_close + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_coupling_restart_080 diff --git a/tests/module/test_output.f90 b/tests/module/test_output.f90 index 36a241d..5337d24 100644 --- a/tests/module/test_output.f90 +++ b/tests/module/test_output.f90 @@ -124,8 +124,9 @@ contains 'blast_solid_fx,blast_solid_fy,blast_solid_fz,' // & 'blast_shell_fx,blast_shell_fy,blast_shell_fz,' // & 'blast_shell_mx,blast_shell_my,blast_shell_mz,' // & + 'coupling_force_x,coupling_force_y,coupling_force_z,' // & 'solid_failure_status,solid_reference_neighbor_count,' // & - 'solid_correction_status') then + 'solid_correction_status,coupling_motion_flag,coupling_region_id') then call fail('particle CSV header mismatch') end if end subroutine check_particle_header @@ -182,6 +183,15 @@ contains call assert_summary_metric(filename, 'solid_correction_status') call assert_summary_metric(filename, 'solid_dtcrit') call assert_summary_metric(filename, 'blast_regions') + call assert_summary_metric(filename, 'coupling_regions') + call assert_summary_metric(filename, 'coupling_particles') + call assert_summary_metric(filename, 'coupling_facets') + call assert_summary_metric(filename, 'coupling_force_x') + call assert_summary_metric(filename, 'coupling_impulse_x') + call assert_summary_metric(filename, 'coupling_work') + call assert_summary_metric(filename, 'coupling_energy_exchange') + call assert_summary_metric(filename, 'coupling_nonfinite_imports') + call assert_summary_metric(filename, 'coupling_stale_loads') end subroutine check_summary_metrics subroutine assert_summary_metric(filename, metric) diff --git a/tests/module/test_solid_output.f90 b/tests/module/test_solid_output.f90 index e6258c4..908e869 100644 --- a/tests/module/test_solid_output.f90 +++ b/tests/module/test_solid_output.f90 @@ -82,8 +82,11 @@ contains real(rk) :: strain_rate_eq real(rk) :: acceleration_norm real(rk) :: blast_force_values(9) + real(rk) :: coupling_force_values(3) integer :: reference_neighbors integer :: correction_status + integer :: motion_flag + integer :: region_id unit = 84 open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) @@ -100,14 +103,16 @@ contains 'blast_solid_fx,blast_solid_fy,blast_solid_fz,' // & 'blast_shell_fx,blast_shell_fy,blast_shell_fz,' // & 'blast_shell_mx,blast_shell_my,blast_shell_mz,' // & + 'coupling_force_x,coupling_force_y,coupling_force_z,' // & 'solid_failure_status,solid_reference_neighbor_count,' // & - 'solid_correction_status') then + 'solid_correction_status,coupling_motion_flag,coupling_region_id') then call fail('Solid particle CSV header mismatch') end if read(unit, *, iostat=ios) id, phase, status, mat, sec, pos, vel, & scalar_values, explosive_values, stress_values, plastic, damage, & - softening, strain_rate_eq, acceleration_norm, blast_force_values, failure, & - reference_neighbors, correction_status + softening, strain_rate_eq, acceleration_norm, blast_force_values, & + coupling_force_values, failure, reference_neighbors, correction_status, & + motion_flag, region_id close(unit=unit) if (ios /= 0) call fail('Solid particle CSV row read failed') if (id /= 1) call fail('Solid particle CSV id mismatch') @@ -135,6 +140,9 @@ contains if (maxval(abs(blast_force_values)) > 1.0e-12_rk) then call fail('Solid particle CSV Blast load pollution') end if + if (maxval(abs(coupling_force_values)) > 1.0e-12_rk) then + call fail('Solid particle CSV coupling load pollution') + end if if (failure /= solid_failure_status(1)) call fail('Solid particle CSV failure mismatch') if (reference_neighbors /= solid_reference_neighbor_count(1)) then call fail('Solid particle CSV reference neighbor mismatch') @@ -142,6 +150,9 @@ contains if (correction_status /= solid_correction_status(1)) then call fail('Solid particle CSV correction status mismatch') end if + if (motion_flag /= 0 .or. region_id /= 0) then + call fail('Solid particle CSV coupling defaults mismatch') + end if end subroutine check_particle_csv subroutine check_summary_csv(filename) diff --git a/todo.md b/todo.md index ec99d0b..d8824cf 100644 --- a/todo.md +++ b/todo.md @@ -229,75 +229,75 @@ ## 阶段 F:Output、Restart、Fortran API 和外部接口层 -- [ ] 定义 summary CSV 的 coupling 字段清单。 -- [ ] 追加 `coupling_regions` summary CSV 字段。 -- [ ] 追加 `coupling_particles` summary CSV 字段。 -- [ ] 追加 `coupling_facets` summary CSV 字段。 -- [ ] 追加 `coupling_force_x/y/z` summary CSV 字段。 -- [ ] 追加 `coupling_impulse_x/y/z` summary CSV 字段。 -- [ ] 追加 `coupling_work` summary CSV 字段。 -- [ ] 追加 `coupling_energy_exchange` summary CSV 字段。 -- [ ] 追加 `coupling_nonfinite_imports` summary CSV 字段。 -- [ ] 追加 `coupling_stale_loads` summary CSV 字段。 -- [ ] 定义粒子 CSV 的 coupling 字段清单。 -- [ ] 追加粒子 CSV 的 `coupling_force_x/y/z` 字段。 -- [ ] 追加粒子 CSV 的 `coupling_motion_flag` 字段。 -- [ ] 追加粒子 CSV 的 `coupling_region_id` 字段。 -- [ ] 明确 0.8.0 restart 状态版本名称。 -- [ ] 实现 `STATE_0_8` 或等价 0.8 restart 写入。 -- [ ] 在 restart 中保存 coupling region 定义和映射。 -- [ ] 在 restart 中保存 coupling export/import mode。 -- [ ] 在 restart 中保存未消费外部力缓存。 -- [ ] 在 restart 中保存未消费 facet load 缓存。 -- [ ] 在 restart 中保存未消费运动边界缓存。 -- [ ] 在 restart 中保存累计冲量和做功。 -- [ ] 在 restart 中保存动量和能量交换诊断。 -- [ ] 在 restart 中保存 stale、skipped 和 nonfinite 计数。 -- [ ] 实现 `STATE_0_8` 或等价 0.8 restart 读回。 -- [ ] 实现 0.7.1 或既有 restart 兼容读入后的空 coupling 状态。 -- [ ] 实现 restart 读回后继续显式步的 coupling 状态恢复。 -- [ ] 新增 `sph_coupling_get_region_count` Fortran API。 -- [ ] 新增 `sph_coupling_get_region_info` Fortran API。 -- [ ] 新增 `sph_coupling_export_particles` Fortran API。 -- [ ] 新增 `sph_coupling_export_facets` Fortran API。 -- [ ] 新增 `sph_coupling_apply_particle_forces` Fortran API。 -- [ ] 新增 `sph_coupling_apply_facet_loads` Fortran API。 -- [ ] 新增 `sph_coupling_apply_motion_boundary` Fortran API。 -- [ ] 新增 `sph_coupling_clear_imports` Fortran API。 -- [ ] 新增 `sph_coupling_get_diagnostics` Fortran API。 -- [ ] 在 C 头文件中声明 coupling region 查询接口。 -- [ ] 在 C 头文件中声明 coupling particle export 接口。 -- [ ] 在 C 头文件中声明 coupling facet export 接口。 -- [ ] 在 C 头文件中声明 coupling force import 接口。 -- [ ] 在 C 头文件中声明 coupling facet load import 接口。 -- [ ] 在 C 头文件中声明 coupling motion import 接口。 -- [ ] 在 C 头文件中声明 coupling diagnostics 接口。 -- [ ] 实现 C ABI 的空指针错误路径。 -- [ ] 实现 C ABI 的小容量错误路径。 -- [ ] 实现 C ABI 的非法 region 错误路径。 -- [ ] 实现 C ABI 的非有限输入错误路径。 -- [ ] 在 C++ wrapper 中追加 coupling region 查询薄包装。 -- [ ] 在 C++ wrapper 中追加 coupling export 薄包装。 -- [ ] 在 C++ wrapper 中追加 coupling import 薄包装。 -- [ ] 在 Python ctypes 中追加 coupling region 查询。 -- [ ] 在 Python ctypes 中追加 coupling particle export。 -- [ ] 在 Python ctypes 中追加 coupling force import。 -- [ ] 在 Python ctypes 中追加 coupling diagnostics 查询。 -- [ ] 在 CLI inspect 中追加 coupling region 列表字段。 -- [ ] 在 CLI inspect 中追加 coupling export/import mode 字段。 -- [ ] 在 CLI inspect 中追加 coupling diagnostics 字段。 -- [ ] 新增 `coupling_output_080` 测试。 -- [ ] 新增 `coupling_restart_080` 测试。 -- [ ] 新增 `coupling_restart_071_compat` 测试。 -- [ ] 新增 `coupling_fortran_api_080` 测试。 -- [ ] 新增 `coupling_c_api_080` 测试。 -- [ ] 新增 `coupling_cpp_api_080` 测试。 -- [ ] 新增 `coupling_python_080` 测试。 -- [ ] 新增 `coupling_cli_080` 测试。 -- [ ] 将阶段 F 新测试接入 CTest。 -- [ ] 运行阶段 F output、restart 和接口专项测试。 -- [ ] 运行 `git diff --check`。 -- [ ] 准备阶段 F 本地提交说明。 +- [x] 定义 summary CSV 的 coupling 字段清单。 +- [x] 追加 `coupling_regions` summary CSV 字段。 +- [x] 追加 `coupling_particles` summary CSV 字段。 +- [x] 追加 `coupling_facets` summary CSV 字段。 +- [x] 追加 `coupling_force_x/y/z` summary CSV 字段。 +- [x] 追加 `coupling_impulse_x/y/z` summary CSV 字段。 +- [x] 追加 `coupling_work` summary CSV 字段。 +- [x] 追加 `coupling_energy_exchange` summary CSV 字段。 +- [x] 追加 `coupling_nonfinite_imports` summary CSV 字段。 +- [x] 追加 `coupling_stale_loads` summary CSV 字段。 +- [x] 定义粒子 CSV 的 coupling 字段清单。 +- [x] 追加粒子 CSV 的 `coupling_force_x/y/z` 字段。 +- [x] 追加粒子 CSV 的 `coupling_motion_flag` 字段。 +- [x] 追加粒子 CSV 的 `coupling_region_id` 字段。 +- [x] 明确 0.8.0 restart 状态版本名称。 +- [x] 实现 `STATE_0_8` 或等价 0.8 restart 写入。 +- [x] 在 restart 中保存 coupling region 定义和映射。 +- [x] 在 restart 中保存 coupling export/import mode。 +- [x] 在 restart 中保存未消费外部力缓存。 +- [x] 在 restart 中保存未消费 facet load 缓存。 +- [x] 在 restart 中保存未消费运动边界缓存。 +- [x] 在 restart 中保存累计冲量和做功。 +- [x] 在 restart 中保存动量和能量交换诊断。 +- [x] 在 restart 中保存 stale、skipped 和 nonfinite 计数。 +- [x] 实现 `STATE_0_8` 或等价 0.8 restart 读回。 +- [x] 实现 0.7.1 或既有 restart 兼容读入后的空 coupling 状态。 +- [x] 实现 restart 读回后继续显式步的 coupling 状态恢复。 +- [x] 新增 `sph_coupling_get_region_count` Fortran API。 +- [x] 新增 `sph_coupling_get_region_info` Fortran API。 +- [x] 新增 `sph_coupling_export_particles` Fortran API。 +- [x] 新增 `sph_coupling_export_facets` Fortran API。 +- [x] 新增 `sph_coupling_apply_particle_forces` Fortran API。 +- [x] 新增 `sph_coupling_apply_facet_loads` Fortran API。 +- [x] 新增 `sph_coupling_apply_motion_boundary` Fortran API。 +- [x] 新增 `sph_coupling_clear_imports` Fortran API。 +- [x] 新增 `sph_coupling_get_diagnostics` Fortran API。 +- [x] 在 C 头文件中声明 coupling region 查询接口。 +- [x] 在 C 头文件中声明 coupling particle export 接口。 +- [x] 在 C 头文件中声明 coupling facet export 接口。 +- [x] 在 C 头文件中声明 coupling force import 接口。 +- [x] 在 C 头文件中声明 coupling facet load import 接口。 +- [x] 在 C 头文件中声明 coupling motion import 接口。 +- [x] 在 C 头文件中声明 coupling diagnostics 接口。 +- [x] 实现 C ABI 的空指针错误路径。 +- [x] 实现 C ABI 的小容量错误路径。 +- [x] 实现 C ABI 的非法 region 错误路径。 +- [x] 实现 C ABI 的非有限输入错误路径。 +- [x] 在 C++ wrapper 中追加 coupling region 查询薄包装。 +- [x] 在 C++ wrapper 中追加 coupling export 薄包装。 +- [x] 在 C++ wrapper 中追加 coupling import 薄包装。 +- [x] 在 Python ctypes 中追加 coupling region 查询。 +- [x] 在 Python ctypes 中追加 coupling particle export。 +- [x] 在 Python ctypes 中追加 coupling force import。 +- [x] 在 Python ctypes 中追加 coupling diagnostics 查询。 +- [x] 在 CLI inspect 中追加 coupling region 列表字段。 +- [x] 在 CLI inspect 中追加 coupling export/import mode 字段。 +- [x] 在 CLI inspect 中追加 coupling diagnostics 字段。 +- [x] 新增 `coupling_output_080` 测试。 +- [x] 新增 `coupling_restart_080` 测试。 +- [x] 新增 `coupling_restart_071_compat` 测试。 +- [x] 新增 `coupling_fortran_api_080` 测试。 +- [x] 新增 `coupling_c_api_080` 测试。 +- [x] 新增 `coupling_cpp_api_080` 测试。 +- [x] 新增 `coupling_python_080` 测试。 +- [x] 新增 `coupling_cli_080` 测试。 +- [x] 将阶段 F 新测试接入 CTest。 +- [x] 运行阶段 F output、restart 和接口专项测试。 +- [x] 运行 `git diff --check`。 +- [x] 准备阶段 F 本地提交说明。 ## 阶段 G:Mock Coupling Smoke、门禁、文档和发布准备 diff --git a/tools/testing/test_coupling_cli_080.py b/tools/testing/test_coupling_cli_080.py new file mode 100644 index 0000000..802d1ed --- /dev/null +++ b/tools/testing/test_coupling_cli_080.py @@ -0,0 +1,97 @@ +from __future__ import annotations + +import argparse +import subprocess +from pathlib import Path + + +def run_command(args: list[str]) -> subprocess.CompletedProcess[str]: + result = subprocess.run(args, text=True, capture_output=True) + if result.returncode != 0: + raise AssertionError(result.stderr or result.stdout) + return result + + +def read_fields(output: str) -> dict[str, str]: + fields: dict[str, str] = {} + for line in output.splitlines(): + if ":" in line: + name, value = line.split(":", 1) + fields[name.strip()] = value.strip() + return fields + + +def require_field(fields: dict[str, str], field: str) -> None: + if field not in fields: + raise AssertionError(f"missing coupling CLI field {field}") + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--exe", required=True) + parser.add_argument("--input", required=True) + args = parser.parse_args() + + exe = str(Path(args.exe)) + model = str(Path(args.input)) + + fields = read_fields(run_command([exe, model]).stdout) + for field in ( + "coupling_regions", + "coupling_region_1_name", + "coupling_region_1_export_mode", + "coupling_region_1_import_mode", + "coupling_region_1_enabled", + "coupling_region_1_facets", + "coupling_region_2_name", + "coupling_region_2_export_mode", + "coupling_region_2_import_mode", + "coupling_region_2_particles", + "coupling_force_x", + "coupling_force_y", + "coupling_force_z", + "coupling_impulse_x", + "coupling_impulse_y", + "coupling_impulse_z", + "coupling_work", + "coupling_energy_exchange", + "coupling_nonfinite_imports", + "coupling_stale_loads", + ): + require_field(fields, field) + + if int(fields["coupling_regions"]) != 2: + raise AssertionError("coupling CLI region count mismatch") + if fields["coupling_region_1_name"] != "SHELL_FACETS": + raise AssertionError("coupling CLI region 1 name mismatch") + if int(fields["coupling_region_1_export_mode"]) != 2: + raise AssertionError("coupling CLI facet export mode mismatch") + if int(fields["coupling_region_1_import_mode"]) != 2: + raise AssertionError("coupling CLI facet import mode mismatch") + if int(fields["coupling_region_1_enabled"]) != 1: + raise AssertionError("coupling CLI enabled flag mismatch") + if int(fields["coupling_region_1_facets"]) != 3: + raise AssertionError("coupling CLI facet count mismatch") + if fields["coupling_region_2_name"] != "SHELL_BOTH": + raise AssertionError("coupling CLI region 2 name mismatch") + if int(fields["coupling_region_2_export_mode"]) != 3: + raise AssertionError("coupling CLI both export mode mismatch") + if int(fields["coupling_region_2_import_mode"]) != 1: + raise AssertionError("coupling CLI force import mode mismatch") + if int(fields["coupling_region_2_particles"]) != 3: + raise AssertionError("coupling CLI particle count mismatch") + if int(fields["coupling_nonfinite_imports"]) != 0: + raise AssertionError("coupling CLI nonfinite count mismatch") + if int(fields["coupling_stale_loads"]) != 0: + raise AssertionError("coupling CLI stale count mismatch") + + check_fields = read_fields(run_command([exe, model, "--check"]).stdout) + if check_fields["mode"] != "check": + raise AssertionError("coupling CLI check mode mismatch") + if int(check_fields["coupling_regions"]) != 2: + raise AssertionError("coupling CLI check region count mismatch") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/testing/test_coupling_python_080.py b/tools/testing/test_coupling_python_080.py new file mode 100644 index 0000000..15613d5 --- /dev/null +++ b/tools/testing/test_coupling_python_080.py @@ -0,0 +1,76 @@ +from __future__ import annotations + +import argparse +import sys +from pathlib import Path + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--library", required=True) + parser.add_argument("--input", required=True) + args = parser.parse_args() + + root = Path(__file__).resolve().parents[2] + sys.path.insert(0, str(root / "python")) + + from sphlite_debug import ( # noqa: PLC0415 + COUPLING_EXPORT_BOTH, + COUPLING_IMPORT_FORCE, + PHASE_SHELL, + SPHLite, + ) + + wrapper_source = (root / "python" / "sphlite_debug.py").read_text(encoding="utf-8") + if "sphlite_coupling_get_region_count" not in wrapper_source: + raise AssertionError("Python wrapper is missing coupling region query") + if "sphlite_coupling_apply_particle_forces" not in wrapper_source: + raise AssertionError("Python wrapper is missing coupling force import") + if "sphft_" in wrapper_source: + raise AssertionError("Python wrapper directly references Fortran symbols") + + solver = SPHLite(args.library) + solver.initialize() + try: + summary = solver.inspect(args.input) + if summary["coupling_regions"] != 2: + raise AssertionError("coupling inspect region count mismatch") + region_info = summary["coupling_region_info"] + if region_info[1]["name"] != "SHELL_BOTH": + raise AssertionError("coupling inspect region name mismatch") + if region_info[1]["export_mode"] != COUPLING_EXPORT_BOTH: + raise AssertionError("coupling inspect export mode mismatch") + if region_info[1]["import_mode"] != COUPLING_IMPORT_FORCE: + raise AssertionError("coupling inspect import mode mismatch") + if summary["coupling_nonfinite_imports"] != 0: + raise AssertionError("coupling inspect nonfinite count mismatch") + + export = solver.coupling_particle_export(2) + if len(export["ext_ids"]) != 3 or export["ext_ids"][0] != 1: + raise AssertionError("coupling Python particle export mismatch") + if export["phases"][0] != PHASE_SHELL: + raise AssertionError("coupling Python particle phase mismatch") + if len(export["scalar_values"]) != 3 * 7: + raise AssertionError("coupling Python scalar export shape mismatch") + if len(export["vector_values"]) != 3 * 3 * 3: + raise AssertionError("coupling Python vector export shape mismatch") + + solver.coupling_apply_particle_forces(2, [1], [2.0, 0.0, 0.0]) + diagnostics = solver.coupling_diagnostics() + if diagnostics["coupling_imported_forces"] != 1: + raise AssertionError("coupling Python diagnostics import count mismatch") + if len(diagnostics["coupling_force"]) != 3: + raise AssertionError("coupling Python diagnostics force shape mismatch") + try: + solver.coupling_apply_particle_forces(2, [1], [1.0, 0.0]) + except ValueError: + pass + else: + raise AssertionError("coupling Python force shape error did not raise") + finally: + solver.finalize() + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())