Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2024 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_dbcsr_operations, ONLY: dbcsr_deallocate_matrix_set
18 : USE cp_fm_types, ONLY: cp_fm_release,&
19 : cp_fm_type
20 : USE dbcsr_api, ONLY: dbcsr_deallocate_matrix,&
21 : dbcsr_p_type,&
22 : dbcsr_type
23 : USE kinds, ONLY: dp
24 : USE pw_types, ONLY: pw_c1d_gs_type,&
25 : pw_r3d_rs_type
26 : USE qs_rho_types, ONLY: qs_rho_release,&
27 : qs_rho_type
28 : #include "./base/base_uses.f90"
29 :
30 : IMPLICIT NONE
31 : PRIVATE
32 :
33 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
34 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_wf_history_types'
35 :
36 : PUBLIC :: qs_wf_snapshot_type, &
37 : qs_wf_history_type, qs_wf_history_p_type
38 : PUBLIC :: wfi_retain, wfi_release, wfi_get_snapshot
39 :
40 : ! **************************************************************************************************
41 : !> \brief represent a past snapshot of the wavefunction.
42 : !> some elements might not be associated (to spare memory)
43 : !> depending on how the snapshot was taken
44 : !> \param wf the wavefunctions
45 : !> \param rho_r the density in r space
46 : !> \param rho_g the density in g space
47 : !> \param rho_ao the density in ao space
48 : !> \param overlap the overlap matrix
49 : !> \param rho_frozen the frozen density structure
50 : !> \param dt the time of the snapshot (wrf to te previous snapshot!)
51 : !> \note
52 : !> keep track also of occupation numbers and energies?
53 : !> \par History
54 : !> 02.2003 created [fawzi]
55 : !> 02.2005 wf_mol added [MI]
56 : !> \author fawzi
57 : ! **************************************************************************************************
58 : TYPE qs_wf_snapshot_type
59 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: wf
60 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
61 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
62 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
63 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
64 : TYPE(dbcsr_type), POINTER :: overlap
65 : TYPE(qs_rho_type), POINTER :: rho_frozen
66 : REAL(KIND=dp) :: dt
67 : END TYPE qs_wf_snapshot_type
68 :
69 : ! **************************************************************************************************
70 : !> \brief pointer to a snapshot
71 : !> \param snapshot the pointer to the snapshot
72 : !> \par History
73 : !> 02.2003 created [fawzi]
74 : !> \author fawzi
75 : ! **************************************************************************************************
76 : TYPE qs_wf_snapshot_p_type
77 : TYPE(qs_wf_snapshot_type), POINTER :: snapshot
78 : END TYPE qs_wf_snapshot_p_type
79 :
80 : ! **************************************************************************************************
81 : !> \brief keeps track of the previous wavefunctions and can extrapolate them
82 : !> for the next step of md
83 : !> \param ref_cont reference count (see doc/ReferenceCounting.html)
84 : !> \param memory_depth how many snapshots should be stored
85 : !> \param last_state_index index of the latest snapshot
86 : !> \param past_states array with the past states (index starts at
87 : !> last_state_index)
88 : !> \param interpolation_method_nr the tag of the method used to
89 : !> extrapolate the new start state for qs
90 : !> \param snapshot_count number of snapshot taken so far (cumulative,
91 : !> can be bigger than the history depth)
92 : !> \note
93 : !> use a linked list for the past states ?
94 : !> \par History
95 : !> 02.2003 created [fawzi]
96 : !> \author fawzi
97 : ! **************************************************************************************************
98 : TYPE qs_wf_history_type
99 : INTEGER :: ref_count, memory_depth, last_state_index, &
100 : interpolation_method_nr, snapshot_count
101 : LOGICAL :: store_wf, store_rho_r, store_rho_g, store_rho_ao, store_rho_ao_kp, &
102 : store_overlap, store_frozen_density
103 : TYPE(qs_wf_snapshot_p_type), DIMENSION(:), POINTER :: past_states
104 : END TYPE qs_wf_history_type
105 :
106 : ! **************************************************************************************************
107 : !> \brief to create arrays of pointers to qs_wf_history_type
108 : !> \param wf_hist the pointer to the wf history
109 : !> \author fawzi
110 : ! **************************************************************************************************
111 : TYPE qs_wf_history_p_type
112 : TYPE(qs_wf_history_type), POINTER :: wf_history
113 : END TYPE qs_wf_history_p_type
114 :
115 : CONTAINS
116 :
117 : ! **************************************************************************************************
118 : !> \brief releases a snapshot of a wavefunction (see doc/ReferenceCounting.html)
119 : !> \param snapshot the snapshot to release
120 : !> \par History
121 : !> 02.2003 created [fawzi]
122 : !> 02.2005 wf_mol added [MI]
123 : !> \author fawzi
124 : ! **************************************************************************************************
125 9350 : SUBROUTINE wfs_release(snapshot)
126 : TYPE(qs_wf_snapshot_type), INTENT(INOUT) :: snapshot
127 :
128 9350 : CALL cp_fm_release(snapshot%wf)
129 : ! snapshot%rho_r & snapshot%rho_g is deallocated in wfs_update
130 : ! of qs_wf_history_methods, in case you wonder about it.
131 9350 : IF (ASSOCIATED(snapshot%rho_ao)) THEN
132 68 : CALL dbcsr_deallocate_matrix_set(snapshot%rho_ao)
133 : END IF
134 9350 : IF (ASSOCIATED(snapshot%rho_ao_kp)) THEN
135 6 : CALL dbcsr_deallocate_matrix_set(snapshot%rho_ao_kp)
136 : END IF
137 9350 : IF (ASSOCIATED(snapshot%overlap)) THEN
138 8248 : CALL dbcsr_deallocate_matrix(snapshot%overlap)
139 : END IF
140 9350 : IF (ASSOCIATED(snapshot%rho_frozen)) THEN
141 4 : CALL qs_rho_release(snapshot%rho_frozen)
142 4 : DEALLOCATE (snapshot%rho_frozen)
143 : END IF
144 :
145 9350 : END SUBROUTINE wfs_release
146 :
147 : ! **************************************************************************************************
148 : !> \brief retains a wf history (see doc/ReferenceCounting.html)
149 : !> \param wf_history the wf_history to retain
150 : !> \par History
151 : !> 02.2003 created [fawzi]
152 : !> \author fawzi
153 : ! **************************************************************************************************
154 6952 : SUBROUTINE wfi_retain(wf_history)
155 : TYPE(qs_wf_history_type), POINTER :: wf_history
156 :
157 6952 : CPASSERT(ASSOCIATED(wf_history))
158 6952 : wf_history%ref_count = wf_history%ref_count + 1
159 :
160 6952 : END SUBROUTINE wfi_retain
161 :
162 : ! **************************************************************************************************
163 : !> \brief releases a wf_history of a wavefunction
164 : !> (see doc/ReferenceCounting.html)
165 : !> \param wf_history the wf_history to release
166 : !> \par History
167 : !> 02.2003 created [fawzi]
168 : !> \author fawzi
169 : ! **************************************************************************************************
170 20081 : SUBROUTINE wfi_release(wf_history)
171 : TYPE(qs_wf_history_type), POINTER :: wf_history
172 :
173 : INTEGER :: i
174 :
175 20081 : IF (ASSOCIATED(wf_history)) THEN
176 13508 : CPASSERT(wf_history%ref_count > 0)
177 13508 : wf_history%ref_count = wf_history%ref_count - 1
178 13508 : IF (wf_history%ref_count == 0) THEN
179 6556 : IF (ASSOCIATED(wf_history%past_states)) THEN
180 37379 : DO i = 1, SIZE(wf_history%past_states)
181 37379 : IF (ASSOCIATED(wf_history%past_states(i)%snapshot)) THEN
182 9350 : CALL wfs_release(wf_history%past_states(i)%snapshot)
183 9350 : DEALLOCATE (wf_history%past_states(i)%snapshot)
184 : END IF
185 : END DO
186 6556 : DEALLOCATE (wf_history%past_states)
187 : END IF
188 6556 : DEALLOCATE (wf_history)
189 : END IF
190 : END IF
191 20081 : NULLIFY (wf_history)
192 20081 : END SUBROUTINE wfi_release
193 :
194 : ! **************************************************************************************************
195 : !> \brief returns a snapshot, the first being the latest snapshot
196 : !> \param wf_history the plage where to get the snapshot
197 : !> \param wf_index the index of the snapshot you want
198 : !> \return ...
199 : !> \par History
200 : !> 12.2002 created [fawzi]
201 : !> \author fawzi
202 : ! **************************************************************************************************
203 42535 : FUNCTION wfi_get_snapshot(wf_history, wf_index) RESULT(res)
204 : TYPE(qs_wf_history_type), POINTER :: wf_history
205 : INTEGER, INTENT(in) :: wf_index
206 : TYPE(qs_wf_snapshot_type), POINTER :: res
207 :
208 42535 : NULLIFY (res)
209 :
210 42535 : CPASSERT(ASSOCIATED(wf_history))
211 42535 : CPASSERT(ASSOCIATED(wf_history%past_states))
212 42535 : IF (wf_index > wf_history%memory_depth .OR. wf_index > wf_history%snapshot_count) THEN
213 0 : CPABORT("")
214 : END IF
215 : res => wf_history%past_states( &
216 : MODULO(wf_history%snapshot_count + 1 - wf_index, &
217 42535 : wf_history%memory_depth) + 1)%snapshot
218 42535 : END FUNCTION wfi_get_snapshot
219 :
220 0 : END MODULE qs_wf_history_types
|