program test_water_force 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, & 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 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 use sph_coupling_interface, only: sph_coupling_import_particle_force implicit none integer :: ierr call sph_water_initialize() call check_pressure_pair() call check_uniform_pressure() call check_uniform_positive_pressure() call check_nonwater_pressure_skip() call check_inactive_pressure_skip() call check_failed_pressure_skip() call check_acceleration_resets() call check_external_particle_force_placeholder() call check_body_force() call sph_water_reset() contains 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') 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') if (maxval(abs(acc_pressure(:,1) + acc_pressure(:,2))) > 1.0e-12_rk) then call fail('pressure acceleration is not symmetric') end if if (.not. is_finite_vector(acc_pressure(:,1))) call fail('pressure acceleration is not finite') 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 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 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 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 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') if (abs(acc_body(3,1) + 9.81_rk) > 1.0e-12_rk) call fail('body force cache mismatch') 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') force = 0.0_rk call sph_water_set_body_force(force, ierr) call assert_ok(ierr, 'clear body force failed') 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 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_force