Files
SPHLite/tests/module/test_restart.f90
T
2026-07-03 09:47:27 +08:00

324 lines
16 KiB
Fortran

program test_restart
use sph_precision, only: rk, SPH_VERSION_MAJOR, SPH_VERSION_MINOR, SPH_VERSION_PATCH
use sph_error, only: SPH_SUCCESS
use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, &
sph_run_explicit, sph_get_current_time, sph_write_restart, sph_read_restart, &
sph_advance_explicit, sph_get_water_summary
use sph_database, only: particle_id_ext, particle_phase, particle_status, &
material_id, section_id, x0, x, v, a, vhalf, mass, rho, volume, pressure, &
internal_energy, temperature, h, acc_body, neighbor_count, &
water_particle_count, water_mass, water_momentum, water_max_pressure, &
water_min_density, water_max_density, water_dt_cfl, water_dt_accel, &
water_dt_viscous, SPH_PHASE_WATER, SPH_STATUS_ACTIVE
use sph_control, only: output_interval, output_enabled, safety_factor
use sph_water, only: water_rho0, water_c0, water_gamma, water_p0, water_b, &
water_visc_alpha, water_visc_beta, water_visc_eta2, water_body_force
implicit none
integer :: ierr
integer :: step
integer :: written_step
real(rk) :: time
real(rk) :: written_time
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read input failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build model failed')
call assert_build_state()
call assert_repeat_summary()
call sph_run_explicit(ierr)
call assert_ok(ierr, 'run explicit failed')
call assert_repeat_summary()
call sph_get_current_time(written_time, written_step, ierr)
call assert_ok(ierr, 'time before restart failed')
call sph_write_restart('build-codex-unix/restart_test.tmp', ierr)
call assert_ok(ierr, 'write restart failed')
call assert_restart_file_content(written_time, written_step)
call sph_read_restart('build-codex-unix/restart_test.tmp', ierr)
call assert_ok(ierr, 'read restart failed')
call assert_restart_identity()
call assert_restart_read_state()
call assert_repeat_summary()
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'time after restart failed')
if (step /= written_step) call fail('restart did not restore step')
if (abs(time - written_time) > 1.0e-12_rk) call fail('restart did not restore time')
call sph_advance_explicit(1.0e-6_rk, ierr)
call assert_ok(ierr, 'advance after restart failed')
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'time after restart advance failed')
if (step /= written_step + 1) call fail('restart advance step mismatch')
if (abs(time - written_time - 1.0e-6_rk) > 1.0e-12_rk) then
call fail('restart advance time mismatch')
end if
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed')
call check_invalid_restart_error()
contains
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_build_state()
if (particle_id_ext(1) /= 1 .or. particle_id_ext(4) /= 4) then
call fail('build particle ids are not stable')
end if
if (particle_phase(1) /= SPH_PHASE_WATER) call fail('build phase mismatch')
if (particle_status(1) /= SPH_STATUS_ACTIVE) call fail('build status mismatch')
if (material_id(1) /= 1) call fail('build material id mismatch')
if (section_id(1) /= 1) call fail('build section id mismatch')
if (maxval(abs(x0(:,2) - (/0.1_rk, 0.0_rk, 0.0_rk/))) > 1.0e-12_rk) then
call fail('build x0 initialization mismatch')
end if
if (maxval(abs(x(:,2) - x0(:,2))) > 1.0e-12_rk) call fail('build x mismatch')
if (maxval(abs(v)) > 1.0e-12_rk) call fail('build velocity mismatch')
if (maxval(abs(a)) > 1.0e-12_rk) call fail('build acceleration mismatch')
if (maxval(abs(vhalf)) > 1.0e-12_rk) call fail('build vhalf mismatch')
if (abs(mass(1) - 1.0_rk) > 1.0e-12_rk) call fail('build mass mismatch')
if (abs(rho(1) - 1000.0_rk) > 1.0e-12_rk) call fail('build rho mismatch')
if (abs(volume(1) - 0.001_rk) > 1.0e-12_rk) call fail('build volume mismatch')
if (abs(pressure(1)) > 1.0e-12_rk) call fail('build pressure mismatch')
if (abs(temperature(1) - 293.15_rk) > 1.0e-12_rk) then
call fail('build temperature mismatch')
end if
if (abs(h(1) - 0.15_rk) > 1.0e-12_rk) call fail('build h mismatch')
if (maxval(abs(acc_body)) > 1.0e-12_rk) call fail('build body acceleration mismatch')
end subroutine assert_build_state
subroutine assert_restart_identity()
if (particle_id_ext(1) /= 1 .or. particle_id_ext(4) /= 4) then
call fail('restart particle ids are not stable')
end if
end subroutine assert_restart_identity
subroutine assert_restart_read_state()
if (particle_phase(1) /= SPH_PHASE_WATER) call fail('restart read phase mismatch')
if (particle_status(1) /= SPH_STATUS_ACTIVE) call fail('restart read status mismatch')
if (abs(rho(1) - 1000.0_rk) > 1.0e-6_rk) call fail('restart read rho mismatch')
if (abs(h(1) - 0.15_rk) > 1.0e-12_rk) call fail('restart read h mismatch')
if (water_rho0 <= 0.0_rk .or. water_c0 <= 0.0_rk) call fail('restart read EOS mismatch')
if (water_visc_alpha < 0.0_rk .or. water_visc_eta2 <= 0.0_rk) then
call fail('restart read viscosity mismatch')
end if
if (maxval(abs(water_body_force)) > 1.0e-12_rk) call fail('restart read body force mismatch')
if (maxval(neighbor_count) <= 0) call fail('restart did not rebuild neighbors')
end subroutine assert_restart_read_state
subroutine assert_restart_file_content(expected_time, expected_step)
real(rk), intent(in) :: expected_time
integer, intent(in) :: expected_step
character(len=64) :: tag
character(len=32) :: state_version
character(len=256) :: directory
integer :: major
integer :: minor
integer :: patch
integer :: count
integer :: file_step
integer :: interval
integer :: pid
integer :: phase
integer :: status
integer :: mat
integer :: sec
integer :: ios
logical :: enabled
real(rk) :: file_time
real(rk) :: file_safety
real(rk) :: eos_values(5)
real(rk) :: visc_values(3)
real(rk) :: body_values(3)
real(rk) :: neighbor_values(5)
integer :: file_water_count
real(rk) :: water_mass_value
real(rk) :: water_diag_values(7)
real(rk) :: dt_values(3)
integer :: gas_eos_type_value
integer :: gas_count_value
real(rk) :: gas_eos_values(10)
real(rk) :: gas_control_values(3)
real(rk) :: gas_body_values(3)
real(rk) :: gas_summary_values(2)
real(rk) :: gas_diag_values(10)
real(rk) :: gas_bubble_values(6)
real(rk) :: gas_dt_values(3)
real(rk) :: rx0(3)
real(rk) :: rx(3)
real(rk) :: rv(3)
real(rk) :: ra(3)
real(rk) :: rvhalf(3)
real(rk) :: scalar_values(7)
open(unit=74, file='build-codex-unix/restart_test.tmp', status='old', &
action='read', iostat=ios)
if (ios /= 0) call fail('restart file open failed')
read(74, *, iostat=ios) tag, major, minor, patch, state_version
if (ios /= 0) call fail('restart header read failed')
if (trim(tag) /= 'SPHLITE_RESTART') call fail('restart header tag mismatch')
if (major /= SPH_VERSION_MAJOR .or. minor /= SPH_VERSION_MINOR) then
call fail('restart version mismatch')
end if
if (patch /= SPH_VERSION_PATCH) call fail('restart patch mismatch')
if (trim(state_version) /= 'STATE_0_3') call fail('restart state version mismatch')
read(74, *, iostat=ios) file_time, file_step, count
if (ios /= 0) call fail('restart time state read failed')
if (abs(file_time - expected_time) > 1.0e-12_rk) call fail('restart time write mismatch')
if (file_step /= expected_step) call fail('restart step write mismatch')
if (count /= 4) call fail('restart particle count write mismatch')
read(74, *, iostat=ios) interval, enabled, file_safety
if (ios /= 0) call fail('restart output state read failed')
if (interval /= output_interval) call fail('restart output interval mismatch')
if (enabled .neqv. output_enabled) call fail('restart output enabled mismatch')
if (abs(file_safety - safety_factor) > 1.0e-12_rk) call fail('restart safety mismatch')
read(74, '(A)', iostat=ios) directory
if (ios /= 0 .or. len_trim(directory) <= 0) call fail('restart output directory missing')
read(74, *, iostat=ios) eos_values
if (ios /= 0) call fail('restart EOS state read failed')
if (abs(eos_values(1) - water_rho0) > 1.0e-12_rk) call fail('restart rho0 mismatch')
if (abs(eos_values(2) - water_c0) > 1.0e-12_rk) call fail('restart c0 mismatch')
if (abs(eos_values(3) - water_gamma) > 1.0e-12_rk) call fail('restart gamma mismatch')
if (abs(eos_values(4) - water_p0) > 1.0e-12_rk) call fail('restart p0 mismatch')
if (abs(eos_values(5) - water_b) > 1.0e-6_rk) call fail('restart B mismatch')
read(74, *, iostat=ios) visc_values
if (ios /= 0) call fail('restart viscosity state read failed')
if (abs(visc_values(1) - water_visc_alpha) > 1.0e-12_rk) call fail('restart alpha mismatch')
if (abs(visc_values(2) - water_visc_beta) > 1.0e-12_rk) call fail('restart beta mismatch')
if (abs(visc_values(3) - water_visc_eta2) > 1.0e-12_rk) call fail('restart eta2 mismatch')
read(74, *, iostat=ios) body_values
if (ios /= 0) call fail('restart body force state read failed')
if (maxval(abs(body_values - water_body_force)) > 1.0e-12_rk) then
call fail('restart body force mismatch')
end if
read(74, *, iostat=ios) neighbor_values
if (ios /= 0) call fail('restart neighbor state read failed')
read(74, *, iostat=ios) file_water_count, water_mass_value
if (ios /= 0) call fail('restart water summary read failed')
if (file_water_count /= water_particle_count) call fail('restart water count mismatch')
if (abs(water_mass_value - water_mass) > 1.0e-12_rk) call fail('restart water mass mismatch')
read(74, *, iostat=ios) water_diag_values
if (ios /= 0) call fail('restart water diagnostic read failed')
if (maxval(abs(water_diag_values(1:3) - water_momentum)) > 1.0e-12_rk) then
call fail('restart water momentum mismatch')
end if
if (abs(water_diag_values(4) - water_max_pressure) > 1.0e-12_rk) then
call fail('restart max pressure mismatch')
end if
if (abs(water_diag_values(5) - water_min_density) > 1.0e-12_rk) then
call fail('restart min density mismatch')
end if
if (abs(water_diag_values(6) - water_max_density) > 1.0e-12_rk) then
call fail('restart max density mismatch')
end if
read(74, *, iostat=ios) dt_values
if (ios /= 0) call fail('restart water dt read failed')
if (abs(dt_values(1) - water_dt_cfl) > 1.0e-12_rk) call fail('restart dt cfl mismatch')
if (abs(dt_values(2) - water_dt_accel) > 1.0e-12_rk) call fail('restart dt accel mismatch')
if (abs(dt_values(3) - water_dt_viscous) > 1.0e-12_rk) call fail('restart dt viscous mismatch')
read(74, *, iostat=ios) gas_eos_type_value, gas_eos_values
if (ios /= 0) call fail('restart Gas EOS read failed')
read(74, *, iostat=ios) gas_control_values
if (ios /= 0) call fail('restart Gas control read failed')
read(74, *, iostat=ios) gas_body_values
if (ios /= 0) call fail('restart Gas body force read failed')
read(74, *, iostat=ios) gas_count_value, gas_summary_values
if (ios /= 0) call fail('restart Gas summary read failed')
read(74, *, iostat=ios) gas_diag_values
if (ios /= 0) call fail('restart Gas diagnostics read failed')
read(74, *, iostat=ios) gas_bubble_values
if (ios /= 0) call fail('restart Gas bubble read failed')
read(74, *, iostat=ios) gas_dt_values
if (ios /= 0) call fail('restart Gas dt read failed')
read(74, *, iostat=ios) pid, phase, status, mat, sec
if (ios /= 0) call fail('restart particle ids read failed')
if (pid /= particle_id_ext(1)) call fail('restart particle id mismatch')
if (phase /= particle_phase(1)) call fail('restart particle phase mismatch')
if (status /= particle_status(1)) call fail('restart particle status mismatch')
if (mat /= material_id(1)) call fail('restart particle material mismatch')
if (sec /= section_id(1)) call fail('restart particle section mismatch')
read(74, *, iostat=ios) rx0, rx, rv, ra, rvhalf
if (ios /= 0) call fail('restart particle vector read failed')
if (maxval(abs(rx0 - x0(:,1))) > 1.0e-12_rk) call fail('restart x0 mismatch')
if (maxval(abs(rx - x(:,1))) > 1.0e-12_rk) call fail('restart x mismatch')
if (maxval(abs(rv - v(:,1))) > 1.0e-12_rk) call fail('restart velocity mismatch')
if (maxval(abs(ra - a(:,1))) > 1.0e-12_rk) call fail('restart acceleration mismatch')
if (maxval(abs(rvhalf - vhalf(:,1))) > 1.0e-12_rk) call fail('restart vhalf mismatch')
read(74, *, iostat=ios) scalar_values
close(unit=74)
if (ios /= 0) call fail('restart particle scalar read failed')
if (abs(scalar_values(1) - mass(1)) > 1.0e-12_rk) call fail('restart mass mismatch')
if (abs(scalar_values(2) - rho(1)) > 1.0e-12_rk) call fail('restart rho mismatch')
if (abs(scalar_values(3) - volume(1)) > 1.0e-12_rk) call fail('restart volume mismatch')
if (abs(scalar_values(4) - pressure(1)) > 1.0e-12_rk) call fail('restart pressure mismatch')
if (abs(scalar_values(5) - internal_energy(1)) > 1.0e-12_rk) then
call fail('restart internal energy mismatch')
end if
if (abs(scalar_values(6) - temperature(1)) > 1.0e-12_rk) call fail('restart temp mismatch')
if (abs(scalar_values(7) - h(1)) > 1.0e-12_rk) call fail('restart h mismatch')
end subroutine assert_restart_file_content
subroutine check_invalid_restart_error()
integer :: ios
open(unit=75, file='build-codex-unix/restart_invalid.tmp', &
status='unknown', action='write', iostat=ios)
if (ios /= 0) call fail('invalid restart fixture open failed')
write(75, '(A)') 'NOT_A_RESTART'
close(unit=75)
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize invalid restart failed')
call sph_read_restart('build-codex-unix/restart_invalid.tmp', ierr)
if (ierr == SPH_SUCCESS) call fail('invalid restart did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize invalid restart failed')
end subroutine check_invalid_restart_error
subroutine assert_repeat_summary()
real(rk) :: mass_a
real(rk) :: mass_b
real(rk) :: momentum_a(3)
real(rk) :: momentum_b(3)
real(rk) :: max_pressure_a
real(rk) :: max_pressure_b
real(rk) :: min_density_a
real(rk) :: min_density_b
real(rk) :: max_density_a
real(rk) :: max_density_b
call sph_get_water_summary(mass_a, momentum_a, max_pressure_a, &
min_density_a, max_density_a, ierr)
call assert_ok(ierr, 'first repeated summary failed')
call sph_get_water_summary(mass_b, momentum_b, max_pressure_b, &
min_density_b, max_density_b, ierr)
call assert_ok(ierr, 'second repeated summary failed')
if (abs(mass_a - mass_b) > 1.0e-12_rk) call fail('summary mass is not repeatable')
if (maxval(abs(momentum_a - momentum_b)) > 1.0e-12_rk) then
call fail('summary momentum is not repeatable')
end if
if (abs(max_pressure_a - max_pressure_b) > 1.0e-12_rk) then
call fail('summary pressure is not repeatable')
end if
if (abs(min_density_a - min_density_b) > 1.0e-12_rk) then
call fail('summary min density is not repeatable')
end if
if (abs(max_density_a - max_density_b) > 1.0e-12_rk) then
call fail('summary max density is not repeatable')
end if
end subroutine assert_repeat_summary
subroutine fail(message)
character(len=*), intent(in) :: message
write(*, '(A)') trim(message)
stop 1
end subroutine fail
end program test_restart