program test_particle_bins use fem_precision use fem_error use fem_particle_state use fem_particle_bins implicit none call check_shape_and_index() call check_build_and_pairs() call check_error_paths() contains subroutine check_shape_and_index() real(rk) :: origin(3) integer :: dims(3) integer :: index(3) integer :: ncell integer :: linear integer :: ierr call pbin_grid_shape((/ -ONE, -ONE, ZERO /), & (/ ONE, ONE, ONE /), ONE, origin, dims, ncell, ierr) if (ierr .ne. FEM_SUCCESS) call fail(10) call check_vec(origin, (/ -ONE, -ONE, ZERO /), 11) if (dims(1) .ne. 2 .or. dims(2) .ne. 2 .or. dims(3) .ne. 1) then call fail(12) end if if (ncell .ne. 4) call fail(13) call pbin_point_index((/ -0.75_rk, 0.25_rk, 0.20_rk /), origin, ONE, & dims, index, linear, ierr) if (ierr .ne. FEM_SUCCESS) call fail(14) if (index(1) .ne. 1 .or. index(2) .ne. 2 .or. index(3) .ne. 1) then call fail(15) end if if (linear .ne. 3) call fail(16) call pbin_linear_index((/ 2, 2, 1 /), dims, linear, ierr) if (ierr .ne. FEM_SUCCESS) call fail(17) if (linear .ne. 4) call fail(18) call pbin_validate_shape(dims, ncell, ierr) if (ierr .ne. FEM_SUCCESS) call fail(19) end subroutine check_shape_and_index subroutine check_build_and_pairs() integer :: s1 integer :: s2 integer :: s3 integer :: s4 integer :: active_count integer :: max_count integer :: empty_count integer :: pair_count integer :: ierr integer :: head(4) integer :: next_slot(4) integer :: pair_i(8) integer :: pair_j(8) integer :: dims(3) real(rk) :: origin(3) call particle_reset() call particle_add(1, (/ 0.10_rk, 0.10_rk, ZERO /), & (/ ZERO, ZERO, ZERO /), 0.10_rk, ONE, s1, ierr) if (ierr .ne. FEM_SUCCESS) call fail(40) call particle_add(2, (/ 0.80_rk, 0.10_rk, ZERO /), & (/ ZERO, ZERO, ZERO /), 0.10_rk, ONE, s2, ierr) if (ierr .ne. FEM_SUCCESS) call fail(41) call particle_add(3, (/ 2.50_rk, ZERO, ZERO /), & (/ ZERO, ZERO, ZERO /), 0.10_rk, ONE, s3, ierr) if (ierr .ne. FEM_SUCCESS) call fail(42) call particle_add(4, (/ 2.60_rk, ZERO, ZERO /), & (/ ZERO, ZERO, ZERO /), 0.10_rk, ONE, s4, ierr) if (ierr .ne. FEM_SUCCESS) call fail(43) origin = (/ ZERO, ZERO, -0.5_rk /) dims = (/ 4, 1, 1 /) call pbin_build(origin, ONE, dims, head, next_slot, active_count, ierr) if (ierr .ne. FEM_SUCCESS) call fail(44) if (active_count .ne. 4) call fail(45) if (head(1) .eq. 0 .or. head(3) .eq. 0) call fail(46) call pbin_count_occupancy(head, next_slot, dims, max_count, empty_count, & ierr) if (ierr .ne. FEM_SUCCESS) call fail(47) if (max_count .ne. 2) call fail(48) if (empty_count .ne. 2) call fail(49) call pbin_pairs(head, next_slot, dims, 8, pair_i, pair_j, pair_count, ierr) if (ierr .ne. FEM_SUCCESS) call fail(50) if (pair_count .ne. 2) call fail(51) if (.not. has_pair(pair_i, pair_j, pair_count, s1, s2)) call fail(52) if (.not. has_pair(pair_i, pair_j, pair_count, s3, s4)) call fail(53) end subroutine check_build_and_pairs subroutine check_error_paths() integer :: dims(3) integer :: ncell integer :: linear integer :: active_count integer :: pair_count integer :: ierr integer :: head(1) integer :: next_slot(1) integer :: pair_i(1) integer :: pair_j(1) real(rk) :: origin(3) dims = (/ 1, 0, 1 /) call pbin_validate_shape(dims, ncell, ierr) if (ierr .eq. FEM_SUCCESS) call fail(80) dims = (/ 1, 1, 1 /) call pbin_linear_index((/ 2, 1, 1 /), dims, linear, ierr) if (ierr .eq. FEM_SUCCESS) call fail(81) call pbin_grid_shape((/ ONE, ZERO, ZERO /), (/ ZERO, ZERO, ZERO /), & ONE, origin, dims, ncell, ierr) if (ierr .eq. FEM_SUCCESS) call fail(82) call particle_reset() call particle_add(1, (/ ZERO, ZERO, ZERO /), (/ ZERO, ZERO, ZERO /), & 0.10_rk, ONE, active_count, ierr) if (ierr .ne. FEM_SUCCESS) call fail(83) dims = (/ 2, 1, 1 /) call pbin_build((/ ZERO, ZERO, ZERO /), ONE, dims, head, next_slot, & active_count, ierr) if (ierr .eq. FEM_SUCCESS) call fail(84) head = 1 next_slot = 0 call pbin_pairs(head, next_slot, dims, 1, pair_i, pair_j, pair_count, ierr) if (ierr .eq. FEM_SUCCESS) call fail(85) end subroutine check_error_paths logical function has_pair(pair_i, pair_j, pair_count, left, right) integer, intent(in) :: pair_i(:) integer, intent(in) :: pair_j(:) integer, intent(in) :: pair_count integer, intent(in) :: left integer, intent(in) :: right integer :: item has_pair = .false. do item = 1, pair_count if (pair_i(item) .eq. left .and. pair_j(item) .eq. right) then has_pair = .true. return end if if (pair_i(item) .eq. right .and. pair_j(item) .eq. left) then has_pair = .true. return end if end do end function has_pair subroutine check_vec(actual, expected, code) real(rk), intent(in) :: actual(3) real(rk), intent(in) :: expected(3) integer, intent(in) :: code if (maxval(abs(actual - expected)) .gt. 1.0e-10_rk) call fail(code) end subroutine check_vec subroutine fail(code) integer, intent(in) :: code write(*,*) 'test_particle_bins failed:', code stop 1 end subroutine fail end program test_particle_bins