From 5853ac2644be93d13f788c12f1d711b4cdec31c0 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 3 Jul 2026 12:11:47 +0800 Subject: [PATCH] Advance Explosive reactions --- src/explosive/sph_explosive.f90 | 215 ++++++++++++++++++++++++++++- src/solver/sph_solver_explicit.f90 | 9 ++ todo.md | 40 +++--- 3 files changed, 242 insertions(+), 22 deletions(-) diff --git a/src/explosive/sph_explosive.f90 b/src/explosive/sph_explosive.f90 index ba24c71..88b0aa0 100644 --- a/src/explosive/sph_explosive.f90 +++ b/src/explosive/sph_explosive.f90 @@ -5,9 +5,11 @@ module sph_explosive use sph_database, only: SPH_MAX_MATERIALS, SPH_MAX_SECTIONS, material_count, & material_name, material_type, material_density, section_count, & section_material_id, npart, particle_phase, particle_status, & - material_id, x0, SPH_PHASE_EXPLOSIVE, SPH_STATUS_ACTIVE + material_id, x0, v, mass, rho, volume, internal_energy, & + SPH_PHASE_EXPLOSIVE, SPH_PHASE_GAS, SPH_STATUS_ACTIVE use sph_gas, only: sph_gas_table_index_by_material_id, & - gas_material_eos_defined + gas_material_eos_defined, sph_gas_clear_sources, & + sph_gas_add_source_by_particle implicit none integer, parameter :: EXPLOSIVE_CONVERSION_NONE = 0 @@ -37,6 +39,7 @@ module sph_explosive real(rk), allocatable :: explosive_released_mass(:) real(rk), allocatable :: explosive_released_energy(:) real(rk), allocatable :: explosive_released_momentum(:,:) + real(rk) :: explosive_dt_reaction = 0.0_rk contains @@ -68,6 +71,7 @@ contains explosive_ignition_material_id = 0 explosive_ignition_x = 0.0_rk explosive_ignition_time = 0.0_rk + explosive_dt_reaction = 0.0_rk end subroutine sph_explosive_clear_material_table subroutine sph_explosive_finalize() @@ -84,6 +88,8 @@ contains call sph_explosive_allocate_state(ierr) if (ierr /= SPH_SUCCESS) return call sph_explosive_clear_particle_state() + call sph_explosive_apply_initial_conditions(ierr) + if (ierr /= SPH_SUCCESS) return call sph_explosive_compute_detonation_times(ierr) end subroutine sph_explosive_build @@ -348,6 +354,187 @@ contains end do end subroutine sph_explosive_compute_detonation_times + subroutine sph_explosive_apply_initial_conditions(ierr) + integer, intent(out) :: ierr + integer :: i + integer :: mat_index + + ierr = SPH_SUCCESS + do i = 1, npart + if (.not. sph_explosive_is_active_particle(i)) cycle + mat_index = material_id(i) + if (mat_index < 1 .or. mat_index > material_count) then + call sph_error_set(SPH_ERROR_INPUT, 'Explosive particle material is invalid') + ierr = SPH_ERROR_INPUT + return + end if + if (material_density(mat_index) <= 0.0_rk) then + call sph_error_set(SPH_ERROR_INPUT, 'Explosive particle density is invalid') + ierr = SPH_ERROR_INPUT + return + end if + rho(i) = material_density(mat_index) + if (mass(i) <= 0.0_rk) mass(i) = 1.0_rk + volume(i) = mass(i) / rho(i) + end do + end subroutine sph_explosive_apply_initial_conditions + + subroutine sph_explosive_advance(dt, time, ierr) + real(rk), intent(in) :: dt + real(rk), intent(in) :: time + integer, intent(out) :: ierr + integer :: i + integer :: explosive_index + real(rk) :: lambda_old + real(rk) :: lambda_target + real(rk) :: delta_lambda + real(rk) :: release_mass + real(rk) :: release_energy + real(rk) :: release_momentum(SPH_NDIM) + + ierr = SPH_SUCCESS + if (.not. explosive_material_seen) return + if (dt <= 0.0_rk) then + call sph_error_set(SPH_ERROR_ARGUMENT, 'Explosive time step must be positive') + ierr = SPH_ERROR_ARGUMENT + return + end if + call sph_explosive_allocate_state(ierr) + if (ierr /= SPH_SUCCESS) return + call sph_gas_clear_sources(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 + lambda_old = explosive_reaction_progress(i) + lambda_target = sph_explosive_reaction_target(i, explosive_index, time) + if (lambda_target < lambda_old) lambda_target = lambda_old + if (lambda_target > 1.0_rk) lambda_target = 1.0_rk + delta_lambda = lambda_target - lambda_old + if (delta_lambda <= 0.0_rk) cycle + release_mass = delta_lambda * mass(i) + release_energy = release_mass * explosive_energy_per_mass(explosive_index) + release_momentum = release_mass * v(:,i) + explosive_reaction_progress(i) = lambda_target + explosive_released_mass(i) = explosive_released_mass(i) + release_mass + explosive_released_energy(i) = explosive_released_energy(i) + release_energy + explosive_released_momentum(:,i) = explosive_released_momentum(:,i) + & + release_momentum + if (explosive_conversion_mode(explosive_index) == EXPLOSIVE_CONVERSION_SOURCE) then + call sph_explosive_apply_source_release(explosive_index, release_mass, & + release_momentum, release_energy, dt, ierr) + if (ierr /= SPH_SUCCESS) return + else if (lambda_target >= 1.0_rk) then + call sph_explosive_convert_particle_to_gas(i, explosive_index, ierr) + if (ierr /= SPH_SUCCESS) return + end if + end do + end subroutine sph_explosive_advance + + real(rk) function sph_explosive_reaction_target(index, explosive_index, time) + integer, intent(in) :: index + integer, intent(in) :: explosive_index + real(rk), intent(in) :: time + real(rk) :: burn_window + + sph_explosive_reaction_target = explosive_reaction_progress(index) + if (time < explosive_detonation_time(index)) return + burn_window = explosive_burn_time(explosive_index) + if (burn_window <= 0.0_rk) then + sph_explosive_reaction_target = 1.0_rk + else + sph_explosive_reaction_target = & + (time - explosive_detonation_time(index)) / burn_window + if (sph_explosive_reaction_target < 0.0_rk) then + sph_explosive_reaction_target = 0.0_rk + end if + if (sph_explosive_reaction_target > 1.0_rk) then + sph_explosive_reaction_target = 1.0_rk + end if + end if + end function sph_explosive_reaction_target + + subroutine sph_explosive_apply_source_release(explosive_index, release_mass, & + release_momentum, release_energy, dt, ierr) + integer, intent(in) :: explosive_index + real(rk), intent(in) :: release_mass + real(rk), intent(in) :: release_momentum(SPH_NDIM) + real(rk), intent(in) :: release_energy + real(rk), intent(in) :: dt + integer, intent(out) :: ierr + integer :: product_id + integer :: target_count + integer :: i + real(rk) :: mass_rate + real(rk) :: energy_rate + real(rk) :: momentum_rate(SPH_NDIM) + + ierr = SPH_SUCCESS + product_id = explosive_product_material_id(explosive_index) + target_count = sph_explosive_count_active_gas(product_id) + if (target_count <= 0) then + call sph_error_set(SPH_ERROR_INPUT, 'Explosive SOURCE conversion has no active product Gas target') + ierr = SPH_ERROR_INPUT + return + end if + mass_rate = release_mass / (dt * real(target_count, rk)) + energy_rate = release_energy / (dt * real(target_count, rk)) + momentum_rate = release_momentum / (dt * real(target_count, rk)) + do i = 1, npart + if (.not. sph_explosive_is_active_gas_target(i, product_id)) cycle + call sph_gas_add_source_by_particle(i, mass_rate, momentum_rate, & + energy_rate, ierr) + if (ierr /= SPH_SUCCESS) return + end do + end subroutine sph_explosive_apply_source_release + + subroutine sph_explosive_convert_particle_to_gas(index, explosive_index, ierr) + integer, intent(in) :: index + integer, intent(in) :: explosive_index + integer, intent(out) :: ierr + integer :: product_id + + ierr = SPH_SUCCESS + product_id = explosive_product_material_id(explosive_index) + if (product_id < 1 .or. product_id > material_count) then + call sph_error_set(SPH_ERROR_INPUT, 'Explosive product material is invalid') + ierr = SPH_ERROR_INPUT + return + end if + particle_phase(index) = SPH_PHASE_GAS + material_id(index) = product_id + if (material_density(product_id) > 0.0_rk) rho(index) = material_density(product_id) + if (rho(index) > 0.0_rk) volume(index) = mass(index) / rho(index) + internal_energy(index) = explosive_energy_per_mass(explosive_index) + end subroutine sph_explosive_convert_particle_to_gas + + subroutine sph_explosive_estimate_dt_limit(dt_reaction, ierr) + real(rk), intent(out) :: dt_reaction + integer, intent(out) :: ierr + integer :: i + real(rk) :: burn_window + + ierr = SPH_SUCCESS + dt_reaction = 0.0_rk + if (.not. explosive_material_seen) then + explosive_dt_reaction = 0.0_rk + return + end if + do i = 1, explosive_material_count + burn_window = explosive_burn_time(i) + if (burn_window <= 0.0_rk) cycle + if (dt_reaction <= 0.0_rk .or. burn_window / 4.0_rk < dt_reaction) then + dt_reaction = burn_window / 4.0_rk + end if + end do + explosive_dt_reaction = dt_reaction + end subroutine sph_explosive_estimate_dt_limit + subroutine sph_explosive_get_detonation_time_range(time_min, time_max, ierr) real(rk), intent(out) :: time_min real(rk), intent(out) :: time_max @@ -510,6 +697,30 @@ contains end do end function sph_explosive_has_ignition_for_material + integer function sph_explosive_count_active_gas(product_id) + integer, intent(in) :: product_id + integer :: i + + sph_explosive_count_active_gas = 0 + do i = 1, npart + if (sph_explosive_is_active_gas_target(i, product_id)) then + sph_explosive_count_active_gas = sph_explosive_count_active_gas + 1 + end if + end do + end function sph_explosive_count_active_gas + + logical function sph_explosive_is_active_gas_target(index, product_id) + integer, intent(in) :: index + integer, intent(in) :: product_id + + sph_explosive_is_active_gas_target = .false. + if (index < 1 .or. index > npart) return + if (particle_phase(index) /= SPH_PHASE_GAS) return + if (particle_status(index) /= SPH_STATUS_ACTIVE) return + if (material_id(index) /= product_id) return + sph_explosive_is_active_gas_target = .true. + end function sph_explosive_is_active_gas_target + logical function sph_explosive_is_active_particle(index) integer, intent(in) :: index diff --git a/src/solver/sph_solver_explicit.f90 b/src/solver/sph_solver_explicit.f90 index b2b193f..c9dbaf3 100644 --- a/src/solver/sph_solver_explicit.f90 +++ b/src/solver/sph_solver_explicit.f90 @@ -11,6 +11,7 @@ module sph_solver_explicit use sph_gas, only: sph_gas_update_pressure, sph_gas_compute_density_rate, & sph_gas_compute_acceleration, sph_gas_update_diagnostics, & sph_gas_estimate_dt_limits, sph_gas_apply_sources_explicit + use sph_explosive, only: sph_explosive_advance, sph_explosive_estimate_dt_limit use sph_output_csv, only: sph_output_write_step implicit none @@ -62,6 +63,8 @@ contains rho(i) = rho(i) + dt * drho_dt(i) v(:,i) = vhalf(:,i) + 0.5_rk * dt * a(:,i) end do + call sph_explosive_advance(dt, current_time + dt, ierr) + if (ierr /= SPH_SUCCESS) return call sph_gas_apply_sources_explicit(dt, ierr) if (ierr /= SPH_SUCCESS) return call sph_water_update_diagnostics(ierr) @@ -107,6 +110,7 @@ contains real(rk) :: gas_cfl real(rk) :: gas_accel real(rk) :: gas_limit + real(rk) :: explosive_limit real(rk) :: water_limit if (safety_factor <= 0.0_rk) then @@ -125,6 +129,11 @@ contains if (gas_limit > 0.0_rk) then if (dtcrit <= 0.0_rk .or. gas_limit < dtcrit) dtcrit = gas_limit end if + call sph_explosive_estimate_dt_limit(explosive_limit, ierr) + if (ierr /= SPH_SUCCESS) return + if (explosive_limit > 0.0_rk) then + if (dtcrit <= 0.0_rk .or. explosive_limit < dtcrit) dtcrit = explosive_limit + end if dtcrit = dtcrit * safety_factor end subroutine sph_solver_phase_dt diff --git a/todo.md b/todo.md index 26a91cf..9e51f87 100644 --- a/todo.md +++ b/todo.md @@ -79,26 +79,26 @@ ## 阶段 D:反应进度、释放量、Gas 源项与 phase 转化 -- [ ] 实现 programmed burn 反应进度目标值计算。 -- [ ] 实现 `burn_time = 0` 的瞬时反应路径。 -- [ ] 实现 `burn_time > 0` 的线性反应窗口路径。 -- [ ] 保证 `lambda` 被限制在 `[0,1]` 范围内。 -- [ ] 保证 `lambda` 单调不可逆增加。 -- [ ] 计算每步 `delta_lambda` 反应增量。 -- [ ] 按 `delta_lambda * mass_initial` 计算释放质量。 -- [ ] 按释放质量和单位质量能量计算释放能量。 -- [ ] 按释放质量和当前速度计算释放动量。 -- [ ] 将释放质量累计到 Explosive 诊断状态。 -- [ ] 将释放能量累计到 Explosive 诊断状态。 -- [ ] 将释放动量累计到 Explosive 诊断状态。 -- [ ] 实现 `CONVERSION=SOURCE` 到 Gas 源项缓存的注入路径。 -- [ ] 实现 `CONVERSION=PHASE` 的完全反应后 phase 转化路径。 -- [ ] 将完全反应粒子的 material 改为 product Gas material。 -- [ ] 在 phase 转化后刷新 Gas 压力和 summary。 -- [ ] 对无法确定目标 Gas 状态的 source 注入路径报 error。 -- [ ] 将 Explosive 更新挂接到显式求解器步骤中。 -- [ ] 增加反应时间步约束诊断或预留字段。 -- [ ] 确认无 Explosive 输入时 Water/Gas 算例不进入 Explosive 更新路径。 +- [x] 实现 programmed burn 反应进度目标值计算。 +- [x] 实现 `burn_time = 0` 的瞬时反应路径。 +- [x] 实现 `burn_time > 0` 的线性反应窗口路径。 +- [x] 保证 `lambda` 被限制在 `[0,1]` 范围内。 +- [x] 保证 `lambda` 单调不可逆增加。 +- [x] 计算每步 `delta_lambda` 反应增量。 +- [x] 按 `delta_lambda * mass_initial` 计算释放质量。 +- [x] 按释放质量和单位质量能量计算释放能量。 +- [x] 按释放质量和当前速度计算释放动量。 +- [x] 将释放质量累计到 Explosive 诊断状态。 +- [x] 将释放能量累计到 Explosive 诊断状态。 +- [x] 将释放动量累计到 Explosive 诊断状态。 +- [x] 实现 `CONVERSION=SOURCE` 到 Gas 源项缓存的注入路径。 +- [x] 实现 `CONVERSION=PHASE` 的完全反应后 phase 转化路径。 +- [x] 将完全反应粒子的 material 改为 product Gas material。 +- [x] 在 phase 转化后刷新 Gas 压力和 summary。 +- [x] 对无法确定目标 Gas 状态的 source 注入路径报 error。 +- [x] 将 Explosive 更新挂接到显式求解器步骤中。 +- [x] 增加反应时间步约束诊断或预留字段。 +- [x] 确认无 Explosive 输入时 Water/Gas 算例不进入 Explosive 更新路径。 ## 阶段 E:Summary、Output、Restart、API、C++、Python 与 CLI