Files
SPHLite/tests/module/test_coupling_smoke_081.f90
2026-07-04 12:06:47 +08:00

312 lines
12 KiB
Fortran

program test_coupling_smoke_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, sph_advance_explicit
use sph_database, only: npart, x, v, a, mass, rho, volume, pressure, &
internal_energy, temperature, h, SPH_COUPLING_DISTRIBUTION_AREA, &
SPH_COUPLING_DISTRIBUTION_MASS
use sph_coupling_interface, only: SPH_COUPLING_MOTION_DIRECT, &
SPH_COUPLING_MOTION_VELOCITY, coupling_max_pressure, &
coupling_max_traction, sph_coupling_apply_particle_forces, &
sph_coupling_apply_facet_pressures, &
sph_coupling_apply_facet_tractions, sph_coupling_apply_contact_state, &
sph_coupling_apply_motion_boundary, sph_coupling_clear_imports, &
sph_coupling_get_load_balance, sph_coupling_get_contact_diagnostics
implicit none
integer :: ierr
call check_solid_surface_pressure()
call check_shell_pressure()
call check_shell_traction()
call check_pressure_motion()
call check_stale_epoch()
contains
subroutine check_solid_surface_pressure()
integer :: ids(1)
real(rk) :: forces(SPH_NDIM,1)
real(rk) :: pressure_value
real(rk) :: proxy_area
call build_case('tests/data/coupling_mock_solid_surface_pressure_081.inp')
ids(1) = 1
pressure_value = 2.0e5_rk
proxy_area = 1.0e-3_rk
forces(:,1) = (/ 0.0_rk, 0.0_rk, pressure_value * proxy_area /)
call sph_coupling_apply_particle_forces(1, 1, ids, forces, ierr)
call assert_ok(ierr, 'solid pressure mock import failed')
call sph_advance_explicit(1.0e-7_rk, ierr)
call assert_ok(ierr, 'solid pressure mock advance failed')
if (v(3,1) <= 0.0_rk) call fail('solid pressure direction mismatch')
call assert_model_finite('solid pressure mock')
call finish_case('solid pressure mock')
end subroutine check_solid_surface_pressure
subroutine check_shell_pressure()
integer :: ids(3)
integer :: contact_ids(2)
real(rk) :: pressures(3)
real(rk) :: normals(SPH_NDIM,2)
real(rk) :: gaps(2)
real(rk) :: penetrations(2)
real(rk) :: contact_pressures(2)
call build_case('tests/data/coupling_mock_shell_pressure_081.inp')
ids = (/ 1, 2, 3 /)
pressures = (/ 60.0_rk, 40.0_rk, 20.0_rk /)
call sph_coupling_apply_facet_pressures(1, 3, ids, pressures, &
SPH_COUPLING_DISTRIBUTION_AREA, 811, ierr)
call assert_ok(ierr, 'shell pressure mock import failed')
contact_ids = (/ 1, 2 /)
normals(:,1) = (/ 0.0_rk, 0.0_rk, 1.0_rk /)
normals(:,2) = (/ 0.0_rk, 0.0_rk, 1.0_rk /)
gaps = (/ 1.0e-4_rk, 2.0e-4_rk /)
penetrations = (/ 1.0e-3_rk, 2.0e-3_rk /)
contact_pressures = (/ 6.0_rk, 4.0_rk /)
call sph_coupling_apply_contact_state(1, 2, contact_ids, normals, &
gaps, penetrations, contact_pressures, 811, ierr)
call assert_ok(ierr, 'shell pressure contact import failed')
call assert_load_balance_finite('shell pressure mock')
call assert_contact_count(2, 0, 'shell pressure mock')
if (coupling_max_pressure < 60.0_rk) call fail('max pressure missing')
call sph_advance_explicit(1.0e-7_rk, ierr)
call assert_ok(ierr, 'shell pressure mock advance failed')
if (v(3,1) <= 0.0_rk .or. v(3,2) <= 0.0_rk) then
call fail('shell pressure direction mismatch')
end if
call assert_model_finite('shell pressure mock')
call finish_case('shell pressure mock')
end subroutine check_shell_pressure
subroutine check_shell_traction()
integer :: ids(2)
real(rk) :: tractions(SPH_NDIM,2)
call build_case('tests/data/coupling_mock_traction_081.inp')
ids = (/ 1, 2 /)
tractions(:,1) = (/ 0.0_rk, 120.0_rk, 0.0_rk /)
tractions(:,2) = (/ 0.0_rk, 80.0_rk, 0.0_rk /)
call sph_coupling_apply_facet_tractions(1, 2, ids, tractions, &
SPH_COUPLING_DISTRIBUTION_MASS, 821, ierr)
call assert_ok(ierr, 'traction mock import failed')
call assert_load_balance_finite('traction mock')
if (coupling_max_traction < 120.0_rk) call fail('max traction missing')
call sph_advance_explicit(1.0e-7_rk, ierr)
call assert_ok(ierr, 'traction mock advance failed')
if (v(2,1) <= 0.0_rk .or. v(2,2) <= 0.0_rk) then
call fail('traction direction mismatch')
end if
call assert_model_finite('traction mock')
call finish_case('traction mock')
end subroutine check_shell_traction
subroutine check_pressure_motion()
integer :: ids(2)
real(rk) :: pressures(2)
real(rk) :: target_velocity(SPH_NDIM)
call build_case('tests/data/coupling_mock_pressure_motion_081.inp')
ids = (/ 1, 2 /)
pressures = (/ 50.0_rk, 50.0_rk /)
call sph_coupling_apply_facet_pressures(1, 2, ids, pressures, &
SPH_COUPLING_DISTRIBUTION_AREA, 831, ierr)
call assert_ok(ierr, 'pressure-motion pressure import failed')
target_velocity = (/ 1.25_rk, 0.0_rk, 0.0_rk /)
call sph_coupling_apply_motion_boundary(2, 1, &
SPH_COUPLING_MOTION_VELOCITY, SPH_COUPLING_MOTION_DIRECT, &
target_velocity, ierr)
call assert_ok(ierr, 'pressure-motion boundary import failed')
call sph_advance_explicit(1.0e-7_rk, ierr)
call assert_ok(ierr, 'pressure-motion advance failed')
call assert_vector_close(v(:,1), target_velocity, &
'motion boundary target mismatch')
if (v(3,2) <= 0.0_rk) call fail('pressure-motion free particle mismatch')
call assert_model_finite('pressure-motion mock')
call finish_case('pressure-motion mock')
end subroutine check_pressure_motion
subroutine check_stale_epoch()
integer :: ids(1)
real(rk) :: normals(SPH_NDIM,1)
real(rk) :: gaps(1)
real(rk) :: penetrations(1)
real(rk) :: contact_pressures(1)
real(rk) :: pressures(1)
integer :: stale_after_contact
integer :: stale_after_pressure
call build_case('tests/data/coupling_mock_stale_epoch_081.inp')
ids(1) = 1
normals(:,1) = (/ 0.0_rk, 0.0_rk, 1.0_rk /)
gaps(1) = 1.0e-4_rk
penetrations(1) = 1.0e-3_rk
contact_pressures(1) = 5.0_rk
call sph_coupling_apply_contact_state(1, 1, ids, normals, gaps, &
penetrations, contact_pressures, 841, ierr)
call assert_ok(ierr, 'stale contact import failed')
call sph_coupling_clear_imports(ierr)
call assert_ok(ierr, 'stale contact clear failed')
call read_stale_count(stale_after_contact, 'stale contact')
if (stale_after_contact <= 0) call fail('stale contact was not counted')
pressures(1) = 30.0_rk
call sph_coupling_apply_facet_pressures(1, 1, ids, pressures, &
SPH_COUPLING_DISTRIBUTION_AREA, 842, ierr)
call assert_ok(ierr, 'stale pressure import failed')
call sph_coupling_clear_imports(ierr)
call assert_ok(ierr, 'stale pressure clear failed')
call read_stale_count(stale_after_pressure, 'stale pressure')
if (stale_after_pressure <= stale_after_contact) then
call fail('stale pressure did not change diagnostics')
end if
call assert_model_finite('stale epoch mock')
call finish_case('stale epoch mock')
end subroutine check_stale_epoch
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 assert_load_balance_finite(label)
character(len=*), intent(in) :: label
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
call sph_coupling_get_load_balance(input_force, distributed_force, &
force_error, input_moment, distributed_moment, moment_error, ierr)
call assert_ok(ierr, trim(label) // ' load balance failed')
call assert_vector_finite(input_force, trim(label) // ' input force bad')
call assert_vector_finite(distributed_force, trim(label) // ' force bad')
call assert_vector_finite(input_moment, trim(label) // ' input moment bad')
call assert_vector_finite(distributed_moment, trim(label) // ' moment bad')
if (.not. scalar_is_finite(force_error)) call fail(trim(label) // ' force error bad')
if (.not. scalar_is_finite(moment_error)) call fail(trim(label) // ' moment error bad')
end subroutine assert_load_balance_finite
subroutine assert_contact_count(expected_active, expected_skipped, label)
integer, intent(in) :: expected_active
integer, intent(in) :: expected_skipped
character(len=*), intent(in) :: label
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_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, trim(label) // ' contact diagnostics failed')
if (active_count /= expected_active) call fail(trim(label) // ' active count bad')
if (skipped_count /= expected_skipped) call fail(trim(label) // ' skipped count bad')
if (.not. scalar_is_finite(max_penetration)) then
call fail(trim(label) // ' max penetration bad')
end if
end subroutine assert_contact_count
subroutine read_stale_count(stale_count, label)
integer, intent(out) :: stale_count
character(len=*), intent(in) :: label
integer :: active_count
integer :: skipped_count
integer :: repeated_count
integer :: nonfinite_count
integer :: bad_area_count
integer :: invalid_weight_count
real(rk) :: max_penetration
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, trim(label) // ' stale diagnostics failed')
if (.not. scalar_is_finite(max_penetration)) then
call fail(trim(label) // ' stale penetration bad')
end if
end subroutine read_stale_count
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 bad')
call assert_vector_finite(v(:,i), trim(label) // ' velocity bad')
call assert_vector_finite(a(:,i), trim(label) // ' acceleration bad')
if (.not. scalar_is_finite(mass(i))) call fail(trim(label) // ' mass bad')
if (.not. scalar_is_finite(rho(i))) call fail(trim(label) // ' density bad')
if (.not. scalar_is_finite(volume(i))) call fail(trim(label) // ' volume bad')
if (.not. scalar_is_finite(pressure(i))) call fail(trim(label) // ' pressure bad')
if (.not. scalar_is_finite(internal_energy(i))) call fail(trim(label) // ' energy bad')
if (.not. scalar_is_finite(temperature(i))) call fail(trim(label) // ' temp bad')
if (.not. scalar_is_finite(h(i))) call fail(trim(label) // ' h bad')
end do
end subroutine assert_model_finite
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
if (abs(actual(i) - expected(i)) > max(1.0e-10_rk, abs(expected(i)) * 1.0e-8_rk)) then
call fail(message)
end if
end do
end subroutine assert_vector_close
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
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_081