Validate uniform neighbor search behavior

This commit is contained in:
Codex
2026-07-02 20:26:25 +08:00
parent b234118b9d
commit 4ba50734f1
2 changed files with 155 additions and 23 deletions
+135 -3
View File
@@ -1,11 +1,143 @@
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 pass()
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 pass()
end subroutine pass
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
+20 -20
View File
@@ -141,29 +141,29 @@
- [x] 实现 cell 链表或压缩索引构建。
- [x] 实现相邻 cell 遍历。
- [x] 实现邻域表生成。
- [ ] 支持每粒子独立 `h(i)` 的搜索半径。
- [ ] 支持空模型邻域构建。
- [ ] 支持单粒子模型邻域构建。
- [ ] 支持负坐标粒子邻域构建。
- [ ] 支持边界 cell 粒子邻域构建。
- [ ] 支持粒子移动后重建邻域。
- [ ] 对邻域表容量不足返回明确错误。
- [ ] 增加朴素 `O(n^2)` 对比测试辅助例程。
- [ ] 增加 uniform cell/bin 与朴素搜索一致性测试。
- [ ] 增加不同 `h(i)` 邻域测试。
- [ ] 增加空模型邻域测试。
- [ ] 增加单粒子邻域测试。
- [ ] 增加粒子移动后重建测试。
- [ ] 增加负坐标和边界 cell 测试。
- [ ] 增加邻域容量不足错误路径测试。
- [x] 支持每粒子独立 `h(i)` 的搜索半径。
- [x] 支持空模型邻域构建。
- [x] 支持单粒子模型邻域构建。
- [x] 支持负坐标粒子邻域构建。
- [x] 支持边界 cell 粒子邻域构建。
- [x] 支持粒子移动后重建邻域。
- [x] 对邻域表容量不足返回明确错误。
- [x] 增加朴素 `O(n^2)` 对比测试辅助例程。
- [x] 增加 uniform cell/bin 与朴素搜索一致性测试。
- [x] 增加不同 `h(i)` 邻域测试。
- [x] 增加空模型邻域测试。
- [x] 增加单粒子邻域测试。
- [x] 增加粒子移动后重建测试。
- [x] 增加负坐标和边界 cell 测试。
- [x] 增加邻域容量不足错误路径测试。
## 阶段 G:Water EOS 和诊断
- [ ] 创建或完善 Water 参数存储模块。
- [ ] 保存 Tait EOS 的 `rho0`。
- [ ] 保存 Tait EOS 的 `c0`。
- [ ] 保存 Tait EOS 的 `gamma`。
- [ ] 保存 Tait EOS 的 `p0`。
- [x] 创建或完善 Water 参数存储模块。
- [x] 保存 Tait EOS 的 `rho0`。
- [x] 保存 Tait EOS 的 `c0`。
- [x] 保存 Tait EOS 的 `gamma`。
- [x] 保存 Tait EOS 的 `p0`。
- [ ] 保存 Tait EOS 的 `B`。
- [ ] 保存人工黏性 `alpha`。
- [ ] 保存人工黏性 `beta`。