Cover neighbor stats and water EOS edge cases

This commit is contained in:
Codex
2026-07-02 23:09:03 +08:00
parent 3e55c4cf4a
commit 45d292a100
3 changed files with 94 additions and 21 deletions
+30
View File
@@ -11,6 +11,9 @@ program test_water_eos
implicit none
integer :: ierr
real(rk) :: p
real(rk) :: p_ref
real(rk) :: p_compressed
real(rk) :: p_tension
real(rk) :: c
real(rk) :: total_mass
real(rk) :: momentum(3)
@@ -30,10 +33,19 @@ program test_water_eos
call sph_water_tait_pressure(1000.0_rk, p, ierr)
call assert_ok(ierr, 'reference pressure failed')
if (abs(p) > 1.0e-8_rk) call fail('reference density pressure is not zero')
p_ref = p
call sph_water_tait_pressure(1010.0_rk, p, ierr)
call assert_ok(ierr, 'compressed pressure failed')
if (p <= 0.0_rk) call fail('compressed water pressure is not positive')
p_compressed = p
call sph_water_tait_pressure(990.0_rk, p, ierr)
call assert_ok(ierr, 'tension pressure failed')
if (p >= p_ref) call fail('tension pressure strategy mismatch')
p_tension = p
if (.not. is_finite_scalar(p_tension)) call fail('tension pressure is not finite')
if (p_compressed <= p_ref) call fail('compressed pressure is not monotonic')
if (.not. is_finite_scalar(p_compressed)) call fail('compressed pressure is not finite')
if (abs(water_b - 1000.0_rk * 1500.0_rk * 1500.0_rk / 7.0_rk) > &
1.0e-6_rk) call fail('derived Tait B mismatch')
@@ -45,10 +57,22 @@ program test_water_eos
call sph_water_sound_speed(c, ierr)
call assert_ok(ierr, 'sound speed failed')
if (abs(c - 1500.0_rk) > 1.0e-12_rk) call fail('sound speed mismatch')
if (c <= 0.0_rk) call fail('sound speed is not positive')
call sph_water_tait_pressure(0.0_rk, p, ierr)
if (ierr == SPH_SUCCESS) call fail('zero density pressure did not fail')
call sph_water_set_eos('TAIT', 'WATER', -1000.0_rk, 1500.0_rk, &
7.0_rk, 0.0_rk, 0.0_rk, .false., ierr)
if (ierr == SPH_SUCCESS) call fail('bad EOS parameters did not fail')
call sph_water_set_eos('TAIT', 'WATER', 1000.0_rk, 0.0_rk, &
7.0_rk, 0.0_rk, 0.0_rk, .false., ierr)
if (ierr == SPH_SUCCESS) call fail('bad sound speed did not fail')
call sph_water_set_eos('TAIT', 'WATER', 1000.0_rk, 1500.0_rk, &
0.0_rk, 0.0_rk, 0.0_rk, .false., ierr)
if (ierr == SPH_SUCCESS) call fail('bad gamma did not fail')
call sph_water_set_eos('TAIT', 'WATER', 1000.0_rk, 1500.0_rk, &
7.0_rk, 0.0_rk, 0.0_rk, .true., ierr)
if (ierr == SPH_SUCCESS) call fail('bad Tait B did not fail')
call sph_water_set_eos('TAIT', 'WATER', 1000.0_rk, 1500.0_rk, &
7.0_rk, 0.0_rk, 0.0_rk, .false., ierr)
@@ -90,6 +114,12 @@ contains
if (ierr_value /= SPH_SUCCESS) call fail(message)
end subroutine assert_ok
logical function is_finite_scalar(value_in)
real(rk), intent(in) :: value_in
is_finite_scalar = value_in == value_in .and. abs(value_in) < huge(value_in)
end function is_finite_scalar
subroutine fail(message)
character(len=*), intent(in) :: message
+44 -1
View File
@@ -2,7 +2,9 @@ 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
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
@@ -15,8 +17,10 @@ program test_neighbor
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
@@ -130,6 +134,24 @@ contains
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')
@@ -163,6 +185,27 @@ contains
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
+20 -20
View File
@@ -141,29 +141,29 @@
- [x] 加固重复坐标粒子的邻域构建。
- [x] 加固极近坐标粒子的邻域构建。
- [x] 加固负坐标粒子的邻域构建。
- [ ] 加固稀疏粒子的包围盒计算。
- [ ] 加固稀疏粒子的 cell 维度控制。
- [ ] 加固不同 `h(i)` 对搜索半径的影响。
- [ ] 加固粒子移动后邻域重建。
- [ ] 加固邻域容量不足错误路径。
- [ ] 加固 naive `O(n^2)` 对比辅助检查。
- [ ] 加固 neighbor stats 最大邻居数。
- [ ] 加固 neighbor stats 平均邻居数。
- [ ] 确认 neighbor 测试保持小规模。
- [x] 加固稀疏粒子的包围盒计算。
- [x] 加固稀疏粒子的 cell 维度控制。
- [x] 加固不同 `h(i)` 对搜索半径的影响。
- [x] 加固粒子移动后邻域重建。
- [x] 加固邻域容量不足错误路径。
- [x] 加固 naive `O(n^2)` 对比辅助检查。
- [x] 加固 neighbor stats 最大邻居数。
- [x] 加固 neighbor stats 平均邻居数。
- [x] 确认 neighbor 测试保持小规模。
## 阶段 G:Water EOS 加固
- [ ] 加固 `rho = rho0` 时压力接近 `p0`。
- [ ] 加固 `rho > rho0` 时压力单调增加。
- [ ] 明确 `rho < rho0` 时负压策略。
- [ ] 为当前负压策略增加测试。
- [ ] 加固 `rho <= 0` 错误路径。
- [ ] 加固 `rho0 <= 0` 错误路径。
- [ ] 加固 `c0 <= 0` 错误路径。
- [ ] 加固 `gamma <= 0` 错误路径。
- [ ] 加固 `B <= 0` 错误路径。
- [ ] 加固声速诊断保持正值。
- [ ] 加固压力计算结果有限。
- [x] 加固 `rho = rho0` 时压力接近 `p0`。
- [x] 加固 `rho > rho0` 时压力单调增加。
- [x] 明确 `rho < rho0` 时负压策略。
- [x] 为当前负压策略增加测试。
- [x] 加固 `rho <= 0` 错误路径。
- [x] 加固 `rho0 <= 0` 错误路径。
- [x] 加固 `c0 <= 0` 错误路径。
- [x] 加固 `gamma <= 0` 错误路径。
- [x] 加固 `B <= 0` 错误路径。
- [x] 加固声速诊断保持正值。
- [x] 加固压力计算结果有限。
- [ ] 加固 `B` 自动推导结果。
- [ ] 加固显式给定 `B` 的结果。
- [ ] 加固 Water summary 中最大压力诊断。