(git:27c966b)
Loading...
Searching...
No Matches
qs_integrate_potential_product.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 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! **************************************************************************************************
38 USE cell_types, ONLY: cell_type,&
39 pbc
41 USE cp_dbcsr_api, ONLY: dbcsr_copy,&
51 USE kinds, ONLY: default_string_length,&
52 dp
54 USE orbital_pointers, ONLY: ncoset
56 USE pw_env_types, ONLY: pw_env_get,&
58 USE pw_types, ONLY: pw_r3d_rs_type
62 USE qs_kind_types, ONLY: get_qs_kind,&
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
95CONTAINS
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 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 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: block_touched
121 INTEGER, DIMENSION(:), POINTER :: col_blk_size, la_max, la_min, lb_max, &
122 lb_min, npgfa, npgfb, nsgfa, nsgfb, &
123 row_blk_size
124 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 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 REAL(kind=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
135 REAL(kind=dp), DIMENSION(:, :), POINTER :: h_block, hab, p_block, pab, rpgfa, &
136 rpgfb, sphi_a, sphi_b, work, zeta, zetb
137 REAL(kind=dp), DIMENSION(:, :, :), POINTER :: habt, hadb, hdab, pabt, workt
138 REAL(kind=dp), DIMENSION(:, :, :, :), POINTER :: hadbt, hdabt
139 TYPE(atom_pair_type), DIMENSION(:), POINTER :: atom_pair_recv, atom_pair_send
140 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
141 TYPE(block_p_type) :: vhxc_block(3)
142 TYPE(cell_type), POINTER :: cell
143 TYPE(dbcsr_p_type) :: matrix_image(1)
144 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 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
150 TYPE(pw_env_type), POINTER :: pw_env
151 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
152 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
153 TYPE(realspace_grid_desc_p_type), DIMENSION(:), &
154 POINTER :: rs_descs
155 TYPE(realspace_grid_type), DIMENSION(:), POINTER :: rs_rho
156 TYPE(task_list_type), POINTER :: task_list
157 TYPE(task_type), DIMENSION(:), POINTER :: tasks
158
159 CALL timeset(routinen, handle)
160 NULLIFY (pw_env)
161
162 ! get the task lists
163 CALL get_qs_env(qs_env=qs_env, task_list=task_list)
164 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 CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
170
171 ! *** assign from pw_env
172 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 particle_set=particle_set)
182
183 ! GAPW not implemented here
184 cpassert(.NOT. dft_control%qs_control%gapw)
185 ! The density input contains a single matrix image.
186 nimages = dft_control%nimages
187 cpassert(nimages == 1)
188
189 ! *** set up the rs multi-grids
190 cpassert(ASSOCIATED(pw_env))
191 CALL pw_env_get(pw_env, rs_descs=rs_descs, rs_grids=rs_rho)
192 redistribute_matrices = ASSOCIATED(task_list%atom_pair_send)
193 ! get mpi group from rs_rho
194 group = rs_rho(1)%desc%group
195
196 ! transform the potential on the rs_multigrids
197 CALL potential_pw2rs(rs_rho, v_rspace, pw_env)
198
199 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 basis_type="ORB")
205
206 ! short cuts to task list variables
207 tasks => task_list%tasks
208 atom_pair_send => task_list%atom_pair_send
209 atom_pair_recv => task_list%atom_pair_recv
210
211 ! needs to be consistent with rho_rspace
212 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 ALLOCATE (deltap(dft_control%nimages))
217 IF (redistribute_matrices) THEN
218 ALLOCATE (deltap(1)%matrix)
219 ! this matrix has no strict sparsity pattern in parallel
220 ! deltap%sparsity_id=-1
221 CALL dbcsr_copy(deltap(1)%matrix, matrix_p, name="DeltaP")
222 ELSE
223 deltap(1)%matrix => matrix_p
224 END IF
225 nthread = 1
226!$ nthread = omp_get_max_threads()
227
228 ! *** Allocate work storage ***
229 NULLIFY (pabt, habt, workt)
230 ALLOCATE (habt(maxco, maxco, 0:nthread))
231 ALLOCATE (workt(maxco, maxsgf_set, 0:nthread))
232 ALLOCATE (hdabt(3, maxco, maxco, 0:nthread))
233 ALLOCATE (hadbt(3, maxco, maxco, 0:nthread))
234 ALLOCATE (pabt(maxco, maxco, 0:nthread))
235
236 IF (redistribute_matrices) THEN
237 CALL rs_distribute_matrix(rs_descs, deltap, atom_pair_send, atom_pair_recv, &
238 nimages, scatter=.true.)
239 ! Derivative integrals are accumulated on task ranks before returning to matrix owners.
240 ALLOCATE (h_buffer(task_list%buffer_size_recv, 3))
241 h_buffer(:, :) = 0.0_dp
242 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!$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
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
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
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
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 IF (redistribute_matrices) THEN
540 DO i = 1, 3
541 task_list%hab_buffer%host_buffer(:) = h_buffer(:, i)
542 matrix_image(1)%matrix => matrix_vhxc_dbasis(i)%matrix
543 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 CALL dbcsr_deallocate_matrix_set(deltap)
549 ELSE
550 DO img = 1, dft_control%nimages
551 NULLIFY (deltap(img)%matrix)
552 END DO
553 DEALLOCATE (deltap)
554 END IF
555
556 DEALLOCATE (pabt, habt, workt, hdabt, hadbt)
557
558 CALL timestop(handle)
559 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 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 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 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: forces_array
627 REAL(kind=dp), DIMENSION(3, 3) :: virial_matrix
628 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
629 TYPE(cell_type), POINTER :: cell
630 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: deltap, dhmat
631 TYPE(dft_control_type), POINTER :: dft_control
632 TYPE(mp_comm_type) :: group
633 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
634 TYPE(pw_env_type), POINTER :: pw_env
635 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
636 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
637 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 CALL timeset(routinen, handle)
642
643 ! we test here if the provided operator matrices are consistent
644 cpassert(PRESENT(hmat) .OR. PRESENT(hmat_kp))
645 do_kp = .false.
646 IF (PRESENT(hmat_kp)) do_kp = .true.
647 IF (PRESENT(pmat)) THEN
648 cpassert(PRESENT(hmat))
649 ELSE IF (PRESENT(pmat_kp)) THEN
650 cpassert(PRESENT(hmat_kp))
651 END IF
652
653 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 my_compute_tau = .false.
659 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 my_gapw = .false.
664 IF (PRESENT(gapw)) my_gapw = gapw
665 IF (PRESENT(basis_type)) THEN
666 my_basis_type = basis_type
667 ELSE
668 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 task_list_soft=task_list_soft)
678 IF (.NOT. my_basis_type == "ORB") THEN
679 cpassert(PRESENT(task_list_external))
680 END IF
681 IF (my_gapw) task_list => task_list_soft
682 IF (PRESENT(task_list_external)) task_list => task_list_external
683 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 CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
689 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 virial=virial)
701
702 admm_scal_fac = 1.0_dp
703 IF (PRESENT(force_adm)) admm_scal_fac = force_adm
704
705 cpassert(ASSOCIATED(pw_env))
706 CALL pw_env_get(pw_env, rs_grids=rs_v)
707
708 ! get mpi group from rs_v
709 group = rs_v(1)%desc%group
710
711 ! transform the potential on the rs_multigrids
712 CALL potential_pw2rs(rs_v, v_rspace, pw_env)
713
714 nimages = dft_control%nimages
715 IF (nimages > 1) THEN
716 cpassert(do_kp)
717 END IF
718 nkind = SIZE(qs_kind_set)
719 calculate_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer) .AND. calculate_forces
720 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 basis_type=my_basis_type)
726
727 ALLOCATE (forces_array(3, natoms))
728
729 IF (pab_required) THEN
730 ! initialize the working pmat structures
731 ALLOCATE (deltap(nimages))
732 IF (do_kp) THEN
733 DO img = 1, nimages
734 deltap(img)%matrix => pmat_kp(img)%matrix
735 END DO
736 ELSE
737 deltap(1)%matrix => pmat%matrix
738 END IF
739
740 ! Distribute matrix blocks.
741 CALL rs_scatter_matrices(deltap, task_list%pab_buffer, task_list, group)
742 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 virial=virial_matrix)
755
756 IF (calculate_forces) THEN
757 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!$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 DEALLOCATE (atom_of_kind, kind_of)
767 END IF
768
769 IF (calculate_virial) THEN
770 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 ALLOCATE (dhmat(nimages))
775 IF (PRESENT(hmat_kp)) THEN
776 cpassert(.NOT. PRESENT(hmat))
777 DO img = 1, nimages
778 dhmat(img)%matrix => hmat_kp(img)%matrix
779 END DO
780 ELSE
781 cpassert(PRESENT(hmat) .AND. nimages == 1)
782 dhmat(1)%matrix => hmat%matrix
783 END IF
784
785 ! Distribute matrix blocks.
786 CALL rs_gather_matrices(task_list%hab_buffer, dhmat, task_list, group)
787 DEALLOCATE (dhmat)
788
789 CALL timestop(handle)
790
791 END SUBROUTINE integrate_v_rspace
792
All kind of helpful little routines.
Definition ao_util.F:14
real(kind=dp) function, public exp_radius_very_extended(la_min, la_max, lb_min, lb_max, pab, o1, o2, ra, rb, rp, zetp, eps, prefactor, cutoff, epsabs)
computes the radius of the Gaussian outside of which it is smaller than eps
Definition ao_util.F:208
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind_set(atomic_kind_set, atom_of_kind, kind_of, natom_of_kind, maxatom, natom, nshell, fist_potential_present, shell_present, shell_adiabatic, shell_check_distance, damping_present)
Get attributes of an atomic kind set.
subroutine, public get_gto_basis_set(gto_basis_set, name, aliases, norm_type, kind_radius, ncgf, nset, nsgf, cgf_symbol, sgf_symbol, norm_cgf, set_radius, lmax, lmin, lx, ly, lz, m, ncgf_set, npgf, nsgf_set, nshell, cphi, pgf_radius, sphi, scon, zet, first_cgf, first_sgf, l, last_cgf, last_sgf, n, gcc, maxco, maxl, maxpgf, maxsgf_set, maxshell, maxso, nco_sum, npgf_sum, nshell_sum, maxder, short_kind_radius, npgf_seg_sum, ccon)
...
collect pointers to a block of reals
Handles all functions related to the CELL.
Definition cell_types.F:15
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_get_block_p(matrix, row, col, block, found, row_size, col_size)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
subroutine, public dbcsr_finalize(matrix)
...
DBCSR operations in CP2K.
Fortran API for the grid package, which is written in C.
Definition grid_api.F:12
subroutine, public integrate_pgf_product(la_max, zeta, la_min, lb_max, zetb, lb_min, ra, rab, rsgrid, hab, pab, o1, o2, radius, calculate_forces, force_a, force_b, compute_tau, use_virial, my_virial_a, my_virial_b, hdab, hadb, a_hdab, use_subpatch, subpatch_pattern)
low level function to compute matrix elements of primitive gaussian functions
Definition grid_api.F:274
subroutine, public grid_integrate_task_list(task_list, compute_tau, calculate_forces, calculate_virial, pab_blocks, rs_grids, hab_blocks, forces, virial)
Integrate all tasks of in given list from given grids.
Definition grid_api.F:1031
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Interface to the message passing library MPI.
Provides Cartesian and spherical orbital pointers and indices.
integer, dimension(:), allocatable, public ncoset
Define the data structure for the particle information.
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Build up the plane wave density by collocating the primitive Gaussian functions (pgf).
subroutine, public integrate_v_rspace(v_rspace, hmat, hmat_kp, pmat, pmat_kp, qs_env, calculate_forces, force_adm, compute_tau, gapw, basis_type, pw_env_external, task_list_external)
computes matrix elements corresponding to a given potential If compute_tau hmat(\mu,...
subroutine, public integrate_v_dbasis(v_rspace, matrix_vhxc_dbasis, matrix_p, qs_env, lambda)
Integrate a potential v_rspace over the derivatives of the basis functions < da/dR | V | b > + < a | ...
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
subroutine, public get_qs_kind_set(qs_kind_set, all_potential_present, tnadd_potential_present, gth_potential_present, sgp_potential_present, paw_atom_present, dft_plus_u_atom_present, maxcgf, maxsgf, maxco, maxco_proj, maxgtops, maxlgto, maxlprj, maxnset, maxsgf_set, ncgf, npgf, nset, nsgf, nshell, maxpol, maxlppl, maxlppnl, maxppnl, nelectron, maxder, max_ngrid_rad, max_sph_harm, maxg_iso_not0, lmax_rho0, basis_rcut, do_mtlr_present, basis_type, total_zeff_corr, npgf_seg, cneo_potential_present, nkind_q, natom_q)
Get attributes of an atomic kind set.
Transfers densities from PW to RS grids and potentials from PW to RS.
subroutine, public potential_pw2rs(rs_v, v_rspace, pw_env)
transfers a potential from a pw_grid to a vector of realspace multigrids
generate the tasks lists used by collocate and integrate routines
subroutine, public rs_scatter_matrices(src_matrices, dest_buffer, task_list, group)
Scatters dbcsr matrix blocks and receives them into a buffer as needed before collocation.
subroutine, public rs_distribute_matrix(rs_descs, pmats, atom_pair_send, atom_pair_recv, nimages, scatter, hmats)
redistributes the matrix so that it can be used in realspace operations i.e. according to the task li...
subroutine, public rs_gather_matrices(src_buffer, dest_matrices, task_list, group)
Gather the dbcsr matrix blocks and receives them into a buffer as needed after integration.
types for task lists
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
contained for different pw related things
Provides all information about a quickstep kind.