Add Gas source placeholders

This commit is contained in:
Codex
2026-07-03 10:52:40 +08:00
parent 834b9945e7
commit c834006eaf
8 changed files with 404 additions and 23 deletions
+157 -1
View File
@@ -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_set
SPH_ERROR_DATABASE, 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, &
@@ -63,6 +63,12 @@ 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
@@ -118,6 +124,12 @@ 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)
gas_previous_equivalent_radius = 0.0_rk
gas_previous_center = 0.0_rk
call sph_db_clear_gas_diagnostics()
@@ -397,6 +409,8 @@ contains
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
@@ -620,6 +634,138 @@ contains
ierr = SPH_SUCCESS
end subroutine sph_gas_compute_acceleration
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
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
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_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()
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)
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, &
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_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
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
@@ -901,6 +1047,16 @@ contains
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