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