Files
SPHLite/tests/module/test_solver_water.f90
T

375 lines
16 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, sph_rebuild_neighbors, sph_get_neighbor_stats, &
sph_get_particle_count, sph_get_particle_state_capacity, sph_get_particle_state, &
sph_set_output_directory, sph_write_output, sph_write_restart, sph_read_restart, &
sph_get_last_error, sph_get_water_summary, sph_get_water_dt_limits
use sph_database, only: rho, v, h, a, water_dt_cfl, water_dt_accel, &
water_dt_viscous
use sph_control, only: dt_default, end_time, max_steps, safety_factor, &
sph_control_validate_time
implicit none
integer :: ierr
integer :: step
real(rk) :: time
call check_uninitialized_errors()
call check_unbuilt_errors()
call check_bad_dt_values()
call check_single_step()
call check_multi_step()
call check_run_explicit()
call check_dt_limits()
call check_safety_factor_paths()
call check_last_small_step()
call check_no_water_dt_strategy()
call check_bad_h_dt_error()
call check_fortran_api_paths()
call check_fortran_api_compatibility()
contains
subroutine check_uninitialized_errors()
call sph_advance_explicit(1.0e-6_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('uninitialized explicit step did not fail')
call sph_run_explicit(ierr)
if (ierr == SPH_SUCCESS) call fail('uninitialized explicit run did not fail')
end subroutine check_uninitialized_errors
subroutine check_unbuilt_errors()
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_run_explicit(ierr)
if (ierr == SPH_SUCCESS) call fail('unbuilt explicit run did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize unbuilt failed')
end subroutine check_unbuilt_errors
subroutine check_bad_dt_values()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize bad dt failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read bad dt failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build bad dt failed')
call sph_advance_explicit(-1.0e-6_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('negative dt did not fail')
call sph_advance_explicit(0.0_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('zero dt did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize bad dt failed')
end subroutine check_bad_dt_values
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 /= 2) call fail('run step count mismatch')
if (abs(time - 2.0e-6_rk) > 1.0e-12_rk) call fail('run time mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize run failed')
end subroutine check_run_explicit
subroutine check_multi_step()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize multi-step failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read multi-step failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build multi-step failed')
call sph_advance_explicit(1.0e-6_rk, ierr)
call assert_ok(ierr, 'first multi-step advance failed')
call sph_advance_explicit(1.0e-6_rk, ierr)
call assert_ok(ierr, 'second multi-step advance failed')
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'time multi-step failed')
if (step /= 2) call fail('multi-step count mismatch')
if (abs(time - 2.0e-6_rk) > 1.0e-12_rk) call fail('multi-step time mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize multi-step failed')
end subroutine check_multi_step
subroutine check_dt_limits()
real(rk) :: dtcrit
real(rk) :: dt_cfl
real(rk) :: dt_accel
real(rk) :: dt_viscous
real(rk) :: dt_cfl_fast
real(rk) :: dt_accel_high
real(rk) :: expected
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')
call sph_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr)
call assert_ok(ierr, 'water dt diagnostics failed')
if (dt_cfl <= 0.0_rk) call fail('CFL dt diagnostic is not positive')
if (dt_accel <= 0.0_rk) call fail('acceleration dt diagnostic is not positive')
if (dt_viscous <= 0.0_rk) call fail('viscous dt diagnostic is not positive')
if (abs(water_dt_cfl - dt_cfl) > 1.0e-12_rk) call fail('CFL dt cache mismatch')
if (abs(water_dt_accel - dt_accel) > 1.0e-12_rk) call fail('accel dt cache mismatch')
if (abs(water_dt_viscous - dt_viscous) > 1.0e-12_rk) call fail('viscous dt cache mismatch')
v(:,1) = (/1000.0_rk, 0.0_rk, 0.0_rk/)
call sph_get_water_dt_limits(dt_cfl_fast, dt_accel, dt_viscous, ierr)
call assert_ok(ierr, 'fast CFL dt diagnostics failed')
if (dt_cfl_fast >= dt_cfl) call fail('CFL dt did not respond to velocity')
a(:,1) = (/1.0e12_rk, 0.0_rk, 0.0_rk/)
call sph_get_water_dt_limits(dt_cfl_fast, dt_accel_high, dt_viscous, ierr)
call assert_ok(ierr, 'high acceleration dt diagnostics failed')
if (dt_accel_high >= dt_accel) call fail('acceleration dt did not respond to acceleration')
dt_default = 1.0_rk
safety_factor = 0.5_rk
call sph_estimate_stable_dt(dtcrit, ierr)
call assert_ok(ierr, 'estimate dt failed')
expected = min(dt_cfl_fast, min(dt_accel_high, dt_viscous)) * safety_factor
if (dtcrit <= 0.0_rk) call fail('dtcrit is not positive')
if (dtcrit >= 1.0_rk) call fail('dtcrit did not use water limits')
if (abs(dtcrit - expected) > 1.0e-12_rk) call fail('dtcrit safety factor mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize dt failed')
end subroutine check_dt_limits
subroutine check_safety_factor_paths()
real(rk) :: dtcrit
call sph_control_validate_time(1.0e-6_rk, 1.0e-6_rk, 1, 0.0_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('invalid safety validation did not fail')
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize safety failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read safety failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build safety failed')
safety_factor = 0.0_rk
call sph_estimate_stable_dt(dtcrit, ierr)
if (ierr == SPH_SUCCESS) call fail('zero safety dt estimate did not fail')
if (dtcrit /= 0.0_rk) call fail('zero safety dtcrit should be zero')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize safety failed')
end subroutine check_safety_factor_paths
subroutine check_last_small_step()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize small step failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read small step failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build small step failed')
dt_default = 1.0e-6_rk
end_time = 1.5e-6_rk
max_steps = 5
safety_factor = 1.0_rk
call sph_run_explicit(ierr)
call assert_ok(ierr, 'run small step failed')
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'time small step failed')
if (step /= 2) call fail('small final step count mismatch')
if (abs(time - 1.5e-6_rk) > 1.0e-12_rk) call fail('small final step exceeded end time')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize small step failed')
end subroutine check_last_small_step
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 check_fortran_api_paths()
integer :: max_neighbors
real(rk) :: average_neighbors
real(rk) :: total_mass
real(rk) :: momentum(3)
real(rk) :: max_pressure
real(rk) :: min_density
real(rk) :: max_density
real(rk) :: dt_cfl
real(rk) :: dt_accel
real(rk) :: dt_viscous
call sph_rebuild_neighbors(ierr)
if (ierr == SPH_SUCCESS) call fail('uninitialized rebuild did not fail')
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize API paths failed')
call sph_get_neighbor_stats(max_neighbors, average_neighbors, ierr)
if (ierr == SPH_SUCCESS) call fail('unbuilt neighbor stats did not fail')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read API paths failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build API paths failed')
call sph_rebuild_neighbors(ierr)
call assert_ok(ierr, 'rebuild API failed')
call sph_get_neighbor_stats(max_neighbors, average_neighbors, ierr)
call assert_ok(ierr, 'neighbor stats API failed')
if (max_neighbors <= 0 .or. average_neighbors <= 0.0_rk) call fail('neighbor stats mismatch')
call sph_get_water_summary(total_mass, momentum, max_pressure, &
min_density, max_density, ierr)
call assert_ok(ierr, 'water summary API failed')
if (total_mass <= 0.0_rk) call fail('water summary mass mismatch')
call sph_get_water_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr)
call assert_ok(ierr, 'water dt limits API failed')
if (dt_cfl <= 0.0_rk .or. dt_viscous <= 0.0_rk) call fail('water dt limits mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize API paths failed')
end subroutine check_fortran_api_paths
subroutine check_fortran_api_compatibility()
integer :: count
integer :: ndim
integer :: actual
integer :: code
integer :: max_neighbors
integer :: ids(8)
integer :: phase(8)
integer :: status(8)
integer :: mat(8)
integer :: sec(8)
real(rk) :: average_neighbors
real(rk) :: x_out(3,8)
real(rk) :: v_out(3,8)
real(rk) :: a_out(3,8)
real(rk) :: rho_out(8)
real(rk) :: pressure_out(8)
real(rk) :: energy_out(8)
real(rk) :: temp_out(8)
real(rk) :: h_out(8)
real(rk) :: dtcrit
real(rk) :: total_mass
real(rk) :: momentum(3)
real(rk) :: max_pressure
real(rk) :: min_density
real(rk) :: max_density
character(len=256) :: message
call sph_initialize(ierr)
call assert_ok(ierr, 'compat initialize failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'compat read failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'compat build failed')
call sph_get_particle_count(count, ierr)
call assert_ok(ierr, 'compat particle count failed')
if (count /= 4) call fail('compat particle count mismatch')
call sph_get_particle_state_capacity(count, ndim, ierr)
call assert_ok(ierr, 'compat state capacity failed')
if (count /= 4 .or. ndim /= 3) call fail('compat state capacity mismatch')
call sph_get_particle_state(8, ids, phase, status, mat, sec, x_out, v_out, &
a_out, rho_out, pressure_out, energy_out, temp_out, h_out, actual, ierr)
call assert_ok(ierr, 'compat particle state failed')
if (actual /= 4 .or. ids(1) /= 1) call fail('compat particle state mismatch')
call sph_rebuild_neighbors(ierr)
call assert_ok(ierr, 'compat neighbor rebuild failed')
call sph_get_neighbor_stats(max_neighbors, average_neighbors, ierr)
call assert_ok(ierr, 'compat neighbor stats failed')
if (max_neighbors <= 0 .or. average_neighbors <= 0.0_rk) call fail('compat neighbor mismatch')
call sph_estimate_stable_dt(dtcrit, ierr)
call assert_ok(ierr, 'compat stable dt failed')
if (dtcrit <= 0.0_rk) call fail('compat stable dt mismatch')
call sph_advance_explicit(1.0e-6_rk, ierr)
call assert_ok(ierr, 'compat advance failed')
call sph_run_explicit(ierr)
call assert_ok(ierr, 'compat run failed')
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'compat current time failed')
if (step <= 0 .or. time <= 0.0_rk) call fail('compat current time mismatch')
call sph_get_water_summary(total_mass, momentum, max_pressure, &
min_density, max_density, ierr)
call assert_ok(ierr, 'compat water summary failed')
if (total_mass <= 0.0_rk .or. min_density <= 0.0_rk) call fail('compat summary mismatch')
call sph_set_output_directory('build-codex-unix', ierr)
call assert_ok(ierr, 'compat output directory failed')
call sph_write_output(88, time, ierr)
call assert_ok(ierr, 'compat write output failed')
call sph_write_restart('build-codex-unix/restart_api_compat.tmp', ierr)
call assert_ok(ierr, 'compat write restart failed')
call sph_read_restart('build-codex-unix/restart_api_compat.tmp', ierr)
call assert_ok(ierr, 'compat read restart failed')
call sph_advance_explicit(-1.0e-6_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('compat error path did not fail')
call sph_get_last_error(code, message, ierr)
call assert_ok(ierr, 'compat get last error failed')
if (code == 0 .or. len_trim(message) <= 0) call fail('compat error query mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'compat finalize failed')
end subroutine check_fortran_api_compatibility
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