program test_coupling_solver_order_080 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 use sph_database, only: v, mass use sph_blast_load, only: blast_solid_external_force use sph_coupling_interface, only: coupling_external_force, & coupling_step_load_consumed, sph_coupling_import_force_by_region_index, & sph_coupling_get_extended_diagnostics implicit none integer :: ierr call check_blast_and_coupling_superpose() call check_step_local_load_is_single_use() contains subroutine check_blast_and_coupling_superpose() real(rk) :: blast_velocity real(rk) :: coupled_velocity real(rk) :: blast_cache_before(SPH_NDIM) real(rk) :: force(SPH_NDIM) call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed for Blast baseline') call sph_read_inp('tests/data/blast_water_gas_explosive_solid_070.inp', ierr) call assert_ok(ierr, 'read failed for Blast baseline') call sph_build_model(ierr) call assert_ok(ierr, 'build failed for Blast baseline') call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'advance failed for Blast baseline') blast_velocity = v(1,5) call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed for Blast baseline') call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed for coupled Blast') call sph_read_inp('tests/data/coupling_blast_solid_080.inp', ierr) call assert_ok(ierr, 'read failed for coupled Blast') call sph_build_model(ierr) call assert_ok(ierr, 'build failed for coupled Blast') if (maxval(abs(blast_solid_external_force(:,5))) <= 0.0_rk) then call fail('Blast cache missing before Coupling import') end if blast_cache_before = blast_solid_external_force(:,5) force = (/mass(5) * 1.0e8_rk, 0.0_rk, 0.0_rk/) call sph_coupling_import_force_by_region_index('COUPLED_SOLID', 1, & force, ierr) call assert_ok(ierr, 'coupled force import failed') call assert_vector_close(blast_solid_external_force(:,5), & blast_cache_before, 'Coupling import changed Blast cache') if (maxval(abs(coupling_external_force(:,5))) <= 0.0_rk) then call fail('Coupling cache missing before advance') end if call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'advance failed for coupled Blast') coupled_velocity = v(1,5) if (coupled_velocity <= blast_velocity) then call fail('Coupling and Blast did not superpose in explicit step') end if if (maxval(abs(blast_solid_external_force(:,5))) <= 0.0_rk) then call fail('Blast cache missing after coupled advance') end if if (maxval(abs(coupling_external_force)) > 0.0_rk) then call fail('Coupling force cache was reused after advance') end if if (coupling_step_load_consumed /= 1) then call fail('Coupling step load was not marked consumed') end if call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed for coupled Blast') end subroutine check_blast_and_coupling_superpose subroutine check_step_local_load_is_single_use() real(rk) :: force(SPH_NDIM) real(rk) :: velocity_after_force real(rk) :: velocity_after_empty_step 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 call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed for single-use load') call sph_read_inp('tests/data/coupling_region_080.inp', ierr) call assert_ok(ierr, 'read failed for single-use load') call sph_build_model(ierr) call assert_ok(ierr, 'build failed for single-use load') force = (/0.0_rk, mass(1) * 1.0e8_rk, 0.0_rk/) call sph_coupling_import_force_by_region_index('CONTACT_PARTICLES', 1, & force, ierr) call assert_ok(ierr, 'single-use force import failed') call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'single-use first advance failed') velocity_after_force = v(2,1) if (velocity_after_force <= 0.0_rk) call fail('single-use force missing') 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) call assert_ok(ierr, 'single-use diagnostics failed') if (step_load_consumed /= 1) call fail('single-use load not consumed') call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'single-use empty advance failed') velocity_after_empty_step = v(2,1) if (velocity_after_empty_step - velocity_after_force > & velocity_after_force * 0.25_rk) then call fail('Coupling load was applied again without reimport') end if 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) call assert_ok(ierr, 'empty-step diagnostics failed') if (maxval(abs(total_force)) > 0.0_rk) then call fail('empty step reported Coupling force') end if if (step_load_consumed /= 0) call fail('empty step reported consumed load') call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed for single-use load') end subroutine check_step_local_load_is_single_use 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_solver_order_080