(git:27c966b)
Loading...
Searching...
No Matches
mp2_ri_grad_util.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
8! **************************************************************************************************
9!> \brief Routines for calculating RI-MP2 gradients
10!> \par History
11!> 10.2013 created [Mauro Del Ben]
12! **************************************************************************************************
14!
25 USE cp_fm_types, ONLY: cp_fm_create,&
34 USE kinds, ONLY: dp
36 USE mp2_types, ONLY: mp2_type
38#include "./base/base_uses.f90"
39
40 IMPLICIT NONE
41
42 PRIVATE
43
44 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mp2_ri_grad_util'
45
46 PUBLIC :: complete_gamma
47
48CONTAINS
49
50! **************************************************************************************************
51!> \brief complete the calculation of the Gamma matrices
52!> \param mp2_env ...
53!> \param B_ia_Q ...
54!> \param dimen_RI ...
55!> \param homo ...
56!> \param virtual ...
57!> \param para_env ...
58!> \param para_env_sub ...
59!> \param my_group_L_size ...
60!> \param my_group_L_start ...
61!> \param my_group_L_end ...
62!> \param my_B_size ...
63!> \param my_B_virtual_start ...
64!> \param gd_B_virtual ...
65!> \param kspin ...
66!> \author Mauro Del Ben
67! **************************************************************************************************
68 SUBROUTINE complete_gamma(mp2_env, B_ia_Q, dimen_RI, homo, virtual, para_env, para_env_sub, my_group_L_size, &
69 my_group_L_start, my_group_L_end, my_B_size, my_B_virtual_start, gd_B_virtual, kspin)
70
71 TYPE(mp2_type) :: mp2_env
72 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :), &
73 INTENT(INOUT) :: b_ia_q
74 INTEGER, INTENT(IN) :: dimen_ri, homo, virtual
75 TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env, para_env_sub
76 INTEGER, INTENT(IN) :: my_group_l_size, my_group_l_start, &
77 my_group_l_end, my_b_size, &
78 my_b_virtual_start
79 TYPE(group_dist_d1_type), INTENT(IN) :: gd_b_virtual
80 INTEGER, INTENT(IN) :: kspin
81
82 CHARACTER(LEN=*), PARAMETER :: routinen = 'complete_gamma'
83
84 INTEGER :: dimen_ia, handle, ispin, kkb, my_ia_end, &
85 my_ia_size, my_ia_start, my_p_end, &
86 my_p_size, my_p_start, nspins
87 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: bib_c_2d, gamma_2d, gamma_pq
88 TYPE(cp_blacs_env_type), POINTER :: blacs_env
89 TYPE(cp_fm_struct_type), POINTER :: fm_struct_ia, fm_struct_ri
90 TYPE(cp_fm_type) :: fm_gamma, fm_gamma_pq, fm_gamma_pq_2, fm_gamma_pq_temp, &
91 fm_gamma_pq_temp_2, fm_ia_p, fm_y, operator_half, pq_half
92 TYPE(group_dist_d1_type) :: gd_ia, gd_p
93
94 CALL timeset(routinen, handle)
95
96 ! reshape the data into a 2D array
97 ! reorder the local data in such a way to help the next stage of matrix creation
98 ! now the data inside the group are divided into a ia x K matrix
99 dimen_ia = homo*virtual
100 CALL create_group_dist(gd_ia, para_env_sub%num_pe, dimen_ia)
101 CALL get_group_dist(gd_ia, para_env_sub%mepos, my_ia_start, my_ia_end, my_ia_size)
102
103 CALL mat_3d_to_2d(b_ia_q, bib_c_2d, homo, my_b_size, virtual, my_b_virtual_start, &
104 my_ia_start, my_ia_end, my_ia_size, my_group_l_size, para_env_sub, gd_b_virtual)
105
106 CALL mat_3d_to_2d_gamma(mp2_env%ri_grad%Gamma_P_ia(kspin)%array, gamma_2d, homo, my_b_size, virtual, my_b_virtual_start, &
107 my_ia_start, my_ia_end, my_ia_size, my_group_l_size, para_env_sub, gd_b_virtual)
108
109 CALL create_group_dist(gd_p, para_env_sub%num_pe, dimen_ri)
110 CALL get_group_dist(gd_p, para_env_sub%mepos, my_p_start, my_p_end, my_p_size)
111
112 ! create the blacs env
113 NULLIFY (blacs_env)
114 CALL cp_blacs_env_create(blacs_env=blacs_env, para_env=para_env)
115
116 NULLIFY (fm_struct_ia)
117 CALL cp_fm_struct_create(fm_struct_ia, context=blacs_env, nrow_global=dimen_ia, &
118 ncol_global=dimen_ri, para_env=para_env)
119
120 ! create the fm matrix Gamma
121 CALL cp_array_to_fm(gamma_2d, fm_struct_ia, my_ia_start, my_ia_end, my_group_l_start, my_group_l_end, &
122 fm_y)
123 ! create the fm matrix B_ia_P
124 CALL cp_array_to_fm(bib_c_2d, fm_struct_ia, my_ia_start, my_ia_end, my_group_l_start, my_group_l_end, &
125 fm_ia_p)
126
127 NULLIFY (fm_struct_ri)
128 CALL cp_fm_struct_create(fm_struct_ri, context=blacs_env, nrow_global=dimen_ri, &
129 ncol_global=dimen_ri, para_env=para_env)
130
131 ! since we will need (P|Q)^(-1/2) in the future, make a copy
132 CALL cp_array_to_fm(mp2_env%ri_grad%PQ_half, fm_struct_ri, my_p_start, my_p_end, my_group_l_start, &
133 my_group_l_end, pq_half, do_release_mat=.false.)
134
135 IF (.NOT. compare_potential_types(mp2_env%ri_metric, mp2_env%potential_parameter)) THEN
136 CALL cp_array_to_fm(mp2_env%ri_grad%operator_half, fm_struct_ri, my_p_start, my_p_end, &
137 my_group_l_start, my_group_l_end, operator_half, do_release_mat=.false.)
138 END IF
139
140 ! complete the gamma matrix Gamma_ia^P = sum_Q (Y_ia^Q * (Q|P)^(-1/2))
141 IF (compare_potential_types(mp2_env%ri_metric, mp2_env%potential_parameter)) THEN
142 CALL cp_fm_create(fm_gamma, fm_struct_ia, name="fm_Gamma")
143 CALL cp_fm_struct_release(fm_struct_ia)
144 ! perform the matrix multiplication
145 CALL parallel_gemm(transa="N", transb="T", m=dimen_ia, n=dimen_ri, k=dimen_ri, alpha=1.0_dp, &
146 matrix_a=fm_y, matrix_b=pq_half, beta=0.0_dp, &
147 matrix_c=fm_gamma)
148 ! release the Y matrix
149 CALL cp_fm_release(fm_y)
150
151 ! complete gamma small (fm_Gamma_PQ)
152 ! create temp matrix
153 CALL cp_fm_create(fm_gamma_pq_temp, fm_struct_ri, name="fm_Gamma_PQ_temp")
154 CALL parallel_gemm(transa="T", transb="N", m=dimen_ri, n=dimen_ri, k=dimen_ia, alpha=1.0_dp, &
155 matrix_a=fm_gamma, matrix_b=fm_ia_p, beta=0.0_dp, &
156 matrix_c=fm_gamma_pq_temp)
157 CALL cp_fm_release(fm_ia_p)
158 ! create fm_Gamma_PQ matrix
159 CALL cp_fm_create(fm_gamma_pq, fm_struct_ri, name="fm_Gamma_PQ")
160 CALL cp_fm_struct_release(fm_struct_ri)
161 ! perform matrix multiplication
162 CALL parallel_gemm(transa="N", transb="T", m=dimen_ri, n=dimen_ri, k=dimen_ri, alpha=1.0_dp, &
163 matrix_a=fm_gamma_pq_temp, matrix_b=pq_half, beta=0.0_dp, &
164 matrix_c=fm_gamma_pq)
165 CALL cp_fm_release(fm_gamma_pq_temp)
166 CALL cp_fm_release(pq_half)
167
168 ELSE
169 ! create temp matrix
170 CALL cp_fm_create(fm_gamma_pq_temp, fm_struct_ri, name="fm_Gamma_PQ_temp")
171 CALL parallel_gemm(transa="T", transb="N", m=dimen_ri, n=dimen_ri, k=dimen_ia, alpha=1.0_dp, &
172 matrix_a=fm_y, matrix_b=fm_ia_p, beta=0.0_dp, &
173 matrix_c=fm_gamma_pq_temp)
174 CALL cp_fm_release(fm_ia_p)
175
176 CALL cp_fm_create(fm_gamma, fm_struct_ia, name="fm_Gamma")
177 CALL cp_fm_struct_release(fm_struct_ia)
178 ! perform the matrix multiplication
179 CALL parallel_gemm(transa="N", transb="T", m=dimen_ia, n=dimen_ri, k=dimen_ri, alpha=1.0_dp, &
180 matrix_a=fm_y, matrix_b=pq_half, beta=0.0_dp, &
181 matrix_c=fm_gamma)
182 CALL cp_fm_release(fm_y)
183
184 CALL cp_fm_create(fm_gamma_pq_temp_2, fm_struct_ri, name="fm_Gamma_PQ_temp_2")
185 CALL parallel_gemm(transa="N", transb="T", m=dimen_ri, n=dimen_ri, k=dimen_ri, alpha=1.0_dp, &
186 matrix_a=fm_gamma_pq_temp, matrix_b=operator_half, beta=0.0_dp, &
187 matrix_c=fm_gamma_pq_temp_2)
188
189 CALL cp_fm_create(fm_gamma_pq_2, fm_struct_ri, name="fm_Gamma_PQ_2")
190 CALL parallel_gemm(transa="N", transb="N", m=dimen_ri, n=dimen_ri, k=dimen_ri, alpha=1.0_dp, &
191 matrix_a=pq_half, matrix_b=fm_gamma_pq_temp_2, beta=0.0_dp, &
192 matrix_c=fm_gamma_pq_temp)
193 CALL cp_fm_to_fm(fm_gamma_pq_temp, fm_gamma_pq_2)
194 CALL cp_fm_geadd(1.0_dp, "T", fm_gamma_pq_temp, 1.0_dp, fm_gamma_pq_2)
195 CALL cp_fm_release(fm_gamma_pq_temp)
196 CALL cp_fm_release(pq_half)
197
198 CALL cp_fm_create(fm_gamma_pq, fm_struct_ri)
199 CALL cp_fm_struct_release(fm_struct_ri)
200 CALL parallel_gemm(transa="N", transb="N", m=dimen_ri, n=dimen_ri, k=dimen_ri, alpha=-1.0_dp, &
201 matrix_a=operator_half, matrix_b=fm_gamma_pq_temp_2, beta=0.0_dp, &
202 matrix_c=fm_gamma_pq)
203 CALL cp_fm_release(operator_half)
204 CALL cp_fm_release(fm_gamma_pq_temp_2)
205 END IF
206
207 ! now redistribute the data, in this case we go back
208 ! to the original way the integrals were distributed
209 CALL cp_fm_to_array(gamma_2d, my_ia_start, my_ia_end, my_group_l_start, my_group_l_end, fm_gamma)
210
211 CALL cp_fm_to_array(gamma_pq, my_p_start, my_p_end, my_group_l_start, my_group_l_end, fm_gamma_pq)
212 IF (.NOT. ALLOCATED(mp2_env%ri_grad%Gamma_PQ)) THEN
213 CALL move_alloc(gamma_pq, mp2_env%ri_grad%Gamma_PQ)
214 ELSE
215 mp2_env%ri_grad%Gamma_PQ(:, :) = mp2_env%ri_grad%Gamma_PQ + gamma_pq
216 DEALLOCATE (gamma_pq)
217 END IF
218
219 IF (.NOT. compare_potential_types(mp2_env%ri_metric, mp2_env%potential_parameter)) THEN
220 CALL cp_fm_to_array(gamma_pq, my_p_start, my_p_end, my_group_l_start, my_group_l_end, fm_gamma_pq_2)
221 IF (.NOT. ALLOCATED(mp2_env%ri_grad%Gamma_PQ_2)) THEN
222 CALL move_alloc(gamma_pq, mp2_env%ri_grad%Gamma_PQ_2)
223 ELSE
224 mp2_env%ri_grad%Gamma_PQ_2(:, :) = mp2_env%ri_grad%Gamma_PQ_2 + gamma_pq
225 DEALLOCATE (gamma_pq)
226 END IF
227 END IF
228
229 ! allocate G_P_ia (DBCSR)
230 IF (.NOT. ALLOCATED(mp2_env%ri_grad%G_P_ia)) THEN
231 nspins = SIZE(mp2_env%ri_grad%mo_coeff_o)
232 ALLOCATE (mp2_env%ri_grad%G_P_ia(my_group_l_size, nspins))
233 DO ispin = 1, nspins
234 DO kkb = 1, my_group_l_size
235 NULLIFY (mp2_env%ri_grad%G_P_ia(kkb, ispin)%matrix)
236 END DO
237 END DO
238 END IF
239
240 ! create the Gamma_ia_P in DBCSR style
241 CALL cp_array_to_dbcsr_slices(gamma_2d, homo, virtual, para_env_sub, my_ia_start, my_ia_end, &
242 mp2_env%ri_grad%mo_coeff_o(1)%matrix, &
243 matrix_ptrs=mp2_env%ri_grad%G_P_ia(:, kspin))
244
245 CALL release_group_dist(gd_ia)
246 CALL release_group_dist(gd_p)
247
248 ! release blacs_env
249 CALL cp_blacs_env_release(blacs_env)
250
251 CALL timestop(handle)
252
253 END SUBROUTINE complete_gamma
254
255! **************************************************************************************************
256!> \brief Redistribute a 3d matrix to a 2d matrix
257!> \param B_ia_Q ...
258!> \param BIb_C_2D ...
259!> \param homo ...
260!> \param my_B_size ...
261!> \param virtual ...
262!> \param my_B_virtual_start ...
263!> \param my_ia_start ...
264!> \param my_ia_end ...
265!> \param my_ia_size ...
266!> \param my_group_L_size ...
267!> \param para_env_sub ...
268!> \param gd_B_virtual ...
269! **************************************************************************************************
270 SUBROUTINE mat_3d_to_2d(B_ia_Q, BIb_C_2D, homo, my_B_size, virtual, my_B_virtual_start, &
271 my_ia_start, my_ia_end, my_ia_size, my_group_L_size, para_env_sub, gd_B_virtual)
272 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :), &
273 INTENT(INOUT) :: b_ia_q
274 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
275 INTENT(OUT) :: bib_c_2d
276 INTEGER, INTENT(IN) :: homo, my_b_size, virtual, &
277 my_b_virtual_start, my_ia_start, &
278 my_ia_end, my_ia_size, my_group_l_size
279 TYPE(mp_para_env_type), INTENT(IN) :: para_env_sub
280 TYPE(group_dist_d1_type), INTENT(IN) :: gd_b_virtual
281
282 CHARACTER(LEN=*), PARAMETER :: routinen = 'mat_3d_to_2d'
283
284 INTEGER :: handle, ia_global, iib, jjb, proc_receive, proc_send, proc_shift, rec_b_size, &
285 rec_b_virtual_end, rec_b_virtual_start
286 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: bib_c_rec
287
288 CALL timeset(routinen, handle)
289
290 ALLOCATE (bib_c_2d(my_ia_size, my_group_l_size))
291 bib_c_2d = 0.0_dp
292
293 DO iib = 1, homo
294 DO jjb = 1, my_b_size
295 ia_global = (iib - 1)*virtual + my_b_virtual_start + jjb - 1
296 IF (ia_global >= my_ia_start .AND. ia_global <= my_ia_end) THEN
297 bib_c_2d(ia_global - my_ia_start + 1, :) = b_ia_q(iib, jjb, 1:my_group_l_size)
298 END IF
299 END DO
300 END DO
301
302 DO proc_shift = 1, para_env_sub%num_pe - 1
303 proc_send = modulo(para_env_sub%mepos + proc_shift, para_env_sub%num_pe)
304 proc_receive = modulo(para_env_sub%mepos - proc_shift, para_env_sub%num_pe)
305
306 CALL get_group_dist(gd_b_virtual, proc_receive, rec_b_virtual_start, rec_b_virtual_end, rec_b_size)
307
308 ALLOCATE (bib_c_rec(homo, rec_b_size, my_group_l_size))
309 bib_c_rec = 0.0_dp
310
311 CALL para_env_sub%sendrecv(b_ia_q, proc_send, &
312 bib_c_rec, proc_receive)
313
314 DO iib = 1, homo
315 DO jjb = 1, rec_b_size
316 ia_global = (iib - 1)*virtual + rec_b_virtual_start + jjb - 1
317 IF (ia_global >= my_ia_start .AND. ia_global <= my_ia_end) THEN
318 bib_c_2d(ia_global - my_ia_start + 1, :) = bib_c_rec(iib, jjb, 1:my_group_l_size)
319 END IF
320 END DO
321 END DO
322
323 DEALLOCATE (bib_c_rec)
324 END DO
325 DEALLOCATE (b_ia_q)
326
327 CALL timestop(handle)
328 END SUBROUTINE mat_3d_to_2d
329
330! **************************************************************************************************
331!> \brief Redistribute a 3d matrix to a 2d matrix, specialized for Gamma_P_ia to account for a different order of indices
332!> \param B_ia_Q ...
333!> \param BIb_C_2D ...
334!> \param homo ...
335!> \param my_B_size ...
336!> \param virtual ...
337!> \param my_B_virtual_start ...
338!> \param my_ia_start ...
339!> \param my_ia_end ...
340!> \param my_ia_size ...
341!> \param my_group_L_size ...
342!> \param para_env_sub ...
343!> \param gd_B_virtual ...
344! **************************************************************************************************
345 SUBROUTINE mat_3d_to_2d_gamma(B_ia_Q, BIb_C_2D, homo, my_B_size, virtual, my_B_virtual_start, &
346 my_ia_start, my_ia_end, my_ia_size, my_group_L_size, para_env_sub, gd_B_virtual)
347 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :), &
348 INTENT(INOUT) :: b_ia_q
349 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
350 INTENT(OUT) :: bib_c_2d
351 INTEGER, INTENT(IN) :: homo, my_b_size, virtual, &
352 my_b_virtual_start, my_ia_start, &
353 my_ia_end, my_ia_size, my_group_l_size
354 TYPE(mp_para_env_type), INTENT(IN) :: para_env_sub
355 TYPE(group_dist_d1_type), INTENT(IN) :: gd_b_virtual
356
357 CHARACTER(LEN=*), PARAMETER :: routinen = 'mat_3d_to_2d_gamma'
358
359 INTEGER :: handle, ia_global, iib, jjb, proc_receive, proc_send, proc_shift, rec_b_size, &
360 rec_b_virtual_end, rec_b_virtual_start
361 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: bib_c_rec
362
363 CALL timeset(routinen, handle)
364
365 ALLOCATE (bib_c_2d(my_ia_size, my_group_l_size))
366 bib_c_2d = 0.0_dp
367
368 DO iib = 1, homo
369 DO jjb = 1, my_b_size
370 ia_global = (iib - 1)*virtual + my_b_virtual_start + jjb - 1
371 IF (ia_global >= my_ia_start .AND. ia_global <= my_ia_end) THEN
372 bib_c_2d(ia_global - my_ia_start + 1, :) = b_ia_q(jjb, iib, 1:my_group_l_size)
373 END IF
374 END DO
375 END DO
376
377 DO proc_shift = 1, para_env_sub%num_pe - 1
378 proc_send = modulo(para_env_sub%mepos + proc_shift, para_env_sub%num_pe)
379 proc_receive = modulo(para_env_sub%mepos - proc_shift, para_env_sub%num_pe)
380
381 CALL get_group_dist(gd_b_virtual, proc_receive, rec_b_virtual_start, rec_b_virtual_end, rec_b_size)
382
383 ALLOCATE (bib_c_rec(rec_b_size, homo, my_group_l_size))
384 bib_c_rec = 0.0_dp
385
386 CALL para_env_sub%sendrecv(b_ia_q, proc_send, &
387 bib_c_rec, proc_receive)
388
389 DO iib = 1, homo
390 DO jjb = 1, rec_b_size
391 ia_global = (iib - 1)*virtual + rec_b_virtual_start + jjb - 1
392 IF (ia_global >= my_ia_start .AND. ia_global <= my_ia_end) THEN
393 bib_c_2d(ia_global - my_ia_start + 1, :) = bib_c_rec(jjb, iib, 1:my_group_l_size)
394 END IF
395 END DO
396 END DO
397
398 DEALLOCATE (bib_c_rec)
399 END DO
400 DEALLOCATE (b_ia_q)
401
402 CALL timestop(handle)
403 END SUBROUTINE mat_3d_to_2d_gamma
404
405END MODULE mp2_ri_grad_util
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
methods related to the blacs parallel environment
subroutine, public cp_blacs_env_release(blacs_env)
releases the given blacs_env
subroutine, public cp_blacs_env_create(blacs_env, para_env, blacs_grid_layout, blacs_repeatable, row_major, grid_2d)
allocates and initializes a type that represent a blacs context
Redistribution of DBCSR blocks and distributed matrix slices.
subroutine, public cp_array_to_dbcsr_slices(array, nrows, ncols, para_env_sub, first_row, last_row, template, matrices, matrix_ptrs)
Consume a distributed flattened array and create a DBCSR matrix per slice.
Basic linear algebra operations for full matrices.
subroutine, public cp_fm_geadd(alpha, trans, matrix_a, beta, matrix_b)
interface to BLACS geadd: matrix_b = beta*matrix_b + alpha*opt(matrix_a) where opt(matrix_a) can be e...
Redistribution of full matrices and distributed arrays.
subroutine, public cp_fm_to_array(mat2d, my_start_row, my_end_row, my_start_col, my_end_col, fm_mat)
Extract distributed rectangular array tiles from a full matrix.
subroutine, public cp_array_to_fm(mat2d, fm_struct, my_start_row, my_end_row, my_start_col, my_end_col, fm_mat, do_release_mat)
Assemble a full matrix from distributed rectangular array tiles.
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
Types to describe group distributions.
Library choices for electronic integral APIs.
pure logical function, public compare_potential_types(potential1, potential2)
Compare the parameters defining two radial operators.
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Interface to the message passing library MPI.
Routines for calculating RI-MP2 gradients.
subroutine, public complete_gamma(mp2_env, b_ia_q, dimen_ri, homo, virtual, para_env, para_env_sub, my_group_l_size, my_group_l_start, my_group_l_end, my_b_size, my_b_virtual_start, gd_b_virtual, kspin)
complete the calculation of the Gamma matrices
Types needed for MP2 calculations.
Definition mp2_types.F:14
basic linear algebra operations for full matrixes
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
keeps the information about the structure of a full matrix
represent a full matrix
stores all the informations relevant to an mpi environment