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

169 lines
6.0 KiB
Fortran

program test_particle_cloud
use fem_precision
use fem_error
use fem_particle_state
use fem_particle_step
use fem_particle_cloud
implicit none
real(rk), parameter :: TOL = 1.0e-10_rk
call check_cloud_fill()
call check_walls_and_velocity()
call check_error_paths()
contains
subroutine check_cloud_fill()
type(particle_cloud_spec) :: spec
type(particle_cloud_stats) :: stats
integer :: nx
integer :: ny
integer :: nz
integer :: count
integer :: ierr
integer :: out_count
real(rk) :: mass
real(rk) :: sphere_mass
real(rk) :: porosity
real(rk) :: volume
call particle_reset()
call pcl_spec_init(100, (/ ZERO, ZERO, ZERO /), &
(/ 2.0_rk, 2.0_rk, 2.0_rk /), 0.5_rk, &
2.0_rk, ONE, (/ ZERO, ZERO, ZERO /), spec, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(10)
spec%stagger_layers = .false.
call pcl_count_box(spec, nx, ny, nz, count, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(11)
if (nx .ne. 2 .or. ny .ne. 2 .or. nz .ne. 2) call fail(12)
if (count .ne. 8) call fail(13)
call pcl_fill_box(spec, stats, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(14)
if (stats%generated_count .ne. 8) call fail(15)
if (stats%first_slot .ne. 1 .or. stats%last_slot .ne. 8) call fail(16)
if (particle_count() .ne. 8) call fail(17)
call check_vec(stats%min_corner, (/ ZERO, ZERO, ZERO /), 18)
call check_vec(stats%max_corner, (/ 2.0_rk, 2.0_rk, 2.0_rk /), 19)
call pcl_particle_mass(0.5_rk, 2.0_rk, sphere_mass, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(20)
call check_close(stats%total_mass, 8.0_rk * sphere_mass, 21)
call pcl_box_volume(spec%min_corner, spec%max_corner, volume, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(22)
call check_close(volume, 8.0_rk, 23)
call pcl_stats_summary(stats, out_count, mass, porosity, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(24)
if (out_count .ne. 8) call fail(25)
call check_close(mass, stats%total_mass, 26)
if (porosity .lt. ZERO .or. porosity .gt. ONE) call fail(27)
end subroutine check_cloud_fill
subroutine check_walls_and_velocity()
type(particle_cloud_stats) :: stats
type(particle_wall_def) :: walls(6)
integer :: nwall
integer :: ierr
integer :: ext_id
logical :: active
real(rk) :: position(3)
real(rk) :: velocity(3)
real(rk) :: radius
real(rk) :: mass
real(rk) :: force(3)
call pcl_cloud_stats(1, particle_count(), stats, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(40)
if (stats%generated_count .ne. 8) call fail(41)
call pcl_container_walls((/ ZERO, ZERO, ZERO /), &
(/ 2.0_rk, 2.0_rk, 2.0_rk /), &
walls, nwall, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(42)
if (nwall .ne. 6) call fail(43)
call check_vec(walls(1)%normal, (/ ONE, ZERO, ZERO /), 44)
call check_vec(walls(2)%normal, (/ -ONE, ZERO, ZERO /), 45)
call check_vec(walls(5)%normal, (/ ZERO, ZERO, ONE /), 46)
call check_vec(walls(6)%normal, (/ ZERO, ZERO, -ONE /), 47)
call pcl_set_linear_velocity(1, particle_count(), (/ ONE, 2.0_rk, ZERO /), &
ierr)
if (ierr .ne. FEM_SUCCESS) call fail(48)
call particle_get(1, ext_id, active, position, velocity, radius, mass, &
force, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(49)
call check_vec(velocity, (/ ONE, 2.0_rk, ZERO /), 50)
call pcl_set_spin_velocity(1, 1, (/ ONE, ONE, ONE /), &
(/ ZERO, ZERO, 2.0_rk /), ierr)
if (ierr .ne. FEM_SUCCESS) call fail(51)
call particle_get(1, ext_id, active, position, velocity, radius, mass, &
force, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(52)
call check_vec(velocity, (/ ONE, -ONE, ZERO /), 53)
end subroutine check_walls_and_velocity
subroutine check_error_paths()
type(particle_cloud_spec) :: spec
type(particle_cloud_stats) :: stats
type(particle_wall_def) :: walls(6)
integer :: nwall
integer :: ierr
integer :: nx
integer :: ny
integer :: nz
integer :: count
real(rk) :: mass
call pcl_spec_init(0, (/ ZERO, ZERO, ZERO /), (/ ONE, ONE, ONE /), &
0.25_rk, ONE, ONE, (/ ZERO, ZERO, ZERO /), spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(80)
call pcl_spec_init(1, (/ ZERO, ZERO, ZERO /), (/ ONE, ONE, ONE /), &
ZERO, ONE, ONE, (/ ZERO, ZERO, ZERO /), spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(81)
call pcl_particle_mass(ZERO, ONE, mass, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(82)
call pcl_box_volume((/ ONE, ZERO, ZERO /), (/ ZERO, ONE, ONE /), &
mass, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(83)
call pcl_container_walls((/ ZERO, ZERO, ZERO /), &
(/ ZERO, ONE, ONE /), walls, nwall, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(84)
call pcl_cloud_stats(9, 1, stats, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(85)
spec%first_id = 1
spec%min_corner = (/ ZERO, ZERO, ZERO /)
spec%max_corner = (/ ONE, ONE, ONE /)
spec%radius = 0.75_rk
spec%density = ONE
spec%spacing_scale = ONE
spec%velocity = ZERO
call pcl_count_box(spec, nx, ny, nz, count, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(86)
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 failed:', code
stop 1
end subroutine fail
end program test_particle_cloud