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