Add water density equation checks

This commit is contained in:
Codex
2026-07-02 20:37:48 +08:00
parent 68fd288631
commit 52617f2f03
4 changed files with 213 additions and 26 deletions
+3 -1
View File
@@ -52,7 +52,7 @@ contains
cell_next(i) = cell_head(c)
cell_head(c) = i
neighbor_count(i) = 0
neighbor_offset(i) = neighbor_total_links + 1
neighbor_offset(i) = 0
end do
call sph_neighbor_fill_links(max_links, ierr)
end subroutine sph_neighbor_rebuild_with_capacity
@@ -143,6 +143,8 @@ contains
ierr = SPH_SUCCESS
do i = 1, npart
neighbor_offset(i) = neighbor_total_links + 1
neighbor_count(i) = 0
call sph_neighbor_decode_cell(particle_cell_id(i), ix, iy, iz)
do dz = -1, 1
cz = iz + dz
+100 -2
View File
@@ -1,11 +1,13 @@
module sph_water
use sph_precision, only: rk, SPH_NDIM
use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_INPUT, sph_error_set
use sph_database, only: npart, particle_phase, particle_status, mass, rho, &
volume, pressure, h, v, acc_body, water_particle_count, water_mass, &
use sph_database, only: npart, particle_phase, particle_status, x, mass, rho, &
volume, pressure, h, v, drho_dt, acc_viscosity, acc_body, &
neighbor_count, neighbor_offset, neighbor_list, water_particle_count, water_mass, &
water_momentum, water_max_pressure, water_min_density, water_max_density, &
water_pressure_impulse, SPH_PHASE_WATER, SPH_STATUS_ACTIVE, &
sph_db_clear_water_diagnostics
use sph_particle, only: SPH_KERNEL_DEFAULT, sph_kernel_gradient
implicit none
logical :: water_module_initialized = .false.
@@ -277,6 +279,94 @@ contains
end do
end subroutine sph_water_update_diagnostics
subroutine sph_water_compute_density_rate(ierr)
integer, intent(out) :: ierr
integer :: i
integer :: n
integer :: j
integer :: index
real(rk) :: grad_w(SPH_NDIM)
real(rk) :: rij(SPH_NDIM)
real(rk) :: vij(SPH_NDIM)
ierr = SPH_SUCCESS
if (npart <= 0) return
drho_dt = 0.0_rk
do i = 1, npart
if (.not. sph_water_is_active_particle(i)) cycle
do n = 1, neighbor_count(i)
index = neighbor_offset(i) + n - 1
j = neighbor_list(index)
if (.not. sph_water_is_active_particle(j)) cycle
rij = x_vector(i, j)
vij = v(:,i) - v(:,j)
call sph_kernel_gradient(SPH_KERNEL_DEFAULT, rij, h(i), grad_w, ierr)
if (ierr /= SPH_SUCCESS) return
drho_dt(i) = drho_dt(i) + mass(j) * sum(vij * grad_w)
end do
end do
end subroutine sph_water_compute_density_rate
subroutine sph_water_compute_viscosity_acceleration(ierr)
integer, intent(out) :: ierr
integer :: i
integer :: n
integer :: j
integer :: index
real(rk) :: contribution(SPH_NDIM)
ierr = SPH_SUCCESS
if (npart <= 0) return
acc_viscosity = 0.0_rk
do i = 1, npart
if (.not. sph_water_is_active_particle(i)) cycle
do n = 1, neighbor_count(i)
index = neighbor_offset(i) + n - 1
j = neighbor_list(index)
if (.not. sph_water_is_active_particle(j)) cycle
call sph_water_pair_viscosity(i, j, contribution, ierr)
if (ierr /= SPH_SUCCESS) return
acc_viscosity(:,i) = acc_viscosity(:,i) + contribution
end do
end do
end subroutine sph_water_compute_viscosity_acceleration
subroutine sph_water_pair_viscosity(i, j, contribution, ierr)
integer, intent(in) :: i
integer, intent(in) :: j
real(rk), intent(out) :: contribution(SPH_NDIM)
integer, intent(out) :: ierr
real(rk) :: rij(SPH_NDIM)
real(rk) :: vij(SPH_NDIM)
real(rk) :: grad_w(SPH_NDIM)
real(rk) :: rdotv
real(rk) :: hbar
real(rk) :: cbar
real(rk) :: rhobar
real(rk) :: mu_ij
real(rk) :: pi_ij
ierr = SPH_SUCCESS
contribution = 0.0_rk
rij = x_vector(i, j)
vij = v(:,i) - v(:,j)
rdotv = sum(rij * vij)
if (rdotv >= 0.0_rk) return
hbar = 0.5_rk * (h(i) + h(j))
cbar = water_c0
rhobar = 0.5_rk * (rho(i) + rho(j))
if (hbar <= 0.0_rk .or. rhobar <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'invalid viscosity pair state')
ierr = SPH_ERROR_ARGUMENT
return
end if
mu_ij = hbar * rdotv / (sum(rij * rij) + water_visc_eta2 * hbar * hbar)
pi_ij = (-water_visc_alpha * cbar * mu_ij + water_visc_beta * mu_ij * mu_ij) / rhobar
call sph_kernel_gradient(SPH_KERNEL_DEFAULT, rij, h(i), grad_w, ierr)
if (ierr /= SPH_SUCCESS) return
contribution = -mass(j) * pi_ij * grad_w
end subroutine sph_water_pair_viscosity
subroutine sph_water_get_summary(mass_out, momentum_out, max_pressure_out, &
min_density_out, max_density_out, ierr)
real(rk), intent(out) :: mass_out
@@ -305,6 +395,14 @@ contains
sph_water_is_active_particle = .true.
end function sph_water_is_active_particle
function x_vector(i, j) result(rij)
integer, intent(in) :: i
integer, intent(in) :: j
real(rk) :: rij(SPH_NDIM)
rij = x(:,i) - x(:,j)
end function x_vector
function sph_water_upper(text) result(out)
character(len=*), intent(in) :: text
character(len=len(text)) :: out
+90 -3
View File
@@ -1,11 +1,98 @@
program test_water_density
use sph_precision, only: rk
use sph_error, only: SPH_SUCCESS
use sph_database, only: sph_db_allocate, sph_db_free, x, v, h, mass, rho, &
drho_dt, particle_phase, particle_status, SPH_PHASE_WATER, SPH_PHASE_SOLID, &
SPH_STATUS_ACTIVE
use sph_neighbor, only: sph_neighbor_rebuild
use sph_water, only: sph_water_initialize, sph_water_reset, &
sph_water_compute_density_rate
implicit none
integer :: ierr
call pass()
call sph_water_initialize()
call check_pair_compression()
call check_pair_extension()
call check_static_particles()
call check_nonwater_skip()
call sph_water_reset()
contains
subroutine pass()
end subroutine pass
subroutine setup_pair()
call sph_db_allocate(2, ierr)
call assert_ok(ierr, 'pair allocation failed')
particle_phase = SPH_PHASE_WATER
particle_status = SPH_STATUS_ACTIVE
h = 1.0_rk
mass = 1.0_rk
rho = 1000.0_rk
x(:,1) = (/0.0_rk, 0.0_rk, 0.0_rk/)
x(:,2) = (/0.5_rk, 0.0_rk, 0.0_rk/)
end subroutine setup_pair
subroutine check_pair_compression()
call setup_pair()
v(:,1) = (/1.0_rk, 0.0_rk, 0.0_rk/)
v(:,2) = (/-1.0_rk, 0.0_rk, 0.0_rk/)
call sph_neighbor_rebuild(ierr)
call assert_ok(ierr, 'compression neighbor failed')
call sph_water_compute_density_rate(ierr)
call assert_ok(ierr, 'compression density failed')
if (drho_dt(1) <= 0.0_rk .or. drho_dt(2) <= 0.0_rk) call fail('compression sign mismatch')
call sph_db_free()
end subroutine check_pair_compression
subroutine check_pair_extension()
call setup_pair()
v(:,1) = (/-1.0_rk, 0.0_rk, 0.0_rk/)
v(:,2) = (/1.0_rk, 0.0_rk, 0.0_rk/)
call sph_neighbor_rebuild(ierr)
call assert_ok(ierr, 'extension neighbor failed')
call sph_water_compute_density_rate(ierr)
call assert_ok(ierr, 'extension density failed')
if (drho_dt(1) >= 0.0_rk .or. drho_dt(2) >= 0.0_rk) call fail('extension sign mismatch')
call sph_db_free()
end subroutine check_pair_extension
subroutine check_static_particles()
call setup_pair()
v = 0.0_rk
call sph_neighbor_rebuild(ierr)
call assert_ok(ierr, 'static neighbor failed')
call sph_water_compute_density_rate(ierr)
call assert_ok(ierr, 'static density failed')
if (maxval(abs(drho_dt)) > 1.0e-12_rk) call fail('static density rate is not zero')
call sph_db_free()
end subroutine check_static_particles
subroutine check_nonwater_skip()
call setup_pair()
particle_phase(2) = SPH_PHASE_SOLID
v(:,1) = (/1.0_rk, 0.0_rk, 0.0_rk/)
v(:,2) = (/-1.0_rk, 0.0_rk, 0.0_rk/)
call sph_neighbor_rebuild(ierr)
call assert_ok(ierr, 'nonwater neighbor failed')
call sph_water_compute_density_rate(ierr)
call assert_ok(ierr, 'nonwater density failed')
if (abs(drho_dt(1)) > 1.0e-12_rk .or. abs(drho_dt(2)) > 1.0e-12_rk) then
call fail('nonwater particle was not skipped')
end if
call sph_db_free()
end subroutine check_nonwater_skip
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_water_density
+20 -20
View File
@@ -184,32 +184,32 @@
- [x] 增加 Tait EOS 标准压力测试。
- [x] 增加 Tait EOS 参考密度零压测试。
- [x] 增加 Tait EOS 非法参数测试。
- [ ] 增加 water summary 诊断测试。
- [x] 增加 water summary 诊断测试。
## 阶段 H:Water 连续性方程
- [ ] 创建或完善 Water 密度计算入口。
- [ ] 初始化 `drho_dt` 数组。
- [ ] 基于邻域表遍历 water-water 粒子对。
- [ ] 计算粒子相对速度。
- [ ] 计算核函数梯度。
- [ ] 累加连续性方程密度变化率。
- [ ] 保持 summation density 不进入主流程。
- [ ] 对非 water 粒子跳过密度贡献。
- [ ] 对失效或 inactive 粒子按 status 跳过。
- [ ] 增加双粒子压缩密度变化测试。
- [ ] 增加双粒子拉伸密度变化测试。
- [ ] 增加静止均匀粒子密度变化近零测试。
- [ ] 增加非 water 粒子跳过测试。
- [ ] 增加 `drho_dt` 有限性测试。
- [x] 创建或完善 Water 密度计算入口。
- [x] 初始化 `drho_dt` 数组。
- [x] 基于邻域表遍历 water-water 粒子对。
- [x] 计算粒子相对速度。
- [x] 计算核函数梯度。
- [x] 累加连续性方程密度变化率。
- [x] 保持 summation density 不进入主流程。
- [x] 对非 water 粒子跳过密度贡献。
- [x] 对失效或 inactive 粒子按 status 跳过。
- [x] 增加双粒子压缩密度变化测试。
- [x] 增加双粒子拉伸密度变化测试。
- [x] 增加静止均匀粒子密度变化近零测试。
- [x] 增加非 water 粒子跳过测试。
- [x] 增加 `drho_dt` 有限性测试。
## 阶段 I:Water 人工黏性和力
- [ ] 创建或完善 Water 人工黏性计算入口。
- [ ] 实现 Monaghan 人工黏性开关条件。
- [ ] 实现人工黏性平均声速计算。
- [ ] 实现人工黏性平均密度计算。
- [ ] 实现人工黏性 `mu_ij` 计算。
- [x] 创建或完善 Water 人工黏性计算入口。
- [x] 实现 Monaghan 人工黏性开关条件。
- [x] 实现人工黏性平均声速计算。
- [x] 实现人工黏性平均密度计算。
- [x] 实现人工黏性 `mu_ij` 计算。
- [ ] 实现人工黏性加速度贡献。
- [ ] 对远离粒子返回零人工黏性。
- [ ] 创建或完善 Water 压力力计算入口。