program test_particle_config use fem_precision use fem_error use fem_particle_state use fem_particle_config implicit none real(rk), parameter :: TOL = 1.0e-10_rk call check_keyword_kind() call check_keyword_config() call check_data_lines() call check_cloud_only_execute() call check_apply_and_step() call check_file_loading() call check_errors() call check_capacity_and_queries() contains subroutine check_keyword_kind() integer :: ierr integer :: kind call pconfig_keyword_kind(' ', kind, ierr) if (ierr .ne. FEM_SUCCESS) call fail(10) if (kind .ne. PCONFIG_KIND_EMPTY) call fail(11) call pconfig_keyword_kind('*particle cloud, id=1', kind, ierr) if (ierr .ne. FEM_SUCCESS) call fail(12) if (kind .ne. PCONFIG_KIND_CLOUD) call fail(13) call pconfig_keyword_kind('CONTACT, 1, 0, 0, 0, 1, 0', kind, ierr) if (ierr .ne. FEM_SUCCESS) call fail(14) if (kind .ne. PCONFIG_KIND_CONTACT) call fail(15) call pconfig_keyword_kind('*PARTICLE STEP, DT=0.1', kind, ierr) if (ierr .ne. FEM_SUCCESS) call fail(16) if (kind .ne. PCONFIG_KIND_STEP) call fail(17) call pconfig_keyword_kind('WALL, 0,0,0, 0,0,1', kind, ierr) if (ierr .ne. FEM_SUCCESS) call fail(18) if (kind .ne. PCONFIG_KIND_WALL) call fail(19) call pconfig_keyword_kind('BOX_WALLS, 0,0,0, 1,1,1', kind, ierr) if (ierr .ne. FEM_SUCCESS) call fail(20) if (kind .ne. PCONFIG_KIND_BOX_WALLS) call fail(21) call pconfig_keyword_kind('*PARTICLE UNKNOWN', kind, ierr) if (ierr .ne. FEM_SUCCESS) call fail(22) if (kind .ne. PCONFIG_KIND_UNKNOWN) call fail(23) end subroutine check_keyword_kind subroutine check_keyword_config() type(particle_config_state) :: config logical :: has_cloud logical :: has_contact logical :: has_step logical :: clear_forces integer :: ierr integer :: nwall real(rk) :: min_corner(3) real(rk) :: max_corner(3) real(rk) :: radius real(rk) :: density real(rk) :: point(3) real(rk) :: normal(3) real(rk) :: kn real(rk) :: cn real(rk) :: kt real(rk) :: mu real(rk) :: kwall real(rk) :: cwall real(rk) :: dt real(rk) :: damping real(rk) :: search_gap real(rk) :: gravity(3) real(rk) :: cell_size call pconfig_reset(config) call pconfig_parse_line('*PARTICLE CLOUD, ID=10, XMIN=0, YMIN=0, ' // & 'ZMIN=0, XMAX=2, YMAX=2, ZMAX=2, ' // & 'RADIUS=0.5, DENSITY=2, SPACING=1, ' // & 'VX=1, VY=2, VZ=3, STAGGER=NO', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(40) call pconfig_parse_line('*PARTICLE CONTACT, KN=400, CN=2, KT=10, MU=0.3, KWALL=500, CWALL=1', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(41) call pconfig_parse_line('*PARTICLE STEP, DT=0.01, DAMPING=0.2, ' // & 'SEARCH=0.05, GX=0, GY=0, GZ=-9.81, ' // & 'CELL=1.25, CLEAR=NO', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(42) call pconfig_parse_line('*PARTICLE WALL, PX=0, PY=0, PZ=0.2, NX=0, NY=0, NZ=1', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(43) call pconfig_parse_line('*PARTICLE BOX WALLS, XMIN=0, YMIN=0, ZMIN=0, XMAX=2, YMAX=2, ZMAX=2', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(44) call pconfig_validate(config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(45) call pconfig_state_counts(config, has_cloud, has_contact, has_step, nwall, & ierr) if (ierr .ne. FEM_SUCCESS) call fail(46) if (.not. has_cloud .or. .not. has_contact .or. .not. has_step) call fail(47) if (nwall .ne. 7) call fail(48) call pconfig_cloud_bounds(config, min_corner, max_corner, radius, density, & ierr) if (ierr .ne. FEM_SUCCESS) call fail(49) call check_vec(min_corner, (/ ZERO, ZERO, ZERO /), 50) call check_vec(max_corner, (/ 2.0_rk, 2.0_rk, 2.0_rk /), 51) call check_close(radius, 0.5_rk, 52) call check_close(density, 2.0_rk, 53) call pconfig_wall_values(config, 1, point, normal, ierr) if (ierr .ne. FEM_SUCCESS) call fail(54) call check_vec(point, (/ ZERO, ZERO, 0.2_rk /), 55) call check_vec(normal, (/ ZERO, ZERO, ONE /), 56) call pconfig_wall_values(config, 2, point, normal, ierr) if (ierr .ne. FEM_SUCCESS) call fail(57) call check_vec(normal, (/ ONE, ZERO, ZERO /), 58) call pconfig_contact_values(config, kn, cn, kt, mu, kwall, cwall, ierr) if (ierr .ne. FEM_SUCCESS) call fail(59) call check_close(kn, 400.0_rk, 60) call check_close(cn, 2.0_rk, 61) call check_close(kt, 10.0_rk, 62) call check_close(mu, 0.3_rk, 63) call check_close(kwall, 500.0_rk, 64) call check_close(cwall, ONE, 65) call pconfig_step_values(config, dt, damping, search_gap, gravity, & cell_size, clear_forces, ierr) if (ierr .ne. FEM_SUCCESS) call fail(66) call check_close(dt, 0.01_rk, 67) call check_close(damping, 0.2_rk, 68) call check_close(search_gap, 0.05_rk, 69) call check_vec(gravity, (/ ZERO, ZERO, -9.81_rk /), 70) call check_close(cell_size, 1.25_rk, 71) if (clear_forces) call fail(72) end subroutine check_keyword_config subroutine check_data_lines() type(particle_config_state) :: config logical :: has_cloud logical :: has_contact logical :: has_step logical :: clear_forces integer :: ierr integer :: nwall real(rk) :: dt real(rk) :: damping real(rk) :: search_gap real(rk) :: gravity(3) real(rk) :: cell_size real(rk) :: min_corner(3) real(rk) :: max_corner(3) real(rk) :: radius real(rk) :: density call pconfig_reset(config) call pconfig_parse_line('CLOUD, 20, 0, 0, 0, 2, 2, 2, 0.5, 1.5, 1, 0.1, 0.2, 0.3, 0', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(100) call pconfig_parse_line('CONTACT, 300, 0, 25, 0.2, 350, 0', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(101) call pconfig_parse_line('STEP, 0.005, 0.1, 0.02, 0, 0, -1, 1.1, 1', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(102) call pconfig_parse_line('WALL, 0, 0, 0.25, 0, 0, 1', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(103) call pconfig_parse_line('BOX_WALLS, 0, 0, 0, 2, 2, 2', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(104) call pconfig_validate(config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(105) call pconfig_state_counts(config, has_cloud, has_contact, has_step, nwall, & ierr) if (ierr .ne. FEM_SUCCESS) call fail(106) if (.not. has_cloud .or. .not. has_contact .or. .not. has_step) call fail(107) if (nwall .ne. 7) call fail(108) call pconfig_cloud_bounds(config, min_corner, max_corner, radius, density, & ierr) if (ierr .ne. FEM_SUCCESS) call fail(109) call check_vec(min_corner, (/ ZERO, ZERO, ZERO /), 110) call check_vec(max_corner, (/ 2.0_rk, 2.0_rk, 2.0_rk /), 111) call check_close(radius, 0.5_rk, 112) call check_close(density, 1.5_rk, 113) call pconfig_step_values(config, dt, damping, search_gap, gravity, & cell_size, clear_forces, ierr) if (ierr .ne. FEM_SUCCESS) call fail(114) call check_close(dt, 0.005_rk, 115) call check_close(damping, 0.1_rk, 116) call check_close(search_gap, 0.02_rk, 117) call check_vec(gravity, (/ ZERO, ZERO, -ONE /), 118) call check_close(cell_size, 1.1_rk, 119) if (.not. clear_forces) call fail(120) end subroutine check_data_lines subroutine check_cloud_only_execute() type(particle_config_state) :: config type(particle_config_diag) :: diag integer :: ierr integer :: generated integer :: active integer :: contacts integer :: raw_pairs integer :: filtered_pairs real(rk) :: max_overlap real(rk) :: total_energy call pconfig_reset(config) call pconfig_parse_line('*PARTICLE CLOUD, ID=100, XMIN=0, YMIN=0, ' // & 'ZMIN=0, XMAX=2, YMAX=2, ZMAX=2, ' // & 'RADIUS=0.5, DENSITY=1, SPACING=1, ' // & 'STAGGER=NO', config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(130) call pconfig_execute(config, diag, .true., ierr) if (ierr .ne. FEM_SUCCESS) call fail(131) if (particle_count() .ne. 8) call fail(132) if (diag%generated_count .ne. 8) call fail(133) if (diag%active_count .ne. 8) call fail(134) if (diag%contact_count .ne. 0) call fail(135) if (diag%raw_pairs .ne. 0) call fail(136) call pconfig_summary(diag, generated, active, contacts, raw_pairs, & filtered_pairs, max_overlap, total_energy, ierr) if (ierr .ne. FEM_SUCCESS) call fail(137) if (generated .ne. 8 .or. active .ne. 8) call fail(138) if (contacts .ne. 0 .or. raw_pairs .ne. 0) call fail(139) if (filtered_pairs .ne. 0) call fail(140) call check_close(max_overlap, ZERO, 141) call check_close(total_energy, ZERO, 142) end subroutine check_cloud_only_execute subroutine check_apply_and_step() type(particle_config_state) :: config type(particle_config_diag) :: diag integer :: ierr integer :: generated integer :: active integer :: contacts integer :: raw_pairs integer :: filtered_pairs real(rk) :: max_overlap real(rk) :: total_energy call build_step_config(config, ierr) if (ierr .ne. FEM_SUCCESS) call fail(150) call pconfig_execute(config, diag, .true., ierr) if (ierr .ne. FEM_SUCCESS) call fail(151) if (particle_count() .ne. 8) call fail(152) if (diag%generated_count .ne. 8) call fail(153) if (diag%active_count .ne. 8) call fail(154) if (diag%wall_contacts .ne. 4) call fail(155) if (diag%pair_contacts .ne. 0) call fail(156) if (diag%contact_count .ne. 4) call fail(157) if (diag%max_overlap .le. ZERO) call fail(158) if (diag%dt_critical .le. ZERO) call fail(159) if (diag%raw_pairs .lt. diag%filtered_pairs) call fail(160) call pconfig_summary(diag, generated, active, contacts, raw_pairs, & filtered_pairs, max_overlap, total_energy, ierr) if (ierr .ne. FEM_SUCCESS) call fail(161) if (generated .ne. 8 .or. active .ne. 8 .or. contacts .ne. 4) call fail(162) call check_close(max_overlap, 0.20_rk, 163) if (total_energy .lt. ZERO) call fail(164) end subroutine check_apply_and_step subroutine check_file_loading() type(particle_config_state) :: config type(particle_config_diag) :: diag integer :: ierr integer :: nwall logical :: has_cloud logical :: has_contact logical :: has_step call write_config_file(ierr) if (ierr .ne. FEM_SUCCESS) call fail(200) call pconfig_load_file('particle_config_load.tmp', config, ierr) call delete_config_file() if (ierr .ne. FEM_SUCCESS) call fail(201) call pconfig_state_counts(config, has_cloud, has_contact, has_step, nwall, & ierr) if (ierr .ne. FEM_SUCCESS) call fail(202) if (.not. has_cloud .or. .not. has_contact .or. .not. has_step) call fail(203) if (nwall .ne. 7) call fail(204) call pconfig_execute(config, diag, .true., ierr) if (ierr .ne. FEM_SUCCESS) call fail(205) if (diag%generated_count .ne. 8) call fail(206) if (diag%wall_contacts .ne. 4) call fail(207) end subroutine check_file_loading subroutine check_errors() type(particle_config_state) :: config type(particle_config_diag) :: diag integer :: ierr real(rk) :: min_corner(3) real(rk) :: max_corner(3) real(rk) :: radius real(rk) :: density real(rk) :: point(3) real(rk) :: normal(3) real(rk) :: kn real(rk) :: cn real(rk) :: kt real(rk) :: mu real(rk) :: kwall real(rk) :: cwall call pconfig_reset(config) call pconfig_parse_line('*PARTICLE BAD, A=1', config, ierr) if (ierr .eq. FEM_SUCCESS) call fail(250) call pconfig_parse_line('*PARTICLE CLOUD, XMIN=0, YMIN=0, ZMIN=0, XMAX=1, YMAX=1, ZMAX=1, RADIUS=0, DENSITY=1', config, ierr) if (ierr .eq. FEM_SUCCESS) call fail(251) call pconfig_parse_line('CLOUD, 1, 0, 0, 0', config, ierr) if (ierr .eq. FEM_SUCCESS) call fail(252) call pconfig_parse_line('CONTACT, 0, 0, 0, 0, 1, 0', config, ierr) if (ierr .eq. FEM_SUCCESS) call fail(253) call pconfig_parse_line('STEP, 0, 0, 0, 0, 0, 0, 1', config, ierr) if (ierr .eq. FEM_SUCCESS) call fail(254) call pconfig_parse_line('WALL, 0, 0, 0, 0, 0, 0', config, ierr) if (ierr .eq. FEM_SUCCESS) call fail(255) call pconfig_cloud_bounds(config, min_corner, max_corner, radius, density, & ierr) if (ierr .eq. FEM_SUCCESS) call fail(256) call pconfig_wall_values(config, 1, point, normal, ierr) if (ierr .eq. FEM_SUCCESS) call fail(257) call pconfig_contact_values(config, kn, cn, kt, mu, kwall, cwall, ierr) if (ierr .eq. FEM_SUCCESS) call fail(258) call pconfig_apply_cloud(config, diag, .true., ierr) if (ierr .eq. FEM_SUCCESS) call fail(259) call pconfig_run_step(config, diag, ierr) if (ierr .eq. FEM_SUCCESS) call fail(260) call pconfig_load_file('does_not_exist_particle_config.tmp', config, ierr) if (ierr .eq. FEM_SUCCESS) call fail(261) end subroutine check_errors subroutine check_capacity_and_queries() type(particle_config_state) :: config type(particle_config_diag) :: diag integer :: ierr integer :: i integer :: generated integer :: active integer :: contacts integer :: raw_pairs integer :: filtered_pairs real(rk) :: point(3) real(rk) :: normal(3) real(rk) :: max_overlap real(rk) :: total_energy call pconfig_reset(config) do i = 1, PCONFIG_MAX_WALLS point = (/ real(i, rk), ZERO, ZERO /) normal = (/ ZERO, ONE, ZERO /) call pconfig_add_wall(config, point, normal, ierr) if (ierr .ne. FEM_SUCCESS) call fail(300 + i) end do call pconfig_add_wall(config, (/ ZERO, ZERO, ZERO /), & (/ ONE, ZERO, ZERO /), ierr) if (ierr .eq. FEM_SUCCESS) call fail(330) call pconfig_wall_values(config, PCONFIG_MAX_WALLS, point, normal, ierr) if (ierr .ne. FEM_SUCCESS) call fail(331) call check_close(point(1), real(PCONFIG_MAX_WALLS, rk), 332) call check_vec(normal, (/ ZERO, ONE, ZERO /), 333) call pconfig_diag_reset(diag) diag%generated_count = -1 call pconfig_summary(diag, generated, active, contacts, raw_pairs, & filtered_pairs, max_overlap, total_energy, ierr) if (ierr .eq. FEM_SUCCESS) call fail(334) end subroutine check_capacity_and_queries subroutine build_step_config(config, ierr) type(particle_config_state), intent(out) :: config integer, intent(out) :: ierr call pconfig_reset(config) call pconfig_parse_line('*PARTICLE CLOUD, ID=1, XMIN=0, YMIN=0, ' // & 'ZMIN=0, XMAX=2, YMAX=2, ZMAX=2, ' // & 'RADIUS=0.5, DENSITY=1, SPACING=1, ' // & 'STAGGER=NO', config, ierr) if (ierr .ne. FEM_SUCCESS) return call pconfig_parse_line('*PARTICLE CONTACT, KN=1000, CN=0, KT=0, MU=0, KWALL=1000, CWALL=0', config, ierr) if (ierr .ne. FEM_SUCCESS) return call pconfig_parse_line('*PARTICLE STEP, DT=0.001, DAMPING=0, SEARCH=0, GX=0, GY=0, GZ=0, CELL=1', config, ierr) if (ierr .ne. FEM_SUCCESS) return call pconfig_parse_line('*PARTICLE WALL, PX=0, PY=0, PZ=0.2, NX=0, NY=0, NZ=1', config, ierr) if (ierr .ne. FEM_SUCCESS) return call pconfig_parse_line('*PARTICLE BOX WALLS', config, ierr) end subroutine build_step_config subroutine write_config_file(ierr) integer, intent(out) :: ierr integer :: unit integer :: ios unit = 78 open(unit=unit, file='particle_config_load.tmp', status='unknown', & action='write', iostat=ios) if (ios .ne. 0) then call error_set(FEM_ERR_OUTPUT, 'failed to write particle config tmp', & ierr) return end if write(unit, '(A)') '# temporary particle config' write(unit, '(A)') '*PARTICLE CLOUD, ID=1, XMIN=0, YMIN=0, ' // & 'ZMIN=0, XMAX=2, YMAX=2, ZMAX=2, ' // & 'RADIUS=0.5, DENSITY=1, SPACING=1, STAGGER=NO' write(unit, '(A)') '*PARTICLE CONTACT, KN=1000, CN=0, KT=0, MU=0, KWALL=1000, CWALL=0' write(unit, '(A)') '*PARTICLE STEP, DT=0.001, DAMPING=0, SEARCH=0, GX=0, GY=0, GZ=0, CELL=1' write(unit, '(A)') '*PARTICLE WALL, PX=0, PY=0, PZ=0.2, NX=0, NY=0, NZ=1' write(unit, '(A)') '*PARTICLE BOX WALLS' close(unit) call error_clear(ierr) end subroutine write_config_file subroutine delete_config_file() integer :: unit integer :: ios unit = 78 open(unit=unit, file='particle_config_load.tmp', status='old', & iostat=ios) if (ios .eq. 0) close(unit, status='delete') end subroutine delete_config_file 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 call delete_config_file() write(*,*) 'test_particle_config failed:', code stop 1 end subroutine fail end program test_particle_config