Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 : ! **************************************************************************************************
8 : !> \brief Calculation of the non-local pseudopotential contribution to the core Hamiltonian
9 : !> <a|V(non-local)|b> = <a|p(l,i)>*h(i,j)*<p(l,j)|b>
10 : !> \par History
11 : !> - refactered from qs_core_hamiltian [Joost VandeVondele, 2008-11-01]
12 : !> - full rewrite [jhu, 2009-01-23]
13 : !> - Extended by the derivatives for DFPT [Sandra Luber, Edward Ditler, 2021]
14 : ! **************************************************************************************************
15 : MODULE core_ppnl
16 : USE ai_angmom, ONLY: angmom
17 : USE ai_overlap, ONLY: overlap
18 : USE atomic_kind_types, ONLY: atomic_kind_type,&
19 : get_atomic_kind_set
20 : USE basis_set_types, ONLY: gto_basis_set_p_type,&
21 : gto_basis_set_type
22 : USE dbcsr_api, ONLY: dbcsr_add,&
23 : dbcsr_get_block_p,&
24 : dbcsr_p_type
25 : USE external_potential_types, ONLY: gth_potential_p_type,&
26 : gth_potential_type,&
27 : sgp_potential_p_type,&
28 : sgp_potential_type
29 : USE kinds, ONLY: dp,&
30 : int_8
31 : USE orbital_pointers, ONLY: init_orbital_pointers,&
32 : nco,&
33 : ncoset
34 : USE particle_types, ONLY: particle_type
35 : USE qs_force_types, ONLY: qs_force_type
36 : USE qs_kind_types, ONLY: get_qs_kind,&
37 : get_qs_kind_set,&
38 : qs_kind_type
39 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
40 : USE sap_kind_types, ONLY: alist_type,&
41 : clist_type,&
42 : get_alist,&
43 : release_sap_int,&
44 : sap_int_type,&
45 : sap_sort
46 : USE virial_methods, ONLY: virial_pair_force
47 : USE virial_types, ONLY: virial_type
48 :
49 : !$ USE OMP_LIB, ONLY: omp_lock_kind, &
50 : !$ omp_init_lock, omp_set_lock, &
51 : !$ omp_unset_lock, omp_destroy_lock
52 :
53 : #include "./base/base_uses.f90"
54 :
55 : IMPLICIT NONE
56 :
57 : PRIVATE
58 :
59 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'core_ppnl'
60 :
61 : PUBLIC :: build_core_ppnl
62 :
63 : CONTAINS
64 :
65 : ! **************************************************************************************************
66 : !> \brief ...
67 : !> \param matrix_h ...
68 : !> \param matrix_p ...
69 : !> \param force ...
70 : !> \param virial ...
71 : !> \param calculate_forces ...
72 : !> \param use_virial ...
73 : !> \param nder ...
74 : !> \param qs_kind_set ...
75 : !> \param atomic_kind_set ...
76 : !> \param particle_set ...
77 : !> \param sab_orb ...
78 : !> \param sap_ppnl ...
79 : !> \param eps_ppnl ...
80 : !> \param nimages ...
81 : !> \param cell_to_index ...
82 : !> \param basis_type ...
83 : !> \param deltaR Weighting factors of the derivatives wrt. nuclear positions
84 : !> \param matrix_l ...
85 : ! **************************************************************************************************
86 14186 : SUBROUTINE build_core_ppnl(matrix_h, matrix_p, force, virial, calculate_forces, use_virial, nder, &
87 : qs_kind_set, atomic_kind_set, particle_set, sab_orb, sap_ppnl, eps_ppnl, &
88 14186 : nimages, cell_to_index, basis_type, deltaR, matrix_l)
89 :
90 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_h, matrix_p
91 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
92 : TYPE(virial_type), POINTER :: virial
93 : LOGICAL, INTENT(IN) :: calculate_forces
94 : LOGICAL :: use_virial
95 : INTEGER :: nder
96 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
97 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
98 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
99 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
100 : POINTER :: sab_orb, sap_ppnl
101 : REAL(KIND=dp), INTENT(IN) :: eps_ppnl
102 : INTEGER, INTENT(IN) :: nimages
103 : INTEGER, DIMENSION(:, :, :), OPTIONAL, POINTER :: cell_to_index
104 : CHARACTER(LEN=*), INTENT(IN) :: basis_type
105 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
106 : OPTIONAL :: deltaR
107 : TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
108 : POINTER :: matrix_l
109 :
110 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_core_ppnl'
111 :
112 : INTEGER :: atom_a, first_col, handle, i, i_dim, iab, iac, iatom, ib, ibc, icol, ikind, &
113 : ilist, img, irow, iset, j, jatom, jb, jkind, jneighbor, kac, katom, kbc, kkind, l, &
114 : lc_max, lc_min, ldai, ldsab, lppnl, maxco, maxder, maxl, maxlgto, maxlppnl, maxppnl, &
115 : maxsgf, na, natom, nb, ncoa, ncoc, nkind, nlist, nneighbor, nnl, np, nppnl, nprjc, nseta, &
116 : nsgfa, prjc, sgfa, slot
117 14186 : INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind, kind_of
118 : INTEGER, DIMENSION(3) :: cell_b, cell_c
119 14186 : INTEGER, DIMENSION(:), POINTER :: la_max, la_min, npgfa, nprj_ppnl, &
120 14186 : nsgf_seta
121 14186 : INTEGER, DIMENSION(:, :), POINTER :: first_sgfa
122 : LOGICAL :: do_dR, do_gth, do_kp, do_soc, found, &
123 : ppnl_present
124 : REAL(KIND=dp) :: dac, f0, ppnl_radius
125 14186 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: radp
126 14186 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: sab, work
127 14186 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: ai_work, lab, work_l
128 : REAL(KIND=dp), DIMENSION(1) :: rprjc, zetc
129 : REAL(KIND=dp), DIMENSION(3) :: fa, fb, rab, rac, rbc
130 : REAL(KIND=dp), DIMENSION(3, 3) :: pv_thread
131 : TYPE(gto_basis_set_type), POINTER :: orb_basis_set
132 14186 : TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set
133 : TYPE(gth_potential_type), POINTER :: gth_potential
134 14186 : TYPE(gth_potential_p_type), DIMENSION(:), POINTER :: gpotential
135 : TYPE(clist_type), POINTER :: clist
136 : TYPE(alist_type), POINTER :: alist_ac, alist_bc
137 14186 : REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: achint, acint, alkint, bchint, bcint, &
138 14186 : blkint
139 14186 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: cprj, h_block, l_block_x, l_block_y, &
140 14186 : l_block_z, p_block, r_2block, &
141 14186 : r_3block, rpgfa, sphi_a, vprj_ppnl, &
142 14186 : wprj_ppnl, zeta
143 14186 : REAL(KIND=dp), DIMENSION(:), POINTER :: a_nl, alpha_ppnl, hprj, set_radius_a
144 28372 : REAL(KIND=dp), DIMENSION(3, SIZE(particle_set)) :: force_thread
145 14186 : TYPE(sap_int_type), DIMENSION(:), POINTER :: sap_int
146 14186 : TYPE(sgp_potential_p_type), DIMENSION(:), POINTER :: spotential
147 : TYPE(sgp_potential_type), POINTER :: sgp_potential
148 :
149 : !$ INTEGER(kind=omp_lock_kind), &
150 14186 : !$ ALLOCATABLE, DIMENSION(:) :: locks
151 : !$ INTEGER(KIND=int_8) :: iatom8
152 : !$ INTEGER :: lock_num, hash
153 : !$ INTEGER, PARAMETER :: nlock = 501
154 :
155 : MARK_USED(int_8)
156 :
157 14186 : do_dR = .FALSE.
158 36 : IF (PRESENT(deltaR)) do_dR = .TRUE.
159 :
160 14186 : IF (calculate_forces) THEN
161 6125 : CALL timeset(routineN//"_forces", handle)
162 : ELSE
163 8061 : CALL timeset(routineN, handle)
164 : END IF
165 :
166 14186 : do_soc = PRESENT(matrix_l)
167 :
168 14186 : ppnl_present = ASSOCIATED(sap_ppnl)
169 :
170 14186 : IF (ppnl_present) THEN
171 :
172 14186 : nkind = SIZE(atomic_kind_set)
173 14186 : natom = SIZE(particle_set)
174 :
175 14186 : do_kp = (nimages > 1)
176 :
177 14186 : IF (do_kp) THEN
178 202 : CPASSERT(PRESENT(cell_to_index) .AND. ASSOCIATED(cell_to_index))
179 : END IF
180 :
181 14186 : IF (calculate_forces) THEN
182 6125 : IF (SIZE(matrix_p, 1) == 2) THEN
183 1870 : DO img = 1, nimages
184 : CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
185 1218 : alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
186 : CALL dbcsr_add(matrix_p(2, img)%matrix, matrix_p(1, img)%matrix, &
187 1870 : alpha_scalar=-2.0_dp, beta_scalar=1.0_dp)
188 : END DO
189 : END IF
190 : END IF
191 :
192 14186 : maxder = ncoset(nder)
193 :
194 : CALL get_qs_kind_set(qs_kind_set, &
195 : maxco=maxco, &
196 : maxlgto=maxlgto, &
197 : maxsgf=maxsgf, &
198 : maxlppnl=maxlppnl, &
199 : maxppnl=maxppnl, &
200 14186 : basis_type=basis_type)
201 :
202 14186 : maxl = MAX(maxlgto, maxlppnl)
203 14186 : CALL init_orbital_pointers(maxl + nder + 1)
204 :
205 14186 : ldsab = MAX(maxco, ncoset(maxlppnl), maxsgf, maxppnl)
206 14186 : ldai = ncoset(maxl + nder + 1)
207 :
208 : ! sap_int needs to be shared as multiple threads need to access this
209 97612 : ALLOCATE (sap_int(nkind*nkind))
210 69240 : DO i = 1, nkind*nkind
211 55054 : NULLIFY (sap_int(i)%alist, sap_int(i)%asort, sap_int(i)%aindex)
212 69240 : sap_int(i)%nalist = 0
213 : END DO
214 :
215 : ! Set up direct access to basis and potential
216 179636 : ALLOCATE (basis_set(nkind), gpotential(nkind), spotential(nkind))
217 40964 : DO ikind = 1, nkind
218 26778 : CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set, basis_type=basis_type)
219 26778 : IF (ASSOCIATED(orb_basis_set)) THEN
220 26778 : basis_set(ikind)%gto_basis_set => orb_basis_set
221 : ELSE
222 0 : NULLIFY (basis_set(ikind)%gto_basis_set)
223 : END IF
224 26778 : CALL get_qs_kind(qs_kind_set(ikind), gth_potential=gth_potential, sgp_potential=sgp_potential)
225 26778 : NULLIFY (gpotential(ikind)%gth_potential)
226 26778 : NULLIFY (spotential(ikind)%sgp_potential)
227 40964 : IF (ASSOCIATED(gth_potential)) THEN
228 26546 : gpotential(ikind)%gth_potential => gth_potential
229 26546 : IF (do_soc .AND. (.NOT. gth_potential%soc)) THEN
230 0 : CPWARN("Spin-orbit coupling selected, but GTH potential without SOC parameters provided")
231 : END IF
232 232 : ELSE IF (ASSOCIATED(sgp_potential)) THEN
233 10 : spotential(ikind)%sgp_potential => sgp_potential
234 : END IF
235 : END DO
236 :
237 : ! Allocate sap int
238 760284 : DO slot = 1, sap_ppnl(1)%nl_size
239 :
240 746098 : ikind = sap_ppnl(1)%nlist_task(slot)%ikind
241 746098 : kkind = sap_ppnl(1)%nlist_task(slot)%jkind
242 746098 : iatom = sap_ppnl(1)%nlist_task(slot)%iatom
243 746098 : katom = sap_ppnl(1)%nlist_task(slot)%jatom
244 746098 : nlist = sap_ppnl(1)%nlist_task(slot)%nlist
245 746098 : ilist = sap_ppnl(1)%nlist_task(slot)%ilist
246 746098 : nneighbor = sap_ppnl(1)%nlist_task(slot)%nnode
247 :
248 746098 : iac = ikind + nkind*(kkind - 1)
249 746098 : IF (.NOT. ASSOCIATED(basis_set(ikind)%gto_basis_set)) CYCLE
250 746098 : IF (.NOT. ASSOCIATED(gpotential(kkind)%gth_potential) .AND. &
251 : .NOT. ASSOCIATED(spotential(kkind)%sgp_potential)) CYCLE
252 746098 : IF (.NOT. ASSOCIATED(sap_int(iac)%alist)) THEN
253 30124 : sap_int(iac)%a_kind = ikind
254 30124 : sap_int(iac)%p_kind = kkind
255 30124 : sap_int(iac)%nalist = nlist
256 150610 : ALLOCATE (sap_int(iac)%alist(nlist))
257 90362 : DO i = 1, nlist
258 60238 : NULLIFY (sap_int(iac)%alist(i)%clist)
259 60238 : sap_int(iac)%alist(i)%aatom = 0
260 90362 : sap_int(iac)%alist(i)%nclist = 0
261 : END DO
262 : END IF
263 760284 : IF (.NOT. ASSOCIATED(sap_int(iac)%alist(ilist)%clist)) THEN
264 60184 : sap_int(iac)%alist(ilist)%aatom = iatom
265 60184 : sap_int(iac)%alist(ilist)%nclist = nneighbor
266 1287754 : ALLOCATE (sap_int(iac)%alist(ilist)%clist(nneighbor))
267 806282 : DO i = 1, nneighbor
268 806282 : sap_int(iac)%alist(ilist)%clist(i)%catom = 0
269 : END DO
270 : END IF
271 : END DO
272 :
273 : ! Calculate the overlap integrals <a|p>
274 : !$OMP PARALLEL &
275 : !$OMP DEFAULT (NONE) &
276 : !$OMP SHARED (basis_set, gpotential, spotential, maxder, ncoset, &
277 : !$OMP sap_ppnl, sap_int, nkind, ldsab, ldai, nder, nco, do_soc ) &
278 : !$OMP PRIVATE (ikind, kkind, iatom, katom, nlist, ilist, nneighbor, jneighbor, &
279 : !$OMP cell_c, rac, iac, first_sgfa, la_max, la_min, npgfa, nseta, nsgfa, nsgf_seta, &
280 : !$OMP slot, sphi_a, zeta, cprj, hprj, lppnl, nppnl, nprj_ppnl, &
281 : !$OMP clist, iset, ncoa, sgfa, prjc, work, work_l, sab, lab, ai_work, nprjc, &
282 : !$OMP ppnl_radius, ncoc, rpgfa, first_col, vprj_ppnl, wprj_ppnl, i, j, l, do_gth, &
283 : !$OMP set_radius_a, rprjc, dac, lc_max, lc_min, zetc, alpha_ppnl, &
284 14186 : !$OMP na, nb, np, nnl, a_nl, radp, i_dim, ib, jb)
285 :
286 : ALLOCATE (sab(ldsab, ldsab*maxder), work(ldsab, ldsab*maxder))
287 : sab = 0.0_dp
288 : ALLOCATE (ai_work(ldai, ldai, ncoset(nder + 1)))
289 : ai_work = 0.0_dp
290 : IF (do_soc) THEN
291 : ALLOCATE (lab(ldsab, ldsab, 3), work_l(ldsab, ldsab, 3))
292 : lab = 0.0_dp
293 : END IF
294 :
295 : !$OMP DO SCHEDULE(GUIDED)
296 : DO slot = 1, sap_ppnl(1)%nl_size
297 :
298 : ikind = sap_ppnl(1)%nlist_task(slot)%ikind
299 : kkind = sap_ppnl(1)%nlist_task(slot)%jkind
300 : iatom = sap_ppnl(1)%nlist_task(slot)%iatom
301 : katom = sap_ppnl(1)%nlist_task(slot)%jatom
302 : nlist = sap_ppnl(1)%nlist_task(slot)%nlist
303 : ilist = sap_ppnl(1)%nlist_task(slot)%ilist
304 : nneighbor = sap_ppnl(1)%nlist_task(slot)%nnode
305 : jneighbor = sap_ppnl(1)%nlist_task(slot)%inode
306 : cell_c(:) = sap_ppnl(1)%nlist_task(slot)%cell(:)
307 : rac(1:3) = sap_ppnl(1)%nlist_task(slot)%r(1:3)
308 :
309 : iac = ikind + nkind*(kkind - 1)
310 : IF (.NOT. ASSOCIATED(basis_set(ikind)%gto_basis_set)) CYCLE
311 : ! Get definition of basis set
312 : first_sgfa => basis_set(ikind)%gto_basis_set%first_sgf
313 : la_max => basis_set(ikind)%gto_basis_set%lmax
314 : la_min => basis_set(ikind)%gto_basis_set%lmin
315 : npgfa => basis_set(ikind)%gto_basis_set%npgf
316 : nseta = basis_set(ikind)%gto_basis_set%nset
317 : nsgfa = basis_set(ikind)%gto_basis_set%nsgf
318 : nsgf_seta => basis_set(ikind)%gto_basis_set%nsgf_set
319 : rpgfa => basis_set(ikind)%gto_basis_set%pgf_radius
320 : set_radius_a => basis_set(ikind)%gto_basis_set%set_radius
321 : sphi_a => basis_set(ikind)%gto_basis_set%sphi
322 : zeta => basis_set(ikind)%gto_basis_set%zet
323 : ! Get definition of PP projectors
324 : IF (ASSOCIATED(gpotential(kkind)%gth_potential)) THEN
325 : ! GTH potential
326 : do_gth = .TRUE.
327 : alpha_ppnl => gpotential(kkind)%gth_potential%alpha_ppnl
328 : cprj => gpotential(kkind)%gth_potential%cprj
329 : lppnl = gpotential(kkind)%gth_potential%lppnl
330 : nppnl = gpotential(kkind)%gth_potential%nppnl
331 : nprj_ppnl => gpotential(kkind)%gth_potential%nprj_ppnl
332 : ppnl_radius = gpotential(kkind)%gth_potential%ppnl_radius
333 : vprj_ppnl => gpotential(kkind)%gth_potential%vprj_ppnl
334 : wprj_ppnl => gpotential(kkind)%gth_potential%wprj_ppnl
335 : ELSE IF (ASSOCIATED(spotential(kkind)%sgp_potential)) THEN
336 : ! SGP potential
337 : do_gth = .FALSE.
338 : nprjc = spotential(kkind)%sgp_potential%nppnl
339 : IF (nprjc == 0) CYCLE
340 : nnl = spotential(kkind)%sgp_potential%n_nonlocal
341 : lppnl = spotential(kkind)%sgp_potential%lmax
342 : a_nl => spotential(kkind)%sgp_potential%a_nonlocal
343 : ppnl_radius = spotential(kkind)%sgp_potential%ppnl_radius
344 : ALLOCATE (radp(nnl))
345 : radp(:) = ppnl_radius
346 : cprj => spotential(kkind)%sgp_potential%cprj_ppnl
347 : hprj => spotential(kkind)%sgp_potential%vprj_ppnl
348 : nppnl = SIZE(cprj, 2)
349 : ELSE
350 : CYCLE
351 : END IF
352 :
353 : dac = SQRT(SUM(rac*rac))
354 : clist => sap_int(iac)%alist(ilist)%clist(jneighbor)
355 : clist%catom = katom
356 : clist%cell = cell_c
357 : clist%rac = rac
358 : ALLOCATE (clist%acint(nsgfa, nppnl, maxder), &
359 : clist%achint(nsgfa, nppnl, maxder), &
360 : clist%alint(nsgfa, nppnl, 3), &
361 : clist%alkint(nsgfa, nppnl, 3))
362 : clist%acint = 0.0_dp
363 : clist%achint = 0.0_dp
364 : clist%alint = 0.0_dp
365 : clist%alkint = 0.0_dp
366 :
367 : clist%nsgf_cnt = 0
368 : NULLIFY (clist%sgf_list)
369 : DO iset = 1, nseta
370 : ncoa = npgfa(iset)*ncoset(la_max(iset))
371 : sgfa = first_sgfa(1, iset)
372 : IF (do_gth) THEN
373 : ! GTH potential
374 : prjc = 1
375 : work = 0.0_dp
376 : DO l = 0, lppnl
377 : nprjc = nprj_ppnl(l)*nco(l)
378 : IF (nprjc == 0) CYCLE
379 : rprjc(1) = ppnl_radius
380 : IF (set_radius_a(iset) + rprjc(1) < dac) CYCLE
381 : lc_max = l + 2*(nprj_ppnl(l) - 1)
382 : lc_min = l
383 : zetc(1) = alpha_ppnl(l)
384 : ncoc = ncoset(lc_max)
385 :
386 : ! Calculate the primitive overlap integrals
387 : CALL overlap(la_max(iset), la_min(iset), npgfa(iset), rpgfa(:, iset), zeta(:, iset), &
388 : lc_max, lc_min, 1, rprjc, zetc, rac, dac, sab, nder, .TRUE., ai_work, ldai)
389 : ! Transformation step projector functions (Cartesian -> spherical)
390 : na = ncoa
391 : nb = nprjc
392 : np = ncoc
393 : DO i = 1, maxder
394 : first_col = (i - 1)*ldsab
395 : ! CALL dgemm("N", "N", ncoa, nprjc, ncoc, 1.0_dp, sab(1, first_col + 1), SIZE(sab, 1), &
396 : ! cprj(1, prjc), SIZE(cprj, 1), 0.0_dp, work(1, first_col + prjc), ldsab)
397 : work(1:na, first_col + prjc:first_col + prjc + nb - 1) = &
398 : MATMUL(sab(1:na, first_col + 1:first_col + np), cprj(1:np, prjc:prjc + nb - 1))
399 : END DO
400 :
401 : IF (do_soc) THEN
402 : ! Calculate the primitive angular momentum integrals needed for spin-orbit coupling
403 : lab = 0.0_dp
404 : CALL angmom(la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
405 : lc_max, 1, zetc, rprjc, -rac, (/0._dp, 0._dp, 0._dp/), lab)
406 : DO i_dim = 1, 3
407 : work_l(1:na, prjc:prjc + nb - 1, i_dim) = &
408 : MATMUL(lab(1:na, 1:np, i_dim), cprj(1:np, prjc:prjc + nb - 1))
409 : END DO
410 : END IF
411 :
412 : prjc = prjc + nprjc
413 :
414 : END DO
415 : na = nsgf_seta(iset)
416 : nb = nppnl
417 : np = ncoa
418 : DO i = 1, maxder
419 : first_col = (i - 1)*ldsab + 1
420 : ! Contraction step (basis functions)
421 : ! CALL dgemm("T", "N", nsgf_seta(iset), nppnl, ncoa, 1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
422 : ! work(1, first_col), ldsab, 0.0_dp, clist%acint(sgfa, 1, i), nsgfa)
423 : clist%acint(sgfa:sgfa + na - 1, 1:nb, i) = &
424 : MATMUL(TRANSPOSE(sphi_a(1:np, sgfa:sgfa + na - 1)), work(1:np, first_col:first_col + nb - 1))
425 : ! Multiply with interaction matrix(h)
426 : ! CALL dgemm("N", "N", nsgf_seta(iset), nppnl, nppnl, 1.0_dp, clist%acint(sgfa, 1, i), nsgfa, &
427 : ! vprj_ppnl(1, 1), SIZE(vprj_ppnl, 1), 0.0_dp, clist%achint(sgfa, 1, i), nsgfa)
428 : clist%achint(sgfa:sgfa + na - 1, 1:nb, i) = &
429 : MATMUL(clist%acint(sgfa:sgfa + na - 1, 1:nb, i), vprj_ppnl(1:nb, 1:nb))
430 : END DO
431 : IF (do_soc) THEN
432 : DO i_dim = 1, 3
433 : clist%alint(sgfa:sgfa + na - 1, 1:nb, i_dim) = &
434 : MATMUL(TRANSPOSE(sphi_a(1:np, sgfa:sgfa + na - 1)), work_l(1:np, 1:nb, i_dim))
435 : clist%alkint(sgfa:sgfa + na - 1, 1:nb, i_dim) = &
436 : MATMUL(clist%alint(sgfa:sgfa + na - 1, 1:nb, i_dim), wprj_ppnl(1:nb, 1:nb))
437 : END DO
438 : END IF
439 : ELSE
440 : ! SGP potential
441 : ! Calculate the primitive overlap integrals
442 : CALL overlap(la_max(iset), la_min(iset), npgfa(iset), rpgfa(:, iset), zeta(:, iset), &
443 : lppnl, 0, nnl, radp, a_nl, rac, dac, sab, nder, .TRUE., ai_work, ldai)
444 : na = nsgf_seta(iset)
445 : nb = nppnl
446 : np = ncoa
447 : DO i = 1, maxder
448 : first_col = (i - 1)*ldsab + 1
449 : ! Transformation step projector functions (cartesian->spherical)
450 : ! CALL dgemm("N", "N", ncoa, nppnl, nprjc, 1.0_dp, sab(1, first_col), ldsab, &
451 : ! cprj(1, 1), SIZE(cprj, 1), 0.0_dp, work(1, 1), ldsab)
452 : work(1:np, 1:nb) = MATMUL(sab(1:np, first_col:first_col + nprjc - 1), cprj(1:nprjc, 1:nb))
453 : ! Contraction step (basis functions)
454 : ! CALL dgemm("T", "N", nsgf_seta(iset), nppnl, ncoa, 1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
455 : ! work(1, 1), ldsab, 0.0_dp, clist%acint(sgfa, 1, i), nsgfa)
456 : clist%acint(sgfa:sgfa + na - 1, 1:nb, i) = &
457 : MATMUL(TRANSPOSE(sphi_a(1:np, sgfa:sgfa + na - 1)), work(1:np, 1:nb))
458 : ! *** Multiply with interaction matrix(h) ***
459 : ncoc = sgfa + nsgf_seta(iset) - 1
460 : DO j = 1, nppnl
461 : clist%achint(sgfa:ncoc, j, i) = clist%acint(sgfa:ncoc, j, i)*hprj(j)
462 : END DO
463 : END DO
464 : END IF
465 : END DO
466 : clist%maxac = MAXVAL(ABS(clist%acint(:, :, 1)))
467 : clist%maxach = MAXVAL(ABS(clist%achint(:, :, 1)))
468 : IF (.NOT. do_gth) DEALLOCATE (radp)
469 : END DO
470 :
471 : DEALLOCATE (sab, ai_work, work)
472 : IF (do_soc) DEALLOCATE (lab, work_l)
473 : !$OMP END PARALLEL
474 :
475 : ! Set up a sorting index
476 14186 : CALL sap_sort(sap_int)
477 : ! All integrals needed have been calculated and stored in sap_int
478 : ! We now calculate the Hamiltonian matrix elements
479 :
480 227338 : force_thread = 0.0_dp
481 14186 : pv_thread = 0.0_dp
482 :
483 : !$OMP PARALLEL &
484 : !$OMP DEFAULT (NONE) &
485 : !$OMP SHARED (do_kp, basis_set, matrix_h, matrix_l, cell_to_index,&
486 : !$OMP sab_orb, matrix_p, sap_int, nkind, eps_ppnl, force, &
487 : !$OMP do_dR, deltaR, maxder, nder, &
488 : !$OMP locks, virial, use_virial, calculate_forces, do_soc, natom) &
489 : !$OMP PRIVATE (ikind, jkind, iatom, jatom, cell_b, rab, &
490 : !$OMP slot, iab, atom_a, f0, irow, icol, h_block, &
491 : !$OMP l_block_x, l_block_y, l_block_z, &
492 : !$OMP r_2block, r_3block, &
493 : !$OMP found,p_block, iac, ibc, alist_ac, alist_bc, acint, bcint, &
494 : !$OMP achint, bchint, alkint, blkint, &
495 : !$OMP na, np, nb, katom, j, fa, fb, rbc, rac, &
496 : !$OMP kkind, kac, kbc, i, img, hash, iatom8) &
497 14186 : !$OMP REDUCTION (+ : pv_thread, force_thread )
498 :
499 : !$OMP SINGLE
500 : !$ ALLOCATE (locks(nlock))
501 : !$OMP END SINGLE
502 :
503 : !$OMP DO
504 : !$ DO lock_num = 1, nlock
505 : !$ call omp_init_lock(locks(lock_num))
506 : !$ END DO
507 : !$OMP END DO
508 :
509 : !$OMP DO SCHEDULE(GUIDED)
510 : DO slot = 1, sab_orb(1)%nl_size
511 :
512 : ikind = sab_orb(1)%nlist_task(slot)%ikind
513 : jkind = sab_orb(1)%nlist_task(slot)%jkind
514 : iatom = sab_orb(1)%nlist_task(slot)%iatom
515 : jatom = sab_orb(1)%nlist_task(slot)%jatom
516 : cell_b(:) = sab_orb(1)%nlist_task(slot)%cell(:)
517 : rab(1:3) = sab_orb(1)%nlist_task(slot)%r(1:3)
518 :
519 : IF (.NOT. ASSOCIATED(basis_set(ikind)%gto_basis_set)) CYCLE
520 : IF (.NOT. ASSOCIATED(basis_set(jkind)%gto_basis_set)) CYCLE
521 :
522 : iab = ikind + nkind*(jkind - 1)
523 :
524 : ! Use the symmetry of the first derivatives
525 : IF (iatom == jatom) THEN
526 : f0 = 1.0_dp
527 : ELSE
528 : f0 = 2.0_dp
529 : END IF
530 :
531 : IF (do_kp) THEN
532 : img = cell_to_index(cell_b(1), cell_b(2), cell_b(3))
533 : ELSE
534 : img = 1
535 : END IF
536 :
537 : ! Create matrix blocks for a new matrix block column
538 : IF (iatom <= jatom) THEN
539 : irow = iatom
540 : icol = jatom
541 : ELSE
542 : irow = jatom
543 : icol = iatom
544 : END IF
545 : NULLIFY (h_block)
546 : CALL dbcsr_get_block_p(matrix_h(1, img)%matrix, irow, icol, h_block, found)
547 : IF (do_soc) THEN
548 : NULLIFY (l_block_x, l_block_y, l_block_z)
549 : CALL dbcsr_get_block_p(matrix_l(1, img)%matrix, irow, icol, l_block_x, found)
550 : CALL dbcsr_get_block_p(matrix_l(2, img)%matrix, irow, icol, l_block_y, found)
551 : CALL dbcsr_get_block_p(matrix_l(3, img)%matrix, irow, icol, l_block_z, found)
552 : END IF
553 :
554 : IF (do_dR) THEN
555 : NULLIFY (r_2block, r_3block)
556 : CALL dbcsr_get_block_p(matrix_h(2, img)%matrix, irow, icol, r_2block, found)
557 : CALL dbcsr_get_block_p(matrix_h(3, img)%matrix, irow, icol, r_3block, found)
558 : END IF
559 :
560 : IF (calculate_forces) THEN
561 : NULLIFY (p_block)
562 : CALL dbcsr_get_block_p(matrix_p(1, img)%matrix, irow, icol, p_block, found)
563 : END IF
564 :
565 : ! loop over all kinds for projector atom
566 : IF (ASSOCIATED(h_block)) THEN
567 : !$ iatom8 = INT(iatom - 1, int_8)*INT(natom, int_8) + INT(jatom, int_8)
568 : !$ hash = INT(MOD(iatom8, INT(nlock, int_8)) + 1)
569 :
570 : DO kkind = 1, nkind
571 : iac = ikind + nkind*(kkind - 1)
572 : ibc = jkind + nkind*(kkind - 1)
573 : IF (.NOT. ASSOCIATED(sap_int(iac)%alist)) CYCLE
574 : IF (.NOT. ASSOCIATED(sap_int(ibc)%alist)) CYCLE
575 : CALL get_alist(sap_int(iac), alist_ac, iatom)
576 : CALL get_alist(sap_int(ibc), alist_bc, jatom)
577 : IF (.NOT. ASSOCIATED(alist_ac)) CYCLE
578 : IF (.NOT. ASSOCIATED(alist_bc)) CYCLE
579 : DO kac = 1, alist_ac%nclist
580 : DO kbc = 1, alist_bc%nclist
581 : IF (alist_ac%clist(kac)%catom /= alist_bc%clist(kbc)%catom) CYCLE
582 : IF (ALL(cell_b + alist_bc%clist(kbc)%cell - alist_ac%clist(kac)%cell == 0)) THEN
583 : IF (alist_ac%clist(kac)%maxac*alist_bc%clist(kbc)%maxach < eps_ppnl) CYCLE
584 : acint => alist_ac%clist(kac)%acint
585 : bcint => alist_bc%clist(kbc)%acint
586 : achint => alist_ac%clist(kac)%achint
587 : bchint => alist_bc%clist(kbc)%achint
588 : IF (do_soc) THEN
589 : alkint => alist_ac%clist(kac)%alkint
590 : blkint => alist_bc%clist(kbc)%alkint
591 : END IF
592 : na = SIZE(acint, 1)
593 : np = SIZE(acint, 2)
594 : nb = SIZE(bcint, 1)
595 : !$ CALL omp_set_lock(locks(hash))
596 : IF (.NOT. do_dR) THEN
597 : IF (iatom <= jatom) THEN
598 : h_block(1:na, 1:nb) = h_block(1:na, 1:nb) + &
599 : MATMUL(achint(1:na, 1:np, 1), TRANSPOSE(bcint(1:nb, 1:np, 1)))
600 : ELSE
601 : h_block(1:nb, 1:na) = h_block(1:nb, 1:na) + &
602 : MATMUL(bchint(1:nb, 1:np, 1), TRANSPOSE(acint(1:na, 1:np, 1)))
603 : END IF
604 : END IF
605 : IF (do_soc) THEN
606 : IF (iatom <= jatom) THEN
607 : l_block_x(1:na, 1:nb) = l_block_x(1:na, 1:nb) + &
608 : MATMUL(alkint(1:na, 1:np, 1), TRANSPOSE(bcint(1:nb, 1:np, 1)))
609 : l_block_y(1:na, 1:nb) = l_block_y(1:na, 1:nb) + &
610 : MATMUL(alkint(1:na, 1:np, 2), TRANSPOSE(bcint(1:nb, 1:np, 1)))
611 : l_block_z(1:na, 1:nb) = l_block_z(1:na, 1:nb) + &
612 : MATMUL(alkint(1:na, 1:np, 3), TRANSPOSE(bcint(1:nb, 1:np, 1)))
613 :
614 : ELSE
615 : l_block_x(1:nb, 1:na) = l_block_x(1:nb, 1:na) - &
616 : MATMUL(blkint(1:nb, 1:np, 1), TRANSPOSE(acint(1:na, 1:np, 1)))
617 : l_block_y(1:nb, 1:na) = l_block_y(1:nb, 1:na) - &
618 : MATMUL(blkint(1:nb, 1:np, 2), TRANSPOSE(acint(1:na, 1:np, 1)))
619 : l_block_z(1:nb, 1:na) = l_block_z(1:nb, 1:na) - &
620 : MATMUL(blkint(1:nb, 1:np, 3), TRANSPOSE(acint(1:na, 1:np, 1)))
621 : END IF
622 : END IF
623 : !$ CALL omp_unset_lock(locks(hash))
624 : IF (calculate_forces) THEN
625 : IF (ASSOCIATED(p_block)) THEN
626 : katom = alist_ac%clist(kac)%catom
627 : DO i = 1, 3
628 : j = i + 1
629 : IF (iatom <= jatom) THEN
630 : fa(i) = SUM(p_block(1:na, 1:nb)* &
631 : MATMUL(acint(1:na, 1:np, j), TRANSPOSE(bchint(1:nb, 1:np, 1))))
632 : fb(i) = SUM(p_block(1:na, 1:nb)* &
633 : MATMUL(achint(1:na, 1:np, 1), TRANSPOSE(bcint(1:nb, 1:np, j))))
634 : ELSE
635 : fa(i) = SUM(p_block(1:nb, 1:na)* &
636 : MATMUL(bchint(1:nb, 1:np, 1), TRANSPOSE(acint(1:na, 1:np, j))))
637 : fb(i) = SUM(p_block(1:nb, 1:na)* &
638 : MATMUL(bcint(1:nb, 1:np, j), TRANSPOSE(achint(1:na, 1:np, 1))))
639 : END IF
640 : force_thread(i, iatom) = force_thread(i, iatom) + f0*fa(i)
641 : force_thread(i, katom) = force_thread(i, katom) - f0*fa(i)
642 : force_thread(i, jatom) = force_thread(i, jatom) + f0*fb(i)
643 : force_thread(i, katom) = force_thread(i, katom) - f0*fb(i)
644 : END DO
645 :
646 : IF (use_virial) THEN
647 : rac = alist_ac%clist(kac)%rac
648 : rbc = alist_bc%clist(kbc)%rac
649 : CALL virial_pair_force(pv_thread, f0, fa, rac)
650 : CALL virial_pair_force(pv_thread, f0, fb, rbc)
651 : END IF
652 : END IF
653 : END IF
654 :
655 : IF (do_dR) THEN
656 : i = 1; j = 2;
657 : katom = alist_ac%clist(kac)%catom
658 : IF (iatom <= jatom) THEN
659 : h_block(1:na, 1:nb) = h_block(1:na, 1:nb) + &
660 : (deltaR(i, iatom) - deltaR(i, katom))* &
661 : MATMUL(acint(1:na, 1:np, j), TRANSPOSE(bchint(1:nb, 1:np, 1)))
662 :
663 : h_block(1:na, 1:nb) = h_block(1:na, 1:nb) + &
664 : (deltaR(i, jatom) - deltaR(i, katom))* &
665 : MATMUL(achint(1:na, 1:np, 1), TRANSPOSE(bcint(1:nb, 1:np, j)))
666 : ELSE
667 : h_block(1:nb, 1:na) = h_block(1:nb, 1:na) + &
668 : (deltaR(i, iatom) - deltaR(i, katom))* &
669 : MATMUL(bchint(1:nb, 1:np, 1), TRANSPOSE(acint(1:na, 1:np, j)))
670 : h_block(1:nb, 1:na) = h_block(1:nb, 1:na) + &
671 : (deltaR(i, jatom) - deltaR(i, katom))* &
672 : MATMUL(bcint(1:nb, 1:np, j), TRANSPOSE(achint(1:na, 1:np, 1)))
673 : END IF
674 :
675 : i = 2; j = 3;
676 : katom = alist_ac%clist(kac)%catom
677 : IF (iatom <= jatom) THEN
678 : r_2block(1:na, 1:nb) = r_2block(1:na, 1:nb) + &
679 : (deltaR(i, iatom) - deltaR(i, katom))* &
680 : MATMUL(acint(1:na, 1:np, j), TRANSPOSE(bchint(1:nb, 1:np, 1)))
681 :
682 : r_2block(1:na, 1:nb) = r_2block(1:na, 1:nb) + &
683 : (deltaR(i, jatom) - deltaR(i, katom))* &
684 : MATMUL(achint(1:na, 1:np, 1), TRANSPOSE(bcint(1:nb, 1:np, j)))
685 : ELSE
686 : r_2block(1:nb, 1:na) = r_2block(1:nb, 1:na) + &
687 : (deltaR(i, iatom) - deltaR(i, katom))* &
688 : MATMUL(bchint(1:nb, 1:np, 1), TRANSPOSE(acint(1:na, 1:np, j)))
689 : r_2block(1:nb, 1:na) = r_2block(1:nb, 1:na) + &
690 : (deltaR(i, jatom) - deltaR(i, katom))* &
691 : MATMUL(bcint(1:nb, 1:np, j), TRANSPOSE(achint(1:na, 1:np, 1)))
692 : END IF
693 :
694 : i = 3; j = 4;
695 : katom = alist_ac%clist(kac)%catom
696 : IF (iatom <= jatom) THEN
697 : r_3block(1:na, 1:nb) = r_3block(1:na, 1:nb) + &
698 : (deltaR(i, iatom) - deltaR(i, katom))* &
699 : MATMUL(acint(1:na, 1:np, j), TRANSPOSE(bchint(1:nb, 1:np, 1)))
700 :
701 : r_3block(1:na, 1:nb) = r_3block(1:na, 1:nb) + &
702 : (deltaR(i, jatom) - deltaR(i, katom))* &
703 : MATMUL(achint(1:na, 1:np, 1), TRANSPOSE(bcint(1:nb, 1:np, j)))
704 : ELSE
705 : r_3block(1:nb, 1:na) = r_3block(1:nb, 1:na) + &
706 : (deltaR(i, iatom) - deltaR(i, katom))* &
707 : MATMUL(bchint(1:nb, 1:np, 1), TRANSPOSE(acint(1:na, 1:np, j)))
708 : r_3block(1:nb, 1:na) = r_3block(1:nb, 1:na) + &
709 : (deltaR(i, jatom) - deltaR(i, katom))* &
710 : MATMUL(bcint(1:nb, 1:np, j), TRANSPOSE(achint(1:na, 1:np, 1)))
711 : END IF
712 :
713 : END IF
714 : EXIT ! We have found a match and there can be only one single match
715 : END IF
716 : END DO
717 : END DO
718 : END DO
719 : END IF
720 : END DO
721 :
722 : !$OMP DO
723 : !$ DO lock_num = 1, nlock
724 : !$ call omp_destroy_lock(locks(lock_num))
725 : !$ END DO
726 : !$OMP END DO
727 :
728 : !$OMP SINGLE
729 : !$ DEALLOCATE (locks)
730 : !$OMP END SINGLE NOWAIT
731 :
732 : !$OMP END PARALLEL
733 :
734 14186 : CALL release_sap_int(sap_int)
735 :
736 14186 : DEALLOCATE (basis_set, gpotential, spotential)
737 14186 : IF (calculate_forces) THEN
738 6125 : CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
739 : !$OMP DO
740 : DO iatom = 1, natom
741 22025 : atom_a = atom_of_kind(iatom)
742 22025 : ikind = kind_of(iatom)
743 88100 : force(ikind)%gth_ppnl(:, atom_a) = force(ikind)%gth_ppnl(:, atom_a) + force_thread(:, iatom)
744 : END DO
745 : !$OMP END DO
746 6125 : DEALLOCATE (atom_of_kind, kind_of)
747 : END IF
748 :
749 14186 : IF (calculate_forces .AND. use_virial) THEN
750 10140 : virial%pv_ppnl = virial%pv_ppnl + pv_thread
751 10140 : virial%pv_virial = virial%pv_virial + pv_thread
752 : END IF
753 :
754 28372 : IF (calculate_forces) THEN
755 : ! If LSD, then recover alpha density and beta density
756 : ! from the total density (1) and the spin density (2)
757 6125 : IF (SIZE(matrix_p, 1) == 2) THEN
758 1870 : DO img = 1, nimages
759 : CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
760 1218 : alpha_scalar=0.5_dp, beta_scalar=0.5_dp)
761 : CALL dbcsr_add(matrix_p(2, img)%matrix, matrix_p(1, img)%matrix, &
762 1870 : alpha_scalar=-1.0_dp, beta_scalar=1.0_dp)
763 : END DO
764 : END IF
765 : END IF
766 :
767 : END IF !ppnl_present
768 :
769 14186 : CALL timestop(handle)
770 :
771 28372 : END SUBROUTINE build_core_ppnl
772 :
773 : END MODULE core_ppnl
|