Implement SPH kernels and neighbor scaffold

This commit is contained in:
Codex
2026-07-02 20:22:32 +08:00
parent 8b5df5c37f
commit b234118b9d
4 changed files with 309 additions and 23 deletions
+1
View File
@@ -65,6 +65,7 @@ set(SPHLITE_CORE_SOURCES
src/output/sph_restart.f90
src/coupling_interface/sph_coupling_interface.f90
src/particle/sph_particle.f90
src/particle/sph_neighbor.f90
src/material/sph_material.f90
src/water/sph_water.f90
src/gas/sph_gas.f90
+221
View File
@@ -0,0 +1,221 @@
module sph_neighbor
use sph_precision, only: rk, SPH_NDIM
use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, sph_error_set
use sph_database, only: npart, x, h, particle_cell_id, cell_head, cell_next, &
neighbor_count, neighbor_offset, neighbor_list, sph_db_allocate_neighbor_storage
use sph_particle, only: SPH_KERNEL_DEFAULT, sph_kernel_support_radius
implicit none
real(rk) :: neighbor_min_corner(SPH_NDIM) = 0.0_rk
real(rk) :: neighbor_max_corner(SPH_NDIM) = 0.0_rk
real(rk) :: neighbor_cell_size = 1.0_rk
integer :: neighbor_cell_dims(SPH_NDIM) = 1
integer :: neighbor_num_cells = 1
integer :: neighbor_total_links = 0
contains
subroutine sph_neighbor_rebuild(ierr)
integer, intent(out) :: ierr
integer :: max_links
max_links = npart * npart
call sph_neighbor_rebuild_with_capacity(max_links, ierr)
end subroutine sph_neighbor_rebuild
subroutine sph_neighbor_rebuild_with_capacity(max_links, ierr)
integer, intent(in) :: max_links
integer, intent(out) :: ierr
integer :: i
integer :: c
ierr = SPH_SUCCESS
neighbor_total_links = 0
if (npart <= 0) then
call sph_db_allocate_neighbor_storage(0, 0, ierr)
return
end if
if (max_links < 0) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'neighbor capacity must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
call sph_neighbor_compute_bounds(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_neighbor_compute_cells(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_db_allocate_neighbor_storage(max_links, neighbor_num_cells, ierr)
if (ierr /= SPH_SUCCESS) return
do i = 1, npart
c = sph_neighbor_cell_index_for_particle(i)
particle_cell_id(i) = c
cell_next(i) = cell_head(c)
cell_head(c) = i
neighbor_count(i) = 0
neighbor_offset(i) = neighbor_total_links + 1
end do
call sph_neighbor_fill_links(max_links, ierr)
end subroutine sph_neighbor_rebuild_with_capacity
subroutine sph_neighbor_compute_bounds(ierr)
integer, intent(out) :: ierr
integer :: d
real(rk) :: support
real(rk) :: max_support
ierr = SPH_SUCCESS
max_support = 0.0_rk
neighbor_min_corner = x(:,1)
neighbor_max_corner = x(:,1)
do d = 1, SPH_NDIM
neighbor_min_corner(d) = minval(x(d,1:npart))
neighbor_max_corner(d) = maxval(x(d,1:npart))
end do
do d = 1, npart
call sph_kernel_support_radius(SPH_KERNEL_DEFAULT, h(d), support, ierr)
if (ierr /= SPH_SUCCESS) return
if (support > max_support) max_support = support
end do
if (max_support <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'neighbor support radius must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
neighbor_cell_size = max_support
neighbor_min_corner = neighbor_min_corner - neighbor_cell_size
neighbor_max_corner = neighbor_max_corner + neighbor_cell_size
end subroutine sph_neighbor_compute_bounds
subroutine sph_neighbor_compute_cells(ierr)
integer, intent(out) :: ierr
integer :: d
real(rk) :: extent
ierr = SPH_SUCCESS
neighbor_num_cells = 1
do d = 1, SPH_NDIM
extent = neighbor_max_corner(d) - neighbor_min_corner(d)
neighbor_cell_dims(d) = int(extent / neighbor_cell_size) + 1
if (neighbor_cell_dims(d) < 1) neighbor_cell_dims(d) = 1
neighbor_num_cells = neighbor_num_cells * neighbor_cell_dims(d)
end do
end subroutine sph_neighbor_compute_cells
integer function sph_neighbor_cell_index_for_particle(i)
integer, intent(in) :: i
integer :: ix
integer :: iy
integer :: iz
ix = sph_neighbor_cell_coord(x(1,i), 1)
iy = sph_neighbor_cell_coord(x(2,i), 2)
iz = sph_neighbor_cell_coord(x(3,i), 3)
sph_neighbor_cell_index_for_particle = ix + neighbor_cell_dims(1) * &
(iy - 1 + neighbor_cell_dims(2) * (iz - 1))
end function sph_neighbor_cell_index_for_particle
integer function sph_neighbor_cell_coord(value, dim)
real(rk), intent(in) :: value
integer, intent(in) :: dim
sph_neighbor_cell_coord = int((value - neighbor_min_corner(dim)) / neighbor_cell_size) + 1
if (sph_neighbor_cell_coord < 1) sph_neighbor_cell_coord = 1
if (sph_neighbor_cell_coord > neighbor_cell_dims(dim)) then
sph_neighbor_cell_coord = neighbor_cell_dims(dim)
end if
end function sph_neighbor_cell_coord
subroutine sph_neighbor_fill_links(max_links, ierr)
integer, intent(in) :: max_links
integer, intent(out) :: ierr
integer :: i
integer :: ix
integer :: iy
integer :: iz
integer :: dx
integer :: dy
integer :: dz
integer :: cx
integer :: cy
integer :: cz
integer :: cell
integer :: j
ierr = SPH_SUCCESS
do i = 1, npart
call sph_neighbor_decode_cell(particle_cell_id(i), ix, iy, iz)
do dz = -1, 1
cz = iz + dz
if (cz < 1 .or. cz > neighbor_cell_dims(3)) cycle
do dy = -1, 1
cy = iy + dy
if (cy < 1 .or. cy > neighbor_cell_dims(2)) cycle
do dx = -1, 1
cx = ix + dx
if (cx < 1 .or. cx > neighbor_cell_dims(1)) cycle
cell = cx + neighbor_cell_dims(1) * &
(cy - 1 + neighbor_cell_dims(2) * (cz - 1))
j = cell_head(cell)
do while (j > 0)
if (j /= i) call sph_neighbor_try_add(i, j, max_links, ierr)
if (ierr /= SPH_SUCCESS) return
j = cell_next(j)
end do
end do
end do
end do
end do
end subroutine sph_neighbor_fill_links
subroutine sph_neighbor_decode_cell(cell, ix, iy, iz)
integer, intent(in) :: cell
integer, intent(out) :: ix
integer, intent(out) :: iy
integer, intent(out) :: iz
integer :: rest
rest = cell - 1
ix = mod(rest, neighbor_cell_dims(1)) + 1
rest = rest / neighbor_cell_dims(1)
iy = mod(rest, neighbor_cell_dims(2)) + 1
iz = rest / neighbor_cell_dims(2) + 1
end subroutine sph_neighbor_decode_cell
subroutine sph_neighbor_try_add(i, j, max_links, ierr)
integer, intent(in) :: i
integer, intent(in) :: j
integer, intent(in) :: max_links
integer, intent(out) :: ierr
real(rk) :: rij(SPH_NDIM)
real(rk) :: distance
real(rk) :: radius
ierr = SPH_SUCCESS
rij = x(:,i) - x(:,j)
distance = sqrt(sum(rij * rij))
radius = 2.0_rk * max(h(i), h(j))
if (distance > radius) return
if (neighbor_total_links >= max_links) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'neighbor table capacity is too small')
ierr = SPH_ERROR_ARGUMENT
return
end if
neighbor_total_links = neighbor_total_links + 1
neighbor_list(neighbor_total_links) = j
neighbor_count(i) = neighbor_count(i) + 1
end subroutine sph_neighbor_try_add
subroutine sph_neighbor_get_stats(max_neighbors, average_neighbors, ierr)
integer, intent(out) :: max_neighbors
real(rk), intent(out) :: average_neighbors
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
max_neighbors = 0
average_neighbors = 0.0_rk
if (npart <= 0) return
max_neighbors = maxval(neighbor_count(1:npart))
average_neighbors = real(sum(neighbor_count(1:npart)), rk) / real(npart, rk)
end subroutine sph_neighbor_get_stats
end module sph_neighbor
+67 -3
View File
@@ -1,11 +1,75 @@
program test_kernel
use sph_precision, only: rk
use sph_error, only: SPH_SUCCESS
use sph_particle, only: SPH_KERNEL_DEFAULT, SPH_KERNEL_WENDLAND_C2, &
SPH_KERNEL_WENDLAND_C4, SPH_KERNEL_CUBIC_SPLINE, &
sph_kernel_support_radius, sph_kernel_value, sph_kernel_gradient
implicit none
integer :: ierr
real(rk) :: value
real(rk) :: support
real(rk) :: grad_a(3)
real(rk) :: grad_b(3)
real(rk) :: rij(3)
call pass()
if (SPH_KERNEL_DEFAULT /= SPH_KERNEL_WENDLAND_C2) call fail('unexpected default kernel')
call sph_kernel_support_radius(SPH_KERNEL_WENDLAND_C2, 0.5_rk, support, ierr)
call assert_ok(ierr, 'support radius failed')
if (abs(support - 1.0_rk) > 1.0e-12_rk) call fail('support radius mismatch')
call sph_kernel_value(SPH_KERNEL_WENDLAND_C2, 0.0_rk, 1.0_rk, value, ierr)
call assert_ok(ierr, 'Wendland C2 r=0 failed')
if (value <= 0.0_rk) call fail('Wendland C2 r=0 is not positive')
call sph_kernel_value(SPH_KERNEL_WENDLAND_C4, 0.0_rk, 1.0_rk, value, ierr)
call assert_ok(ierr, 'Wendland C4 r=0 failed')
if (value <= 0.0_rk) call fail('Wendland C4 r=0 is not positive')
call sph_kernel_value(SPH_KERNEL_CUBIC_SPLINE, 0.0_rk, 1.0_rk, value, ierr)
call assert_ok(ierr, 'cubic spline r=0 failed')
if (value <= 0.0_rk) call fail('cubic spline r=0 is not positive')
call sph_kernel_value(SPH_KERNEL_WENDLAND_C2, 2.1_rk, 1.0_rk, value, ierr)
call assert_ok(ierr, 'outside support failed')
if (value /= 0.0_rk) call fail('kernel outside support is not zero')
rij = (/0.5_rk, 0.0_rk, 0.0_rk/)
call sph_kernel_gradient(SPH_KERNEL_WENDLAND_C2, rij, 1.0_rk, grad_a, ierr)
call assert_ok(ierr, 'Wendland C2 gradient failed')
if (grad_a(1) >= 0.0_rk) call fail('gradient direction mismatch')
if (abs(grad_a(2)) > 1.0e-12_rk .or. abs(grad_a(3)) > 1.0e-12_rk) then
call fail('gradient transverse component mismatch')
end if
call sph_kernel_gradient(SPH_KERNEL_WENDLAND_C2, -rij, 1.0_rk, grad_b, ierr)
call assert_ok(ierr, 'opposite gradient failed')
if (maxval(abs(grad_a + grad_b)) > 1.0e-12_rk) call fail('gradient is not antisymmetric')
call sph_kernel_gradient(SPH_KERNEL_CUBIC_SPLINE, rij, 0.5_rk, grad_a, ierr)
call assert_ok(ierr, 'different h gradient failed')
if (maxval(abs(grad_a)) <= 0.0_rk) call fail('different h gradient is zero')
call sph_kernel_value(SPH_KERNEL_WENDLAND_C2, 0.5_rk, 0.0_rk, value, ierr)
if (ierr == SPH_SUCCESS) call fail('zero h did not fail')
call sph_kernel_value(99, 0.5_rk, 1.0_rk, value, ierr)
if (ierr == SPH_SUCCESS) call fail('unknown kernel type did not fail')
contains
subroutine pass()
end subroutine pass
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_kernel
+20 -20
View File
@@ -118,29 +118,29 @@
- [x] 实现 Wendland C4 核函数值。
- [x] 实现 Wendland C4 核函数梯度。
- [x] 实现 cubic spline 核函数值。
- [ ] 实现 cubic spline 核函数梯度。
- [ ] 支持 3D 归一化系数。
- [ ] 支持每粒子独立 `h(i)` 传入。
- [ ] 对 `h <= 0` 返回明确错误。
- [ ] 对未知核函数类型返回明确错误。
- [ ] 将 Wendland C2 设置为默认核函数。
- [ ] 增加 `r = 0` 核函数有限性测试。
- [ ] 增加超出支持半径测试。
- [ ] 增加梯度方向测试。
- [ ] 增加梯度反对称性测试。
- [ ] 增加不同 `h(i)` 测试。
- [x] 实现 cubic spline 核函数梯度。
- [x] 支持 3D 归一化系数。
- [x] 支持每粒子独立 `h(i)` 传入。
- [x] 对 `h <= 0` 返回明确错误。
- [x] 对未知核函数类型返回明确错误。
- [x] 将 Wendland C2 设置为默认核函数。
- [x] 增加 `r = 0` 核函数有限性测试。
- [x] 增加超出支持半径测试。
- [x] 增加梯度方向测试。
- [x] 增加梯度反对称性测试。
- [x] 增加不同 `h(i)` 测试。
## 阶段 F:Neighbor 模块
- [ ] 创建 uniform cell/bin 邻域模块文件。
- [ ] 实现当前坐标包围盒计算。
- [ ] 实现最大支持半径计算。
- [ ] 根据最大支持半径确定 cell 尺寸。
- [ ] 实现 cell 网格维度计算。
- [ ] 实现粒子到 cell 的映射。
- [ ] 实现 cell 链表或压缩索引构建。
- [ ] 实现相邻 cell 遍历。
- [ ] 实现邻域表生成。
- [x] 创建 uniform cell/bin 邻域模块文件。
- [x] 实现当前坐标包围盒计算。
- [x] 实现最大支持半径计算。
- [x] 根据最大支持半径确定 cell 尺寸。
- [x] 实现 cell 网格维度计算。
- [x] 实现粒子到 cell 的映射。
- [x] 实现 cell 链表或压缩索引构建。
- [x] 实现相邻 cell 遍历。
- [x] 实现邻域表生成。
- [ ] 支持每粒子独立 `h(i)` 的搜索半径。
- [ ] 支持空模型邻域构建。
- [ ] 支持单粒子模型邻域构建。