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 implicit none integer :: ierr call check_empty_model() call check_single_particle() call check_move_rebuild() call check_negative_coordinates() call check_different_h() call check_naive_consistency() 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_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_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 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