Files
SPHLite/tests/module/test_gas_summary.f90
2026-07-03 09:32:34 +08:00

123 lines
5.2 KiB
Fortran

program test_gas_summary
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: particle_status, volume, 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_center_shift, &
SPH_STATUS_INACTIVE, SPH_STATUS_FAILED
use sph_gas, only: sph_gas_update_diagnostics
implicit none
integer :: ierr
call check_summary_values()
call check_inactive_and_failed_skip()
call check_bubble_metrics_history()
call check_empty_gas_defaults()
call check_zero_volume_error()
contains
subroutine build_gas_case()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for Gas summary')
call sph_read_inp('tests/data/gas_ideal_minimal_030.inp', ierr)
call assert_ok(ierr, 'Gas summary input read failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'Gas summary build failed')
end subroutine build_gas_case
subroutine check_summary_values()
real(rk) :: expected_radius
real(rk) :: pi_value
call build_gas_case()
pi_value = acos(-1.0_rk)
expected_radius = (3.0_rk * 0.032_rk / (4.0_rk * pi_value))**(1.0_rk / 3.0_rk)
if (gas_particle_count /= 4) call fail('Gas summary particle count mismatch')
if (abs(gas_mass - 0.04_rk) > 1.0e-12_rk) call fail('Gas summary mass mismatch')
if (abs(gas_volume - 0.032_rk) > 1.0e-12_rk) call fail('Gas summary volume mismatch')
if (maxval(abs(gas_momentum)) > 1.0e-12_rk) call fail('Gas summary momentum mismatch')
if (maxval(abs(gas_center - (/0.025_rk, 0.025_rk, 0.025_rk/))) > 1.0e-12_rk) then
call fail('Gas summary center mismatch')
end if
if (abs(gas_mean_pressure - 0.96_rk) > 1.0e-12_rk) call fail('Gas mean pressure mismatch')
if (abs(gas_max_pressure - 0.96_rk) > 1.0e-12_rk) call fail('Gas max pressure mismatch')
if (abs(gas_min_density - 1.2_rk) > 1.0e-12_rk) call fail('Gas min density mismatch')
if (abs(gas_max_density - 1.2_rk) > 1.0e-12_rk) call fail('Gas max density mismatch')
if (abs(gas_equivalent_radius - expected_radius) > 1.0e-12_rk) then
call fail('Gas equivalent radius mismatch')
end if
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for Gas summary values')
end subroutine check_summary_values
subroutine check_inactive_and_failed_skip()
call build_gas_case()
particle_status(2) = SPH_STATUS_INACTIVE
particle_status(3) = SPH_STATUS_FAILED
call sph_gas_update_diagnostics(ierr)
call assert_ok(ierr, 'Gas summary skip update failed')
if (gas_particle_count /= 2) call fail('inactive or failed Gas particles were counted')
if (abs(gas_mass - 0.02_rk) > 1.0e-12_rk) call fail('Gas skip mass mismatch')
if (abs(gas_volume - 0.016_rk) > 1.0e-12_rk) call fail('Gas skip volume mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for Gas skip summary')
end subroutine check_inactive_and_failed_skip
subroutine check_bubble_metrics_history()
real(rk) :: old_radius
call build_gas_case()
old_radius = gas_equivalent_radius
if (gas_previous_equivalent_radius <= 0.0_rk) call fail('Gas previous radius was not saved')
volume(1) = 0.016_rk
call sph_gas_update_diagnostics(ierr)
call assert_ok(ierr, 'Gas bubble history update failed')
if (gas_equivalent_radius <= old_radius) call fail('Gas equivalent radius did not increase')
if (gas_radius_rate <= 0.0_rk) call fail('Gas radius rate mismatch')
if (gas_center_shift <= 0.0_rk) call fail('Gas center shift mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for Gas bubble history')
end subroutine check_bubble_metrics_history
subroutine check_empty_gas_defaults()
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for empty Gas defaults')
call sph_gas_update_diagnostics(ierr)
call assert_ok(ierr, 'empty Gas diagnostics failed')
if (gas_particle_count /= 0) call fail('empty Gas count mismatch')
if (gas_equivalent_radius /= 0.0_rk) call fail('empty Gas radius mismatch')
if (gas_radius_rate /= 0.0_rk) call fail('empty Gas radius rate mismatch')
if (gas_center_shift /= 0.0_rk) call fail('empty Gas center shift mismatch')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for empty Gas defaults')
end subroutine check_empty_gas_defaults
subroutine check_zero_volume_error()
call build_gas_case()
volume(1) = 0.0_rk
call sph_gas_update_diagnostics(ierr)
if (ierr == SPH_SUCCESS) call fail('zero Gas volume did not fail')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for zero Gas volume')
end subroutine check_zero_volume_error
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_summary