Files
SPHLite/tests/module/test_water_force.f90
T

182 lines
6.8 KiB
Fortran
Raw Normal View History

2026-07-02 19:55:48 +08:00
program test_water_force
2026-07-02 20:44:19 +08:00
use sph_precision, only: rk
use sph_error, only: SPH_SUCCESS
use sph_database, only: sph_db_allocate, sph_db_free, x, v, h, mass, rho, pressure, &
2026-07-02 23:20:39 +08:00
acc_pressure, acc_viscosity, acc_body, a, particle_phase, particle_status, &
SPH_PHASE_WATER, SPH_PHASE_SOLID, SPH_STATUS_ACTIVE, SPH_STATUS_INACTIVE, &
SPH_STATUS_FAILED
2026-07-02 20:44:19 +08:00
use sph_neighbor, only: sph_neighbor_rebuild
use sph_water, only: sph_water_initialize, sph_water_reset, sph_water_set_body_force, &
sph_water_compute_pressure_acceleration, sph_water_compute_acceleration
2026-07-02 23:20:39 +08:00
use sph_coupling_interface, only: sph_coupling_import_particle_force
2026-07-02 19:55:48 +08:00
implicit none
2026-07-02 20:44:19 +08:00
integer :: ierr
2026-07-02 19:55:48 +08:00
2026-07-02 20:44:19 +08:00
call sph_water_initialize()
call check_pressure_pair()
call check_uniform_pressure()
2026-07-02 23:20:39 +08:00
call check_uniform_positive_pressure()
call check_nonwater_pressure_skip()
call check_inactive_pressure_skip()
call check_failed_pressure_skip()
2026-07-02 23:20:39 +08:00
call check_acceleration_resets()
call check_external_particle_force_placeholder()
2026-07-02 20:44:19 +08:00
call check_body_force()
call sph_water_reset()
2026-07-02 19:55:48 +08:00
contains
2026-07-02 20:44:19 +08:00
subroutine setup_pair()
call sph_db_allocate(2, ierr)
call assert_ok(ierr, 'force allocation failed')
particle_phase = SPH_PHASE_WATER
particle_status = SPH_STATUS_ACTIVE
h = 1.0_rk
mass = 1.0_rk
rho = 1000.0_rk
v = 0.0_rk
x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/)
x(:,2) = (/0.5_rk, 0.0_rk, 0.0_rk/)
call sph_neighbor_rebuild(ierr)
call assert_ok(ierr, 'force neighbor failed')
end subroutine setup_pair
subroutine check_pressure_pair()
call setup_pair()
pressure = (/1000.0_rk, 0.0_rk/)
call sph_water_compute_pressure_acceleration(ierr)
call assert_ok(ierr, 'pressure acceleration failed')
if (maxval(abs(acc_pressure)) <= 0.0_rk) call fail('pressure acceleration is zero')
2026-07-02 23:20:39 +08:00
if (acc_pressure(1,1) >= 0.0_rk) call fail('pressure direction mismatch')
if (acc_pressure(1,2) <= 0.0_rk) call fail('pressure neighbor direction mismatch')
2026-07-02 20:44:19 +08:00
if (maxval(abs(acc_pressure(:,1) + acc_pressure(:,2))) > 1.0e-12_rk) then
call fail('pressure acceleration is not symmetric')
end if
2026-07-02 23:20:39 +08:00
if (.not. is_finite_vector(acc_pressure(:,1))) call fail('pressure acceleration is not finite')
2026-07-02 20:44:19 +08:00
call sph_db_free()
end subroutine check_pressure_pair
subroutine check_uniform_pressure()
call setup_pair()
pressure = 0.0_rk
acc_pressure = 3.0_rk
2026-07-02 20:44:19 +08:00
call sph_water_compute_pressure_acceleration(ierr)
call assert_ok(ierr, 'uniform pressure failed')
if (maxval(abs(acc_pressure)) > 1.0e-12_rk) call fail('zero pressure force is not zero')
call sph_db_free()
end subroutine check_uniform_pressure
2026-07-02 23:20:39 +08:00
subroutine check_uniform_positive_pressure()
call setup_pair()
pressure = 1000.0_rk
call sph_water_compute_pressure_acceleration(ierr)
call assert_ok(ierr, 'positive uniform pressure failed')
if (maxval(abs(acc_pressure(:,1) + acc_pressure(:,2))) > 1.0e-12_rk) then
call fail('uniform pressure net force mismatch')
end if
call sph_db_free()
end subroutine check_uniform_positive_pressure
subroutine check_nonwater_pressure_skip()
call setup_pair()
particle_phase(2) = SPH_PHASE_SOLID
pressure = 1000.0_rk
acc_pressure = 4.0_rk
call sph_water_compute_pressure_acceleration(ierr)
call assert_ok(ierr, 'non-water pressure skip failed')
if (maxval(abs(acc_pressure)) > 1.0e-12_rk) call fail('non-water pressure contribution remained')
call sph_db_free()
end subroutine check_nonwater_pressure_skip
subroutine check_inactive_pressure_skip()
call setup_pair()
particle_status(2) = SPH_STATUS_INACTIVE
pressure = 1000.0_rk
acc_pressure = 4.0_rk
call sph_water_compute_pressure_acceleration(ierr)
call assert_ok(ierr, 'inactive pressure skip failed')
if (maxval(abs(acc_pressure)) > 1.0e-12_rk) call fail('inactive pressure contribution remained')
call sph_db_free()
end subroutine check_inactive_pressure_skip
subroutine check_failed_pressure_skip()
call setup_pair()
particle_status(2) = SPH_STATUS_FAILED
pressure = 1000.0_rk
acc_pressure = 4.0_rk
call sph_water_compute_pressure_acceleration(ierr)
call assert_ok(ierr, 'failed pressure skip failed')
if (maxval(abs(acc_pressure)) > 1.0e-12_rk) call fail('failed pressure contribution remained')
call sph_db_free()
end subroutine check_failed_pressure_skip
2026-07-02 23:20:39 +08:00
subroutine check_acceleration_resets()
call setup_pair()
pressure = 0.0_rk
acc_pressure = 7.0_rk
acc_viscosity = 5.0_rk
acc_body = 0.0_rk
call sph_water_compute_acceleration(ierr)
call assert_ok(ierr, 'reset acceleration failed')
if (maxval(abs(acc_pressure)) > 1.0e-12_rk) call fail('pressure cache was not reset')
if (maxval(abs(acc_viscosity)) > 1.0e-12_rk) call fail('viscosity cache was not reset')
if (maxval(abs(a)) > 1.0e-12_rk) call fail('total acceleration reset mismatch')
call sph_db_free()
end subroutine check_acceleration_resets
subroutine check_external_particle_force_placeholder()
real(rk) :: force(3)
call setup_pair()
pressure = 0.0_rk
force = (/0.0_rk, 2.0_rk, 0.0_rk/)
call sph_coupling_import_particle_force(1, force, ierr)
call assert_ok(ierr, 'external particle force import failed')
call sph_water_compute_acceleration(ierr)
call assert_ok(ierr, 'external particle force acceleration failed')
if (abs(acc_body(2,1) - 2.0_rk) > 1.0e-12_rk) call fail('external body cache mismatch')
if (a(2,1) <= 0.0_rk) call fail('external particle force not in total acceleration')
call sph_db_free()
end subroutine check_external_particle_force_placeholder
2026-07-02 20:44:19 +08:00
subroutine check_body_force()
real(rk) :: force(3)
call setup_pair()
pressure = 0.0_rk
force = (/0.0_rk, 0.0_rk, -9.81_rk/)
call sph_water_set_body_force(force, ierr)
call assert_ok(ierr, 'set body force failed')
2026-07-02 23:20:39 +08:00
if (abs(acc_body(3,1) + 9.81_rk) > 1.0e-12_rk) call fail('body force cache mismatch')
2026-07-02 20:44:19 +08:00
call sph_water_compute_acceleration(ierr)
call assert_ok(ierr, 'total acceleration failed')
if (abs(a(3,1) + 9.81_rk) > 1.0e-12_rk) call fail('body force acceleration mismatch')
2026-07-02 23:20:39 +08:00
force = 0.0_rk
call sph_water_set_body_force(force, ierr)
call assert_ok(ierr, 'clear body force failed')
2026-07-02 20:44:19 +08:00
call sph_db_free()
end subroutine check_body_force
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
2026-07-02 23:20:39 +08:00
logical function is_finite_vector(value_in)
real(rk), intent(in) :: value_in(:)
is_finite_vector = all(value_in == value_in) .and. &
maxval(abs(value_in)) < huge(value_in(1))
end function is_finite_vector
2026-07-02 20:44:19 +08:00
subroutine fail(message)
character(len=*), intent(in) :: message
write(*, '(A)') trim(message)
stop 1
end subroutine fail
2026-07-02 19:55:48 +08:00
end program test_water_force