Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 2 : PROGRAM qs_ot_minimizer_unittest
9 2 : USE kinds, ONLY: dp
10 : USE qs_ot_minimizer, ONLY: broyden_history_restart_required,&
11 : cg_history_restart_required,&
12 : lbfgs_curvature_damping_shift,&
13 : lbfgs_history_restart_required,&
14 : lbfgs_response_secant_parameters,&
15 : lbfgs_step_restart_required,&
16 : ot_mermin_response_assess
17 :
18 : IMPLICIT NONE
19 :
20 : REAL(KIND=dp), PARAMETER :: tolerance = 1.0E-14_dp
21 :
22 : INTEGER :: i
23 : LOGICAL :: accepted
24 : REAL(KIND=dp) :: damping, measured, predicted, quality, ratio, &
25 : scale, sy, weight, yy
26 :
27 : ! Histories must restart when they cease to describe a usable descent step.
28 2 : IF (.NOT. broyden_history_restart_required(.TRUE., .FALSE., .TRUE.)) THEN
29 0 : ERROR STOP "Broyden retained a non-descent history"
30 : END IF
31 2 : IF (broyden_history_restart_required(.FALSE., .TRUE., .TRUE.)) THEN
32 0 : ERROR STOP "Broyden discarded a descent history"
33 : END IF
34 2 : IF (.NOT. cg_history_restart_required(.TRUE., 0.5_dp + 32.0_dp*EPSILON(1.0_dp), 0.5_dp)) THEN
35 0 : ERROR STOP "CG retained unresolved free-energy history"
36 : END IF
37 2 : IF (cg_history_restart_required(.TRUE., 0.5_dp + 256.0_dp*EPSILON(1.0_dp), 0.5_dp)) THEN
38 0 : ERROR STOP "CG discarded resolved free-energy history"
39 : END IF
40 2 : IF (lbfgs_history_restart_required(100.0_dp, 1.0_dp)) THEN
41 0 : ERROR STOP "L-BFGS restarted at the gradient-growth threshold"
42 : END IF
43 2 : IF (.NOT. lbfgs_history_restart_required(100.0001_dp, 1.0_dp)) THEN
44 0 : ERROR STOP "L-BFGS missed excessive gradient growth"
45 : END IF
46 2 : IF (lbfgs_step_restart_required(1.0E-6_dp*0.1_dp, 0.1_dp)) THEN
47 0 : ERROR STOP "L-BFGS restarted at the step-collapse threshold"
48 : END IF
49 2 : IF (.NOT. lbfgs_step_restart_required(0.999E-6_dp*0.1_dp, 0.1_dp)) THEN
50 0 : ERROR STOP "L-BFGS retained a collapsed step"
51 : END IF
52 :
53 : ! Damping must satisfy the curvature bound for weak and negative curvature.
54 2 : IF (lbfgs_curvature_damping_shift(0.6_dp, 1.0_dp, 1.0_dp, 0.5_dp) /= 0.0_dp) THEN
55 0 : ERROR STOP "Well-conditioned L-BFGS curvature was damped"
56 : END IF
57 4 : DO i = -1, 1, 2
58 4 : sy = REAL(i, KIND=dp)*0.1_dp
59 4 : damping = lbfgs_curvature_damping_shift(sy, 1.0_dp, 1.0_dp, 0.5_dp)
60 4 : yy = 1.0_dp + 2.0_dp*damping*sy + damping**2
61 6 : IF (.NOT. (damping > 0.0_dp .AND. sy + damping > 0.5_dp*SQRT(yy))) THEN
62 0 : ERROR STOP "L-BFGS damping failed the relative-curvature bound"
63 : END IF
64 : END DO
65 :
66 : ! Keep the norm matching, Powell bound, and rejected-secant behavior together.
67 2 : CALL lbfgs_response_secant_parameters(1.0_dp, 4.0_dp, 2.0_dp, 9.0_dp, scale, weight, accepted)
68 2 : IF (.NOT. (accepted .AND. ABS(scale - 1.5_dp) <= tolerance .AND. weight == 1.0_dp)) THEN
69 0 : ERROR STOP "L-BFGS response secant changed the H0 direction norm"
70 : END IF
71 2 : CALL lbfgs_response_secant_parameters(0.01_dp, 4.0_dp, 2.0_dp, 9.0_dp, scale, weight, accepted)
72 2 : sy = weight*scale*0.01_dp + (1.0_dp - weight)*2.0_dp
73 2 : IF (.NOT. (accepted .AND. ABS(sy - 0.4_dp) <= tolerance .AND. weight > 0.0_dp .AND. weight < 1.0_dp)) THEN
74 0 : ERROR STOP "L-BFGS response secant failed the Powell curvature bound"
75 : END IF
76 2 : CALL lbfgs_response_secant_parameters(-1.0_dp, 4.0_dp, 2.0_dp, 9.0_dp, scale, weight, accepted)
77 2 : IF (accepted .OR. weight /= 0.0_dp) ERROR STOP "Rejected response secant changed H0"
78 :
79 : ! A useful Mermin response must lower free energy without growing the residual.
80 : CALL ot_mermin_response_assess( &
81 : 10.0_dp, 9.6_dp, 1.0_dp, 0.8_dp, 1.0_dp, 1.0_dp, 0.5_dp, 0.1_dp, &
82 2 : predicted, measured, quality, ratio, accepted)
83 2 : IF (.NOT. (accepted .AND. ABS(predicted - 0.375_dp) <= tolerance .AND. &
84 : ABS(measured - 0.4_dp) <= tolerance .AND. ABS(quality - 0.4_dp/0.375_dp) <= tolerance .AND. &
85 0 : ABS(ratio - 0.8_dp) <= tolerance)) ERROR STOP "Useful Mermin response was rejected"
86 : CALL ot_mermin_response_assess( &
87 : 10.0_dp, 9.6_dp, 1.0_dp, 1.5_dp, 1.0_dp, 1.0_dp, 0.5_dp, 0.1_dp, &
88 2 : predicted, measured, quality, ratio, accepted)
89 2 : IF (accepted) ERROR STOP "Residual-growing Mermin response was accepted"
90 :
91 2 : END PROGRAM qs_ot_minimizer_unittest
|