Files
SPHLite/tests/module/test_restart.f90
T

128 lines
5.2 KiB
Fortran

program test_restart
use sph_precision, only: rk
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, &
temperature, h, acc_body, SPH_PHASE_WATER, SPH_STATUS_ACTIVE
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 sph_read_restart('build-codex-unix/restart_test.tmp', ierr)
call assert_ok(ierr, 'read restart failed')
call assert_restart_identity()
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_finalize(ierr)
call assert_ok(ierr, 'finalize failed')
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_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