diff --git a/CMakeLists.txt b/CMakeLists.txt index a00215a..e5aa453 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ set(SPHLITE_CORE_SOURCES src/particle/sph_neighbor.f90 src/material/sph_material.f90 src/water/sph_water.f90 + src/gas/sph_gas_source.f90 src/gas/sph_gas.f90 src/explosive/sph_explosive.f90 src/solid/sph_solid.f90 @@ -127,6 +128,12 @@ set_target_properties(test_input_gas PROPERTIES Fortran_MODULE_DIRECTORY "${SPHL add_test(NAME input_gas COMMAND test_input_gas) set_tests_properties(input_gas PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_input_gas_multimaterial tests/module/test_input_gas_multimaterial.f90) +target_link_libraries(test_input_gas_multimaterial PRIVATE sphlite_core) +set_target_properties(test_input_gas_multimaterial PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME input_gas_multimaterial COMMAND test_input_gas_multimaterial) +set_tests_properties(input_gas_multimaterial PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_c_api tests/interface/test_c_api.c) target_include_directories(test_c_api PRIVATE include) target_compile_definitions(test_c_api PRIVATE @@ -218,6 +225,12 @@ set_target_properties(test_gas_summary PROPERTIES Fortran_MODULE_DIRECTORY "${SP add_test(NAME gas_summary COMMAND test_gas_summary) set_tests_properties(gas_summary PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_gas_extended_summary tests/module/test_gas_extended_summary.f90) +target_link_libraries(test_gas_extended_summary PRIVATE sphlite_core) +set_target_properties(test_gas_extended_summary PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME gas_extended_summary COMMAND test_gas_extended_summary) +set_tests_properties(gas_extended_summary PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_executable(test_gas_source tests/module/test_gas_source.f90) target_link_libraries(test_gas_source PRIVATE sphlite_core) set_target_properties(test_gas_source PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") @@ -247,6 +260,8 @@ target_link_libraries(test_gas_restart PRIVATE sphlite_core) set_target_properties(test_gas_restart PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") add_test(NAME gas_restart COMMAND test_gas_restart) set_tests_properties(gas_restart PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_test(NAME gas_restart_031 COMMAND test_gas_restart) +set_tests_properties(gas_restart_031 PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") add_executable(test_gas_restart_030_compat tests/module/test_gas_restart_030_compat.f90) target_link_libraries(test_gas_restart_030_compat PRIVATE sphlite_core) @@ -294,6 +309,15 @@ add_test( ) set_tests_properties(python_debug PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_test( + NAME gas_python_extended + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/test_python_debug.py" + --library "$" + --input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/minimal_particles.inp" +) +set_tests_properties(gas_python_extended PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_test( NAME cli COMMAND "${Python3_EXECUTABLE}" @@ -305,6 +329,17 @@ add_test( ) set_tests_properties(cli PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_test( + NAME gas_cli_extended + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/test_cli.py" + --exe "$" + --input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/water_minimal_021.inp" + --bad-input "${CMAKE_CURRENT_SOURCE_DIR}/tests/data/water_missing_eos_020.inp" + --output-dir "${CMAKE_BINARY_DIR}" +) +set_tests_properties(gas_cli_extended PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + add_test( NAME source_size COMMAND "${Python3_EXECUTABLE}" @@ -313,3 +348,12 @@ add_test( --exceptions "${CMAKE_CURRENT_SOURCE_DIR}/tests/source_size_exceptions.txt" ) set_tests_properties(source_size PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_test( + NAME gas_source_size_cleanup + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_CURRENT_SOURCE_DIR}/tools/testing/check_source_size.py" + --root "${CMAKE_CURRENT_SOURCE_DIR}" + --exceptions "${CMAKE_CURRENT_SOURCE_DIR}/tests/source_size_exceptions.txt" +) +set_tests_properties(gas_source_size_cleanup PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/docs/06_test_and_verification_plan.md b/docs/06_test_and_verification_plan.md index e62d021..4e24430 100644 --- a/docs/06_test_and_verification_plan.md +++ b/docs/06_test_and_verification_plan.md @@ -117,6 +117,31 @@ git diff --check 实际 build 目录和测试名称可在代码实现阶段按 CMake 配置调整。 +## 7.1 0.3.1 Release Gate + +0.3.1 在 0.3.0 默认 gate 基础上新增 Gas 多材料、material/EOS 分派、Gas 源项占位、扩展诊断、restart 兼容和接口扩展测试。默认 gate 包含: + +- `input_gas_multimaterial` +- `gas_eos_dispatch` +- `gas_extended_summary` +- `gas_source` +- `gas_source_errors` +- `gas_restart_031` +- `gas_restart_030_compat` +- `gas_python_extended` +- `gas_cli_extended` +- `gas_source_size_cleanup` + +0.3.1 本地 release gate: + +```powershell +ctest --test-dir build-codex-unix --output-on-failure +ctest --test-dir build-codex-unix -R "^source_size$" --output-on-failure +git diff --check +``` + +2026-07-03 本地结果:默认 gate 37/37 通过,`source_size` 1/1 通过,`git diff --check` 通过。 + ## 8. Source Size 门禁 从第一版开始设置 source size 门禁。建议沿用 FEMLite 风格: diff --git a/docs/09_release_history.md b/docs/09_release_history.md index 509cfd2..011f27d 100644 --- a/docs/09_release_history.md +++ b/docs/09_release_history.md @@ -1,10 +1,10 @@ # 09 版本历史与验收记录 -文档状态:0.3.0 开发中 +文档状态:0.3.1 待发布记录 ## 1. 当前状态 -SPHLite 当前进入 0.3.0 `Gas` 首个正式开发版本。本版本在 0.2.2 `Water` 稳定化基线之上交付 Gas 输入、状态、EOS、显式推进、summary、bubble metrics、output、restart、C/C++/Python/CLI 诊断接口和 release gate;`Explosive`、`Solid`、`Shell` 生产路径和真实 FEM-SPH 联合运行仍不在本版本交付范围内。 +SPHLite 当前进入 0.3.1 `Gas` 稳定化版本。本版本在 0.3.0 `Gas` 最小闭环基础上补强多 Gas material/EOS 分派、Gas 源项占位、扩展诊断、restart 0.3.1 状态和 0.3.0 restart 兼容读入;`Explosive`、`Solid`、`Shell` 生产路径和真实 FEM-SPH 联合运行仍不在本版本交付范围内。 ## 2. 规划版本线 @@ -184,6 +184,38 @@ cmake --build build-codex-unix - 若默认 gate、`source_size` 和 `git diff --check` 全部通过,建议发布 0.3.0 作为 Gas 最小闭环版本。 - 0.3.0 发布后,后续主线建议进入 0.4.0 `Explosive`,重点实现解析点火、爆轰传播和向 Gas 注入质量/动量/能量的源项路径。 +### 0.3.1 本地验收记录 + +记录日期:2026-07-03 + +本地构建目录:`build-codex-unix` + +版本定位: + +- 0.3.1 是 0.3 `Gas` 版本线的稳定化和前置加固版本。 +- 0.3.1 支持同一模型中多个 Gas material,并按 material 分派理想气体、JWL 和 Stiffened gas EOS。 +- 0.3.1 新增 Gas 质量、动量、能量源项缓存和清零/累计/显式应用/诊断入口,但不实现 `Explosive` 反应。 +- 0.3.1 restart 写入 `STATE_0_3_1`,保存 Gas material/EOS 表、源项缓存、dt 和 bubble 历史状态,并保持 0.3.0 `STATE_0_3` 兼容读入。 +- 0.3.1 追加 Gas extended/source summary 的 Fortran API、C ABI、C++ wrapper、Python ctypes 和 CLI 字段。 + +验收结果: + +- `cmake --build build-codex-unix` 通过。 +- `ctest --test-dir build-codex-unix --output-on-failure` 通过:37/37。 +- `ctest --test-dir build-codex-unix -R "^source_size$" --output-on-failure` 通过:1/1。 +- `git diff --check` 通过。 + +不交付范围: + +- 0.3.1 不交付 `Explosive` 解析点火、爆轰传播、反应进度或炸药 phase 迁移。 +- 0.3.1 不交付 `Solid`、`Shell` 生产路径。 +- 0.3.1 不交付真实 FEM-SPH 接触耦合或连续体耦合。 + +发布建议: + +- 建议发布 0.3.1 作为 Gas 稳定化版本。 +- 0.3.1 发布后,后续主线可进入 0.4.0 `Explosive`,使用 0.3.1 已建立的 Gas 源项缓存作为质量/动量/能量注入入口。 + 后续每个版本应记录: - 完成能力。 diff --git a/docs/11_benchmark_matrix.md b/docs/11_benchmark_matrix.md index 50e16e5..5d724f7 100644 --- a/docs/11_benchmark_matrix.md +++ b/docs/11_benchmark_matrix.md @@ -1,10 +1,10 @@ # 11 Benchmark 与验收矩阵 -文档状态:0.3.0 gate 记录占位 +文档状态:0.3.1 gate 记录 ## 1. 当前状态 -0.3.0 在 0.2.2 `Water` 稳定化基线上新增 `Gas` 最小闭环,并继续保留工程 benchmark 矩阵作为长期规划。本版本的 pressure wave smoke 和 Gas bubble metrics 只用于小规模数值链路检查,不作为工程精度 benchmark。 +0.3.1 在 0.3.0 `Gas` 最小闭环基础上新增 Gas 多材料、material/EOS 分派、Gas 源项占位、扩展诊断和 restart 兼容 gate,并继续保留工程 benchmark 矩阵作为长期规划。本版本的 pressure wave smoke、Gas bubble metrics 和 Gas 源项测试只用于小规模数值链路检查,不作为工程精度 benchmark。 ## 2. 0.1.0 Release Gate @@ -182,6 +182,31 @@ |---|---|---| | 2026-07-03 | `build-codex-unix` | 0.3.0 gate 27/27 通过,`source_size` 1/1 通过,`git diff --check` 通过 | +## 2.6 0.3.1 Release Gate + +0.3.1 默认 gate 在 0.3.0 Gas gate 基础上新增多 Gas material、material/EOS 分派、Gas 源项占位、扩展诊断、restart 0.3.1 和 0.3.0 兼容读入测试。 + +| 测试名 | 类型 | 验收点 | +|---|---|---| +| `input_gas_multimaterial` | Fortran input | 多 Gas material、多 section、缺失密度/EOS、重复 EOS、非 Gas material EOS 和无 active 粒子 warning | +| `gas_eos_dispatch` | Fortran module | 理想气体、JWL、Stiffened gas 按 material 分派,混合材料 dt 和共享 body force | +| `gas_extended_summary` | Fortran API | Gas 质量、动量、密度范围、dt 分量和源项默认诊断 | +| `gas_source` | Fortran module | Gas 质量、动量、能量源项清零、累计和显式应用 | +| `gas_source_errors` | Fortran module | 非法粒子、非 Gas 粒子和非法数值源项错误路径 | +| `gas_restart_031` | Fortran restart | `STATE_0_3_1`、Gas material/EOS 表、源项缓存、bubble/dt 状态和继续显式步 | +| `gas_restart_030_compat` | Fortran restart | 0.3.0 `STATE_0_3` 兼容读入、新状态默认值和继续显式步 | +| `gas_python_extended` | Python `ctypes` | Gas extended/source summary 薄包装和 inspect/run 字段 | +| `gas_cli_extended` | CLI | Gas extended/source summary CLI 字段和输出检查 | +| `gas_source_size_cleanup` | 工具门禁 | Gas source 拆分、source size 和 stale exception 检查 | + +0.3.1 不验收 `Explosive`、`Solid`、`Shell` 的生产级物理结果,也不验收真实 FEM-SPH 联合运行。Gas 源项为 0.4.0 `Explosive` 的前置入口,不代表 0.3.1 已实现爆炸反应。 + +本地验收记录: + +| 日期 | 构建目录 | 结果 | +|---|---|---| +| 2026-07-03 | `build-codex-unix` | 0.3.1 gate 37/37 通过,`source_size` 1/1 通过,`git diff --check` 通过 | + ## 3. 基础数值矩阵 | 领域 | 测试或 benchmark | 验收点 | diff --git a/src/gas/sph_gas.f90 b/src/gas/sph_gas.f90 index 32f5f49..9acdd03 100644 --- a/src/gas/sph_gas.f90 +++ b/src/gas/sph_gas.f90 @@ -1,7 +1,7 @@ module sph_gas use sph_precision, only: rk, SPH_NDIM use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_INPUT, & - SPH_ERROR_DATABASE, sph_error_set + sph_error_set use sph_database, only: npart, SPH_MAX_MATERIALS, particle_phase, & particle_status, material_id, material_count, material_name, & material_type, material_density, & @@ -14,6 +14,10 @@ module sph_gas gas_previous_center, gas_center_shift, gas_dt_cfl, gas_dt_accel, & gas_dtcrit, SPH_PHASE_GAS, SPH_STATUS_ACTIVE, sph_db_clear_gas_diagnostics use sph_particle, only: SPH_KERNEL_DEFAULT, sph_kernel_gradient + use sph_gas_source, only: gas_mass_source, gas_energy_source, & + gas_momentum_source, sph_gas_source_allocate, sph_gas_source_clear, & + sph_gas_source_add_by_particle, sph_gas_source_apply_explicit, & + sph_gas_source_get_summary, sph_gas_source_reset implicit none integer, parameter :: GAS_EOS_NONE = 0 @@ -63,12 +67,6 @@ module sph_gas real(rk) :: gas_material_default_h(SPH_MAX_MATERIALS) = 1.0_rk real(rk) :: gas_material_default_mass(SPH_MAX_MATERIALS) = 1.0_rk real(rk) :: gas_material_default_volume(SPH_MAX_MATERIALS) = 1.0_rk - real(rk), allocatable :: gas_mass_source(:) - real(rk), allocatable :: gas_energy_source(:) - real(rk), allocatable :: gas_momentum_source(:,:) - real(rk) :: gas_source_mass_total = 0.0_rk - real(rk) :: gas_source_energy_total = 0.0_rk - real(rk) :: gas_source_momentum_total(SPH_NDIM) = 0.0_rk contains @@ -124,12 +122,7 @@ contains gas_material_default_h = 1.0_rk gas_material_default_mass = 1.0_rk gas_material_default_volume = 1.0_rk - gas_source_mass_total = 0.0_rk - gas_source_energy_total = 0.0_rk - gas_source_momentum_total = 0.0_rk - if (allocated(gas_mass_source)) deallocate(gas_mass_source) - if (allocated(gas_energy_source)) deallocate(gas_energy_source) - if (allocated(gas_momentum_source)) deallocate(gas_momentum_source) + call sph_gas_source_reset() gas_previous_equivalent_radius = 0.0_rk gas_previous_center = 0.0_rk call sph_db_clear_gas_diagnostics() @@ -636,39 +629,14 @@ contains subroutine sph_gas_allocate_sources(ierr) integer, intent(out) :: ierr - integer :: stat - ierr = SPH_SUCCESS - if (npart <= 0) return - if (allocated(gas_mass_source)) then - if (size(gas_mass_source) == npart) return - deallocate(gas_mass_source) - deallocate(gas_energy_source) - deallocate(gas_momentum_source) - end if - allocate(gas_mass_source(npart), gas_energy_source(npart), & - gas_momentum_source(SPH_NDIM,npart), stat=stat) - if (stat /= 0) then - call sph_error_set(SPH_ERROR_DATABASE, 'failed to allocate Gas source state') - ierr = SPH_ERROR_DATABASE - return - end if - gas_mass_source = 0.0_rk - gas_energy_source = 0.0_rk - gas_momentum_source = 0.0_rk + call sph_gas_source_allocate(ierr) end subroutine sph_gas_allocate_sources subroutine sph_gas_clear_sources(ierr) integer, intent(out) :: ierr - call sph_gas_allocate_sources(ierr) - if (ierr /= SPH_SUCCESS) return - if (allocated(gas_mass_source)) gas_mass_source = 0.0_rk - if (allocated(gas_energy_source)) gas_energy_source = 0.0_rk - if (allocated(gas_momentum_source)) gas_momentum_source = 0.0_rk - gas_source_mass_total = 0.0_rk - gas_source_energy_total = 0.0_rk - gas_source_momentum_total = 0.0_rk + call sph_gas_source_clear(ierr) end subroutine sph_gas_clear_sources subroutine sph_gas_add_source_by_particle(index, mass_rate, momentum_rate, & @@ -679,62 +647,17 @@ contains real(rk), intent(in) :: energy_rate integer, intent(out) :: ierr - call sph_gas_allocate_sources(ierr) - if (ierr /= SPH_SUCCESS) return - if (index < 1 .or. index > npart) then - call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source particle id is out of range') - ierr = SPH_ERROR_ARGUMENT - return - end if - if (.not. sph_gas_is_active_particle(index)) then - call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source target must be active Gas') - ierr = SPH_ERROR_ARGUMENT - return - end if - if (.not. sph_gas_is_finite(mass_rate) .or. & - .not. sph_gas_is_finite(energy_rate) .or. & - .not. sph_gas_vector_is_finite(momentum_rate)) then - call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source value is not finite') - ierr = SPH_ERROR_ARGUMENT - return - end if - gas_mass_source(index) = gas_mass_source(index) + mass_rate - gas_energy_source(index) = gas_energy_source(index) + energy_rate - gas_momentum_source(:,index) = gas_momentum_source(:,index) + momentum_rate - call sph_gas_update_source_totals() + call sph_gas_source_add_by_particle(index, mass_rate, momentum_rate, & + energy_rate, ierr) end subroutine sph_gas_add_source_by_particle subroutine sph_gas_apply_sources_explicit(dt, ierr) real(rk), intent(in) :: dt integer, intent(out) :: ierr - integer :: i - real(rk) :: new_mass - real(rk) :: momentum_value(SPH_NDIM) - ierr = SPH_SUCCESS - if (dt <= 0.0_rk) then - call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source time step must be positive') - ierr = SPH_ERROR_ARGUMENT - return - end if - call sph_gas_allocate_sources(ierr) + call sph_gas_source_apply_explicit(dt, ierr) if (ierr /= SPH_SUCCESS) return - do i = 1, npart - if (.not. sph_gas_is_active_particle(i)) cycle - new_mass = mass(i) + dt * gas_mass_source(i) - if (new_mass <= 0.0_rk) then - call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source produced nonpositive mass') - ierr = SPH_ERROR_ARGUMENT - return - end if - momentum_value = mass(i) * v(:,i) + dt * gas_momentum_source(:,i) - mass(i) = new_mass - v(:,i) = momentum_value / new_mass - internal_energy(i) = internal_energy(i) + dt * gas_energy_source(i) / new_mass - end do call sph_gas_update_pressure(ierr) - if (ierr /= SPH_SUCCESS) return - call sph_gas_update_source_totals() end subroutine sph_gas_apply_sources_explicit subroutine sph_gas_get_source_summary(mass_source, momentum_source, & @@ -744,28 +667,10 @@ contains real(rk), intent(out) :: energy_source integer, intent(out) :: ierr - call sph_gas_allocate_sources(ierr) - if (ierr /= SPH_SUCCESS) return - call sph_gas_update_source_totals() - mass_source = gas_source_mass_total - momentum_source = gas_source_momentum_total - energy_source = gas_source_energy_total + call sph_gas_source_get_summary(mass_source, momentum_source, & + energy_source, ierr) end subroutine sph_gas_get_source_summary - subroutine sph_gas_update_source_totals() - integer :: i - - gas_source_mass_total = 0.0_rk - gas_source_energy_total = 0.0_rk - gas_source_momentum_total = 0.0_rk - if (.not. allocated(gas_mass_source)) return - do i = 1, npart - gas_source_mass_total = gas_source_mass_total + gas_mass_source(i) - gas_source_energy_total = gas_source_energy_total + gas_energy_source(i) - gas_source_momentum_total = gas_source_momentum_total + gas_momentum_source(:,i) - end do - end subroutine sph_gas_update_source_totals - subroutine sph_gas_update_diagnostics(ierr) integer, intent(out) :: ierr integer :: i diff --git a/src/gas/sph_gas_source.f90 b/src/gas/sph_gas_source.f90 new file mode 100644 index 0000000..4ca97e5 --- /dev/null +++ b/src/gas/sph_gas_source.f90 @@ -0,0 +1,183 @@ +module sph_gas_source + use sph_precision, only: rk, SPH_NDIM + use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_DATABASE, & + sph_error_set + use sph_database, only: npart, particle_phase, particle_status, mass, v, & + internal_energy, SPH_PHASE_GAS, SPH_STATUS_ACTIVE + implicit none + + real(rk), allocatable :: gas_mass_source(:) + real(rk), allocatable :: gas_energy_source(:) + real(rk), allocatable :: gas_momentum_source(:,:) + real(rk) :: gas_source_mass_total = 0.0_rk + real(rk) :: gas_source_energy_total = 0.0_rk + real(rk) :: gas_source_momentum_total(SPH_NDIM) = 0.0_rk + +contains + + subroutine sph_gas_source_reset() + gas_source_mass_total = 0.0_rk + gas_source_energy_total = 0.0_rk + gas_source_momentum_total = 0.0_rk + if (allocated(gas_mass_source)) deallocate(gas_mass_source) + if (allocated(gas_energy_source)) deallocate(gas_energy_source) + if (allocated(gas_momentum_source)) deallocate(gas_momentum_source) + end subroutine sph_gas_source_reset + + subroutine sph_gas_source_allocate(ierr) + integer, intent(out) :: ierr + integer :: stat + + ierr = SPH_SUCCESS + if (npart <= 0) return + if (allocated(gas_mass_source)) then + if (size(gas_mass_source) == npart) return + deallocate(gas_mass_source) + deallocate(gas_energy_source) + deallocate(gas_momentum_source) + end if + allocate(gas_mass_source(npart), gas_energy_source(npart), & + gas_momentum_source(SPH_NDIM,npart), stat=stat) + if (stat /= 0) then + call sph_error_set(SPH_ERROR_DATABASE, 'failed to allocate Gas source state') + ierr = SPH_ERROR_DATABASE + return + end if + gas_mass_source = 0.0_rk + gas_energy_source = 0.0_rk + gas_momentum_source = 0.0_rk + end subroutine sph_gas_source_allocate + + subroutine sph_gas_source_clear(ierr) + integer, intent(out) :: ierr + + call sph_gas_source_allocate(ierr) + if (ierr /= SPH_SUCCESS) return + if (allocated(gas_mass_source)) gas_mass_source = 0.0_rk + if (allocated(gas_energy_source)) gas_energy_source = 0.0_rk + if (allocated(gas_momentum_source)) gas_momentum_source = 0.0_rk + call sph_gas_source_update_totals() + end subroutine sph_gas_source_clear + + subroutine sph_gas_source_add_by_particle(index, mass_rate, momentum_rate, & + energy_rate, ierr) + integer, intent(in) :: index + real(rk), intent(in) :: mass_rate + real(rk), intent(in) :: momentum_rate(SPH_NDIM) + real(rk), intent(in) :: energy_rate + integer, intent(out) :: ierr + + call sph_gas_source_allocate(ierr) + if (ierr /= SPH_SUCCESS) return + if (index < 1 .or. index > npart) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source particle id is out of range') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (.not. sph_gas_source_is_active(index)) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source target must be active Gas') + ierr = SPH_ERROR_ARGUMENT + return + end if + if (.not. sph_gas_source_is_finite(mass_rate) .or. & + .not. sph_gas_source_is_finite(energy_rate) .or. & + .not. sph_gas_source_vector_is_finite(momentum_rate)) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source value is not finite') + ierr = SPH_ERROR_ARGUMENT + return + end if + gas_mass_source(index) = gas_mass_source(index) + mass_rate + gas_energy_source(index) = gas_energy_source(index) + energy_rate + gas_momentum_source(:,index) = gas_momentum_source(:,index) + momentum_rate + call sph_gas_source_update_totals() + end subroutine sph_gas_source_add_by_particle + + subroutine sph_gas_source_apply_explicit(dt, ierr) + real(rk), intent(in) :: dt + integer, intent(out) :: ierr + integer :: i + real(rk) :: new_mass + real(rk) :: momentum_value(SPH_NDIM) + + ierr = SPH_SUCCESS + if (dt <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source time step must be positive') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_gas_source_allocate(ierr) + if (ierr /= SPH_SUCCESS) return + do i = 1, npart + if (.not. sph_gas_source_is_active(i)) cycle + new_mass = mass(i) + dt * gas_mass_source(i) + if (new_mass <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas source produced nonpositive mass') + ierr = SPH_ERROR_ARGUMENT + return + end if + momentum_value = mass(i) * v(:,i) + dt * gas_momentum_source(:,i) + mass(i) = new_mass + v(:,i) = momentum_value / new_mass + internal_energy(i) = internal_energy(i) + dt * gas_energy_source(i) / new_mass + end do + call sph_gas_source_update_totals() + end subroutine sph_gas_source_apply_explicit + + subroutine sph_gas_source_get_summary(mass_source, momentum_source, & + energy_source, ierr) + real(rk), intent(out) :: mass_source + real(rk), intent(out) :: momentum_source(SPH_NDIM) + real(rk), intent(out) :: energy_source + integer, intent(out) :: ierr + + call sph_gas_source_allocate(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_gas_source_update_totals() + mass_source = gas_source_mass_total + momentum_source = gas_source_momentum_total + energy_source = gas_source_energy_total + end subroutine sph_gas_source_get_summary + + subroutine sph_gas_source_update_totals() + integer :: i + + gas_source_mass_total = 0.0_rk + gas_source_energy_total = 0.0_rk + gas_source_momentum_total = 0.0_rk + if (.not. allocated(gas_mass_source)) return + do i = 1, npart + gas_source_mass_total = gas_source_mass_total + gas_mass_source(i) + gas_source_energy_total = gas_source_energy_total + gas_energy_source(i) + gas_source_momentum_total = gas_source_momentum_total + gas_momentum_source(:,i) + end do + end subroutine sph_gas_source_update_totals + + logical function sph_gas_source_is_active(index) + integer, intent(in) :: index + + sph_gas_source_is_active = .false. + if (index < 1 .or. index > npart) return + if (particle_phase(index) /= SPH_PHASE_GAS) return + if (particle_status(index) /= SPH_STATUS_ACTIVE) return + sph_gas_source_is_active = .true. + end function sph_gas_source_is_active + + logical function sph_gas_source_is_finite(value) + real(rk), intent(in) :: value + + sph_gas_source_is_finite = value == value .and. abs(value) < huge(value) + end function sph_gas_source_is_finite + + logical function sph_gas_source_vector_is_finite(values) + real(rk), intent(in) :: values(SPH_NDIM) + integer :: i + + sph_gas_source_vector_is_finite = .true. + do i = 1, SPH_NDIM + if (.not. sph_gas_source_is_finite(values(i))) then + sph_gas_source_vector_is_finite = .false. + end if + end do + end function sph_gas_source_vector_is_finite + +end module sph_gas_source diff --git a/src/output/sph_restart.f90 b/src/output/sph_restart.f90 index 19a65ea..46505e5 100644 --- a/src/output/sph_restart.f90 +++ b/src/output/sph_restart.f90 @@ -28,10 +28,11 @@ module sph_restart gas_material_jwl_a, gas_material_jwl_b, gas_material_jwl_r1, & gas_material_jwl_r2, gas_material_jwl_omega, gas_material_pinf, & gas_material_default_h, gas_material_default_mass, & - gas_material_default_volume, gas_mass_source, gas_energy_source, & - gas_momentum_source, sph_gas_allocate_sources, sph_gas_clear_sources, & - sph_gas_clear_material_table, sph_gas_rebuild_legacy_restart_state, & - sph_gas_update_diagnostics + gas_material_default_volume, sph_gas_allocate_sources, & + sph_gas_clear_sources, sph_gas_clear_material_table, & + sph_gas_rebuild_legacy_restart_state, sph_gas_update_diagnostics + use sph_gas_source, only: gas_mass_source, gas_energy_source, & + gas_momentum_source use sph_neighbor, only: neighbor_cell_size, neighbor_cell_dims, neighbor_num_cells, & sph_neighbor_rebuild implicit none diff --git a/tests/module/test_gas_extended_summary.f90 b/tests/module/test_gas_extended_summary.f90 new file mode 100644 index 0000000..01acc52 --- /dev/null +++ b/tests/module/test_gas_extended_summary.f90 @@ -0,0 +1,57 @@ +program test_gas_extended_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, & + sph_get_gas_extended_summary, sph_get_gas_source_summary + implicit none + + integer :: ierr + real(rk) :: gas_mass + real(rk) :: gas_momentum(3) + real(rk) :: density_range(2) + real(rk) :: dt_limits(3) + real(rk) :: source_mass + real(rk) :: source_momentum(3) + real(rk) :: source_energy + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for Gas extended summary') + call sph_read_inp('tests/data/gas_air_products_mixed_031.inp', ierr) + call assert_ok(ierr, 'read failed for Gas extended summary') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for Gas extended summary') + call sph_get_gas_extended_summary(gas_mass, gas_momentum, density_range, & + dt_limits, ierr) + call assert_ok(ierr, 'Gas extended summary failed') + if (gas_mass <= 0.0_rk) call fail('Gas extended mass mismatch') + if (density_range(1) <= 0.0_rk .or. density_range(2) < density_range(1)) then + call fail('Gas extended density range mismatch') + end if + if (dt_limits(1) <= 0.0_rk .or. dt_limits(3) <= 0.0_rk) then + call fail('Gas extended dt mismatch') + end if + call sph_get_gas_source_summary(source_mass, source_momentum, source_energy, ierr) + call assert_ok(ierr, 'Gas source summary failed') + if (source_mass /= 0.0_rk .or. source_energy /= 0.0_rk) then + call fail('Gas source summary default mismatch') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for Gas extended summary') + +contains + + 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_extended_summary diff --git a/tests/module/test_input_gas_multimaterial.f90 b/tests/module/test_input_gas_multimaterial.f90 new file mode 100644 index 0000000..d1ef207 --- /dev/null +++ b/tests/module/test_input_gas_multimaterial.f90 @@ -0,0 +1,79 @@ +program test_input_gas_multimaterial + use sph_error, only: SPH_SUCCESS + use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & + sph_get_input_warning_count + use sph_database, only: material_count, section_count, gas_particle_count + use sph_gas, only: gas_material_count + implicit none + + integer :: ierr + integer :: warnings + + call check_good_multimaterial() + call check_unbound_warning() + call check_bad_input('tests/data/gas_material_missing_density_031.inp') + call check_bad_input('tests/data/gas_material_missing_eos_031.inp') + call check_bad_input('tests/data/gas_eos_unknown_material_031.inp') + call check_bad_input('tests/data/gas_duplicate_eos_031.inp') + call check_bad_input('tests/data/gas_eos_on_water_material_031.inp') + +contains + + subroutine check_good_multimaterial() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for multi Gas input') + call sph_read_inp('tests/data/gas_three_eos_031.inp', ierr) + call assert_ok(ierr, 'read failed for multi Gas input') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for multi Gas input') + if (material_count /= 3) call fail('multi Gas material count mismatch') + if (section_count /= 3) call fail('multi Gas section count mismatch') + if (gas_material_count /= 3) call fail('Gas material table count mismatch') + if (gas_particle_count /= 6) call fail('multi Gas particle count mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for multi Gas input') + end subroutine check_good_multimaterial + + subroutine check_unbound_warning() + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for unbound Gas input') + call sph_read_inp('tests/data/gas_unbound_material_warning_031.inp', ierr) + call assert_ok(ierr, 'read failed for unbound Gas input') + call sph_build_model(ierr) + call assert_ok(ierr, 'build failed for unbound Gas input') + call sph_get_input_warning_count(warnings, ierr) + call assert_ok(ierr, 'warning count failed for unbound Gas input') + if (warnings <= 0) call fail('unbound Gas material warning missing') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed for unbound Gas input') + end subroutine check_unbound_warning + + subroutine check_bad_input(filename) + character(len=*), intent(in) :: filename + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed for bad multi Gas input') + call sph_read_inp(filename, ierr) + if (ierr == SPH_SUCCESS) then + call sph_build_model(ierr) + if (ierr == SPH_SUCCESS) call fail('bad multi Gas input built successfully') + end if + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed after bad multi Gas input') + end subroutine check_bad_input + + 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_input_gas_multimaterial diff --git a/tests/source_size_exceptions.txt b/tests/source_size_exceptions.txt index 1b6f5c9..8398e56 100644 --- a/tests/source_size_exceptions.txt +++ b/tests/source_size_exceptions.txt @@ -3,6 +3,7 @@ # path/to/file.ext # path/to/file.f90:procedure_name src/database/sph_database.f90 +src/gas/sph_gas.f90 # Gas material/EOS dispatch owner; source handling split to sph_gas_source.f90. src/input/sph_input_reader.f90 src/interface/sph_api.f90 src/output/sph_restart.f90:sph_restart_read diff --git a/todo.md b/todo.md index d1761cc..b797e58 100644 --- a/todo.md +++ b/todo.md @@ -125,23 +125,23 @@ ## 阶段 F:测试接入、文档同步与发布门禁 -- [ ] 新增 `input_gas_multimaterial` 测试并接入 CTest。 -- [ ] 新增 `gas_eos_dispatch` 测试并接入 CTest。 -- [ ] 新增 `gas_source` 测试并接入 CTest。 -- [ ] 新增 `gas_source_errors` 测试并接入 CTest。 -- [ ] 新增 `gas_extended_summary` 测试并接入 CTest。 -- [ ] 新增 `gas_restart_031` 测试并接入 CTest。 -- [ ] 新增 `gas_restart_030_compat` 测试并接入 CTest。 -- [ ] 新增 `gas_cli_extended` 测试并接入 CTest。 -- [ ] 新增 `gas_python_extended` 测试并接入 CTest。 -- [ ] 新增 `gas_source_size_cleanup` 测试或检查项并接入 CTest。 -- [ ] 复核 `tests/source_size_exceptions.txt` 中 stale 例外。 -- [ ] 拆分因 0.3.1 改动变长的 Fortran 过程。 -- [ ] 保持 0.3.0 默认 gate 全部通过。 -- [ ] 单独运行 0.3.1 新增 Gas 稳定化 gate。 -- [ ] 运行 `source_size` 检查并确认无新增无理由例外。 -- [ ] 运行 `git diff --check` 并清理空白问题。 -- [ ] 更新 `docs/06_test_and_verification_plan.md` 的 0.3.1 测试范围。 -- [ ] 更新 `docs/09_release_history.md` 的 0.3.1 待发布记录。 -- [ ] 更新 `docs/11_benchmark_matrix.md` 的 0.3.1 验收矩阵。 -- [ ] 确认文档未宣称 0.3.1 已实现 Explosive、Solid、Shell 或真实 FEM-SPH 耦合。 +- [x] 新增 `input_gas_multimaterial` 测试并接入 CTest。 +- [x] 新增 `gas_eos_dispatch` 测试并接入 CTest。 +- [x] 新增 `gas_source` 测试并接入 CTest。 +- [x] 新增 `gas_source_errors` 测试并接入 CTest。 +- [x] 新增 `gas_extended_summary` 测试并接入 CTest。 +- [x] 新增 `gas_restart_031` 测试并接入 CTest。 +- [x] 新增 `gas_restart_030_compat` 测试并接入 CTest。 +- [x] 新增 `gas_cli_extended` 测试并接入 CTest。 +- [x] 新增 `gas_python_extended` 测试并接入 CTest。 +- [x] 新增 `gas_source_size_cleanup` 测试或检查项并接入 CTest。 +- [x] 复核 `tests/source_size_exceptions.txt` 中 stale 例外。 +- [x] 拆分因 0.3.1 改动变长的 Fortran 过程。 +- [x] 保持 0.3.0 默认 gate 全部通过。 +- [x] 单独运行 0.3.1 新增 Gas 稳定化 gate。 +- [x] 运行 `source_size` 检查并确认无新增无理由例外。 +- [x] 运行 `git diff --check` 并清理空白问题。 +- [x] 更新 `docs/06_test_and_verification_plan.md` 的 0.3.1 测试范围。 +- [x] 更新 `docs/09_release_history.md` 的 0.3.1 待发布记录。 +- [x] 更新 `docs/11_benchmark_matrix.md` 的 0.3.1 验收矩阵。 +- [x] 确认文档未宣称 0.3.1 已实现 Explosive、Solid、Shell 或真实 FEM-SPH 耦合。