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_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 represent a past snapshot of the wavefunction.
44 : !> some elements might not be associated (to spare memory)
45 : !> depending on how the snapshot was taken
46 : !> \param wf the wavefunctions
47 : !> \param rho_r the density in r space
48 : !> \param rho_g the density in g space
49 : !> \param rho_ao the density in ao space
50 : !> \param overlap the overlap matrix
51 : !> \param rho_frozen the frozen density structure
52 : !> \param dt the time of the snapshot (wrf to te previous snapshot!)
53 : !> \note
54 : !> keep track also of occupation numbers and energies?
55 : !> \par History
56 : !> 02.2003 created [fawzi]
57 : !> 02.2005 wf_mol added [MI]
58 : !> \author fawzi
59 : ! **************************************************************************************************
60 : TYPE qs_wf_snapshot_type
61 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: wf => NULL()
62 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r => NULL()
63 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g => NULL()
64 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao => NULL()
65 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp => NULL()
66 : TYPE(dbcsr_type), POINTER :: overlap => NULL()
67 : TYPE(cp_fm_type), DIMENSION(:, :, :), POINTER :: wf_kp => NULL()
68 : TYPE(cp_cfm_type), DIMENSION(:), POINTER :: overlap_cfm_kp => NULL()
69 : INTEGER, DIMENSION(:, :), POINTER :: kp_pbc_shift => NULL()
70 : TYPE(qs_rho_type), POINTER :: rho_frozen => NULL()
71 : REAL(KIND=dp) :: dt = 0.0_dp
72 : END TYPE qs_wf_snapshot_type
73 :
74 : ! **************************************************************************************************
75 : !> \brief pointer to a snapshot
76 : !> \param snapshot the pointer to the snapshot
77 : !> \par History
78 : !> 02.2003 created [fawzi]
79 : !> \author fawzi
80 : ! **************************************************************************************************
81 : TYPE qs_wf_snapshot_p_type
82 : TYPE(qs_wf_snapshot_type), POINTER :: snapshot => NULL()
83 : END TYPE qs_wf_snapshot_p_type
84 :
85 : ! **************************************************************************************************
86 : !> \brief keeps track of the previous wavefunctions and can extrapolate them
87 : !> for the next step of md
88 : !> \param ref_cont reference count (see doc/ReferenceCounting.html)
89 : !> \param memory_depth how many snapshots should be stored
90 : !> \param last_state_index index of the latest snapshot
91 : !> \param past_states array with the past states (index starts at
92 : !> last_state_index)
93 : !> \param interpolation_method_nr the tag of the method used to
94 : !> extrapolate the new start state for qs
95 : !> \param snapshot_count number of snapshot taken so far (cumulative,
96 : !> can be bigger than the history depth)
97 : !> \note
98 : !> use a linked list for the past states ?
99 : !> \par History
100 : !> 02.2003 created [fawzi]
101 : !> \author fawzi
102 : ! **************************************************************************************************
103 : TYPE qs_wf_history_type
104 : INTEGER :: ref_count = -1, memory_depth = -1, last_state_index = -1, &
105 : interpolation_method_nr = -1, snapshot_count = -1
106 : LOGICAL :: store_wf = .FALSE., store_rho_r = .FALSE., store_rho_g = .FALSE., &
107 : store_rho_ao = .FALSE., store_rho_ao_kp = .FALSE., &
108 : store_overlap = .FALSE., store_wf_kp = .FALSE., store_overlap_kp = .FALSE., &
109 : store_frozen_density = .FALSE.
110 : TYPE(qs_wf_snapshot_p_type), DIMENSION(:), POINTER :: past_states => NULL()
111 : END TYPE qs_wf_history_type
112 :
113 : ! **************************************************************************************************
114 : !> \brief to create arrays of pointers to qs_wf_history_type
115 : !> \param wf_hist the pointer to the wf history
116 : !> \author fawzi
117 : ! **************************************************************************************************
118 : TYPE qs_wf_history_p_type
119 : TYPE(qs_wf_history_type), POINTER :: wf_history => NULL()
120 : END TYPE qs_wf_history_p_type
121 :
122 : CONTAINS
123 :
124 : ! **************************************************************************************************
125 : !> \brief releases a snapshot of a wavefunction (see doc/ReferenceCounting.html)
126 : !> \param snapshot the snapshot to release
127 : !> \par History
128 : !> 02.2003 created [fawzi]
129 : !> 02.2005 wf_mol added [MI]
130 : !> \author fawzi
131 : ! **************************************************************************************************
132 14010 : SUBROUTINE wfs_release(snapshot)
133 : TYPE(qs_wf_snapshot_type), INTENT(INOUT) :: snapshot
134 :
135 : INTEGER :: i, ic, ikp, ispin
136 :
137 14010 : CALL cp_fm_release(snapshot%wf)
138 : ! snapshot%rho_r & snapshot%rho_g is deallocated in wfs_update
139 : ! of qs_wf_history_methods, in case you wonder about it.
140 14010 : IF (ASSOCIATED(snapshot%rho_ao)) THEN
141 68 : CALL dbcsr_deallocate_matrix_set(snapshot%rho_ao)
142 : END IF
143 14010 : IF (ASSOCIATED(snapshot%rho_ao_kp)) THEN
144 12 : CALL dbcsr_deallocate_matrix_set(snapshot%rho_ao_kp)
145 : END IF
146 14010 : IF (ASSOCIATED(snapshot%overlap)) THEN
147 10736 : CALL dbcsr_deallocate_matrix(snapshot%overlap)
148 : END IF
149 14010 : IF (ASSOCIATED(snapshot%wf_kp)) THEN
150 6352 : DO ikp = 1, SIZE(snapshot%wf_kp, 1)
151 14742 : DO ic = 1, SIZE(snapshot%wf_kp, 2)
152 21692 : DO ispin = 1, SIZE(snapshot%wf_kp, 3)
153 17496 : CALL cp_fm_release(snapshot%wf_kp(ikp, ic, ispin))
154 : END DO
155 : END DO
156 : END DO
157 2156 : DEALLOCATE (snapshot%wf_kp)
158 : END IF
159 14010 : IF (ASSOCIATED(snapshot%overlap_cfm_kp)) THEN
160 6342 : DO i = 1, SIZE(snapshot%overlap_cfm_kp)
161 6342 : CALL cp_cfm_release(snapshot%overlap_cfm_kp(i))
162 : END DO
163 2154 : DEALLOCATE (snapshot%overlap_cfm_kp)
164 : END IF
165 14010 : IF (ASSOCIATED(snapshot%kp_pbc_shift)) THEN
166 2156 : DEALLOCATE (snapshot%kp_pbc_shift)
167 : END IF
168 14010 : IF (ASSOCIATED(snapshot%rho_frozen)) THEN
169 4 : CALL qs_rho_release(snapshot%rho_frozen)
170 4 : DEALLOCATE (snapshot%rho_frozen)
171 : END IF
172 :
173 14010 : END SUBROUTINE wfs_release
174 :
175 : ! **************************************************************************************************
176 : !> \brief retains a wf history (see doc/ReferenceCounting.html)
177 : !> \param wf_history the wf_history to retain
178 : !> \par History
179 : !> 02.2003 created [fawzi]
180 : !> \author fawzi
181 : ! **************************************************************************************************
182 8832 : SUBROUTINE wfi_retain(wf_history)
183 : TYPE(qs_wf_history_type), POINTER :: wf_history
184 :
185 8832 : CPASSERT(ASSOCIATED(wf_history))
186 8832 : wf_history%ref_count = wf_history%ref_count + 1
187 :
188 8832 : END SUBROUTINE wfi_retain
189 :
190 : ! **************************************************************************************************
191 : !> \brief releases a wf_history of a wavefunction
192 : !> (see doc/ReferenceCounting.html)
193 : !> \param wf_history the wf_history to release
194 : !> \par History
195 : !> 02.2003 created [fawzi]
196 : !> \author fawzi
197 : ! **************************************************************************************************
198 25707 : SUBROUTINE wfi_release(wf_history)
199 : TYPE(qs_wf_history_type), POINTER :: wf_history
200 :
201 : INTEGER :: i
202 :
203 25707 : IF (ASSOCIATED(wf_history)) THEN
204 17268 : CPASSERT(wf_history%ref_count > 0)
205 17268 : wf_history%ref_count = wf_history%ref_count - 1
206 17268 : IF (wf_history%ref_count == 0) THEN
207 8436 : IF (ASSOCIATED(wf_history%past_states)) THEN
208 48215 : DO i = 1, SIZE(wf_history%past_states)
209 48215 : IF (ASSOCIATED(wf_history%past_states(i)%snapshot)) THEN
210 12272 : CALL wfs_release(wf_history%past_states(i)%snapshot)
211 12272 : DEALLOCATE (wf_history%past_states(i)%snapshot)
212 : END IF
213 : END DO
214 8436 : DEALLOCATE (wf_history%past_states)
215 : END IF
216 8436 : DEALLOCATE (wf_history)
217 : END IF
218 : END IF
219 25707 : NULLIFY (wf_history)
220 25707 : END SUBROUTINE wfi_release
221 :
222 : ! **************************************************************************************************
223 : !> \brief Clear stored wavefunction snapshots while preserving history settings.
224 : !> \param wf_history the wf_history to clear
225 : ! **************************************************************************************************
226 2112 : SUBROUTINE wfi_clear(wf_history)
227 : TYPE(qs_wf_history_type), POINTER :: wf_history
228 :
229 : INTEGER :: i
230 :
231 2112 : IF (ASSOCIATED(wf_history)) THEN
232 2112 : CPASSERT(wf_history%ref_count > 0)
233 2112 : IF (ASSOCIATED(wf_history%past_states)) THEN
234 11152 : DO i = 1, SIZE(wf_history%past_states)
235 11152 : IF (ASSOCIATED(wf_history%past_states(i)%snapshot)) THEN
236 1738 : CALL wfs_release(wf_history%past_states(i)%snapshot)
237 1738 : DEALLOCATE (wf_history%past_states(i)%snapshot)
238 1738 : NULLIFY (wf_history%past_states(i)%snapshot)
239 : END IF
240 : END DO
241 : END IF
242 2112 : wf_history%snapshot_count = 0
243 2112 : wf_history%last_state_index = 1
244 : END IF
245 :
246 2112 : END SUBROUTINE wfi_clear
247 :
248 : ! **************************************************************************************************
249 : !> \brief returns a snapshot, the first being the latest snapshot
250 : !> \param wf_history the plage where to get the snapshot
251 : !> \param wf_index the index of the snapshot you want
252 : !> \return ...
253 : !> \par History
254 : !> 12.2002 created [fawzi]
255 : !> \author fawzi
256 : ! **************************************************************************************************
257 64443 : FUNCTION wfi_get_snapshot(wf_history, wf_index) RESULT(res)
258 : TYPE(qs_wf_history_type), POINTER :: wf_history
259 : INTEGER, INTENT(in) :: wf_index
260 : TYPE(qs_wf_snapshot_type), POINTER :: res
261 :
262 64443 : NULLIFY (res)
263 :
264 64443 : CPASSERT(ASSOCIATED(wf_history))
265 64443 : CPASSERT(ASSOCIATED(wf_history%past_states))
266 64443 : IF (wf_index > wf_history%memory_depth .OR. wf_index > wf_history%snapshot_count) THEN
267 0 : CPABORT("")
268 : END IF
269 : res => wf_history%past_states( &
270 : MODULO(wf_history%snapshot_count + 1 - wf_index, &
271 64443 : wf_history%memory_depth) + 1)%snapshot
272 64443 : END FUNCTION wfi_get_snapshot
273 :
274 0 : END MODULE qs_wf_history_types
|