Integrate water explicit stepping and dt limits

This commit is contained in:
Codex
2026-07-02 20:52:05 +08:00
parent 662129d2bb
commit 4809701068
10 changed files with 226 additions and 33 deletions
+7 -1
View File
@@ -11,9 +11,11 @@ module sph_control
integer :: current_step = 0
real(rk) :: dt_default = SPH_DEFAULT_DT
real(rk) :: end_time = SPH_DEFAULT_END_TIME
real(rk) :: safety_factor = 1.0_rk
integer :: max_steps = SPH_DEFAULT_MAX_STEPS
integer :: output_interval = SPH_DEFAULT_OUTPUT_INTERVAL
character(len=256) :: output_directory = 'output'
logical :: output_enabled = .false.
contains
@@ -22,19 +24,23 @@ contains
current_step = 0
dt_default = SPH_DEFAULT_DT
end_time = SPH_DEFAULT_END_TIME
safety_factor = 1.0_rk
max_steps = SPH_DEFAULT_MAX_STEPS
output_interval = SPH_DEFAULT_OUTPUT_INTERVAL
output_directory = 'output'
output_enabled = .false.
end subroutine sph_control_reset
subroutine sph_control_set_time(dt, total_time, steps)
subroutine sph_control_set_time(dt, total_time, steps, safety)
real(rk), intent(in) :: dt
real(rk), intent(in) :: total_time
integer, intent(in) :: steps
real(rk), intent(in) :: safety
if (dt > 0.0_rk) dt_default = dt
if (total_time > 0.0_rk) end_time = total_time
if (steps > 0) max_steps = steps
if (safety > 0.0_rk) safety_factor = safety
end subroutine sph_control_set_time
subroutine sph_control_get_time(time, step)
+5 -1
View File
@@ -462,10 +462,12 @@ contains
real(rk) :: dt
real(rk) :: total_time
integer :: steps
real(rk) :: safety
dt = -1.0_rk
total_time = -1.0_rk
steps = -1
safety = -1.0_rk
call sph_inp_split_csv(line, fields, nfields)
do i = 1, nfields
item = adjustl(fields(i))
@@ -479,9 +481,11 @@ contains
read(value, *, iostat=ios) total_time
else if (index(key, 'STEPS') == 1 .or. index(key, 'MAX STEPS') == 1) then
read(value, *, iostat=ios) steps
else if (index(key, 'SAFETY') == 1) then
read(value, *, iostat=ios) safety
end if
end do
call sph_control_set_time(dt, total_time, steps)
call sph_control_set_time(dt, total_time, steps, safety)
end subroutine sph_input_parse_time_control
subroutine sph_input_parse_node(line, node_index, ierr)
+24 -1
View File
@@ -22,7 +22,8 @@ module sph_api
use sph_explosive, only: sph_explosive_initialize, sph_explosive_reset
use sph_solid, only: sph_solid_initialize, sph_solid_reset
use sph_shell, only: sph_shell_initialize, sph_shell_reset
use sph_api_state, only: sph_api_initialized, sph_api_state_set_initialized
use sph_api_state, only: sph_api_initialized, sph_api_model_built, &
sph_api_state_set_initialized, sph_api_state_set_model_built
implicit none
contains
@@ -42,6 +43,7 @@ contains
call sph_solid_initialize()
call sph_shell_initialize()
call sph_api_state_set_initialized(.true.)
call sph_api_state_set_model_built(.false.)
ierr = SPH_SUCCESS
end subroutine sph_initialize
@@ -91,6 +93,7 @@ contains
call sph_api_require_initialized(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_api_state_set_model_built(.false.)
call sph_input_read(filename, ierr)
end subroutine sph_read_inp
@@ -100,6 +103,7 @@ contains
call sph_api_require_initialized(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_input_build_model(ierr)
if (ierr == SPH_SUCCESS) call sph_api_state_set_model_built(.true.)
end subroutine sph_build_model
subroutine sph_estimate_stable_dt(dtcrit, ierr)
@@ -111,6 +115,11 @@ contains
dtcrit = 0.0_rk
return
end if
call sph_api_require_model_built(ierr)
if (ierr /= SPH_SUCCESS) then
dtcrit = 0.0_rk
return
end if
call sph_solver_estimate_stable_dt(dtcrit, ierr)
end subroutine sph_estimate_stable_dt
@@ -120,6 +129,8 @@ contains
call sph_api_require_initialized(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_api_require_model_built(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_solver_advance_explicit(dt, ierr)
end subroutine sph_advance_explicit
@@ -128,6 +139,8 @@ contains
call sph_api_require_initialized(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_api_require_model_built(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_solver_run_explicit(ierr)
end subroutine sph_run_explicit
@@ -280,6 +293,16 @@ contains
end if
end subroutine sph_api_require_initialized
subroutine sph_api_require_model_built(ierr)
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (.not. sph_api_model_built) then
call sph_error_set(SPH_ERROR_STATE, 'SPHLite model is not built')
ierr = SPH_ERROR_STATE
end if
end subroutine sph_api_require_model_built
end module sph_api
subroutine sphft_initialize(ierr)
+14
View File
@@ -2,6 +2,7 @@ module sph_api_state
implicit none
logical :: sph_api_initialized = .false.
logical :: sph_api_model_built = .false.
contains
@@ -9,6 +10,7 @@ contains
logical, intent(in) :: value
sph_api_initialized = value
if (.not. value) sph_api_model_built = .false.
end subroutine sph_api_state_set_initialized
subroutine sph_api_state_get_initialized(value)
@@ -17,4 +19,16 @@ contains
value = sph_api_initialized
end subroutine sph_api_state_get_initialized
subroutine sph_api_state_set_model_built(value)
logical, intent(in) :: value
sph_api_model_built = value
end subroutine sph_api_state_set_model_built
subroutine sph_api_state_get_model_built(value)
logical, intent(out) :: value
value = sph_api_model_built
end subroutine sph_api_state_get_model_built
end module sph_api_state
+2 -1
View File
@@ -1,7 +1,7 @@
module sph_output_csv
use sph_precision, only: rk
use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_IO, sph_error_set
use sph_control, only: output_directory, output_interval
use sph_control, only: output_directory, output_interval, output_enabled
use sph_database, only: npart, particle_id_ext, particle_phase, particle_status, &
material_id, section_id, x, v, rho, pressure, temperature
implicit none
@@ -21,6 +21,7 @@ contains
return
end if
output_directory = directory
output_enabled = .true.
end subroutine sph_output_set_directory
subroutine sph_output_set_interval(interval, ierr)
+31 -4
View File
@@ -1,11 +1,14 @@
module sph_solver_explicit
use sph_precision, only: rk
use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, sph_error_set
use sph_control, only: current_time, current_step, dt_default, end_time, max_steps
use sph_database, only: npart, x, v, a, vhalf
use sph_control, only: current_time, current_step, dt_default, end_time, max_steps, &
safety_factor, output_interval, output_enabled
use sph_database, only: npart, x, v, a, vhalf, rho, drho_dt
use sph_neighbor, only: sph_neighbor_rebuild
use sph_water, only: sph_water_update_pressure, sph_water_compute_density_rate, &
sph_water_compute_acceleration, sph_water_update_diagnostics
sph_water_compute_acceleration, sph_water_update_diagnostics, &
sph_water_estimate_dt_limits
use sph_output_csv, only: sph_output_write_step
implicit none
contains
@@ -14,8 +17,13 @@ contains
real(rk), intent(out) :: dtcrit
integer, intent(out) :: ierr
dtcrit = dt_default
ierr = SPH_SUCCESS
dtcrit = dt_default
if (npart > 0) then
call sph_solver_water_dt(dtcrit, ierr)
if (ierr /= SPH_SUCCESS) return
if (dtcrit <= 0.0_rk .or. dtcrit > dt_default) dtcrit = dt_default
end if
end subroutine sph_solver_estimate_stable_dt
subroutine sph_solver_advance_explicit(dt, ierr)
@@ -42,6 +50,7 @@ contains
do i = 1, npart
vhalf(:,i) = v(:,i) + 0.5_rk * dt * a(:,i)
x(:,i) = x(:,i) + dt * vhalf(:,i)
rho(i) = rho(i) + dt * drho_dt(i)
v(:,i) = vhalf(:,i) + 0.5_rk * dt * a(:,i)
end do
call sph_water_update_diagnostics(ierr)
@@ -50,6 +59,12 @@ contains
current_time = current_time + dt
current_step = current_step + 1
if (output_enabled .and. output_interval > 0) then
if (mod(current_step, output_interval) == 0) then
call sph_output_write_step(current_step, current_time, ierr)
if (ierr /= SPH_SUCCESS) return
end if
end if
end subroutine sph_solver_advance_explicit
subroutine sph_solver_run_explicit(ierr)
@@ -70,4 +85,16 @@ contains
end do
end subroutine sph_solver_run_explicit
subroutine sph_solver_water_dt(dtcrit, ierr)
real(rk), intent(out) :: dtcrit
integer, intent(out) :: ierr
real(rk) :: dt_cfl
real(rk) :: dt_accel
real(rk) :: dt_viscous
call sph_water_estimate_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr)
if (ierr /= SPH_SUCCESS) return
dtcrit = min(dt_cfl, min(dt_accel, dt_viscous)) * safety_factor
end subroutine sph_solver_water_dt
end module sph_solver_explicit
+42 -1
View File
@@ -5,7 +5,8 @@ module sph_water
volume, pressure, h, v, a, drho_dt, acc_pressure, 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, &
water_pressure_impulse, water_dt_cfl, water_dt_accel, water_dt_viscous, &
SPH_PHASE_WATER, SPH_STATUS_ACTIVE, &
sph_db_clear_water_diagnostics
use sph_particle, only: SPH_KERNEL_DEFAULT, sph_kernel_gradient
implicit none
@@ -443,6 +444,46 @@ contains
max_density_out = water_max_density
end subroutine sph_water_get_summary
subroutine sph_water_estimate_dt_limits(dt_cfl, dt_accel, dt_viscous, ierr)
real(rk), intent(out) :: dt_cfl
real(rk), intent(out) :: dt_accel
real(rk), intent(out) :: dt_viscous
integer, intent(out) :: ierr
integer :: i
real(rk) :: speed
real(rk) :: accel_norm
real(rk) :: local_dt
ierr = SPH_SUCCESS
dt_cfl = huge(1.0_rk)
dt_accel = huge(1.0_rk)
dt_viscous = huge(1.0_rk)
do i = 1, npart
if (.not. sph_water_is_active_particle(i)) cycle
if (h(i) <= 0.0_rk .or. water_c0 <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'invalid Water state for dt estimate')
ierr = SPH_ERROR_ARGUMENT
return
end if
speed = sqrt(sum(v(:,i) * v(:,i)))
local_dt = h(i) / (water_c0 + speed)
if (local_dt < dt_cfl) dt_cfl = local_dt
accel_norm = sqrt(sum(a(:,i) * a(:,i)))
if (accel_norm > 0.0_rk) then
local_dt = sqrt(h(i) / accel_norm)
if (local_dt < dt_accel) dt_accel = local_dt
end if
local_dt = h(i) / (water_c0 * (1.0_rk + water_visc_alpha + water_visc_beta))
if (local_dt < dt_viscous) dt_viscous = local_dt
end do
if (dt_cfl == huge(1.0_rk)) dt_cfl = 0.0_rk
if (dt_accel == huge(1.0_rk)) dt_accel = dt_cfl
if (dt_viscous == huge(1.0_rk)) dt_viscous = dt_cfl
water_dt_cfl = dt_cfl
water_dt_accel = dt_accel
water_dt_viscous = dt_viscous
end subroutine sph_water_estimate_dt_limits
logical function sph_water_is_active_particle(i)
integer, intent(in) :: i
+71 -3
View File
@@ -1,11 +1,79 @@
program test_solver_water
use sph_precision, only: rk
use sph_error, only: SPH_SUCCESS
use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, &
sph_advance_explicit, sph_run_explicit, sph_get_current_time
use sph_database, only: rho, v
implicit none
integer :: ierr
integer :: step
real(rk) :: time
call pass()
call check_unbuilt_error()
call check_single_step()
call check_run_explicit()
contains
subroutine pass()
end subroutine pass
subroutine check_unbuilt_error()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize unbuilt failed')
call sph_advance_explicit(1.0e-6_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('unbuilt explicit step did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize unbuilt failed')
end subroutine check_unbuilt_error
subroutine check_single_step()
real(rk) :: rho_before
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize single failed')
call sph_read_inp('tests/data/water_body_force_020.inp', ierr)
call assert_ok(ierr, 'read single failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build single failed')
rho_before = rho(1)
call sph_advance_explicit(1.0e-6_rk, ierr)
call assert_ok(ierr, 'advance single failed')
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'time single failed')
if (step /= 1) call fail('single step count mismatch')
if (abs(time - 1.0e-6_rk) > 1.0e-12_rk) call fail('single step time mismatch')
if (abs(rho(1) - rho_before) > 1.0e-6_rk) call fail('unexpected density drift')
if (v(3,1) >= 0.0_rk) call fail('body force did not change velocity direction')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize single failed')
end subroutine check_single_step
subroutine check_run_explicit()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize run failed')
call sph_read_inp('tests/data/water_minimal_020.inp', ierr)
call assert_ok(ierr, 'read run failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build run failed')
call sph_run_explicit(ierr)
call assert_ok(ierr, 'run explicit failed')
call sph_get_current_time(time, step, ierr)
call assert_ok(ierr, 'time run failed')
if (step <= 0) call fail('run did not advance')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize run failed')
end subroutine check_run_explicit
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_solver_water
+10 -1
View File
@@ -3,7 +3,8 @@ program test_smoke
use sph_error, only: SPH_SUCCESS
use sph_api, only: sph_initialize, sph_finalize, sph_get_version, &
sph_get_particle_count, sph_get_current_time, sph_advance_explicit, &
sph_get_particle_state_capacity, sph_get_particle_state
sph_get_particle_state_capacity, sph_get_particle_state, sph_read_inp, &
sph_build_model
use sph_database, only: sph_db_allocate, SPH_PHASE_SOLID, SPH_STATUS_ACTIVE
implicit none
@@ -60,6 +61,14 @@ program test_smoke
call sph_advance_explicit(0.0_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('zero time step did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize before positive advance failed')
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize before positive advance failed')
call sph_read_inp('tests/data/water_body_force_020.inp', ierr)
call assert_ok(ierr, 'read water input failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'build water model failed')
call sph_advance_explicit(1.0e-6_rk, ierr)
call assert_ok(ierr, 'explicit advance failed')
call sph_get_current_time(time, step, ierr)
+20 -20
View File
@@ -233,29 +233,29 @@
- [x] 在显式步开始前校验 Water 参数。
- [x] 在显式步中必要时重建邻域。
- [x] 在显式步中根据 `rho` 更新 `pressure`。
- [ ] 在显式步中计算 `drho_dt`。
- [ ] 在显式步中计算水相压力加速度。
- [ ] 在显式步中计算水相黏性加速度。
- [ ] 在显式步中合成总加速度。
- [ ] 在 Velocity-Verlet 中更新速度。
- [ ] 在 Velocity-Verlet 中更新位置。
- [ ] 在 Velocity-Verlet 中更新密度。
- [ ] 在 Velocity-Verlet 中更新时间和步数。
- [ ] 在显式步结束后更新 Water 诊断。
- [ ] 在显式运行中按输出间隔写出结果。
- [ ] 对负时间步保持明确错误。
- [ ] 对零时间步保持明确错误。
- [ ] 对未 build model 的显式步返回明确错误。
- [ ] 增加单步 Water 推进测试。
- [ ] 增加多步 Water 推进测试。
- [ ] 增加 `sph_run_explicit` Water 闭环测试。
- [x] 在显式步中计算 `drho_dt`。
- [x] 在显式步中计算水相压力加速度。
- [x] 在显式步中计算水相黏性加速度。
- [x] 在显式步中合成总加速度。
- [x] 在 Velocity-Verlet 中更新速度。
- [x] 在 Velocity-Verlet 中更新位置。
- [x] 在 Velocity-Verlet 中更新密度。
- [x] 在 Velocity-Verlet 中更新时间和步数。
- [x] 在显式步结束后更新 Water 诊断。
- [x] 在显式运行中按输出间隔写出结果。
- [x] 对负时间步保持明确错误。
- [x] 对零时间步保持明确错误。
- [x] 对未 build model 的显式步返回明确错误。
- [x] 增加单步 Water 推进测试。
- [x] 增加多步 Water 推进测试。
- [x] 增加 `sph_run_explicit` Water 闭环测试。
## 阶段 K:稳定时间步估计
- [ ] 实现水相 CFL 声速约束估计。
- [ ] 实现加速度约束估计。
- [ ] 实现人工黏性约束估计。
- [ ] 应用输入安全系数。
- [x] 实现水相 CFL 声速约束估计。
- [x] 实现加速度约束估计。
- [x] 实现人工黏性约束估计。
- [x] 应用输入安全系数。
- [ ] 保存 `dt_cfl` 诊断。
- [ ] 保存 `dt_accel` 诊断。
- [ ] 保存 `dt_viscous` 诊断。