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

455 lines
17 KiB
Fortran

program test_particle_config_check
use fem_precision
use fem_error
use fem_particle_state
use fem_particle_config
use fem_particle_config_run
use fem_particle_config_check
implicit none
call check_stop_mapping()
call check_parse_keyword_spec()
call check_parse_data_spec()
call check_pass_thresholds()
call check_default_checker()
call check_threshold_failures()
call check_series_failures()
call check_load_file()
call check_result_format()
call check_result_export()
call check_spec_errors()
contains
subroutine check_stop_mapping()
integer :: ierr
integer :: reason
character(len=32) :: text
call pcheck_stop_from_text('none', reason, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(10)
if (reason .ne. PCRUN_STOP_NONE) call fail(11)
call pcheck_stop_from_text('overlap', reason, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(12)
if (reason .ne. PCRUN_STOP_OVERLAP) call fail(13)
call pcheck_stop_from_text('max_energy', reason, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(14)
if (reason .ne. PCRUN_STOP_ENERGY) call fail(15)
call pcheck_stop_from_text('capacity', reason, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(16)
if (reason .ne. PCRUN_STOP_CAPACITY) call fail(17)
call pcheck_stop_from_text('unknown', reason, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(18)
call pcheck_stop_to_text(PCRUN_STOP_OVERLAP, text, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(19)
if (trim(text) .ne. 'max_overlap') call fail(20)
call pcheck_failure_name(PCHECK_FAIL_ENERGY, text, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(21)
if (trim(text) .ne. 'energy') call fail(22)
call pcheck_failure_name(-99, text, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(23)
end subroutine check_stop_mapping
subroutine check_parse_keyword_spec()
type(particle_check_spec) :: spec
integer :: ierr
call pcheck_spec_reset(spec)
call pcheck_parse_line('*PARTICLE CHECK, MIN_RECORDS=3, ' // &
'MAX_RECORDS=5, MIN_FINAL_STEP=3, ' // &
'MAX_FINAL_STEP=5, EXPECT_STOP=NONE, ' // &
'MIN_ACTIVE=8, MAX_ACTIVE=8, ' // &
'MIN_GENERATED=8, MAX_GENERATED=8, ' // &
'MIN_CONTACTS=4, MAX_CONTACTS=99, ' // &
'MIN_PEAK_CONTACTS=4, MAX_OVERLAP=1, ' // &
'MAX_ENERGY=1000, MAX_KINETIC=1000', &
spec, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(40)
if (.not. spec%check_min_records) call fail(41)
if (.not. spec%check_max_records) call fail(42)
if (.not. spec%check_min_active) call fail(43)
if (.not. spec%check_max_active) call fail(44)
if (.not. spec%check_stop_reason) call fail(45)
if (spec%min_records .ne. 3 .or. spec%max_records .ne. 5) call fail(46)
if (spec%min_active .ne. 8 .or. spec%max_active .ne. 8) call fail(47)
if (spec%expected_stop_reason .ne. PCRUN_STOP_NONE) call fail(48)
if (.not. spec%require_monotonic_steps) call fail(49)
if (.not. spec%require_pair_bounds) call fail(50)
call pcheck_parse_line('*PARTICLE CHECK, MONOTONIC=NO, ' // &
'ENERGY_NONNEG=NO, OVERLAP_NONNEG=NO, ' // &
'PAIR_BOUNDS=NO', spec, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(51)
if (spec%require_monotonic_steps) call fail(52)
if (spec%require_energy_nonnegative) call fail(53)
if (spec%require_overlap_nonnegative) call fail(54)
if (spec%require_pair_bounds) call fail(55)
end subroutine check_parse_keyword_spec
subroutine check_parse_data_spec()
type(particle_check_spec) :: spec
integer :: ierr
call pcheck_spec_reset(spec)
call pcheck_parse_line('CHECK, 8, 4, 1.0, 1000.0, none, 2', spec, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(80)
if (.not. spec%check_min_active) call fail(81)
if (.not. spec%check_min_contacts) call fail(82)
if (.not. spec%check_max_overlap) call fail(83)
if (.not. spec%check_max_energy) call fail(84)
if (.not. spec%check_stop_reason) call fail(85)
if (.not. spec%check_min_records) call fail(86)
if (spec%min_active .ne. 8) call fail(87)
if (spec%min_contacts .ne. 4) call fail(88)
if (abs(spec%max_overlap - ONE) .gt. 1.0e-12_rk) call fail(89)
if (spec%expected_stop_reason .ne. PCRUN_STOP_NONE) call fail(90)
if (spec%min_records .ne. 2) call fail(91)
end subroutine check_parse_data_spec
subroutine check_pass_thresholds()
type(particle_config_run_history) :: history
type(particle_check_spec) :: spec
type(particle_check_result) :: result
integer :: ierr
logical :: passed
integer :: failures
integer :: checks_run
call build_wall_history(history, 3, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(120)
call pcheck_spec_reset(spec)
call pcheck_parse_line('*PARTICLE CHECK, MIN_RECORDS=3, ' // &
'MAX_RECORDS=3, MIN_FINAL_STEP=3, ' // &
'MAX_FINAL_STEP=3, STOP=NONE, ' // &
'MIN_ACTIVE=8, MAX_ACTIVE=8, ' // &
'MIN_GENERATED=8, MIN_CONTACTS=4, ' // &
'MIN_PEAK_CONTACTS=4, MAX_OVERLAP=1, ' // &
'MAX_ENERGY=1000, MAX_KINETIC=1000', &
spec, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(121)
call pcheck_run(history, spec, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(122)
if (.not. result%passed) call fail(123)
if (result%failure_count .ne. 0) call fail(124)
if (result%checks_run .le. 10) call fail(125)
call pcheck_summary(result, passed, failures, checks_run, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(126)
if (.not. passed .or. failures .ne. 0) call fail(127)
if (checks_run .ne. result%checks_run) call fail(128)
end subroutine check_pass_thresholds
subroutine check_default_checker()
type(particle_config_run_history) :: history
type(particle_check_result) :: result
integer :: ierr
call build_wall_history(history, 2, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(150)
call pcheck_run_default(history, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(151)
if (.not. result%passed) call fail(152)
if (result%checks_run .le. 0) call fail(153)
end subroutine check_default_checker
subroutine check_threshold_failures()
type(particle_config_run_history) :: history
type(particle_check_spec) :: spec
type(particle_check_result) :: result
integer :: ierr
call build_wall_history(history, 2, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(180)
call pcheck_spec_reset(spec)
spec%check_min_active = .true.
spec%min_active = 99
call pcheck_run(history, spec, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(181)
if (result%passed) call fail(182)
if (result%first_failure_code .ne. PCHECK_FAIL_ACTIVE) call fail(183)
call pcheck_spec_reset(spec)
spec%check_max_overlap = .true.
spec%max_overlap = 0.01_rk
call pcheck_run(history, spec, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(184)
if (result%passed) call fail(185)
if (result%first_failure_code .ne. PCHECK_FAIL_OVERLAP) call fail(186)
call pcheck_spec_reset(spec)
spec%check_stop_reason = .true.
spec%expected_stop_reason = PCRUN_STOP_ENERGY
call pcheck_run(history, spec, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(187)
if (result%passed) call fail(188)
if (result%first_failure_code .ne. PCHECK_FAIL_STOP_REASON) call fail(189)
end subroutine check_threshold_failures
subroutine check_series_failures()
type(particle_config_run_history) :: history
type(particle_check_result) :: result
integer :: ierr
call build_wall_history(history, 3, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(220)
history%filt_pair_series(1) = history%raw_pair_series(1) + 1
call pcheck_run_default(history, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(221)
if (result%passed) call fail(222)
if (result%first_failure_code .ne. PCHECK_FAIL_SERIES) call fail(223)
if (result%first_failure_index .ne. 1) call fail(224)
call build_wall_history(history, 3, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(225)
history%energy_series(2) = -ONE
call pcheck_run_default(history, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(226)
if (result%passed) call fail(227)
if (result%first_failure_code .ne. PCHECK_FAIL_ENERGY) call fail(228)
if (result%first_failure_index .ne. 2) call fail(229)
call build_wall_history(history, 3, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(230)
history%overlap_series(3) = -ONE
call pcheck_run_default(history, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(231)
if (result%passed) call fail(232)
if (result%first_failure_code .ne. PCHECK_FAIL_OVERLAP) call fail(233)
call build_wall_history(history, 3, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(234)
history%step_index(2) = history%step_index(1)
call pcheck_run_default(history, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(235)
if (result%passed) call fail(236)
if (result%first_failure_code .ne. PCHECK_FAIL_MONOTONIC) call fail(237)
end subroutine check_series_failures
subroutine check_load_file()
type(particle_config_run_history) :: history
type(particle_check_spec) :: spec
type(particle_check_result) :: result
integer :: ierr
call write_check_file(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(260)
call pcheck_load_file('particle_check.tmp', spec, ierr)
call delete_check_file()
if (ierr .ne. FEM_SUCCESS) call fail(261)
if (.not. spec%check_min_active) call fail(262)
if (.not. spec%check_max_overlap) call fail(263)
call build_wall_history(history, 3, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(264)
call pcheck_run(history, spec, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(265)
if (.not. result%passed) call fail(266)
end subroutine check_load_file
subroutine check_result_format()
type(particle_config_run_history) :: history
type(particle_check_spec) :: spec
type(particle_check_result) :: result
character(len=192) :: line
integer :: ierr
call build_wall_history(history, 1, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(290)
call pcheck_spec_reset(spec)
call pcheck_run_default(history, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(291)
call pcheck_result_line(result, line, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(292)
if (index(line, 'passed=T') .ne. 1) call fail(293)
if (index(line, 'first_code=ok') .le. 0) call fail(294)
spec%check_max_overlap = .true.
spec%max_overlap = 0.01_rk
call pcheck_run(history, spec, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(295)
call pcheck_result_line(result, line, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(296)
if (index(line, 'passed=F') .ne. 1) call fail(297)
if (index(line, 'first_code=overlap') .le. 0) call fail(298)
call pcheck_result_line(result, line(1:16), ierr)
if (ierr .eq. FEM_SUCCESS) call fail(299)
end subroutine check_result_format
subroutine check_result_export()
type(particle_config_run_history) :: history
type(particle_check_spec) :: spec
type(particle_check_result) :: result
character(len=192) :: line
integer :: ierr
integer :: unit
integer :: ios
integer :: count
call build_wall_history(history, 2, 1, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(305)
call pcheck_spec_reset(spec)
spec%check_min_records = .true.
spec%min_records = 2
spec%check_stop_reason = .true.
spec%expected_stop_reason = PCRUN_STOP_NONE
call pcheck_run(history, spec, result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(306)
call pcheck_result_header(line, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(307)
if (index(line, 'passed,checks,failures') .ne. 1) call fail(308)
call pcheck_result_csv_line(result, line, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(309)
if (index(line, '1,') .ne. 1) call fail(310)
call pcheck_spec_line(spec, line, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(311)
if (index(line, 'stop=none') .ne. 1) call fail(312)
if (index(line, 'min_records=2') .le. 0) call fail(313)
call pcheck_write_result('particle_check_result.tmp', result, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(314)
unit = 72
open(unit=unit, file='particle_check_result.tmp', status='old', &
action='read', iostat=ios)
if (ios .ne. 0) call fail(315)
count = 0
do
read(unit, '(A)', iostat=ios) line
if (ios .lt. 0) exit
if (ios .gt. 0) call fail(316)
count = count + 1
end do
close(unit, status='delete')
if (count .ne. 2) call fail(317)
call pcheck_result_header(line(1:8), ierr)
if (ierr .eq. FEM_SUCCESS) call fail(318)
end subroutine check_result_export
subroutine check_spec_errors()
type(particle_check_spec) :: spec
type(particle_check_result) :: result
logical :: passed
integer :: ierr
integer :: failures
integer :: checks_run
call pcheck_spec_reset(spec)
spec%check_min_records = .true.
spec%check_max_records = .true.
spec%min_records = 4
spec%max_records = 3
call pcheck_validate_spec(spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(320)
call pcheck_spec_reset(spec)
spec%check_min_overlap = .true.
spec%min_overlap = -ONE
call pcheck_validate_spec(spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(321)
call pcheck_parse_line('*PARTICLE CHECK, STOP=BOGUS', spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(322)
call pcheck_parse_line('*PARTICLE CHECK, MIN_RECORDS=A', spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(323)
call pcheck_parse_line('NOT_CHECK, MIN_RECORDS=1', spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(324)
call pcheck_parse_line('CHECK, 1', spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(325)
call pcheck_load_file('missing_particle_check.tmp', spec, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(326)
call pcheck_result_reset(result)
result%failure_count = -1
call pcheck_summary(result, passed, failures, checks_run, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(327)
end subroutine check_spec_errors
subroutine build_wall_history(history, nsteps, output_interval, ierr)
type(particle_config_run_history), intent(out) :: history
integer, intent(in) :: nsteps
integer, intent(in) :: output_interval
integer, intent(out) :: ierr
type(particle_config_state) :: config
type(particle_config_run_control) :: control
call build_wall_config(config, ierr)
if (ierr .ne. FEM_SUCCESS) return
call pcrun_control_init(nsteps, output_interval, .true., control, ierr)
if (ierr .ne. FEM_SUCCESS) return
call pcrun_execute(config, control, history, ierr)
end subroutine build_wall_history
subroutine build_wall_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)
end subroutine build_wall_config
subroutine write_check_file(ierr)
integer, intent(out) :: ierr
integer :: unit
integer :: ios
unit = 73
open(unit=unit, file='particle_check.tmp', status='unknown', &
action='write', iostat=ios)
if (ios .ne. 0) then
call error_set(FEM_ERR_OUTPUT, 'failed to write particle check tmp', &
ierr)
return
end if
write(unit, '(A)') '# temporary particle check file'
write(unit, '(A)') '*PARTICLE CHECK, MIN_RECORDS=3, MAX_RECORDS=3, ' // &
'MIN_FINAL_STEP=3, MAX_FINAL_STEP=3, ' // &
'STOP=NONE, MIN_ACTIVE=8, MIN_GENERATED=8, ' // &
'MIN_CONTACTS=4, MIN_PEAK_CONTACTS=4, ' // &
'MAX_OVERLAP=1, MAX_ENERGY=1000'
close(unit)
call error_clear(ierr)
end subroutine write_check_file
subroutine delete_check_file()
integer :: unit
integer :: ios
unit = 73
open(unit=unit, file='particle_check.tmp', status='old', iostat=ios)
if (ios .eq. 0) close(unit, status='delete')
end subroutine delete_check_file
subroutine delete_result_file()
integer :: unit
integer :: ios
unit = 72
open(unit=unit, file='particle_check_result.tmp', status='old', &
iostat=ios)
if (ios .eq. 0) close(unit, status='delete')
end subroutine delete_result_file
subroutine fail(code)
integer, intent(in) :: code
call delete_check_file()
call delete_result_file()
write(*,*) 'test_particle_config_check failed:', code
stop 1
end subroutine fail
end program test_particle_config_check