program test_coupling_smoke_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: npart, x, v, a, mass, rho, volume, pressure, & internal_energy, temperature, h, particle_status, SPH_STATUS_INACTIVE use sph_coupling_interface, only: coupling_stale_load_count, & coupling_skipped_particle_count, SPH_COUPLING_MOTION_DIRECT, & sph_coupling_import_force_by_region_index, & sph_coupling_import_region_facet_load, & sph_coupling_import_motion_velocity, sph_coupling_clear_imports, & sph_coupling_get_extended_diagnostics implicit none integer :: ierr real(rk) :: diag_force(SPH_NDIM) real(rk) :: diag_moment(SPH_NDIM) real(rk) :: diag_impulse(SPH_NDIM) real(rk) :: diag_cumulative_impulse(SPH_NDIM) real(rk) :: diag_momentum(SPH_NDIM) real(rk) :: diag_work real(rk) :: diag_cumulative_work real(rk) :: diag_energy real(rk) :: diag_max_force real(rk) :: diag_max_acceleration real(rk) :: diag_velocity_error real(rk) :: diag_position_error integer :: diag_stale integer :: diag_skipped integer :: diag_nonfinite integer :: diag_imported_force integer :: diag_step_consumed call check_solid_contact_smoke() call check_shell_contact_smoke() call check_mixed_contact_smoke() call check_disabled_region_smoke() call check_no_active_region_smoke() call check_stale_and_repeated_import_smoke() contains subroutine check_solid_contact_smoke() real(rk) :: force(SPH_NDIM) call build_case('tests/data/coupling_mock_solid_080.inp') force = (/mass(1) * 1.0e8_rk, 0.0_rk, 0.0_rk/) call sph_coupling_import_force_by_region_index('SOLID_CONTACT_SMOKE', & 1, force, ierr) call assert_ok(ierr, 'Solid smoke force import failed') call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'Solid smoke advance failed') call collect_diagnostics('Solid smoke') if (v(1,1) <= 0.0_rk .or. a(1,1) <= 0.0_rk) then call fail('Solid smoke response direction mismatch') end if if (diag_force(1) <= 0.0_rk .or. diag_impulse(1) <= 0.0_rk) then call fail('Solid smoke force or impulse diagnostic missing') end if call assert_diag_finite('Solid smoke') call assert_model_finite('Solid smoke') call finish_case('Solid smoke') end subroutine check_solid_contact_smoke subroutine check_shell_contact_smoke() real(rk) :: load(SPH_NDIM) call build_case('tests/data/coupling_mock_shell_080.inp') load = (/0.0_rk, mass(1) * 1.0e8_rk, 0.0_rk/) call sph_coupling_import_region_facet_load('SHELL_CONTACT_SMOKE', & 1, load, ierr) call assert_ok(ierr, 'Shell smoke facet load import failed') call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'Shell smoke advance failed') call collect_diagnostics('Shell smoke') if (v(2,1) <= 0.0_rk .or. a(2,1) <= 0.0_rk) then call fail('Shell smoke translation direction mismatch') end if if (diag_force(2) <= 0.0_rk .or. diag_momentum(2) <= 0.0_rk) then call fail('Shell smoke force or momentum diagnostic missing') end if call assert_diag_finite('Shell smoke') call assert_model_finite('Shell smoke') call finish_case('Shell smoke') end subroutine check_shell_contact_smoke subroutine check_mixed_contact_smoke() real(rk) :: solid_force(SPH_NDIM) real(rk) :: shell_load(SPH_NDIM) real(rk) :: target_velocity(SPH_NDIM) call build_case('tests/data/coupling_mock_mixed_080.inp') shell_load = (/0.0_rk, mass(1) * 5.0e7_rk, 0.0_rk/) call sph_coupling_import_region_facet_load('MIXED_SHELL_FACETS', & 1, shell_load, ierr) call assert_ok(ierr, 'Mixed shell facet load import failed') solid_force = (/mass(4) * 8.0e7_rk, 0.0_rk, 0.0_rk/) call sph_coupling_import_force_by_region_index('MIXED_SOLID_CONTACT', & 1, solid_force, ierr) call assert_ok(ierr, 'Mixed solid force import failed') target_velocity = (/0.0_rk, 0.0_rk, 3.0_rk/) call sph_coupling_import_motion_velocity('MIXED_SHELL_MOTION', 2, & SPH_COUPLING_MOTION_DIRECT, target_velocity, ierr) call assert_ok(ierr, 'Mixed shell motion import failed') call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'Mixed smoke advance failed') call collect_diagnostics('Mixed smoke') if (v(2,1) <= 0.0_rk) call fail('Mixed Shell force direction mismatch') if (v(1,4) <= 0.0_rk) call fail('Mixed Solid force direction mismatch') call assert_close(v(3,2), target_velocity(3), & 'Mixed motion velocity target mismatch') if (diag_force(1) <= 0.0_rk .or. diag_force(2) <= 0.0_rk) then call fail('Mixed force diagnostics missing') end if call assert_diag_finite('Mixed smoke') call assert_model_finite('Mixed smoke') call finish_case('Mixed smoke') end subroutine check_mixed_contact_smoke subroutine check_disabled_region_smoke() real(rk) :: force(SPH_NDIM) call build_case('tests/data/coupling_mock_disabled_080.inp') force = (/1.0e8_rk, 0.0_rk, 0.0_rk/) call sph_coupling_import_force_by_region_index('DISABLED_SMOKE', & 1, force, ierr) if (ierr == SPH_SUCCESS) call fail('disabled smoke import succeeded') call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'disabled smoke advance failed') call collect_diagnostics('disabled smoke') call assert_zero_diagnostics('disabled smoke') if (maxval(abs(v)) > 1.0e-12_rk) then call fail('disabled smoke produced velocity response') end if call assert_model_finite('disabled smoke') call finish_case('disabled smoke') end subroutine check_disabled_region_smoke subroutine check_no_active_region_smoke() real(rk) :: force(SPH_NDIM) call build_case('tests/data/coupling_mock_no_active_080.inp') particle_status = SPH_STATUS_INACTIVE force = (/0.0_rk, 1.0e8_rk, 0.0_rk/) call sph_coupling_import_force_by_region_index('NO_ACTIVE_CONTACT', & 1, force, ierr) call assert_ok(ierr, 'no-active smoke import failed') if (coupling_skipped_particle_count <= 0) then call fail('no-active smoke skipped count missing') end if call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'no-active smoke advance failed') call collect_diagnostics('no-active smoke') call assert_zero_vector(diag_force, 'no-active smoke force changed') if (maxval(abs(v)) > 1.0e-12_rk) then call fail('no-active smoke produced velocity response') end if if (diag_skipped <= 0) call fail('no-active diagnostic skipped count missing') call assert_diag_finite('no-active smoke') call assert_model_finite('no-active smoke') call finish_case('no-active smoke') end subroutine check_no_active_region_smoke subroutine check_stale_and_repeated_import_smoke() real(rk) :: stale_force(SPH_NDIM) real(rk) :: force_a(SPH_NDIM) real(rk) :: force_b(SPH_NDIM) real(rk) :: expected(SPH_NDIM) call build_case('tests/data/coupling_mock_stale_080.inp') stale_force = (/1.0e8_rk, 0.0_rk, 0.0_rk/) call sph_coupling_import_force_by_region_index('STALE_CONTACT', & 1, stale_force, ierr) call assert_ok(ierr, 'stale smoke initial force import failed') call sph_coupling_clear_imports(ierr) call assert_ok(ierr, 'stale smoke clear failed') if (coupling_stale_load_count /= 1) then call fail('stale smoke did not count cleared load') end if force_a = (/0.0_rk, mass(1) * 4.0e7_rk, 0.0_rk/) force_b = (/0.0_rk, mass(1) * 2.0e7_rk, 0.0_rk/) expected = force_a + force_b call sph_coupling_import_force_by_region_index('STALE_CONTACT', & 1, force_a, ierr) call assert_ok(ierr, 'stale smoke repeated import A failed') call sph_coupling_import_force_by_region_index('STALE_CONTACT', & 1, force_b, ierr) call assert_ok(ierr, 'stale smoke repeated import B failed') call sph_advance_explicit(1.0e-7_rk, ierr) call assert_ok(ierr, 'stale smoke advance failed') call collect_diagnostics('stale smoke') call assert_close(diag_force(2), expected(2), & 'stale smoke repeated force diagnostic mismatch') if (v(2,1) <= 0.0_rk) call fail('stale smoke response direction mismatch') if (diag_stale /= 1) call fail('stale smoke diagnostic count mismatch') call assert_diag_finite('stale smoke') call assert_model_finite('stale smoke') call finish_case('stale smoke') end subroutine check_stale_and_repeated_import_smoke subroutine build_case(filename) character(len=*), intent(in) :: filename call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed') call sph_read_inp(filename, ierr) call assert_ok(ierr, trim(filename) // ' read failed') call sph_build_model(ierr) call assert_ok(ierr, trim(filename) // ' build failed') end subroutine build_case subroutine finish_case(label) character(len=*), intent(in) :: label call sph_finalize(ierr) call assert_ok(ierr, trim(label) // ' finalize failed') end subroutine finish_case subroutine collect_diagnostics(label) character(len=*), intent(in) :: label call sph_coupling_get_extended_diagnostics(diag_force, diag_moment, & diag_impulse, diag_cumulative_impulse, diag_work, & diag_cumulative_work, diag_momentum, diag_energy, diag_max_force, & diag_max_acceleration, diag_velocity_error, diag_position_error, & diag_stale, diag_skipped, diag_nonfinite, diag_imported_force, & diag_step_consumed, ierr) call assert_ok(ierr, trim(label) // ' diagnostics failed') end subroutine collect_diagnostics subroutine assert_model_finite(label) character(len=*), intent(in) :: label integer :: i do i = 1, npart call assert_vector_finite(x(:,i), trim(label) // ' position is not finite') call assert_vector_finite(v(:,i), trim(label) // ' velocity is not finite') call assert_vector_finite(a(:,i), trim(label) // ' acceleration is not finite') if (.not. scalar_is_finite(mass(i))) call fail(trim(label) // ' mass is not finite') if (.not. scalar_is_finite(rho(i))) call fail(trim(label) // ' density is not finite') if (.not. scalar_is_finite(volume(i))) call fail(trim(label) // ' volume is not finite') if (.not. scalar_is_finite(pressure(i))) call fail(trim(label) // ' pressure is not finite') if (.not. scalar_is_finite(internal_energy(i))) then call fail(trim(label) // ' internal energy is not finite') end if if (.not. scalar_is_finite(temperature(i))) then call fail(trim(label) // ' temperature is not finite') end if if (.not. scalar_is_finite(h(i))) call fail(trim(label) // ' h is not finite') end do end subroutine assert_model_finite subroutine assert_diag_finite(label) character(len=*), intent(in) :: label call assert_vector_finite(diag_force, trim(label) // ' force diagnostic is not finite') call assert_vector_finite(diag_moment, trim(label) // ' moment diagnostic is not finite') call assert_vector_finite(diag_impulse, trim(label) // ' impulse diagnostic is not finite') call assert_vector_finite(diag_cumulative_impulse, & trim(label) // ' cumulative impulse diagnostic is not finite') call assert_vector_finite(diag_momentum, trim(label) // ' momentum diagnostic is not finite') if (.not. scalar_is_finite(diag_work)) call fail(trim(label) // ' work is not finite') if (.not. scalar_is_finite(diag_cumulative_work)) then call fail(trim(label) // ' cumulative work is not finite') end if if (.not. scalar_is_finite(diag_energy)) then call fail(trim(label) // ' energy exchange is not finite') end if if (.not. scalar_is_finite(diag_max_force)) call fail(trim(label) // ' max force is not finite') if (.not. scalar_is_finite(diag_max_acceleration)) then call fail(trim(label) // ' max acceleration is not finite') end if if (.not. scalar_is_finite(diag_velocity_error)) then call fail(trim(label) // ' velocity error is not finite') end if if (.not. scalar_is_finite(diag_position_error)) then call fail(trim(label) // ' position error is not finite') end if end subroutine assert_diag_finite subroutine assert_zero_diagnostics(label) character(len=*), intent(in) :: label call assert_zero_vector(diag_force, trim(label) // ' force diagnostic changed') call assert_zero_vector(diag_impulse, trim(label) // ' impulse diagnostic changed') call assert_zero_vector(diag_momentum, trim(label) // ' momentum diagnostic changed') if (diag_max_force /= 0.0_rk .or. diag_max_acceleration /= 0.0_rk) then call fail(trim(label) // ' max response diagnostic changed') end if if (diag_stale /= 0 .or. diag_skipped /= 0 .or. diag_nonfinite /= 0) then call fail(trim(label) // ' counters changed') end if call assert_diag_finite(label) end subroutine assert_zero_diagnostics subroutine assert_vector_finite(values, message) real(rk), intent(in) :: values(SPH_NDIM) character(len=*), intent(in) :: message integer :: i do i = 1, SPH_NDIM if (.not. scalar_is_finite(values(i))) call fail(message) end do end subroutine assert_vector_finite subroutine assert_zero_vector(values, message) real(rk), intent(in) :: values(SPH_NDIM) character(len=*), intent(in) :: message if (maxval(abs(values)) > 1.0e-12_rk) call fail(message) end subroutine assert_zero_vector 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 logical function scalar_is_finite(value) real(rk), intent(in) :: value scalar_is_finite = (value == value) .and. abs(value) < huge(value) end function scalar_is_finite 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 end program test_coupling_smoke_080