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

202 lines
8.8 KiB
Fortran

program test_coupling_restart_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, sph_write_restart, sph_read_restart, &
sph_get_last_error
use sph_coupling_interface, only: coupling_external_force, &
coupling_motion_flag, coupling_motion_mode, coupling_motion_position, &
coupling_imported_force_count, coupling_cumulative_impulse, &
coupling_cumulative_work, coupling_momentum_exchange, &
coupling_energy_exchange, sph_coupling_get_region_count, &
sph_coupling_get_region_info, sph_coupling_apply_particle_forces, &
sph_coupling_apply_facet_loads, sph_coupling_apply_motion_boundary, &
SPH_COUPLING_MOTION_POSITION, SPH_COUPLING_MOTION_DIRECT
implicit none
integer :: ierr
call check_coupling_restart_round_trip()
call check_empty_coupling_restart_round_trip()
contains
subroutine check_coupling_restart_round_trip()
integer :: indices(1)
integer :: facets(1)
real(rk) :: forces(SPH_NDIM,1)
real(rk) :: loads(SPH_NDIM,1)
real(rk) :: target(SPH_NDIM)
integer :: region_count
character(len=64) :: name
integer :: region_type
integer :: export_mode
integer :: import_mode
integer :: enabled
integer :: particle_count
integer :: facet_count
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for coupling restart')
call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr)
call assert_ok(ierr, 'read failed for coupling restart')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for coupling restart')
indices(1) = 1
forces(:,1) = (/1.0_rk, 2.0_rk, 3.0_rk/)
call sph_coupling_apply_particle_forces(2, 1, indices, forces, ierr)
call assert_ok(ierr, 'restart particle force import failed')
facets(1) = 1
loads(:,1) = (/0.0_rk, 0.0_rk, 5.0_rk/)
call sph_coupling_apply_facet_loads(1, 1, facets, loads, ierr)
call assert_ok(ierr, 'restart facet load import failed')
target = (/0.11_rk, 0.22_rk, 0.33_rk/)
call sph_coupling_apply_motion_boundary(2, 2, SPH_COUPLING_MOTION_POSITION, &
SPH_COUPLING_MOTION_DIRECT, target, ierr)
call assert_ok(ierr, 'restart motion import failed')
coupling_cumulative_impulse = (/0.1_rk, 0.2_rk, 0.3_rk/)
coupling_momentum_exchange = (/0.4_rk, 0.5_rk, 0.6_rk/)
coupling_cumulative_work = 0.7_rk
coupling_energy_exchange = 0.8_rk
call sph_write_restart('build-codex-unix/restart_coupling_080.tmp', ierr)
call assert_ok(ierr, 'coupling restart write failed')
call assert_restart_state_version('build-codex-unix/restart_coupling_080.tmp', &
'STATE_0_8_1')
call scramble_coupling_state()
call sph_read_restart('build-codex-unix/restart_coupling_080.tmp', ierr)
call assert_ok(ierr, 'coupling restart read failed')
call sph_coupling_get_region_count(region_count, ierr)
call assert_ok(ierr, 'coupling restart region count query failed')
if (region_count /= 2) call fail('coupling restart region count mismatch')
call sph_coupling_get_region_info(1, name, region_type, export_mode, &
import_mode, enabled, particle_count, facet_count, ierr)
call assert_ok(ierr, 'coupling restart region info query failed')
if (trim(name) /= 'SHELL_FACETS') call fail('coupling restart region name mismatch')
if (enabled /= 1 .or. particle_count /= 0 .or. facet_count /= 3) then
call fail('coupling restart facet region mismatch')
end if
call sph_coupling_get_region_info(2, name, region_type, export_mode, &
import_mode, enabled, particle_count, facet_count, ierr)
call assert_ok(ierr, 'coupling restart both-region info query failed')
if (trim(name) /= 'SHELL_BOTH') call fail('coupling restart both region mismatch')
if (particle_count /= 3 .or. facet_count /= 3) then
call fail('coupling restart both region counts mismatch')
end if
call assert_close(coupling_external_force(1,1), 1.0_rk, &
'coupling restart force x mismatch')
call assert_close(coupling_external_force(2,1), 2.0_rk, &
'coupling restart force y mismatch')
call assert_close(coupling_external_force(3,1), 8.0_rk, &
'coupling restart facet load cache mismatch')
if (coupling_motion_flag(2) /= SPH_COUPLING_MOTION_POSITION) then
call fail('coupling restart motion flag mismatch')
end if
if (coupling_motion_mode(2) /= SPH_COUPLING_MOTION_DIRECT) then
call fail('coupling restart motion mode mismatch')
end if
call assert_close(coupling_motion_position(3,2), 0.33_rk, &
'coupling restart motion target mismatch')
if (coupling_imported_force_count /= 2) then
call fail('coupling restart imported force count mismatch')
end if
call assert_close(coupling_cumulative_impulse(2), 0.2_rk, &
'coupling restart cumulative impulse mismatch')
call assert_close(coupling_momentum_exchange(3), 0.6_rk, &
'coupling restart momentum exchange mismatch')
call assert_close(coupling_cumulative_work, 0.7_rk, &
'coupling restart cumulative work mismatch')
call assert_close(coupling_energy_exchange, 0.8_rk, &
'coupling restart energy exchange mismatch')
call sph_advance_explicit(1.0e-7_rk, ierr)
call assert_ok(ierr, 'coupling restart continuation failed')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for coupling restart')
end subroutine check_coupling_restart_round_trip
subroutine check_empty_coupling_restart_round_trip()
integer :: region_count
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for empty coupling restart')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read failed for empty coupling restart')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for empty coupling restart')
call sph_write_restart('build-codex-unix/restart_coupling_empty_080.tmp', ierr)
call assert_ok(ierr, 'empty coupling restart write failed')
call sph_read_restart('build-codex-unix/restart_coupling_empty_080.tmp', ierr)
call assert_ok(ierr, 'empty coupling restart read failed')
call sph_coupling_get_region_count(region_count, ierr)
call assert_ok(ierr, 'empty coupling restart region count failed')
if (region_count /= 0) call fail('empty coupling restart region count mismatch')
if (coupling_imported_force_count /= 0) then
call fail('empty coupling restart imported force count mismatch')
end if
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for empty coupling restart')
end subroutine check_empty_coupling_restart_round_trip
subroutine scramble_coupling_state()
coupling_external_force = -999.0_rk
coupling_motion_flag = -999
coupling_motion_mode = -999
coupling_motion_position = -999.0_rk
coupling_cumulative_impulse = -999.0_rk
coupling_momentum_exchange = -999.0_rk
coupling_cumulative_work = -999.0_rk
coupling_energy_exchange = -999.0_rk
coupling_imported_force_count = -999
end subroutine scramble_coupling_state
subroutine assert_restart_state_version(filename, expected_state)
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: expected_state
character(len=64) :: tag
character(len=32) :: state_version
integer :: major
integer :: minor
integer :: patch
integer :: ios
open(unit=96, file=trim(filename), status='old', action='read', iostat=ios)
if (ios /= 0) call fail('coupling restart header open failed')
read(96, *, iostat=ios) tag, major, minor, patch, state_version
close(unit=96)
if (ios /= 0) call fail('coupling restart header read failed')
if (trim(tag) /= 'SPHLITE_RESTART') call fail('coupling restart header tag mismatch')
if (trim(state_version) /= trim(expected_state)) then
call fail('coupling restart state version mismatch')
end if
end subroutine assert_restart_state_version
subroutine assert_ok(ierr_value, message)
integer, intent(in) :: ierr_value
character(len=*), intent(in) :: message
integer :: code
character(len=256) :: last_message
if (ierr_value /= SPH_SUCCESS) then
call sph_get_last_error(code, last_message, ierr)
write(*, '(A,": ",A)') trim(message), trim(last_message)
call fail(message)
end if
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) > 1.0e-12_rk) call fail(message)
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_080