Files
SPHLite/tests/module/test_solver_water.f90
T
2026-07-02 20:55:01 +08:00

138 lines
4.9 KiB
Fortran

program test_solver_water
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_advance_explicit, sph_run_explicit, sph_get_current_time, &
sph_estimate_stable_dt
use sph_database, only: rho, v, h, a
use sph_control, only: dt_default, safety_factor
implicit none
integer :: ierr
integer :: step
real(rk) :: time
call check_unbuilt_error()
call check_single_step()
call check_run_explicit()
call check_dt_limits()
call check_no_water_dt_strategy()
call check_bad_h_dt_error()
contains
subroutine check_unbuilt_error()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize unbuilt failed')
call sph_advance_explicit(1.0e-6_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('unbuilt explicit step did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize unbuilt failed')
end subroutine check_unbuilt_error
subroutine check_single_step()
real(rk) :: rho_before
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize single failed')
call sph_read_inp('tests/data/water_body_force_020.inp', ierr)
call assert_ok(ierr, 'read single failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build single failed')
rho_before = rho(1)
call sph_advance_explicit(1.0e-6_rk, ierr)
call assert_ok(ierr, 'advance single failed')
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'time single failed')
if (step /= 1) call fail('single step count mismatch')
if (abs(time - 1.0e-6_rk) > 1.0e-12_rk) call fail('single step time mismatch')
if (abs(rho(1) - rho_before) > 1.0e-6_rk) call fail('unexpected density drift')
if (v(3,1) >= 0.0_rk) call fail('body force did not change velocity direction')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize single failed')
end subroutine check_single_step
subroutine check_run_explicit()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize run failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read run failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build run failed')
call sph_run_explicit(ierr)
call assert_ok(ierr, 'run explicit failed')
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'time run failed')
if (step <= 0) call fail('run did not advance')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize run failed')
end subroutine check_run_explicit
subroutine check_dt_limits()
real(rk) :: dtcrit
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize dt failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read dt failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build dt failed')
dt_default = 1.0_rk
safety_factor = 0.5_rk
a(:,1) = (/100.0_rk, 0.0_rk, 0.0_rk/)
call sph_estimate_stable_dt(dtcrit, ierr)
call assert_ok(ierr, 'estimate dt failed')
if (dtcrit <= 0.0_rk) call fail('dtcrit is not positive')
if (dtcrit >= 1.0_rk) call fail('dtcrit did not use water limits')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize dt failed')
end subroutine check_dt_limits
subroutine check_no_water_dt_strategy()
real(rk) :: dtcrit
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize no-water dt failed')
call sph_read_inp('tests/data/minimal_particles_011.inp', ierr)
call assert_ok(ierr, 'read no-water dt failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build no-water dt failed')
dt_default = 2.0e-6_rk
call sph_estimate_stable_dt(dtcrit, ierr)
call assert_ok(ierr, 'estimate no-water dt failed')
if (abs(dtcrit - 2.0e-6_rk) > 1.0e-12_rk) call fail('no-water dt did not use default')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize no-water dt failed')
end subroutine check_no_water_dt_strategy
subroutine check_bad_h_dt_error()
real(rk) :: dtcrit
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize bad h failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read bad h failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build bad h failed')
h(1) = 0.0_rk
call sph_estimate_stable_dt(dtcrit, ierr)
if (ierr == SPH_SUCCESS) call fail('bad h dt estimate did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize bad h failed')
end subroutine check_bad_h_dt_error
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_solver_water