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