diff --git a/CMakeLists.txt b/CMakeLists.txt index 97e0d57..c9e338b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -201,6 +201,12 @@ set_target_properties(test_gas_state PROPERTIES Fortran_MODULE_DIRECTORY "${SPHL add_test(NAME gas_state COMMAND test_gas_state) set_tests_properties(gas_state PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_gas_summary tests/module/test_gas_summary.f90) +target_link_libraries(test_gas_summary PRIVATE sphlite_core) +set_target_properties(test_gas_summary PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME gas_summary COMMAND test_gas_summary) +set_tests_properties(gas_summary PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_water_density tests/module/test_water_density.f90) target_link_libraries(test_water_density PRIVATE sphlite_core) set_target_properties(test_water_density PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") diff --git a/docs/04_mathematical_model_and_algorithms.md b/docs/04_mathematical_model_and_algorithms.md index b1312f4..76b8612 100644 --- a/docs/04_mathematical_model_and_algorithms.md +++ b/docs/04_mathematical_model_and_algorithms.md @@ -82,6 +82,14 @@ Riemann-SPH、Godunov-SPH 或其他强冲击格式作为后续升级方向,用 - 脉动周期估计。 - 气泡中心迁移。 +0.3.0 的 bubble metrics 只作为 `Gas` 诊断使用,不作为独立气泡模块,也不作为工程精度气泡脉动 benchmark。当前等效半径按所有 active Gas 粒子的总体积计算: + +```text +R_eq = (3 V_gas / 4 pi)^(1/3) +``` + +半径变化率和中心迁移量由相邻诊断状态差分得到,因此第一版等价于单气团近似;多气团连通分量、复杂水气界面追踪和多周期气泡脉动留给后续版本。 + 第一阶段水-气界面不做复杂界面重构,采用粒子相间基础压力/动量交换和气体团簇统计。 ## 6. 爆炸模型 diff --git a/tests/module/test_gas_summary.f90 b/tests/module/test_gas_summary.f90 new file mode 100644 index 0000000..06d2fc0 --- /dev/null +++ b/tests/module/test_gas_summary.f90 @@ -0,0 +1,122 @@ +program test_gas_summary + 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 + use sph_database, only: particle_status, volume, gas_particle_count, gas_mass, & + gas_volume, gas_momentum, gas_center, gas_mean_pressure, gas_max_pressure, & + gas_min_density, gas_max_density, gas_equivalent_radius, & + gas_previous_equivalent_radius, gas_radius_rate, gas_center_shift, & + SPH_STATUS_INACTIVE, SPH_STATUS_FAILED + use sph_gas, only: sph_gas_update_diagnostics + implicit none + + integer :: ierr + + call check_summary_values() + call check_inactive_and_failed_skip() + call check_bubble_metrics_history() + call check_empty_gas_defaults() + call check_zero_volume_error() + +contains + + subroutine build_gas_case() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Gas summary') + call sph_read_inp('tests/data/gas_ideal_minimal_030.inp', ierr) + call assert_ok(ierr, 'Gas summary input read failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'Gas summary build failed') + end subroutine build_gas_case + + subroutine check_summary_values() + real(rk) :: expected_radius + real(rk) :: pi_value + + call build_gas_case() + pi_value = acos(-1.0_rk) + expected_radius = (3.0_rk * 0.032_rk / (4.0_rk * pi_value))**(1.0_rk / 3.0_rk) + if (gas_particle_count /= 4) call fail('Gas summary particle count mismatch') + if (abs(gas_mass - 0.04_rk) > 1.0e-12_rk) call fail('Gas summary mass mismatch') + if (abs(gas_volume - 0.032_rk) > 1.0e-12_rk) call fail('Gas summary volume mismatch') + if (maxval(abs(gas_momentum)) > 1.0e-12_rk) call fail('Gas summary momentum mismatch') + if (maxval(abs(gas_center - (/0.025_rk, 0.025_rk, 0.025_rk/))) > 1.0e-12_rk) then + call fail('Gas summary center mismatch') + end if + if (abs(gas_mean_pressure - 0.96_rk) > 1.0e-12_rk) call fail('Gas mean pressure mismatch') + if (abs(gas_max_pressure - 0.96_rk) > 1.0e-12_rk) call fail('Gas max pressure mismatch') + if (abs(gas_min_density - 1.2_rk) > 1.0e-12_rk) call fail('Gas min density mismatch') + if (abs(gas_max_density - 1.2_rk) > 1.0e-12_rk) call fail('Gas max density mismatch') + if (abs(gas_equivalent_radius - expected_radius) > 1.0e-12_rk) then + call fail('Gas equivalent radius mismatch') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Gas summary values') + end subroutine check_summary_values + + subroutine check_inactive_and_failed_skip() + call build_gas_case() + particle_status(2) = SPH_STATUS_INACTIVE + particle_status(3) = SPH_STATUS_FAILED + call sph_gas_update_diagnostics(ierr) + call assert_ok(ierr, 'Gas summary skip update failed') + if (gas_particle_count /= 2) call fail('inactive or failed Gas particles were counted') + if (abs(gas_mass - 0.02_rk) > 1.0e-12_rk) call fail('Gas skip mass mismatch') + if (abs(gas_volume - 0.016_rk) > 1.0e-12_rk) call fail('Gas skip volume mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Gas skip summary') + end subroutine check_inactive_and_failed_skip + + subroutine check_bubble_metrics_history() + real(rk) :: old_radius + + call build_gas_case() + old_radius = gas_equivalent_radius + if (gas_previous_equivalent_radius <= 0.0_rk) call fail('Gas previous radius was not saved') + volume(1) = 0.016_rk + call sph_gas_update_diagnostics(ierr) + call assert_ok(ierr, 'Gas bubble history update failed') + if (gas_equivalent_radius <= old_radius) call fail('Gas equivalent radius did not increase') + if (gas_radius_rate <= 0.0_rk) call fail('Gas radius rate mismatch') + if (gas_center_shift <= 0.0_rk) call fail('Gas center shift mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Gas bubble history') + end subroutine check_bubble_metrics_history + + subroutine check_empty_gas_defaults() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for empty Gas defaults') + call sph_gas_update_diagnostics(ierr) + call assert_ok(ierr, 'empty Gas diagnostics failed') + if (gas_particle_count /= 0) call fail('empty Gas count mismatch') + if (gas_equivalent_radius /= 0.0_rk) call fail('empty Gas radius mismatch') + if (gas_radius_rate /= 0.0_rk) call fail('empty Gas radius rate mismatch') + if (gas_center_shift /= 0.0_rk) call fail('empty Gas center shift mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for empty Gas defaults') + end subroutine check_empty_gas_defaults + + subroutine check_zero_volume_error() + call build_gas_case() + volume(1) = 0.0_rk + call sph_gas_update_diagnostics(ierr) + if (ierr == SPH_SUCCESS) call fail('zero Gas volume did not fail') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for zero Gas volume') + end subroutine check_zero_volume_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_gas_summary diff --git a/todo.md b/todo.md index fe2fd75..4c85793 100644 --- a/todo.md +++ b/todo.md @@ -114,29 +114,29 @@ ## 阶段 I:Gas Summary -- [ ] 统计 active Gas 粒子数量。 -- [ ] 统计 Gas 总质量。 -- [ ] 统计 Gas 总体积。 -- [ ] 统计 Gas 动量。 -- [ ] 计算 Gas 体积加权中心。 -- [ ] 计算 Gas 平均压力。 -- [ ] 计算 Gas 最大压力。 -- [ ] 计算 Gas 密度范围。 -- [ ] 确认 inactive 或 failed 粒子不进入 Gas summary。 -- [ ] 增加 `gas_summary` 测试目标并接入 CTest。 +- [x] 统计 active Gas 粒子数量。 +- [x] 统计 Gas 总质量。 +- [x] 统计 Gas 总体积。 +- [x] 统计 Gas 动量。 +- [x] 计算 Gas 体积加权中心。 +- [x] 计算 Gas 平均压力。 +- [x] 计算 Gas 最大压力。 +- [x] 计算 Gas 密度范围。 +- [x] 确认 inactive 或 failed 粒子不进入 Gas summary。 +- [x] 增加 `gas_summary` 测试目标并接入 CTest。 ## 阶段 J:Bubble 诊断 -- [ ] 按 Gas 总体积计算等效气泡半径。 -- [ ] 保存上一时间步等效气泡半径。 -- [ ] 计算等效气泡半径变化率。 -- [ ] 保存上一时间步 Gas 中心。 -- [ ] 计算 Gas 中心迁移量。 -- [ ] 处理无 Gas 粒子时的 bubble metrics 默认值。 -- [ ] 处理零体积 Gas 粒子时的错误路径。 -- [ ] 在文档中说明 0.3.0 bubble metrics 为单气团近似。 -- [ ] 增加 bubble metrics 单元测试。 -- [ ] 将 bubble metrics 纳入 `gas_summary` 或独立测试。 +- [x] 按 Gas 总体积计算等效气泡半径。 +- [x] 保存上一时间步等效气泡半径。 +- [x] 计算等效气泡半径变化率。 +- [x] 保存上一时间步 Gas 中心。 +- [x] 计算 Gas 中心迁移量。 +- [x] 处理无 Gas 粒子时的 bubble metrics 默认值。 +- [x] 处理零体积 Gas 粒子时的错误路径。 +- [x] 在文档中说明 0.3.0 bubble metrics 为单气团近似。 +- [x] 增加 bubble metrics 单元测试。 +- [x] 将 bubble metrics 纳入 `gas_summary` 或独立测试。 ## 阶段 K:Gas 单相显式推进