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 : ! **************************************************************************************************
9 : !> \brief interpolate the wavefunctions to speed up the convergence when
10 : !> doing MD
11 : !> \par History
12 : !> 12.2002 created [fawzi]
13 : !> 02.2005 wf_mol added [MI]
14 : !> \author fawzi
15 : ! **************************************************************************************************
16 : MODULE qs_wf_history_types
17 : USE cp_cfm_types, ONLY: cp_cfm_release,&
18 : cp_cfm_type
19 : USE cp_dbcsr_api, ONLY: dbcsr_deallocate_matrix,&
20 : dbcsr_p_type,&
21 : dbcsr_type
22 : USE cp_dbcsr_operations, ONLY: dbcsr_deallocate_matrix_set
23 : USE cp_fm_types, ONLY: cp_fm_release,&
24 : cp_fm_type
25 : USE kinds, ONLY: dp
26 : USE pw_types, ONLY: pw_c1d_gs_type,&
27 : pw_r3d_rs_type
28 : USE qs_rho_types, ONLY: qs_rho_release,&
29 : qs_rho_type
30 : #include "./base/base_uses.f90"
31 :
32 : IMPLICIT NONE
33 : PRIVATE
34 :
35 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
36 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_wf_history_types'
37 :
38 : PUBLIC :: qs_wf_mo_state_type, qs_wf_snapshot_type, &
39 : qs_wf_history_type, qs_wf_history_p_type
40 : PUBLIC :: wfi_retain, wfi_release, wfi_clear, wfi_get_snapshot
41 :
42 : ! **************************************************************************************************
43 : !> \brief stores scalar and occupation metadata for an MO set snapshot
44 : !> \param eigenvalues MO eigenvalues
45 : !> \param occupation_numbers MO occupation numbers
46 : !> \param homo index of highest occupied MO
47 : !> \param lfomo index of lowest fractional/empty MO
48 : !> \param uniform_occupation whether orbitals 1..homo share uniform occupation
49 : !> \param kTS electronic entropic energy contribution
50 : !> \param mu Fermi energy / chemical potential
51 : ! **************************************************************************************************
52 : TYPE qs_wf_mo_state_type
53 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: eigenvalues
54 : REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: occupation_numbers
55 : INTEGER :: homo = -1
56 : INTEGER :: lfomo = -1
57 : LOGICAL :: uniform_occupation = .FALSE.
58 : REAL(KIND=dp) :: kTS = 0.0_dp
59 : REAL(KIND=dp) :: mu = 0.0_dp
60 : END TYPE qs_wf_mo_state_type
61 :
62 : ! **************************************************************************************************
63 : !> \brief represent a past snapshot of the wavefunction.
64 : !> some elements might not be associated (to spare memory)
65 : !> depending on how the snapshot was taken
66 : !> \param wf the wavefunctions
67 : !> \param rho_r the density in r space
68 : !> \param rho_g the density in g space
69 : !> \param rho_ao the AO density matrices indexed by spin and real-space image
70 : !> \param overlap the overlap matrix
71 : !> \param wf_kp real k-point MO snapshots
72 : !> \param wf_kp_cfm complex k-point MO snapshots
73 : !> \param rho_frozen the frozen density structure
74 : !> \param mo_state Gamma-point MO eigenvalues and occupations per spin
75 : !> \param mo_state_kp k-point MO eigenvalues and occupations per local k-point and spin
76 : !> \param dt the time of the snapshot (wrf to te previous snapshot!)
77 : !> \par History
78 : !> 02.2003 created [fawzi]
79 : !> 02.2005 wf_mol added [MI]
80 : !> \author fawzi
81 : ! **************************************************************************************************
82 : TYPE qs_wf_snapshot_type
83 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: wf => NULL()
84 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r => NULL()
85 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g => NULL()
86 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao => NULL()
87 : TYPE(dbcsr_type), POINTER :: overlap => NULL()
88 : TYPE(cp_fm_type), DIMENSION(:, :), POINTER :: wf_kp => NULL()
89 : TYPE(cp_cfm_type), DIMENSION(:, :), POINTER :: wf_kp_cfm => NULL()
90 : TYPE(cp_cfm_type), DIMENSION(:), POINTER :: overlap_cfm_kp => NULL()
91 : INTEGER, DIMENSION(:, :), POINTER :: kp_pbc_shift => NULL()
92 : TYPE(qs_rho_type), POINTER :: rho_frozen => NULL()
93 : TYPE(qs_wf_mo_state_type), DIMENSION(:), ALLOCATABLE :: mo_state
94 : TYPE(qs_wf_mo_state_type), DIMENSION(:, :), ALLOCATABLE :: mo_state_kp
95 : REAL(KIND=dp) :: dt = 0.0_dp
96 : END TYPE qs_wf_snapshot_type
97 :
98 : ! **************************************************************************************************
99 : !> \brief pointer to a snapshot
100 : !> \param snapshot the pointer to the snapshot
101 : !> \par History
102 : !> 02.2003 created [fawzi]
103 : !> \author fawzi
104 : ! **************************************************************************************************
105 : TYPE qs_wf_snapshot_p_type
106 : TYPE(qs_wf_snapshot_type), POINTER :: snapshot => NULL()
107 : END TYPE qs_wf_snapshot_p_type
108 :
109 : ! **************************************************************************************************
110 : !> \brief keeps track of the previous wavefunctions and can extrapolate them
111 : !> for the next step of md
112 : !> \param ref_cont reference count (see doc/ReferenceCounting.html)
113 : !> \param memory_depth how many snapshots should be stored
114 : !> \param last_state_index index of the latest snapshot
115 : !> \param past_states array with the past states (index starts at
116 : !> last_state_index)
117 : !> \param interpolation_method_nr the tag of the method used to
118 : !> extrapolate the new start state for qs
119 : !> \param snapshot_count number of snapshot taken so far (cumulative,
120 : !> can be bigger than the history depth)
121 : !> \note
122 : !> use a linked list for the past states ?
123 : !> \par History
124 : !> 02.2003 created [fawzi]
125 : !> \author fawzi
126 : ! **************************************************************************************************
127 : TYPE qs_wf_history_type
128 : INTEGER :: ref_count = -1, memory_depth = -1, last_state_index = -1, &
129 : interpolation_method_nr = -1, snapshot_count = -1
130 : LOGICAL :: store_wf = .FALSE., store_rho_r = .FALSE., store_rho_g = .FALSE., &
131 : store_rho_ao = .FALSE., &
132 : store_overlap = .FALSE., store_wf_kp = .FALSE., store_overlap_kp = .FALSE., &
133 : store_frozen_density = .FALSE.
134 : TYPE(qs_wf_snapshot_p_type), DIMENSION(:), POINTER :: past_states => NULL()
135 : END TYPE qs_wf_history_type
136 :
137 : ! **************************************************************************************************
138 : !> \brief to create arrays of pointers to qs_wf_history_type
139 : !> \param wf_hist the pointer to the wf history
140 : !> \author fawzi
141 : ! **************************************************************************************************
142 : TYPE qs_wf_history_p_type
143 : TYPE(qs_wf_history_type), POINTER :: wf_history => NULL()
144 : END TYPE qs_wf_history_p_type
145 :
146 : CONTAINS
147 :
148 : ! **************************************************************************************************
149 : !> \brief releases a snapshot of a wavefunction (see doc/ReferenceCounting.html)
150 : !> \param snapshot the snapshot to release
151 : !> \par History
152 : !> 02.2003 created [fawzi]
153 : !> 02.2005 wf_mol added [MI]
154 : !> \author fawzi
155 : ! **************************************************************************************************
156 12446 : SUBROUTINE wfs_release(snapshot)
157 : TYPE(qs_wf_snapshot_type), INTENT(INOUT) :: snapshot
158 :
159 : INTEGER :: i, ikp, ispin
160 :
161 12446 : CALL cp_fm_release(snapshot%wf)
162 : ! snapshot%rho_r & snapshot%rho_g is deallocated in wfs_update
163 : ! of qs_wf_history_methods, in case you wonder about it.
164 12446 : IF (ASSOCIATED(snapshot%rho_ao)) THEN
165 62 : CALL dbcsr_deallocate_matrix_set(snapshot%rho_ao)
166 : END IF
167 12446 : IF (ASSOCIATED(snapshot%overlap)) THEN
168 9042 : CALL dbcsr_deallocate_matrix(snapshot%overlap)
169 : END IF
170 12446 : IF (ASSOCIATED(snapshot%wf_kp)) THEN
171 0 : CALL cp_fm_release(snapshot%wf_kp)
172 : END IF
173 12446 : IF (ASSOCIATED(snapshot%wf_kp_cfm)) THEN
174 6724 : DO ikp = 1, SIZE(snapshot%wf_kp_cfm, 1)
175 11702 : DO ispin = 1, SIZE(snapshot%wf_kp_cfm, 2)
176 9274 : CALL cp_cfm_release(snapshot%wf_kp_cfm(ikp, ispin))
177 : END DO
178 : END DO
179 2428 : DEALLOCATE (snapshot%wf_kp_cfm)
180 : END IF
181 12446 : IF (ASSOCIATED(snapshot%overlap_cfm_kp)) THEN
182 6714 : DO i = 1, SIZE(snapshot%overlap_cfm_kp)
183 6714 : CALL cp_cfm_release(snapshot%overlap_cfm_kp(i))
184 : END DO
185 2426 : DEALLOCATE (snapshot%overlap_cfm_kp)
186 : END IF
187 12446 : IF (ASSOCIATED(snapshot%kp_pbc_shift)) THEN
188 2428 : DEALLOCATE (snapshot%kp_pbc_shift)
189 : END IF
190 12446 : IF (ASSOCIATED(snapshot%rho_frozen)) THEN
191 4 : CALL qs_rho_release(snapshot%rho_frozen)
192 4 : DEALLOCATE (snapshot%rho_frozen)
193 : END IF
194 12446 : IF (ALLOCATED(snapshot%mo_state)) THEN
195 21583 : DEALLOCATE (snapshot%mo_state)
196 : END IF
197 12446 : IF (ALLOCATED(snapshot%mo_state_kp)) THEN
198 7406 : DEALLOCATE (snapshot%mo_state_kp)
199 : END IF
200 :
201 12446 : END SUBROUTINE wfs_release
202 :
203 : ! **************************************************************************************************
204 : !> \brief retains a wf history (see doc/ReferenceCounting.html)
205 : !> \param wf_history the wf_history to retain
206 : !> \par History
207 : !> 02.2003 created [fawzi]
208 : !> \author fawzi
209 : ! **************************************************************************************************
210 9810 : SUBROUTINE wfi_retain(wf_history)
211 : TYPE(qs_wf_history_type), POINTER :: wf_history
212 :
213 9810 : CPASSERT(ASSOCIATED(wf_history))
214 9810 : wf_history%ref_count = wf_history%ref_count + 1
215 :
216 9810 : END SUBROUTINE wfi_retain
217 :
218 : ! **************************************************************************************************
219 : !> \brief releases a wf_history of a wavefunction
220 : !> (see doc/ReferenceCounting.html)
221 : !> \param wf_history the wf_history to release
222 : !> \par History
223 : !> 02.2003 created [fawzi]
224 : !> \author fawzi
225 : ! **************************************************************************************************
226 28723 : SUBROUTINE wfi_release(wf_history)
227 : TYPE(qs_wf_history_type), POINTER :: wf_history
228 :
229 : INTEGER :: i
230 :
231 28723 : IF (ASSOCIATED(wf_history)) THEN
232 19224 : CPASSERT(wf_history%ref_count > 0)
233 19224 : wf_history%ref_count = wf_history%ref_count - 1
234 19224 : IF (wf_history%ref_count == 0) THEN
235 9414 : IF (ASSOCIATED(wf_history%past_states)) THEN
236 53699 : DO i = 1, SIZE(wf_history%past_states)
237 53699 : IF (ASSOCIATED(wf_history%past_states(i)%snapshot)) THEN
238 10464 : CALL wfs_release(wf_history%past_states(i)%snapshot)
239 10464 : DEALLOCATE (wf_history%past_states(i)%snapshot)
240 : END IF
241 : END DO
242 9414 : DEALLOCATE (wf_history%past_states)
243 : END IF
244 9414 : DEALLOCATE (wf_history)
245 : END IF
246 : END IF
247 28723 : NULLIFY (wf_history)
248 28723 : END SUBROUTINE wfi_release
249 :
250 : ! **************************************************************************************************
251 : !> \brief Clear stored wavefunction snapshots while preserving history settings.
252 : !> \param wf_history the wf_history to clear
253 : ! **************************************************************************************************
254 2332 : SUBROUTINE wfi_clear(wf_history)
255 : TYPE(qs_wf_history_type), POINTER :: wf_history
256 :
257 : INTEGER :: i
258 :
259 2332 : IF (ASSOCIATED(wf_history)) THEN
260 2332 : CPASSERT(wf_history%ref_count > 0)
261 2332 : IF (ASSOCIATED(wf_history%past_states)) THEN
262 12522 : DO i = 1, SIZE(wf_history%past_states)
263 12522 : IF (ASSOCIATED(wf_history%past_states(i)%snapshot)) THEN
264 1982 : CALL wfs_release(wf_history%past_states(i)%snapshot)
265 1982 : DEALLOCATE (wf_history%past_states(i)%snapshot)
266 1982 : NULLIFY (wf_history%past_states(i)%snapshot)
267 : END IF
268 : END DO
269 : END IF
270 2332 : wf_history%snapshot_count = 0
271 2332 : wf_history%last_state_index = 1
272 : END IF
273 :
274 2332 : END SUBROUTINE wfi_clear
275 :
276 : ! **************************************************************************************************
277 : !> \brief returns a snapshot, the first being the latest snapshot
278 : !> \param wf_history the plage where to get the snapshot
279 : !> \param wf_index the index of the snapshot you want
280 : !> \return ...
281 : !> \par History
282 : !> 12.2002 created [fawzi]
283 : !> \author fawzi
284 : ! **************************************************************************************************
285 85953 : FUNCTION wfi_get_snapshot(wf_history, wf_index) RESULT(res)
286 : TYPE(qs_wf_history_type), POINTER :: wf_history
287 : INTEGER, INTENT(in) :: wf_index
288 : TYPE(qs_wf_snapshot_type), POINTER :: res
289 :
290 85953 : NULLIFY (res)
291 :
292 85953 : CPASSERT(ASSOCIATED(wf_history))
293 85953 : CPASSERT(ASSOCIATED(wf_history%past_states))
294 85953 : IF (wf_index > wf_history%memory_depth .OR. wf_index > wf_history%snapshot_count) THEN
295 0 : CPABORT("Requested wavefunction index not available in history")
296 : END IF
297 : res => wf_history%past_states( &
298 : MODULO(wf_history%snapshot_count + 1 - wf_index, &
299 85953 : wf_history%memory_depth) + 1)%snapshot
300 85953 : END FUNCTION wfi_get_snapshot
301 :
302 0 : END MODULE qs_wf_history_types
|