From 4b95609d14cd20f8fad4f899a401f1c3c03b8714 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 3 Jul 2026 12:03:06 +0800 Subject: [PATCH] Add Explosive detonation state --- CMakeLists.txt | 6 + src/explosive/sph_explosive.f90 | 143 +++++++++++++++++- src/input/sph_input_reader.f90 | 4 +- .../module/test_explosive_detonation_time.f90 | 63 ++++++++ todo.md | 40 ++--- 5 files changed, 233 insertions(+), 23 deletions(-) create mode 100644 tests/module/test_explosive_detonation_time.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b01947..78b6acf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,6 +269,12 @@ set_target_properties(test_gas_restart_030_compat PROPERTIES Fortran_MODULE_DIRE add_test(NAME gas_restart_030_compat COMMAND test_gas_restart_030_compat) set_tests_properties(gas_restart_030_compat PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") +add_executable(test_explosive_detonation_time tests/module/test_explosive_detonation_time.f90) +target_link_libraries(test_explosive_detonation_time PRIVATE sphlite_core) +set_target_properties(test_explosive_detonation_time PROPERTIES Fortran_MODULE_DIRECTORY "${SPHLITE_MODULE_DIR}") +add_test(NAME explosive_detonation_time COMMAND test_explosive_detonation_time) +set_tests_properties(explosive_detonation_time 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/src/explosive/sph_explosive.f90 b/src/explosive/sph_explosive.f90 index 8fe5126..ba24c71 100644 --- a/src/explosive/sph_explosive.f90 +++ b/src/explosive/sph_explosive.f90 @@ -4,7 +4,8 @@ module sph_explosive sph_error_set use sph_database, only: SPH_MAX_MATERIALS, SPH_MAX_SECTIONS, material_count, & material_name, material_type, material_density, section_count, & - section_material_id + section_material_id, npart, particle_phase, particle_status, & + material_id, x0, SPH_PHASE_EXPLOSIVE, SPH_STATUS_ACTIVE use sph_gas, only: sph_gas_table_index_by_material_id, & gas_material_eos_defined implicit none @@ -30,6 +31,12 @@ module sph_explosive integer :: explosive_ignition_material_id(SPH_MAX_MATERIALS) = 0 real(rk) :: explosive_ignition_x(SPH_NDIM,SPH_MAX_MATERIALS) = 0.0_rk real(rk) :: explosive_ignition_time(SPH_MAX_MATERIALS) = 0.0_rk + integer, allocatable :: explosive_ignition_status(:) + real(rk), allocatable :: explosive_detonation_time(:) + real(rk), allocatable :: explosive_reaction_progress(:) + real(rk), allocatable :: explosive_released_mass(:) + real(rk), allocatable :: explosive_released_energy(:) + real(rk), allocatable :: explosive_released_momentum(:,:) contains @@ -41,6 +48,7 @@ contains subroutine sph_explosive_reset() explosive_module_initialized = .false. call sph_explosive_clear_material_table() + call sph_explosive_deallocate_state() end subroutine sph_explosive_reset subroutine sph_explosive_clear_material_table() @@ -62,6 +70,65 @@ contains explosive_ignition_time = 0.0_rk end subroutine sph_explosive_clear_material_table + subroutine sph_explosive_finalize() + call sph_explosive_reset() + end subroutine sph_explosive_finalize + + subroutine sph_explosive_build(ierr) + integer, intent(out) :: ierr + + ierr = SPH_SUCCESS + if (.not. explosive_material_seen) return + call sph_explosive_validate_input(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_explosive_allocate_state(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_explosive_clear_particle_state() + call sph_explosive_compute_detonation_times(ierr) + end subroutine sph_explosive_build + + subroutine sph_explosive_allocate_state(ierr) + integer, intent(out) :: ierr + integer :: stat + + ierr = SPH_SUCCESS + if (npart <= 0) return + if (allocated(explosive_detonation_time)) then + if (size(explosive_detonation_time) == npart) return + call sph_explosive_deallocate_state() + end if + allocate(explosive_ignition_status(npart), & + explosive_detonation_time(npart), & + explosive_reaction_progress(npart), & + explosive_released_mass(npart), & + explosive_released_energy(npart), & + explosive_released_momentum(SPH_NDIM,npart), stat=stat) + if (stat /= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'failed to allocate Explosive state') + ierr = SPH_ERROR_INPUT + return + end if + call sph_explosive_clear_particle_state() + end subroutine sph_explosive_allocate_state + + subroutine sph_explosive_deallocate_state() + if (allocated(explosive_ignition_status)) deallocate(explosive_ignition_status) + if (allocated(explosive_detonation_time)) deallocate(explosive_detonation_time) + if (allocated(explosive_reaction_progress)) deallocate(explosive_reaction_progress) + if (allocated(explosive_released_mass)) deallocate(explosive_released_mass) + if (allocated(explosive_released_energy)) deallocate(explosive_released_energy) + if (allocated(explosive_released_momentum)) deallocate(explosive_released_momentum) + end subroutine sph_explosive_deallocate_state + + subroutine sph_explosive_clear_particle_state() + if (allocated(explosive_ignition_status)) explosive_ignition_status = 0 + if (allocated(explosive_detonation_time)) explosive_detonation_time = 0.0_rk + if (allocated(explosive_reaction_progress)) explosive_reaction_progress = 0.0_rk + if (allocated(explosive_released_mass)) explosive_released_mass = 0.0_rk + if (allocated(explosive_released_energy)) explosive_released_energy = 0.0_rk + if (allocated(explosive_released_momentum)) explosive_released_momentum = 0.0_rk + end subroutine sph_explosive_clear_particle_state + subroutine sph_explosive_note_material(name, material_type_in) character(len=*), intent(in) :: name character(len=*), intent(in) :: material_type_in @@ -242,6 +309,70 @@ contains end do end subroutine sph_explosive_count_unbound_materials + subroutine sph_explosive_compute_detonation_times(ierr) + integer, intent(out) :: ierr + integer :: i + integer :: ign + integer :: explosive_index + real(rk) :: best_time + real(rk) :: distance + real(rk) :: arrival + + ierr = SPH_SUCCESS + call sph_explosive_allocate_state(ierr) + if (ierr /= SPH_SUCCESS) return + do i = 1, npart + if (.not. sph_explosive_is_active_particle(i)) cycle + explosive_index = sph_explosive_table_index_by_material_id(material_id(i)) + if (explosive_index <= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'Explosive particle material has no control') + ierr = SPH_ERROR_INPUT + return + end if + best_time = huge(1.0_rk) + do ign = 1, explosive_ignition_count + if (explosive_ignition_material_id(ign) /= material_id(i)) cycle + distance = sqrt(sum((x0(:,i) - explosive_ignition_x(:,ign)) * & + (x0(:,i) - explosive_ignition_x(:,ign)))) + arrival = explosive_ignition_time(ign) + distance / & + explosive_detonation_velocity(explosive_index) + if (arrival < best_time) best_time = arrival + end do + if (best_time == huge(1.0_rk)) then + call sph_error_set(SPH_ERROR_INPUT, 'Explosive particle has no ignition') + ierr = SPH_ERROR_INPUT + return + end if + explosive_detonation_time(i) = best_time + explosive_ignition_status(i) = 1 + end do + end subroutine sph_explosive_compute_detonation_times + + subroutine sph_explosive_get_detonation_time_range(time_min, time_max, ierr) + real(rk), intent(out) :: time_min + real(rk), intent(out) :: time_max + integer, intent(out) :: ierr + integer :: i + logical :: first_value + + ierr = SPH_SUCCESS + time_min = 0.0_rk + time_max = 0.0_rk + first_value = .true. + if (.not. allocated(explosive_detonation_time)) return + do i = 1, npart + if (.not. sph_explosive_is_active_particle(i)) cycle + if (first_value) then + time_min = explosive_detonation_time(i) + time_max = explosive_detonation_time(i) + first_value = .false. + else + if (explosive_detonation_time(i) < time_min) time_min = explosive_detonation_time(i) + if (explosive_detonation_time(i) > time_max) time_max = explosive_detonation_time(i) + end if + end do + end subroutine sph_explosive_get_detonation_time_range + subroutine sph_explosive_prepare_material(name, explosive_index, ierr) character(len=*), intent(in) :: name integer, intent(out) :: explosive_index @@ -379,6 +510,16 @@ contains end do end function sph_explosive_has_ignition_for_material + logical function sph_explosive_is_active_particle(index) + integer, intent(in) :: index + + sph_explosive_is_active_particle = .false. + if (index < 1 .or. index > npart) return + if (particle_phase(index) /= SPH_PHASE_EXPLOSIVE) return + if (particle_status(index) /= SPH_STATUS_ACTIVE) return + sph_explosive_is_active_particle = .true. + end function sph_explosive_is_active_particle + logical function sph_explosive_is_finite(value) real(rk), intent(in) :: value diff --git a/src/input/sph_input_reader.f90 b/src/input/sph_input_reader.f90 index 7cc4c0c..02a8e3a 100644 --- a/src/input/sph_input_reader.f90 +++ b/src/input/sph_input_reader.f90 @@ -18,7 +18,7 @@ module sph_input_reader sph_gas_apply_initial_conditions, sph_gas_count_unbound_materials use sph_explosive, only: sph_explosive_note_material, & sph_explosive_set_control, sph_explosive_set_ignition, & - sph_explosive_validate_input, sph_explosive_count_unbound_materials + sph_explosive_build, sph_explosive_count_unbound_materials use sph_neighbor, only: sph_neighbor_rebuild use sph_inp_defs, only: SPH_INPUT_LINE_LEN, SPH_INPUT_FIELD_LEN, & SPH_INPUT_MAX_FIELDS, SPH_KEY_NODE, SPH_KEY_ELEMENT, SPH_KEY_NSET, & @@ -107,7 +107,7 @@ contains if (ierr /= SPH_SUCCESS) return call sph_gas_apply_initial_conditions(ierr) if (ierr /= SPH_SUCCESS) return - call sph_explosive_validate_input(ierr) + call sph_explosive_build(ierr) if (ierr /= SPH_SUCCESS) return call sph_gas_count_unbound_materials(gas_unbound_count) input_warning_count = input_warning_count + gas_unbound_count diff --git a/tests/module/test_explosive_detonation_time.f90 b/tests/module/test_explosive_detonation_time.f90 new file mode 100644 index 0000000..e321b7c --- /dev/null +++ b/tests/module/test_explosive_detonation_time.f90 @@ -0,0 +1,63 @@ +program test_explosive_detonation_time + 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_input_warning_count + use sph_explosive, only: explosive_detonation_time, & + explosive_ignition_status, sph_explosive_get_detonation_time_range + implicit none + + integer :: ierr + integer :: warnings + real(rk) :: expected + real(rk) :: time_min + real(rk) :: time_max + + call sph_initialize(ierr) + call assert_ok(ierr, 'initialize failed') + call sph_read_inp('tests/data/explosive_minimal_040.inp', ierr) + call assert_ok(ierr, 'read explosive input failed') + call sph_build_model(ierr) + call assert_ok(ierr, 'build explosive model failed') + call sph_get_input_warning_count(warnings, ierr) + call assert_ok(ierr, 'warning count failed') + if (warnings /= 1) call fail('unexpected explosive warning count') + if (.not. allocated(explosive_detonation_time)) call fail('detonation time not allocated') + if (explosive_ignition_status(1) /= 1) call fail('ignition status not set') + expected = 0.1_rk / 6900.0_rk + if (abs(explosive_detonation_time(1)) > 1.0e-15_rk) then + call fail('origin detonation time mismatch') + end if + if (abs(explosive_detonation_time(2) - expected) > 1.0e-12_rk) then + call fail('x detonation time mismatch') + end if + if (abs(explosive_detonation_time(3) - expected) > 1.0e-12_rk) then + call fail('y detonation time mismatch') + end if + if (abs(explosive_detonation_time(4) - expected) > 1.0e-12_rk) then + call fail('z detonation time mismatch') + end if + call sph_explosive_get_detonation_time_range(time_min, time_max, ierr) + call assert_ok(ierr, 'detonation time range failed') + if (abs(time_min) > 1.0e-15_rk) call fail('detonation min mismatch') + if (abs(time_max - expected) > 1.0e-12_rk) call fail('detonation max mismatch') + call sph_finalize(ierr) + call assert_ok(ierr, 'finalize failed') + +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_explosive_detonation_time diff --git a/todo.md b/todo.md index 0062166..26a91cf 100644 --- a/todo.md +++ b/todo.md @@ -56,26 +56,26 @@ ## 阶段 C:Explosive 模块状态、点火与爆轰到达时间 -- [ ] 新增 `src/explosive` 模块目录和 Fortran 源文件骨架。 -- [ ] 将 Explosive 源文件接入 CMake 构建列表。 -- [ ] 增加 `sph_explosive_reset` 生命周期入口。 -- [ ] 增加 `sph_explosive_build` 模型构建入口。 -- [ ] 增加 `sph_explosive_finalize` 或清理入口。 -- [ ] 为每粒子爆轰到达时间增加状态数组。 -- [ ] 为每粒子反应进度 `lambda` 增加状态数组。 -- [ ] 为每粒子累计释放质量增加状态数组。 -- [ ] 为每粒子累计释放能量增加状态数组。 -- [ ] 为每粒子累计释放动量增加状态数组。 -- [ ] 为每粒子 ignition status 增加状态数组。 -- [ ] 增加 Explosive 状态数组分配过程。 -- [ ] 增加 Explosive 状态数组清零过程。 -- [ ] 增加 Explosive active 粒子筛选过程。 -- [ ] 增加单点点火记录的内部设置过程。 -- [ ] 增加多点点火记录的状态预留和清理过程。 -- [ ] 实现按初始坐标计算单点爆轰到达时间。 -- [ ] 实现多点点火到达时间取最小值的预留路径。 -- [ ] 增加爆轰到达时间范围诊断过程。 -- [ ] 增加爆轰到达时间模块测试并接入 CTest。 +- [x] 新增 `src/explosive` 模块目录和 Fortran 源文件骨架。 +- [x] 将 Explosive 源文件接入 CMake 构建列表。 +- [x] 增加 `sph_explosive_reset` 生命周期入口。 +- [x] 增加 `sph_explosive_build` 模型构建入口。 +- [x] 增加 `sph_explosive_finalize` 或清理入口。 +- [x] 为每粒子爆轰到达时间增加状态数组。 +- [x] 为每粒子反应进度 `lambda` 增加状态数组。 +- [x] 为每粒子累计释放质量增加状态数组。 +- [x] 为每粒子累计释放能量增加状态数组。 +- [x] 为每粒子累计释放动量增加状态数组。 +- [x] 为每粒子 ignition status 增加状态数组。 +- [x] 增加 Explosive 状态数组分配过程。 +- [x] 增加 Explosive 状态数组清零过程。 +- [x] 增加 Explosive active 粒子筛选过程。 +- [x] 增加单点点火记录的内部设置过程。 +- [x] 增加多点点火记录的状态预留和清理过程。 +- [x] 实现按初始坐标计算单点爆轰到达时间。 +- [x] 实现多点点火到达时间取最小值的预留路径。 +- [x] 增加爆轰到达时间范围诊断过程。 +- [x] 增加爆轰到达时间模块测试并接入 CTest。 ## 阶段 D:反应进度、释放量、Gas 源项与 phase 转化