program test_coupling_interface use sph_precision, only: rk use sph_error, only: SPH_SUCCESS use sph_database, only: SPH_PHASE_WATER use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & sph_advance_explicit use sph_coupling_interface, only: sph_coupling_get_particle_count, & sph_coupling_export_particle, sph_coupling_get_facet_count, & sph_coupling_export_facet, sph_coupling_import_particle_force, & sph_coupling_import_facet_load, sph_coupling_get_diagnostics, & sph_coupling_export_particle_state, coupling_type implicit none call check_legacy_region() call check_water_runtime_state() contains subroutine check_legacy_region() integer :: ierr integer :: count integer :: ext_id integer :: nodes(3) real(rk) :: position(3) real(rk) :: velocity(3) real(rk) :: force(3) real(rk) :: work real(rk) :: energy real(rk) :: momentum(3) call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed') call sph_read_inp('tests/data/coupling_region_011.inp', ierr) call assert_ok(ierr, 'read input failed') call sph_build_model(ierr) call assert_ok(ierr, 'build model failed') call sph_coupling_get_particle_count(count, ierr) call assert_ok(ierr, 'coupling particle count failed') if (count /= 2) call fail('coupling particle count mismatch') call sph_coupling_export_particle(1, ext_id, position, velocity, ierr) call assert_ok(ierr, 'coupling particle export failed') if (ext_id /= 1) call fail('coupling particle export id mismatch') call sph_coupling_get_facet_count(count, ierr) call assert_ok(ierr, 'coupling facet count failed') if (count /= 0) call fail('0.1.1 should not export real facets') call sph_coupling_export_facet(2, nodes, ierr) if (ierr == SPH_SUCCESS) call fail('facet export error path did not fail') force = 1.0_rk call sph_coupling_import_particle_force(1, force, ierr) call assert_ok(ierr, 'coupling particle force import failed') call sph_coupling_import_facet_load(1, force, ierr) call assert_ok(ierr, 'coupling facet load import failed') call sph_coupling_get_diagnostics(work, energy, momentum) if (work <= 0.0_rk .or. energy <= 0.0_rk) call fail('coupling diagnostics not updated') if (coupling_type /= 0) call fail('default coupling type should remain neutral') call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed') end subroutine check_legacy_region subroutine check_water_runtime_state() integer :: ierr integer :: count integer :: ext_id integer :: phase integer :: material real(rk) :: position(3) real(rk) :: velocity(3) real(rk) :: particle_pressure real(rk) :: force(3) real(rk) :: work real(rk) :: energy real(rk) :: momentum(3) call sph_initialize(ierr) call assert_ok(ierr, 'initialize water failed') call sph_read_inp('tests/data/water_minimal_020.inp', ierr) call assert_ok(ierr, 'read water input failed') call sph_build_model(ierr) call assert_ok(ierr, 'build water model failed') call sph_advance_explicit(1.0e-6_rk, ierr) call assert_ok(ierr, 'advance water model failed') call sph_coupling_get_particle_count(count, ierr) call assert_ok(ierr, 'water coupling particle count failed') if (count /= 4) call fail('water coupling particle count mismatch') call sph_coupling_export_particle_state(1, ext_id, phase, material, & position, velocity, particle_pressure, ierr) call assert_ok(ierr, 'water coupling particle state export failed') if (ext_id /= 1) call fail('water coupling export id mismatch') if (phase /= SPH_PHASE_WATER) call fail('water coupling phase mismatch') if (material <= 0) call fail('water coupling material mismatch') if (.not. is_finite_vector(position)) call fail('water coupling position is not finite') if (.not. is_finite_vector(velocity)) call fail('water coupling velocity is not finite') if (.not. is_finite(particle_pressure)) call fail('water coupling pressure is not finite') force = (/0.0_rk, 0.0_rk, 2.0_rk/) call sph_coupling_import_particle_force(1, force, ierr) call assert_ok(ierr, 'water coupling force import failed') call sph_advance_explicit(1.0e-6_rk, ierr) call assert_ok(ierr, 'advance after coupling force failed') call sph_coupling_export_particle_state(1, ext_id, phase, material, & position, velocity, particle_pressure, ierr) call assert_ok(ierr, 'water coupling export after force failed') if (velocity(3) <= 0.0_rk) call fail('coupling force did not affect explicit step') if (.not. is_finite_vector(position)) call fail('water coupling position after force is not finite') if (.not. is_finite_vector(velocity)) call fail('water coupling velocity after force is not finite') if (.not. is_finite(particle_pressure)) call fail('water coupling pressure is not finite') call sph_coupling_import_facet_load(1, force, ierr) call assert_ok(ierr, 'water coupling facet load import failed') call sph_coupling_get_diagnostics(work, energy, momentum) if (work <= 0.0_rk) call fail('coupling work placeholder not updated') if (energy <= 0.0_rk) call fail('coupling energy placeholder not updated') if (momentum(3) <= 0.0_rk) call fail('coupling momentum placeholder not updated') if (coupling_type /= 0) call fail('water coupling type should remain neutral') call sph_finalize(ierr) call assert_ok(ierr, 'finalize water failed') end subroutine check_water_runtime_state subroutine assert_ok(ierr_value, message) integer, intent(in) :: ierr_value character(len=*), intent(in) :: message if (ierr_value /= SPH_SUCCESS) call fail(message) end subroutine assert_ok subroutine fail(message) character(len=*), intent(in) :: message write(*, '(A)') trim(message) stop 1 end subroutine fail logical function is_finite(value) real(rk), intent(in) :: value is_finite = (value == value .and. abs(value) < huge(1.0_rk)) end function is_finite logical function is_finite_vector(value) real(rk), intent(in) :: value(:) is_finite_vector = all(value == value) .and. maxval(abs(value)) < huge(value(1)) end function is_finite_vector end program test_coupling_interface