-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_efield_local.F
395 lines (343 loc) · 18 KB
/
qs_efield_local.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Calculates the energy contribution and the mo_derivative of
!> a static electric field (nonperiodic)
!> \par History
!> none
!> \author JGH (05.2015)
! **************************************************************************************************
MODULE qs_efield_local
USE ai_moments, ONLY: dipole_force
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind,&
get_atomic_kind_set
USE basis_set_types, ONLY: gto_basis_set_p_type,&
gto_basis_set_type
USE cell_types, ONLY: cell_type,&
pbc
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: dbcsr_add,&
dbcsr_copy,&
dbcsr_dot,&
dbcsr_get_block_p,&
dbcsr_p_type,&
dbcsr_set
USE kinds, ONLY: dp
USE message_passing, ONLY: mp_para_env_type
USE orbital_pointers, ONLY: ncoset
USE particle_types, ONLY: particle_type
USE qs_energy_types, ONLY: qs_energy_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type,&
set_qs_env
USE qs_force_types, ONLY: qs_force_type
USE qs_kind_types, ONLY: get_qs_kind,&
qs_kind_type
USE qs_moments, ONLY: build_local_moment_matrix
USE qs_neighbor_list_types, ONLY: get_iterator_info,&
neighbor_list_iterate,&
neighbor_list_iterator_create,&
neighbor_list_iterator_p_type,&
neighbor_list_iterator_release,&
neighbor_list_set_p_type
USE qs_period_efield_types, ONLY: efield_berry_type,&
init_efield_matrices,&
set_efield_matrices
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_efield_local'
! *** Public subroutines ***
PUBLIC :: qs_efield_local_operator
! **************************************************************************************************
CONTAINS
! **************************************************************************************************
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param just_energy ...
!> \param calculate_forces ...
! **************************************************************************************************
SUBROUTINE qs_efield_local_operator(qs_env, just_energy, calculate_forces)
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, INTENT(IN) :: just_energy, calculate_forces
CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_efield_local_operator'
INTEGER :: handle
LOGICAL :: s_mstruct_changed
REAL(dp), DIMENSION(3) :: rpoint
TYPE(dft_control_type), POINTER :: dft_control
CALL timeset(routineN, handle)
NULLIFY (dft_control)
CALL get_qs_env(qs_env, s_mstruct_changed=s_mstruct_changed, &
dft_control=dft_control)
IF (dft_control%apply_efield) THEN
rpoint = 0.0_dp
IF (s_mstruct_changed) CALL qs_efield_integrals(qs_env, rpoint)
CALL qs_efield_mo_derivatives(qs_env, rpoint, just_energy, calculate_forces)
END IF
CALL timestop(handle)
END SUBROUTINE qs_efield_local_operator
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param rpoint ...
! **************************************************************************************************
SUBROUTINE qs_efield_integrals(qs_env, rpoint)
TYPE(qs_environment_type), POINTER :: qs_env
REAL(dp), DIMENSION(3), INTENT(IN) :: rpoint
CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_efield_integrals'
INTEGER :: handle, i
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: dipmat, matrix_s
TYPE(dft_control_type), POINTER :: dft_control
TYPE(efield_berry_type), POINTER :: efield
CALL timeset(routineN, handle)
CPASSERT(ASSOCIATED(qs_env))
CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
NULLIFY (matrix_s)
CALL get_qs_env(qs_env=qs_env, efield=efield, matrix_s=matrix_s)
CALL init_efield_matrices(efield)
ALLOCATE (dipmat(3))
DO i = 1, 3
ALLOCATE (dipmat(i)%matrix)
CALL dbcsr_copy(dipmat(i)%matrix, matrix_s(1)%matrix, 'DIP MAT')
CALL dbcsr_set(dipmat(i)%matrix, 0.0_dp)
END DO
CALL build_local_moment_matrix(qs_env, dipmat, 1, rpoint)
CALL set_efield_matrices(efield=efield, dipmat=dipmat)
CALL set_qs_env(qs_env=qs_env, efield=efield)
CALL timestop(handle)
END SUBROUTINE qs_efield_integrals
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param rpoint ...
!> \param just_energy ...
!> \param calculate_forces ...
! **************************************************************************************************
SUBROUTINE qs_efield_mo_derivatives(qs_env, rpoint, just_energy, calculate_forces)
TYPE(qs_environment_type), POINTER :: qs_env
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rpoint
LOGICAL :: just_energy, calculate_forces
CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_efield_mo_derivatives'
INTEGER :: atom_a, atom_b, handle, i, ia, iatom, icol, idir, ikind, irow, iset, ispin, &
jatom, jkind, jset, ldab, natom, ncoa, ncob, nkind, nseta, nsetb, sgfa, sgfb
INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind
INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, npgfa, &
npgfb, nsgfa, nsgfb
INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, first_sgfb
LOGICAL :: found, trans
REAL(dp) :: charge, ci(3), dab, ener_field, fdir, &
fieldpol(3), tmp
REAL(dp), DIMENSION(3) :: ra, rab, rac, rbc, ria
REAL(dp), DIMENSION(3, 3) :: forcea, forceb
REAL(dp), DIMENSION(:, :), POINTER :: p_block_a, p_block_b, pblock, pmat, work
REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
REAL(KIND=dp), DIMENSION(:, :), POINTER :: rpgfa, rpgfb, sphi_a, sphi_b, zeta, zetb
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: dipmat, matrix_ks, matrix_p
TYPE(dft_control_type), POINTER :: dft_control
TYPE(efield_berry_type), POINTER :: efield
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_orb
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_energy_type), POINTER :: energy
TYPE(qs_force_type), DIMENSION(:), POINTER :: force
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_kind_type), POINTER :: qs_kind
TYPE(qs_rho_type), POINTER :: rho
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, dft_control=dft_control, cell=cell, particle_set=particle_set)
CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set, &
efield=efield, energy=energy, para_env=para_env, sab_orb=sab_orb)
fieldpol = dft_control%efield_fields(1)%efield%polarisation* &
dft_control%efield_fields(1)%efield%strength
! nuclear contribution
natom = SIZE(particle_set)
IF (calculate_forces) THEN
CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, force=force)
CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind)
END IF
ci = 0.0_dp
DO ia = 1, natom
CALL get_atomic_kind(particle_set(ia)%atomic_kind, kind_number=ikind)
CALL get_qs_kind(qs_kind_set(ikind), core_charge=charge)
ria = particle_set(ia)%r - rpoint
ria = pbc(ria, cell)
ci(:) = ci(:) + charge*ria(:)
IF (calculate_forces) THEN
IF (para_env%mepos == 0) THEN
iatom = atom_of_kind(ia)
DO idir = 1, 3
force(ikind)%efield(idir, iatom) = force(ikind)%efield(idir, iatom) - fieldpol(idir)*charge
END DO
END IF
END IF
END DO
ener_field = -SUM(ci(:)*fieldpol(:))
! Energy
dipmat => efield%dipmat
NULLIFY (rho, matrix_p)
CALL get_qs_env(qs_env=qs_env, rho=rho)
CALL qs_rho_get(rho, rho_ao=matrix_p)
DO ispin = 1, SIZE(matrix_p)
DO idir = 1, 3
CALL dbcsr_dot(matrix_p(ispin)%matrix, dipmat(idir)%matrix, tmp)
ener_field = ener_field + fieldpol(idir)*tmp
END DO
END DO
energy%efield = ener_field
IF (.NOT. just_energy) THEN
! Update KS matrix
NULLIFY (matrix_ks)
CALL get_qs_env(qs_env=qs_env, matrix_ks=matrix_ks)
DO ispin = 1, SIZE(matrix_ks)
DO idir = 1, 3
CALL dbcsr_add(matrix_ks(ispin)%matrix, dipmat(idir)%matrix, &
alpha_scalar=1.0_dp, beta_scalar=fieldpol(idir))
END DO
END DO
! forces from the efield contribution
IF (calculate_forces) THEN
nkind = SIZE(qs_kind_set)
natom = SIZE(particle_set)
ALLOCATE (basis_set_list(nkind))
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a)
IF (ASSOCIATED(basis_set_a)) THEN
basis_set_list(ikind)%gto_basis_set => basis_set_a
ELSE
NULLIFY (basis_set_list(ikind)%gto_basis_set)
END IF
END DO
!
CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
iatom=iatom, jatom=jatom, r=rab)
basis_set_a => basis_set_list(ikind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
basis_set_b => basis_set_list(jkind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
! basis ikind
first_sgfa => basis_set_a%first_sgf
la_max => basis_set_a%lmax
la_min => basis_set_a%lmin
npgfa => basis_set_a%npgf
nseta = basis_set_a%nset
nsgfa => basis_set_a%nsgf_set
rpgfa => basis_set_a%pgf_radius
set_radius_a => basis_set_a%set_radius
sphi_a => basis_set_a%sphi
zeta => basis_set_a%zet
! basis jkind
first_sgfb => basis_set_b%first_sgf
lb_max => basis_set_b%lmax
lb_min => basis_set_b%lmin
npgfb => basis_set_b%npgf
nsetb = basis_set_b%nset
nsgfb => basis_set_b%nsgf_set
rpgfb => basis_set_b%pgf_radius
set_radius_b => basis_set_b%set_radius
sphi_b => basis_set_b%sphi
zetb => basis_set_b%zet
atom_a = atom_of_kind(iatom)
atom_b = atom_of_kind(jatom)
ra(:) = particle_set(iatom)%r(:) - rpoint(:)
rac(:) = pbc(ra(:), cell)
rbc(:) = rac(:) + rab(:)
dab = SQRT(rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3))
IF (iatom <= jatom) THEN
irow = iatom
icol = jatom
trans = .FALSE.
ELSE
irow = jatom
icol = iatom
trans = .TRUE.
END IF
fdir = 2.0_dp
IF (iatom == jatom .AND. dab < 1.e-10_dp) fdir = 1.0_dp
! density matrix
NULLIFY (p_block_a)
CALL dbcsr_get_block_p(matrix_p(1)%matrix, irow, icol, p_block_a, found)
IF (.NOT. found) CYCLE
IF (SIZE(matrix_p) > 1) THEN
NULLIFY (p_block_b)
CALL dbcsr_get_block_p(matrix_p(2)%matrix, irow, icol, p_block_b, found)
CPASSERT(found)
END IF
forcea = 0.0_dp
forceb = 0.0_dp
DO iset = 1, nseta
ncoa = npgfa(iset)*ncoset(la_max(iset))
sgfa = first_sgfa(1, iset)
DO jset = 1, nsetb
IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
ncob = npgfb(jset)*ncoset(lb_max(jset))
sgfb = first_sgfb(1, jset)
! Calculate the primitive integrals (da|O|b) and (a|O|db)
ldab = MAX(ncoa, ncob)
ALLOCATE (work(ldab, ldab), pmat(ncoa, ncob))
! Decontract P matrix block
pmat = 0.0_dp
DO i = 1, SIZE(matrix_p)
IF (i == 1) THEN
pblock => p_block_a
ELSE
pblock => p_block_b
END IF
IF (trans) THEN
CALL dgemm("N", "T", ncoa, nsgfb(jset), nsgfa(iset), &
1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
pblock(sgfb, sgfa), SIZE(pblock, 1), &
0.0_dp, work(1, 1), ldab)
ELSE
CALL dgemm("N", "N", ncoa, nsgfb(jset), nsgfa(iset), &
1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
pblock(sgfa, sgfb), SIZE(pblock, 1), &
0.0_dp, work(1, 1), ldab)
END IF
CALL dgemm("N", "T", ncoa, ncob, nsgfb(jset), &
1.0_dp, work(1, 1), ldab, &
sphi_b(1, sgfb), SIZE(sphi_b, 1), &
1.0_dp, pmat(1, 1), ncoa)
END DO
CALL dipole_force(la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
lb_max(jset), npgfb(jset), zetb(:, jset), rpgfb(:, jset), lb_min(jset), &
1, rac, rbc, pmat, forcea, forceb)
DEALLOCATE (work, pmat)
END DO
END DO
DO idir = 1, 3
force(ikind)%efield(1:3, atom_a) = force(ikind)%efield(1:3, atom_a) &
+ fdir*fieldpol(idir)*forcea(idir, 1:3)
force(jkind)%efield(1:3, atom_b) = force(jkind)%efield(1:3, atom_b) &
+ fdir*fieldpol(idir)*forceb(idir, 1:3)
END DO
END DO
CALL neighbor_list_iterator_release(nl_iterator)
DEALLOCATE (basis_set_list)
END IF
END IF
IF (calculate_forces) THEN
DO ikind = 1, SIZE(atomic_kind_set)
CALL para_env%sum(force(ikind)%efield)
END DO
END IF
CALL timestop(handle)
END SUBROUTINE qs_efield_mo_derivatives
END MODULE qs_efield_local