Files
SPHLite/src/gas/sph_gas.f90
T
2026-07-03 11:31:28 +08:00

1065 lines
38 KiB
Fortran

module sph_gas
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: npart, SPH_MAX_MATERIALS, particle_phase, &
particle_status, material_id, material_count, material_name, &
material_type, material_density, &
section_count, section_material_id, x, v, a, mass, rho, volume, &
pressure, internal_energy, temperature, h, drho_dt, acc_pressure, &
acc_body, neighbor_count, neighbor_offset, neighbor_list, &
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_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
integer, parameter :: GAS_EOS_IDEAL = 1
integer, parameter :: GAS_EOS_JWL = 2
integer, parameter :: GAS_EOS_STIFFENED = 3
logical :: gas_module_initialized = .false.
logical :: gas_material_seen = .false.
logical :: gas_eos_defined = .false.
logical :: gas_particle_control_defined = .false.
logical :: gas_bubble_history_valid = .false.
character(len=64) :: gas_material_name = 'GAS'
integer :: gas_material_internal_id = 0
integer :: gas_eos_type = GAS_EOS_NONE
real(rk) :: gas_rho0 = 1.0_rk
real(rk) :: gas_gamma = 1.4_rk
real(rk) :: gas_e0 = 0.0_rk
real(rk) :: gas_p0 = 0.0_rk
real(rk) :: gas_jwl_a = 0.0_rk
real(rk) :: gas_jwl_b = 0.0_rk
real(rk) :: gas_jwl_r1 = 0.0_rk
real(rk) :: gas_jwl_r2 = 0.0_rk
real(rk) :: gas_jwl_omega = 0.0_rk
real(rk) :: gas_pinf = 0.0_rk
real(rk) :: gas_default_h = 1.0_rk
real(rk) :: gas_default_mass = 1.0_rk
real(rk) :: gas_default_volume = 1.0_rk
real(rk) :: gas_body_force(SPH_NDIM) = 0.0_rk
integer :: gas_material_count = 0
integer :: gas_material_ids(SPH_MAX_MATERIALS) = 0
character(len=64) :: gas_material_names(SPH_MAX_MATERIALS) = ' '
logical :: gas_material_eos_defined(SPH_MAX_MATERIALS) = .false.
integer :: gas_material_eos_type(SPH_MAX_MATERIALS) = GAS_EOS_NONE
real(rk) :: gas_material_density_default(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: gas_material_energy_default(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: gas_material_temperature_default(SPH_MAX_MATERIALS) = 293.15_rk
real(rk) :: gas_material_rho0(SPH_MAX_MATERIALS) = 1.0_rk
real(rk) :: gas_material_gamma(SPH_MAX_MATERIALS) = 1.4_rk
real(rk) :: gas_material_p0(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: gas_material_jwl_a(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: gas_material_jwl_b(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: gas_material_jwl_r1(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: gas_material_jwl_r2(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: gas_material_jwl_omega(SPH_MAX_MATERIALS) = 0.0_rk
real(rk) :: gas_material_pinf(SPH_MAX_MATERIALS) = 0.0_rk
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
contains
subroutine sph_gas_initialize()
gas_module_initialized = .true.
call sph_gas_reset_state()
end subroutine sph_gas_initialize
subroutine sph_gas_reset()
gas_module_initialized = .false.
call sph_gas_reset_state()
end subroutine sph_gas_reset
subroutine sph_gas_reset_state()
gas_material_seen = .false.
gas_eos_defined = .false.
gas_particle_control_defined = .false.
gas_bubble_history_valid = .false.
gas_material_name = 'GAS'
gas_material_internal_id = 0
gas_eos_type = GAS_EOS_NONE
gas_rho0 = 1.0_rk
gas_gamma = 1.4_rk
gas_e0 = 0.0_rk
gas_p0 = 0.0_rk
gas_jwl_a = 0.0_rk
gas_jwl_b = 0.0_rk
gas_jwl_r1 = 0.0_rk
gas_jwl_r2 = 0.0_rk
gas_jwl_omega = 0.0_rk
gas_pinf = 0.0_rk
gas_default_h = 1.0_rk
gas_default_mass = 1.0_rk
gas_default_volume = 1.0_rk
gas_body_force = 0.0_rk
gas_material_count = 0
gas_material_ids = 0
gas_material_names = ' '
gas_material_eos_defined = .false.
gas_material_eos_type = GAS_EOS_NONE
gas_material_density_default = 0.0_rk
gas_material_energy_default = 0.0_rk
gas_material_temperature_default = 293.15_rk
gas_material_rho0 = 1.0_rk
gas_material_gamma = 1.4_rk
gas_material_p0 = 0.0_rk
gas_material_jwl_a = 0.0_rk
gas_material_jwl_b = 0.0_rk
gas_material_jwl_r1 = 0.0_rk
gas_material_jwl_r2 = 0.0_rk
gas_material_jwl_omega = 0.0_rk
gas_material_pinf = 0.0_rk
gas_material_default_h = 1.0_rk
gas_material_default_mass = 1.0_rk
gas_material_default_volume = 1.0_rk
call sph_gas_source_reset()
gas_previous_equivalent_radius = 0.0_rk
gas_previous_center = 0.0_rk
call sph_db_clear_gas_diagnostics()
end subroutine sph_gas_reset_state
subroutine sph_gas_note_material(name, material_type_in)
character(len=*), intent(in) :: name
character(len=*), intent(in) :: material_type_in
integer :: gas_index
if (index(sph_gas_upper(material_type_in), 'GAS') > 0) then
gas_material_seen = .true.
gas_material_name = name
gas_material_internal_id = sph_gas_find_material(name)
gas_index = sph_gas_register_material(name)
end if
end subroutine sph_gas_note_material
subroutine sph_gas_set_eos(eos_type, material_name_in, rho0, gamma, e0, p0, &
a_value, b_value, r1, r2, omega, pinf, ierr)
character(len=*), intent(in) :: eos_type
character(len=*), intent(in) :: material_name_in
real(rk), intent(in) :: rho0
real(rk), intent(in) :: gamma
real(rk), intent(in) :: e0
real(rk), intent(in) :: p0
real(rk), intent(in) :: a_value
real(rk), intent(in) :: b_value
real(rk), intent(in) :: r1
real(rk), intent(in) :: r2
real(rk), intent(in) :: omega
real(rk), intent(in) :: pinf
integer, intent(out) :: ierr
character(len=64) :: upper_type
ierr = SPH_SUCCESS
upper_type = sph_gas_upper(eos_type)
if (len_trim(eos_type) == 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS TYPE is required')
ierr = SPH_ERROR_INPUT
return
end if
if (len_trim(material_name_in) == 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS MATERIAL is required')
ierr = SPH_ERROR_INPUT
return
end if
if (index(upper_type, 'IDEAL_GAS') > 0) then
call sph_gas_set_ideal(material_name_in, gamma, e0, p0, ierr)
else if (index(upper_type, 'JWL') > 0) then
call sph_gas_set_jwl(material_name_in, rho0, a_value, b_value, r1, r2, &
omega, e0, ierr)
else if (index(upper_type, 'STIFFENED_GAS') > 0) then
call sph_gas_set_stiffened(material_name_in, rho0, gamma, e0, pinf, ierr)
else
call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS TYPE is not supported')
ierr = SPH_ERROR_INPUT
end if
end subroutine sph_gas_set_eos
subroutine sph_gas_set_ideal(material_name_in, gamma, e0, p0, ierr)
character(len=*), intent(in) :: material_name_in
real(rk), intent(in) :: gamma
real(rk), intent(in) :: e0
real(rk), intent(in) :: p0
integer, intent(out) :: ierr
integer :: gas_index
ierr = SPH_SUCCESS
if (gamma <= 1.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Ideal gas gamma must be greater than one')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (e0 < 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Ideal gas internal energy must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
call sph_gas_prepare_eos_material(material_name_in, gas_index, ierr)
if (ierr /= SPH_SUCCESS) return
gas_material_seen = .true.
gas_material_name = material_name_in
gas_material_internal_id = sph_gas_find_material(material_name_in)
gas_eos_type = GAS_EOS_IDEAL
gas_gamma = gamma
gas_e0 = e0
gas_p0 = p0
gas_eos_defined = .true.
gas_material_eos_type(gas_index) = GAS_EOS_IDEAL
gas_material_eos_defined(gas_index) = .true.
gas_material_gamma(gas_index) = gamma
gas_material_energy_default(gas_index) = e0
gas_material_p0(gas_index) = p0
gas_material_rho0(gas_index) = sph_gas_material_density(material_name_in)
gas_material_density_default(gas_index) = sph_gas_material_density(material_name_in)
end subroutine sph_gas_set_ideal
subroutine sph_gas_set_jwl(material_name_in, rho0, a_value, b_value, r1, r2, &
omega, e0, ierr)
character(len=*), intent(in) :: material_name_in
real(rk), intent(in) :: rho0
real(rk), intent(in) :: a_value
real(rk), intent(in) :: b_value
real(rk), intent(in) :: r1
real(rk), intent(in) :: r2
real(rk), intent(in) :: omega
real(rk), intent(in) :: e0
integer, intent(out) :: ierr
integer :: gas_index
ierr = SPH_SUCCESS
if (rho0 <= 0.0_rk .or. r1 <= 0.0_rk .or. r2 <= 0.0_rk .or. &
omega <= 0.0_rk .or. e0 < 0.0_rk .or. a_value < 0.0_rk .or. &
b_value < 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'JWL gas EOS parameters are invalid')
ierr = SPH_ERROR_ARGUMENT
return
end if
call sph_gas_prepare_eos_material(material_name_in, gas_index, ierr)
if (ierr /= SPH_SUCCESS) return
gas_material_seen = .true.
gas_material_name = material_name_in
gas_material_internal_id = sph_gas_find_material(material_name_in)
gas_eos_type = GAS_EOS_JWL
gas_rho0 = rho0
gas_jwl_a = a_value
gas_jwl_b = b_value
gas_jwl_r1 = r1
gas_jwl_r2 = r2
gas_jwl_omega = omega
gas_e0 = e0
gas_eos_defined = .true.
gas_material_eos_type(gas_index) = GAS_EOS_JWL
gas_material_eos_defined(gas_index) = .true.
gas_material_rho0(gas_index) = rho0
gas_material_jwl_a(gas_index) = a_value
gas_material_jwl_b(gas_index) = b_value
gas_material_jwl_r1(gas_index) = r1
gas_material_jwl_r2(gas_index) = r2
gas_material_jwl_omega(gas_index) = omega
gas_material_energy_default(gas_index) = e0
gas_material_density_default(gas_index) = sph_gas_material_density(material_name_in)
end subroutine sph_gas_set_jwl
subroutine sph_gas_set_stiffened(material_name_in, rho0, gamma, e0, pinf, ierr)
character(len=*), intent(in) :: material_name_in
real(rk), intent(in) :: rho0
real(rk), intent(in) :: gamma
real(rk), intent(in) :: e0
real(rk), intent(in) :: pinf
integer, intent(out) :: ierr
integer :: gas_index
ierr = SPH_SUCCESS
if (rho0 <= 0.0_rk .or. gamma <= 1.0_rk .or. e0 < 0.0_rk .or. &
pinf < 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Stiffened gas EOS parameters are invalid')
ierr = SPH_ERROR_ARGUMENT
return
end if
call sph_gas_prepare_eos_material(material_name_in, gas_index, ierr)
if (ierr /= SPH_SUCCESS) return
gas_material_seen = .true.
gas_material_name = material_name_in
gas_material_internal_id = sph_gas_find_material(material_name_in)
gas_eos_type = GAS_EOS_STIFFENED
gas_rho0 = rho0
gas_gamma = gamma
gas_e0 = e0
gas_pinf = pinf
gas_eos_defined = .true.
gas_material_eos_type(gas_index) = GAS_EOS_STIFFENED
gas_material_eos_defined(gas_index) = .true.
gas_material_rho0(gas_index) = rho0
gas_material_gamma(gas_index) = gamma
gas_material_energy_default(gas_index) = e0
gas_material_pinf(gas_index) = pinf
gas_material_density_default(gas_index) = sph_gas_material_density(material_name_in)
end subroutine sph_gas_set_stiffened
subroutine sph_gas_set_particle_control(h_value, mass_value, volume_value, ierr)
real(rk), intent(in) :: h_value
real(rk), intent(in) :: mass_value
real(rk), intent(in) :: volume_value
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (h_value <= 0.0_rk .or. mass_value <= 0.0_rk .or. volume_value <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas particle control values must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
gas_default_h = h_value
gas_default_mass = mass_value
gas_default_volume = volume_value
if (gas_material_count > 0) then
gas_material_default_h(1:gas_material_count) = h_value
gas_material_default_mass(1:gas_material_count) = mass_value
gas_material_default_volume(1:gas_material_count) = volume_value
end if
gas_particle_control_defined = .true.
end subroutine sph_gas_set_particle_control
subroutine sph_gas_set_body_force(force, ierr)
real(rk), intent(in) :: force(SPH_NDIM)
integer, intent(out) :: ierr
integer :: i
gas_body_force = force
do i = 1, npart
if (sph_gas_is_active_particle(i)) acc_body(:,i) = force
end do
ierr = SPH_SUCCESS
end subroutine sph_gas_set_body_force
subroutine sph_gas_validate_input(ierr)
integer, intent(out) :: ierr
integer :: gas_index
integer :: mat_index
ierr = SPH_SUCCESS
if (.not. gas_material_seen) return
do gas_index = 1, gas_material_count
mat_index = gas_material_ids(gas_index)
if (.not. gas_material_eos_defined(gas_index)) then
call sph_error_set(SPH_ERROR_INPUT, 'input is missing *SPHLITE EOS for Gas')
ierr = SPH_ERROR_INPUT
return
end if
if (mat_index < 1 .or. mat_index > material_count) then
call sph_error_set(SPH_ERROR_INPUT, 'Gas 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 Gas material')
ierr = SPH_ERROR_INPUT
return
end if
end do
end subroutine sph_gas_validate_input
logical function sph_gas_has_material_density()
integer :: i
sph_gas_has_material_density = .false.
do i = 1, material_count
if (trim(sph_gas_upper(material_name(i))) == trim(sph_gas_upper(gas_material_name))) then
sph_gas_has_material_density = material_density(i) > 0.0_rk
return
end if
end do
end function sph_gas_has_material_density
logical function sph_gas_has_section_binding_for_id(mat_id)
integer, intent(in) :: mat_id
integer :: i
sph_gas_has_section_binding_for_id = .false.
do i = 1, section_count
if (section_material_id(i) == mat_id) then
sph_gas_has_section_binding_for_id = .true.
return
end if
end do
end function sph_gas_has_section_binding_for_id
subroutine sph_gas_apply_initial_conditions(ierr)
integer, intent(out) :: ierr
integer :: i
integer :: gas_index
integer :: mat_index
real(rk) :: density_value
ierr = SPH_SUCCESS
if (.not. gas_material_seen) return
call sph_gas_validate_input(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_gas_allocate_sources(ierr)
if (ierr /= SPH_SUCCESS) return
do i = 1, npart
if (particle_status(i) /= SPH_STATUS_ACTIVE) cycle
if (.not. sph_gas_is_material_particle(i)) cycle
mat_index = material_id(i)
gas_index = sph_gas_table_index_by_material_id(mat_index)
if (gas_index <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Gas particle material has no EOS')
ierr = SPH_ERROR_INPUT
return
end if
density_value = material_density(mat_index)
if (density_value <= 0.0_rk) density_value = gas_material_density_default(gas_index)
particle_phase(i) = SPH_PHASE_GAS
rho(i) = density_value
internal_energy(i) = gas_material_energy_default(gas_index)
temperature(i) = gas_material_temperature_default(gas_index)
mass(i) = gas_material_default_mass(gas_index)
volume(i) = gas_material_default_volume(gas_index)
h(i) = gas_material_default_h(gas_index)
acc_body(:,i) = gas_body_force
call sph_gas_compute_pressure_for_material(mat_index, rho(i), &
internal_energy(i), pressure(i), ierr)
if (ierr /= SPH_SUCCESS) return
end do
call sph_gas_update_diagnostics(ierr)
end subroutine sph_gas_apply_initial_conditions
subroutine sph_gas_compute_pressure(rho_value, energy_value, pressure_value, ierr)
real(rk), intent(in) :: rho_value
real(rk), intent(in) :: energy_value
real(rk), intent(out) :: pressure_value
integer, intent(out) :: ierr
real(rk) :: relative_volume
ierr = SPH_SUCCESS
pressure_value = 0.0_rk
if (rho_value <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas density must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (energy_value < 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas internal energy must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (gas_eos_type == GAS_EOS_IDEAL) then
pressure_value = (gas_gamma - 1.0_rk) * rho_value * energy_value + gas_p0
else if (gas_eos_type == GAS_EOS_JWL) then
if (gas_rho0 <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'JWL gas reference density must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
relative_volume = gas_rho0 / rho_value
pressure_value = gas_jwl_a * exp(-gas_jwl_r1 * relative_volume) + &
gas_jwl_b * exp(-gas_jwl_r2 * relative_volume) + &
gas_jwl_omega * energy_value / relative_volume
else if (gas_eos_type == GAS_EOS_STIFFENED) then
pressure_value = (gas_gamma - 1.0_rk) * rho_value * energy_value - &
gas_gamma * gas_pinf
else
call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS is not defined')
ierr = SPH_ERROR_INPUT
return
end if
if (.not. sph_gas_is_finite(pressure_value)) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas pressure is not finite')
ierr = SPH_ERROR_ARGUMENT
end if
end subroutine sph_gas_compute_pressure
subroutine sph_gas_compute_pressure_for_material(mat_index, rho_value, &
energy_value, pressure_value, ierr)
integer, intent(in) :: mat_index
real(rk), intent(in) :: rho_value
real(rk), intent(in) :: energy_value
real(rk), intent(out) :: pressure_value
integer, intent(out) :: ierr
integer :: gas_index
real(rk) :: relative_volume
ierr = SPH_SUCCESS
pressure_value = 0.0_rk
if (rho_value <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas density must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (energy_value < 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas internal energy must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
gas_index = sph_gas_table_index_by_material_id(mat_index)
if (gas_index <= 0 .or. .not. gas_material_eos_defined(gas_index)) then
call sph_error_set(SPH_ERROR_INPUT, 'Gas particle material has no EOS')
ierr = SPH_ERROR_INPUT
return
end if
if (gas_material_eos_type(gas_index) == GAS_EOS_IDEAL) then
pressure_value = (gas_material_gamma(gas_index) - 1.0_rk) * &
rho_value * energy_value + gas_material_p0(gas_index)
else if (gas_material_eos_type(gas_index) == GAS_EOS_JWL) then
if (gas_material_rho0(gas_index) <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'JWL gas reference density must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
relative_volume = gas_material_rho0(gas_index) / rho_value
pressure_value = gas_material_jwl_a(gas_index) * &
exp(-gas_material_jwl_r1(gas_index) * relative_volume) + &
gas_material_jwl_b(gas_index) * &
exp(-gas_material_jwl_r2(gas_index) * relative_volume) + &
gas_material_jwl_omega(gas_index) * energy_value / relative_volume
else if (gas_material_eos_type(gas_index) == GAS_EOS_STIFFENED) then
pressure_value = (gas_material_gamma(gas_index) - 1.0_rk) * &
rho_value * energy_value - &
gas_material_gamma(gas_index) * gas_material_pinf(gas_index)
else
call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS is not defined')
ierr = SPH_ERROR_INPUT
return
end if
if (.not. sph_gas_is_finite(pressure_value)) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas pressure is not finite')
ierr = SPH_ERROR_ARGUMENT
end if
end subroutine sph_gas_compute_pressure_for_material
subroutine sph_gas_update_pressure(ierr)
integer, intent(out) :: ierr
integer :: i
ierr = SPH_SUCCESS
do i = 1, npart
if (.not. sph_gas_is_active_particle(i)) cycle
call sph_gas_compute_pressure_for_material(material_id(i), rho(i), &
internal_energy(i), pressure(i), ierr)
if (ierr /= SPH_SUCCESS) return
end do
end subroutine sph_gas_update_pressure
subroutine sph_gas_compute_density_rate(ierr)
integer, intent(out) :: ierr
integer :: i
integer :: n
integer :: j
integer :: index
real(rk) :: grad_w(SPH_NDIM)
real(rk) :: rij(SPH_NDIM)
real(rk) :: vij(SPH_NDIM)
ierr = SPH_SUCCESS
if (npart <= 0) return
do i = 1, npart
if (sph_gas_is_active_particle(i)) drho_dt(i) = 0.0_rk
end do
do i = 1, npart
if (.not. sph_gas_is_active_particle(i)) cycle
do n = 1, neighbor_count(i)
index = neighbor_offset(i) + n - 1
j = neighbor_list(index)
if (.not. sph_gas_is_active_particle(j)) cycle
rij = x(:,i) - x(:,j)
vij = v(:,i) - v(:,j)
call sph_kernel_gradient(SPH_KERNEL_DEFAULT, rij, h(i), grad_w, ierr)
if (ierr /= SPH_SUCCESS) return
drho_dt(i) = drho_dt(i) + mass(j) * sum(vij * grad_w)
end do
end do
end subroutine sph_gas_compute_density_rate
subroutine sph_gas_compute_pressure_acceleration(ierr)
integer, intent(out) :: ierr
integer :: i
integer :: n
integer :: j
integer :: index
real(rk) :: grad_w(SPH_NDIM)
real(rk) :: rij(SPH_NDIM)
real(rk) :: pressure_term
ierr = SPH_SUCCESS
if (npart <= 0) return
acc_pressure = 0.0_rk
do i = 1, npart
if (.not. sph_gas_is_active_particle(i)) cycle
if (rho(i) <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas density must be positive for pressure force')
ierr = SPH_ERROR_ARGUMENT
return
end if
do n = 1, neighbor_count(i)
index = neighbor_offset(i) + n - 1
j = neighbor_list(index)
if (.not. sph_gas_is_active_particle(j)) cycle
if (rho(j) <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas neighbor density must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
rij = x(:,i) - x(:,j)
call sph_kernel_gradient(SPH_KERNEL_DEFAULT, rij, h(i), grad_w, ierr)
if (ierr /= SPH_SUCCESS) return
pressure_term = pressure(i) / (rho(i) * rho(i)) + pressure(j) / (rho(j) * rho(j))
acc_pressure(:,i) = acc_pressure(:,i) - mass(j) * pressure_term * grad_w
end do
end do
end subroutine sph_gas_compute_pressure_acceleration
subroutine sph_gas_compute_acceleration(ierr)
integer, intent(out) :: ierr
integer :: i
call sph_gas_compute_pressure_acceleration(ierr)
if (ierr /= SPH_SUCCESS) return
do i = 1, npart
if (sph_gas_is_active_particle(i)) a(:,i) = acc_pressure(:,i) + acc_body(:,i)
end do
ierr = SPH_SUCCESS
end subroutine sph_gas_compute_acceleration
subroutine sph_gas_allocate_sources(ierr)
integer, intent(out) :: ierr
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_source_clear(ierr)
end subroutine sph_gas_clear_sources
subroutine sph_gas_add_source_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_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
call sph_gas_source_apply_explicit(dt, ierr)
if (ierr /= SPH_SUCCESS) return
call sph_gas_update_pressure(ierr)
end subroutine sph_gas_apply_sources_explicit
subroutine sph_gas_get_source_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_get_summary(mass_source, momentum_source, &
energy_source, ierr)
end subroutine sph_gas_get_source_summary
subroutine sph_gas_update_diagnostics(ierr)
integer, intent(out) :: ierr
integer :: i
logical :: first_gas
real(rk) :: pressure_integral
real(rk) :: current_center(SPH_NDIM)
real(rk) :: pi_value
ierr = SPH_SUCCESS
current_center = gas_center
call sph_db_clear_gas_diagnostics()
first_gas = .true.
pressure_integral = 0.0_rk
do i = 1, npart
if (.not. sph_gas_is_active_particle(i)) cycle
if (volume(i) <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'Gas volume must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
gas_particle_count = gas_particle_count + 1
gas_mass = gas_mass + mass(i)
gas_volume = gas_volume + volume(i)
gas_momentum = gas_momentum + mass(i) * v(:,i)
gas_center = gas_center + volume(i) * x(:,i)
pressure_integral = pressure_integral + volume(i) * pressure(i)
if (first_gas) then
gas_max_pressure = pressure(i)
gas_min_density = rho(i)
gas_max_density = rho(i)
first_gas = .false.
else
if (pressure(i) > gas_max_pressure) gas_max_pressure = pressure(i)
if (rho(i) < gas_min_density) gas_min_density = rho(i)
if (rho(i) > gas_max_density) gas_max_density = rho(i)
end if
end do
if (gas_volume > 0.0_rk) then
gas_center = gas_center / gas_volume
gas_mean_pressure = pressure_integral / gas_volume
pi_value = acos(-1.0_rk)
gas_equivalent_radius = (3.0_rk * gas_volume / (4.0_rk * pi_value))**(1.0_rk / 3.0_rk)
end if
if (gas_bubble_history_valid) then
gas_radius_rate = gas_equivalent_radius - gas_previous_equivalent_radius
gas_center_shift = sqrt(sum((gas_center - gas_previous_center) * &
(gas_center - gas_previous_center)))
else
gas_radius_rate = 0.0_rk
gas_center_shift = 0.0_rk
end if
gas_previous_equivalent_radius = gas_equivalent_radius
gas_previous_center = gas_center
gas_bubble_history_valid = gas_particle_count > 0
current_center = current_center
end subroutine sph_gas_update_diagnostics
subroutine sph_gas_estimate_dt_limits(dt_cfl, dt_accel, dt_limit, ierr)
real(rk), intent(out) :: dt_cfl
real(rk), intent(out) :: dt_accel
real(rk), intent(out) :: dt_limit
integer, intent(out) :: ierr
integer :: i
real(rk) :: speed
real(rk) :: sound_speed
real(rk) :: accel_norm
real(rk) :: local_dt
ierr = SPH_SUCCESS
dt_cfl = huge(1.0_rk)
dt_accel = huge(1.0_rk)
do i = 1, npart
if (.not. sph_gas_is_active_particle(i)) cycle
if (h(i) <= 0.0_rk .or. rho(i) <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'invalid Gas state for dt estimate')
ierr = SPH_ERROR_ARGUMENT
return
end if
sound_speed = sph_gas_sound_speed(i)
speed = sqrt(sum(v(:,i) * v(:,i)))
local_dt = h(i) / (sound_speed + speed)
if (local_dt < dt_cfl) dt_cfl = local_dt
accel_norm = sqrt(sum(a(:,i) * a(:,i)))
if (accel_norm > 0.0_rk) then
local_dt = sqrt(h(i) / accel_norm)
if (local_dt < dt_accel) dt_accel = local_dt
end if
end do
if (dt_cfl == huge(1.0_rk)) dt_cfl = 0.0_rk
if (dt_accel == huge(1.0_rk)) dt_accel = dt_cfl
dt_limit = dt_cfl
if (dt_accel > 0.0_rk .and. dt_accel < dt_limit) dt_limit = dt_accel
gas_dt_cfl = dt_cfl
gas_dt_accel = dt_accel
gas_dtcrit = dt_limit
end subroutine sph_gas_estimate_dt_limits
subroutine sph_gas_get_summary(volume_out, center_out, mean_pressure_out, &
max_pressure_out, ierr)
real(rk), intent(out) :: volume_out
real(rk), intent(out) :: center_out(SPH_NDIM)
real(rk), intent(out) :: mean_pressure_out
real(rk), intent(out) :: max_pressure_out
integer, intent(out) :: ierr
call sph_gas_update_diagnostics(ierr)
if (ierr /= SPH_SUCCESS) return
volume_out = gas_volume
center_out = gas_center
mean_pressure_out = gas_mean_pressure
max_pressure_out = gas_max_pressure
end subroutine sph_gas_get_summary
subroutine sph_gas_get_extended_summary(mass_out, momentum_out, &
density_range_out, dt_limits_out, ierr)
real(rk), intent(out) :: mass_out
real(rk), intent(out) :: momentum_out(SPH_NDIM)
real(rk), intent(out) :: density_range_out(2)
real(rk), intent(out) :: dt_limits_out(3)
integer, intent(out) :: ierr
call sph_gas_update_diagnostics(ierr)
if (ierr /= SPH_SUCCESS) return
call sph_gas_estimate_dt_limits(dt_limits_out(1), dt_limits_out(2), &
dt_limits_out(3), ierr)
if (ierr /= SPH_SUCCESS) return
mass_out = gas_mass
momentum_out = gas_momentum
density_range_out(1) = gas_min_density
density_range_out(2) = gas_max_density
end subroutine sph_gas_get_extended_summary
subroutine sph_gas_get_bubble_metrics(equivalent_radius_out, radius_rate_out, &
center_out, ierr)
real(rk), intent(out) :: equivalent_radius_out
real(rk), intent(out) :: radius_rate_out
real(rk), intent(out) :: center_out(SPH_NDIM)
integer, intent(out) :: ierr
equivalent_radius_out = gas_equivalent_radius
radius_rate_out = gas_radius_rate
center_out = gas_center
ierr = SPH_SUCCESS
end subroutine sph_gas_get_bubble_metrics
real(rk) function sph_gas_sound_speed(i)
integer, intent(in) :: i
integer :: gas_index
real(rk) :: pressure_scale
gas_index = sph_gas_table_index_by_material_id(material_id(i))
if (gas_index > 0) then
pressure_scale = abs(pressure(i)) + abs(gas_material_pinf(gas_index)) + 1.0_rk
else
pressure_scale = abs(pressure(i)) + abs(gas_pinf) + 1.0_rk
end if
sph_gas_sound_speed = sqrt(max(pressure_scale / rho(i), 1.0e-12_rk))
end function sph_gas_sound_speed
logical function sph_gas_is_active_particle(i)
integer, intent(in) :: i
sph_gas_is_active_particle = .false.
if (i < 1 .or. i > npart) return
if (particle_phase(i) /= SPH_PHASE_GAS) return
if (particle_status(i) /= SPH_STATUS_ACTIVE) return
sph_gas_is_active_particle = .true.
end function sph_gas_is_active_particle
logical function sph_gas_is_material_particle(i)
integer, intent(in) :: i
integer :: mat_index
sph_gas_is_material_particle = .false.
if (i < 1 .or. i > npart) return
mat_index = material_id(i)
if (mat_index < 1 .or. mat_index > material_count) return
if (index(sph_gas_upper(material_type(mat_index)), 'GAS') <= 0) return
sph_gas_is_material_particle = .true.
end function sph_gas_is_material_particle
integer function sph_gas_find_material(name)
character(len=*), intent(in) :: name
integer :: i
sph_gas_find_material = 0
do i = 1, material_count
if (trim(sph_gas_upper(material_name(i))) == trim(sph_gas_upper(name))) then
sph_gas_find_material = i
return
end if
end do
end function sph_gas_find_material
real(rk) function sph_gas_material_density(name)
character(len=*), intent(in) :: name
integer :: i
sph_gas_material_density = gas_rho0
do i = 1, material_count
if (trim(sph_gas_upper(material_name(i))) == trim(sph_gas_upper(name))) then
if (material_density(i) > 0.0_rk) sph_gas_material_density = material_density(i)
return
end if
end do
end function sph_gas_material_density
subroutine sph_gas_prepare_eos_material(name, gas_index, ierr)
character(len=*), intent(in) :: name
integer, intent(out) :: gas_index
integer, intent(out) :: ierr
integer :: mat_index
ierr = SPH_SUCCESS
gas_index = 0
mat_index = sph_gas_find_material(name)
if (mat_index <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS references unknown material')
ierr = SPH_ERROR_INPUT
return
end if
if (index(sph_gas_upper(material_type(mat_index)), 'GAS') <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'Gas EOS references non-Gas material')
ierr = SPH_ERROR_INPUT
return
end if
gas_index = sph_gas_register_material(name)
if (gas_index <= 0) then
call sph_error_set(SPH_ERROR_INPUT, 'too many Gas material records')
ierr = SPH_ERROR_INPUT
return
end if
if (gas_material_eos_defined(gas_index)) then
call sph_error_set(SPH_ERROR_INPUT, 'Gas material EOS is already defined')
ierr = SPH_ERROR_INPUT
end if
end subroutine sph_gas_prepare_eos_material
integer function sph_gas_register_material(name)
character(len=*), intent(in) :: name
integer :: mat_index
integer :: gas_index
sph_gas_register_material = 0
mat_index = sph_gas_find_material(name)
if (mat_index <= 0) return
gas_index = sph_gas_table_index_by_material_id(mat_index)
if (gas_index > 0) then
sph_gas_register_material = gas_index
return
end if
if (gas_material_count >= SPH_MAX_MATERIALS) return
gas_material_count = gas_material_count + 1
gas_material_ids(gas_material_count) = mat_index
gas_material_names(gas_material_count) = material_name(mat_index)
gas_material_eos_defined(gas_material_count) = .false.
gas_material_eos_type(gas_material_count) = GAS_EOS_NONE
gas_material_density_default(gas_material_count) = material_density(mat_index)
gas_material_energy_default(gas_material_count) = gas_e0
gas_material_temperature_default(gas_material_count) = 293.15_rk
gas_material_rho0(gas_material_count) = gas_rho0
gas_material_gamma(gas_material_count) = gas_gamma
gas_material_p0(gas_material_count) = gas_p0
gas_material_default_h(gas_material_count) = gas_default_h
gas_material_default_mass(gas_material_count) = gas_default_mass
gas_material_default_volume(gas_material_count) = gas_default_volume
sph_gas_register_material = gas_material_count
end function sph_gas_register_material
integer function sph_gas_table_index_by_material_id(mat_index)
integer, intent(in) :: mat_index
integer :: i
sph_gas_table_index_by_material_id = 0
do i = 1, gas_material_count
if (gas_material_ids(i) == mat_index) then
sph_gas_table_index_by_material_id = i
return
end if
end do
end function sph_gas_table_index_by_material_id
subroutine sph_gas_count_unbound_materials(count)
integer, intent(out) :: count
integer :: i
count = 0
do i = 1, gas_material_count
if (.not. sph_gas_has_section_binding_for_id(gas_material_ids(i))) then
count = count + 1
end if
end do
end subroutine sph_gas_count_unbound_materials
subroutine sph_gas_rebuild_legacy_restart_state(ierr)
integer, intent(out) :: ierr
integer :: i
integer :: gas_index
ierr = SPH_SUCCESS
call sph_gas_clear_material_table()
do i = 1, npart
if (particle_phase(i) /= SPH_PHASE_GAS) cycle
if (particle_status(i) /= SPH_STATUS_ACTIVE) cycle
if (gas_material_count >= SPH_MAX_MATERIALS) exit
gas_material_count = 1
gas_material_ids(1) = material_id(i)
gas_material_names(1) = gas_material_name
gas_material_eos_defined(1) = gas_eos_type /= GAS_EOS_NONE
gas_material_eos_type(1) = gas_eos_type
gas_material_density_default(1) = rho(i)
gas_material_energy_default(1) = gas_e0
gas_material_temperature_default(1) = temperature(i)
gas_material_rho0(1) = gas_rho0
gas_material_gamma(1) = gas_gamma
gas_material_p0(1) = gas_p0
gas_material_jwl_a(1) = gas_jwl_a
gas_material_jwl_b(1) = gas_jwl_b
gas_material_jwl_r1(1) = gas_jwl_r1
gas_material_jwl_r2(1) = gas_jwl_r2
gas_material_jwl_omega(1) = gas_jwl_omega
gas_material_pinf(1) = gas_pinf
gas_material_default_h(1) = gas_default_h
gas_material_default_mass(1) = gas_default_mass
gas_material_default_volume(1) = gas_default_volume
exit
end do
gas_index = gas_material_count
if (gas_index > 0) then
gas_material_seen = .true.
gas_eos_defined = gas_material_eos_defined(1)
gas_material_internal_id = gas_material_ids(1)
end if
call sph_gas_clear_sources(ierr)
end subroutine sph_gas_rebuild_legacy_restart_state
subroutine sph_gas_clear_material_table()
gas_material_count = 0
gas_material_ids = 0
gas_material_names = ' '
gas_material_eos_defined = .false.
gas_material_eos_type = GAS_EOS_NONE
gas_material_density_default = 0.0_rk
gas_material_energy_default = 0.0_rk
gas_material_temperature_default = 293.15_rk
gas_material_rho0 = 1.0_rk
gas_material_gamma = 1.4_rk
gas_material_p0 = 0.0_rk
gas_material_jwl_a = 0.0_rk
gas_material_jwl_b = 0.0_rk
gas_material_jwl_r1 = 0.0_rk
gas_material_jwl_r2 = 0.0_rk
gas_material_jwl_omega = 0.0_rk
gas_material_pinf = 0.0_rk
gas_material_default_h = 1.0_rk
gas_material_default_mass = 1.0_rk
gas_material_default_volume = 1.0_rk
end subroutine sph_gas_clear_material_table
logical function sph_gas_is_finite(value)
real(rk), intent(in) :: value
sph_gas_is_finite = value == value .and. abs(value) < huge(value)
end function sph_gas_is_finite
logical function sph_gas_vector_is_finite(values)
real(rk), intent(in) :: values(SPH_NDIM)
integer :: i
sph_gas_vector_is_finite = .true.
do i = 1, SPH_NDIM
if (.not. sph_gas_is_finite(values(i))) sph_gas_vector_is_finite = .false.
end do
end function sph_gas_vector_is_finite
function sph_gas_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_gas_upper
end module sph_gas