diff --git a/CMakeLists.txt b/CMakeLists.txt index 92cfea0..1f1a19c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,6 +189,48 @@ set_target_properties(test_coupling_contact_state_081 PROPERTIES Fortran_MODULE_ add_test(NAME coupling_contact_state_081 COMMAND test_coupling_contact_state_081) set_tests_properties(coupling_contact_state_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_coupling_facet_pressure_081 tests/module/test_coupling_facet_pressure_081.f90) +target_link_libraries(test_coupling_facet_pressure_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_facet_pressure_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_facet_pressure_081 COMMAND test_coupling_facet_pressure_081) +set_tests_properties(coupling_facet_pressure_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_facet_traction_081 tests/module/test_coupling_facet_traction_081.f90) +target_link_libraries(test_coupling_facet_traction_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_facet_traction_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_facet_traction_081 COMMAND test_coupling_facet_traction_081) +set_tests_properties(coupling_facet_traction_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_facet_resultant_081 tests/module/test_coupling_facet_resultant_081.f90) +target_link_libraries(test_coupling_facet_resultant_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_facet_resultant_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_facet_resultant_081 COMMAND test_coupling_facet_resultant_081) +set_tests_properties(coupling_facet_resultant_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_distribution_uniform_081 tests/module/test_coupling_distribution_uniform_081.f90) +target_link_libraries(test_coupling_distribution_uniform_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_distribution_uniform_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_distribution_uniform_081 COMMAND test_coupling_distribution_uniform_081) +set_tests_properties(coupling_distribution_uniform_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_distribution_area_081 tests/module/test_coupling_distribution_area_081.f90) +target_link_libraries(test_coupling_distribution_area_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_distribution_area_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_distribution_area_081 COMMAND test_coupling_distribution_area_081) +set_tests_properties(coupling_distribution_area_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_distribution_mass_081 tests/module/test_coupling_distribution_mass_081.f90) +target_link_libraries(test_coupling_distribution_mass_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_distribution_mass_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_distribution_mass_081 COMMAND test_coupling_distribution_mass_081) +set_tests_properties(coupling_distribution_mass_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(test_coupling_load_balance_081 tests/module/test_coupling_load_balance_081.f90) +target_link_libraries(test_coupling_load_balance_081 PRIVATE sphlite_core) +set_target_properties(test_coupling_load_balance_081 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME coupling_load_balance_081 COMMAND test_coupling_load_balance_081) +set_tests_properties(coupling_load_balance_081 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_coupling_particle_export_080 tests/module/test_coupling_particle_export_080.f90) target_link_libraries(test_coupling_particle_export_080 PRIVATE sphlite_core) set_target_properties(test_coupling_particle_export_080 PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") diff --git a/src/coupling_interface/sph_coupling_interface.f90 b/src/coupling_interface/sph_coupling_interface.f90 index b196e27..d888956 100644 --- a/src/coupling_interface/sph_coupling_interface.f90 +++ b/src/coupling_interface/sph_coupling_interface.f90 @@ -8,6 +8,9 @@ module sph_coupling_interface DB_EXPORT_PARTICLE => SPH_COUPLING_EXPORT_PARTICLE, & DB_EXPORT_FACET => SPH_COUPLING_EXPORT_FACET, & DB_EXPORT_BOTH => SPH_COUPLING_EXPORT_BOTH, & + DB_DISTRIBUTION_UNIFORM => SPH_COUPLING_DISTRIBUTION_UNIFORM, & + DB_DISTRIBUTION_AREA => SPH_COUPLING_DISTRIBUTION_AREA, & + DB_DISTRIBUTION_MASS => SPH_COUPLING_DISTRIBUTION_MASS, & db_coupling_region_count => coupling_region_count, & db_coupling_region_name => coupling_region_name, & db_coupling_region_type => coupling_region_type, & @@ -66,6 +69,9 @@ module sph_coupling_interface integer :: coupling_skipped_particle_count = 0 integer :: coupling_nonfinite_import_count = 0 integer :: coupling_imported_force_count = 0 + integer :: coupling_repeated_import_count = 0 + integer :: coupling_duplicate_facet_count = 0 + integer :: coupling_invalid_weight_count = 0 real(rk) :: coupling_current_force(SPH_NDIM) = 0.0_rk real(rk) :: coupling_current_moment(SPH_NDIM) = 0.0_rk real(rk) :: coupling_current_impulse(SPH_NDIM) = 0.0_rk @@ -78,6 +84,14 @@ module sph_coupling_interface real(rk) :: coupling_max_acceleration = 0.0_rk real(rk) :: coupling_max_velocity_error = 0.0_rk real(rk) :: coupling_max_position_error = 0.0_rk + real(rk) :: coupling_facet_input_force(SPH_NDIM) = 0.0_rk + real(rk) :: coupling_facet_distributed_force(SPH_NDIM) = 0.0_rk + real(rk) :: coupling_facet_force_error = 0.0_rk + real(rk) :: coupling_facet_input_moment(SPH_NDIM) = 0.0_rk + real(rk) :: coupling_facet_distributed_moment(SPH_NDIM) = 0.0_rk + real(rk) :: coupling_facet_moment_error = 0.0_rk + real(rk) :: coupling_max_pressure = 0.0_rk + real(rk) :: coupling_max_traction = 0.0_rk integer :: coupling_step_load_consumed = 0 interface sph_coupling_get_diagnostics @@ -99,6 +113,7 @@ contains coupling_cumulative_work = 0.0_rk coupling_energy_exchange = 0.0_rk coupling_step_load_consumed = 0 + call sph_coupling_reset_load_balance() call sph_coupling_release_imports() end subroutine sph_coupling_reset @@ -118,7 +133,11 @@ contains coupling_skipped_particle_count = 0 coupling_nonfinite_import_count = 0 coupling_imported_force_count = 0 + coupling_repeated_import_count = 0 + coupling_duplicate_facet_count = 0 + coupling_invalid_weight_count = 0 call sph_coupling_reset_step_diagnostics() + call sph_coupling_reset_load_balance() coupling_cumulative_impulse = 0.0_rk coupling_momentum_exchange = 0.0_rk coupling_cumulative_work = 0.0_rk @@ -189,6 +208,7 @@ contains end if end if call sph_coupling_zero_import_buffers() + call sph_coupling_reset_load_balance() coupling_epoch = coupling_epoch + 1 end subroutine sph_coupling_clear_imports @@ -206,6 +226,17 @@ contains coupling_imported_force_count = 0 end subroutine sph_coupling_zero_import_buffers + subroutine sph_coupling_reset_load_balance() + coupling_facet_input_force = 0.0_rk + coupling_facet_distributed_force = 0.0_rk + coupling_facet_force_error = 0.0_rk + coupling_facet_input_moment = 0.0_rk + coupling_facet_distributed_moment = 0.0_rk + coupling_facet_moment_error = 0.0_rk + coupling_max_pressure = 0.0_rk + coupling_max_traction = 0.0_rk + end subroutine sph_coupling_reset_load_balance + subroutine sph_coupling_get_particle_count(count, ierr) integer, intent(out) :: count integer, intent(out) :: ierr @@ -770,6 +801,193 @@ contains end do end subroutine sph_coupling_apply_facet_loads + subroutine sph_coupling_apply_facet_pressures(region_index, load_count, & + facet_ids, pressures, distribution_mode, coupling_step, ierr) + 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 + integer :: i + real(rk) :: area + real(rk) :: normal(SPH_NDIM) + real(rk) :: center(SPH_NDIM) + real(rk) :: force(SPH_NDIM) + + ierr = SPH_SUCCESS + call sph_coupling_prepare_import_epoch(coupling_step, ierr) + if (ierr /= SPH_SUCCESS) return + if (.not. sph_coupling_distribution_is_valid(distribution_mode)) then + coupling_invalid_weight_count = coupling_invalid_weight_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, & + 'coupling distribution mode is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (region_index < 1 .or. region_index > db_coupling_region_count) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (load_count < 0) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling pressure count is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + do i = 1, load_count + call sph_coupling_check_duplicate_facet(i, load_count, facet_ids, ierr) + if (ierr /= SPH_SUCCESS) return + if (.not. sph_coupling_scalar_is_finite(pressures(i))) then + coupling_nonfinite_import_count = coupling_nonfinite_import_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling pressure is not finite') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_coupling_get_facet_geometry(region_index, facet_ids(i), & + area, normal, center, ierr) + if (ierr /= SPH_SUCCESS) return + force = pressures(i) * area * normal + if (abs(pressures(i)) > coupling_max_pressure) then + coupling_max_pressure = abs(pressures(i)) + end if + call sph_coupling_distribute_facet_force(region_index, facet_ids(i), & + force, distribution_mode, center, ierr) + if (ierr /= SPH_SUCCESS) return + end do + end subroutine sph_coupling_apply_facet_pressures + + subroutine sph_coupling_apply_facet_tractions(region_index, load_count, & + facet_ids, tractions, distribution_mode, coupling_step, ierr) + 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 + integer :: i + real(rk) :: area + real(rk) :: normal(SPH_NDIM) + real(rk) :: center(SPH_NDIM) + real(rk) :: force(SPH_NDIM) + real(rk) :: traction_norm + + ierr = SPH_SUCCESS + call sph_coupling_prepare_import_epoch(coupling_step, ierr) + if (ierr /= SPH_SUCCESS) return + if (.not. sph_coupling_distribution_is_valid(distribution_mode)) then + coupling_invalid_weight_count = coupling_invalid_weight_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, & + 'coupling distribution mode is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (region_index < 1 .or. region_index > db_coupling_region_count) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (load_count < 0) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling traction count is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + do i = 1, load_count + call sph_coupling_check_duplicate_facet(i, load_count, facet_ids, ierr) + if (ierr /= SPH_SUCCESS) return + if (.not. sph_coupling_vector_is_finite(tractions(:,i))) then + coupling_nonfinite_import_count = coupling_nonfinite_import_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling traction is not finite') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_coupling_get_facet_geometry(region_index, facet_ids(i), & + area, normal, center, ierr) + if (ierr /= SPH_SUCCESS) return + force = tractions(:,i) * area + traction_norm = sph_coupling_vector_norm(tractions(:,i)) + if (traction_norm > coupling_max_traction) then + coupling_max_traction = traction_norm + end if + call sph_coupling_distribute_facet_force(region_index, facet_ids(i), & + force, distribution_mode, center, ierr) + if (ierr /= SPH_SUCCESS) return + end do + end subroutine sph_coupling_apply_facet_tractions + + subroutine sph_coupling_apply_facet_resultants(region_index, load_count, & + facet_ids, resultants, distribution_mode, coupling_step, ierr) + 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 + integer :: i + real(rk) :: area + real(rk) :: normal(SPH_NDIM) + real(rk) :: center(SPH_NDIM) + + ierr = SPH_SUCCESS + call sph_coupling_prepare_import_epoch(coupling_step, ierr) + if (ierr /= SPH_SUCCESS) return + if (.not. sph_coupling_distribution_is_valid(distribution_mode)) then + coupling_invalid_weight_count = coupling_invalid_weight_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, & + 'coupling distribution mode is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (region_index < 1 .or. region_index > db_coupling_region_count) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (load_count < 0) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling resultant count is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + do i = 1, load_count + call sph_coupling_check_duplicate_facet(i, load_count, facet_ids, ierr) + if (ierr /= SPH_SUCCESS) return + if (.not. sph_coupling_vector_is_finite(resultants(:,i))) then + coupling_nonfinite_import_count = coupling_nonfinite_import_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling resultant is not finite') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_coupling_get_facet_geometry(region_index, facet_ids(i), & + area, normal, center, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_coupling_distribute_facet_force(region_index, facet_ids(i), & + resultants(:,i), distribution_mode, center, ierr) + if (ierr /= SPH_SUCCESS) return + end do + end subroutine sph_coupling_apply_facet_resultants + + subroutine sph_coupling_get_load_balance(input_force, distributed_force, & + force_error, input_moment, distributed_moment, moment_error, ierr) + 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 + + input_force = coupling_facet_input_force + distributed_force = coupling_facet_distributed_force + force_error = coupling_facet_force_error + input_moment = coupling_facet_input_moment + distributed_moment = coupling_facet_distributed_moment + moment_error = coupling_facet_moment_error + ierr = SPH_SUCCESS + end subroutine sph_coupling_get_load_balance + subroutine sph_coupling_apply_motion_boundary(region_index, export_index, & target_kind, mode, target, ierr) integer, intent(in) :: region_index @@ -1190,6 +1408,172 @@ contains coupling_imported_force_count = coupling_imported_force_count + 1 end subroutine sph_coupling_add_force_to_particle + subroutine sph_coupling_prepare_import_epoch(coupling_step, ierr) + integer, intent(in) :: coupling_step + integer, intent(out) :: ierr + logical :: has_force + + ierr = SPH_SUCCESS + if (coupling_step < 0) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling step is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_coupling_allocate_imports(ierr) + if (ierr /= SPH_SUCCESS) return + if (coupling_step /= coupling_epoch) then + call sph_coupling_zero_import_buffers() + call sph_coupling_reset_load_balance() + coupling_epoch = coupling_step + return + end if + has_force = .false. + if (allocated(coupling_external_force)) then + if (maxval(abs(coupling_external_force)) > 0.0_rk) has_force = .true. + end if + if (has_force) coupling_repeated_import_count = coupling_repeated_import_count + 1 + end subroutine sph_coupling_prepare_import_epoch + + logical function sph_coupling_distribution_is_valid(distribution_mode) + integer, intent(in) :: distribution_mode + + sph_coupling_distribution_is_valid = & + distribution_mode == DB_DISTRIBUTION_UNIFORM .or. & + distribution_mode == DB_DISTRIBUTION_AREA .or. & + distribution_mode == DB_DISTRIBUTION_MASS + end function sph_coupling_distribution_is_valid + + subroutine sph_coupling_check_duplicate_facet(position, load_count, & + facet_ids, ierr) + integer, intent(in) :: position + integer, intent(in) :: load_count + integer, intent(in) :: facet_ids(load_count) + integer, intent(out) :: ierr + integer :: i + + ierr = SPH_SUCCESS + do i = 1, position - 1 + if (facet_ids(i) == facet_ids(position)) then + coupling_duplicate_facet_count = coupling_duplicate_facet_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling facet id is duplicated') + ierr = SPH_ERROR_ARGUMENT + return + end if + end do + end subroutine sph_coupling_check_duplicate_facet + + subroutine sph_coupling_get_facet_geometry(region, facet_id, area, & + normal, center, ierr) + integer, intent(in) :: region + integer, intent(in) :: facet_id + real(rk), intent(out) :: area + real(rk), intent(out) :: normal(SPH_NDIM) + real(rk), intent(out) :: center(SPH_NDIM) + integer, intent(out) :: ierr + real(rk) :: normal_length + + ierr = SPH_SUCCESS + area = 0.0_rk + normal = 0.0_rk + center = 0.0_rk + call sph_coupling_require_enabled_region(region, ierr) + if (ierr /= SPH_SUCCESS) return + call sph_coupling_require_facet(region, facet_id, ierr) + if (ierr /= SPH_SUCCESS) return + area = shell_reference_area(facet_id) + normal = shell_director(:,facet_id) + center = x(:,facet_id) + if (.not. sph_coupling_scalar_is_finite(area) .or. area <= 0.0_rk) then + coupling_invalid_weight_count = coupling_invalid_weight_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling facet area is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (.not. sph_coupling_vector_is_finite(normal)) then + coupling_nonfinite_import_count = coupling_nonfinite_import_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling facet normal is not finite') + ierr = SPH_ERROR_ARGUMENT + return + end if + normal_length = sph_coupling_vector_norm(normal) + if (normal_length <= 1.0e-12_rk) then + coupling_invalid_weight_count = coupling_invalid_weight_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling facet normal is degenerate') + ierr = SPH_ERROR_ARGUMENT + return + end if + normal = normal / normal_length + if (.not. sph_coupling_vector_is_finite(center)) then + coupling_nonfinite_import_count = coupling_nonfinite_import_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling facet center is not finite') + ierr = SPH_ERROR_ARGUMENT + return + end if + end subroutine sph_coupling_get_facet_geometry + + subroutine sph_coupling_distribute_facet_force(region, facet_id, force, & + distribution_mode, center, ierr) + integer, intent(in) :: region + integer, intent(in) :: facet_id + integer, intent(in) :: distribution_mode + real(rk), intent(in) :: force(SPH_NDIM) + real(rk), intent(in) :: center(SPH_NDIM) + integer, intent(out) :: ierr + real(rk) :: weight + real(rk) :: before_force(SPH_NDIM) + real(rk) :: applied_force(SPH_NDIM) + real(rk) :: input_moment(SPH_NDIM) + real(rk) :: applied_moment(SPH_NDIM) + real(rk) :: residual_force(SPH_NDIM) + real(rk) :: residual_moment(SPH_NDIM) + + ierr = SPH_SUCCESS + if (region < 1 .or. region > db_coupling_region_count) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling region index is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (.not. sph_coupling_distribution_is_valid(distribution_mode)) then + coupling_invalid_weight_count = coupling_invalid_weight_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling distribution mode is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (.not. sph_coupling_vector_is_finite(force)) then + coupling_nonfinite_import_count = coupling_nonfinite_import_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling facet force is not finite') + ierr = SPH_ERROR_ARGUMENT + return + end if + weight = 1.0_rk + if (distribution_mode == DB_DISTRIBUTION_AREA) then + weight = shell_reference_area(facet_id) + else if (distribution_mode == DB_DISTRIBUTION_MASS) then + weight = mass(facet_id) + end if + if (.not. sph_coupling_scalar_is_finite(weight) .or. weight <= 0.0_rk) then + coupling_invalid_weight_count = coupling_invalid_weight_count + 1 + call sph_error_set(SPH_ERROR_ARGUMENT, 'coupling distribution weight is invalid') + ierr = SPH_ERROR_ARGUMENT + return + end if + before_force = 0.0_rk + if (allocated(coupling_external_force)) before_force = coupling_external_force(:,facet_id) + call sph_coupling_cross(center, force, input_moment) + call sph_coupling_add_force_to_particle(facet_id, force, ierr) + if (ierr /= SPH_SUCCESS) return + applied_force = coupling_external_force(:,facet_id) - before_force + call sph_coupling_cross(x(:,facet_id), applied_force, applied_moment) + coupling_facet_input_force = coupling_facet_input_force + force + coupling_facet_distributed_force = coupling_facet_distributed_force + applied_force + coupling_facet_input_moment = coupling_facet_input_moment + input_moment + coupling_facet_distributed_moment = coupling_facet_distributed_moment + applied_moment + residual_force = coupling_facet_input_force - coupling_facet_distributed_force + residual_moment = coupling_facet_input_moment - coupling_facet_distributed_moment + coupling_facet_force_error = sph_coupling_vector_norm(residual_force) + coupling_facet_moment_error = sph_coupling_vector_norm(residual_moment) + end subroutine sph_coupling_distribute_facet_force + subroutine sph_coupling_require_enabled_region(region, ierr) integer, intent(in) :: region integer, intent(out) :: ierr diff --git a/tests/module/test_coupling_distribution_area_081.f90 b/tests/module/test_coupling_distribution_area_081.f90 new file mode 100644 index 0000000..3522451 --- /dev/null +++ b/tests/module/test_coupling_distribution_area_081.f90 @@ -0,0 +1,109 @@ +program test_coupling_distribution_area_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_DISTRIBUTION_AREA + use sph_shell, only: shell_director, shell_reference_area + use sph_coupling_interface, only: coupling_external_force, & + sph_coupling_apply_facet_resultants + implicit none + + integer :: ierr + + call check_area_distribution() + call check_bad_area() + call check_bad_normal() + +contains + + subroutine check_area_distribution() + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + call build_case() + ids(1) = 1 + resultants(:,1) = (/ 0.0_rk, 4.0_rk, 2.0_rk /) + call sph_coupling_apply_facet_resultants(1, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_AREA, 51, ierr) + call assert_ok(ierr, 'area distribution failed') + call assert_vector_close(coupling_external_force(:,1), resultants(:,1), & + 'area distribution force mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after area distribution') + end subroutine check_area_distribution + + subroutine check_bad_area() + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + call build_case() + ids(1) = 1 + resultants(:,1) = (/ 1.0_rk, 0.0_rk, 0.0_rk /) + shell_reference_area(1) = 0.0_rk + call sph_coupling_apply_facet_resultants(1, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_AREA, 52, ierr) + if (ierr == SPH_SUCCESS) call fail('nonpositive facet area did not fail') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after bad area') + end subroutine check_bad_area + + subroutine check_bad_normal() + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + call build_case() + ids(1) = 1 + resultants(:,1) = (/ 1.0_rk, 0.0_rk, 0.0_rk /) + shell_director(:,1) = 0.0_rk + call sph_coupling_apply_facet_resultants(1, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_AREA, 53, ierr) + if (ierr == SPH_SUCCESS) call fail('degenerate facet normal did not fail') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after bad normal') + end subroutine check_bad_normal + + subroutine build_case() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for area distribution') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for area distribution') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for area distribution') + end subroutine build_case + + 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_distribution_area_081 diff --git a/tests/module/test_coupling_distribution_mass_081.f90 b/tests/module/test_coupling_distribution_mass_081.f90 new file mode 100644 index 0000000..8730f64 --- /dev/null +++ b/tests/module/test_coupling_distribution_mass_081.f90 @@ -0,0 +1,109 @@ +program test_coupling_distribution_mass_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: mass, SPH_COUPLING_DISTRIBUTION_MASS + use sph_shell, only: shell_director + use sph_coupling_interface, only: coupling_external_force, & + sph_coupling_apply_facet_resultants + implicit none + + integer :: ierr + + call check_mass_distribution() + call check_bad_mass_weight() + call check_nonfinite_normal() + +contains + + subroutine check_mass_distribution() + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + call build_case() + ids(1) = 2 + resultants(:,1) = (/ -1.0_rk, 3.0_rk, 2.0_rk /) + call sph_coupling_apply_facet_resultants(2, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_MASS, 61, ierr) + call assert_ok(ierr, 'mass distribution failed') + call assert_vector_close(coupling_external_force(:,2), resultants(:,1), & + 'mass distribution force mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after mass distribution') + end subroutine check_mass_distribution + + subroutine check_bad_mass_weight() + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + call build_case() + ids(1) = 2 + resultants(:,1) = (/ 1.0_rk, 0.0_rk, 0.0_rk /) + mass(2) = 0.0_rk + call sph_coupling_apply_facet_resultants(2, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_MASS, 62, ierr) + if (ierr == SPH_SUCCESS) call fail('nonpositive mass weight did not fail') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after bad mass') + end subroutine check_bad_mass_weight + + subroutine check_nonfinite_normal() + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + call build_case() + ids(1) = 2 + resultants(:,1) = (/ 1.0_rk, 0.0_rk, 0.0_rk /) + shell_director(1,2) = huge(shell_director(1,2)) + call sph_coupling_apply_facet_resultants(2, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_MASS, 63, ierr) + if (ierr == SPH_SUCCESS) call fail('nonfinite facet normal did not fail') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after nonfinite normal') + end subroutine check_nonfinite_normal + + subroutine build_case() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for mass distribution') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for mass distribution') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for mass distribution') + end subroutine build_case + + 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_distribution_mass_081 diff --git a/tests/module/test_coupling_distribution_uniform_081.f90 b/tests/module/test_coupling_distribution_uniform_081.f90 new file mode 100644 index 0000000..64b0552 --- /dev/null +++ b/tests/module/test_coupling_distribution_uniform_081.f90 @@ -0,0 +1,69 @@ +program test_coupling_distribution_uniform_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_DISTRIBUTION_UNIFORM + use sph_coupling_interface, only: coupling_external_force, & + sph_coupling_apply_facet_resultants + implicit none + + integer :: ierr + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for uniform distribution') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for uniform distribution') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for uniform distribution') + + ids(1) = 1 + resultants(:,1) = (/ 3.0_rk, -2.0_rk, 1.0_rk /) + call sph_coupling_apply_facet_resultants(1, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, 41, ierr) + call assert_ok(ierr, 'uniform distribution failed') + call assert_vector_close(coupling_external_force(:,1), resultants(:,1), & + 'uniform distribution force mismatch') + + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for uniform distribution') + +contains + + 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_distribution_uniform_081 diff --git a/tests/module/test_coupling_facet_pressure_081.f90 b/tests/module/test_coupling_facet_pressure_081.f90 new file mode 100644 index 0000000..ae403ad --- /dev/null +++ b/tests/module/test_coupling_facet_pressure_081.f90 @@ -0,0 +1,121 @@ +program test_coupling_facet_pressure_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_DISTRIBUTION_AREA + use sph_shell, only: shell_director, shell_reference_area + use sph_coupling_interface, only: coupling_external_force, & + coupling_max_pressure, coupling_nonfinite_import_count, & + sph_coupling_apply_facet_pressures, sph_coupling_get_load_balance + implicit none + + integer :: ierr + + call check_pressure_mapping() + call check_pressure_nonfinite() + +contains + + subroutine check_pressure_mapping() + integer :: ids(3) + integer :: i + real(rk) :: pressures(3) + real(rk) :: expected(SPH_NDIM) + real(rk) :: total(SPH_NDIM) + 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 build_case() + ids = (/ 1, 2, 3 /) + pressures = (/ 10.0_rk, -4.0_rk, 2.5_rk /) + call sph_coupling_apply_facet_pressures(1, 3, ids, pressures, & + SPH_COUPLING_DISTRIBUTION_AREA, 11, ierr) + call assert_ok(ierr, 'facet pressure import failed') + total = 0.0_rk + do i = 1, 3 + expected = pressures(i) * shell_reference_area(ids(i)) * & + shell_director(:,ids(i)) + total = total + expected + call assert_vector_close(coupling_external_force(:,ids(i)), & + expected, 'facet pressure force mismatch') + end do + call sph_coupling_get_load_balance(input_force, distributed_force, & + force_error, input_moment, distributed_moment, moment_error, ierr) + call assert_ok(ierr, 'pressure load balance failed') + call assert_vector_close(input_force, total, 'pressure input force mismatch') + call assert_vector_close(distributed_force, total, & + 'pressure distributed force mismatch') + call assert_close(force_error, 0.0_rk, 'pressure force error mismatch') + call assert_close(moment_error, 0.0_rk, 'pressure moment error mismatch') + call assert_close(coupling_max_pressure, 10.0_rk, & + 'pressure max diagnostic mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after pressure mapping') + end subroutine check_pressure_mapping + + subroutine check_pressure_nonfinite() + integer :: ids(1) + real(rk) :: pressures(1) + + call build_case() + ids(1) = 1 + pressures(1) = huge(pressures(1)) + call sph_coupling_apply_facet_pressures(1, 1, ids, pressures, & + SPH_COUPLING_DISTRIBUTION_AREA, 12, ierr) + if (ierr == SPH_SUCCESS) call fail('nonfinite pressure did not fail') + if (coupling_nonfinite_import_count <= 0) then + call fail('nonfinite pressure diagnostic missing') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after pressure error') + end subroutine check_pressure_nonfinite + + subroutine build_case() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for pressure test') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for pressure test') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for pressure test') + end subroutine build_case + + 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_facet_pressure_081 diff --git a/tests/module/test_coupling_facet_resultant_081.f90 b/tests/module/test_coupling_facet_resultant_081.f90 new file mode 100644 index 0000000..d9cca3f --- /dev/null +++ b/tests/module/test_coupling_facet_resultant_081.f90 @@ -0,0 +1,100 @@ +program test_coupling_facet_resultant_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_DISTRIBUTION_UNIFORM + use sph_coupling_interface, only: coupling_external_force, & + coupling_nonfinite_import_count, sph_coupling_apply_facet_resultants + implicit none + + integer :: ierr + + call check_resultant_mapping() + call check_resultant_nonfinite() + +contains + + subroutine check_resultant_mapping() + integer :: ids(3) + integer :: i + real(rk) :: resultants(SPH_NDIM,3) + + call build_case() + ids = (/ 1, 2, 3 /) + resultants(:,1) = (/ 1.0_rk, 2.0_rk, 3.0_rk /) + resultants(:,2) = (/ -2.0_rk, 1.0_rk, 0.0_rk /) + resultants(:,3) = (/ 0.5_rk, -0.5_rk, 4.0_rk /) + call sph_coupling_apply_facet_resultants(1, 3, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, 31, ierr) + call assert_ok(ierr, 'facet resultant import failed') + do i = 1, 3 + call assert_vector_close(coupling_external_force(:,ids(i)), & + resultants(:,i), 'facet resultant force mismatch') + end do + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after resultant mapping') + end subroutine check_resultant_mapping + + subroutine check_resultant_nonfinite() + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + call build_case() + ids(1) = 1 + resultants = 0.0_rk + resultants(2,1) = huge(resultants(2,1)) + call sph_coupling_apply_facet_resultants(1, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, 32, ierr) + if (ierr == SPH_SUCCESS) call fail('nonfinite resultant did not fail') + if (coupling_nonfinite_import_count <= 0) then + call fail('nonfinite resultant diagnostic missing') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after resultant error') + end subroutine check_resultant_nonfinite + + subroutine build_case() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for resultant test') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for resultant test') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for resultant test') + end subroutine build_case + + 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_facet_resultant_081 diff --git a/tests/module/test_coupling_facet_traction_081.f90 b/tests/module/test_coupling_facet_traction_081.f90 new file mode 100644 index 0000000..a1ad29b --- /dev/null +++ b/tests/module/test_coupling_facet_traction_081.f90 @@ -0,0 +1,109 @@ +program test_coupling_facet_traction_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_DISTRIBUTION_MASS + use sph_shell, only: shell_reference_area + use sph_coupling_interface, only: coupling_external_force, & + coupling_max_traction, coupling_nonfinite_import_count, & + sph_coupling_apply_facet_tractions + implicit none + + integer :: ierr + + call check_traction_mapping() + call check_traction_nonfinite() + +contains + + subroutine check_traction_mapping() + integer :: ids(3) + integer :: i + real(rk) :: tractions(SPH_NDIM,3) + real(rk) :: expected(SPH_NDIM) + real(rk) :: max_norm + + call build_case() + ids = (/ 1, 2, 3 /) + tractions(:,1) = (/ 1.0_rk, 0.0_rk, 3.0_rk /) + tractions(:,2) = (/ 0.0_rk, 2.0_rk, 0.0_rk /) + tractions(:,3) = (/ -1.0_rk, 1.0_rk, 0.5_rk /) + call sph_coupling_apply_facet_tractions(2, 3, ids, tractions, & + SPH_COUPLING_DISTRIBUTION_MASS, 21, ierr) + call assert_ok(ierr, 'facet traction import failed') + max_norm = 0.0_rk + do i = 1, 3 + expected = tractions(:,i) * shell_reference_area(ids(i)) + max_norm = max(max_norm, sqrt(sum(tractions(:,i) * tractions(:,i)))) + call assert_vector_close(coupling_external_force(:,ids(i)), & + expected, 'facet traction force mismatch') + end do + call assert_close(coupling_max_traction, max_norm, & + 'traction max diagnostic mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after traction mapping') + end subroutine check_traction_mapping + + subroutine check_traction_nonfinite() + integer :: ids(1) + real(rk) :: tractions(SPH_NDIM,1) + + call build_case() + ids(1) = 1 + tractions = 0.0_rk + tractions(1,1) = huge(tractions(1,1)) + call sph_coupling_apply_facet_tractions(2, 1, ids, tractions, & + SPH_COUPLING_DISTRIBUTION_MASS, 22, ierr) + if (ierr == SPH_SUCCESS) call fail('nonfinite traction did not fail') + if (coupling_nonfinite_import_count <= 0) then + call fail('nonfinite traction diagnostic missing') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after traction error') + end subroutine check_traction_nonfinite + + subroutine build_case() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for traction test') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for traction test') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for traction test') + end subroutine build_case + + 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_facet_traction_081 diff --git a/tests/module/test_coupling_load_balance_081.f90 b/tests/module/test_coupling_load_balance_081.f90 new file mode 100644 index 0000000..39308d6 --- /dev/null +++ b/tests/module/test_coupling_load_balance_081.f90 @@ -0,0 +1,152 @@ +program test_coupling_load_balance_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: x, SPH_COUPLING_DISTRIBUTION_UNIFORM + use sph_coupling_interface, only: coupling_external_force, & + coupling_duplicate_facet_count, coupling_repeated_import_count, & + sph_coupling_apply_facet_resultants, sph_coupling_get_load_balance + implicit none + + integer :: ierr + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for load balance') + call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr) + call assert_ok(ierr, 'read failed for load balance') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for load balance') + + call check_force_and_moment_balance() + call check_repeated_epoch_and_cleanup() + call check_duplicate_and_invalid_facet() + + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for load balance') + +contains + + subroutine check_force_and_moment_balance() + integer :: ids(2) + real(rk) :: resultants(SPH_NDIM,2) + 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) :: expected_force(SPH_NDIM) + real(rk) :: expected_moment(SPH_NDIM) + real(rk) :: moment_value(SPH_NDIM) + real(rk) :: force_error + real(rk) :: moment_error + + ids = (/ 1, 2 /) + resultants(:,1) = (/ 1.0_rk, 0.0_rk, 2.0_rk /) + resultants(:,2) = (/ 0.0_rk, 3.0_rk, 1.0_rk /) + call sph_coupling_apply_facet_resultants(1, 2, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, 71, ierr) + call assert_ok(ierr, 'load balance import failed') + expected_force = resultants(:,1) + resultants(:,2) + call cross3(x(:,1), resultants(:,1), expected_moment) + call cross3(x(:,2), resultants(:,2), moment_value) + expected_moment = expected_moment + moment_value + call sph_coupling_get_load_balance(input_force, distributed_force, & + force_error, input_moment, distributed_moment, moment_error, ierr) + call assert_ok(ierr, 'load balance query failed') + call assert_vector_close(input_force, expected_force, 'input force mismatch') + call assert_vector_close(distributed_force, expected_force, & + 'distributed force mismatch') + call assert_vector_close(input_moment, expected_moment, & + 'input moment mismatch') + call assert_vector_close(distributed_moment, expected_moment, & + 'distributed moment mismatch') + call assert_close(force_error, 0.0_rk, 'force error mismatch') + call assert_close(moment_error, 0.0_rk, 'moment error mismatch') + end subroutine check_force_and_moment_balance + + subroutine check_repeated_epoch_and_cleanup() + integer :: ids(1) + real(rk) :: resultants(SPH_NDIM,1) + + ids(1) = 3 + resultants(:,1) = (/ 0.0_rk, 0.0_rk, 5.0_rk /) + call sph_coupling_apply_facet_resultants(1, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, 71, ierr) + call assert_ok(ierr, 'same epoch import failed') + if (coupling_repeated_import_count <= 0) then + call fail('same epoch repeated import diagnostic missing') + end if + resultants(:,1) = (/ -2.0_rk, 0.0_rk, 0.0_rk /) + call sph_coupling_apply_facet_resultants(1, 1, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, 72, ierr) + call assert_ok(ierr, 'new epoch import failed') + if (maxval(abs(coupling_external_force(:,1))) > 0.0_rk) then + call fail('new epoch did not clear old facet load') + end if + call assert_vector_close(coupling_external_force(:,3), resultants(:,1), & + 'new epoch facet force mismatch') + end subroutine check_repeated_epoch_and_cleanup + + subroutine check_duplicate_and_invalid_facet() + integer :: ids(2) + real(rk) :: resultants(SPH_NDIM,2) + + ids = (/ 1, 1 /) + resultants = 1.0_rk + call sph_coupling_apply_facet_resultants(1, 2, ids, resultants, & + SPH_COUPLING_DISTRIBUTION_UNIFORM, 73, ierr) + if (ierr == SPH_SUCCESS) call fail('duplicate facet id did not fail') + if (coupling_duplicate_facet_count <= 0) then + call fail('duplicate facet diagnostic missing') + end if + ids(1) = 99 + call sph_coupling_apply_facet_resultants(1, 1, ids(1:1), & + resultants(:,1:1), SPH_COUPLING_DISTRIBUTION_UNIFORM, 74, ierr) + if (ierr == SPH_SUCCESS) call fail('invalid facet id did not fail') + end subroutine check_duplicate_and_invalid_facet + + subroutine cross3(left, right, result) + real(rk), intent(in) :: left(SPH_NDIM) + real(rk), intent(in) :: right(SPH_NDIM) + real(rk), intent(out) :: result(SPH_NDIM) + + result(1) = left(2) * right(3) - left(3) * right(2) + result(2) = left(3) * right(1) - left(1) * right(3) + result(3) = left(1) * right(2) - left(2) * right(1) + end subroutine cross3 + + 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_load_balance_081 diff --git a/todo.md b/todo.md index fedcedf..99c1cdf 100644 --- a/todo.md +++ b/todo.md @@ -77,47 +77,47 @@ ## 阶段 C:Facet Pressure、Traction、Resultant 和分配策略 -- [ ] 设计 facet pressure 导入缓存。 -- [ ] 设计 facet traction 导入缓存。 -- [ ] 设计 facet resultant 导入缓存。 -- [ ] 设计 facet 分配后粒子力缓存复用路径。 -- [ ] 定义 pressure 正负号约定。 -- [ ] 实现 pressure 到 facet 合力的转换。 -- [ ] 实现 traction 到 facet 合力的转换。 -- [ ] 实现 resultant force 的直接接收路径。 -- [ ] 实现 `UNIFORM` 分配策略。 -- [ ] 实现 `AREA` 或受控 facet 权重分配策略。 -- [ ] 实现 `MASS` 分配策略。 -- [ ] 对权重和非正时报 error。 -- [ ] 对 facet 面积非正时报 error。 -- [ ] 对 facet 法向非有限时报 error。 -- [ ] 对 facet 法向长度过小时报 error。 -- [ ] 对 pressure 非有限时报 error。 -- [ ] 对 traction 非有限时报 error。 -- [ ] 对 resultant 非有限时报 error。 -- [ ] 对非法 facet id 报 error。 -- [ ] 对重复 facet id 记录诊断或报 error。 -- [ ] 对同一 epoch 重复导入按固定策略处理。 -- [ ] 对新 epoch 导入清理上一 epoch 临时载荷。 -- [ ] 计算 facet 输入总力。 -- [ ] 计算 facet 分配后总力。 -- [ ] 计算 facet 总力误差。 -- [ ] 计算 facet 输入参考力矩。 -- [ ] 计算 facet 分配后参考力矩。 -- [ ] 计算 facet 力矩误差。 -- [ ] 记录最大 pressure。 -- [ ] 记录最大 traction。 -- [ ] 新增 `coupling_facet_pressure_081` 测试。 -- [ ] 新增 `coupling_facet_traction_081` 测试。 -- [ ] 新增 `coupling_facet_resultant_081` 测试。 -- [ ] 新增 `coupling_distribution_uniform_081` 测试。 -- [ ] 新增 `coupling_distribution_area_081` 测试。 -- [ ] 新增 `coupling_distribution_mass_081` 测试。 -- [ ] 新增 `coupling_load_balance_081` 测试。 -- [ ] 将阶段 C 新测试接入 CTest。 -- [ ] 运行阶段 C facet 载荷分配专项测试。 -- [ ] 运行 `git diff --check`。 -- [ ] 准备阶段 C 本地提交说明。 +- [x] 设计 facet pressure 导入缓存。 +- [x] 设计 facet traction 导入缓存。 +- [x] 设计 facet resultant 导入缓存。 +- [x] 设计 facet 分配后粒子力缓存复用路径。 +- [x] 定义 pressure 正负号约定。 +- [x] 实现 pressure 到 facet 合力的转换。 +- [x] 实现 traction 到 facet 合力的转换。 +- [x] 实现 resultant force 的直接接收路径。 +- [x] 实现 `UNIFORM` 分配策略。 +- [x] 实现 `AREA` 或受控 facet 权重分配策略。 +- [x] 实现 `MASS` 分配策略。 +- [x] 对权重和非正时报 error。 +- [x] 对 facet 面积非正时报 error。 +- [x] 对 facet 法向非有限时报 error。 +- [x] 对 facet 法向长度过小时报 error。 +- [x] 对 pressure 非有限时报 error。 +- [x] 对 traction 非有限时报 error。 +- [x] 对 resultant 非有限时报 error。 +- [x] 对非法 facet id 报 error。 +- [x] 对重复 facet id 记录诊断或报 error。 +- [x] 对同一 epoch 重复导入按固定策略处理。 +- [x] 对新 epoch 导入清理上一 epoch 临时载荷。 +- [x] 计算 facet 输入总力。 +- [x] 计算 facet 分配后总力。 +- [x] 计算 facet 总力误差。 +- [x] 计算 facet 输入参考力矩。 +- [x] 计算 facet 分配后参考力矩。 +- [x] 计算 facet 力矩误差。 +- [x] 记录最大 pressure。 +- [x] 记录最大 traction。 +- [x] 新增 `coupling_facet_pressure_081` 测试。 +- [x] 新增 `coupling_facet_traction_081` 测试。 +- [x] 新增 `coupling_facet_resultant_081` 测试。 +- [x] 新增 `coupling_distribution_uniform_081` 测试。 +- [x] 新增 `coupling_distribution_area_081` 测试。 +- [x] 新增 `coupling_distribution_mass_081` 测试。 +- [x] 新增 `coupling_load_balance_081` 测试。 +- [x] 将阶段 C 新测试接入 CTest。 +- [x] 运行阶段 C facet 载荷分配专项测试。 +- [x] 运行 `git diff --check`。 +- [x] 准备阶段 C 本地提交说明。 ## 阶段 D:显式步接入、接触诊断和回归隔离