program test_neighbor use sph_precision, only: rk use sph_error, only: SPH_SUCCESS use sph_database, only: sph_db_allocate, sph_db_free, x, h, neighbor_count use sph_neighbor, only: sph_neighbor_rebuild, sph_neighbor_rebuild_with_capacity, & sph_neighbor_get_stats, neighbor_min_corner, neighbor_max_corner, & neighbor_cell_dims, neighbor_num_cells implicit none integer :: ierr call check_empty_model() call check_single_particle() call check_two_particle_inside() call check_two_particle_outside() call check_support_boundary() call check_duplicate_coordinates() call check_near_coordinates() call check_move_rebuild() call check_negative_coordinates() call check_sparse_coordinates() call check_different_h() call check_naive_consistency() call check_neighbor_stats() call check_capacity_error() contains subroutine check_empty_model() call sph_db_allocate(0, ierr) call assert_ok(ierr, 'empty allocation failed') call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'empty neighbor rebuild failed') call sph_db_free() end subroutine check_empty_model subroutine check_single_particle() call sph_db_allocate(1, ierr) call assert_ok(ierr, 'single allocation failed') h(1) = 0.5_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'single neighbor rebuild failed') if (neighbor_count(1) /= 0) call fail('single particle has neighbors') call sph_db_free() end subroutine check_single_particle subroutine check_two_particle_inside() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'inside allocation failed') h = 0.25_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/0.2_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'inside rebuild failed') if (neighbor_count(1) /= 1 .or. neighbor_count(2) /= 1) call fail('inside pair mismatch') call sph_db_free() end subroutine check_two_particle_inside subroutine check_two_particle_outside() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'outside allocation failed') h = 0.25_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/0.6_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'outside rebuild failed') if (neighbor_count(1) /= 0 .or. neighbor_count(2) /= 0) call fail('outside pair mismatch') call sph_db_free() end subroutine check_two_particle_outside subroutine check_support_boundary() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'boundary allocation failed') h = 0.25_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, 'boundary rebuild failed') if (neighbor_count(1) /= 1 .or. neighbor_count(2) /= 1) call fail('boundary pair mismatch') call sph_db_free() end subroutine check_support_boundary subroutine check_duplicate_coordinates() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'duplicate allocation failed') h = 0.25_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = x(:,1) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'duplicate rebuild failed') if (neighbor_count(1) /= 1 .or. neighbor_count(2) /= 1) call fail('duplicate pair mismatch') call sph_db_free() end subroutine check_duplicate_coordinates subroutine check_near_coordinates() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'near allocation failed') h = 0.25_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/1.0e-12_rk, -1.0e-12_rk, 1.0e-12_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'near rebuild failed') if (neighbor_count(1) /= 1 .or. neighbor_count(2) /= 1) call fail('near pair mismatch') call sph_db_free() end subroutine check_near_coordinates subroutine check_move_rebuild() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'move allocation failed') h = 0.2_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/10.0_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'far rebuild failed') if (neighbor_count(1) /= 0) call fail('far particles should not be neighbors') x(:,2) = (/0.1_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'near rebuild failed') if (neighbor_count(1) /= 1 .or. neighbor_count(2) /= 1) call fail('moved particles not linked') call sph_db_free() end subroutine check_move_rebuild subroutine check_negative_coordinates() call sph_db_allocate(3, ierr) call assert_ok(ierr, 'negative allocation failed') h = 0.25_rk x(:,1) = (/-0.2_rk, -0.1_rk, 0.0_rk/) x(:,2) = (/-0.1_rk, -0.1_rk, 0.0_rk/) x(:,3) = (/0.8_rk, 0.8_rk, 0.8_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'negative coordinate rebuild failed') if (neighbor_count(1) /= 1 .or. neighbor_count(2) /= 1) call fail('negative pair mismatch') if (neighbor_count(3) /= 0) call fail('distant boundary particle mismatch') call sph_db_free() end subroutine check_negative_coordinates subroutine check_sparse_coordinates() call sph_db_allocate(3, ierr) call assert_ok(ierr, 'sparse allocation failed') h = 0.1_rk x(:,1) = (/-10.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/0.0_rk, 12.0_rk, 0.0_rk/) x(:,3) = (/15.0_rk, 0.0_rk, -14.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'sparse rebuild failed') if (neighbor_count(1) /= 0 .or. neighbor_count(2) /= 0) call fail('sparse count mismatch') if (neighbor_min_corner(1) >= -10.0_rk) call fail('sparse min bound mismatch') if (neighbor_max_corner(1) <= 15.0_rk) call fail('sparse max bound mismatch') if (minval(neighbor_cell_dims) < 1 .or. neighbor_num_cells < 1) then call fail('sparse cell dimensions mismatch') end if call sph_db_free() end subroutine check_sparse_coordinates subroutine check_different_h() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'different h allocation failed') h(1) = 0.1_rk h(2) = 1.0_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/1.5_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'different h rebuild failed') if (neighbor_count(1) /= 1 .or. neighbor_count(2) /= 1) call fail('different h search failed') call sph_db_free() end subroutine check_different_h subroutine check_naive_consistency() integer :: i integer :: expected call sph_db_allocate(4, ierr) call assert_ok(ierr, 'naive allocation failed') h = 0.25_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/0.2_rk, 0.0_rk, 0.0_rk/) x(:,3) = (/0.0_rk, 0.2_rk, 0.0_rk/) x(:,4) = (/1.5_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'naive rebuild failed') do i = 1, 4 expected = naive_neighbor_count(i) if (neighbor_count(i) /= expected) call fail('neighbor count differs from naive search') end do call sph_db_free() end subroutine check_naive_consistency subroutine check_neighbor_stats() integer :: max_neighbors real(rk) :: average_neighbors call sph_db_allocate(3, ierr) call assert_ok(ierr, 'stats allocation failed') h = 0.25_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/0.2_rk, 0.0_rk, 0.0_rk/) x(:,3) = (/1.0_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild(ierr) call assert_ok(ierr, 'stats rebuild failed') call sph_neighbor_get_stats(max_neighbors, average_neighbors, ierr) call assert_ok(ierr, 'neighbor stats failed') if (max_neighbors /= 1) call fail('max neighbor stat mismatch') if (abs(average_neighbors - 2.0_rk / 3.0_rk) > 1.0e-12_rk) then call fail('average neighbor stat mismatch') end if call sph_db_free() end subroutine check_neighbor_stats integer function naive_neighbor_count(i) integer, intent(in) :: i integer :: j real(rk) :: rij(3) real(rk) :: distance real(rk) :: radius naive_neighbor_count = 0 do j = 1, size(h) if (j == i) cycle rij = x(:,i) - x(:,j) distance = sqrt(sum(rij * rij)) radius = 2.0_rk * max(h(i), h(j)) if (distance <= radius) naive_neighbor_count = naive_neighbor_count + 1 end do end function naive_neighbor_count subroutine check_capacity_error() call sph_db_allocate(2, ierr) call assert_ok(ierr, 'capacity allocation failed') h = 1.0_rk x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/) x(:,2) = (/0.1_rk, 0.0_rk, 0.0_rk/) call sph_neighbor_rebuild_with_capacity(0, ierr) if (ierr == SPH_SUCCESS) call fail('neighbor capacity error was not reported') call sph_db_free() end subroutine check_capacity_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 subroutine fail(message) character(len=*), intent(in) :: message write(*, '(A)') trim(message) stop 1 end subroutine fail end program test_neighbor