Files
SPHLite/tests/module/test_coupling_restart_081.f90
2026-07-04 11:43:00 +08:00

187 lines
7.6 KiB
Fortran

program test_coupling_restart_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, sph_write_restart, sph_read_restart
use sph_database, only: SPH_COUPLING_LOAD_PRESSURE, &
SPH_COUPLING_DISTRIBUTION_AREA, SPH_COUPLING_CONTACT_STATE_IMPORT
use sph_coupling_interface, only: coupling_external_force, &
coupling_contact_flag, coupling_contact_gap, coupling_contact_penetration, &
coupling_contact_pressure, coupling_facet_force_error, &
coupling_max_pressure, coupling_max_penetration, &
sph_coupling_apply_facet_pressures, sph_coupling_apply_contact_state, &
sph_coupling_get_region_modes, sph_coupling_get_load_balance, &
sph_coupling_get_contact_diagnostics
implicit none
integer :: ierr
call check_restart_081_round_trip()
contains
subroutine check_restart_081_round_trip()
integer :: ids(1)
real(rk) :: pressures(1)
real(rk) :: normals(SPH_NDIM,1)
real(rk) :: gaps(1)
real(rk) :: penetrations(1)
real(rk) :: contact_pressures(1)
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
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 delete_file_if_exists('build-codex-unix/restart_coupling_081.tmp')
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for restart 081')
call sph_read_inp('tests/data/coupling_load_mode_081.inp', ierr)
call assert_ok(ierr, 'read failed for restart 081')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for restart 081')
ids(1) = 1
pressures(1) = 15.0_rk
normals(:,1) = (/0.0_rk, 0.0_rk, 1.0_rk/)
gaps(1) = 3.0e-4_rk
penetrations(1) = 4.0e-3_rk
contact_pressures(1) = 11.0_rk
call sph_coupling_apply_facet_pressures(1, 1, ids, pressures, &
SPH_COUPLING_DISTRIBUTION_AREA, 91, ierr)
call assert_ok(ierr, 'restart 081 pressure import failed')
call sph_coupling_apply_contact_state(1, 1, ids, normals, gaps, &
penetrations, contact_pressures, 91, ierr)
call assert_ok(ierr, 'restart 081 contact import failed')
call sph_write_restart('build-codex-unix/restart_coupling_081.tmp', ierr)
call assert_ok(ierr, 'restart 081 write failed')
call assert_restart_state_version('build-codex-unix/restart_coupling_081.tmp')
call scramble_coupling_state()
call sph_read_restart('build-codex-unix/restart_coupling_081.tmp', ierr)
call assert_ok(ierr, 'restart 081 read failed')
call sph_coupling_get_region_modes(1, load_mode, distribution_mode, &
contact_state_mode, ierr)
call assert_ok(ierr, 'restart 081 mode query failed')
if (load_mode /= SPH_COUPLING_LOAD_PRESSURE) call fail('restart 081 load mode mismatch')
if (distribution_mode /= SPH_COUPLING_DISTRIBUTION_AREA) then
call fail('restart 081 distribution mode mismatch')
end if
if (contact_state_mode /= SPH_COUPLING_CONTACT_STATE_IMPORT) then
call fail('restart 081 contact state mode mismatch')
end if
if (coupling_contact_flag(1) /= 1) call fail('restart 081 contact flag mismatch')
call assert_close(coupling_contact_gap(1), gaps(1), 'restart 081 gap mismatch')
call assert_close(coupling_contact_penetration(1), penetrations(1), &
'restart 081 penetration mismatch')
call assert_close(coupling_contact_pressure(1), contact_pressures(1), &
'restart 081 contact pressure mismatch')
call assert_close(coupling_max_pressure, pressures(1), &
'restart 081 max pressure mismatch')
call assert_close(coupling_max_penetration, penetrations(1), &
'restart 081 max penetration mismatch')
call assert_close(coupling_facet_force_error, 0.0_rk, &
'restart 081 force balance error mismatch')
call sph_coupling_get_load_balance(input_force, distributed_force, &
force_error, input_moment, distributed_moment, moment_error, ierr)
call assert_ok(ierr, 'restart 081 load balance query failed')
call assert_close(force_error, 0.0_rk, 'restart 081 load force error mismatch')
call assert_close(moment_error, 0.0_rk, 'restart 081 load moment error mismatch')
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, 'restart 081 contact diagnostic query failed')
if (active_count /= 1 .or. skipped_count /= 0) then
call fail('restart 081 contact diagnostic count mismatch')
end if
call assert_close(max_penetration, penetrations(1), &
'restart 081 diagnostic penetration mismatch')
if (maxval(abs(coupling_external_force(:,1))) <= 0.0_rk) then
call fail('restart 081 external force was not restored')
end if
call sph_advance_explicit(1.0e-7_rk, ierr)
call assert_ok(ierr, 'restart 081 continuation failed')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for restart 081')
end subroutine check_restart_081_round_trip
subroutine scramble_coupling_state()
coupling_external_force = -999.0_rk
coupling_contact_flag = -999
coupling_contact_gap = -999.0_rk
coupling_contact_penetration = -999.0_rk
coupling_contact_pressure = -999.0_rk
coupling_facet_force_error = -999.0_rk
coupling_max_pressure = -999.0_rk
coupling_max_penetration = -999.0_rk
end subroutine scramble_coupling_state
subroutine assert_restart_state_version(filename)
character(len=*), intent(in) :: filename
character(len=64) :: tag
character(len=32) :: state_version
integer :: major
integer :: minor
integer :: patch
integer :: ios
open(unit=91, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('restart 081 header open failed')
read(91, *, iostat=ios) tag, major, minor, patch, state_version
close(unit=91)
if (ios /= 0) call fail('restart 081 header read failed')
if (trim(tag) /= 'SPHLITE_RESTART') call fail('restart 081 header tag mismatch')
if (trim(state_version) /= 'STATE_0_8_1') call fail('restart 081 state mismatch')
end subroutine assert_restart_state_version
subroutine delete_file_if_exists(filename)
character(len=*), intent(in) :: filename
logical :: exists
integer :: ios
inquire(file=trim(filename), exist=exists)
if (.not. exists) return
open(unit=92, file=trim(filename), status='old', iostat=ios)
if (ios == 0) close(unit=92, status='delete')
end subroutine delete_file_if_exists
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 fail(message)
character(len=*), intent(in) :: message
write(*, '(A)') trim(message)
stop 1
end subroutine fail
end program test_coupling_restart_081