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

236 lines
8.3 KiB
Fortran

program test_particle_step_binned
use fem_precision
use fem_error
use fem_particle_state
use fem_particle_contact
use fem_particle_step
use fem_particle_step_binned
implicit none
real(rk), parameter :: TOL = 1.0e-10_rk
call check_binned_matches_naive()
call check_binned_wall_step()
call check_build_pairs_and_errors()
contains
subroutine check_binned_matches_naive()
type(particle_contact_params) :: params
type(particle_step_ctrl) :: ctrl
type(particle_step_diag) :: naive
type(particle_bin_step_diag) :: binned
type(particle_wall_def) :: walls(1)
real(rk) :: pos_naive(3,2)
real(rk) :: vel_naive(3,2)
real(rk) :: force_naive(3,2)
real(rk) :: pos_bin(3,2)
real(rk) :: vel_bin(3,2)
real(rk) :: force_bin(3,2)
integer :: ierr
integer :: contacts
integer :: raw_pairs
integer :: filt_pairs
real(rk) :: max_overlap
real(rk) :: total_energy
call particle_contact_init(100.0_rk, ZERO, ZERO, ZERO, 100.0_rk, ZERO, &
params, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(10)
call pstep_ctrl_init(0.10_rk, ZERO, (/ ZERO, ZERO, ZERO /), ZERO, &
ctrl, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(11)
walls(1)%active = .false.
call setup_pair(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(12)
call pstep_advance(ctrl, params, walls, 1, naive, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(13)
call capture_two(pos_naive, vel_naive, force_naive, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(14)
call setup_pair(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(15)
call pbstep_advance(ctrl, params, walls, 1, 1.10_rk, binned, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(16)
call capture_two(pos_bin, vel_bin, force_bin, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(17)
if (binned%step%active_count .ne. naive%active_count) call fail(18)
if (binned%step%pair_count .ne. naive%pair_count) call fail(19)
if (binned%filtered_pair_count .ne. naive%candidate_count) call fail(20)
if (binned%raw_pair_count .lt. binned%filtered_pair_count) call fail(21)
call check_close(binned%step%max_overlap, naive%max_overlap, 22)
call check_close(binned%step%elastic_energy, naive%elastic_energy, 23)
call check_close(binned%step%kinetic_after, naive%kinetic_after, 24)
call check_mat(pos_bin, pos_naive, 25)
call check_mat(vel_bin, vel_naive, 26)
call check_mat(force_bin, force_naive, 27)
call pbstep_summary(binned, contacts, raw_pairs, filt_pairs, max_overlap, &
total_energy, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(28)
if (contacts .ne. 1 .or. raw_pairs .lt. 1 .or. filt_pairs .ne. 1) then
call fail(29)
end if
call check_close(max_overlap, 0.20_rk, 30)
call check_close(total_energy, 6.0_rk, 31)
end subroutine check_binned_matches_naive
subroutine check_binned_wall_step()
type(particle_contact_params) :: params
type(particle_step_ctrl) :: ctrl
type(particle_wall_def) :: walls(1)
type(particle_bin_step_diag) :: diag
integer :: slot
integer :: ierr
integer :: dims(3)
integer :: ncell
integer :: raw_pairs
integer :: max_occ
integer :: empty_count
integer :: ext_id
logical :: active
real(rk) :: position(3)
real(rk) :: velocity(3)
real(rk) :: radius
real(rk) :: mass
real(rk) :: force(3)
call particle_reset()
call particle_add(10, (/ ZERO, ZERO, 0.40_rk /), &
(/ ZERO, ZERO, -0.50_rk /), 0.50_rk, ONE, slot, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(50)
call particle_contact_init(1000.0_rk, ZERO, ZERO, ZERO, 1000.0_rk, ZERO, &
params, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(51)
call pstep_wall_init((/ ZERO, ZERO, ZERO /), &
(/ ZERO, ZERO, ONE /), walls(1), ierr)
if (ierr .ne. FEM_SUCCESS) call fail(52)
call pstep_ctrl_init(0.01_rk, ZERO, (/ ZERO, ZERO, ZERO /), ZERO, &
ctrl, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(53)
call pbstep_advance(ctrl, params, walls, 1, ONE, diag, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(54)
if (diag%step%pair_count .ne. 0) call fail(55)
if (diag%step%wall_count .ne. 1) call fail(56)
call check_close(diag%step%max_overlap, 0.10_rk, 57)
call particle_get(slot, ext_id, active, position, velocity, radius, mass, &
force, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(58)
call check_vec(force, (/ ZERO, ZERO, 100.0_rk /), 59)
call check_vec(velocity, (/ ZERO, ZERO, 0.50_rk /), 60)
call pbstep_grid_stats(diag, dims, ncell, raw_pairs, max_occ, &
empty_count, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(61)
if (raw_pairs .ne. 0) call fail(62)
end subroutine check_binned_wall_step
subroutine check_build_pairs_and_errors()
type(particle_bin_step_diag) :: diag
type(particle_contact_params) :: params
integer :: pair_i(4)
integer :: pair_j(4)
integer :: pair_count
integer :: ierr
call setup_three(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(80)
call pbstep_diag_reset(diag)
call pbstep_build_pairs(1.10_rk, diag, pair_i, pair_j, pair_count, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(81)
if (pair_count .lt. 1) call fail(82)
if (diag%ncell .le. 0) call fail(83)
if (diag%max_occupancy .lt. 1) call fail(84)
call particle_contact_init(100.0_rk, ZERO, ZERO, ZERO, 100.0_rk, ZERO, &
params, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(85)
call pbstep_apply_pairs(params, -ONE, ONE, diag, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(86)
call pbstep_build_pairs(ZERO, diag, pair_i, pair_j, pair_count, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(87)
end subroutine check_build_pairs_and_errors
subroutine setup_pair(ierr)
integer, intent(out) :: ierr
integer :: s1
integer :: s2
call particle_reset()
call particle_add(1, (/ ZERO, ZERO, ZERO /), (/ ZERO, ZERO, ZERO /), &
0.50_rk, ONE, s1, ierr)
if (ierr .ne. FEM_SUCCESS) return
call particle_add(2, (/ 0.80_rk, ZERO, ZERO /), &
(/ ZERO, ZERO, ZERO /), 0.50_rk, ONE, s2, ierr)
end subroutine setup_pair
subroutine setup_three(ierr)
integer, intent(out) :: ierr
integer :: slot
call particle_reset()
call particle_add(1, (/ ZERO, ZERO, ZERO /), (/ ZERO, ZERO, ZERO /), &
0.50_rk, ONE, slot, ierr)
if (ierr .ne. FEM_SUCCESS) return
call particle_add(2, (/ 0.80_rk, ZERO, ZERO /), &
(/ ZERO, ZERO, ZERO /), 0.50_rk, ONE, slot, ierr)
if (ierr .ne. FEM_SUCCESS) return
call particle_add(3, (/ 4.0_rk, ZERO, ZERO /), &
(/ ZERO, ZERO, ZERO /), 0.50_rk, ONE, slot, ierr)
end subroutine setup_three
subroutine capture_two(position, velocity, force, ierr)
real(rk), intent(out) :: position(3,2)
real(rk), intent(out) :: velocity(3,2)
real(rk), intent(out) :: force(3,2)
integer, intent(out) :: ierr
integer :: slot
integer :: ext_id
logical :: active
real(rk) :: radius
real(rk) :: mass
do slot = 1, 2
call particle_get(slot, ext_id, active, position(:,slot), &
velocity(:,slot), radius, mass, force(:,slot), ierr)
if (ierr .ne. FEM_SUCCESS) return
end do
call error_clear(ierr)
end subroutine capture_two
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 check_mat(actual, expected, code)
real(rk), intent(in) :: actual(3,2)
real(rk), intent(in) :: expected(3,2)
integer, intent(in) :: code
if (maxval(abs(actual - expected)) .gt. TOL) call fail(code)
end subroutine check_mat
subroutine fail(code)
integer, intent(in) :: code
write(*,*) 'test_particle_step_binned failed:', code
stop 1
end subroutine fail
end program test_particle_step_binned