(git:6344619)
Loading...
Searching...
No Matches
qs_charge_mixing_unittest.F
Go to the documentation of this file.
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
9 USE ieee_arithmetic, ONLY: ieee_is_finite
10 USE kinds, ONLY: dp
18#if defined(__TBLITE)
19 USE mctc_env, ONLY: error_type
20 USE tblite_scf_mixer_type, ONLY: mixer_type
21 USE tblite_scc_mixer, ONLY: new_cp2k_tblite_mixer
22#endif
23
24 IMPLICIT NONE
25
26 REAL(kind=dp), DIMENSION(1, 1) :: charges
27 TYPE(mp_para_env_type), POINTER :: para_env
28 TYPE(mixing_storage_type), POINTER :: mixing_store
29
30 NULLIFY (mixing_store, para_env)
31 ALLOCATE (para_env)
32 CALL mp_world_init(para_env)
33
34 ALLOCATE (mixing_store)
35 mixing_store%ncall = 0
36 mixing_store%nbuffer = 4
37 mixing_store%n_simple_mix = 0
38 mixing_store%nskip_mixing = 0
39 mixing_store%alpha = 0.1_dp
40 mixing_store%broy_w0 = 0.01_dp
41 mixing_store%nat_local = 1
42 mixing_store%max_shell = 1
43 ALLOCATE (mixing_store%acharge(1, 1, 4), mixing_store%dacharge(1, 1, 4), &
44 mixing_store%dfbroy(1, 1, 4), mixing_store%ubroy(1, 1, 4), &
45 mixing_store%abroy(4, 4), mixing_store%wbroy(4), mixing_store%atlist(1))
46 mixing_store%acharge = 0.0_dp
47 mixing_store%dacharge = 0.0_dp
48 mixing_store%dfbroy = 0.0_dp
49 mixing_store%ubroy = 0.0_dp
50 mixing_store%abroy = 0.0_dp
51 mixing_store%wbroy = 0.0_dp
52 mixing_store%atlist = 1
53
54 charges = 1.0_dp
55 CALL charge_mixing(broyden_mixing_nr, mixing_store, charges, para_env, 1)
56 charges = 0.5_dp
57 CALL charge_mixing(broyden_mixing_nr, mixing_store, charges, para_env, 2)
58
59 ! Reproduce a degenerate Broyden history by supplying the previous residual again.
60 charges(1, 1) = mixing_store%acharge(1, 1, 2) - mixing_store%dacharge(1, 1, 1)
61 CALL charge_mixing(broyden_mixing_nr, mixing_store, charges, para_env, 3)
62
63 IF (.NOT. all(ieee_is_finite(charges))) THEN
64 error stop "Broyden mixing produced non-finite charges for identical residuals"
65 END IF
66 IF (.NOT. all(mixing_store%dfbroy(:, :, 1) == 0.0_dp)) THEN
67 error stop "Degenerate Broyden direction was not discarded"
68 END IF
69
70 CALL mixing_storage_release(mixing_store)
71 DEALLOCATE (mixing_store)
72#if defined(__TBLITE)
73 CALL test_tblite_history()
74#endif
75 DEALLOCATE (para_env)
77
78#if defined(__TBLITE)
79CONTAINS
80
81! **************************************************************************************************
82!> \brief A wrapped Broyden history must match a fresh replay of the retained input/output pairs.
83! **************************************************************************************************
84 SUBROUTINE test_tblite_history()
85 INTEGER, PARAMETER :: nstep = 16, nvar = 4
86
87 INTEGER :: config, i, j, memory, step
88 REAL(kind=dp) :: damping, max_weight, min_weight, omega0, &
89 weight_factor
90 REAL(kind=dp), DIMENSION(nvar) :: actual, expected
91 REAL(kind=dp), DIMENSION(nvar, nstep) :: qin, qout
92
93 CLASS(mixer_type), ALLOCATABLE :: mixer, replay
94 TYPE(error_type), ALLOCATABLE :: error
95
96 DO step = 1, nstep
97 DO i = 1, nvar
98 qin(i, step) = sin(0.37_dp*real(i*step, dp))
99 qout(i, step) = qin(i, step) + 0.05_dp*cos(0.21_dp*real(i + 2*step, dp))
100 END DO
101 END DO
102 DO config = 1, 2
103 damping = 0.4_dp
104 omega0 = 0.01_dp
105 min_weight = 1.0_dp
106 max_weight = 100000.0_dp
107 weight_factor = 0.01_dp
108 IF (config == 2) THEN
109 damping = 0.25_dp
110 omega0 = 0.1_dp
111 min_weight = 0.5_dp
112 max_weight = 3.0_dp
113 weight_factor = 0.2_dp
114 END IF
115 DO memory = 1, 4
116 CALL new_cp2k_tblite_mixer(mixer, memory, nvar, damping, omega0, min_weight, max_weight, weight_factor)
117 DO step = 1, nstep
118 CALL mixer%set(qin(:, step))
119 CALL mixer%diff(qout(:, step))
120 CALL mixer%next(error)
121 IF (ALLOCATED(error)) error stop "Wrapped tblite mixer failed"
122 CALL mixer%get(actual)
123
124 ! Replay at most memory + 1 pairs: this builds exactly the retained
125 ! secant directions without ever overwriting a history slot.
126 CALL new_cp2k_tblite_mixer(replay, memory, nvar, damping, omega0, min_weight, max_weight, weight_factor)
127 DO j = max(1, step - memory), step
128 CALL replay%set(qin(:, j))
129 CALL replay%diff(qout(:, j))
130 CALL replay%next(error)
131 IF (ALLOCATED(error)) error stop "Replayed tblite mixer failed"
132 END DO
133 CALL replay%get(expected)
134 IF (.NOT. all(ieee_is_finite(actual))) error stop "Non-finite wrapped tblite history"
135 IF (maxval(abs(actual - expected)) > 1.0e-10_dp) THEN
136 error stop "Wrapped tblite history differs from an unwrapped replay"
137 END IF
138 END DO
139 END DO
140 END DO
141
142 END SUBROUTINE test_tblite_history
143#endif
144
146! vim: set ts=3 sw=3 tw=132 :
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Interface to the message passing library MPI.
subroutine, public mp_world_init(mp_comm)
initializes the system default communicator
subroutine, public mp_world_finalize()
Finalize the system default communicator and MPI when CP2K owns MPI.
subroutine, public charge_mixing(mixing_method, mixing_store, charges, para_env, iter_count, scc_mixer, tblite_mixer_iterations, tblite_mixer_damping, tblite_mixer_memory, tblite_mixer_omega0, tblite_mixer_min_weight, tblite_mixer_max_weight, tblite_mixer_weight_factor)
Driver for TB SCC variable mixing, calls the requested method.
module that contains the definitions of the scf types
integer, parameter, public broyden_mixing_nr
subroutine, public mixing_storage_release(mixing_store)
releases a mixing_storage
CP2K-side tblite-compatible SCC Broyden mixer.
program qs_charge_mixing_unittest
stores all the informations relevant to an mpi environment