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 Routines needed for EMD
10 : !> \author Florian Schiffmann (02.09)
11 : ! **************************************************************************************************
12 :
13 : MODULE rt_propagation_forces
14 : USE admm_types, ONLY: admm_type,&
15 : get_admm_env
16 : USE atomic_kind_types, ONLY: atomic_kind_type,&
17 : get_atomic_kind_set
18 : USE cp_control_types, ONLY: dft_control_type,&
19 : rtp_control_type
20 : USE cp_dbcsr_api, ONLY: &
21 : dbcsr_copy, dbcsr_create, dbcsr_deallocate_matrix, dbcsr_desymmetrize, dbcsr_get_block_p, &
22 : dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
23 : dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, dbcsr_p_type, dbcsr_release, &
24 : dbcsr_type, dbcsr_type_no_symmetry
25 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
26 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
27 : copy_fm_to_dbcsr,&
28 : cp_dbcsr_sm_fm_multiply
29 : USE cp_fm_types, ONLY: cp_fm_create,&
30 : cp_fm_release,&
31 : cp_fm_type
32 : USE kinds, ONLY: dp
33 : USE mathconstants, ONLY: one,&
34 : zero
35 : USE parallel_gemm_api, ONLY: parallel_gemm
36 : USE particle_types, ONLY: particle_type
37 : USE qs_environment_types, ONLY: get_qs_env,&
38 : qs_environment_type
39 : USE qs_force_types, ONLY: add_qs_force,&
40 : qs_force_type
41 : USE qs_ks_types, ONLY: qs_ks_env_type
42 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
43 : USE qs_overlap, ONLY: build_overlap_force
44 : USE qs_rho_types, ONLY: qs_rho_get,&
45 : qs_rho_type
46 : USE rt_propagation_types, ONLY: get_rtp,&
47 : rt_prop_type
48 : #include "./base/base_uses.f90"
49 :
50 : IMPLICIT NONE
51 : PRIVATE
52 :
53 : PUBLIC :: calc_c_mat_force, &
54 : rt_admm_force
55 :
56 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rt_propagation_forces'
57 :
58 : CONTAINS
59 :
60 : ! **************************************************************************************************
61 : !> \brief calculates the three additional force contributions needed in EMD
62 : !> P_imag*C , P_imag*B*S^-1*S_der , P*S^-1*H*S_der
63 : !> \param qs_env ...
64 : !> \par History
65 : !> 02.2014 switched to dbcsr matrices [Samuel Andermatt]
66 : !> 10.2023 merge MO-based and all-atom into one routine [Guillaume Le Breton]
67 : !> \author Florian Schiffmann (02.09)
68 : ! **************************************************************************************************
69 :
70 1592 : SUBROUTINE calc_c_mat_force(qs_env)
71 : TYPE(qs_environment_type), POINTER :: qs_env
72 :
73 : CHARACTER(LEN=*), PARAMETER :: routineN = 'calc_c_mat_force'
74 : REAL(KIND=dp), PARAMETER :: one = 1.0_dp, zero = 0.0_dp
75 :
76 : INTEGER :: handle, i, im, ispin, re
77 1592 : INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind, kind_of
78 1592 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
79 1592 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: C_mat, rho_ao, rho_ao_im, rho_new, &
80 1592 : S_der, SinvB, SinvH, SinvH_imag
81 : TYPE(dbcsr_type), POINTER :: S_inv, tmp
82 : TYPE(dft_control_type), POINTER :: dft_control
83 1592 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
84 1592 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
85 : TYPE(qs_rho_type), POINTER :: rho
86 : TYPE(rt_prop_type), POINTER :: rtp
87 : TYPE(rtp_control_type), POINTER :: rtp_control
88 :
89 1592 : CALL timeset(routineN, handle)
90 1592 : NULLIFY (rtp, particle_set, atomic_kind_set, dft_control)
91 :
92 : CALL get_qs_env(qs_env, &
93 : rtp=rtp, &
94 : rho=rho, &
95 : particle_set=particle_set, &
96 : atomic_kind_set=atomic_kind_set, &
97 : force=force, &
98 1592 : dft_control=dft_control)
99 :
100 1592 : rtp_control => dft_control%rtp_control
101 : CALL get_rtp(rtp=rtp, C_mat=C_mat, S_der=S_der, S_inv=S_inv, &
102 1592 : SinvH=SinvH, SinvB=SinvB)
103 :
104 1592 : CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
105 :
106 : NULLIFY (tmp)
107 1592 : ALLOCATE (tmp)
108 1592 : CALL dbcsr_create(tmp, template=SinvB(1)%matrix)
109 :
110 1592 : IF (rtp%linear_scaling) THEN
111 356 : CALL get_rtp(rtp=rtp, rho_new=rho_new)
112 : ELSE
113 1236 : CALL qs_rho_get(rho_struct=rho, rho_ao=rho_ao, rho_ao_im=rho_ao_im)
114 : END IF
115 :
116 : ! If SinvH has an imaginary part (the minus sign is already in SinvH_imag)
117 1592 : IF (rtp%propagate_complex_ks) CALL get_rtp(rtp=rtp, SinvH_imag=SinvH_imag)
118 :
119 3436 : DO ispin = 1, SIZE(SinvH)
120 1844 : re = 2*ispin - 1
121 1844 : im = 2*ispin
122 3436 : IF (rtp%linear_scaling) THEN
123 : CALL dbcsr_multiply("N", "N", one, SinvH(ispin)%matrix, rho_new(re)%matrix, zero, tmp, &
124 356 : filter_eps=rtp%filter_eps)
125 356 : IF (rtp%propagate_complex_ks) THEN
126 : CALL dbcsr_multiply("N", "N", one, SinvH_imag(ispin)%matrix, rho_new(im)%matrix, one, tmp, &
127 114 : filter_eps=rtp%filter_eps)
128 : END IF
129 : CALL dbcsr_multiply("N", "N", one, SinvB(ispin)%matrix, rho_new(im)%matrix, one, tmp, &
130 356 : filter_eps=rtp%filter_eps)
131 356 : CALL compute_forces(force, tmp, S_der, rho_new(im)%matrix, C_mat, kind_of, atom_of_kind)
132 : ELSE
133 1488 : CALL dbcsr_multiply("N", "N", one, SinvH(ispin)%matrix, rho_ao(ispin)%matrix, zero, tmp)
134 1488 : IF (rtp%propagate_complex_ks) THEN
135 372 : CALL dbcsr_multiply("N", "N", one, SinvH_imag(ispin)%matrix, rho_ao_im(ispin)%matrix, one, tmp)
136 : END IF
137 1488 : CALL dbcsr_multiply("N", "N", one, SinvB(ispin)%matrix, rho_ao_im(ispin)%matrix, one, tmp)
138 1488 : CALL compute_forces(force, tmp, S_der, rho_ao_im(ispin)%matrix, C_mat, kind_of, atom_of_kind)
139 : END IF
140 : END DO
141 :
142 : ! recall QS forces, at this point have the other sign.
143 4506 : DO i = 1, SIZE(force)
144 23658 : force(i)%ehrenfest(:, :) = -force(i)%ehrenfest(:, :)
145 : END DO
146 :
147 1592 : CALL dbcsr_deallocate_matrix(tmp)
148 :
149 1592 : CALL timestop(handle)
150 :
151 3184 : END SUBROUTINE calc_c_mat_force
152 :
153 : ! **************************************************************************************************
154 : !> \brief ...
155 : !> \param force ...
156 : !> \param tmp ...
157 : !> \param S_der ...
158 : !> \param rho_im ...
159 : !> \param C_mat ...
160 : !> \param kind_of ...
161 : !> \param atom_of_kind ...
162 : ! **************************************************************************************************
163 1844 : SUBROUTINE compute_forces(force, tmp, S_der, rho_im, C_mat, kind_of, atom_of_kind)
164 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
165 : TYPE(dbcsr_type), POINTER :: tmp
166 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: S_der
167 : TYPE(dbcsr_type), POINTER :: rho_im
168 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: C_mat
169 : INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of, atom_of_kind
170 :
171 : INTEGER :: col_atom, i, ikind, kind_atom, row_atom
172 : LOGICAL :: found
173 1844 : REAL(dp), DIMENSION(:, :), POINTER :: block_values, block_values2
174 : TYPE(dbcsr_iterator_type) :: iter
175 : TYPE(dbcsr_type) :: rho_im_full
176 :
177 : ! A block of a (anti)symmetric matrix need not be on the process that holds the same
178 : ! block of the non-symmetric C_mat, and both triangles are needed, so use a full copy
179 1844 : CALL dbcsr_create(rho_im_full, template=C_mat(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
180 1844 : CALL dbcsr_desymmetrize(rho_im, rho_im_full)
181 :
182 7376 : DO i = 1, 3
183 : !Calculate the sum over the hadmard product
184 : !S_der part
185 :
186 5532 : CALL dbcsr_iterator_start(iter, tmp)
187 32664 : DO WHILE (dbcsr_iterator_blocks_left(iter))
188 27132 : CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
189 27132 : CALL dbcsr_get_block_p(S_der(i)%matrix, row_atom, col_atom, block_values2, found=found)
190 32664 : IF (found) THEN
191 25996 : ikind = kind_of(col_atom)
192 25996 : kind_atom = atom_of_kind(col_atom)
193 : !The block_values are in a vector format,
194 : ! so the dot_product is the sum over all elements of the hamand product, that I need
195 : force(ikind)%ehrenfest(i, kind_atom) = force(ikind)%ehrenfest(i, kind_atom) + &
196 960330 : 2.0_dp*SUM(block_values*block_values2)
197 : END IF
198 : END DO
199 5532 : CALL dbcsr_iterator_stop(iter)
200 :
201 : !C_mat part: i*Tr[(C^T - C)P] = -2*sum(C*P_im) for the column atom, over all blocks,
202 : ! since the blocks below and above the diagonal carry the velocities of different atoms
203 :
204 5532 : CALL dbcsr_iterator_start(iter, rho_im_full)
205 32178 : DO WHILE (dbcsr_iterator_blocks_left(iter))
206 26646 : CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
207 26646 : CALL dbcsr_get_block_p(C_mat(i)%matrix, row_atom, col_atom, block_values2, found=found)
208 32178 : IF (found) THEN
209 25155 : ikind = kind_of(col_atom)
210 25155 : kind_atom = atom_of_kind(col_atom)
211 : !The block_values are in a vector format, so the dot_product is
212 : ! the sum over all elements of the hamand product, that I need
213 : force(ikind)%ehrenfest(i, kind_atom) = force(ikind)%ehrenfest(i, kind_atom) - &
214 852609 : 2.0_dp*SUM(block_values*block_values2)
215 : END IF
216 : END DO
217 18440 : CALL dbcsr_iterator_stop(iter)
218 : END DO
219 1844 : CALL dbcsr_release(rho_im_full)
220 :
221 1844 : END SUBROUTINE compute_forces
222 :
223 : ! **************************************************************************************************
224 : !> \brief Force contribution from the dependence of the ADMM projection on the nuclear positions
225 : !> \param qs_env ...
226 : !> \par History
227 : !> 10.2026 use the density matrix instead of the MO coefficients
228 : ! **************************************************************************************************
229 30 : SUBROUTINE rt_admm_force(qs_env)
230 : TYPE(qs_environment_type), POINTER :: qs_env
231 :
232 : TYPE(admm_type), POINTER :: admm_env
233 30 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: KS_aux_im, KS_aux_re, matrix_s_aux_fit, &
234 30 : matrix_s_aux_fit_vs_orb
235 : TYPE(rt_prop_type), POINTER :: rtp
236 :
237 : CALL get_qs_env(qs_env, &
238 : admm_env=admm_env, &
239 30 : rtp=rtp)
240 : CALL get_admm_env(admm_env, matrix_ks_aux_fit=KS_aux_re, &
241 : matrix_ks_aux_fit_im=KS_aux_im, &
242 : matrix_s_aux_fit=matrix_s_aux_fit, &
243 30 : matrix_s_aux_fit_vs_orb=matrix_s_aux_fit_vs_orb)
244 :
245 : ! currently only none option
246 : CALL rt_admm_forces_none(qs_env, admm_env, rtp, KS_aux_re, KS_aux_im, &
247 30 : matrix_s_aux_fit, matrix_s_aux_fit_vs_orb)
248 :
249 30 : END SUBROUTINE rt_admm_force
250 :
251 : ! **************************************************************************************************
252 : !> \brief ADMM projection force without purification.
253 : !> With P~ = A*P*A^T and A = S~^-1*Q, the weighted matrices are
254 : !> W_Q = -2*S~^-1*Re(K~*A*P) and W_S = -W_Q*A^T.
255 : !> The complex density matrix P is used instead of the MO coefficients, so that
256 : !> P is consistent with the auxiliary KS matrix K~ and includes the occupations.
257 : !> \param qs_env ...
258 : !> \param admm_env ...
259 : !> \param rtp ...
260 : !> \param KS_aux_re ...
261 : !> \param KS_aux_im ...
262 : !> \param matrix_s_aux_fit ...
263 : !> \param matrix_s_aux_fit_vs_orb ...
264 : ! **************************************************************************************************
265 30 : SUBROUTINE rt_admm_forces_none(qs_env, admm_env, rtp, KS_aux_re, KS_aux_im, matrix_s_aux_fit, &
266 : matrix_s_aux_fit_vs_orb)
267 : TYPE(qs_environment_type), POINTER :: qs_env
268 : TYPE(admm_type), POINTER :: admm_env
269 : TYPE(rt_prop_type), POINTER :: rtp
270 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: KS_aux_re, KS_aux_im, matrix_s_aux_fit, &
271 : matrix_s_aux_fit_vs_orb
272 :
273 : INTEGER :: ispin, nao, natom, naux
274 30 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: admm_force
275 30 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
276 : TYPE(cp_fm_type) :: tmp_aux_aux, tmp_aux_kap, tmp_aux_nao, &
277 : tmp_nao_nao
278 90 : TYPE(cp_fm_type), DIMENSION(2) :: tmp_aux_ap
279 30 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao, rho_ao_im, rho_new
280 : TYPE(dbcsr_type), POINTER :: matrix_w_q, matrix_w_s, rho_im, rho_re
281 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
282 30 : POINTER :: sab_aux_fit_asymm, sab_aux_fit_vs_orb
283 30 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
284 : TYPE(qs_ks_env_type), POINTER :: ks_env
285 : TYPE(qs_rho_type), POINTER :: rho
286 :
287 30 : NULLIFY (sab_aux_fit_asymm, sab_aux_fit_vs_orb, ks_env, rho, rho_ao, rho_ao_im, rho_new)
288 :
289 : CALL get_qs_env(qs_env, ks_env=ks_env, rho=rho, natom=natom, &
290 30 : atomic_kind_set=atomic_kind_set, force=force)
291 : CALL get_admm_env(admm_env, sab_aux_fit_asymm=sab_aux_fit_asymm, &
292 30 : sab_aux_fit_vs_orb=sab_aux_fit_vs_orb)
293 30 : IF (rtp%linear_scaling) THEN
294 0 : CALL get_rtp(rtp=rtp, rho_new=rho_new)
295 : ELSE
296 30 : CALL qs_rho_get(rho_struct=rho, rho_ao=rho_ao, rho_ao_im=rho_ao_im)
297 : END IF
298 :
299 30 : naux = admm_env%nao_aux_fit; nao = admm_env%nao_orb
300 :
301 30 : ALLOCATE (matrix_w_s)
302 : CALL dbcsr_create(matrix_w_s, template=matrix_s_aux_fit(1)%matrix, &
303 30 : name='W MATRIX AUX S', matrix_type=dbcsr_type_no_symmetry)
304 30 : CALL cp_dbcsr_alloc_block_from_nbl(matrix_w_s, sab_aux_fit_asymm)
305 :
306 30 : ALLOCATE (matrix_w_q)
307 : CALL dbcsr_copy(matrix_w_q, matrix_s_aux_fit_vs_orb(1)%matrix, &
308 30 : "W MATRIX AUX Q")
309 :
310 30 : CALL cp_fm_create(tmp_nao_nao, admm_env%work_orb_orb%matrix_struct, name="too")
311 30 : CALL cp_fm_create(tmp_aux_ap(1), admm_env%work_aux_orb%matrix_struct, name="tao")
312 30 : CALL cp_fm_create(tmp_aux_ap(2), admm_env%work_aux_orb%matrix_struct, name="tao")
313 30 : CALL cp_fm_create(tmp_aux_kap, admm_env%work_aux_orb%matrix_struct, name="tao")
314 30 : CALL cp_fm_create(tmp_aux_nao, admm_env%work_aux_orb%matrix_struct, name="tao")
315 30 : CALL cp_fm_create(tmp_aux_aux, admm_env%work_aux_aux%matrix_struct, name="taa")
316 :
317 90 : ALLOCATE (admm_force(3, natom))
318 :
319 70 : DO ispin = 1, SIZE(KS_aux_re)
320 40 : IF (rtp%linear_scaling) THEN
321 0 : rho_re => rho_new(2*ispin - 1)%matrix
322 0 : rho_im => rho_new(2*ispin)%matrix
323 : ELSE
324 40 : rho_re => rho_ao(ispin)%matrix
325 40 : rho_im => rho_ao_im(ispin)%matrix
326 : END IF
327 :
328 : ! First calculate A*P, real part ends on tmp_aux_ap(1), imaginary part on tmp_aux_ap(2)
329 40 : CALL copy_dbcsr_to_fm(rho_re, tmp_nao_nao)
330 40 : CALL parallel_gemm('N', 'N', naux, nao, nao, 1.0_dp, admm_env%A, tmp_nao_nao, 0.0_dp, tmp_aux_ap(1))
331 40 : CALL copy_dbcsr_to_fm(rho_im, tmp_nao_nao)
332 40 : CALL parallel_gemm('N', 'N', naux, nao, nao, 1.0_dp, admm_env%A, tmp_nao_nao, 0.0_dp, tmp_aux_ap(2))
333 :
334 : ! Next step Re(K~*A*P) = K~_re*A*P_re - K~_im*A*P_im
335 40 : CALL cp_dbcsr_sm_fm_multiply(KS_aux_re(ispin)%matrix, tmp_aux_ap(1), tmp_aux_kap, nao, 1.0_dp, 0.0_dp)
336 40 : CALL cp_dbcsr_sm_fm_multiply(KS_aux_im(ispin)%matrix, tmp_aux_ap(2), tmp_aux_kap, nao, -1.0_dp, 1.0_dp)
337 :
338 : ! Wq=-2*S~^-1*Re(K~*A*P), minus sign due to force = -dE/dR
339 40 : CALL parallel_gemm('N', 'N', naux, nao, naux, -2.0_dp, admm_env%S_inv, tmp_aux_kap, 0.0_dp, tmp_aux_nao)
340 :
341 : ! Ws=-Wq*A^T
342 40 : CALL parallel_gemm('N', 'T', naux, naux, nao, -1.0_dp, tmp_aux_nao, admm_env%A, 0.0_dp, tmp_aux_aux)
343 :
344 : ! *** copy to sparse matrix
345 40 : CALL copy_fm_to_dbcsr(tmp_aux_nao, matrix_w_q, keep_sparsity=.TRUE.)
346 :
347 : ! *** copy to sparse matrix
348 40 : CALL copy_fm_to_dbcsr(tmp_aux_aux, matrix_w_s, keep_sparsity=.TRUE.)
349 :
350 40 : admm_force = 0.0_dp
351 : CALL build_overlap_force(ks_env, admm_force, &
352 : basis_type_a="AUX_FIT", basis_type_b="AUX_FIT", &
353 40 : sab_nl=sab_aux_fit_asymm, matrix_p=matrix_w_s)
354 : CALL build_overlap_force(ks_env, admm_force, &
355 : basis_type_a="AUX_FIT", basis_type_b="ORB", &
356 40 : sab_nl=sab_aux_fit_vs_orb, matrix_p=matrix_w_q)
357 : ! add forces
358 70 : CALL add_qs_force(admm_force, force, "overlap_admm", atomic_kind_set)
359 :
360 : END DO
361 :
362 30 : DEALLOCATE (admm_force)
363 :
364 : ! *** Deallocated weighted density matrices
365 30 : CALL dbcsr_deallocate_matrix(matrix_w_s)
366 30 : CALL dbcsr_deallocate_matrix(matrix_w_q)
367 :
368 30 : CALL cp_fm_release(tmp_nao_nao)
369 30 : CALL cp_fm_release(tmp_aux_ap(1))
370 30 : CALL cp_fm_release(tmp_aux_ap(2))
371 30 : CALL cp_fm_release(tmp_aux_kap)
372 30 : CALL cp_fm_release(tmp_aux_nao)
373 30 : CALL cp_fm_release(tmp_aux_aux)
374 :
375 120 : END SUBROUTINE rt_admm_forces_none
376 :
377 : END MODULE rt_propagation_forces
|