From 0c5728542a8a38270da1942b1eb2e02504894457 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 4 Jul 2026 11:43:00 +0800 Subject: [PATCH] Add 0.8.1 coupling output restart APIs --- CMakeLists.txt | 67 +++++ Python/sphlite_debug.py | 250 +++++++++++++++++- include/sphlite.hpp | 101 ++++++- include/sphlite_c_api.h | 58 +++- src/app/sph_main.c | 23 ++ .../sph_coupling_interface.f90 | 24 ++ src/interface/sph_api.f90 | 181 ++++++++++--- src/interface/sph_c_api.c | 197 ++++++++++++++ src/output/sph_output_csv.f90 | 41 ++- src/output/sph_restart.f90 | 146 ++++++++-- src/solid/sph_solid_restart.f90 | 3 +- tests/interface/test_coupling_c_api_081.c | 144 ++++++++++ tests/interface/test_coupling_cpp_api_081.cpp | 98 +++++++ .../module/test_coupling_fortran_api_081.f90 | 169 ++++++++++++ tests/module/test_coupling_output_080.f90 | 30 ++- tests/module/test_coupling_output_081.f90 | 234 ++++++++++++++++ tests/module/test_coupling_restart_080.f90 | 2 +- .../test_coupling_restart_080_compat.f90 | 219 +++++++++++++++ tests/module/test_coupling_restart_081.f90 | 186 +++++++++++++ todo.md | 100 +++---- tools/testing/test_coupling_cli_081.py | 90 +++++++ tools/testing/test_coupling_python_081.py | 105 ++++++++ 22 files changed, 2337 insertions(+), 131 deletions(-) create mode 100644 tests/interface/test_coupling_c_api_081.c create mode 100644 tests/interface/test_coupling_cpp_api_081.cpp create mode 100644 tests/module/test_coupling_fortran_api_081.f90 create mode 100644 tests/module/test_coupling_output_081.f90 create mode 100644 tests/module/test_coupling_restart_080_compat.f90 create mode 100644 tests/module/test_coupling_restart_081.f90 create mode 100644 tools/testing/test_coupling_cli_081.py create mode 100644 tools/testing/test_coupling_python_081.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 21e140c..46bf8bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,12 +64,19 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") #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_get_region_modes sphft_coupling_get_region_modes_ #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_facet_pressures sphft_coupling_apply_facet_pressures_ +#define SPHLITE_FC_sphft_coupling_apply_facet_tractions sphft_coupling_apply_facet_tractions_ +#define SPHLITE_FC_sphft_coupling_apply_facet_resultants sphft_coupling_apply_facet_resultants_ +#define SPHLITE_FC_sphft_coupling_apply_contact_state sphft_coupling_apply_contact_state_ #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_load_balance sphft_coupling_get_load_balance_ +#define SPHLITE_FC_sphft_coupling_get_contact_diagnostics sphft_coupling_get_contact_diagnostics_ #define SPHLITE_FC_sphft_coupling_get_diagnostics sphft_coupling_get_diagnostics_ #endif ") @@ -339,12 +346,30 @@ set_target_properties(test_coupling_output_080 PROPERTIES Fortran_MODULE_DIRECTO 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_output_081 tests/module/test_coupling_output_081.f90) +target_link_libraries(test_coupling_output_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_output_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_output_081 COMMAND test_coupling_output_081) +set_tests_properties(coupling_output_081 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_081 tests/module/test_coupling_restart_081.f90) +target_link_libraries(test_coupling_restart_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_restart_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_restart_081 COMMAND test_coupling_restart_081) +set_tests_properties(coupling_restart_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_restart_080_compat tests/module/test_coupling_restart_080_compat.f90) +target_link_libraries(test_coupling_restart_080_compat PRIVATE sphlite_core) +set_target_properties(test_coupling_restart_080_compat PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_restart_080_compat COMMAND test_coupling_restart_080_compat) +set_tests_properties(coupling_restart_080_compat 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}") @@ -357,6 +382,12 @@ set_target_properties(test_coupling_fortran_api_080 PROPERTIES Fortran_MODULE_DI 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_coupling_fortran_api_081 tests/module/test_coupling_fortran_api_081.f90) +target_link_libraries(test_coupling_fortran_api_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_fortran_api_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_fortran_api_081 COMMAND test_coupling_fortran_api_081) +set_tests_properties(coupling_fortran_api_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_coupling_smoke_080 tests/module/test_coupling_smoke_080.f90) target_link_libraries(test_coupling_smoke_080 PRIVATE sphlite_core) set_target_properties(test_coupling_smoke_080 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") @@ -810,6 +841,15 @@ 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_coupling_c_api_081 tests/interface/test_coupling_c_api_081.c) +target_include_directories(test_coupling_c_api_081 PRIVATE include) +target_compile_definitions(test_coupling_c_api_081 PRIVATE + SPHLITE_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data" +) +target_link_libraries(test_coupling_c_api_081 PRIVATE sphlite_c) +add_test(NAME coupling_c_api_081 COMMAND test_coupling_c_api_081) +set_tests_properties(coupling_c_api_081 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 @@ -830,6 +870,15 @@ 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_coupling_cpp_api_081 tests/interface/test_coupling_cpp_api_081.cpp) +target_include_directories(test_coupling_cpp_api_081 PRIVATE include) +target_compile_definitions(test_coupling_cpp_api_081 PRIVATE + SPHLITE_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data" +) +target_link_libraries(test_coupling_cpp_api_081 PRIVATE sphlite_c) +add_test(NAME coupling_cpp_api_081 COMMAND test_coupling_cpp_api_081) +set_tests_properties(coupling_cpp_api_081 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}") @@ -1135,6 +1184,15 @@ add_test( ) set_tests_properties(coupling_python_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_test( + NAME coupling_python_081 + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/test_coupling_python_081.py" + --library "$" + --input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/coupling_load_mode_081.inp" +) +set_tests_properties(coupling_python_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_test( NAME cli COMMAND "${Python3_EXECUTABLE}" @@ -1209,6 +1267,15 @@ add_test( ) set_tests_properties(coupling_cli_080 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_test( + NAME coupling_cli_081 + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/test_coupling_cli_081.py" + --exe "$" + --input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/coupling_load_mode_081.inp" +) +set_tests_properties(coupling_cli_081 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 c44ac5c..bc1ed37 100644 --- a/Python/sphlite_debug.py +++ b/Python/sphlite_debug.py @@ -35,10 +35,24 @@ COUPLING_IMPORT_FORCE = 1 COUPLING_IMPORT_FACET_LOAD = 2 COUPLING_IMPORT_MOTION = 3 +COUPLING_LOAD_PARTICLE_FORCE = 1 +COUPLING_LOAD_FACET_FORCE = 2 +COUPLING_LOAD_PRESSURE = 3 +COUPLING_LOAD_TRACTION = 4 + +COUPLING_DISTRIBUTION_UNIFORM = 1 +COUPLING_DISTRIBUTION_AREA = 2 +COUPLING_DISTRIBUTION_MASS = 3 + +COUPLING_CONTACT_STATE_NONE = 0 +COUPLING_CONTACT_STATE_IMPORT = 1 + COUPLING_PARTICLE_SCALAR_COUNT = 7 COUPLING_PARTICLE_VECTOR_COUNT = 3 -COUPLING_DIAGNOSTIC_VALUE_COUNT = 22 -COUPLING_DIAGNOSTIC_COUNT_COUNT = 5 +COUPLING_DIAGNOSTIC_VALUE_COUNT = 27 +COUPLING_DIAGNOSTIC_COUNT_COUNT = 12 +COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT = 1 +COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT = 7 class SPHLiteError(RuntimeError): @@ -165,6 +179,13 @@ class SPHLite: ctypes.POINTER(ctypes.c_int), ] self.lib.sphlite_coupling_get_region_info.restype = ctypes.c_int + self.lib.sphlite_coupling_get_region_modes.argtypes = [ + ctypes.c_int, + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_int), + ] + self.lib.sphlite_coupling_get_region_modes.restype = ctypes.c_int self.lib.sphlite_coupling_export_particles.argtypes = [ ctypes.c_int, ctypes.c_int, @@ -186,6 +207,60 @@ class SPHLite: ctypes.POINTER(ctypes.c_double), ] self.lib.sphlite_coupling_apply_particle_forces.restype = ctypes.c_int + self.lib.sphlite_coupling_apply_facet_pressures.argtypes = [ + ctypes.c_int, + ctypes.c_int, + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_double), + ctypes.c_int, + ctypes.c_int, + ] + self.lib.sphlite_coupling_apply_facet_pressures.restype = ctypes.c_int + self.lib.sphlite_coupling_apply_facet_tractions.argtypes = [ + ctypes.c_int, + ctypes.c_int, + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_double), + ctypes.c_int, + ctypes.c_int, + ] + self.lib.sphlite_coupling_apply_facet_tractions.restype = ctypes.c_int + self.lib.sphlite_coupling_apply_facet_resultants.argtypes = [ + ctypes.c_int, + ctypes.c_int, + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_double), + ctypes.c_int, + ctypes.c_int, + ] + self.lib.sphlite_coupling_apply_facet_resultants.restype = ctypes.c_int + self.lib.sphlite_coupling_apply_contact_state.argtypes = [ + ctypes.c_int, + ctypes.c_int, + ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.c_int, + ] + self.lib.sphlite_coupling_apply_contact_state.restype = ctypes.c_int + self.lib.sphlite_coupling_get_load_balance.argtypes = [ + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ctypes.POINTER(ctypes.c_double), + ] + self.lib.sphlite_coupling_get_load_balance.restype = ctypes.c_int + self.lib.sphlite_coupling_get_contact_diagnostics.argtypes = [ + ctypes.POINTER(ctypes.c_int), + ctypes.c_int, + ctypes.POINTER(ctypes.c_double), + ctypes.c_int, + ] + self.lib.sphlite_coupling_get_contact_diagnostics.restype = ctypes.c_int self.lib.sphlite_coupling_get_diagnostics.argtypes = [ ctypes.POINTER(ctypes.c_double), ctypes.c_int, @@ -530,6 +605,9 @@ class SPHLite: enabled = ctypes.c_int(0) particle_count = ctypes.c_int(0) facet_count = ctypes.c_int(0) + load_mode = ctypes.c_int(0) + distribution_mode = ctypes.c_int(0) + contact_state_mode = ctypes.c_int(0) self._check(self.lib.sphlite_coupling_get_region_info( int(region_index), buffer, @@ -541,11 +619,20 @@ class SPHLite: ctypes.byref(particle_count), ctypes.byref(facet_count), )) + self._check(self.lib.sphlite_coupling_get_region_modes( + int(region_index), + ctypes.byref(load_mode), + ctypes.byref(distribution_mode), + ctypes.byref(contact_state_mode), + )) 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), + "load_mode": int(load_mode.value), + "distribution_mode": int(distribution_mode.value), + "contact_state_mode": int(contact_state_mode.value), "enabled": int(enabled.value), "particle_count": int(particle_count.value), "facet_count": int(facet_count.value), @@ -609,6 +696,153 @@ class SPHLite: force_array, )) + def coupling_apply_facet_pressures( + self, + region_index: int, + facet_ids: list[int], + pressures: list[float], + distribution_mode: int, + coupling_step: int, + ) -> None: + count = len(facet_ids) + if len(pressures) != count: + raise ValueError("coupling pressure vector size mismatch") + id_array = (ctypes.c_int * max(1, count))(*facet_ids) + pressure_array = (ctypes.c_double * max(1, count))(*pressures) + self._check(self.lib.sphlite_coupling_apply_facet_pressures( + int(region_index), + count, + id_array, + pressure_array, + int(distribution_mode), + int(coupling_step), + )) + + def coupling_apply_facet_tractions( + self, + region_index: int, + facet_ids: list[int], + tractions: list[float], + distribution_mode: int, + coupling_step: int, + ) -> None: + count = len(facet_ids) + if len(tractions) != 3 * count: + raise ValueError("coupling traction vector size mismatch") + id_array = (ctypes.c_int * max(1, count))(*facet_ids) + traction_array = (ctypes.c_double * max(1, 3 * count))(*tractions) + self._check(self.lib.sphlite_coupling_apply_facet_tractions( + int(region_index), + count, + id_array, + traction_array, + int(distribution_mode), + int(coupling_step), + )) + + def coupling_apply_facet_resultants( + self, + region_index: int, + facet_ids: list[int], + resultants: list[float], + distribution_mode: int, + coupling_step: int, + ) -> None: + count = len(facet_ids) + if len(resultants) != 3 * count: + raise ValueError("coupling resultant vector size mismatch") + id_array = (ctypes.c_int * max(1, count))(*facet_ids) + resultant_array = (ctypes.c_double * max(1, 3 * count))(*resultants) + self._check(self.lib.sphlite_coupling_apply_facet_resultants( + int(region_index), + count, + id_array, + resultant_array, + int(distribution_mode), + int(coupling_step), + )) + + def coupling_apply_contact_state( + self, + region_index: int, + facet_ids: list[int], + normals: list[float], + gaps: list[float], + penetrations: list[float], + contact_pressures: list[float], + coupling_step: int, + ) -> None: + count = len(facet_ids) + if ( + len(normals) != 3 * count + or len(gaps) != count + or len(penetrations) != count + or len(contact_pressures) != count + ): + raise ValueError("coupling contact state vector size mismatch") + id_array = (ctypes.c_int * max(1, count))(*facet_ids) + normal_array = (ctypes.c_double * max(1, 3 * count))(*normals) + gap_array = (ctypes.c_double * max(1, count))(*gaps) + penetration_array = (ctypes.c_double * max(1, count))(*penetrations) + pressure_array = (ctypes.c_double * max(1, count))(*contact_pressures) + self._check(self.lib.sphlite_coupling_apply_contact_state( + int(region_index), + count, + id_array, + normal_array, + gap_array, + penetration_array, + pressure_array, + int(coupling_step), + )) + + def coupling_load_balance(self) -> dict[str, float | list[float]]: + vector_type = ctypes.c_double * 3 + input_force = vector_type() + distributed_force = vector_type() + input_moment = vector_type() + distributed_moment = vector_type() + force_error = ctypes.c_double(0.0) + moment_error = ctypes.c_double(0.0) + self._check(self.lib.sphlite_coupling_get_load_balance( + input_force, + distributed_force, + ctypes.byref(force_error), + input_moment, + distributed_moment, + ctypes.byref(moment_error), + )) + return { + "coupling_input_force": list(input_force), + "coupling_distributed_force": list(distributed_force), + "coupling_force_balance_error": float(force_error.value), + "coupling_input_moment": list(input_moment), + "coupling_distributed_moment": list(distributed_moment), + "coupling_moment_balance_error": float(moment_error.value), + } + + def coupling_contact_diagnostics(self) -> dict[str, float | int]: + count_array = ctypes.c_int * COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT + value_array = ctypes.c_double * COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT + counts = count_array() + values = value_array() + self._check(self.lib.sphlite_coupling_get_contact_diagnostics( + counts, + COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT, + values, + COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT, + )) + return { + "coupling_active_contacts": int(counts[0]), + "coupling_skipped_contacts": int(counts[1]), + "coupling_stale_loads": int(counts[2]), + "coupling_repeated_imports": int(counts[3]), + "coupling_nonfinite_contact_imports": int(counts[4]), + "coupling_bad_facet_areas": int(counts[5]), + "coupling_invalid_weights": int(counts[6]), + "coupling_max_penetration": float(values[0]), + } + 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 @@ -634,11 +868,23 @@ class SPHLite: "coupling_max_acceleration": values_list[19], "coupling_max_velocity_error": values_list[20], "coupling_max_position_error": values_list[21], + "coupling_force_balance_error": values_list[22], + "coupling_moment_balance_error": values_list[23], + "coupling_max_pressure": values_list[24], + "coupling_max_traction": values_list[25], + "coupling_max_penetration": values_list[26], "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]), + "coupling_repeated_imports": int(counts[5]), + "coupling_duplicate_facets": int(counts[6]), + "coupling_invalid_weights": int(counts[7]), + "coupling_active_contacts": int(counts[8]), + "coupling_skipped_contacts": int(counts[9]), + "coupling_nonfinite_contact_imports": int(counts[10]), + "coupling_bad_facet_areas": int(counts[11]), } def inspect(self, filename: str | Path) -> dict[str, int | float | list[float]]: diff --git a/include/sphlite.hpp b/include/sphlite.hpp index faabe2d..c928544 100644 --- a/include/sphlite.hpp +++ b/include/sphlite.hpp @@ -149,6 +149,9 @@ struct CouplingRegionInfo { int region_type; int export_mode; int import_mode; + int load_mode; + int distribution_mode; + int contact_state_mode; int enabled; int particle_count; int facet_count; @@ -179,6 +182,20 @@ struct CouplingDiagnostics { std::vector counts; }; +struct CouplingLoadBalance { + double input_force[3]; + double distributed_force[3]; + double force_error; + double input_moment[3]; + double distributed_moment[3]; + double moment_error; +}; + +struct CouplingContactDiagnostics { + std::vector counts; + std::vector values; +}; + class Error : public std::runtime_error { public: explicit Error(const std::string &message) : std::runtime_error(message) {} @@ -442,11 +459,13 @@ inline int coupling_region_count() inline CouplingRegionInfo coupling_region_info(int region_index) { char name[128]; - CouplingRegionInfo info = {"", 0, 0, 0, 0, 0, 0}; + CouplingRegionInfo info = {"", 0, 0, 0, 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)); + check(sphlite_coupling_get_region_modes(region_index, &info.load_mode, + &info.distribution_mode, &info.contact_state_mode)); info.name = std::string(name); return info; } @@ -529,6 +548,66 @@ inline void coupling_apply_facet_loads(int region_index, static_cast(facet_ids.size()), facet_ids.data(), loads.data())); } +inline void coupling_apply_facet_pressures(int region_index, + const std::vector &facet_ids, + const std::vector &pressures, + int distribution_mode, + int coupling_step) +{ + if (pressures.size() != facet_ids.size()) { + throw Error("coupling pressure vector size mismatch"); + } + check(sphlite_coupling_apply_facet_pressures(region_index, + static_cast(facet_ids.size()), facet_ids.data(), pressures.data(), + distribution_mode, coupling_step)); +} + +inline void coupling_apply_facet_tractions(int region_index, + const std::vector &facet_ids, + const std::vector &tractions, + int distribution_mode, + int coupling_step) +{ + if (tractions.size() != 3 * facet_ids.size()) { + throw Error("coupling traction vector size mismatch"); + } + check(sphlite_coupling_apply_facet_tractions(region_index, + static_cast(facet_ids.size()), facet_ids.data(), tractions.data(), + distribution_mode, coupling_step)); +} + +inline void coupling_apply_facet_resultants(int region_index, + const std::vector &facet_ids, + const std::vector &resultants, + int distribution_mode, + int coupling_step) +{ + if (resultants.size() != 3 * facet_ids.size()) { + throw Error("coupling resultant vector size mismatch"); + } + check(sphlite_coupling_apply_facet_resultants(region_index, + static_cast(facet_ids.size()), facet_ids.data(), resultants.data(), + distribution_mode, coupling_step)); +} + +inline void coupling_apply_contact_state(int region_index, + const std::vector &facet_ids, + const std::vector &normals, + const std::vector &gaps, + const std::vector &penetrations, + const std::vector &pressures, + int coupling_step) +{ + std::size_t count = facet_ids.size(); + if (normals.size() != 3 * count || gaps.size() != count || + penetrations.size() != count || pressures.size() != count) { + throw Error("coupling contact state vector size mismatch"); + } + check(sphlite_coupling_apply_contact_state(region_index, + static_cast(count), facet_ids.data(), normals.data(), gaps.data(), + penetrations.data(), pressures.data(), coupling_step)); +} + inline void coupling_apply_motion_boundary(int region_index, int export_index, int target_kind, @@ -544,6 +623,26 @@ inline void coupling_clear_imports() check(sphlite_coupling_clear_imports()); } +inline CouplingLoadBalance coupling_load_balance() +{ + CouplingLoadBalance balance = {}; + check(sphlite_coupling_get_load_balance(balance.input_force, + balance.distributed_force, &balance.force_error, balance.input_moment, + balance.distributed_moment, &balance.moment_error)); + return balance; +} + +inline CouplingContactDiagnostics coupling_contact_diagnostics() +{ + CouplingContactDiagnostics diagnostics; + diagnostics.counts.resize(SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT); + diagnostics.values.resize(SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT); + check(sphlite_coupling_get_contact_diagnostics(diagnostics.counts.data(), + static_cast(diagnostics.counts.size()), diagnostics.values.data(), + static_cast(diagnostics.values.size()))); + return diagnostics; +} + inline CouplingDiagnostics coupling_diagnostics() { CouplingDiagnostics diagnostics; diff --git a/include/sphlite_c_api.h b/include/sphlite_c_api.h index a88bf43..58e4b55 100644 --- a/include/sphlite_c_api.h +++ b/include/sphlite_c_api.h @@ -46,6 +46,18 @@ extern "C" { #define SPHLITE_COUPLING_IMPORT_FACET_LOAD 2 #define SPHLITE_COUPLING_IMPORT_MOTION 3 +#define SPHLITE_COUPLING_LOAD_PARTICLE_FORCE 1 +#define SPHLITE_COUPLING_LOAD_FACET_FORCE 2 +#define SPHLITE_COUPLING_LOAD_PRESSURE 3 +#define SPHLITE_COUPLING_LOAD_TRACTION 4 + +#define SPHLITE_COUPLING_DISTRIBUTION_UNIFORM 1 +#define SPHLITE_COUPLING_DISTRIBUTION_AREA 2 +#define SPHLITE_COUPLING_DISTRIBUTION_MASS 3 + +#define SPHLITE_COUPLING_CONTACT_STATE_NONE 0 +#define SPHLITE_COUPLING_CONTACT_STATE_IMPORT 1 + #define SPHLITE_COUPLING_MOTION_NONE 0 #define SPHLITE_COUPLING_MOTION_VELOCITY 1 #define SPHLITE_COUPLING_MOTION_POSITION 2 @@ -55,8 +67,10 @@ extern "C" { #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 +#define SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT 27 +#define SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT 12 +#define SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT 1 +#define SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT 7 SPHLITE_API int sphlite_initialize(void); SPHLITE_API int sphlite_finalize(void); @@ -153,6 +167,10 @@ SPHLITE_API int sphlite_coupling_get_region_info(int region_index, int *enabled, int *particle_count, int *facet_count); +SPHLITE_API int sphlite_coupling_get_region_modes(int region_index, + int *load_mode, + int *distribution_mode, + int *contact_state_mode); SPHLITE_API int sphlite_coupling_export_particles(int region_index, int capacity, int *count, @@ -181,12 +199,48 @@ 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_facet_pressures(int region_index, + int load_count, + const int *facet_ids, + const double *pressures, + int distribution_mode, + int coupling_step); +SPHLITE_API int sphlite_coupling_apply_facet_tractions(int region_index, + int load_count, + const int *facet_ids, + const double *tractions, + int distribution_mode, + int coupling_step); +SPHLITE_API int sphlite_coupling_apply_facet_resultants(int region_index, + int load_count, + const int *facet_ids, + const double *resultants, + int distribution_mode, + int coupling_step); +SPHLITE_API int sphlite_coupling_apply_contact_state(int region_index, + int contact_count, + const int *facet_ids, + const double *normals, + const double *gaps, + const double *penetrations, + const double *contact_pressures, + int coupling_step); 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_load_balance(double *input_force, + double *distributed_force, + double *force_error, + double *input_moment, + double *distributed_moment, + double *moment_error); +SPHLITE_API int sphlite_coupling_get_contact_diagnostics(int *counts, + int count_capacity, + double *values, + int value_capacity); SPHLITE_API int sphlite_coupling_get_diagnostics(double *values, int value_capacity, int *counts, diff --git a/src/app/sph_main.c b/src/app/sph_main.c index 5f48ca4..fb87ea4 100644 --- a/src/app/sph_main.c +++ b/src/app/sph_main.c @@ -467,6 +467,9 @@ int main(int argc, char **argv) int region_type = 0; int export_mode = 0; int import_mode = 0; + int load_mode = 0; + int distribution_mode = 0; + int contact_state_mode = 0; int enabled = 0; int region_particles = 0; int region_facets = 0; @@ -478,10 +481,20 @@ int main(int argc, char **argv) (void)sphlite_finalize(); return 1; } + ierr = sphlite_coupling_get_region_modes(i, &load_mode, + &distribution_mode, &contact_state_mode); + 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_load_mode: %d\n", i, load_mode); + printf("coupling_region_%d_distribution_mode: %d\n", i, distribution_mode); + printf("coupling_region_%d_contact_state_mode: %d\n", i, contact_state_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); @@ -494,8 +507,18 @@ int main(int argc, char **argv) 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_force_balance_error: %.16e\n", coupling_values[22]); + printf("coupling_moment_balance_error: %.16e\n", coupling_values[23]); + printf("coupling_max_pressure: %.16e\n", coupling_values[24]); + printf("coupling_max_traction: %.16e\n", coupling_values[25]); + printf("coupling_max_penetration: %.16e\n", coupling_values[26]); printf("coupling_nonfinite_imports: %d\n", coupling_counts[2]); printf("coupling_stale_loads: %d\n", coupling_counts[0]); + printf("coupling_repeated_imports: %d\n", coupling_counts[5]); + printf("coupling_invalid_weights: %d\n", coupling_counts[7]); + printf("coupling_active_contacts: %d\n", coupling_counts[8]); + printf("coupling_skipped_contacts: %d\n", coupling_counts[9]); + printf("coupling_nonfinite_contact_imports: %d\n", coupling_counts[10]); 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 86012e7..55465d6 100644 --- a/src/coupling_interface/sph_coupling_interface.f90 +++ b/src/coupling_interface/sph_coupling_interface.f90 @@ -17,6 +17,8 @@ module sph_coupling_interface 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_load_mode => coupling_region_load_mode, & + db_coupling_region_distribution_mode => coupling_region_distribution_mode, & db_coupling_region_contact_state_mode => coupling_region_contact_state_mode, & db_coupling_region_enabled => coupling_region_enabled, & db_coupling_region_particle_count => coupling_region_particle_count, & @@ -314,6 +316,28 @@ contains end if end subroutine sph_coupling_get_region_info + subroutine sph_coupling_get_region_modes(region_index, load_mode, & + distribution_mode, contact_state_mode, ierr) + integer, intent(in) :: region_index + integer, intent(out) :: load_mode + integer, intent(out) :: distribution_mode + integer, intent(out) :: contact_state_mode + integer, intent(out) :: ierr + + load_mode = 0 + distribution_mode = 0 + contact_state_mode = 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 + load_mode = db_coupling_region_load_mode(region_index) + distribution_mode = db_coupling_region_distribution_mode(region_index) + contact_state_mode = db_coupling_region_contact_state_mode(region_index) + end subroutine sph_coupling_get_region_modes + subroutine sph_coupling_get_region_particle_count(region_name, count, ierr) character(len=*), intent(in) :: region_name integer, intent(out) :: count diff --git a/src/interface/sph_api.f90 b/src/interface/sph_api.f90 index ac1db17..1916467 100644 --- a/src/interface/sph_api.f90 +++ b/src/interface/sph_api.f90 @@ -1179,6 +1179,20 @@ subroutine sphft_coupling_get_region_info(region_index, chars, capacity, & chars(n + 1) = 0 end subroutine sphft_coupling_get_region_info +subroutine sphft_coupling_get_region_modes(region_index, load_mode, & + distribution_mode, contact_state_mode, ierr) + use sph_coupling_interface, only: sph_coupling_get_region_modes + implicit none + integer, intent(in) :: region_index + integer, intent(out) :: load_mode + integer, intent(out) :: distribution_mode + integer, intent(out) :: contact_state_mode + integer, intent(out) :: ierr + + call sph_coupling_get_region_modes(region_index, load_mode, & + distribution_mode, contact_state_mode, ierr) +end subroutine sphft_coupling_get_region_modes + subroutine sphft_coupling_export_particles(region_index, capacity, count, & ext_ids, internal_indices, phases, statuses, materials, sections, & scalar_values, vector_values, ierr) @@ -1256,6 +1270,78 @@ subroutine sphft_coupling_apply_facet_loads(region_index, load_count, & loads, ierr) end subroutine sphft_coupling_apply_facet_loads +subroutine sphft_coupling_apply_facet_pressures(region_index, load_count, & + facet_ids, pressures, distribution_mode, coupling_step, ierr) + use sph_precision, only: rk + use sph_coupling_interface, only: sph_coupling_apply_facet_pressures + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: load_count + integer, intent(in) :: facet_ids(load_count) + real(rk), intent(in) :: pressures(load_count) + integer, intent(in) :: distribution_mode + integer, intent(in) :: coupling_step + integer, intent(out) :: ierr + + call sph_coupling_apply_facet_pressures(region_index, load_count, & + facet_ids, pressures, distribution_mode, coupling_step, ierr) +end subroutine sphft_coupling_apply_facet_pressures + +subroutine sphft_coupling_apply_facet_tractions(region_index, load_count, & + facet_ids, tractions, distribution_mode, coupling_step, ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_apply_facet_tractions + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: load_count + integer, intent(in) :: facet_ids(load_count) + real(rk), intent(in) :: tractions(SPH_NDIM, load_count) + integer, intent(in) :: distribution_mode + integer, intent(in) :: coupling_step + integer, intent(out) :: ierr + + call sph_coupling_apply_facet_tractions(region_index, load_count, & + facet_ids, tractions, distribution_mode, coupling_step, ierr) +end subroutine sphft_coupling_apply_facet_tractions + +subroutine sphft_coupling_apply_facet_resultants(region_index, load_count, & + facet_ids, resultants, distribution_mode, coupling_step, ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_apply_facet_resultants + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: load_count + integer, intent(in) :: facet_ids(load_count) + real(rk), intent(in) :: resultants(SPH_NDIM, load_count) + integer, intent(in) :: distribution_mode + integer, intent(in) :: coupling_step + integer, intent(out) :: ierr + + call sph_coupling_apply_facet_resultants(region_index, load_count, & + facet_ids, resultants, distribution_mode, coupling_step, ierr) +end subroutine sphft_coupling_apply_facet_resultants + +subroutine sphft_coupling_apply_contact_state(region_index, contact_count, & + facet_ids, normals, gaps, penetrations, contact_pressures, coupling_step, & + ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_apply_contact_state + implicit none + integer, intent(in) :: region_index + integer, intent(in) :: contact_count + integer, intent(in) :: facet_ids(contact_count) + real(rk), intent(in) :: normals(SPH_NDIM, contact_count) + real(rk), intent(in) :: gaps(contact_count) + real(rk), intent(in) :: penetrations(contact_count) + real(rk), intent(in) :: contact_pressures(contact_count) + integer, intent(in) :: coupling_step + integer, intent(out) :: ierr + + call sph_coupling_apply_contact_state(region_index, contact_count, & + facet_ids, normals, gaps, penetrations, contact_pressures, & + coupling_step, ierr) +end subroutine sphft_coupling_apply_contact_state + subroutine sphft_coupling_apply_motion_boundary(region_index, export_index, & target_kind, mode, target, ierr) use sph_precision, only: rk, SPH_NDIM @@ -1280,63 +1366,70 @@ subroutine sphft_coupling_clear_imports(ierr) call sph_coupling_clear_imports(ierr) end subroutine sphft_coupling_clear_imports +subroutine sphft_coupling_get_load_balance(input_force, distributed_force, & + force_error, input_moment, distributed_moment, moment_error, ierr) + use sph_precision, only: rk, SPH_NDIM + use sph_coupling_interface, only: sph_coupling_get_load_balance + implicit none + real(rk), intent(out) :: input_force(SPH_NDIM) + real(rk), intent(out) :: distributed_force(SPH_NDIM) + real(rk), intent(out) :: force_error + real(rk), intent(out) :: input_moment(SPH_NDIM) + real(rk), intent(out) :: distributed_moment(SPH_NDIM) + real(rk), intent(out) :: moment_error + integer, intent(out) :: ierr + + call sph_coupling_get_load_balance(input_force, distributed_force, & + force_error, input_moment, distributed_moment, moment_error, ierr) +end subroutine sphft_coupling_get_load_balance + +subroutine sphft_coupling_get_contact_diagnostics(counts, values, ierr) + use sph_precision, only: rk + use sph_coupling_interface, only: sph_coupling_get_contact_diagnostics + implicit none + integer, intent(out) :: counts(7) + real(rk), intent(out) :: values(1) + integer, intent(out) :: ierr + integer :: active_count + integer :: skipped_count + integer :: stale_count + integer :: repeated_count + integer :: nonfinite_count + integer :: bad_area_count + integer :: invalid_weight_count + real(rk) :: max_penetration + + call sph_coupling_get_contact_diagnostics(active_count, skipped_count, & + max_penetration, stale_count, repeated_count, nonfinite_count, & + bad_area_count, invalid_weight_count, ierr) + if (ierr /= 0) return + counts(1) = active_count + counts(2) = skipped_count + counts(3) = stale_count + counts(4) = repeated_count + counts(5) = nonfinite_count + counts(6) = bad_area_count + counts(7) = invalid_weight_count + values(1) = max_penetration +end subroutine sphft_coupling_get_contact_diagnostics + subroutine sphft_coupling_get_diagnostics(value_capacity, values, & count_capacity, counts, ierr) - use sph_precision, only: rk, SPH_NDIM + use sph_precision, only: rk use sph_error, only: SPH_ERROR_ARGUMENT, sph_error_set - use sph_coupling_interface, only: sph_coupling_get_extended_diagnostics + use sph_coupling_interface, only: sph_coupling_get_diagnostic_values 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 + call sph_coupling_get_diagnostic_values(value_capacity, values, & + count_capacity, counts, ierr) end subroutine sphft_coupling_get_diagnostics diff --git a/src/interface/sph_c_api.c b/src/interface/sph_c_api.c index 4496758..66f1842 100644 --- a/src/interface/sph_c_api.c +++ b/src/interface/sph_c_api.c @@ -116,6 +116,12 @@ extern void SPHLITE_FC_sphft_coupling_get_region_info( int *particle_count, int *facet_count, int *ierr); +extern void SPHLITE_FC_sphft_coupling_get_region_modes( + const int *region_index, + int *load_mode, + int *distribution_mode, + int *contact_state_mode, + int *ierr); extern void SPHLITE_FC_sphft_coupling_export_particles( const int *region_index, const int *capacity, @@ -152,6 +158,40 @@ extern void SPHLITE_FC_sphft_coupling_apply_facet_loads( const int *facet_ids, const double *loads, int *ierr); +extern void SPHLITE_FC_sphft_coupling_apply_facet_pressures( + const int *region_index, + const int *load_count, + const int *facet_ids, + const double *pressures, + const int *distribution_mode, + const int *coupling_step, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_apply_facet_tractions( + const int *region_index, + const int *load_count, + const int *facet_ids, + const double *tractions, + const int *distribution_mode, + const int *coupling_step, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_apply_facet_resultants( + const int *region_index, + const int *load_count, + const int *facet_ids, + const double *resultants, + const int *distribution_mode, + const int *coupling_step, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_apply_contact_state( + const int *region_index, + const int *contact_count, + const int *facet_ids, + const double *normals, + const double *gaps, + const double *penetrations, + const double *contact_pressures, + const int *coupling_step, + int *ierr); extern void SPHLITE_FC_sphft_coupling_apply_motion_boundary( const int *region_index, const int *export_index, @@ -160,6 +200,18 @@ extern void SPHLITE_FC_sphft_coupling_apply_motion_boundary( const double *target, int *ierr); extern void SPHLITE_FC_sphft_coupling_clear_imports(int *ierr); +extern void SPHLITE_FC_sphft_coupling_get_load_balance( + double *input_force, + double *distributed_force, + double *force_error, + double *input_moment, + double *distributed_moment, + double *moment_error, + int *ierr); +extern void SPHLITE_FC_sphft_coupling_get_contact_diagnostics( + int *counts, + double *values, + int *ierr); extern void SPHLITE_FC_sphft_coupling_get_diagnostics( const int *value_capacity, double *values, @@ -587,6 +639,22 @@ int sphlite_coupling_get_region_info(int region_index, return ierr; } +int sphlite_coupling_get_region_modes(int region_index, + int *load_mode, + int *distribution_mode, + int *contact_state_mode) +{ + int ierr = 0; + + if (region_index <= 0 || load_mode == NULL || + distribution_mode == NULL || contact_state_mode == NULL) { + return -1; + } + SPHLITE_FC_sphft_coupling_get_region_modes(®ion_index, load_mode, + distribution_mode, contact_state_mode, &ierr); + return ierr; +} + int sphlite_coupling_export_particles(int region_index, int capacity, int *count, @@ -674,6 +742,100 @@ int sphlite_coupling_apply_facet_loads(int region_index, return ierr; } +int sphlite_coupling_apply_facet_pressures(int region_index, + int load_count, + const int *facet_ids, + const double *pressures, + int distribution_mode, + int coupling_step) +{ + int ierr = 0; + + if (region_index <= 0 || load_count < 0 || coupling_step < 0) { + return -1; + } + if (load_count > 0 && (facet_ids == NULL || pressures == NULL || + !sphlite_values_are_finite(pressures, load_count))) { + return -1; + } + SPHLITE_FC_sphft_coupling_apply_facet_pressures(®ion_index, + &load_count, facet_ids, pressures, &distribution_mode, &coupling_step, + &ierr); + return ierr; +} + +int sphlite_coupling_apply_facet_tractions(int region_index, + int load_count, + const int *facet_ids, + const double *tractions, + int distribution_mode, + int coupling_step) +{ + int ierr = 0; + + if (region_index <= 0 || load_count < 0 || coupling_step < 0) { + return -1; + } + if (load_count > 0 && (facet_ids == NULL || tractions == NULL || + !sphlite_values_are_finite(tractions, 3 * load_count))) { + return -1; + } + SPHLITE_FC_sphft_coupling_apply_facet_tractions(®ion_index, + &load_count, facet_ids, tractions, &distribution_mode, &coupling_step, + &ierr); + return ierr; +} + +int sphlite_coupling_apply_facet_resultants(int region_index, + int load_count, + const int *facet_ids, + const double *resultants, + int distribution_mode, + int coupling_step) +{ + int ierr = 0; + + if (region_index <= 0 || load_count < 0 || coupling_step < 0) { + return -1; + } + if (load_count > 0 && (facet_ids == NULL || resultants == NULL || + !sphlite_values_are_finite(resultants, 3 * load_count))) { + return -1; + } + SPHLITE_FC_sphft_coupling_apply_facet_resultants(®ion_index, + &load_count, facet_ids, resultants, &distribution_mode, &coupling_step, + &ierr); + return ierr; +} + +int sphlite_coupling_apply_contact_state(int region_index, + int contact_count, + const int *facet_ids, + const double *normals, + const double *gaps, + const double *penetrations, + const double *contact_pressures, + int coupling_step) +{ + int ierr = 0; + + if (region_index <= 0 || contact_count < 0 || coupling_step < 0) { + return -1; + } + if (contact_count > 0 && (facet_ids == NULL || normals == NULL || + gaps == NULL || penetrations == NULL || contact_pressures == NULL || + !sphlite_values_are_finite(normals, 3 * contact_count) || + !sphlite_values_are_finite(gaps, contact_count) || + !sphlite_values_are_finite(penetrations, contact_count) || + !sphlite_values_are_finite(contact_pressures, contact_count))) { + return -1; + } + SPHLITE_FC_sphft_coupling_apply_contact_state(®ion_index, + &contact_count, facet_ids, normals, gaps, penetrations, + contact_pressures, &coupling_step, &ierr); + return ierr; +} + int sphlite_coupling_apply_motion_boundary(int region_index, int export_index, int target_kind, @@ -699,6 +861,41 @@ int sphlite_coupling_clear_imports(void) return ierr; } +int sphlite_coupling_get_load_balance(double *input_force, + double *distributed_force, + double *force_error, + double *input_moment, + double *distributed_moment, + double *moment_error) +{ + int ierr = 0; + + if (input_force == NULL || distributed_force == NULL || + force_error == NULL || input_moment == NULL || + distributed_moment == NULL || moment_error == NULL) { + return -1; + } + SPHLITE_FC_sphft_coupling_get_load_balance(input_force, distributed_force, + force_error, input_moment, distributed_moment, moment_error, &ierr); + return ierr; +} + +int sphlite_coupling_get_contact_diagnostics(int *counts, + int count_capacity, + double *values, + int value_capacity) +{ + int ierr = 0; + + if (counts == NULL || values == NULL || + count_capacity < SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT || + value_capacity < SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT) { + return -1; + } + SPHLITE_FC_sphft_coupling_get_contact_diagnostics(counts, values, &ierr); + return ierr; +} + int sphlite_coupling_get_diagnostics(double *values, int value_capacity, int *counts, diff --git a/src/output/sph_output_csv.f90 b/src/output/sph_output_csv.f90 index 23d2aee..5692f51 100644 --- a/src/output/sph_output_csv.f90 +++ b/src/output/sph_output_csv.f90 @@ -49,7 +49,11 @@ module sph_output_csv 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 + coupling_nonfinite_import_count, coupling_stale_load_count, & + coupling_contact_flag, coupling_contact_gap, coupling_contact_penetration, & + coupling_active_contact_count, coupling_skipped_contact_count, & + coupling_max_pressure, coupling_max_traction, coupling_max_penetration, & + coupling_facet_force_error, coupling_facet_moment_error 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 @@ -107,11 +111,14 @@ contains real(rk) :: blast_shell_force(3) real(rk) :: blast_shell_moment(3) real(rk) :: coupling_force(3) + real(rk) :: coupling_gap_value + real(rk) :: coupling_penetration_value integer :: solid_reference_neighbors integer :: solid_correction_state integer :: solid_failure integer :: coupling_motion_value integer :: coupling_region_value + integer :: coupling_contact_value ierr = SPH_SUCCESS open(unit=SPH_OUTPUT_UNIT, file=trim(filename), status='unknown', & @@ -133,6 +140,7 @@ contains '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,' // & + 'coupling_gap,coupling_penetration,coupling_contact_flag,' // & 'solid_failure_status,solid_reference_neighbor_count,' // & 'solid_correction_status,coupling_motion_flag,coupling_region_id' do i = 1, npart @@ -152,11 +160,14 @@ contains blast_shell_force = 0.0_rk blast_shell_moment = 0.0_rk coupling_force = 0.0_rk + coupling_gap_value = 0.0_rk + coupling_penetration_value = 0.0_rk solid_reference_neighbors = 0 solid_correction_state = 0 solid_failure = 0 coupling_motion_value = 0 coupling_region_value = 0 + coupling_contact_value = 0 if (allocated(explosive_detonation_time)) explosive_td = explosive_detonation_time(i) if (allocated(explosive_reaction_progress)) explosive_lambda = & explosive_reaction_progress(i) @@ -193,8 +204,17 @@ contains if (allocated(coupling_motion_flag)) then coupling_motion_value = coupling_motion_flag(i) end if + if (allocated(coupling_contact_flag)) then + coupling_contact_value = coupling_contact_flag(i) + end if + if (allocated(coupling_contact_gap)) then + coupling_gap_value = coupling_contact_gap(i) + end if + if (allocated(coupling_contact_penetration)) then + coupling_penetration_value = coupling_contact_penetration(i) + end if coupling_region_value = sph_output_coupling_region_id(i) - write(SPH_OUTPUT_UNIT, '(5(I0,","),39(ES24.16,","),4(I0,","),I0)') & + write(SPH_OUTPUT_UNIT, '(5(I0,","),41(ES24.16,","),5(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), & @@ -203,7 +223,8 @@ 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, coupling_force, solid_failure, & + blast_shell_moment, coupling_force, coupling_gap_value, & + coupling_penetration_value, coupling_contact_value, solid_failure, & solid_reference_neighbors, solid_correction_state, & coupling_motion_value, coupling_region_value end do @@ -519,6 +540,20 @@ contains coupling_nonfinite_import_count write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'coupling_stale_loads', & coupling_stale_load_count + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'coupling_active_contacts', & + coupling_active_contact_count + write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'coupling_skipped_contacts', & + coupling_skipped_contact_count + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_max_pressure', & + coupling_max_pressure + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_max_traction', & + coupling_max_traction + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_max_penetration', & + coupling_max_penetration + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_force_balance_error', & + coupling_facet_force_error + write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'coupling_moment_balance_error', & + coupling_facet_moment_error end subroutine sph_output_write_coupling_summary subroutine sph_output_coupling_counts(particle_total, facet_total) diff --git a/src/output/sph_restart.f90 b/src/output/sph_restart.f90 index fe10fb2..6cdd2d7 100644 --- a/src/output/sph_restart.f90 +++ b/src/output/sph_restart.f90 @@ -24,10 +24,15 @@ module sph_restart 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_load_mode, coupling_region_distribution_mode, & + coupling_region_contact_state_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 + coupling_region_particles, coupling_region_disabled_count, & + SPH_COUPLING_IMPORT_FACET_LOAD, SPH_COUPLING_LOAD_PARTICLE_FORCE, & + SPH_COUPLING_LOAD_FACET_FORCE, SPH_COUPLING_DISTRIBUTION_UNIFORM, & + SPH_COUPLING_CONTACT_STATE_NONE 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 @@ -74,10 +79,20 @@ module sph_restart sph_coupling_allocate_imports, coupling_external_force, & coupling_motion_velocity, coupling_motion_position, & coupling_motion_flag, coupling_motion_mode, & + coupling_contact_flag, coupling_contact_normal, coupling_contact_gap, & + coupling_contact_penetration, coupling_contact_pressure, & 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 + coupling_nonfinite_import_count, coupling_imported_force_count, & + coupling_repeated_import_count, coupling_duplicate_facet_count, & + coupling_invalid_weight_count, coupling_active_contact_count, & + coupling_skipped_contact_count, coupling_nonfinite_contact_count, & + coupling_bad_facet_area_count, coupling_epoch, & + coupling_facet_input_force, coupling_facet_distributed_force, & + coupling_facet_force_error, coupling_facet_input_moment, & + coupling_facet_distributed_moment, coupling_facet_moment_error, & + coupling_max_pressure, coupling_max_traction, coupling_max_penetration use sph_neighbor, only: neighbor_cell_size, neighbor_cell_dims, neighbor_num_cells, & sph_neighbor_rebuild implicit none @@ -105,7 +120,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_8' + SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH, 'STATE_0_8_1' 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) @@ -286,7 +301,8 @@ contains trim(state_version) == 'STATE_0_4_1' .or. & trim(state_version) == 'STATE_0_5' .or. & trim(state_version) == 'STATE_0_5_1' .or. & - trim(state_version) == 'STATE_0_8') then + trim(state_version) == 'STATE_0_8' .or. & + trim(state_version) == 'STATE_0_8_1') 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 @@ -348,7 +364,8 @@ contains trim(state_version) == 'STATE_0_4_1' .or. & trim(state_version) == 'STATE_0_5' .or. & trim(state_version) == 'STATE_0_5_1' .or. & - trim(state_version) == 'STATE_0_8') then + trim(state_version) == 'STATE_0_8' .or. & + trim(state_version) == 'STATE_0_8_1') then has_gas_table = .true. has_gas_sources = .true. call sph_restart_read_gas_material_table(ios) @@ -363,10 +380,12 @@ contains trim(state_version) == 'STATE_0_4_1' .or. & trim(state_version) == 'STATE_0_5' .or. & trim(state_version) == 'STATE_0_5_1' .or. & - trim(state_version) == 'STATE_0_8') has_explosive_state = .true. + trim(state_version) == 'STATE_0_8' .or. & + trim(state_version) == 'STATE_0_8_1') has_explosive_state = .true. if (trim(state_version) == 'STATE_0_5' .or. & trim(state_version) == 'STATE_0_5_1' .or. & - trim(state_version) == 'STATE_0_8') has_solid_state = .true. + trim(state_version) == 'STATE_0_8' .or. & + trim(state_version) == 'STATE_0_8_1') 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 @@ -417,8 +436,10 @@ 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 (ierr == SPH_SUCCESS .and. & + (trim(state_version) == 'STATE_0_8' .or. & + trim(state_version) == 'STATE_0_8_1')) then + call sph_restart_read_coupling_state(state_version, ios) if (ios /= 0) then call sph_error_set(SPH_ERROR_INPUT, 'invalid restart Coupling state') ierr = SPH_ERROR_INPUT @@ -527,7 +548,8 @@ contains 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' .or. & - trim(state_version) == 'STATE_0_8' + trim(state_version) == 'STATE_0_8' .or. & + trim(state_version) == 'STATE_0_8_1' call sph_explosive_clear_material_table() read(SPH_RESTART_UNIT, *, iostat=ios) table_count if (ios /= 0) then @@ -679,6 +701,9 @@ contains 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_load_mode(region), & + coupling_region_distribution_mode(region), & + coupling_region_contact_state_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) @@ -693,15 +718,30 @@ contains write(SPH_RESTART_UNIT, '(I0,1X,I0,1X,I0,1X,I0)') & coupling_stale_load_count, coupling_skipped_particle_count, & coupling_nonfinite_import_count, coupling_imported_force_count + write(SPH_RESTART_UNIT, '(8(I0,1X),I0)') coupling_epoch, & + coupling_repeated_import_count, coupling_duplicate_facet_count, & + coupling_invalid_weight_count, coupling_active_contact_count, & + coupling_skipped_contact_count, coupling_nonfinite_contact_count, & + coupling_bad_facet_area_count, 0 + write(SPH_RESTART_UNIT, *) coupling_facet_input_force(:), & + coupling_facet_distributed_force(:), coupling_facet_force_error + write(SPH_RESTART_UNIT, *) coupling_facet_input_moment(:), & + coupling_facet_distributed_moment(:), coupling_facet_moment_error + write(SPH_RESTART_UNIT, *) coupling_max_pressure, coupling_max_traction, & + coupling_max_penetration write(SPH_RESTART_UNIT, '(I0)') 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) + coupling_contact_normal(:,i), coupling_contact_gap(i), & + coupling_contact_penetration(i), coupling_contact_pressure(i), & + coupling_motion_flag(i), coupling_motion_mode(i), & + coupling_contact_flag(i) end do end subroutine sph_restart_write_coupling_state - subroutine sph_restart_read_coupling_state(ios) + subroutine sph_restart_read_coupling_state(state_version, ios) + character(len=*), intent(in) :: state_version integer, intent(out) :: ios character(len=64) :: marker integer :: region @@ -710,8 +750,10 @@ contains integer :: local_ierr integer :: imported_force_count character(len=256) :: counter_line + logical :: has_081_state ios = 0 + has_081_state = trim(state_version) == 'STATE_0_8_1' call sph_restart_clear_coupling_state(local_ierr) if (local_ierr /= SPH_SUCCESS) then ios = 1 @@ -741,11 +783,25 @@ contains 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 (has_081_state) then + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_region_type(region), & + coupling_region_export_mode(region), coupling_region_import_mode(region), & + coupling_region_load_mode(region), & + coupling_region_distribution_mode(region), & + coupling_region_contact_state_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) + else + 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) + call sph_restart_set_legacy_coupling_modes(region) + end if if (ios /= 0) return if (coupling_region_particle_count(region) < 0 .or. & coupling_region_particle_count(region) > SPH_MAX_SET_MEMBERS) then @@ -758,6 +814,11 @@ contains if (ios /= 0) return end if end do + call sph_coupling_allocate_imports(local_ierr) + if (local_ierr /= SPH_SUCCESS) then + ios = 1 + return + end if read(SPH_RESTART_UNIT, *, iostat=ios) coupling_cumulative_impulse(:), & coupling_momentum_exchange(:) if (ios /= 0) return @@ -776,26 +837,58 @@ contains if (ios /= 0) return imported_force_count = 0 end if + if (has_081_state) then + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_epoch, & + coupling_repeated_import_count, coupling_duplicate_facet_count, & + coupling_invalid_weight_count, coupling_active_contact_count, & + coupling_skipped_contact_count, coupling_nonfinite_contact_count, & + coupling_bad_facet_area_count, i + if (ios /= 0) return + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_facet_input_force(:), & + coupling_facet_distributed_force(:), coupling_facet_force_error + if (ios /= 0) return + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_facet_input_moment(:), & + coupling_facet_distributed_moment(:), coupling_facet_moment_error + if (ios /= 0) return + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_max_pressure, & + coupling_max_traction, coupling_max_penetration + if (ios /= 0) return + end if 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 (has_081_state) then + read(SPH_RESTART_UNIT, *, iostat=ios) coupling_external_force(:,i), & + coupling_motion_velocity(:,i), coupling_motion_position(:,i), & + coupling_contact_normal(:,i), coupling_contact_gap(i), & + coupling_contact_penetration(i), coupling_contact_pressure(i), & + coupling_motion_flag(i), coupling_motion_mode(i), & + coupling_contact_flag(i) + else + 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) + end if if (ios /= 0) return end do end subroutine sph_restart_read_coupling_state + subroutine sph_restart_set_legacy_coupling_modes(region) + integer, intent(in) :: region + + coupling_region_load_mode(region) = SPH_COUPLING_LOAD_PARTICLE_FORCE + if (coupling_region_import_mode(region) == SPH_COUPLING_IMPORT_FACET_LOAD) then + coupling_region_load_mode(region) = SPH_COUPLING_LOAD_FACET_FORCE + end if + coupling_region_distribution_mode(region) = SPH_COUPLING_DISTRIBUTION_UNIFORM + coupling_region_contact_state_mode(region) = SPH_COUPLING_CONTACT_STATE_NONE + end subroutine sph_restart_set_legacy_coupling_modes + subroutine sph_restart_clear_coupling_state(ierr) integer, intent(out) :: ierr @@ -809,6 +902,9 @@ contains coupling_region_type = 0 coupling_region_export_mode = 0 coupling_region_import_mode = 0 + coupling_region_load_mode = 0 + coupling_region_distribution_mode = 0 + coupling_region_contact_state_mode = 0 coupling_region_enabled = 0 coupling_region_phase_filter = 0 coupling_region_material_filter = 0 diff --git a/src/solid/sph_solid_restart.f90 b/src/solid/sph_solid_restart.f90 index 845dd91..10f9c4c 100644 --- a/src/solid/sph_solid_restart.f90 +++ b/src/solid/sph_solid_restart.f90 @@ -96,7 +96,8 @@ contains ios = 0 has_051_state = trim(state_version) == 'STATE_0_5_1' .or. & - trim(state_version) == 'STATE_0_8' + trim(state_version) == 'STATE_0_8' .or. & + trim(state_version) == 'STATE_0_8_1' 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_081.c b/tests/interface/test_coupling_c_api_081.c new file mode 100644 index 0000000..7ac2f4b --- /dev/null +++ b/tests/interface/test_coupling_c_api_081.c @@ -0,0 +1,144 @@ +#include "sphlite_c_api.h" + +#include +#include + +static int fail(const char *message) +{ + fprintf(stderr, "%s\n", message); + return 1; +} + +static int check_close(double actual, double expected, const char *message) +{ + if (fabs(actual - expected) > fmax(1.0e-10, fabs(expected) * 1.0e-8)) { + return fail(message); + } + return 0; +} + +int main(void) +{ + char path[1024]; + int ierr; + int load_mode = 0; + int distribution_mode = 0; + int contact_state_mode = 0; + int ids[1] = {1}; + double pressures[1] = {13.0}; + double vectors[3] = {1.0, 2.0, 3.0}; + double normals[3] = {0.0, 0.0, 1.0}; + double gaps[1] = {2.0e-4}; + double penetrations[1] = {5.0e-3}; + double contact_pressures[1] = {8.0}; + double input_force[3] = {0.0, 0.0, 0.0}; + double distributed_force[3] = {0.0, 0.0, 0.0}; + double input_moment[3] = {0.0, 0.0, 0.0}; + double distributed_moment[3] = {0.0, 0.0, 0.0}; + double force_error = 0.0; + double moment_error = 0.0; + double diagnostics[SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT] = {0.0}; + double contact_values[SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT] = {0.0}; + int counts[SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT] = {0}; + int contact_counts[SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT] = {0}; + + if (sphlite_coupling_get_region_modes(1, NULL, &distribution_mode, + &contact_state_mode) != -1) { + return fail("C API 081 null mode pointer did not fail"); + } + if (sphlite_coupling_get_load_balance(NULL, distributed_force, &force_error, + input_moment, distributed_moment, &moment_error) != -1) { + return fail("C API 081 null load balance pointer did not fail"); + } + if (sphlite_coupling_get_contact_diagnostics(contact_counts, 1, + contact_values, SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT) != -1) { + return fail("C API 081 small contact diagnostics buffer did not fail"); + } + pressures[0] = HUGE_VAL; + if (sphlite_coupling_apply_facet_pressures(1, 1, ids, pressures, + SPHLITE_COUPLING_DISTRIBUTION_AREA, 1) != -1) { + return fail("C API 081 nonfinite pressure did not fail"); + } + pressures[0] = 13.0; + + ierr = sphlite_initialize(); + if (ierr != 0) return fail("C API 081 initialize failed"); + snprintf(path, sizeof(path), "%s/coupling_load_mode_081.inp", + SPHLITE_TEST_DATA_DIR); + ierr = sphlite_read_inp(path); + if (ierr != 0) return fail("C API 081 read input failed"); + ierr = sphlite_build_model(); + if (ierr != 0) return fail("C API 081 build model failed"); + + ierr = sphlite_coupling_get_region_modes(1, &load_mode, &distribution_mode, + &contact_state_mode); + if (ierr != 0) return fail("C API 081 region mode query failed"); + if (load_mode != SPHLITE_COUPLING_LOAD_PRESSURE || + distribution_mode != SPHLITE_COUPLING_DISTRIBUTION_AREA || + contact_state_mode != SPHLITE_COUPLING_CONTACT_STATE_IMPORT) { + return fail("C API 081 region mode mismatch"); + } + ierr = sphlite_coupling_get_region_modes(99, &load_mode, &distribution_mode, + &contact_state_mode); + if (ierr == 0) return fail("C API 081 invalid region did not fail"); + + ierr = sphlite_coupling_apply_facet_resultants(1, 1, ids, vectors, + SPHLITE_COUPLING_DISTRIBUTION_UNIFORM, 101); + if (ierr != 0) return fail("C API 081 resultant import failed"); + ierr = sphlite_coupling_get_load_balance(input_force, distributed_force, + &force_error, input_moment, distributed_moment, &moment_error); + if (ierr != 0) return fail("C API 081 load balance failed"); + if (check_close(force_error, 0.0, "C API 081 force error mismatch") != 0) { + return 1; + } + vectors[0] = 0.0; + vectors[1] = 4.0; + vectors[2] = 0.0; + ierr = sphlite_coupling_apply_facet_pressures(1, 1, ids, pressures, + SPHLITE_COUPLING_DISTRIBUTION_AREA, 102); + if (ierr != 0) return fail("C API 081 pressure import failed"); + ierr = sphlite_coupling_apply_facet_tractions(2, 1, ids, vectors, + SPHLITE_COUPLING_DISTRIBUTION_MASS, 102); + if (ierr != 0) return fail("C API 081 traction import failed"); + ierr = sphlite_coupling_apply_contact_state(1, 1, ids, normals, gaps, + penetrations, contact_pressures, 102); + if (ierr != 0) return fail("C API 081 contact import failed"); + + ierr = sphlite_coupling_get_contact_diagnostics(contact_counts, + SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_COUNT_COUNT, contact_values, + SPHLITE_COUPLING_CONTACT_DIAGNOSTIC_VALUE_COUNT); + if (ierr != 0) return fail("C API 081 contact diagnostics failed"); + if (contact_counts[0] != 1 || contact_counts[1] != 0) { + return fail("C API 081 contact count mismatch"); + } + if (check_close(contact_values[0], 5.0e-3, + "C API 081 contact penetration mismatch") != 0) { + return 1; + } + ierr = sphlite_coupling_get_diagnostics(diagnostics, + SPHLITE_COUPLING_DIAGNOSTIC_VALUE_COUNT, counts, + SPHLITE_COUPLING_DIAGNOSTIC_COUNT_COUNT); + if (ierr != 0) return fail("C API 081 diagnostics failed"); + if (counts[8] != 1) return fail("C API 081 diagnostics contact mismatch"); + if (check_close(diagnostics[24], 13.0, + "C API 081 diagnostics pressure mismatch") != 0) { + return 1; + } + if (check_close(diagnostics[25], 4.0, + "C API 081 diagnostics traction mismatch") != 0) { + return 1; + } + if (sphlite_coupling_apply_facet_tractions(2, 1, NULL, vectors, + SPHLITE_COUPLING_DISTRIBUTION_MASS, 103) != -1) { + return fail("C API 081 null traction id buffer did not fail"); + } + vectors[0] = HUGE_VAL; + if (sphlite_coupling_apply_facet_resultants(1, 1, ids, vectors, + SPHLITE_COUPLING_DISTRIBUTION_UNIFORM, 103) != -1) { + return fail("C API 081 nonfinite resultant did not fail"); + } + + ierr = sphlite_finalize(); + if (ierr != 0) return fail("C API 081 finalize failed"); + return 0; +} diff --git a/tests/interface/test_coupling_cpp_api_081.cpp b/tests/interface/test_coupling_cpp_api_081.cpp new file mode 100644 index 0000000..91eab38 --- /dev/null +++ b/tests/interface/test_coupling_cpp_api_081.cpp @@ -0,0 +1,98 @@ +#include "sphlite.hpp" + +#include +#include +#include + +static bool close_to(double actual, double expected) +{ + return std::fabs(actual - expected) <= + std::max(1.0e-10, std::fabs(expected) * 1.0e-8); +} + +int main() +{ + sphlite::Session session; + std::string input = + std::string(SPHLITE_TEST_DATA_DIR) + "/coupling_load_mode_081.inp"; + sphlite::read_inp(input); + sphlite::build_model(); + + sphlite::CouplingRegionInfo pressure_region = sphlite::coupling_region_info(1); + if (pressure_region.load_mode != SPHLITE_COUPLING_LOAD_PRESSURE || + pressure_region.distribution_mode != SPHLITE_COUPLING_DISTRIBUTION_AREA || + pressure_region.contact_state_mode != SPHLITE_COUPLING_CONTACT_STATE_IMPORT) { + return 1; + } + sphlite::CouplingRegionInfo traction_region = sphlite::coupling_region_info(2); + if (traction_region.load_mode != SPHLITE_COUPLING_LOAD_TRACTION || + traction_region.distribution_mode != SPHLITE_COUPLING_DISTRIBUTION_MASS || + traction_region.contact_state_mode != SPHLITE_COUPLING_CONTACT_STATE_IMPORT) { + return 1; + } + try { + (void)sphlite::coupling_region_info(99); + return 1; + } catch (const sphlite::Error &) { + } + + std::vector facet_ids(1, 1); + std::vector resultants; + resultants.push_back(1.0); + resultants.push_back(2.0); + resultants.push_back(3.0); + sphlite::coupling_apply_facet_resultants(1, facet_ids, resultants, + SPHLITE_COUPLING_DISTRIBUTION_UNIFORM, 101); + sphlite::CouplingLoadBalance balance = sphlite::coupling_load_balance(); + if (!close_to(balance.force_error, 0.0)) { + return 1; + } + + std::vector pressures(1, 13.0); + sphlite::coupling_apply_facet_pressures(1, facet_ids, pressures, + SPHLITE_COUPLING_DISTRIBUTION_AREA, 102); + std::vector tractions; + tractions.push_back(0.0); + tractions.push_back(4.0); + tractions.push_back(0.0); + sphlite::coupling_apply_facet_tractions(2, facet_ids, tractions, + SPHLITE_COUPLING_DISTRIBUTION_MASS, 102); + std::vector normals; + normals.push_back(0.0); + normals.push_back(0.0); + normals.push_back(1.0); + std::vector gaps(1, 2.0e-4); + std::vector penetrations(1, 5.0e-3); + std::vector contact_pressures(1, 8.0); + sphlite::coupling_apply_contact_state(1, facet_ids, normals, gaps, + penetrations, contact_pressures, 102); + sphlite::CouplingContactDiagnostics contact = + sphlite::coupling_contact_diagnostics(); + if (contact.counts[0] != 1 || contact.counts[1] != 0 || + !close_to(contact.values[0], 5.0e-3)) { + return 1; + } + sphlite::CouplingDiagnostics diagnostics = sphlite::coupling_diagnostics(); + if (diagnostics.counts[8] != 1 || !close_to(diagnostics.values[24], 13.0) || + !close_to(diagnostics.values[25], 4.0) || + !close_to(diagnostics.values[26], 5.0e-3)) { + return 1; + } + + try { + std::vector bad_pressures(2, 1.0); + sphlite::coupling_apply_facet_pressures(1, facet_ids, bad_pressures, + SPHLITE_COUPLING_DISTRIBUTION_AREA, 103); + return 1; + } catch (const sphlite::Error &) { + } + try { + std::vector bad_contacts(2, 0.0); + sphlite::coupling_apply_contact_state(1, facet_ids, bad_contacts, gaps, + penetrations, contact_pressures, 103); + return 1; + } catch (const sphlite::Error &) { + } + session.close(); + return 0; +} diff --git a/tests/module/test_coupling_fortran_api_081.f90 b/tests/module/test_coupling_fortran_api_081.f90 new file mode 100644 index 0000000..3f75df9 --- /dev/null +++ b/tests/module/test_coupling_fortran_api_081.f90 @@ -0,0 +1,169 @@ +program test_coupling_fortran_api_081 + 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_database, only: SPH_COUPLING_LOAD_PRESSURE, & + SPH_COUPLING_LOAD_TRACTION, SPH_COUPLING_DISTRIBUTION_AREA, & + SPH_COUPLING_DISTRIBUTION_MASS, SPH_COUPLING_DISTRIBUTION_UNIFORM, & + SPH_COUPLING_CONTACT_STATE_IMPORT + use sph_coupling_interface, only: sph_coupling_get_region_modes, & + sph_coupling_apply_facet_pressures, sph_coupling_apply_facet_tractions, & + sph_coupling_apply_facet_resultants, sph_coupling_apply_contact_state, & + sph_coupling_get_load_balance, sph_coupling_get_contact_diagnostics, & + sph_coupling_get_diagnostics + implicit none + + integer :: ierr + + call check_fortran_api_081() + +contains + + subroutine check_fortran_api_081() + integer :: ids(1) + real(rk) :: pressures(1) + real(rk) :: vectors(SPH_NDIM,1) + real(rk) :: normals(SPH_NDIM,1) + real(rk) :: gaps(1) + real(rk) :: penetrations(1) + real(rk) :: contact_pressures(1) + real(rk) :: input_force(SPH_NDIM) + real(rk) :: distributed_force(SPH_NDIM) + real(rk) :: input_moment(SPH_NDIM) + real(rk) :: distributed_moment(SPH_NDIM) + real(rk) :: force_error + real(rk) :: moment_error + real(rk) :: diag_values(27) + integer :: diag_counts(12) + integer :: load_mode + integer :: distribution_mode + integer :: contact_state_mode + integer :: active_count + integer :: skipped_count + integer :: stale_count + integer :: repeated_count + integer :: nonfinite_count + integer :: bad_area_count + integer :: invalid_weight_count + real(rk) :: max_penetration + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Fortran API 081') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for Fortran API 081') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Fortran API 081') + call assert_region_modes() + + ids(1) = 1 + vectors(:,1) = (/1.0_rk, 2.0_rk, 3.0_rk/) + call sph_coupling_apply_facet_resultants(1, 1, ids, vectors, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, 101, ierr) + call assert_ok(ierr, 'Fortran API 081 resultant import failed') + call sph_coupling_get_load_balance(input_force, distributed_force, & + force_error, input_moment, distributed_moment, moment_error, ierr) + call assert_ok(ierr, 'Fortran API 081 load balance failed') + call assert_vector_close(input_force, distributed_force, & + 'Fortran API 081 force balance mismatch') + call assert_close(force_error, 0.0_rk, 'Fortran API 081 force error mismatch') + + pressures(1) = 13.0_rk + call sph_coupling_apply_facet_pressures(1, 1, ids, pressures, & + SPH_COUPLING_DISTRIBUTION_AREA, 102, ierr) + call assert_ok(ierr, 'Fortran API 081 pressure import failed') + vectors(:,1) = (/0.0_rk, 4.0_rk, 0.0_rk/) + call sph_coupling_apply_facet_tractions(2, 1, ids, vectors, & + SPH_COUPLING_DISTRIBUTION_MASS, 102, ierr) + call assert_ok(ierr, 'Fortran API 081 traction import failed') + normals(:,1) = (/0.0_rk, 0.0_rk, 1.0_rk/) + gaps(1) = 2.0e-4_rk + penetrations(1) = 5.0e-3_rk + contact_pressures(1) = 8.0_rk + call sph_coupling_apply_contact_state(1, 1, ids, normals, gaps, & + penetrations, contact_pressures, 102, ierr) + call assert_ok(ierr, 'Fortran API 081 contact import failed') + call sph_coupling_get_contact_diagnostics(active_count, skipped_count, & + max_penetration, stale_count, repeated_count, nonfinite_count, & + bad_area_count, invalid_weight_count, ierr) + call assert_ok(ierr, 'Fortran API 081 contact diagnostics failed') + if (active_count /= 1 .or. skipped_count /= 0) then + call fail('Fortran API 081 contact count mismatch') + end if + call assert_close(max_penetration, penetrations(1), & + 'Fortran API 081 contact penetration mismatch') + call sph_coupling_get_diagnostics(27, diag_values, 12, diag_counts, ierr) + call assert_ok(ierr, 'Fortran API 081 extended diagnostics failed') + call assert_close(diag_values(25), pressures(1), & + 'Fortran API 081 max pressure mismatch') + call assert_close(diag_values(26), 4.0_rk, & + 'Fortran API 081 max traction mismatch') + call assert_close(diag_values(27), penetrations(1), & + 'Fortran API 081 max penetration mismatch') + if (diag_counts(9) /= 1) call fail('Fortran API 081 active contact mismatch') + call sph_coupling_get_region_modes(99, load_mode, distribution_mode, & + contact_state_mode, ierr) + if (ierr == SPH_SUCCESS) call fail('Fortran API 081 invalid region did not fail') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Fortran API 081') + end subroutine check_fortran_api_081 + + subroutine assert_region_modes() + integer :: load_mode + integer :: distribution_mode + integer :: contact_state_mode + + call sph_coupling_get_region_modes(1, load_mode, distribution_mode, & + contact_state_mode, ierr) + call assert_ok(ierr, 'Fortran API 081 region 1 mode query failed') + if (load_mode /= SPH_COUPLING_LOAD_PRESSURE) call fail('region 1 load mismatch') + if (distribution_mode /= SPH_COUPLING_DISTRIBUTION_AREA) then + call fail('region 1 distribution mismatch') + end if + if (contact_state_mode /= SPH_COUPLING_CONTACT_STATE_IMPORT) then + call fail('region 1 contact state mismatch') + end if + call sph_coupling_get_region_modes(2, load_mode, distribution_mode, & + contact_state_mode, ierr) + call assert_ok(ierr, 'Fortran API 081 region 2 mode query failed') + if (load_mode /= SPH_COUPLING_LOAD_TRACTION) call fail('region 2 load mismatch') + if (distribution_mode /= SPH_COUPLING_DISTRIBUTION_MASS) then + call fail('region 2 distribution mismatch') + end if + end subroutine assert_region_modes + + 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 assert_close(actual, expected, message) + real(rk), intent(in) :: actual + real(rk), intent(in) :: expected + character(len=*), intent(in) :: message + + if (abs(actual - expected) > max(1.0e-10_rk, abs(expected) * 1.0e-8_rk)) then + call fail(message) + end if + end subroutine assert_close + + subroutine assert_vector_close(actual, expected, message) + real(rk), intent(in) :: actual(SPH_NDIM) + real(rk), intent(in) :: expected(SPH_NDIM) + character(len=*), intent(in) :: message + integer :: i + + do i = 1, SPH_NDIM + call assert_close(actual(i), expected(i), message) + end do + end subroutine assert_vector_close + + subroutine fail(message) + character(len=*), intent(in) :: message + + write(*, '(A)') trim(message) + stop 1 + end subroutine fail + +end program test_coupling_fortran_api_081 diff --git a/tests/module/test_coupling_output_080.f90 b/tests/module/test_coupling_output_080.f90 index ba91568..5309082 100644 --- a/tests/module/test_coupling_output_080.f90 +++ b/tests/module/test_coupling_output_080.f90 @@ -60,6 +60,9 @@ contains real(rk) :: solid_values(5) real(rk) :: blast_values(9) real(rk) :: coupling_force(3) + real(rk) :: coupling_gap + real(rk) :: coupling_penetration + integer :: contact_flag unit = 84 open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) @@ -72,10 +75,14 @@ contains if (index(header, 'coupling_motion_flag,coupling_region_id') <= 0) then call fail('coupling motion particle CSV columns missing') end if + if (index(header, 'coupling_gap,coupling_penetration,coupling_contact_flag') <= 0) then + call fail('coupling contact 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 + blast_values, coupling_force, coupling_gap, coupling_penetration, & + contact_flag, 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') @@ -83,6 +90,10 @@ contains 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') + call assert_close(coupling_gap, 0.0_rk, 'coupling output gap default mismatch') + call assert_close(coupling_penetration, 0.0_rk, & + 'coupling output penetration default mismatch') + if (contact_flag /= 0) call fail('coupling output contact flag default mismatch') end subroutine check_particle_csv subroutine check_summary_csv(filename) @@ -101,6 +112,13 @@ contains 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) + call assert_summary_int(filename, 'coupling_active_contacts', 0) + call assert_summary_int(filename, 'coupling_skipped_contacts', 0) + call assert_summary_present(filename, 'coupling_max_pressure') + call assert_summary_present(filename, 'coupling_max_traction') + call assert_summary_present(filename, 'coupling_max_penetration') + call assert_summary_present(filename, 'coupling_force_balance_error') + call assert_summary_present(filename, 'coupling_moment_balance_error') end subroutine check_summary_csv subroutine assert_summary_present(filename, metric) @@ -189,6 +207,14 @@ contains if (ierr_value /= SPH_SUCCESS) call fail(message) 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 diff --git a/tests/module/test_coupling_output_081.f90 b/tests/module/test_coupling_output_081.f90 new file mode 100644 index 0000000..8c59bc8 --- /dev/null +++ b/tests/module/test_coupling_output_081.f90 @@ -0,0 +1,234 @@ +program test_coupling_output_081 + 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_database, only: SPH_COUPLING_DISTRIBUTION_AREA + use sph_coupling_interface, only: sph_coupling_apply_facet_pressures, & + sph_coupling_apply_contact_state + implicit none + + integer :: ierr + + call check_coupling_output_081() + +contains + + subroutine check_coupling_output_081() + integer :: ids(1) + real(rk) :: pressures(1) + real(rk) :: normals(SPH_NDIM,1) + real(rk) :: gaps(1) + real(rk) :: penetrations(1) + real(rk) :: contact_pressures(1) + + call delete_output_pair(81) + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for coupling output 081') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for coupling output 081') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for coupling output 081') + + ids(1) = 1 + pressures(1) = 12.0_rk + normals(:,1) = (/0.0_rk, 0.0_rk, 1.0_rk/) + gaps(1) = 1.0e-4_rk + penetrations(1) = 2.0e-3_rk + contact_pressures(1) = 9.0_rk + call sph_coupling_apply_facet_pressures(1, 1, ids, pressures, & + SPH_COUPLING_DISTRIBUTION_AREA, 81, ierr) + call assert_ok(ierr, 'pressure import failed for output 081') + call sph_coupling_apply_contact_state(1, 1, ids, normals, gaps, & + penetrations, contact_pressures, 81, ierr) + call assert_ok(ierr, 'contact import failed for output 081') + + call sph_set_output_directory('build-codex-unix', ierr) + call assert_ok(ierr, 'output directory failed for output 081') + call sph_write_output(81, 0.0_rk, ierr) + call assert_ok(ierr, 'write failed for output 081') + call check_particle_csv('build-codex-unix/sphlite_00000081.csv') + call check_summary_csv('build-codex-unix/sphlite_00000081_summary.csv') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for coupling output 081') + end subroutine check_coupling_output_081 + + 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 :: trailing_values(5) + 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) + real(rk) :: coupling_gap + real(rk) :: coupling_penetration + integer :: contact_flag + + unit = 84 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('coupling output 081 CSV open failed') + read(unit, '(A)', iostat=ios) header + if (ios /= 0) call fail('coupling output 081 header read failed') + if (index(header, 'coupling_gap,coupling_penetration,coupling_contact_flag') <= 0) then + call fail('coupling output 081 contact 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, coupling_gap, coupling_penetration, contact_flag, & + trailing_values + close(unit=unit) + if (ios /= 0) call fail('coupling output 081 row read failed') + if (id /= 1) call fail('coupling output 081 id mismatch') + if (contact_flag /= 1) call fail('coupling output 081 contact flag mismatch') + call assert_close(coupling_gap, 1.0e-4_rk, 'coupling output 081 gap mismatch') + call assert_close(coupling_penetration, 2.0e-3_rk, & + 'coupling output 081 penetration mismatch') + end subroutine check_particle_csv + + subroutine check_summary_csv(filename) + character(len=*), intent(in) :: filename + + call assert_summary_int(filename, 'coupling_active_contacts', 1) + call assert_summary_int(filename, 'coupling_skipped_contacts', 0) + call assert_summary_real(filename, 'coupling_max_pressure', 12.0_rk) + call assert_summary_real(filename, 'coupling_max_penetration', 2.0e-3_rk) + call assert_summary_present(filename, 'coupling_force_balance_error') + call assert_summary_present(filename, 'coupling_moment_balance_error') + end subroutine check_summary_csv + + subroutine assert_summary_present(filename, metric) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + real(rk) :: value + + call read_summary_real(filename, metric, value) + 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 :: value + + call read_summary_int(filename, metric, value) + if (value /= expected) call fail('coupling output 081 summary int mismatch') + end subroutine assert_summary_int + + subroutine assert_summary_real(filename, metric, expected) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + real(rk), intent(in) :: expected + real(rk) :: value + + call read_summary_real(filename, metric, value) + call assert_close(value, expected, 'coupling output 081 summary real mismatch') + end subroutine assert_summary_real + + 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 + + unit = 85 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('coupling output 081 summary open failed') + do + read(unit, '(A)', iostat=ios) line + if (ios /= 0) exit + if (index(line, trim(metric) // ',') == 1) then + read(line(len_trim(metric)+2:), *, iostat=ios) value + close(unit=unit) + if (ios /= 0) call fail('coupling output 081 summary int parse failed') + return + end if + end do + close(unit=unit) + call fail('coupling output 081 summary int missing') + end subroutine read_summary_int + + subroutine read_summary_real(filename, metric, value) + character(len=*), intent(in) :: filename + character(len=*), intent(in) :: metric + real(rk), intent(out) :: value + character(len=256) :: line + integer :: unit + integer :: ios + + unit = 86 + open(unit=unit, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('coupling output 081 summary real open failed') + do + read(unit, '(A)', iostat=ios) line + if (ios /= 0) exit + if (index(line, trim(metric) // ',') == 1) then + read(line(len_trim(metric)+2:), *, iostat=ios) value + close(unit=unit) + if (ios /= 0) call fail('coupling output 081 summary real parse failed') + return + end if + end do + close(unit=unit) + call fail('coupling output 081 summary real missing') + end subroutine read_summary_real + + 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 assert_close(actual, expected, message) + real(rk), intent(in) :: actual + real(rk), intent(in) :: expected + character(len=*), intent(in) :: message + + if (abs(actual - expected) > max(1.0e-10_rk, abs(expected) * 1.0e-8_rk)) then + call fail(message) + end if + 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_output_081 diff --git a/tests/module/test_coupling_restart_080.f90 b/tests/module/test_coupling_restart_080.f90 index c76da45..90c4429 100644 --- a/tests/module/test_coupling_restart_080.f90 +++ b/tests/module/test_coupling_restart_080.f90 @@ -61,7 +61,7 @@ contains 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') + 'STATE_0_8_1') call scramble_coupling_state() call sph_read_restart('build-codex-unix/restart_coupling_080.tmp', ierr) diff --git a/tests/module/test_coupling_restart_080_compat.f90 b/tests/module/test_coupling_restart_080_compat.f90 new file mode 100644 index 0000000..3ab8ec7 --- /dev/null +++ b/tests/module/test_coupling_restart_080_compat.f90 @@ -0,0 +1,219 @@ +program test_coupling_restart_080_compat + 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_write_restart, sph_read_restart + use sph_database, only: npart, coupling_region_count, & + coupling_region_disabled_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, & + SPH_COUPLING_LOAD_PARTICLE_FORCE, SPH_COUPLING_LOAD_FACET_FORCE, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, SPH_COUPLING_CONTACT_STATE_NONE + use sph_coupling_interface, only: coupling_external_force, & + coupling_motion_velocity, coupling_motion_position, coupling_motion_flag, & + coupling_motion_mode, coupling_contact_flag, coupling_contact_gap, & + coupling_contact_penetration, coupling_contact_pressure, & + 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, & + sph_coupling_apply_particle_forces, sph_coupling_apply_facet_loads, & + sph_coupling_get_region_modes + implicit none + + integer :: ierr + + call check_legacy_restart_read() + +contains + + subroutine check_legacy_restart_read() + integer :: indices(1) + integer :: facets(1) + real(rk) :: forces(SPH_NDIM,1) + real(rk) :: loads(SPH_NDIM,1) + integer :: load_mode + integer :: distribution_mode + integer :: contact_state_mode + + call delete_file_if_exists('build-codex-unix/restart_coupling_080_seed.tmp') + call delete_file_if_exists('build-codex-unix/restart_coupling_080_compat.tmp') + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for restart 080 compat') + call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr) + call assert_ok(ierr, 'read failed for restart 080 compat') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for restart 080 compat') + + 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, 'particle force import failed for restart 080 compat') + 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, 'facet load import failed for restart 080 compat') + call sph_write_restart('build-codex-unix/restart_coupling_080_seed.tmp', ierr) + call assert_ok(ierr, 'seed restart write failed for restart 080 compat') + call write_legacy_restart('build-codex-unix/restart_coupling_080_seed.tmp', & + 'build-codex-unix/restart_coupling_080_compat.tmp') + + call scramble_coupling_state() + call sph_read_restart('build-codex-unix/restart_coupling_080_compat.tmp', ierr) + call assert_ok(ierr, 'legacy restart read failed') + call sph_coupling_get_region_modes(1, load_mode, distribution_mode, & + contact_state_mode, ierr) + call assert_ok(ierr, 'legacy region 1 mode query failed') + if (load_mode /= SPH_COUPLING_LOAD_FACET_FORCE) then + call fail('legacy region 1 load mode mismatch') + end if + call assert_legacy_common_modes(distribution_mode, contact_state_mode) + call sph_coupling_get_region_modes(2, load_mode, distribution_mode, & + contact_state_mode, ierr) + call assert_ok(ierr, 'legacy region 2 mode query failed') + if (load_mode /= SPH_COUPLING_LOAD_PARTICLE_FORCE) then + call fail('legacy region 2 load mode mismatch') + end if + call assert_legacy_common_modes(distribution_mode, contact_state_mode) + call assert_close(coupling_external_force(1,1), 1.0_rk, & + 'legacy restart force x mismatch') + call assert_close(coupling_external_force(2,1), 2.0_rk, & + 'legacy restart force y mismatch') + call assert_close(coupling_external_force(3,1), 8.0_rk, & + 'legacy restart force z mismatch') + if (coupling_imported_force_count /= 2) then + call fail('legacy restart imported force count mismatch') + end if + if (coupling_contact_flag(1) /= 0) call fail('legacy restart contact flag mismatch') + call assert_close(coupling_contact_gap(1), 0.0_rk, 'legacy restart gap mismatch') + call assert_close(coupling_contact_penetration(1), 0.0_rk, & + 'legacy restart penetration mismatch') + call assert_close(coupling_contact_pressure(1), 0.0_rk, & + 'legacy restart contact pressure mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for restart 080 compat') + end subroutine check_legacy_restart_read + + subroutine write_legacy_restart(source_name, target_name) + character(len=*), intent(in) :: source_name + character(len=*), intent(in) :: target_name + character(len=2048) :: line + integer :: ios + + open(unit=91, file=trim(source_name), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('legacy restart seed open failed') + open(unit=92, file=trim(target_name), status='unknown', action='write', iostat=ios) + if (ios /= 0) call fail('legacy restart target open failed') + read(91, '(A)', iostat=ios) line + if (ios /= 0) call fail('legacy restart seed header read failed') + write(92, '(A)') 'SPHLITE_RESTART 0 8 1 STATE_0_8' + do + read(91, '(A)', iostat=ios) line + if (ios /= 0) call fail('legacy restart coupling marker missing') + if (trim(line) == 'SPHLITE_COUPLING_STATE') exit + write(92, '(A)') trim(line) + end do + call write_legacy_coupling_block(92) + close(unit=91) + close(unit=92) + end subroutine write_legacy_restart + + subroutine write_legacy_coupling_block(unit) + integer, intent(in) :: unit + integer :: region + integer :: i + + write(unit, '(A)') 'SPHLITE_COUPLING_STATE' + write(unit, *) coupling_region_count, coupling_region_disabled_count + do region = 1, coupling_region_count + write(unit, '(A)') trim(coupling_region_name(region)) + write(unit, '(A)') trim(coupling_region_set_name(region)) + write(unit, '(A)') trim(coupling_region_surface_name(region)) + write(unit, '(A)') trim(coupling_region_material_name(region)) + write(unit, '(A)') trim(coupling_region_section_name(region)) + write(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(unit, *) coupling_region_particles(1:coupling_region_particle_count(region), & + region) + end if + end do + write(unit, *) coupling_cumulative_impulse(:), coupling_momentum_exchange(:) + write(unit, *) coupling_cumulative_work, coupling_energy_exchange + write(unit, *) coupling_stale_load_count, coupling_skipped_particle_count, & + coupling_nonfinite_import_count, coupling_imported_force_count + write(unit, *) npart + do i = 1, npart + write(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 write_legacy_coupling_block + + subroutine assert_legacy_common_modes(distribution_mode, contact_state_mode) + integer, intent(in) :: distribution_mode + integer, intent(in) :: contact_state_mode + + if (distribution_mode /= SPH_COUPLING_DISTRIBUTION_UNIFORM) then + call fail('legacy restart distribution mode mismatch') + end if + if (contact_state_mode /= SPH_COUPLING_CONTACT_STATE_NONE) then + call fail('legacy restart contact state mode mismatch') + end if + end subroutine assert_legacy_common_modes + + subroutine scramble_coupling_state() + coupling_external_force = -999.0_rk + coupling_motion_velocity = -999.0_rk + coupling_motion_position = -999.0_rk + coupling_motion_flag = -999 + coupling_motion_mode = -999 + coupling_contact_flag = -999 + coupling_contact_gap = -999.0_rk + coupling_contact_penetration = -999.0_rk + coupling_contact_pressure = -999.0_rk + coupling_imported_force_count = -999 + end subroutine scramble_coupling_state + + 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=93, file=trim(filename), status='old', iostat=ios) + if (ios == 0) close(unit=93, 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 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_compat diff --git a/tests/module/test_coupling_restart_081.f90 b/tests/module/test_coupling_restart_081.f90 new file mode 100644 index 0000000..7cc1767 --- /dev/null +++ b/tests/module/test_coupling_restart_081.f90 @@ -0,0 +1,186 @@ +program test_coupling_restart_081 + 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 + use sph_database, only: SPH_COUPLING_LOAD_PRESSURE, & + SPH_COUPLING_DISTRIBUTION_AREA, SPH_COUPLING_CONTACT_STATE_IMPORT + use sph_coupling_interface, only: coupling_external_force, & + coupling_contact_flag, coupling_contact_gap, coupling_contact_penetration, & + coupling_contact_pressure, coupling_facet_force_error, & + coupling_max_pressure, coupling_max_penetration, & + sph_coupling_apply_facet_pressures, sph_coupling_apply_contact_state, & + sph_coupling_get_region_modes, sph_coupling_get_load_balance, & + sph_coupling_get_contact_diagnostics + implicit none + + integer :: ierr + + call check_restart_081_round_trip() + +contains + + subroutine check_restart_081_round_trip() + integer :: ids(1) + real(rk) :: pressures(1) + real(rk) :: normals(SPH_NDIM,1) + real(rk) :: gaps(1) + real(rk) :: penetrations(1) + real(rk) :: contact_pressures(1) + integer :: load_mode + integer :: distribution_mode + integer :: contact_state_mode + integer :: active_count + integer :: skipped_count + integer :: stale_count + integer :: repeated_count + integer :: nonfinite_count + integer :: bad_area_count + integer :: invalid_weight_count + real(rk) :: max_penetration + real(rk) :: input_force(SPH_NDIM) + real(rk) :: distributed_force(SPH_NDIM) + real(rk) :: input_moment(SPH_NDIM) + real(rk) :: distributed_moment(SPH_NDIM) + real(rk) :: force_error + real(rk) :: moment_error + + call delete_file_if_exists('build-codex-unix/restart_coupling_081.tmp') + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for restart 081') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for restart 081') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for restart 081') + + ids(1) = 1 + pressures(1) = 15.0_rk + normals(:,1) = (/0.0_rk, 0.0_rk, 1.0_rk/) + gaps(1) = 3.0e-4_rk + penetrations(1) = 4.0e-3_rk + contact_pressures(1) = 11.0_rk + call sph_coupling_apply_facet_pressures(1, 1, ids, pressures, & + SPH_COUPLING_DISTRIBUTION_AREA, 91, ierr) + call assert_ok(ierr, 'restart 081 pressure import failed') + call sph_coupling_apply_contact_state(1, 1, ids, normals, gaps, & + penetrations, contact_pressures, 91, ierr) + call assert_ok(ierr, 'restart 081 contact import failed') + + call sph_write_restart('build-codex-unix/restart_coupling_081.tmp', ierr) + call assert_ok(ierr, 'restart 081 write failed') + call assert_restart_state_version('build-codex-unix/restart_coupling_081.tmp') + call scramble_coupling_state() + call sph_read_restart('build-codex-unix/restart_coupling_081.tmp', ierr) + call assert_ok(ierr, 'restart 081 read failed') + + call sph_coupling_get_region_modes(1, load_mode, distribution_mode, & + contact_state_mode, ierr) + call assert_ok(ierr, 'restart 081 mode query failed') + if (load_mode /= SPH_COUPLING_LOAD_PRESSURE) call fail('restart 081 load mode mismatch') + if (distribution_mode /= SPH_COUPLING_DISTRIBUTION_AREA) then + call fail('restart 081 distribution mode mismatch') + end if + if (contact_state_mode /= SPH_COUPLING_CONTACT_STATE_IMPORT) then + call fail('restart 081 contact state mode mismatch') + end if + if (coupling_contact_flag(1) /= 1) call fail('restart 081 contact flag mismatch') + call assert_close(coupling_contact_gap(1), gaps(1), 'restart 081 gap mismatch') + call assert_close(coupling_contact_penetration(1), penetrations(1), & + 'restart 081 penetration mismatch') + call assert_close(coupling_contact_pressure(1), contact_pressures(1), & + 'restart 081 contact pressure mismatch') + call assert_close(coupling_max_pressure, pressures(1), & + 'restart 081 max pressure mismatch') + call assert_close(coupling_max_penetration, penetrations(1), & + 'restart 081 max penetration mismatch') + call assert_close(coupling_facet_force_error, 0.0_rk, & + 'restart 081 force balance error mismatch') + + call sph_coupling_get_load_balance(input_force, distributed_force, & + force_error, input_moment, distributed_moment, moment_error, ierr) + call assert_ok(ierr, 'restart 081 load balance query failed') + call assert_close(force_error, 0.0_rk, 'restart 081 load force error mismatch') + call assert_close(moment_error, 0.0_rk, 'restart 081 load moment error mismatch') + call sph_coupling_get_contact_diagnostics(active_count, skipped_count, & + max_penetration, stale_count, repeated_count, nonfinite_count, & + bad_area_count, invalid_weight_count, ierr) + call assert_ok(ierr, 'restart 081 contact diagnostic query failed') + if (active_count /= 1 .or. skipped_count /= 0) then + call fail('restart 081 contact diagnostic count mismatch') + end if + call assert_close(max_penetration, penetrations(1), & + 'restart 081 diagnostic penetration mismatch') + if (maxval(abs(coupling_external_force(:,1))) <= 0.0_rk) then + call fail('restart 081 external force was not restored') + end if + call sph_advance_explicit(1.0e-7_rk, ierr) + call assert_ok(ierr, 'restart 081 continuation failed') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for restart 081') + end subroutine check_restart_081_round_trip + + subroutine scramble_coupling_state() + coupling_external_force = -999.0_rk + coupling_contact_flag = -999 + coupling_contact_gap = -999.0_rk + coupling_contact_penetration = -999.0_rk + coupling_contact_pressure = -999.0_rk + coupling_facet_force_error = -999.0_rk + coupling_max_pressure = -999.0_rk + coupling_max_penetration = -999.0_rk + end subroutine scramble_coupling_state + + subroutine assert_restart_state_version(filename) + character(len=*), intent(in) :: filename + character(len=64) :: tag + character(len=32) :: state_version + integer :: major + integer :: minor + integer :: patch + integer :: ios + + open(unit=91, file=trim(filename), status='old', action='read', iostat=ios) + if (ios /= 0) call fail('restart 081 header open failed') + read(91, *, iostat=ios) tag, major, minor, patch, state_version + close(unit=91) + if (ios /= 0) call fail('restart 081 header read failed') + if (trim(tag) /= 'SPHLITE_RESTART') call fail('restart 081 header tag mismatch') + if (trim(state_version) /= 'STATE_0_8_1') call fail('restart 081 state mismatch') + end subroutine assert_restart_state_version + + 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=92, file=trim(filename), status='old', iostat=ios) + if (ios == 0) close(unit=92, 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 assert_close(actual, expected, message) + real(rk), intent(in) :: actual + real(rk), intent(in) :: expected + character(len=*), intent(in) :: message + + if (abs(actual - expected) > max(1.0e-10_rk, abs(expected) * 1.0e-8_rk)) then + call fail(message) + end if + 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_081 diff --git a/todo.md b/todo.md index 701d1bd..0d98922 100644 --- a/todo.md +++ b/todo.md @@ -157,56 +157,56 @@ ## 阶段 E:Output、Restart、Fortran API 和外部接口层 -- [ ] 定义 summary CSV 的 0.8.1 contact 字段清单。 -- [ ] 追加 `coupling_active_contacts` summary CSV 字段。 -- [ ] 追加 `coupling_skipped_contacts` summary CSV 字段。 -- [ ] 追加 `coupling_max_pressure` summary CSV 字段。 -- [ ] 追加 `coupling_max_traction` summary CSV 字段。 -- [ ] 追加 `coupling_max_penetration` summary CSV 字段。 -- [ ] 追加 `coupling_force_balance_error` summary CSV 字段。 -- [ ] 追加 `coupling_moment_balance_error` summary CSV 字段。 -- [ ] 定义粒子 CSV 的 0.8.1 contact 调试字段清单。 -- [ ] 追加粒子 CSV 的 `coupling_contact_flag` 字段。 -- [ ] 追加粒子 CSV 的 `coupling_gap` 字段。 -- [ ] 追加粒子 CSV 的 `coupling_penetration` 字段。 -- [ ] 明确 0.8.1 restart 状态版本名称。 -- [ ] 实现 `STATE_0_8_1` 或等价 0.8.1 restart 写入。 -- [ ] 在 restart 中保存 load mode 和 distribution mode。 -- [ ] 在 restart 中保存 contact state 缓存。 -- [ ] 在 restart 中保存 facet 分配诊断。 -- [ ] 在 restart 中保存新增 contact diagnostics。 -- [ ] 实现 `STATE_0_8_1` 或等价 0.8.1 restart 读回。 -- [ ] 实现 0.8.0 restart 兼容读入后的空 0.8.1 状态初始化。 -- [ ] 实现 restart 读回后继续显式步的 0.8.1 coupling 状态恢复。 -- [ ] 新增 `sph_coupling_apply_facet_pressures` Fortran API。 -- [ ] 新增 `sph_coupling_apply_facet_tractions` Fortran API。 -- [ ] 新增 `sph_coupling_apply_facet_resultants` Fortran API。 -- [ ] 新增 `sph_coupling_apply_contact_state` Fortran API。 -- [ ] 新增 `sph_coupling_get_load_balance` Fortran API。 -- [ ] 新增 `sph_coupling_get_contact_diagnostics` Fortran API。 -- [ ] 在 C 头文件中声明 facet pressure 导入接口。 -- [ ] 在 C 头文件中声明 facet traction 导入接口。 -- [ ] 在 C 头文件中声明 contact state 导入接口。 -- [ ] 在 C 头文件中声明 contact diagnostics 查询接口。 -- [ ] 实现 C ABI 新接口的空指针错误路径。 -- [ ] 实现 C ABI 新接口的小容量错误路径。 -- [ ] 实现 C ABI 新接口的非法 region 错误路径。 -- [ ] 实现 C ABI 新接口的非有限输入错误路径。 -- [ ] 在 C++ wrapper 中追加 pressure、traction 和 diagnostics 薄包装。 -- [ ] 在 Python ctypes 中追加 pressure、traction 和 diagnostics 调试入口。 -- [ ] 在 CLI inspect 中追加 load mode、distribution 和 contact diagnostics 字段。 -- [ ] 新增 `coupling_output_081` 测试。 -- [ ] 新增 `coupling_restart_081` 测试。 -- [ ] 新增 `coupling_restart_080_compat` 测试。 -- [ ] 新增 `coupling_fortran_api_081` 测试。 -- [ ] 新增 `coupling_c_api_081` 测试。 -- [ ] 新增 `coupling_cpp_api_081` 测试。 -- [ ] 新增 `coupling_python_081` 测试。 -- [ ] 新增 `coupling_cli_081` 测试。 -- [ ] 将阶段 E 新测试接入 CTest。 -- [ ] 运行阶段 E output、restart 和接口专项测试。 -- [ ] 运行 `git diff --check`。 -- [ ] 准备阶段 E 本地提交说明。 +- [x] 定义 summary CSV 的 0.8.1 contact 字段清单。 +- [x] 追加 `coupling_active_contacts` summary CSV 字段。 +- [x] 追加 `coupling_skipped_contacts` summary CSV 字段。 +- [x] 追加 `coupling_max_pressure` summary CSV 字段。 +- [x] 追加 `coupling_max_traction` summary CSV 字段。 +- [x] 追加 `coupling_max_penetration` summary CSV 字段。 +- [x] 追加 `coupling_force_balance_error` summary CSV 字段。 +- [x] 追加 `coupling_moment_balance_error` summary CSV 字段。 +- [x] 定义粒子 CSV 的 0.8.1 contact 调试字段清单。 +- [x] 追加粒子 CSV 的 `coupling_contact_flag` 字段。 +- [x] 追加粒子 CSV 的 `coupling_gap` 字段。 +- [x] 追加粒子 CSV 的 `coupling_penetration` 字段。 +- [x] 明确 0.8.1 restart 状态版本名称。 +- [x] 实现 `STATE_0_8_1` 或等价 0.8.1 restart 写入。 +- [x] 在 restart 中保存 load mode 和 distribution mode。 +- [x] 在 restart 中保存 contact state 缓存。 +- [x] 在 restart 中保存 facet 分配诊断。 +- [x] 在 restart 中保存新增 contact diagnostics。 +- [x] 实现 `STATE_0_8_1` 或等价 0.8.1 restart 读回。 +- [x] 实现 0.8.0 restart 兼容读入后的空 0.8.1 状态初始化。 +- [x] 实现 restart 读回后继续显式步的 0.8.1 coupling 状态恢复。 +- [x] 新增 `sph_coupling_apply_facet_pressures` Fortran API。 +- [x] 新增 `sph_coupling_apply_facet_tractions` Fortran API。 +- [x] 新增 `sph_coupling_apply_facet_resultants` Fortran API。 +- [x] 新增 `sph_coupling_apply_contact_state` Fortran API。 +- [x] 新增 `sph_coupling_get_load_balance` Fortran API。 +- [x] 新增 `sph_coupling_get_contact_diagnostics` Fortran API。 +- [x] 在 C 头文件中声明 facet pressure 导入接口。 +- [x] 在 C 头文件中声明 facet traction 导入接口。 +- [x] 在 C 头文件中声明 contact state 导入接口。 +- [x] 在 C 头文件中声明 contact diagnostics 查询接口。 +- [x] 实现 C ABI 新接口的空指针错误路径。 +- [x] 实现 C ABI 新接口的小容量错误路径。 +- [x] 实现 C ABI 新接口的非法 region 错误路径。 +- [x] 实现 C ABI 新接口的非有限输入错误路径。 +- [x] 在 C++ wrapper 中追加 pressure、traction 和 diagnostics 薄包装。 +- [x] 在 Python ctypes 中追加 pressure、traction 和 diagnostics 调试入口。 +- [x] 在 CLI inspect 中追加 load mode、distribution 和 contact diagnostics 字段。 +- [x] 新增 `coupling_output_081` 测试。 +- [x] 新增 `coupling_restart_081` 测试。 +- [x] 新增 `coupling_restart_080_compat` 测试。 +- [x] 新增 `coupling_fortran_api_081` 测试。 +- [x] 新增 `coupling_c_api_081` 测试。 +- [x] 新增 `coupling_cpp_api_081` 测试。 +- [x] 新增 `coupling_python_081` 测试。 +- [x] 新增 `coupling_cli_081` 测试。 +- [x] 将阶段 E 新测试接入 CTest。 +- [x] 运行阶段 E output、restart 和接口专项测试。 +- [x] 运行 `git diff --check`。 +- [x] 准备阶段 E 本地提交说明。 ## 阶段 F:Mock Coupling Smoke、门禁、文档和发布准备 diff --git a/tools/testing/test_coupling_cli_081.py b/tools/testing/test_coupling_cli_081.py new file mode 100644 index 0000000..8e46f6d --- /dev/null +++ b/tools/testing/test_coupling_cli_081.py @@ -0,0 +1,90 @@ +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 081 field {field}") + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--exe", required=True) + parser.add_argument("--input", required=True) + args = parser.parse_args() + + fields = read_fields(run_command([str(Path(args.exe)), str(Path(args.input))]).stdout) + for field in ( + "coupling_regions", + "coupling_region_1_load_mode", + "coupling_region_1_distribution_mode", + "coupling_region_1_contact_state_mode", + "coupling_region_2_load_mode", + "coupling_region_2_distribution_mode", + "coupling_region_2_contact_state_mode", + "coupling_region_3_enabled", + "coupling_force_balance_error", + "coupling_moment_balance_error", + "coupling_max_pressure", + "coupling_max_traction", + "coupling_max_penetration", + "coupling_repeated_imports", + "coupling_invalid_weights", + "coupling_active_contacts", + "coupling_skipped_contacts", + "coupling_nonfinite_contact_imports", + ): + require_field(fields, field) + + if int(fields["coupling_regions"]) != 3: + raise AssertionError("coupling CLI 081 region count mismatch") + if int(fields["coupling_region_1_load_mode"]) != 3: + raise AssertionError("coupling CLI 081 pressure load mode mismatch") + if int(fields["coupling_region_1_distribution_mode"]) != 2: + raise AssertionError("coupling CLI 081 pressure distribution mismatch") + if int(fields["coupling_region_1_contact_state_mode"]) != 1: + raise AssertionError("coupling CLI 081 pressure contact state mismatch") + if int(fields["coupling_region_2_load_mode"]) != 4: + raise AssertionError("coupling CLI 081 traction load mode mismatch") + if int(fields["coupling_region_2_distribution_mode"]) != 3: + raise AssertionError("coupling CLI 081 traction distribution mismatch") + if int(fields["coupling_region_2_contact_state_mode"]) != 1: + raise AssertionError("coupling CLI 081 traction contact state mismatch") + if int(fields["coupling_region_3_enabled"]) != 0: + raise AssertionError("coupling CLI 081 disabled region mismatch") + if int(fields["coupling_active_contacts"]) != 0: + raise AssertionError("coupling CLI 081 active contact default mismatch") + if int(fields["coupling_skipped_contacts"]) != 0: + raise AssertionError("coupling CLI 081 skipped contact default mismatch") + + check_fields = read_fields( + run_command([str(Path(args.exe)), str(Path(args.input)), "--check"]).stdout + ) + if check_fields["mode"] != "check": + raise AssertionError("coupling CLI 081 check mode mismatch") + if int(check_fields["coupling_regions"]) != 3: + raise AssertionError("coupling CLI 081 check region count mismatch") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/testing/test_coupling_python_081.py b/tools/testing/test_coupling_python_081.py new file mode 100644 index 0000000..229e4c5 --- /dev/null +++ b/tools/testing/test_coupling_python_081.py @@ -0,0 +1,105 @@ +from __future__ import annotations + +import argparse +import math +import sys +from pathlib import Path + + +def close_to(actual: float, expected: float) -> bool: + return math.isclose(actual, expected, rel_tol=1.0e-8, abs_tol=1.0e-10) + + +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_CONTACT_STATE_IMPORT, + COUPLING_DISTRIBUTION_AREA, + COUPLING_DISTRIBUTION_MASS, + COUPLING_DISTRIBUTION_UNIFORM, + COUPLING_LOAD_PRESSURE, + COUPLING_LOAD_TRACTION, + SPHLite, + ) + + wrapper_source = (root / "Python" / "sphlite_debug.py").read_text(encoding="utf-8") + for symbol in ( + "sphlite_coupling_apply_facet_pressures", + "sphlite_coupling_apply_facet_tractions", + "sphlite_coupling_apply_facet_resultants", + "sphlite_coupling_apply_contact_state", + "sphlite_coupling_get_contact_diagnostics", + ): + if symbol not in wrapper_source: + raise AssertionError(f"Python wrapper is missing {symbol}") + 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) + pressure_region = summary["coupling_region_info"][0] + traction_region = summary["coupling_region_info"][1] + if pressure_region["load_mode"] != COUPLING_LOAD_PRESSURE: + raise AssertionError("Python region pressure load mode mismatch") + if pressure_region["distribution_mode"] != COUPLING_DISTRIBUTION_AREA: + raise AssertionError("Python region pressure distribution mismatch") + if pressure_region["contact_state_mode"] != COUPLING_CONTACT_STATE_IMPORT: + raise AssertionError("Python region contact state mismatch") + if traction_region["load_mode"] != COUPLING_LOAD_TRACTION: + raise AssertionError("Python region traction load mode mismatch") + if traction_region["distribution_mode"] != COUPLING_DISTRIBUTION_MASS: + raise AssertionError("Python region traction distribution mismatch") + + solver.coupling_apply_facet_resultants( + 1, [1], [1.0, 2.0, 3.0], COUPLING_DISTRIBUTION_UNIFORM, 101 + ) + balance = solver.coupling_load_balance() + if not close_to(balance["coupling_force_balance_error"], 0.0): + raise AssertionError("Python load balance force error mismatch") + solver.coupling_apply_facet_pressures(1, [1], [13.0], COUPLING_DISTRIBUTION_AREA, 102) + solver.coupling_apply_facet_tractions( + 2, [1], [0.0, 4.0, 0.0], COUPLING_DISTRIBUTION_MASS, 102 + ) + solver.coupling_apply_contact_state( + 1, [1], [0.0, 0.0, 1.0], [2.0e-4], [5.0e-3], [8.0], 102 + ) + contact = solver.coupling_contact_diagnostics() + if contact["coupling_active_contacts"] != 1: + raise AssertionError("Python active contact count mismatch") + if not close_to(contact["coupling_max_penetration"], 5.0e-3): + raise AssertionError("Python max penetration mismatch") + diagnostics = solver.coupling_diagnostics() + if diagnostics["coupling_active_contacts"] != 1: + raise AssertionError("Python diagnostics contact count mismatch") + if not close_to(diagnostics["coupling_max_pressure"], 13.0): + raise AssertionError("Python diagnostics pressure mismatch") + if not close_to(diagnostics["coupling_max_traction"], 4.0): + raise AssertionError("Python diagnostics traction mismatch") + try: + solver.coupling_apply_facet_tractions(2, [1], [1.0], COUPLING_DISTRIBUTION_MASS, 103) + except ValueError: + pass + else: + raise AssertionError("Python traction shape error did not raise") + try: + solver.coupling_apply_contact_state(1, [1], [0.0], [0.0], [0.0], [0.0], 103) + except ValueError: + pass + else: + raise AssertionError("Python contact shape error did not raise") + finally: + solver.finalize() + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())