Files
FemLite/tests/module/test_nonlinear_lifecycle.f90
shenw ee18a9b757
FemLite CI / build-test (push) Has been cancelled
Release FemLite 0.6.2 stability hardening
2026-07-02 08:26:46 +08:00

484 lines
20 KiB
Fortran

program test_nonlinear_lifecycle
use fem_precision
use fem_error
use fem_control
use fem_api, only: fem_initialize, fem_finalize
use fem_api_diagnostics, only: fem_record_convergence, &
fem_get_cutback_reason, fem_get_nl_lifecycle_snapshot, &
fem_get_nl_lifecycle_summary
use fem_nonlinear_lifecycle
use fem_output_csv, only: output_csv_set_directory, &
output_csv_write_nl_lifecycle
implicit none
call check_event_names()
call check_empty_snapshot()
call check_recorded_snapshot()
call check_begin_increment()
call check_audit_core()
call check_cutback_reason_names()
call check_summary_core()
call check_lifecycle_report_empty()
call check_api_snapshot()
call check_api_summary()
contains
subroutine check_event_names()
character(len=64) :: name
call nl_lifecycle_event_name(NLC_EVENT_NONE, name)
if (trim(name) .ne. 'none') call fail(10)
call nl_lifecycle_event_name(NLC_EVENT_INCREMENT_BEGIN, name)
if (trim(name) .ne. 'increment-begin') call fail(11)
call nl_lifecycle_event_name(NLC_EVENT_TRIAL_BEGIN, name)
if (trim(name) .ne. 'trial-begin') call fail(12)
call nl_lifecycle_event_name(NLC_EVENT_ITERATION_BEGIN, name)
if (trim(name) .ne. 'iteration-begin') call fail(120)
call nl_lifecycle_event_name(NLC_EVENT_ITERATION_RESULT, name)
if (trim(name) .ne. 'iteration-result') call fail(13)
call nl_lifecycle_event_name(NLC_EVENT_ACCEPT, name)
if (trim(name) .ne. 'accept') call fail(14)
call nl_lifecycle_event_name(NLC_EVENT_REJECT, name)
if (trim(name) .ne. 'reject') call fail(15)
call nl_lifecycle_event_name(NLC_EVENT_CUTBACK, name)
if (trim(name) .ne. 'cutback') call fail(16)
call nl_lifecycle_event_name(NLC_EVENT_COMMIT, name)
if (trim(name) .ne. 'commit') call fail(17)
call nl_lifecycle_event_name(NLC_EVENT_ROLLBACK, name)
if (trim(name) .ne. 'rollback') call fail(18)
call nl_lifecycle_event_name(-999, name)
if (trim(name) .ne. 'unknown') call fail(19)
end subroutine check_event_names
subroutine check_empty_snapshot()
integer :: step
integer :: increment
integer :: iteration
integer :: event_id
integer :: reason_id
character(len=64) :: event_name
character(len=64) :: reason_name
real(rk) :: load_factor
real(rk) :: arc_residual
logical :: path_appended
call nl_lifecycle_reset()
call nl_lifecycle_get_snapshot(step, increment, iteration, event_id, &
event_name, reason_id, reason_name)
call nl_lifecycle_get_arc_state(load_factor, arc_residual, path_appended)
if (nl_lifecycle_get_event_count() .ne. 0) call fail(29)
if (step .ne. 0) call fail(30)
if (increment .ne. 0) call fail(31)
if (iteration .ne. 0) call fail(32)
if (event_id .ne. NLC_EVENT_NONE) call fail(33)
if (trim(event_name) .ne. 'none') call fail(34)
if (reason_id .ne. NFAIL_NONE) call fail(35)
if (trim(reason_name) .ne. 'none') call fail(36)
if (abs(load_factor) .gt. 1.0e-14_rk) call fail(37)
if (abs(arc_residual) .gt. 1.0e-14_rk) call fail(38)
if (path_appended) call fail(39)
end subroutine check_empty_snapshot
subroutine check_recorded_snapshot()
integer :: step
integer :: increment
integer :: iteration
integer :: event_id
integer :: reason_id
character(len=64) :: event_name
character(len=64) :: reason_name
call nl_lifecycle_record_event(2, 3, 4, NLC_EVENT_TRIAL_BEGIN, &
NFAIL_LINE_SEARCH)
if (nl_lifecycle_get_event_count() .ne. 1) call fail(49)
call nl_lifecycle_get_snapshot(step, increment, iteration, event_id, &
event_name, reason_id, reason_name)
if (step .ne. 2) call fail(50)
if (increment .ne. 3) call fail(51)
if (iteration .ne. 4) call fail(52)
if (event_id .ne. NLC_EVENT_TRIAL_BEGIN) call fail(53)
if (trim(event_name) .ne. 'trial-begin') call fail(54)
if (reason_id .ne. NFAIL_LINE_SEARCH) call fail(55)
if (trim(reason_name) .ne. 'line-search-failure') call fail(56)
call nl_lifecycle_get_event(1, step, increment, iteration, event_id, &
event_name, reason_id, reason_name)
if (step .ne. 2) call fail(57)
if (increment .ne. 3) call fail(58)
if (iteration .ne. 4) call fail(59)
if (event_id .ne. NLC_EVENT_TRIAL_BEGIN) call fail(60)
if (trim(event_name) .ne. 'trial-begin') call fail(61)
end subroutine check_recorded_snapshot
subroutine check_begin_increment()
integer :: step
integer :: increment
integer :: iteration
integer :: event_id
integer :: reason_id
character(len=64) :: event_name
character(len=64) :: reason_name
call nl_lifecycle_begin_increment(5, 6)
if (nl_lifecycle_get_event_count() .ne. 2) call fail(69)
call nl_lifecycle_get_snapshot(step, increment, iteration, event_id, &
event_name, reason_id, reason_name)
if (step .ne. 5) call fail(70)
if (increment .ne. 6) call fail(71)
if (iteration .ne. 0) call fail(72)
if (event_id .ne. NLC_EVENT_INCREMENT_BEGIN) call fail(73)
if (trim(event_name) .ne. 'increment-begin') call fail(74)
if (reason_id .ne. NFAIL_NONE) call fail(75)
if (trim(reason_name) .ne. 'none') call fail(76)
end subroutine check_begin_increment
subroutine check_audit_core()
integer :: step
integer :: increment
integer :: iteration
integer :: event_id
integer :: reason_id
integer :: audit_status
integer :: begin_trial_count
integer :: rollback_requested_count
integer :: rollback_ok_count
integer :: rollback_failed_count
integer :: commit_ok_count
character(len=64) :: event_name
character(len=64) :: reason_name
character(len=64) :: audit_name
call nl_lifecycle_reset()
call nl_lifecycle_get_snapshot(step, increment, iteration, event_id, &
event_name, reason_id, reason_name, audit_status, audit_name)
if (audit_status .ne. NLC_AUDIT_NONE) call fail(80)
if (trim(audit_name) .ne. 'none') call fail(81)
call nl_lifecycle_audit_status_name(NLC_AUDIT_ROLLBACK_FAILED, &
audit_name)
if (trim(audit_name) .ne. 'rollback-failed') call fail(82)
call nl_lifecycle_audit_status_name(-999, audit_name)
if (trim(audit_name) .ne. 'unknown') call fail(83)
call nl_lifecycle_record_audit(NLC_AUDIT_BEGIN_TRIAL)
call nl_lifecycle_record_audit(NLC_AUDIT_ROLLBACK_REQUESTED)
call nl_lifecycle_record_audit(NLC_AUDIT_ROLLBACK_OK)
call nl_lifecycle_record_audit(NLC_AUDIT_ROLLBACK_FAILED)
call nl_lifecycle_record_audit(NLC_AUDIT_COMMIT_OK)
call nl_lifecycle_get_audit(audit_status, audit_name, &
begin_trial_count, rollback_requested_count, rollback_ok_count, &
rollback_failed_count, commit_ok_count)
if (audit_status .ne. NLC_AUDIT_COMMIT_OK) call fail(84)
if (trim(audit_name) .ne. 'commit-ok') call fail(85)
if (begin_trial_count .ne. 1) call fail(86)
if (rollback_requested_count .ne. 1) call fail(87)
if (rollback_ok_count .ne. 1) call fail(88)
if (rollback_failed_count .ne. 1) call fail(89)
if (commit_ok_count .ne. 1) call fail(900)
call nl_lifecycle_get_snapshot(step, increment, iteration, event_id, &
event_name, reason_id, reason_name, audit_status, audit_name)
if (audit_status .ne. NLC_AUDIT_COMMIT_OK) call fail(901)
if (trim(audit_name) .ne. 'commit-ok') call fail(902)
end subroutine check_audit_core
subroutine check_cutback_reason_names()
character(len=64) :: name
call cutback_reason_name(CUTBACK_REASON_NONE, name)
if (trim(name) .ne. 'none') call fail(950)
call cutback_reason_name(CUTBACK_REASON_NONCONVERGENCE, name)
if (trim(name) .ne. 'nonlinear-nonconvergence') call fail(951)
call cutback_reason_name(CUTBACK_REASON_MIN_STEP, name)
if (trim(name) .ne. 'minimum-step-exhausted') call fail(952)
call cutback_reason_name(CUTBACK_REASON_LINEAR_SOLVE, name)
if (trim(name) .ne. 'linear-solve-failure') call fail(953)
call cutback_reason_name(-999, name)
if (trim(name) .ne. 'unknown') call fail(954)
if (cutback_reason_from_failure(CONV_FAIL_CUTBACK_LIMIT, &
NFAIL_NONE) .ne. CUTBACK_REASON_MIN_STEP) call fail(955)
if (cutback_reason_from_failure(CONV_FAIL_NONE, &
NFAIL_LINEAR_SOLVE) .ne. CUTBACK_REASON_LINEAR_SOLVE) call fail(956)
if (cutback_reason_from_failure(CONV_FAIL_NONCONVERGENCE, &
NFAIL_NONE) .ne. CUTBACK_REASON_NONCONVERGENCE) call fail(957)
end subroutine check_cutback_reason_names
subroutine check_summary_core()
integer :: step
integer :: increment
integer :: iteration
integer :: event_id
integer :: reason_id
integer :: audit_status
integer :: begin_trial_count
integer :: rollback_requested_count
integer :: rollback_ok_count
integer :: rollback_failed_count
integer :: commit_ok_count
integer :: accept_count
integer :: reject_count
integer :: cutback_count
character(len=64) :: event_name
character(len=64) :: reason_name
character(len=64) :: audit_name
call nl_lifecycle_reset()
call nl_lifecycle_get_summary(step, increment, iteration, event_id, &
event_name, reason_id, reason_name, audit_status, audit_name, &
begin_trial_count, rollback_requested_count, rollback_ok_count, &
rollback_failed_count, commit_ok_count, accept_count, reject_count, &
cutback_count)
if (step .ne. 0) call fail(1000)
if (event_id .ne. NLC_EVENT_NONE) call fail(1001)
if (trim(event_name) .ne. 'none') call fail(1002)
if (audit_status .ne. NLC_AUDIT_NONE) call fail(1003)
if (accept_count .ne. 0) call fail(1004)
if (reject_count .ne. 0) call fail(1005)
if (cutback_count .ne. 0) call fail(1006)
call nl_lifecycle_record_event(1, 2, 0, NLC_EVENT_INCREMENT_BEGIN, &
NFAIL_NONE)
call nl_lifecycle_record_event(1, 2, 1, NLC_EVENT_ACCEPT, NFAIL_NONE)
call nl_lifecycle_record_event(1, 3, 1, NLC_EVENT_CUTBACK, &
NFAIL_NONCONVERGENCE)
call nl_lifecycle_record_event(1, 3, 1, NLC_EVENT_REJECT, &
NFAIL_MIN_STEP)
call nl_lifecycle_record_audit(NLC_AUDIT_BEGIN_TRIAL)
call nl_lifecycle_record_audit(NLC_AUDIT_ROLLBACK_REQUESTED)
call nl_lifecycle_get_summary(step, increment, iteration, event_id, &
event_name, reason_id, reason_name, audit_status, audit_name, &
begin_trial_count, rollback_requested_count, rollback_ok_count, &
rollback_failed_count, commit_ok_count, accept_count, reject_count, &
cutback_count)
if (step .ne. 1) call fail(1010)
if (increment .ne. 3) call fail(1011)
if (iteration .ne. 1) call fail(1012)
if (event_id .ne. NLC_EVENT_REJECT) call fail(1013)
if (trim(event_name) .ne. 'reject') call fail(1014)
if (reason_id .ne. NFAIL_MIN_STEP) call fail(1015)
if (trim(reason_name) .ne. 'minimum-step-exhausted') call fail(1016)
if (audit_status .ne. NLC_AUDIT_ROLLBACK_REQUESTED) call fail(1017)
if (trim(audit_name) .ne. 'rollback-requested') call fail(1018)
if (begin_trial_count .ne. 1) call fail(1019)
if (rollback_requested_count .ne. 1) call fail(1020)
if (rollback_ok_count .ne. 0) call fail(1021)
if (rollback_failed_count .ne. 0) call fail(1022)
if (commit_ok_count .ne. 0) call fail(1023)
if (accept_count .ne. 1) call fail(1024)
if (reject_count .ne. 1) call fail(1025)
if (cutback_count .ne. 1) call fail(1026)
end subroutine check_summary_core
subroutine check_lifecycle_report_empty()
integer :: ierr
integer :: ios
integer :: unit_id
character(len=2048) :: header
character(len=2048) :: row_line
call nl_lifecycle_reset()
call output_csv_set_directory( &
'build-codex-gcc/test_nonlinear_lifecycle_report_empty', ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1030)
call output_csv_write_nl_lifecycle(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1031)
unit_id = 93
open(unit=unit_id, &
file='build-codex-gcc/test_nonlinear_lifecycle_report_empty/nonlinear_lifecycle.csv', &
status='old', action='read', iostat=ios)
if (ios .ne. 0) call fail(1032)
read(unit_id,'(A)',iostat=ios) header
if (ios .ne. 0) call fail(1033)
read(unit_id,'(A)',iostat=ios) row_line
close(unit_id)
if (ios .ne. 0) call fail(1034)
call check_lifecycle_report_header(header, 1035)
if (csv_life_fields(header) .ne. 38) call fail(1046)
if (csv_life_fields(row_line) .ne. 38) call fail(1047)
if (index(row_line, ',none,') .le. 0) call fail(1045)
end subroutine check_lifecycle_report_empty
subroutine check_api_snapshot()
integer :: ierr
integer :: step
integer :: increment
integer :: iteration
integer :: event_id
integer :: reason_id
character(len=64) :: event_name
character(len=64) :: reason_name
call fem_initialize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(90)
call fem_get_nl_lifecycle_snapshot(step, increment, iteration, &
event_id, event_name, reason_id, reason_name, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(91)
if (step .ne. 0) call fail(92)
if (increment .ne. 0) call fail(93)
if (iteration .ne. 0) call fail(94)
if (event_id .ne. NLC_EVENT_NONE) call fail(95)
if (trim(event_name) .ne. 'none') call fail(96)
call nl_lifecycle_record_event(7, 8, 9, NLC_EVENT_CUTBACK, &
NFAIL_MIN_STEP)
call fem_get_nl_lifecycle_snapshot(step, increment, iteration, &
event_id, event_name, reason_id, reason_name, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(97)
if (step .ne. 7) call fail(98)
if (increment .ne. 8) call fail(99)
if (iteration .ne. 9) call fail(100)
if (event_id .ne. NLC_EVENT_CUTBACK) call fail(101)
if (trim(event_name) .ne. 'cutback') call fail(102)
if (reason_id .ne. NFAIL_MIN_STEP) call fail(103)
if (trim(reason_name) .ne. 'minimum-step-exhausted') call fail(104)
call fem_finalize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(105)
end subroutine check_api_snapshot
subroutine check_api_summary()
integer :: ierr
integer :: step
integer :: increment
integer :: iteration
integer :: event_id
integer :: reason_id
integer :: convergence_status
integer :: failure_code
integer :: failure_reason_id
integer :: material_fail_count
integer :: linear_matrix_class
integer :: linear_status
integer :: audit_status
integer :: begin_trial_count
integer :: rollback_requested_count
integer :: rollback_ok_count
integer :: rollback_failed_count
integer :: commit_ok_count
integer :: accept_count
integer :: reject_count
integer :: cutback_count
integer :: cutback_reason_id
real(rk) :: total_residual
real(rk) :: force_residual
real(rk) :: contact_residual
real(rk) :: constraint_residual
real(rk) :: arc_residual
character(len=64) :: event_name
character(len=64) :: reason_name
character(len=64) :: failure_reason_name
character(len=64) :: audit_name
character(len=64) :: cutback_reason
call fem_initialize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1100)
call nl_lifecycle_reset()
call nl_lifecycle_record_event(4, 5, 6, NLC_EVENT_ACCEPT, NFAIL_NONE)
call nl_lifecycle_record_audit(NLC_AUDIT_COMMIT_OK)
call fem_record_convergence(5, 6, 0.25_rk, 0.125_rk, 0.1_rk, &
0.5_rk, .true., 0.025_rk, 0.01_rk)
call fem_get_cutback_reason(cutback_reason_id, cutback_reason, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1127)
if (cutback_reason_id .ne. CUTBACK_REASON_NONE) call fail(1128)
if (trim(cutback_reason) .ne. 'none') call fail(1129)
call fem_get_nl_lifecycle_summary(step, increment, iteration, event_id, &
event_name, reason_id, reason_name, convergence_status, failure_code, &
failure_reason_id, failure_reason_name, total_residual, &
force_residual, contact_residual, constraint_residual, arc_residual, &
material_fail_count, linear_matrix_class, linear_status, audit_status, &
audit_name, begin_trial_count, rollback_requested_count, &
rollback_ok_count, rollback_failed_count, commit_ok_count, &
accept_count, reject_count, cutback_count, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1101)
if (step .ne. 4) call fail(1102)
if (increment .ne. 5) call fail(1103)
if (iteration .ne. 6) call fail(1104)
if (event_id .ne. NLC_EVENT_ACCEPT) call fail(1105)
if (trim(event_name) .ne. 'accept') call fail(1106)
if (reason_id .ne. NFAIL_NONE) call fail(1107)
if (convergence_status .ne. CONVERGENCE_STATUS_CONVERGED) &
call fail(1108)
if (failure_code .ne. CONV_FAIL_NONE) call fail(1109)
if (failure_reason_id .ne. NFAIL_NONE) call fail(1110)
if (trim(failure_reason_name) .ne. 'none') call fail(1111)
if (abs(total_residual - 0.25_rk) .gt. 1.0e-14_rk) call fail(1112)
if (abs(force_residual - 0.25_rk) .gt. 1.0e-14_rk) call fail(1113)
if (abs(contact_residual - 0.01_rk) .gt. 1.0e-14_rk) call fail(1114)
if (abs(constraint_residual) .gt. 1.0e-14_rk) call fail(1115)
if (abs(arc_residual) .gt. 1.0e-14_rk) call fail(1116)
if (material_fail_count .ne. 0) call fail(1117)
if (linear_matrix_class .lt. 1) call fail(1118)
if (linear_status .lt. 0) call fail(1119)
if (audit_status .ne. NLC_AUDIT_COMMIT_OK) call fail(1120)
if (trim(audit_name) .ne. 'commit-ok') call fail(1121)
if (commit_ok_count .ne. 1) call fail(1122)
if (accept_count .ne. 1) call fail(1123)
if (reject_count .ne. 0) call fail(1124)
if (cutback_count .ne. 0) call fail(1125)
call fem_finalize(ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1126)
end subroutine check_api_summary
subroutine check_lifecycle_report_header(header, code_base)
character(len=*), intent(in) :: header
integer, intent(in) :: code_base
if (index(header, 'event_id') .le. 0) call fail(code_base)
if (index(header, 'event_name') .le. 0) call fail(code_base + 1)
if (index(header, 'reason_id') .le. 0) call fail(code_base + 2)
if (index(header, 'convergence_status') .le. 0) call fail(code_base + 3)
if (index(header, 'cutback_reason_id') .le. 0) call fail(code_base + 4)
if (index(header, 'residual_total') .le. 0) call fail(code_base + 5)
if (index(header, 'linear_status') .le. 0) call fail(code_base + 6)
if (index(header, 'audit_status') .le. 0) call fail(code_base + 7)
if (index(header, 'accept_count') .le. 0) call fail(code_base + 8)
if (index(header, 'reject_count') .le. 0) call fail(code_base + 9)
if (index(header, 'failure_reason_id') .le. 0) call fail(code_base + 10)
if (index(header, 'failure_reason_name') .le. 0) call fail(code_base + 11)
if (index(header, 'cutback_reason_name') .le. 0) call fail(code_base + 12)
if (index(header, 'residual_force') .le. 0) call fail(code_base + 13)
if (index(header, 'residual_contact') .le. 0) call fail(code_base + 14)
if (index(header, 'residual_constraint') .le. 0) call fail(code_base + 15)
if (index(header, 'residual_arc') .le. 0) call fail(code_base + 16)
if (index(header, 'linear_matrix_class') .le. 0) call fail(code_base + 17)
if (index(header, 'linear_method') .le. 0) call fail(code_base + 18)
if (index(header, 'linear_precond') .le. 0) call fail(code_base + 19)
if (index(header, 'linear_iterations') .le. 0) call fail(code_base + 20)
if (index(header, 'linear_initial_residual') .le. 0) &
call fail(code_base + 21)
if (index(header, 'linear_final_residual') .le. 0) &
call fail(code_base + 22)
if (index(header, 'linear_relative_residual') .le. 0) &
call fail(code_base + 23)
if (index(header, 'linear_fail_class') .le. 0) call fail(code_base + 24)
if (index(header, 'audit_rollback_failed') .le. 0) &
call fail(code_base + 25)
end subroutine check_lifecycle_report_header
integer function csv_life_fields(line)
character(len=*), intent(in) :: line
integer :: i
integer :: n
n = len_trim(line)
if (n .le. 0) then
csv_life_fields = 0
return
end if
csv_life_fields = 1
do i = 1, n
if (line(i:i) .eq. ',') csv_life_fields = csv_life_fields + 1
end do
end function csv_life_fields
subroutine fail(code)
integer, intent(in) :: code
write(*,*) 'test_nonlinear_lifecycle failed:', code
stop 1
end subroutine fail
end program test_nonlinear_lifecycle