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 Build up the plane wave density by collocating the primitive Gaussian
10 : !> functions (pgf).
11 : !> \par History
12 : !> Joost VandeVondele (02.2002)
13 : !> 1) rewrote collocate_pgf for increased accuracy and speed
14 : !> 2) collocate_core hack for PGI compiler
15 : !> 3) added multiple grid feature
16 : !> 4) new way to go over the grid
17 : !> Joost VandeVondele (05.2002)
18 : !> 1) prelim. introduction of the real space grid type
19 : !> JGH [30.08.02] multigrid arrays independent from potential
20 : !> JGH [17.07.03] distributed real space code
21 : !> JGH [23.11.03] refactoring and new loop ordering
22 : !> JGH [04.12.03] OpneMP parallelization of main loops
23 : !> Joost VandeVondele (12.2003)
24 : !> 1) modified to compute tau
25 : !> Joost removed incremental build feature
26 : !> Joost introduced map consistent
27 : !> Rewrote grid integration/collocation routines, [Joost VandeVondele,03.2007]
28 : !> JGH [26.06.15] modification to allow for k-points
29 : !> \author Matthias Krack (03.04.2001)
30 : ! **************************************************************************************************
31 : MODULE qs_integrate_potential_product
32 : USE ao_util, ONLY: exp_radius_very_extended
33 : USE atomic_kind_types, ONLY: atomic_kind_type,&
34 : get_atomic_kind_set
35 : USE basis_set_types, ONLY: get_gto_basis_set,&
36 : gto_basis_set_type
37 : USE block_p_types, ONLY: block_p_type
38 : USE cell_types, ONLY: cell_type,&
39 : pbc
40 : USE cp_control_types, ONLY: dft_control_type
41 : USE cp_dbcsr_api, ONLY: dbcsr_copy,&
42 : dbcsr_finalize,&
43 : dbcsr_get_block_p,&
44 : dbcsr_get_info,&
45 : dbcsr_p_type,&
46 : dbcsr_type
47 : USE cp_dbcsr_operations, ONLY: dbcsr_deallocate_matrix_set
48 : USE gaussian_gridlevels, ONLY: gridlevel_info_type
49 : USE grid_api, ONLY: grid_integrate_task_list,&
50 : integrate_pgf_product
51 : USE kinds, ONLY: default_string_length,&
52 : dp
53 : USE message_passing, ONLY: mp_comm_type
54 : USE orbital_pointers, ONLY: ncoset
55 : USE particle_types, ONLY: particle_type
56 : USE pw_env_types, ONLY: pw_env_get,&
57 : pw_env_type
58 : USE pw_types, ONLY: pw_r3d_rs_type
59 : USE qs_environment_types, ONLY: get_qs_env,&
60 : qs_environment_type
61 : USE qs_force_types, ONLY: qs_force_type
62 : USE qs_kind_types, ONLY: get_qs_kind,&
63 : get_qs_kind_set,&
64 : qs_kind_type
65 : USE realspace_grid_types, ONLY: realspace_grid_desc_p_type,&
66 : realspace_grid_type
67 : USE rs_pw_interface, ONLY: potential_pw2rs
68 : USE task_list_methods, ONLY: rs_distribute_matrix,&
69 : rs_gather_matrices,&
70 : rs_scatter_matrices
71 : USE task_list_types, ONLY: atom_pair_type,&
72 : task_list_type,&
73 : task_type
74 : USE virial_types, ONLY: virial_type
75 :
76 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
77 : !$ USE OMP_LIB, ONLY: omp_lock_kind, &
78 : !$ omp_init_lock, omp_set_lock, &
79 : !$ omp_unset_lock, omp_destroy_lock
80 : #include "./base/base_uses.f90"
81 :
82 : IMPLICIT NONE
83 :
84 : PRIVATE
85 :
86 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_integrate_potential_product'
87 :
88 : ! *** Public subroutines ***
89 : ! *** Don't include this routines directly, use the interface to
90 : ! *** qs_integrate_potential
91 :
92 : PUBLIC :: integrate_v_rspace
93 : PUBLIC :: integrate_v_dbasis
94 :
95 : CONTAINS
96 :
97 : ! **************************************************************************************************
98 : !> \brief Integrate a potential v_rspace over the derivatives of the basis functions
99 : !> < da/dR | V | b > + < a | V | db/dR >
100 : !> Adapted from the old version of integrate_v_rspace (ED)
101 : !> \param v_rspace ...
102 : !> \param matrix_vhxc_dbasis ...
103 : !> \param matrix_p ...
104 : !> \param qs_env ...
105 : !> \param lambda The atom index.
106 : ! **************************************************************************************************
107 84 : SUBROUTINE integrate_v_dbasis(v_rspace, matrix_vhxc_dbasis, matrix_p, qs_env, lambda)
108 : TYPE(pw_r3d_rs_type), INTENT(IN) :: v_rspace
109 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_vhxc_dbasis
110 : TYPE(dbcsr_type), POINTER :: matrix_p
111 : TYPE(qs_environment_type), POINTER :: qs_env
112 : INTEGER, INTENT(IN) :: lambda
113 :
114 : CHARACTER(len=*), PARAMETER :: routineN = 'integrate_v_dbasis'
115 :
116 : INTEGER :: bcol, brow, handle, i, iatom, igrid_level, ikind, ikind_old, ilevel, img, ipair, &
117 : ipgf, ipgf_new, iset, iset_new, iset_old, itask, ithread, jatom, jkind, jkind_old, jpgf, &
118 : jpgf_new, jset, jset_new, jset_old, maxco, maxpgf, maxset, maxsgf_set, na1, na2, natom, &
119 : nb1, nb2, ncoa, ncob, nimages, nkind, nseta, nsetb, nthread, offset, sgfa, sgfb
120 84 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: block_touched
121 84 : INTEGER, DIMENSION(:), POINTER :: col_blk_size, la_max, la_min, lb_max, &
122 168 : lb_min, npgfa, npgfb, nsgfa, nsgfb, &
123 84 : row_blk_size
124 84 : INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, first_sgfb
125 : LOGICAL :: atom_pair_changed, atom_pair_done, dh_duplicated, found, my_compute_tau, &
126 : new_set_pair_coming, pab_required, redistribute_matrices, use_subpatch
127 : REAL(KIND=dp) :: eps_rho_rspace, f, prefactor, radius, &
128 : scalef, zetp
129 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
130 84 : TARGET :: h_buffer
131 : REAL(KIND=dp), DIMENSION(3) :: force_a, force_b, ra, rab, rab_inv, rb, &
132 : rp
133 : REAL(KIND=dp), DIMENSION(3, 3) :: my_virial_a, my_virial_b
134 84 : REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
135 168 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: h_block, hab, p_block, pab, rpgfa, &
136 168 : rpgfb, sphi_a, sphi_b, work, zeta, zetb
137 252 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: habt, hadb, hdab, pabt, workt
138 84 : REAL(kind=dp), DIMENSION(:, :, :, :), POINTER :: hadbt, hdabt
139 84 : TYPE(atom_pair_type), DIMENSION(:), POINTER :: atom_pair_recv, atom_pair_send
140 84 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
141 336 : TYPE(block_p_type) :: vhxc_block(3)
142 : TYPE(cell_type), POINTER :: cell
143 168 : TYPE(dbcsr_p_type) :: matrix_image(1)
144 84 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: deltap
145 : TYPE(dft_control_type), POINTER :: dft_control
146 : TYPE(gridlevel_info_type), POINTER :: gridlevel_info
147 : TYPE(gto_basis_set_type), POINTER :: orb_basis_set
148 : TYPE(mp_comm_type) :: group
149 84 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
150 : TYPE(pw_env_type), POINTER :: pw_env
151 84 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
152 84 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
153 : TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
154 84 : POINTER :: rs_descs
155 84 : TYPE(realspace_grid_type), DIMENSION(:), POINTER :: rs_rho
156 : TYPE(task_list_type), POINTER :: task_list
157 84 : TYPE(task_type), DIMENSION(:), POINTER :: tasks
158 :
159 84 : CALL timeset(routineN, handle)
160 84 : NULLIFY (pw_env)
161 :
162 : ! get the task lists
163 84 : CALL get_qs_env(qs_env=qs_env, task_list=task_list)
164 84 : CPASSERT(ASSOCIATED(task_list))
165 :
166 : ! the information on the grids is provided through pw_env
167 : ! pw_env has to be the parent env for the potential grid (input)
168 : ! there is an option to provide an external grid
169 84 : CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
170 :
171 : ! *** assign from pw_env
172 84 : gridlevel_info => pw_env%gridlevel_info
173 :
174 : ! get all the general information on the system we are working on
175 : CALL get_qs_env(qs_env=qs_env, &
176 : atomic_kind_set=atomic_kind_set, &
177 : qs_kind_set=qs_kind_set, &
178 : cell=cell, &
179 : natom=natom, &
180 : dft_control=dft_control, &
181 84 : particle_set=particle_set)
182 :
183 : ! GAPW not implemented here
184 84 : CPASSERT(.NOT. dft_control%qs_control%gapw)
185 : ! The density input contains a single matrix image.
186 84 : nimages = dft_control%nimages
187 84 : CPASSERT(nimages == 1)
188 :
189 : ! *** set up the rs multi-grids
190 84 : CPASSERT(ASSOCIATED(pw_env))
191 84 : CALL pw_env_get(pw_env, rs_descs=rs_descs, rs_grids=rs_rho)
192 84 : redistribute_matrices = ASSOCIATED(task_list%atom_pair_send)
193 : ! get mpi group from rs_rho
194 84 : group = rs_rho(1)%desc%group
195 :
196 : ! transform the potential on the rs_multigrids
197 84 : CALL potential_pw2rs(rs_rho, v_rspace, pw_env)
198 :
199 84 : nkind = SIZE(qs_kind_set)
200 :
201 : CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
202 : maxco=maxco, &
203 : maxsgf_set=maxsgf_set, &
204 84 : basis_type="ORB")
205 :
206 : ! short cuts to task list variables
207 84 : tasks => task_list%tasks
208 84 : atom_pair_send => task_list%atom_pair_send
209 84 : atom_pair_recv => task_list%atom_pair_recv
210 :
211 : ! needs to be consistent with rho_rspace
212 84 : eps_rho_rspace = dft_control%qs_control%eps_rho_rspace
213 :
214 : ! *** Initialize working density matrix ***
215 : ! Remote tasks need a working matrix containing the received density blocks.
216 336 : ALLOCATE (deltap(dft_control%nimages))
217 84 : IF (redistribute_matrices) THEN
218 84 : ALLOCATE (deltap(1)%matrix)
219 : ! this matrix has no strict sparsity pattern in parallel
220 : ! deltap%sparsity_id=-1
221 84 : CALL dbcsr_copy(deltap(1)%matrix, matrix_p, name="DeltaP")
222 : ELSE
223 0 : deltap(1)%matrix => matrix_p
224 : END IF
225 84 : nthread = 1
226 84 : !$ nthread = omp_get_max_threads()
227 :
228 : ! *** Allocate work storage ***
229 84 : NULLIFY (pabt, habt, workt)
230 420 : ALLOCATE (habt(maxco, maxco, 0:nthread))
231 420 : ALLOCATE (workt(maxco, maxsgf_set, 0:nthread))
232 420 : ALLOCATE (hdabt(3, maxco, maxco, 0:nthread))
233 336 : ALLOCATE (hadbt(3, maxco, maxco, 0:nthread))
234 336 : ALLOCATE (pabt(maxco, maxco, 0:nthread))
235 :
236 84 : IF (redistribute_matrices) THEN
237 : CALL rs_distribute_matrix(rs_descs, deltap, atom_pair_send, atom_pair_recv, &
238 84 : nimages, scatter=.TRUE.)
239 : ! Derivative integrals are accumulated on task ranks before returning to matrix owners.
240 252 : ALLOCATE (h_buffer(task_list%buffer_size_recv, 3))
241 84 : h_buffer(:, :) = 0.0_dp
242 84 : CALL dbcsr_get_info(matrix_p, row_blk_size=row_blk_size, col_blk_size=col_blk_size)
243 : END IF
244 :
245 : !$OMP PARALLEL DEFAULT(NONE), &
246 : !$OMP SHARED(workt,habt,hdabt,hadbt,pabt,tasks,particle_set,natom,maxset), &
247 : !$OMP SHARED(maxpgf,matrix_vhxc_dbasis,deltap), &
248 : !$OMP SHARED(pab_required,ncoset,rs_rho,my_compute_tau), &
249 : !$OMP SHARED(eps_rho_rspace,force,cell), &
250 : !$OMP SHARED(gridlevel_info,task_list,block_touched,nthread,qs_kind_set), &
251 : !$OMP SHARED(nimages,lambda, dh_duplicated), &
252 : !$OMP SHARED(redistribute_matrices,h_buffer,row_blk_size,col_blk_size), &
253 : !$OMP PRIVATE(ithread,work,hab,hdab,hadb,pab,iset_old,jset_old), &
254 : !$OMP PRIVATE(ikind_old,jkind_old,iatom,jatom,iset,jset,ikind,jkind,ilevel,ipgf,jpgf), &
255 : !$OMP PRIVATE(img,brow,bcol,orb_basis_set,first_sgfa,la_max,la_min,npgfa,nseta,nsgfa), &
256 : !$OMP PRIVATE(rpgfa,set_radius_a,sphi_a,zeta,first_sgfb,lb_max,lb_min,npgfb), &
257 : !$OMP PRIVATE(nsetb,nsgfb,rpgfb,set_radius_b,sphi_b,zetb,found), &
258 : !$OMP PRIVATE(force_a,force_b,my_virial_a,my_virial_b,atom_pair_changed,h_block, vhxc_block), &
259 : !$OMP PRIVATE(p_block,ncoa,sgfa,ncob,sgfb,rab,ra,rb,rp,zetp,f,prefactor,radius,igrid_level), &
260 : !$OMP PRIVATE(na1,na2,nb1,nb2,use_subpatch,rab_inv,new_set_pair_coming,atom_pair_done), &
261 : !$OMP PRIVATE(iset_new,jset_new,ipgf_new,jpgf_new,scalef), &
262 84 : !$OMP PRIVATE(itask,offset)
263 :
264 : ithread = 0
265 : !$ ithread = omp_get_thread_num()
266 : work => workt(:, :, ithread)
267 : hab => habt(:, :, ithread)
268 : pab => pabt(:, :, ithread)
269 : hdab => hdabt(:, :, :, ithread)
270 : hadb => hadbt(:, :, :, ithread)
271 :
272 : iset_old = -1; jset_old = -1
273 : ikind_old = -1; jkind_old = -1
274 :
275 : ! Here we loop over gridlevels first, finalising the matrix after each grid level is
276 : ! completed. On each grid level, we loop over atom pairs, which will only access
277 : ! a single block of each matrix, so with OpenMP, each matrix block is only touched
278 : ! by a single thread for each grid level
279 : loop_gridlevels: DO igrid_level = 1, gridlevel_info%ngrid_levels
280 : !$OMP BARRIER
281 : !$OMP DO schedule (dynamic, MAX(1,task_list%npairs(igrid_level)/(nthread*50)))
282 : loop_pairs: DO ipair = 1, task_list%npairs(igrid_level)
283 : loop_tasks: DO itask = task_list%taskstart(ipair, igrid_level), task_list%taskstop(ipair, igrid_level)
284 : ilevel = tasks(itask)%grid_level
285 : img = tasks(itask)%image
286 : iatom = tasks(itask)%iatom
287 : jatom = tasks(itask)%jatom
288 : iset = tasks(itask)%iset
289 : jset = tasks(itask)%jset
290 : ipgf = tasks(itask)%ipgf
291 : jpgf = tasks(itask)%jpgf
292 :
293 : ! At the start of a block of tasks, get atom data (and kind data, if needed)
294 : IF (itask == task_list%taskstart(ipair, igrid_level)) THEN
295 :
296 : ikind = particle_set(iatom)%atomic_kind%kind_number
297 : jkind = particle_set(jatom)%atomic_kind%kind_number
298 :
299 : ra(:) = pbc(particle_set(iatom)%r, cell)
300 :
301 : IF (iatom <= jatom) THEN
302 : brow = iatom
303 : bcol = jatom
304 : ELSE
305 : brow = jatom
306 : bcol = iatom
307 : END IF
308 :
309 : IF (ikind /= ikind_old) THEN
310 : CALL get_qs_kind(qs_kind_set(ikind), &
311 : basis_set=orb_basis_set, basis_type="ORB")
312 :
313 : CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
314 : first_sgf=first_sgfa, &
315 : lmax=la_max, &
316 : lmin=la_min, &
317 : npgf=npgfa, &
318 : nset=nseta, &
319 : nsgf_set=nsgfa, &
320 : pgf_radius=rpgfa, &
321 : set_radius=set_radius_a, &
322 : sphi=sphi_a, &
323 : zet=zeta)
324 : END IF
325 :
326 : IF (jkind /= jkind_old) THEN
327 : CALL get_qs_kind(qs_kind_set(jkind), &
328 : basis_set=orb_basis_set, basis_type="ORB")
329 : CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
330 : first_sgf=first_sgfb, &
331 : lmax=lb_max, &
332 : lmin=lb_min, &
333 : npgf=npgfb, &
334 : nset=nsetb, &
335 : nsgf_set=nsgfb, &
336 : pgf_radius=rpgfb, &
337 : set_radius=set_radius_b, &
338 : sphi=sphi_b, &
339 : zet=zetb)
340 :
341 : END IF
342 :
343 : DO i = 1, 3
344 : NULLIFY (vhxc_block(i)%block)
345 : IF (redistribute_matrices) THEN
346 : offset = task_list%pair_offsets_recv(tasks(itask)%pair_index)
347 : vhxc_block(i)%block(1:row_blk_size(brow), 1:col_blk_size(bcol)) => &
348 : h_buffer(offset + 1:offset + row_blk_size(brow)*col_blk_size(bcol), i)
349 : ELSE
350 : CALL dbcsr_get_block_p(matrix_vhxc_dbasis(i)%matrix, brow, bcol, vhxc_block(i)%block, found)
351 : CPASSERT(found)
352 : END IF
353 : END DO
354 :
355 : CALL dbcsr_get_block_p(matrix=deltap(img)%matrix, &
356 : row=brow, col=bcol, BLOCK=p_block, found=found)
357 : CPASSERT(found)
358 :
359 : ikind_old = ikind
360 : jkind_old = jkind
361 :
362 : atom_pair_changed = .TRUE.
363 :
364 : ELSE
365 :
366 : atom_pair_changed = .FALSE.
367 :
368 : END IF
369 :
370 : IF (atom_pair_changed .OR. iset_old /= iset .OR. jset_old /= jset) THEN
371 :
372 : ncoa = npgfa(iset)*ncoset(la_max(iset))
373 : sgfa = first_sgfa(1, iset)
374 : ncob = npgfb(jset)*ncoset(lb_max(jset))
375 : sgfb = first_sgfb(1, jset)
376 :
377 : IF (iatom <= jatom) THEN
378 : work(1:ncoa, 1:nsgfb(jset)) = MATMUL(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1), &
379 : p_block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1))
380 : pab(1:ncoa, 1:ncob) = MATMUL(work(1:ncoa, 1:nsgfb(jset)), TRANSPOSE(sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1)))
381 : ELSE
382 : work(1:ncob, 1:nsgfa(iset)) = MATMUL(sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1), &
383 : p_block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1))
384 : pab(1:ncob, 1:ncoa) = MATMUL(work(1:ncob, 1:nsgfa(iset)), TRANSPOSE(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1)))
385 : END IF
386 :
387 : IF (iatom <= jatom) THEN
388 : hab(1:ncoa, 1:ncob) = 0._dp
389 : hdab(:, 1:ncoa, 1:ncob) = 0._dp
390 : hadb(:, 1:ncoa, 1:ncob) = 0._dp
391 : ELSE
392 : hab(1:ncob, 1:ncoa) = 0._dp
393 : hdab(:, 1:ncob, 1:ncoa) = 0._dp
394 : hadb(:, 1:ncob, 1:ncoa) = 0._dp
395 : END IF
396 :
397 : iset_old = iset
398 : jset_old = jset
399 :
400 : END IF
401 :
402 : rab = tasks(itask)%rab
403 : rb(:) = ra(:) + rab(:)
404 : zetp = zeta(ipgf, iset) + zetb(jpgf, jset)
405 :
406 : f = zetb(jpgf, jset)/zetp
407 : rp(:) = ra(:) + f*rab(:)
408 : prefactor = EXP(-zeta(ipgf, iset)*f*DOT_PRODUCT(rab, rab))
409 : radius = exp_radius_very_extended(la_min=la_min(iset), la_max=la_max(iset), &
410 : lb_min=lb_min(jset), lb_max=lb_max(jset), &
411 : ra=ra, rb=rb, rp=rp, &
412 : zetp=zetp, eps=eps_rho_rspace, &
413 : prefactor=prefactor, cutoff=1.0_dp)
414 :
415 : na1 = (ipgf - 1)*ncoset(la_max(iset)) + 1
416 : na2 = ipgf*ncoset(la_max(iset))
417 : nb1 = (jpgf - 1)*ncoset(lb_max(jset)) + 1
418 : nb2 = jpgf*ncoset(lb_max(jset))
419 :
420 : ! check whether we need to use fawzi's generalised collocation scheme
421 : IF (rs_rho(igrid_level)%desc%distributed) THEN
422 : !tasks(4,:) is 0 for replicated, 1 for distributed 2 for exceptional distributed tasks
423 : IF (tasks(itask)%dist_type == 2) THEN
424 : use_subpatch = .TRUE.
425 : ELSE
426 : use_subpatch = .FALSE.
427 : END IF
428 : ELSE
429 : use_subpatch = .FALSE.
430 : END IF
431 :
432 : IF (iatom <= jatom) THEN
433 : IF (iatom == lambda) THEN
434 : CALL integrate_pgf_product( &
435 : la_max(iset), zeta(ipgf, iset), la_min(iset), &
436 : lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
437 : ra, rab, rs_rho(igrid_level), &
438 : hab, o1=na1 - 1, o2=nb1 - 1, &
439 : radius=radius, &
440 : calculate_forces=.TRUE., &
441 : compute_tau=.FALSE., &
442 : use_subpatch=use_subpatch, subpatch_pattern=tasks(itask)%subpatch_pattern, &
443 : hdab=hdab, pab=pab)
444 : END IF
445 : IF (jatom == lambda) THEN
446 : CALL integrate_pgf_product( &
447 : la_max(iset), zeta(ipgf, iset), la_min(iset), &
448 : lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
449 : ra, rab, rs_rho(igrid_level), &
450 : hab, o1=na1 - 1, o2=nb1 - 1, &
451 : radius=radius, &
452 : calculate_forces=.TRUE., &
453 : compute_tau=.FALSE., &
454 : use_subpatch=use_subpatch, subpatch_pattern=tasks(itask)%subpatch_pattern, &
455 : hadb=hadb, pab=pab)
456 : END IF
457 : ELSE
458 : rab_inv = -rab
459 : IF (iatom == lambda) THEN
460 : CALL integrate_pgf_product( &
461 : lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
462 : la_max(iset), zeta(ipgf, iset), la_min(iset), &
463 : rb, rab_inv, rs_rho(igrid_level), &
464 : hab, o1=nb1 - 1, o2=na1 - 1, &
465 : radius=radius, &
466 : calculate_forces=.TRUE., &
467 : force_a=force_b, force_b=force_a, &
468 : compute_tau=.FALSE., &
469 : use_subpatch=use_subpatch, subpatch_pattern=tasks(itask)%subpatch_pattern, &
470 : hadb=hadb, pab=pab)
471 : END IF
472 : IF (jatom == lambda) THEN
473 : CALL integrate_pgf_product( &
474 : lb_max(jset), zetb(jpgf, jset), lb_min(jset), &
475 : la_max(iset), zeta(ipgf, iset), la_min(iset), &
476 : rb, rab_inv, rs_rho(igrid_level), &
477 : hab, o1=nb1 - 1, o2=na1 - 1, &
478 : radius=radius, &
479 : calculate_forces=.TRUE., &
480 : force_a=force_b, force_b=force_a, &
481 : compute_tau=.FALSE., &
482 : use_subpatch=use_subpatch, subpatch_pattern=tasks(itask)%subpatch_pattern, &
483 : hdab=hdab, pab=pab)
484 : END IF
485 : END IF
486 :
487 : new_set_pair_coming = .FALSE.
488 : atom_pair_done = .FALSE.
489 : IF (itask < task_list%taskstop(ipair, igrid_level)) THEN
490 : ilevel = tasks(itask + 1)%grid_level
491 : img = tasks(itask + 1)%image
492 : iatom = tasks(itask + 1)%iatom
493 : jatom = tasks(itask + 1)%jatom
494 : iset_new = tasks(itask + 1)%iset
495 : jset_new = tasks(itask + 1)%jset
496 : ipgf_new = tasks(itask + 1)%ipgf
497 : jpgf_new = tasks(itask + 1)%jpgf
498 : IF (iset_new /= iset .OR. jset_new /= jset) THEN
499 : new_set_pair_coming = .TRUE.
500 : END IF
501 : ELSE
502 : ! do not forget the last block
503 : new_set_pair_coming = .TRUE.
504 : atom_pair_done = .TRUE.
505 : END IF
506 :
507 : IF (new_set_pair_coming) THEN
508 :
509 : DO i = 1, 3
510 : hdab(i, :, :) = hdab(i, :, :) + hadb(i, :, :)
511 : IF (iatom <= jatom) THEN
512 : work(1:ncoa, 1:nsgfb(jset)) = MATMUL(hdab(i, 1:ncoa, 1:ncob), sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1))
513 : vhxc_block(i)%block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) = &
514 : vhxc_block(i)%block(sgfa:sgfa + nsgfa(iset) - 1, sgfb:sgfb + nsgfb(jset) - 1) + &
515 : MATMUL(TRANSPOSE(sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1)), work(1:ncoa, 1:nsgfb(jset)))
516 : ELSE
517 : work(1:ncob, 1:nsgfa(iset)) = MATMUL(hdab(i, 1:ncob, 1:ncoa), sphi_a(1:ncoa, sgfa:sgfa + nsgfa(iset) - 1))
518 : vhxc_block(i)%block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) = &
519 : vhxc_block(i)%block(sgfb:sgfb + nsgfb(jset) - 1, sgfa:sgfa + nsgfa(iset) - 1) + &
520 : MATMUL(TRANSPOSE(sphi_b(1:ncob, sgfb:sgfb + nsgfb(jset) - 1)), work(1:ncob, 1:nsgfa(iset)))
521 : END IF
522 : END DO
523 : END IF ! new_set_pair_coming
524 :
525 : END DO loop_tasks
526 : END DO loop_pairs
527 : !$OMP END DO
528 :
529 : IF (.NOT. redistribute_matrices) THEN
530 : DO i = 1, 3
531 : CALL dbcsr_finalize(matrix_vhxc_dbasis(i)%matrix)
532 : END DO
533 : END IF
534 :
535 : END DO loop_gridlevels
536 :
537 : !$OMP END PARALLEL
538 :
539 84 : IF (redistribute_matrices) THEN
540 336 : DO i = 1, 3
541 8316 : task_list%hab_buffer%host_buffer(:) = h_buffer(:, i)
542 252 : matrix_image(1)%matrix => matrix_vhxc_dbasis(i)%matrix
543 336 : CALL rs_gather_matrices(task_list%hab_buffer, matrix_image, task_list, group)
544 : END DO
545 : END IF
546 :
547 : IF (redistribute_matrices) THEN
548 84 : CALL dbcsr_deallocate_matrix_set(deltap)
549 : ELSE
550 0 : DO img = 1, dft_control%nimages
551 0 : NULLIFY (deltap(img)%matrix)
552 : END DO
553 0 : DEALLOCATE (deltap)
554 : END IF
555 :
556 84 : DEALLOCATE (pabt, habt, workt, hdabt, hadbt)
557 :
558 84 : CALL timestop(handle)
559 420 : END SUBROUTINE integrate_v_dbasis
560 :
561 : ! **************************************************************************************************
562 : !> \brief computes matrix elements corresponding to a given potential
563 : !> If compute_tau
564 : !> hmat(\mu,\nu) = \int v_rspace(r)\nabla\mu(r)\cdot\nabla\nu(r) dr
565 : !> else
566 : !> hmat(\mu,\nu) = \int v_rspace(r)\mu(r)\nu(r) dr
567 : !> \param v_rspace Potential in real space
568 : !> \param hmat Matrix to write the result
569 : !> \param hmat_kp ...
570 : !> \param pmat Input density matrix, not used in the calculation but the screening. See note below.
571 : !> \param pmat_kp ...
572 : !> \param qs_env ...
573 : !> \param calculate_forces ...
574 : !> \param force_adm optional scaling of force
575 : !> \param compute_tau ...
576 : !> \param gapw ...
577 : !> \param basis_type ...
578 : !> \param pw_env_external ...
579 : !> \param task_list_external ...
580 : !> \par History
581 : !> IAB (29-Apr-2010): Added OpenMP parallelisation to task loop
582 : !> (c) The Numerical Algorithms Group (NAG) Ltd, 2010 on behalf of the HECToR project
583 : !> Some refactoring, get priorities for options correct (JGH, 04.2014)
584 : !> Added options to allow for k-points
585 : !> For a smooth transition we allow for old and new (vector) matrices (hmat, pmat) (JGH, 06.2015)
586 : !> \note
587 : !> integrates a given potential (or other object on a real
588 : !> space grid) = v_rspace using a multi grid technique (mgrid_*)
589 : !> over the basis set producing a number for every element of h
590 : !> (should have the same sparsity structure of S)
591 : !> additional screening is available using the magnitude of the
592 : !> elements in p (? I'm not sure this is a very good idea)
593 : !> this argument is optional
594 : !> derivatives of these matrix elements with respect to the ionic
595 : !> coordinates can be computed as well
596 : ! **************************************************************************************************
597 247796 : SUBROUTINE integrate_v_rspace(v_rspace, hmat, hmat_kp, pmat, pmat_kp, &
598 : qs_env, calculate_forces, force_adm, &
599 : compute_tau, gapw, basis_type, pw_env_external, task_list_external)
600 :
601 : TYPE(pw_r3d_rs_type), INTENT(IN) :: v_rspace
602 : TYPE(dbcsr_p_type), INTENT(INOUT), OPTIONAL :: hmat
603 : TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
604 : POINTER :: hmat_kp
605 : TYPE(dbcsr_p_type), INTENT(IN), OPTIONAL :: pmat
606 : TYPE(dbcsr_p_type), DIMENSION(:), OPTIONAL, &
607 : POINTER :: pmat_kp
608 : TYPE(qs_environment_type), POINTER :: qs_env
609 : LOGICAL, INTENT(IN) :: calculate_forces
610 : REAL(KIND=dp), INTENT(IN), OPTIONAL :: force_adm
611 : LOGICAL, INTENT(IN), OPTIONAL :: compute_tau, gapw
612 : CHARACTER(len=*), INTENT(IN), OPTIONAL :: basis_type
613 : TYPE(pw_env_type), OPTIONAL, POINTER :: pw_env_external
614 : TYPE(task_list_type), OPTIONAL, POINTER :: task_list_external
615 :
616 : CHARACTER(len=*), PARAMETER :: routineN = 'integrate_v_rspace'
617 :
618 : CHARACTER(len=default_string_length) :: my_basis_type
619 : INTEGER :: atom_a, handle, iatom, ikind, img, &
620 : maxco, maxsgf_set, natoms, nimages, &
621 : nkind
622 247796 : INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind, kind_of
623 : LOGICAL :: calculate_virial, do_kp, my_compute_tau, &
624 : my_gapw, pab_required
625 : REAL(KIND=dp) :: admm_scal_fac
626 247796 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: forces_array
627 : REAL(KIND=dp), DIMENSION(3, 3) :: virial_matrix
628 247796 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
629 : TYPE(cell_type), POINTER :: cell
630 247796 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: deltap, dhmat
631 : TYPE(dft_control_type), POINTER :: dft_control
632 : TYPE(mp_comm_type) :: group
633 247796 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
634 : TYPE(pw_env_type), POINTER :: pw_env
635 247796 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
636 247796 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
637 247796 : TYPE(realspace_grid_type), DIMENSION(:), POINTER :: rs_v
638 : TYPE(task_list_type), POINTER :: task_list, task_list_soft
639 : TYPE(virial_type), POINTER :: virial
640 :
641 247796 : CALL timeset(routineN, handle)
642 :
643 : ! we test here if the provided operator matrices are consistent
644 247796 : CPASSERT(PRESENT(hmat) .OR. PRESENT(hmat_kp))
645 247796 : do_kp = .FALSE.
646 247796 : IF (PRESENT(hmat_kp)) do_kp = .TRUE.
647 247796 : IF (PRESENT(pmat)) THEN
648 36838 : CPASSERT(PRESENT(hmat))
649 210958 : ELSE IF (PRESENT(pmat_kp)) THEN
650 173598 : CPASSERT(PRESENT(hmat_kp))
651 : END IF
652 :
653 247796 : NULLIFY (pw_env)
654 :
655 : ! this routine works in two modes:
656 : ! normal mode : <a| V | b>
657 : ! tau mode : < nabla a| V | nabla b>
658 247796 : my_compute_tau = .FALSE.
659 247796 : IF (PRESENT(compute_tau)) my_compute_tau = compute_tau
660 :
661 : ! this sets the basis set to be used. GAPW(==soft basis) overwrites basis_type
662 : ! default is "ORB"
663 247796 : my_gapw = .FALSE.
664 247796 : IF (PRESENT(gapw)) my_gapw = gapw
665 247796 : IF (PRESENT(basis_type)) THEN
666 17362 : my_basis_type = basis_type
667 : ELSE
668 230434 : my_basis_type = "ORB"
669 : END IF
670 :
671 : ! get the task lists
672 : ! task lists have to be in sync with basis sets
673 : ! there is an option to provide the task list from outside (not through qs_env)
674 : ! outside option has highest priority
675 : CALL get_qs_env(qs_env=qs_env, &
676 : task_list=task_list, &
677 247796 : task_list_soft=task_list_soft)
678 247796 : IF (.NOT. my_basis_type == "ORB") THEN
679 15470 : CPASSERT(PRESENT(task_list_external))
680 : END IF
681 247796 : IF (my_gapw) task_list => task_list_soft
682 247796 : IF (PRESENT(task_list_external)) task_list => task_list_external
683 247796 : CPASSERT(ASSOCIATED(task_list))
684 :
685 : ! the information on the grids is provided through pw_env
686 : ! pw_env has to be the parent env for the potential grid (input)
687 : ! there is an option to provide an external grid
688 247796 : CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
689 247796 : IF (PRESENT(pw_env_external)) pw_env => pw_env_external
690 :
691 : ! get all the general information on the system we are working on
692 : CALL get_qs_env(qs_env=qs_env, &
693 : atomic_kind_set=atomic_kind_set, &
694 : qs_kind_set=qs_kind_set, &
695 : cell=cell, &
696 : natom=natoms, &
697 : dft_control=dft_control, &
698 : particle_set=particle_set, &
699 : force=force, &
700 247796 : virial=virial)
701 :
702 247796 : admm_scal_fac = 1.0_dp
703 247796 : IF (PRESENT(force_adm)) admm_scal_fac = force_adm
704 :
705 247796 : CPASSERT(ASSOCIATED(pw_env))
706 247796 : CALL pw_env_get(pw_env, rs_grids=rs_v)
707 :
708 : ! get mpi group from rs_v
709 247796 : group = rs_v(1)%desc%group
710 :
711 : ! transform the potential on the rs_multigrids
712 247796 : CALL potential_pw2rs(rs_v, v_rspace, pw_env)
713 :
714 247796 : nimages = dft_control%nimages
715 247796 : IF (nimages > 1) THEN
716 12856 : CPASSERT(do_kp)
717 : END IF
718 247796 : nkind = SIZE(qs_kind_set)
719 247796 : calculate_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer) .AND. calculate_forces
720 247796 : pab_required = (PRESENT(pmat) .OR. PRESENT(pmat_kp)) .AND. calculate_forces
721 :
722 : CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
723 : maxco=maxco, &
724 : maxsgf_set=maxsgf_set, &
725 247796 : basis_type=my_basis_type)
726 :
727 743388 : ALLOCATE (forces_array(3, natoms))
728 :
729 247796 : IF (pab_required) THEN
730 : ! initialize the working pmat structures
731 146292 : ALLOCATE (deltap(nimages))
732 25623 : IF (do_kp) THEN
733 59730 : DO img = 1, nimages
734 59730 : deltap(img)%matrix => pmat_kp(img)%matrix
735 : END DO
736 : ELSE
737 17658 : deltap(1)%matrix => pmat%matrix
738 : END IF
739 :
740 : ! Distribute matrix blocks.
741 25623 : CALL rs_scatter_matrices(deltap, task_list%pab_buffer, task_list, group)
742 25623 : DEALLOCATE (deltap)
743 : END IF
744 :
745 : ! Map all tasks from the grids
746 : CALL grid_integrate_task_list(task_list=task_list%grid_task_list, &
747 : compute_tau=my_compute_tau, &
748 : calculate_forces=calculate_forces, &
749 : calculate_virial=calculate_virial, &
750 : pab_blocks=task_list%pab_buffer, &
751 : rs_grids=rs_v, &
752 : hab_blocks=task_list%hab_buffer, &
753 : forces=forces_array, &
754 247796 : virial=virial_matrix)
755 :
756 247796 : IF (calculate_forces) THEN
757 25623 : CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
758 : !$OMP PARALLEL DO DEFAULT(NONE) PRIVATE(atom_a, ikind) &
759 25623 : !$OMP SHARED(natoms, force, forces_array, atom_of_kind, kind_of, admm_scal_fac)
760 : DO iatom = 1, natoms
761 : atom_a = atom_of_kind(iatom)
762 : ikind = kind_of(iatom)
763 : force(ikind)%rho_elec(:, atom_a) = force(ikind)%rho_elec(:, atom_a) + admm_scal_fac*forces_array(:, iatom)
764 : END DO
765 : !$OMP END PARALLEL DO
766 25623 : DEALLOCATE (atom_of_kind, kind_of)
767 : END IF
768 :
769 247796 : IF (calculate_virial) THEN
770 51181 : virial%pv_virial = virial%pv_virial + admm_scal_fac*virial_matrix
771 : END IF
772 :
773 : ! Gather all matrix images into a single array.
774 2210458 : ALLOCATE (dhmat(nimages))
775 247796 : IF (PRESENT(hmat_kp)) THEN
776 174570 : CPASSERT(.NOT. PRESENT(hmat))
777 1568414 : DO img = 1, nimages
778 1568414 : dhmat(img)%matrix => hmat_kp(img)%matrix
779 : END DO
780 : ELSE
781 73226 : CPASSERT(PRESENT(hmat) .AND. nimages == 1)
782 73226 : dhmat(1)%matrix => hmat%matrix
783 : END IF
784 :
785 : ! Distribute matrix blocks.
786 247796 : CALL rs_gather_matrices(task_list%hab_buffer, dhmat, task_list, group)
787 247796 : DEALLOCATE (dhmat)
788 :
789 247796 : CALL timestop(handle)
790 :
791 743388 : END SUBROUTINE integrate_v_rspace
792 :
793 : END MODULE qs_integrate_potential_product
|