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

328 lines
12 KiB
Fortran

program test_particle_step
use fem_precision
use fem_error
use fem_particle_state
use fem_particle_contact
use fem_particle_step
use fem_particle_audit
implicit none
real(rk), parameter :: TOL = 1.0e-10_rk
call check_ctrl_wall()
call check_pair_step()
call check_wall_step()
call check_bounds_and_dt()
call check_audit_paths()
call check_error_paths()
contains
subroutine check_ctrl_wall()
type(particle_step_ctrl) :: ctrl
type(particle_wall_def) :: wall
integer :: ierr
call pstep_ctrl_init(0.01_rk, 0.02_rk, (/ ZERO, ZERO, -9.8_rk /), &
0.05_rk, ctrl, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(10)
call check_close(ctrl%dt, 0.01_rk, 11)
call check_close(ctrl%damping, 0.02_rk, 12)
call check_vec(ctrl%gravity, (/ ZERO, ZERO, -9.8_rk /), 13)
if (.not. ctrl%clear_forces) call fail(14)
call pstep_wall_init((/ ZERO, ZERO, ZERO /), &
(/ ZERO, ZERO, 2.0_rk /), wall, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(15)
if (.not. wall%active) call fail(16)
call check_vec(wall%normal, (/ ZERO, ZERO, ONE /), 17)
call pstep_validate_walls((/ wall /), 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(18)
ctrl%dt = ZERO
call pstep_ctrl_validate(ctrl, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(19)
end subroutine check_ctrl_wall
subroutine check_pair_step()
type(particle_contact_params) :: params
type(particle_step_ctrl) :: ctrl
type(particle_step_diag) :: diag
type(particle_wall_def) :: walls(1)
integer :: s1
integer :: s2
integer :: ierr
integer :: ext_id
integer :: contacts
logical :: active
real(rk) :: position(3)
real(rk) :: velocity(3)
real(rk) :: radius
real(rk) :: mass
real(rk) :: force(3)
real(rk) :: max_overlap
real(rk) :: total_energy
real(rk) :: total_power
call particle_reset()
call particle_add(1, (/ ZERO, ZERO, ZERO /), (/ ZERO, ZERO, ZERO /), &
0.50_rk, ONE, s1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(40)
call particle_add(2, (/ 0.80_rk, ZERO, ZERO /), &
(/ ZERO, ZERO, ZERO /), 0.50_rk, ONE, s2, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(41)
call particle_contact_init(100.0_rk, ZERO, ZERO, ZERO, 100.0_rk, ZERO, &
params, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(42)
call pstep_ctrl_init(0.10_rk, ZERO, (/ ZERO, ZERO, ZERO /), ZERO, &
ctrl, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(43)
walls(1)%active = .false.
call pstep_advance(ctrl, params, walls, 1, diag, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(44)
if (diag%active_count .ne. 2) call fail(45)
if (diag%candidate_count .ne. 1) call fail(46)
if (diag%pair_count .ne. 1) call fail(47)
if (diag%wall_count .ne. 0) call fail(48)
call check_close(diag%max_overlap, 0.20_rk, 49)
call check_close(diag%elastic_energy, 2.0_rk, 50)
call check_close(diag%kinetic_before, ZERO, 51)
call check_close(diag%kinetic_after, 4.0_rk, 52)
call check_close(diag%max_force, 20.0_rk, 53)
call check_close(diag%force_norm_sum, 40.0_rk, 54)
call particle_get(s1, ext_id, active, position, velocity, radius, mass, &
force, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(55)
call check_vec(force, (/ -20.0_rk, ZERO, ZERO /), 56)
call check_vec(velocity, (/ -2.0_rk, ZERO, ZERO /), 57)
call check_vec(position, (/ -0.20_rk, ZERO, ZERO /), 58)
call particle_get(s2, ext_id, active, position, velocity, radius, mass, &
force, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(59)
call check_vec(force, (/ 20.0_rk, ZERO, ZERO /), 60)
call check_vec(velocity, (/ 2.0_rk, ZERO, ZERO /), 61)
call check_vec(position, (/ ONE, ZERO, ZERO /), 62)
call pstep_diag_summary(diag, contacts, max_overlap, total_energy, &
total_power, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(63)
if (contacts .ne. 1) call fail(64)
call check_close(max_overlap, 0.20_rk, 65)
call check_close(total_energy, 6.0_rk, 66)
call check_close(total_power, ZERO, 67)
end subroutine check_pair_step
subroutine check_wall_step()
type(particle_contact_params) :: params
type(particle_step_ctrl) :: ctrl
type(particle_step_diag) :: diag
type(particle_wall_def) :: wall(1)
type(particle_audit_state) :: audit
integer :: slot
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 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(80)
call particle_contact_init(1000.0_rk, ZERO, ZERO, ZERO, 1000.0_rk, ZERO, &
params, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(81)
call pstep_wall_init((/ ZERO, ZERO, ZERO /), &
(/ ZERO, ZERO, ONE /), wall(1), ierr)
if (ierr .ne. FEM_SUCCESS) call fail(82)
call pstep_ctrl_init(0.01_rk, ZERO, (/ ZERO, ZERO, ZERO /), ZERO, &
ctrl, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(83)
call pstep_advance(ctrl, params, wall, 1, diag, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(84)
if (diag%pair_count .ne. 0 .or. diag%wall_count .ne. 1) call fail(85)
call check_close(diag%max_overlap, 0.10_rk, 86)
call check_close(diag%elastic_energy, 5.0_rk, 87)
call particle_get(slot, ext_id, active, position, velocity, radius, mass, &
force, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(88)
call check_vec(force, (/ ZERO, ZERO, 100.0_rk /), 89)
call check_vec(velocity, (/ ZERO, ZERO, 0.50_rk /), 90)
call check_vec(position, (/ ZERO, ZERO, 0.405_rk /), 91)
call paudit_reset(audit)
call paudit_rebound(slot, (/ ZERO, ZERO, ONE /), 0.10_rk, audit, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(92)
if (audit%status .ne. PAUDIT_PASS) call fail(93)
call check_close(audit%rebound_speed, 0.50_rk, 94)
end subroutine check_wall_step
subroutine check_bounds_and_dt()
type(particle_contact_params) :: params
integer :: s1
integer :: s2
integer :: ierr
real(rk) :: dtcrit
real(rk) :: max_force
real(rk) :: sum_force
real(rk) :: min_corner(3)
real(rk) :: max_corner(3)
call particle_reset()
call particle_add(1, (/ ZERO, ZERO, ONE /), (/ ONE, ZERO, ZERO /), &
0.25_rk, 2.0_rk, s1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(110)
call particle_add(2, (/ ONE, ZERO, 2.0_rk /), (/ ZERO, ONE, ZERO /), &
0.50_rk, 3.0_rk, s2, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(111)
call particle_contact_init(200.0_rk, ZERO, ZERO, ZERO, 800.0_rk, ZERO, &
params, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(112)
call pstep_estimate_dt(params, 0.2_rk, dtcrit, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(113)
call check_close(dtcrit, 0.01_rk, 114)
call particle_set_force(s1, (/ 3.0_rk, 4.0_rk, ZERO /), ierr)
if (ierr .ne. FEM_SUCCESS) call fail(115)
call particle_set_force(s2, (/ ZERO, ZERO, 12.0_rk /), ierr)
if (ierr .ne. FEM_SUCCESS) call fail(116)
call pstep_force_norms(max_force, sum_force, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(117)
call check_close(max_force, 12.0_rk, 118)
call check_close(sum_force, 17.0_rk, 119)
call pstep_predict_bounds(0.5_rk, (/ ZERO, ZERO, -2.0_rk /), &
min_corner, max_corner, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(120)
call check_vec(min_corner, (/ 0.25_rk, -0.25_rk, 0.50_rk /), 121)
call check_vec(max_corner, (/ 1.50_rk, ONE, 2.25_rk /), 122)
end subroutine check_bounds_and_dt
subroutine check_audit_paths()
type(particle_contact_params) :: params
type(particle_step_ctrl) :: ctrl
type(particle_step_diag) :: diag
type(particle_wall_def) :: walls(1)
type(particle_audit_state) :: audit
type(particle_audit_state) :: other
type(particle_audit_state) :: merged
integer :: s1
integer :: s2
integer :: ierr
integer :: status
integer :: checked_steps
character(len=16) :: name
real(rk) :: mass_error
real(rk) :: mom_change
real(rk) :: energy_change
real(rk) :: overlap_ratio
real(rk) :: rebound_speed
call particle_reset()
call particle_add(1, (/ ZERO, ZERO, ZERO /), (/ ZERO, ZERO, ZERO /), &
0.50_rk, ONE, s1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(140)
call particle_add(2, (/ 0.80_rk, ZERO, ZERO /), &
(/ ZERO, ZERO, ZERO /), 0.50_rk, ONE, s2, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(141)
call particle_contact_init(100.0_rk, ZERO, ZERO, ZERO, 100.0_rk, ZERO, &
params, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(142)
call pstep_ctrl_init(0.10_rk, ZERO, (/ ZERO, ZERO, ZERO /), ZERO, &
ctrl, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(143)
walls(1)%active = .false.
call pstep_advance(ctrl, params, walls, 1, diag, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(144)
call paudit_reset(audit)
call paudit_step(diag, 1.0e-12_rk, 10.0_rk, 10.0_rk, audit, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(145)
call paudit_overlap(diag, 0.50_rk, 0.3_rk, 0.6_rk, audit, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(146)
if (audit%status .ne. PAUDIT_WARN) call fail(147)
call paudit_values(audit, status, checked_steps, mass_error, mom_change, &
energy_change, overlap_ratio, rebound_speed, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(148)
if (status .ne. PAUDIT_WARN .or. checked_steps .ne. 1) call fail(149)
call check_close(mass_error, ZERO, 150)
call check_close(overlap_ratio, 0.40_rk, 151)
call paudit_status_name(PAUDIT_WARN, name, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(152)
if (trim(name) .ne. 'warn') call fail(153)
call paudit_reset(other)
call paudit_merge(audit, other, merged, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(154)
if (merged%status .ne. PAUDIT_WARN) call fail(155)
call paudit_status_name(99, name, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(156)
end subroutine check_audit_paths
subroutine check_error_paths()
type(particle_contact_params) :: params
type(particle_step_ctrl) :: ctrl
type(particle_step_diag) :: diag
type(particle_wall_def) :: wall(1)
type(particle_audit_state) :: audit
integer :: slot
integer :: ierr
real(rk) :: ratio
call particle_reset()
call particle_add(1, (/ ZERO, ZERO, ZERO /), (/ ZERO, ZERO, ZERO /), &
0.50_rk, ONE, slot, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(180)
call particle_contact_init(100.0_rk, ZERO, ZERO, ZERO, 100.0_rk, ZERO, &
params, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(181)
call pstep_ctrl_init(0.01_rk, ZERO, (/ ZERO, ZERO, ZERO /), ZERO, &
ctrl, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(182)
call pstep_wall_init((/ ZERO, ZERO, ZERO /), &
(/ ZERO, ZERO, ZERO /), wall(1), ierr)
if (ierr .eq. FEM_SUCCESS) call fail(183)
wall(1)%active = .true.
wall(1)%normal = (/ ZERO, ZERO, 2.0_rk /)
call pstep_advance(ctrl, params, wall, 1, diag, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(184)
call pstep_contact_ratio(diag, ZERO, ratio, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(185)
call paudit_reset(audit)
call paudit_overlap(diag, 0.50_rk, 0.4_rk, 0.2_rk, audit, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(186)
call paudit_rebound(99, (/ ZERO, ZERO, ONE /), ZERO, audit, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(187)
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_step failed:', code
stop 1
end subroutine fail
end program test_particle_step