program test_water_density 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, & drho_dt, particle_phase, particle_status, SPH_PHASE_WATER, SPH_PHASE_SOLID, & SPH_STATUS_ACTIVE, SPH_STATUS_INACTIVE, SPH_STATUS_FAILED use sph_neighbor, only: sph_neighbor_rebuild use sph_water, only: sph_water_initialize, sph_water_reset, & sph_water_compute_density_rate, sph_water_tait_pressure implicit none integer :: ierr call sph_water_initialize() call check_pair_compression() call check_pair_extension() call check_static_particles() call check_nonwater_skip() call check_inactive_skip() call check_failed_skip() call check_empty_neighbor_density() call check_low_density_error() call sph_water_reset() contains subroutine setup_pair() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'pair allocation failed') particle_phase = SPH_PHASE_WATER particle_status = SPH_STATUS_ACTIVE h = 1.0_rk mass = 1.0_rk rho = 1000.0_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/0.5_rk, 0.0_rk, 0.0_rk/) end subroutine setup_pair subroutine check_pair_compression() call setup_pair() v(:,1) = (/1.0_rk, 0.0_rk, 0.0_rk/) v(:,2) = (/-1.0_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'compression neighbor failed') call sph_water_compute_density_rate(ierr) call assert_ok(ierr, 'compression density failed') if (drho_dt(1) <= 0.0_rk .or. drho_dt(2) <= 0.0_rk) call fail('compression sign mismatch') if (.not. is_finite_vector(drho_dt)) call fail('compression density is not finite') call sph_db_free() end subroutine check_pair_compression subroutine check_pair_extension() call setup_pair() v(:,1) = (/-1.0_rk, 0.0_rk, 0.0_rk/) v(:,2) = (/1.0_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'extension neighbor failed') call sph_water_compute_density_rate(ierr) call assert_ok(ierr, 'extension density failed') if (drho_dt(1) >= 0.0_rk .or. drho_dt(2) >= 0.0_rk) call fail('extension sign mismatch') if (.not. is_finite_vector(drho_dt)) call fail('extension density is not finite') call sph_db_free() end subroutine check_pair_extension subroutine check_static_particles() call setup_pair() v = 0.0_rk drho_dt = 10.0_rk call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'static neighbor failed') call sph_water_compute_density_rate(ierr) call assert_ok(ierr, 'static density failed') if (maxval(abs(drho_dt)) > 1.0e-12_rk) call fail('static density rate is not zero') call sph_db_free() end subroutine check_static_particles subroutine check_nonwater_skip() call setup_pair() particle_phase(2) = SPH_PHASE_SOLID v(:,1) = (/1.0_rk, 0.0_rk, 0.0_rk/) v(:,2) = (/-1.0_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'nonwater neighbor failed') call sph_water_compute_density_rate(ierr) call assert_ok(ierr, 'nonwater density failed') if (abs(drho_dt(1)) > 1.0e-12_rk .or. abs(drho_dt(2)) > 1.0e-12_rk) then call fail('nonwater particle was not skipped') end if call sph_db_free() end subroutine check_nonwater_skip subroutine check_inactive_skip() call setup_pair() particle_status(2) = SPH_STATUS_INACTIVE v(:,1) = (/1.0_rk, 0.0_rk, 0.0_rk/) v(:,2) = (/-1.0_rk, 0.0_rk, 0.0_rk/) drho_dt = 5.0_rk call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'inactive neighbor failed') call sph_water_compute_density_rate(ierr) call assert_ok(ierr, 'inactive density failed') if (abs(drho_dt(1)) > 1.0e-12_rk .or. abs(drho_dt(2)) > 1.0e-12_rk) then call fail('inactive particle was not skipped') end if call sph_db_free() end subroutine check_inactive_skip subroutine check_failed_skip() call setup_pair() particle_status(2) = SPH_STATUS_FAILED v(:,1) = (/1.0_rk, 0.0_rk, 0.0_rk/) v(:,2) = (/-1.0_rk, 0.0_rk, 0.0_rk/) drho_dt = 5.0_rk call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'failed neighbor failed') call sph_water_compute_density_rate(ierr) call assert_ok(ierr, 'failed density failed') if (maxval(abs(drho_dt)) > 1.0e-12_rk) call fail('failed particle was not skipped') call sph_db_free() end subroutine check_failed_skip subroutine check_empty_neighbor_density() call sph_db_allocate(1, ierr) call assert_ok(ierr, 'empty-neighbor allocation failed') particle_phase = SPH_PHASE_WATER particle_status = SPH_STATUS_ACTIVE h = 1.0_rk mass = 1.0_rk rho = 1000.0_rk drho_dt = 3.0_rk call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'empty-neighbor rebuild failed') call sph_water_compute_density_rate(ierr) call assert_ok(ierr, 'empty-neighbor density failed') if (maxval(abs(drho_dt)) > 1.0e-12_rk) call fail('empty-neighbor density mismatch') call sph_db_free() end subroutine check_empty_neighbor_density subroutine check_low_density_error() real(rk) :: pressure_value call sph_water_tait_pressure(0.0_rk, pressure_value, ierr) if (ierr == SPH_SUCCESS) call fail('low density did not fail') end subroutine check_low_density_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 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 subroutine fail(message) character(len=*), intent(in) :: message write(*, '(A)') trim(message) stop 1 end subroutine fail end program test_water_density