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 : !> \brief Calculation of the local potential contribution of the nonadditive kinetic energy
9 : !> <a|V(local)|b> = <a|Sum e^a*rc**2|b>
10 : !> \par History
11 : !> - adapted from core_ppl
12 : ! **************************************************************************************************
13 : MODULE kg_tnadd_mat
14 : USE ai_overlap_ppl, ONLY: ppl_integral
15 : USE atomic_kind_types, ONLY: atomic_kind_type,&
16 : get_atomic_kind_set
17 : USE basis_set_types, ONLY: gto_basis_set_p_type,&
18 : gto_basis_set_type
19 : USE cp_dbcsr_api, ONLY: dbcsr_add,&
20 : dbcsr_create,&
21 : dbcsr_distribution_type,&
22 : dbcsr_finalize,&
23 : dbcsr_get_block_p,&
24 : dbcsr_p_type,&
25 : dbcsr_set,&
26 : dbcsr_type_symmetric
27 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
28 : USE cp_dbcsr_operations, ONLY: dbcsr_allocate_matrix_set,&
29 : dbcsr_deallocate_matrix_set
30 : USE external_potential_types, ONLY: get_potential,&
31 : local_potential_type
32 : USE kg_environment_types, ONLY: kg_environment_type
33 : USE kinds, ONLY: dp
34 : USE orbital_pointers, ONLY: init_orbital_pointers,&
35 : ncoset
36 : USE particle_methods, ONLY: get_particle_set
37 : USE particle_types, ONLY: particle_type
38 : USE qs_force_types, ONLY: qs_force_type
39 : USE qs_kind_types, ONLY: get_qs_kind,&
40 : get_qs_kind_set,&
41 : qs_kind_type
42 : USE qs_neighbor_list_types, ONLY: get_iterator_info,&
43 : neighbor_list_iterate,&
44 : neighbor_list_iterator_create,&
45 : neighbor_list_iterator_p_type,&
46 : neighbor_list_iterator_release,&
47 : neighbor_list_set_p_type,&
48 : nl_set_sub_iterator,&
49 : nl_sub_iterate
50 : USE virial_methods, ONLY: virial_pair_force
51 : USE virial_types, ONLY: virial_type
52 :
53 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
54 :
55 : #include "./base/base_uses.f90"
56 :
57 : IMPLICIT NONE
58 :
59 : PRIVATE
60 :
61 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'kg_tnadd_mat'
62 :
63 : PUBLIC :: build_tnadd_mat
64 :
65 : CONTAINS
66 :
67 : !==========================================================================================================
68 :
69 : ! **************************************************************************************************
70 : !> \brief ...
71 : !> \param kg_env ...
72 : !> \param matrix_p ...
73 : !> \param force ...
74 : !> \param virial ...
75 : !> \param calculate_forces ...
76 : !> \param use_virial ...
77 : !> \param qs_kind_set ...
78 : !> \param atomic_kind_set ...
79 : !> \param particle_set ...
80 : !> \param sab_orb ...
81 : !> \param dbcsr_dist ...
82 : ! **************************************************************************************************
83 50 : SUBROUTINE build_tnadd_mat(kg_env, matrix_p, force, virial, calculate_forces, use_virial, &
84 : qs_kind_set, atomic_kind_set, particle_set, sab_orb, dbcsr_dist)
85 :
86 : TYPE(kg_environment_type), POINTER :: kg_env
87 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_p
88 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
89 : TYPE(virial_type), POINTER :: virial
90 : LOGICAL, INTENT(IN) :: calculate_forces
91 : LOGICAL :: use_virial
92 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
93 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
94 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
95 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
96 : POINTER :: sab_orb
97 : TYPE(dbcsr_distribution_type), POINTER :: dbcsr_dist
98 :
99 : CHARACTER(LEN=*), PARAMETER :: routineN = 'build_tnadd_mat'
100 : INTEGER, PARAMETER :: nexp_max = 10
101 :
102 : INTEGER :: atom_a, atom_b, atom_c, handle, i, iatom, icol, ikind, img, imol, inode, irow, &
103 : iset, jatom, jkind, jmol, jset, katom, kkind, kmol, last_iatom, last_jatom, ldai, ldsab, &
104 : maxco, maxder, maxl, maxlgto, maxnset, maxpol, maxsgf, mepos, natom, ncoa, ncob, nder, &
105 : ngau, nkind, npol, nseta, nsetb, nthread, sgfa, sgfb
106 50 : INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind
107 50 : INTEGER, CONTIGUOUS, DIMENSION(:), POINTER :: col_blk_sizes, row_blk_sizes
108 50 : INTEGER, DIMENSION(:), POINTER :: atom_to_molecule, la_max, la_min, &
109 50 : lb_max, lb_min, npgfa, npgfb, nsgfa, &
110 50 : nsgfb
111 50 : INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, first_sgfb
112 : INTEGER, DIMENSION(nexp_max) :: nct
113 : LOGICAL :: found, new_atom_pair
114 : REAL(KIND=dp) :: dab, dac, dbc, f0, radius
115 50 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: work
116 50 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: ppl_fwork, ppl_work
117 50 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: hab, pab
118 : REAL(KIND=dp), DIMENSION(3) :: force_a, force_b, rab, rac, rbc
119 50 : REAL(KIND=dp), DIMENSION(:), POINTER :: alpha, set_radius_a, set_radius_b
120 50 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: ccval, cval, h_block, p_block, rpgfa, &
121 50 : rpgfb, sphi_a, sphi_b, zeta, zetb
122 50 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_kg
123 50 : TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
124 : TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
125 : TYPE(local_potential_type), POINTER :: tnadd_potential
126 : TYPE(neighbor_list_iterator_p_type), &
127 50 : DIMENSION(:), POINTER :: ap_iterator, nl_iterator
128 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
129 50 : POINTER :: sac_kin
130 :
131 100 : IF (calculate_forces) THEN
132 24 : CALL timeset(routineN//"_forces", handle)
133 : ELSE
134 26 : CALL timeset(routineN, handle)
135 : END IF
136 :
137 50 : NULLIFY (matrix_kg)
138 50 : IF (ASSOCIATED(kg_env%tnadd_mat)) THEN
139 34 : CALL dbcsr_deallocate_matrix_set(kg_env%tnadd_mat)
140 : END IF
141 50 : sac_kin => kg_env%sac_kin
142 50 : atom_to_molecule => kg_env%atom_to_molecule
143 :
144 50 : nkind = SIZE(atomic_kind_set)
145 50 : natom = SIZE(particle_set)
146 :
147 50 : CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, atom_of_kind=atom_of_kind)
148 :
149 50 : IF (calculate_forces) THEN
150 24 : nder = 1
151 24 : IF (SIZE(matrix_p, 1) == 2) THEN
152 0 : DO img = 1, SIZE(matrix_p, 2)
153 : CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
154 0 : alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
155 : CALL dbcsr_add(matrix_p(2, img)%matrix, matrix_p(1, img)%matrix, &
156 0 : alpha_scalar=-2.0_dp, beta_scalar=1.0_dp)
157 : END DO
158 : END IF
159 : ELSE
160 : nder = 0
161 : END IF
162 :
163 50 : maxder = ncoset(nder)
164 :
165 : CALL get_qs_kind_set(qs_kind_set, maxpol=maxpol, maxco=maxco, maxlgto=maxlgto, &
166 50 : maxsgf=maxsgf, maxnset=maxnset)
167 :
168 50 : maxl = MAX(maxlgto, maxpol)
169 50 : CALL init_orbital_pointers(maxl + nder + 1)
170 :
171 50 : ldsab = MAX(maxco, ncoset(maxpol), maxsgf, maxpol)
172 50 : ldai = ncoset(maxl + nder + 1)
173 :
174 224 : ALLOCATE (basis_set_list(nkind))
175 124 : DO ikind = 1, nkind
176 74 : CALL get_qs_kind(qs_kind_set(ikind), basis_set=basis_set_a)
177 124 : IF (ASSOCIATED(basis_set_a)) THEN
178 74 : basis_set_list(ikind)%gto_basis_set => basis_set_a
179 : ELSE
180 0 : NULLIFY (basis_set_list(ikind)%gto_basis_set)
181 : END IF
182 : END DO
183 :
184 : ! build the matrix
185 200 : ALLOCATE (row_blk_sizes(natom), col_blk_sizes(natom))
186 :
187 50 : CALL get_particle_set(particle_set, qs_kind_set, nsgf=row_blk_sizes)
188 50 : CALL get_particle_set(particle_set, qs_kind_set, nsgf=col_blk_sizes)
189 :
190 50 : CALL dbcsr_allocate_matrix_set(matrix_kg, 1)
191 :
192 50 : ALLOCATE (matrix_kg(1)%matrix)
193 : CALL dbcsr_create(matrix=matrix_kg(1)%matrix, &
194 : name="Nonadditive kinetic energy potential", &
195 : dist=dbcsr_dist, matrix_type=dbcsr_type_symmetric, &
196 : row_blk_size=row_blk_sizes, col_blk_size=col_blk_sizes, &
197 50 : reuse_arrays=.TRUE.)
198 50 : CALL cp_dbcsr_alloc_block_from_nbl(matrix_kg(1)%matrix, sab_orb)
199 50 : CALL dbcsr_set(matrix_kg(1)%matrix, 0.0_dp)
200 :
201 : nthread = 1
202 50 : !$ nthread = omp_get_max_threads()
203 :
204 50 : CALL neighbor_list_iterator_create(nl_iterator, sab_orb, nthread=nthread)
205 : ! iterator for basis/potential list
206 50 : CALL neighbor_list_iterator_create(ap_iterator, sac_kin, search=.TRUE., nthread=nthread)
207 :
208 : !$OMP PARALLEL &
209 : !$OMP DEFAULT (NONE) &
210 : !$OMP SHARED (nl_iterator, ap_iterator, basis_set_list, calculate_forces, force, use_virial,&
211 : !$OMP matrix_kg, matrix_p,virial, atomic_kind_set, qs_kind_set, particle_set,&
212 : !$OMP sab_orb, sac_kin, nthread, ncoset, nkind,&
213 : !$OMP atom_of_kind, ldsab, maxnset, maxder, &
214 : !$OMP maxlgto, nder, maxco, atom_to_molecule) &
215 : !$OMP PRIVATE (ikind, jkind, inode, iatom, jatom, rab, basis_set_a, basis_set_b, atom_a, &
216 : !$OMP atom_b, first_sgfa, la_max, la_min, npgfa, nsgfa, sphi_a, &
217 : !$OMP zeta, first_sgfb, lb_max, lb_min, npgfb, nsetb, rpgfb, set_radius_b, sphi_b, &
218 : !$OMP zetb, last_iatom, last_jatom, new_atom_pair, dab, irow, icol, h_block, found, iset, ncoa, &
219 : !$OMP sgfa, jset, ncob, sgfb, nsgfb, p_block, work, pab, hab, kkind, nseta, &
220 : !$OMP radius, alpha, nct, imol, jmol, kmol,&
221 : !$OMP npol, ngau, cval, ccval, rac, dac, rbc, dbc, &
222 : !$OMP set_radius_a, rpgfa, force_a, force_b, ppl_fwork, mepos, &
223 : !$OMP f0, katom, ppl_work, atom_c,&
224 50 : !$OMP ldai,tnadd_potential)
225 :
226 : mepos = 0
227 : !$ mepos = omp_get_thread_num()
228 :
229 : ALLOCATE (hab(ldsab, ldsab, maxnset, maxnset), work(ldsab, ldsab*maxder))
230 : ldai = ncoset(2*maxlgto + 2*nder)
231 : ALLOCATE (ppl_work(ldai, ldai, MAX(maxder, 2*maxlgto + 2*nder + 1)))
232 : IF (calculate_forces) THEN
233 : ALLOCATE (pab(maxco, maxco, maxnset, maxnset))
234 : ldai = ncoset(maxlgto)
235 : ALLOCATE (ppl_fwork(ldai, ldai, maxder))
236 : END IF
237 :
238 : last_iatom = 0
239 : last_jatom = 0
240 : DO WHILE (neighbor_list_iterate(nl_iterator, mepos=mepos) == 0)
241 :
242 : CALL get_iterator_info(nl_iterator, mepos=mepos, ikind=ikind, jkind=jkind, inode=inode, &
243 : iatom=iatom, jatom=jatom, r=rab)
244 :
245 : basis_set_a => basis_set_list(ikind)%gto_basis_set
246 : IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
247 : basis_set_b => basis_set_list(jkind)%gto_basis_set
248 : IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
249 :
250 : atom_a = atom_of_kind(iatom)
251 : atom_b = atom_of_kind(jatom)
252 : imol = atom_to_molecule(iatom)
253 : jmol = atom_to_molecule(jatom)
254 : CPASSERT(imol == jmol)
255 :
256 : ! basis ikind
257 : first_sgfa => basis_set_a%first_sgf
258 : la_max => basis_set_a%lmax
259 : la_min => basis_set_a%lmin
260 : npgfa => basis_set_a%npgf
261 : nseta = basis_set_a%nset
262 : nsgfa => basis_set_a%nsgf_set
263 : rpgfa => basis_set_a%pgf_radius
264 : set_radius_a => basis_set_a%set_radius
265 : sphi_a => basis_set_a%sphi
266 : zeta => basis_set_a%zet
267 : ! basis jkind
268 : first_sgfb => basis_set_b%first_sgf
269 : lb_max => basis_set_b%lmax
270 : lb_min => basis_set_b%lmin
271 : npgfb => basis_set_b%npgf
272 : nsetb = basis_set_b%nset
273 : nsgfb => basis_set_b%nsgf_set
274 : rpgfb => basis_set_b%pgf_radius
275 : set_radius_b => basis_set_b%set_radius
276 : sphi_b => basis_set_b%sphi
277 : zetb => basis_set_b%zet
278 :
279 : dab = SQRT(SUM(rab*rab))
280 :
281 : IF (iatom == last_iatom .AND. jatom == last_jatom) THEN
282 : new_atom_pair = .FALSE.
283 : ELSE
284 : new_atom_pair = .TRUE.
285 : last_jatom = jatom
286 : last_iatom = iatom
287 : END IF
288 :
289 : ! *** Use the symmetry of the first derivatives ***
290 : IF (iatom == jatom) THEN
291 : f0 = 1.0_dp
292 : ELSE
293 : f0 = 2.0_dp
294 : END IF
295 :
296 : ! *** Create matrix blocks for a new matrix block column ***
297 : IF (new_atom_pair) THEN
298 : IF (iatom <= jatom) THEN
299 : irow = iatom
300 : icol = jatom
301 : ELSE
302 : irow = jatom
303 : icol = iatom
304 : END IF
305 : NULLIFY (h_block)
306 : CALL dbcsr_get_block_p(matrix_kg(1)%matrix, irow, icol, h_block, found)
307 : IF (ASSOCIATED(h_block)) THEN
308 : IF (calculate_forces) THEN
309 : CPASSERT(SIZE(matrix_p, 2) == 1)
310 : NULLIFY (p_block)
311 : CALL dbcsr_get_block_p(matrix_p(1, 1)%matrix, irow, icol, p_block, found)
312 : IF (ASSOCIATED(p_block)) THEN
313 : DO iset = 1, nseta
314 : ncoa = npgfa(iset)*ncoset(la_max(iset))
315 : sgfa = first_sgfa(1, iset)
316 : DO jset = 1, nsetb
317 : ncob = npgfb(jset)*ncoset(lb_max(jset))
318 : sgfb = first_sgfb(1, jset)
319 :
320 : ! *** Decontract density matrix block ***
321 : IF (iatom <= jatom) THEN
322 : CALL dgemm("N", "N", ncoa, nsgfb(jset), nsgfa(iset), &
323 : 1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
324 : p_block(sgfa, sgfb), SIZE(p_block, 1), &
325 : 0.0_dp, work(1, 1), SIZE(work, 1))
326 : ELSE
327 : CALL dgemm("N", "T", ncoa, nsgfb(jset), nsgfa(iset), &
328 : 1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
329 : p_block(sgfb, sgfa), SIZE(p_block, 1), &
330 : 0.0_dp, work(1, 1), SIZE(work, 1))
331 : END IF
332 :
333 : CALL dgemm("N", "T", ncoa, ncob, nsgfb(jset), &
334 : 1.0_dp, work(1, 1), SIZE(work, 1), &
335 : sphi_b(1, sgfb), SIZE(sphi_b, 1), &
336 : 0.0_dp, pab(1, 1, iset, jset), SIZE(pab, 1))
337 : END DO
338 : END DO
339 : END IF
340 : END IF
341 : END IF
342 : END IF
343 :
344 : hab = 0._dp
345 :
346 : ! loop over all kinds for nonadditive kinetic energy potential atoms
347 : DO kkind = 1, nkind
348 :
349 : CALL get_qs_kind(qs_kind_set(kkind), tnadd_potential=tnadd_potential)
350 : IF (.NOT. ASSOCIATED(tnadd_potential)) CYCLE
351 : CALL get_potential(potential=tnadd_potential, &
352 : alpha=alpha, cval=cval, ngau=ngau, npol=npol, radius=radius)
353 : nct = npol
354 : ALLOCATE (ccval(npol, ngau))
355 : ccval(1:npol, 1:ngau) = TRANSPOSE(cval(1:ngau, 1:npol))
356 :
357 : CALL nl_set_sub_iterator(ap_iterator, ikind, kkind, iatom, mepos)
358 :
359 : DO WHILE (nl_sub_iterate(ap_iterator, mepos) == 0)
360 :
361 : CALL get_iterator_info(ap_iterator, mepos, jatom=katom, r=rac)
362 :
363 : ! this potential only acts on other moleclules
364 : kmol = atom_to_molecule(katom)
365 : IF (kmol == imol) CYCLE
366 :
367 : dac = SQRT(SUM(rac*rac))
368 : rbc(:) = rac(:) - rab(:)
369 : dbc = SQRT(SUM(rbc*rbc))
370 : IF ((MAXVAL(set_radius_a(:)) + radius < dac) .OR. &
371 : (MAXVAL(set_radius_b(:)) + radius < dbc)) THEN
372 : CYCLE
373 : END IF
374 :
375 : DO iset = 1, nseta
376 : IF (set_radius_a(iset) + radius < dac) CYCLE
377 : ncoa = npgfa(iset)*ncoset(la_max(iset))
378 : sgfa = first_sgfa(1, iset)
379 : DO jset = 1, nsetb
380 : IF (set_radius_b(jset) + radius < dbc) CYCLE
381 : ncob = npgfb(jset)*ncoset(lb_max(jset))
382 : sgfb = first_sgfb(1, jset)
383 : IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
384 : ! *** Calculate the GTH pseudo potential forces ***
385 : IF (calculate_forces) THEN
386 :
387 : CALL ppl_integral( &
388 : la_max(iset), la_min(iset), npgfa(iset), &
389 : rpgfa(:, iset), zeta(:, iset), &
390 : lb_max(jset), lb_min(jset), npgfb(jset), &
391 : rpgfb(:, jset), zetb(:, jset), &
392 : ngau, alpha, nct, ccval, radius, &
393 : rab, dab, rac, dac, rbc, dbc, &
394 : hab(:, :, iset, jset), ppl_work, pab(:, :, iset, jset), &
395 : force_a, force_b, ppl_fwork)
396 : ! *** The derivatives w.r.t. atomic center c are ***
397 : ! *** calculated using the translational invariance ***
398 : ! *** of the first derivatives ***
399 : atom_c = atom_of_kind(katom)
400 :
401 : !$OMP CRITICAL(force_critical)
402 : force(ikind)%kinetic(1, atom_a) = force(ikind)%kinetic(1, atom_a) + f0*force_a(1)
403 : force(ikind)%kinetic(2, atom_a) = force(ikind)%kinetic(2, atom_a) + f0*force_a(2)
404 : force(ikind)%kinetic(3, atom_a) = force(ikind)%kinetic(3, atom_a) + f0*force_a(3)
405 : force(kkind)%kinetic(1, atom_c) = force(kkind)%kinetic(1, atom_c) - f0*force_a(1)
406 : force(kkind)%kinetic(2, atom_c) = force(kkind)%kinetic(2, atom_c) - f0*force_a(2)
407 : force(kkind)%kinetic(3, atom_c) = force(kkind)%kinetic(3, atom_c) - f0*force_a(3)
408 :
409 : force(jkind)%kinetic(1, atom_b) = force(jkind)%kinetic(1, atom_b) + f0*force_b(1)
410 : force(jkind)%kinetic(2, atom_b) = force(jkind)%kinetic(2, atom_b) + f0*force_b(2)
411 : force(jkind)%kinetic(3, atom_b) = force(jkind)%kinetic(3, atom_b) + f0*force_b(3)
412 : force(kkind)%kinetic(1, atom_c) = force(kkind)%kinetic(1, atom_c) - f0*force_b(1)
413 : force(kkind)%kinetic(2, atom_c) = force(kkind)%kinetic(2, atom_c) - f0*force_b(2)
414 : force(kkind)%kinetic(3, atom_c) = force(kkind)%kinetic(3, atom_c) - f0*force_b(3)
415 :
416 : IF (use_virial) THEN
417 : CALL virial_pair_force(virial%pv_virial, f0, force_a, rac)
418 : CALL virial_pair_force(virial%pv_virial, f0, force_b, rbc)
419 : END IF
420 : !$OMP END CRITICAL(force_critical)
421 :
422 : ELSE
423 : CALL ppl_integral( &
424 : la_max(iset), la_min(iset), npgfa(iset), &
425 : rpgfa(:, iset), zeta(:, iset), &
426 : lb_max(jset), lb_min(jset), npgfb(jset), &
427 : rpgfb(:, jset), zetb(:, jset), &
428 : ngau, alpha, nct, ccval, radius, &
429 : rab, dab, rac, dac, rbc, dbc, hab(:, :, iset, jset), ppl_work)
430 : END IF
431 : END DO
432 : END DO
433 : END DO
434 : DEALLOCATE (ccval)
435 : END DO
436 :
437 : ! *** Contract integrals
438 : DO iset = 1, nseta
439 : ncoa = npgfa(iset)*ncoset(la_max(iset))
440 : sgfa = first_sgfa(1, iset)
441 : DO jset = 1, nsetb
442 : ncob = npgfb(jset)*ncoset(lb_max(jset))
443 : sgfb = first_sgfb(1, jset)
444 :
445 : CALL dgemm("N", "N", ncoa, nsgfb(jset), ncob, &
446 : 1.0_dp, hab(1, 1, iset, jset), SIZE(hab, 1), &
447 : sphi_b(1, sgfb), SIZE(sphi_b, 1), &
448 : 0.0_dp, work(1, 1), SIZE(work, 1))
449 :
450 : !$OMP CRITICAL(h_block_critical)
451 : IF (iatom <= jatom) THEN
452 : CALL dgemm("T", "N", nsgfa(iset), nsgfb(jset), ncoa, &
453 : 1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
454 : work(1, 1), SIZE(work, 1), &
455 : 1.0_dp, h_block(sgfa, sgfb), SIZE(h_block, 1))
456 : ELSE
457 : CALL dgemm("T", "N", nsgfb(jset), nsgfa(iset), ncoa, &
458 : 1.0_dp, work(1, 1), SIZE(work, 1), &
459 : sphi_a(1, sgfa), SIZE(sphi_a, 1), &
460 : 1.0_dp, h_block(sgfb, sgfa), SIZE(h_block, 1))
461 : END IF
462 : !$OMP END CRITICAL(h_block_critical)
463 :
464 : END DO
465 : END DO
466 : END DO
467 :
468 : DEALLOCATE (hab, work, ppl_work)
469 :
470 : IF (calculate_forces) THEN
471 : DEALLOCATE (pab, ppl_fwork)
472 : END IF
473 :
474 : !$OMP END PARALLEL
475 :
476 50 : CALL neighbor_list_iterator_release(ap_iterator)
477 50 : CALL neighbor_list_iterator_release(nl_iterator)
478 :
479 100 : DO i = 1, SIZE(matrix_kg)
480 100 : CALL dbcsr_finalize(matrix_kg(i)%matrix)
481 : END DO
482 50 : kg_env%tnadd_mat => matrix_kg
483 :
484 50 : DEALLOCATE (basis_set_list)
485 :
486 50 : IF (calculate_forces) THEN
487 : ! *** If LSD, then recover alpha density and beta density ***
488 : ! *** from the total density (1) and the spin density (2) ***
489 24 : IF (SIZE(matrix_p, 1) == 2) THEN
490 0 : DO img = 1, SIZE(matrix_p, 2)
491 : CALL dbcsr_add(matrix_p(1, img)%matrix, matrix_p(2, img)%matrix, &
492 0 : alpha_scalar=0.5_dp, beta_scalar=0.5_dp)
493 : CALL dbcsr_add(matrix_p(2, img)%matrix, matrix_p(1, img)%matrix, &
494 0 : alpha_scalar=-1.0_dp, beta_scalar=1.0_dp)
495 : END DO
496 : END IF
497 : END IF
498 :
499 50 : CALL timestop(handle)
500 :
501 150 : END SUBROUTINE build_tnadd_mat
502 :
503 : !==========================================================================================================
504 :
505 : END MODULE kg_tnadd_mat
|