Files
FemLite/tests/module/test_particle_cloud_metrics.f90
T
2026-06-26 00:48:57 +08:00

122 lines
4.1 KiB
Fortran

program test_particle_cloud_metrics
use fem_precision
use fem_error
use fem_particle_state
use fem_particle_cloud
use fem_particle_cloud_metrics
implicit none
real(rk), parameter :: TOL = 1.0e-10_rk
call check_profile_metrics()
call check_error_paths()
contains
subroutine check_profile_metrics()
type(particle_cloud_spec) :: spec
type(particle_cloud_stats) :: stats
type(particle_cloud_profile) :: profile
integer :: ierr
integer :: count
integer :: particle_num
integer :: mass_count(4)
real(rk) :: mass_bins(4)
real(rk) :: center(3)
real(rk) :: total_mass
real(rk) :: max_speed
real(rk) :: mean_speed
real(rk) :: kinetic
real(rk) :: occupied
real(rk) :: fraction
call particle_reset()
call pcl_spec_init(1, (/ ZERO, ZERO, ZERO /), &
(/ 2.0_rk, 2.0_rk, 2.0_rk /), 0.5_rk, ONE, ONE, &
(/ ONE, ZERO, ZERO /), spec, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(10)
spec%stagger_layers = .false.
call pcl_fill_box(spec, stats, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(11)
call pcm_center_mass(center, total_mass, particle_num, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(12)
if (particle_num .ne. 8) call fail(13)
call check_vec(center, (/ ONE, ONE, ONE /), 14)
call check_close(total_mass, stats%total_mass, 15)
call pcm_speed_stats(max_speed, mean_speed, kinetic, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(16)
call check_close(max_speed, ONE, 17)
call check_close(mean_speed, ONE, 18)
call check_close(kinetic, HALF * stats%total_mass, 19)
call pcm_height_profile(ZERO, 2.0_rk, 4, mass_bins, mass_count, profile, &
ierr)
if (ierr .ne. FEM_SUCCESS) call fail(20)
if (mass_count(2) .ne. 4 .or. mass_count(4) .ne. 4) call fail(21)
if (mass_count(1) .ne. 0 .or. mass_count(3) .ne. 0) call fail(22)
call pcm_profile_values(profile, count, total_mass, center, occupied, &
kinetic, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(23)
if (count .ne. 8) call fail(24)
call check_vec(center, (/ ONE, ONE, ONE /), 25)
call check_close(total_mass, stats%total_mass, 26)
call check_close(occupied, 1.5_rk, 27)
call check_close(kinetic, HALF * stats%total_mass, 28)
if (profile%max_bin_mass .le. ZERO) call fail(29)
if (profile%min_bin_mass .le. ZERO) call fail(30)
call pcm_pack_fraction(8.0_rk, 2.0_rk, fraction, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(31)
call check_close(fraction, 0.25_rk, 32)
call pcm_validate_profile(profile, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(33)
end subroutine check_profile_metrics
subroutine check_error_paths()
type(particle_cloud_profile) :: profile
integer :: ierr
integer :: count_bins(2)
real(rk) :: mass_bins(2)
real(rk) :: fraction
mass_bins = ZERO
count_bins = 0
call pcm_height_profile(ONE, ZERO, 2, mass_bins, count_bins, profile, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(60)
call pcm_pack_fraction(ZERO, ONE, fraction, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(61)
call pcm_pack_fraction(ONE, -ONE, fraction, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(62)
call pcm_profile_reset(profile)
profile%particle_count = -1
call pcm_validate_profile(profile, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(63)
end subroutine check_error_paths
subroutine check_close(actual, expected, code)
real(rk), intent(in) :: actual
real(rk), intent(in) :: expected
integer, intent(in) :: code
if (abs(actual - expected) .gt. TOL * max(ONE, abs(expected))) call fail(code)
end subroutine check_close
subroutine check_vec(actual, expected, code)
real(rk), intent(in) :: actual(3)
real(rk), intent(in) :: expected(3)
integer, intent(in) :: code
if (maxval(abs(actual - expected)) .gt. TOL) call fail(code)
end subroutine check_vec
subroutine fail(code)
integer, intent(in) :: code
write(*,*) 'test_particle_cloud_metrics failed:', code
stop 1
end subroutine fail
end program test_particle_cloud_metrics