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
+8
View File
@@ -31,9 +31,17 @@ contains
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_center_z')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_mean_pressure')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_max_pressure')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_momentum_x')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_min_density')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_max_density')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_equivalent_radius')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_radius_rate')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_dt_cfl')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_dt_accel')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_dtcrit')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_source_mass')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_source_energy')
call assert_summary_metric('build-codex-unix/sphlite_00000030_summary.csv', 'gas_source_momentum_x')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for Gas output')
end subroutine check_gas_summary_output
+102
View File
@@ -0,0 +1,102 @@
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
+77
View File
@@ -0,0 +1,77 @@
program test_gas_source_errors
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_gas, only: sph_gas_add_source_by_particle
implicit none
integer :: ierr
call check_invalid_particle()
call check_invalid_value()
call check_non_gas_particle()
contains
subroutine check_invalid_particle()
real(rk) :: zero_momentum(3)
zero_momentum = 0.0_rk
call build_gas_case()
call sph_gas_add_source_by_particle(0, 1.0_rk, zero_momentum, 1.0_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('invalid Gas source particle did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after invalid particle')
end subroutine check_invalid_particle
subroutine check_invalid_value()
real(rk) :: zero_momentum(3)
zero_momentum = 0.0_rk
call build_gas_case()
call sph_gas_add_source_by_particle(1, huge(1.0_rk), zero_momentum, 1.0_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('invalid Gas source value did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after invalid value')
end subroutine check_invalid_value
subroutine check_non_gas_particle()
real(rk) :: zero_momentum(3)
zero_momentum = 0.0_rk
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for non-Gas source')
call sph_read_inp('tests/data/water_minimal_021.inp', ierr)
call assert_ok(ierr, 'read failed for non-Gas source')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for non-Gas source')
call sph_gas_add_source_by_particle(1, 1.0_rk, zero_momentum, 1.0_rk, ierr)
if (ierr == SPH_SUCCESS) call fail('non-Gas source target did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after non-Gas source')
end subroutine check_non_gas_particle
subroutine build_gas_case()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for Gas source error')
call sph_read_inp('tests/data/gas_air_ideal_031.inp', ierr)
call assert_ok(ierr, 'read failed for Gas source error')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for Gas source error')
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 fail(message)
character(len=*), intent(in) :: message
write(*, '(A)') trim(message)
stop 1
end subroutine fail
end program test_gas_source_errors