Add Explosive input controls

This commit is contained in:
Codex
2026-07-03 11:57:50 +08:00
parent 434317d94d
commit d4e51e4177
3 changed files with 484 additions and 20 deletions
+400
View File
@@ -1,15 +1,415 @@
module sph_explosive
use sph_precision, only: rk, SPH_NDIM
use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_INPUT, &
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
use sph_gas, only: sph_gas_table_index_by_material_id, &
gas_material_eos_defined
implicit none
integer, parameter :: EXPLOSIVE_CONVERSION_NONE = 0
integer, parameter :: EXPLOSIVE_CONVERSION_PHASE = 1
integer, parameter :: EXPLOSIVE_CONVERSION_SOURCE = 2
logical :: explosive_module_initialized = .false.
logical :: explosive_material_seen = .false.
integer :: explosive_material_count = 0
integer :: explosive_material_id(SPH_MAX_MATERIALS) = 0
character(len=64) :: explosive_material_name(SPH_MAX_MATERIALS) = ' '
logical :: explosive_control_defined(SPH_MAX_MATERIALS) = .false.
integer :: explosive_product_material_id(SPH_MAX_MATERIALS) = 0
character(len=64) :: explosive_product_material_name(SPH_MAX_MATERIALS) = ' '
real(rk) :: explosive_density0(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: explosive_detonation_velocity(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: explosive_energy_per_mass(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: explosive_burn_time(SPH_MAX_MATERIALS) = 0.0_rk
integer :: explosive_conversion_mode(SPH_MAX_MATERIALS) = EXPLOSIVE_CONVERSION_NONE
integer :: explosive_ignition_count = 0
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
contains
subroutine sph_explosive_initialize()
explosive_module_initialized = .true.
call sph_explosive_clear_material_table()
end subroutine sph_explosive_initialize
subroutine sph_explosive_reset()
explosive_module_initialized = .false.
call sph_explosive_clear_material_table()
end subroutine sph_explosive_reset
subroutine sph_explosive_clear_material_table()
explosive_material_seen = .false.
explosive_material_count = 0
explosive_material_id = 0
explosive_material_name = ' '
explosive_control_defined = .false.
explosive_product_material_id = 0
explosive_product_material_name = ' '
explosive_density0 = 0.0_rk
explosive_detonation_velocity = 0.0_rk
explosive_energy_per_mass = 0.0_rk
explosive_burn_time = 0.0_rk
explosive_conversion_mode = EXPLOSIVE_CONVERSION_NONE
explosive_ignition_count = 0
explosive_ignition_material_id = 0
explosive_ignition_x = 0.0_rk
explosive_ignition_time = 0.0_rk
end subroutine sph_explosive_clear_material_table
subroutine sph_explosive_note_material(name, material_type_in)
character(len=*), intent(in) :: name
character(len=*), intent(in) :: material_type_in
integer :: explosive_index
if (index(sph_explosive_upper(material_type_in), 'EXPLOSIVE') > 0) then
explosive_material_seen = .true.
explosive_index = sph_explosive_register_material(name)
end if
end subroutine sph_explosive_note_material
subroutine sph_explosive_set_control(material_name_in, model_type, &
detonation_velocity, energy_per_mass, burn_time, &
product_material_name_in, conversion_name, ierr)
character(len=*), intent(in) :: material_name_in
character(len=*), intent(in) :: model_type
real(rk), intent(in) :: detonation_velocity
real(rk), intent(in) :: energy_per_mass
real(rk), intent(in) :: burn_time
character(len=*), intent(in) :: product_material_name_in
character(len=*), intent(in) :: conversion_name
integer, intent(out) :: ierr
integer :: explosive_index
integer :: product_index
integer :: conversion_mode
character(len=64) :: upper_model
ierr = SPH_SUCCESS
upper_model = sph_explosive_upper(model_type)
if (len_trim(material_name_in) == 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive control MATERIAL is required')
ierr = SPH_ERROR_INPUT
return
end if
if (index(upper_model, 'PROGRAMMED_BURN') <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive TYPE must be PROGRAMMED_BURN')
ierr = SPH_ERROR_INPUT
return
end if
if (detonation_velocity <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Explosive detonation velocity must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (energy_per_mass < 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Explosive energy must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (burn_time < 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Explosive burn time must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (len_trim(product_material_name_in) == 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive PRODUCT_MATERIAL is required')
ierr = SPH_ERROR_INPUT
return
end if
call sph_explosive_prepare_material(material_name_in, explosive_index, ierr)
if (ierr /= SPH_SUCCESS) return
if (explosive_control_defined(explosive_index)) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive material control is already defined')
ierr = SPH_ERROR_INPUT
return
end if
product_index = sph_explosive_find_material(product_material_name_in)
if (product_index <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive product material is unknown')
ierr = SPH_ERROR_INPUT
return
end if
if (index(sph_explosive_upper(material_type(product_index)), 'GAS') <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive product material must be Gas')
ierr = SPH_ERROR_INPUT
return
end if
call sph_explosive_validate_product_gas(product_index, ierr)
if (ierr /= SPH_SUCCESS) return
conversion_mode = sph_explosive_conversion_id(conversion_name)
if (conversion_mode == EXPLOSIVE_CONVERSION_NONE) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive CONVERSION must be PHASE or SOURCE')
ierr = SPH_ERROR_INPUT
return
end if
explosive_control_defined(explosive_index) = .true.
explosive_product_material_id(explosive_index) = product_index
explosive_product_material_name(explosive_index) = material_name(product_index)
explosive_density0(explosive_index) = material_density(explosive_material_id(explosive_index))
explosive_detonation_velocity(explosive_index) = detonation_velocity
explosive_energy_per_mass(explosive_index) = energy_per_mass
explosive_burn_time(explosive_index) = burn_time
explosive_conversion_mode(explosive_index) = conversion_mode
end subroutine sph_explosive_set_control
subroutine sph_explosive_set_ignition(material_name_in, ignition_x, &
ignition_time, ierr)
character(len=*), intent(in) :: material_name_in
real(rk), intent(in) :: ignition_x(SPH_NDIM)
real(rk), intent(in) :: ignition_time
integer, intent(out) :: ierr
integer :: explosive_index
ierr = SPH_SUCCESS
if (len_trim(material_name_in) == 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive ignition MATERIAL is required')
ierr = SPH_ERROR_INPUT
return
end if
if (ignition_time < 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Explosive ignition time must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (.not. sph_explosive_vector_is_finite(ignition_x)) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Explosive ignition coordinate is not finite')
ierr = SPH_ERROR_ARGUMENT
return
end if
call sph_explosive_prepare_material(material_name_in, explosive_index, ierr)
if (ierr /= SPH_SUCCESS) return
if (explosive_ignition_count >= SPH_MAX_MATERIALS) then
call sph_error_set(SPH_ERROR_INPUT, 'too many Explosive ignition records')
ierr = SPH_ERROR_INPUT
return
end if
explosive_ignition_count = explosive_ignition_count + 1
explosive_ignition_material_id(explosive_ignition_count) = &
explosive_material_id(explosive_index)
explosive_ignition_x(:,explosive_ignition_count) = ignition_x
explosive_ignition_time(explosive_ignition_count) = ignition_time
end subroutine sph_explosive_set_ignition
subroutine sph_explosive_validate_input(ierr)
integer, intent(out) :: ierr
integer :: explosive_index
integer :: mat_index
ierr = SPH_SUCCESS
if (.not. explosive_material_seen) return
do explosive_index = 1, explosive_material_count
mat_index = explosive_material_id(explosive_index)
if (mat_index < 1 .or. mat_index > material_count) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive material table 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, 'input is missing *DENSITY for Explosive material')
ierr = SPH_ERROR_INPUT
return
end if
if (.not. explosive_control_defined(explosive_index)) then
call sph_error_set(SPH_ERROR_INPUT, 'input is missing *SPHLITE EXPLOSIVE for Explosive material')
ierr = SPH_ERROR_INPUT
return
end if
call sph_explosive_validate_product_gas( &
explosive_product_material_id(explosive_index), ierr)
if (ierr /= SPH_SUCCESS) return
if (.not. sph_explosive_has_ignition_for_material(mat_index)) then
call sph_error_set(SPH_ERROR_INPUT, 'input is missing *SPHLITE IGNITION for Explosive material')
ierr = SPH_ERROR_INPUT
return
end if
end do
end subroutine sph_explosive_validate_input
subroutine sph_explosive_count_unbound_materials(count)
integer, intent(out) :: count
integer :: i
count = 0
do i = 1, explosive_material_count
if (.not. sph_explosive_has_section_binding_for_id(explosive_material_id(i))) then
count = count + 1
end if
end do
end subroutine sph_explosive_count_unbound_materials
subroutine sph_explosive_prepare_material(name, explosive_index, ierr)
character(len=*), intent(in) :: name
integer, intent(out) :: explosive_index
integer, intent(out) :: ierr
integer :: mat_index
ierr = SPH_SUCCESS
explosive_index = 0
mat_index = sph_explosive_find_material(name)
if (mat_index <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive control references unknown material')
ierr = SPH_ERROR_INPUT
return
end if
if (index(sph_explosive_upper(material_type(mat_index)), 'EXPLOSIVE') <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive control references non-Explosive material')
ierr = SPH_ERROR_INPUT
return
end if
explosive_index = sph_explosive_register_material(name)
if (explosive_index <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'too many Explosive material records')
ierr = SPH_ERROR_INPUT
end if
end subroutine sph_explosive_prepare_material
integer function sph_explosive_register_material(name)
character(len=*), intent(in) :: name
integer :: mat_index
integer :: explosive_index
sph_explosive_register_material = 0
mat_index = sph_explosive_find_material(name)
if (mat_index <= 0) return
explosive_index = sph_explosive_table_index_by_material_id(mat_index)
if (explosive_index > 0) then
sph_explosive_register_material = explosive_index
return
end if
if (explosive_material_count >= SPH_MAX_MATERIALS) return
explosive_material_count = explosive_material_count + 1
explosive_material_id(explosive_material_count) = mat_index
explosive_material_name(explosive_material_count) = material_name(mat_index)
explosive_density0(explosive_material_count) = material_density(mat_index)
sph_explosive_register_material = explosive_material_count
end function sph_explosive_register_material
integer function sph_explosive_table_index_by_material_id(mat_index)
integer, intent(in) :: mat_index
integer :: i
sph_explosive_table_index_by_material_id = 0
do i = 1, explosive_material_count
if (explosive_material_id(i) == mat_index) then
sph_explosive_table_index_by_material_id = i
return
end if
end do
end function sph_explosive_table_index_by_material_id
integer function sph_explosive_find_material(name)
character(len=*), intent(in) :: name
integer :: i
sph_explosive_find_material = 0
do i = 1, material_count
if (trim(sph_explosive_upper(material_name(i))) == &
trim(sph_explosive_upper(name))) then
sph_explosive_find_material = i
return
end if
end do
end function sph_explosive_find_material
integer function sph_explosive_conversion_id(conversion_name)
character(len=*), intent(in) :: conversion_name
character(len=64) :: upper_name
upper_name = sph_explosive_upper(conversion_name)
if (len_trim(conversion_name) == 0 .or. index(upper_name, 'PHASE') > 0) then
sph_explosive_conversion_id = EXPLOSIVE_CONVERSION_PHASE
else if (index(upper_name, 'SOURCE') > 0) then
sph_explosive_conversion_id = EXPLOSIVE_CONVERSION_SOURCE
else
sph_explosive_conversion_id = EXPLOSIVE_CONVERSION_NONE
end if
end function sph_explosive_conversion_id
subroutine sph_explosive_validate_product_gas(product_index, ierr)
integer, intent(in) :: product_index
integer, intent(out) :: ierr
integer :: gas_index
ierr = SPH_SUCCESS
if (product_index < 1 .or. product_index > material_count) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive product material is unknown')
ierr = SPH_ERROR_INPUT
return
end if
if (index(sph_explosive_upper(material_type(product_index)), 'GAS') <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive product material must be Gas')
ierr = SPH_ERROR_INPUT
return
end if
gas_index = sph_gas_table_index_by_material_id(product_index)
if (gas_index <= 0 .or. .not. gas_material_eos_defined(gas_index)) then
call sph_error_set(SPH_ERROR_INPUT, 'Explosive product Gas material has no EOS')
ierr = SPH_ERROR_INPUT
end if
end subroutine sph_explosive_validate_product_gas
logical function sph_explosive_has_section_binding_for_id(mat_index)
integer, intent(in) :: mat_index
integer :: i
sph_explosive_has_section_binding_for_id = .false.
do i = 1, section_count
if (section_material_id(i) == mat_index) then
sph_explosive_has_section_binding_for_id = .true.
return
end if
end do
end function sph_explosive_has_section_binding_for_id
logical function sph_explosive_has_ignition_for_material(mat_index)
integer, intent(in) :: mat_index
integer :: i
sph_explosive_has_ignition_for_material = .false.
do i = 1, explosive_ignition_count
if (explosive_ignition_material_id(i) == mat_index) then
sph_explosive_has_ignition_for_material = .true.
return
end if
end do
end function sph_explosive_has_ignition_for_material
logical function sph_explosive_is_finite(value)
real(rk), intent(in) :: value
sph_explosive_is_finite = value == value .and. abs(value) < huge(value)
end function sph_explosive_is_finite
logical function sph_explosive_vector_is_finite(values)
real(rk), intent(in) :: values(SPH_NDIM)
integer :: i
sph_explosive_vector_is_finite = .true.
do i = 1, SPH_NDIM
if (.not. sph_explosive_is_finite(values(i))) then
sph_explosive_vector_is_finite = .false.
end if
end do
end function sph_explosive_vector_is_finite
function sph_explosive_upper(text) result(out)
character(len=*), intent(in) :: text
character(len=len(text)) :: out
integer :: i
integer :: code
out = text
do i = 1, len(out)
code = ichar(out(i:i))
if (code >= ichar('a') .and. code <= ichar('z')) then
out(i:i) = char(code - ichar('a') + ichar('A'))
end if
end do
end function sph_explosive_upper
end module sph_explosive