Files
SPHLite/tests/module/test_gas_source.f90
T

103 lines
4.1 KiB
Fortran
Raw Normal View History

2026-07-03 10:52:40 +08:00
program test_gas_source
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
use sph_database, only: mass, v, internal_energy, pressure
use sph_gas, only: sph_gas_clear_sources, sph_gas_add_source_by_particle, &
sph_gas_apply_sources_explicit, sph_gas_get_source_summary
implicit none
integer :: ierr
call check_source_accumulate_and_clear()
call check_source_explicit_apply()
contains
subroutine check_source_accumulate_and_clear()
real(rk) :: momentum_source(3)
real(rk) :: mass_source
real(rk) :: energy_source
real(rk) :: add_momentum(3)
call build_gas_case()
call sph_gas_clear_sources(ierr)
call assert_ok(ierr, 'Gas source clear failed')
add_momentum = (/3.0_rk, 4.0_rk, 5.0_rk/)
call sph_gas_add_source_by_particle(1, 2.0_rk, add_momentum, 7.0_rk, ierr)
call assert_ok(ierr, 'Gas source add failed')
call sph_gas_get_source_summary(mass_source, momentum_source, energy_source, ierr)
call assert_ok(ierr, 'Gas source summary failed')
call assert_close(mass_source, 2.0_rk, 'Gas mass source summary mismatch')
call assert_close(energy_source, 7.0_rk, 'Gas energy source summary mismatch')
call assert_close(momentum_source(2), 4.0_rk, 'Gas momentum source summary mismatch')
call sph_gas_clear_sources(ierr)
call assert_ok(ierr, 'Gas source second clear failed')
call sph_gas_get_source_summary(mass_source, momentum_source, energy_source, ierr)
call assert_ok(ierr, 'Gas source zero summary failed')
call assert_close(mass_source, 0.0_rk, 'Gas mass source clear mismatch')
call assert_close(energy_source, 0.0_rk, 'Gas energy source clear mismatch')
call assert_close(maxval(abs(momentum_source)), 0.0_rk, 'Gas momentum source clear mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after source clear')
end subroutine check_source_accumulate_and_clear
subroutine check_source_explicit_apply()
real(rk) :: add_momentum(3)
real(rk) :: mass_before
real(rk) :: energy_before
real(rk) :: pressure_before
call build_gas_case()
mass_before = mass(1)
energy_before = internal_energy(1)
pressure_before = pressure(1)
add_momentum = (/0.5_rk, 0.0_rk, 0.0_rk/)
call sph_gas_clear_sources(ierr)
call assert_ok(ierr, 'Gas source clear before apply failed')
call sph_gas_add_source_by_particle(1, 1.0_rk, add_momentum, 2.0_rk, ierr)
call assert_ok(ierr, 'Gas source add before apply failed')
call sph_gas_apply_sources_explicit(0.1_rk, ierr)
call assert_ok(ierr, 'Gas source explicit apply failed')
if (mass(1) <= mass_before) call fail('Gas source did not increase mass')
if (v(1,1) <= 0.0_rk) call fail('Gas source did not change velocity')
if (internal_energy(1) <= energy_before) call fail('Gas source did not increase energy')
if (pressure(1) <= pressure_before) call fail('Gas source did not update pressure')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after source apply')
end subroutine check_source_explicit_apply
subroutine build_gas_case()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for Gas source')
call sph_read_inp('tests/data/gas_air_ideal_031.inp', ierr)
call assert_ok(ierr, 'read failed for Gas source')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for Gas source')
end subroutine build_gas_case
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 assert_close(actual, expected, message)
real(rk), intent(in) :: actual
real(rk), intent(in) :: expected
character(len=*), intent(in) :: message
if (abs(actual - expected) > 1.0e-12_rk) call fail(message)
end subroutine assert_close
subroutine fail(message)
character(len=*), intent(in) :: message
write(*, '(A)') trim(message)
stop 1
end subroutine fail
end program test_gas_source