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 periodic electric field
11 : !> \par History
12 : !> none
13 : !> \author fschiff (06.2010)
14 : ! **************************************************************************************************
15 : MODULE qs_efield_berry
16 : USE ai_moments, ONLY: cossin
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 block_p_types, ONLY: block_p_type
23 : USE cell_types, ONLY: cell_type,&
24 : pbc
25 : USE cp_cfm_basic_linalg, ONLY: cp_cfm_scale_and_add_fm,&
26 : cp_cfm_solve
27 : USE cp_cfm_types, ONLY: cp_cfm_create,&
28 : cp_cfm_release,&
29 : cp_cfm_set_all,&
30 : cp_cfm_type
31 : USE cp_control_types, ONLY: dft_control_type
32 : USE cp_dbcsr_api, ONLY: dbcsr_copy,&
33 : dbcsr_get_block_p,&
34 : dbcsr_p_type,&
35 : dbcsr_set,&
36 : dbcsr_type
37 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
38 : copy_fm_to_dbcsr,&
39 : cp_dbcsr_plus_fm_fm_t,&
40 : cp_dbcsr_sm_fm_multiply,&
41 : dbcsr_deallocate_matrix_set
42 : USE cp_fm_basic_linalg, ONLY: cp_fm_scale_and_add
43 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
44 : cp_fm_struct_release,&
45 : cp_fm_struct_type
46 : USE cp_fm_types, ONLY: cp_fm_create,&
47 : cp_fm_release,&
48 : cp_fm_set_all,&
49 : cp_fm_type
50 : USE kinds, ONLY: dp
51 : USE mathconstants, ONLY: gaussi,&
52 : pi,&
53 : twopi,&
54 : z_one,&
55 : z_zero
56 : USE message_passing, ONLY: mp_para_env_type
57 : USE orbital_pointers, ONLY: ncoset
58 : USE parallel_gemm_api, ONLY: parallel_gemm
59 : USE particle_types, ONLY: particle_type
60 : USE qs_energy_types, ONLY: qs_energy_type
61 : USE qs_environment_types, ONLY: get_qs_env,&
62 : qs_environment_type,&
63 : set_qs_env
64 : USE qs_force_types, ONLY: qs_force_type
65 : USE qs_kind_types, ONLY: get_qs_kind,&
66 : get_qs_kind_set,&
67 : qs_kind_type
68 : USE qs_mo_types, ONLY: get_mo_set,&
69 : mo_set_type
70 : USE qs_moments, ONLY: build_berry_moment_matrix
71 : USE qs_neighbor_list_types, ONLY: get_iterator_info,&
72 : neighbor_list_iterate,&
73 : neighbor_list_iterator_create,&
74 : neighbor_list_iterator_p_type,&
75 : neighbor_list_iterator_release,&
76 : neighbor_list_set_p_type
77 : USE qs_period_efield_types, ONLY: efield_berry_type,&
78 : init_efield_matrices,&
79 : set_efield_matrices
80 : USE virial_methods, ONLY: virial_pair_force
81 : USE virial_types, ONLY: virial_type
82 : #include "./base/base_uses.f90"
83 :
84 : IMPLICIT NONE
85 :
86 : PRIVATE
87 :
88 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_efield_berry'
89 :
90 : ! *** Public subroutines ***
91 :
92 : PUBLIC :: qs_efield_berry_phase
93 :
94 : ! **************************************************************************************************
95 :
96 : CONTAINS
97 :
98 : ! **************************************************************************************************
99 :
100 : ! **************************************************************************************************
101 : !> \brief ...
102 : !> \param qs_env ...
103 : !> \param just_energy ...
104 : !> \param calculate_forces ...
105 : ! **************************************************************************************************
106 121829 : SUBROUTINE qs_efield_berry_phase(qs_env, just_energy, calculate_forces)
107 :
108 : TYPE(qs_environment_type), POINTER :: qs_env
109 : LOGICAL, INTENT(IN) :: just_energy, calculate_forces
110 :
111 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_efield_berry_phase'
112 :
113 : INTEGER :: handle
114 : LOGICAL :: s_mstruct_changed
115 : TYPE(dft_control_type), POINTER :: dft_control
116 :
117 121829 : CALL timeset(routineN, handle)
118 :
119 121829 : NULLIFY (dft_control)
120 : CALL get_qs_env(qs_env, s_mstruct_changed=s_mstruct_changed, &
121 121829 : dft_control=dft_control)
122 :
123 121829 : IF (dft_control%apply_period_efield) THEN
124 : ! check if the periodic efield should be applied in the current step
125 3758 : IF (dft_control%period_efield%start_frame <= qs_env%sim_step .AND. &
126 : (dft_control%period_efield%end_frame == -1 .OR. dft_control%period_efield%end_frame >= qs_env%sim_step)) THEN
127 :
128 3326 : IF (s_mstruct_changed) CALL qs_efield_integrals(qs_env)
129 3326 : IF (dft_control%period_efield%displacement_field) THEN
130 898 : CALL qs_dispfield_derivatives(qs_env, just_energy, calculate_forces)
131 : ELSE
132 2428 : CALL qs_efield_derivatives(qs_env, just_energy, calculate_forces)
133 : END IF
134 : END IF
135 : END IF
136 :
137 121829 : CALL timestop(handle)
138 :
139 121829 : END SUBROUTINE qs_efield_berry_phase
140 :
141 : ! **************************************************************************************************
142 : !> \brief ...
143 : !> \param qs_env ...
144 : ! **************************************************************************************************
145 252 : SUBROUTINE qs_efield_integrals(qs_env)
146 :
147 : TYPE(qs_environment_type), POINTER :: qs_env
148 :
149 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_efield_integrals'
150 :
151 : INTEGER :: handle, i
152 : REAL(dp), DIMENSION(3) :: kvec
153 : TYPE(cell_type), POINTER :: cell
154 252 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: cosmat, matrix_s, sinmat
155 : TYPE(dft_control_type), POINTER :: dft_control
156 : TYPE(efield_berry_type), POINTER :: efield
157 :
158 252 : CALL timeset(routineN, handle)
159 252 : CPASSERT(ASSOCIATED(qs_env))
160 :
161 252 : CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
162 252 : NULLIFY (matrix_s)
163 252 : CALL get_qs_env(qs_env=qs_env, efield=efield, cell=cell, matrix_s=matrix_s)
164 252 : CALL init_efield_matrices(efield)
165 1764 : ALLOCATE (cosmat(3), sinmat(3))
166 1008 : DO i = 1, 3
167 756 : ALLOCATE (cosmat(i)%matrix, sinmat(i)%matrix)
168 :
169 756 : CALL dbcsr_copy(cosmat(i)%matrix, matrix_s(1)%matrix, 'COS MAT')
170 756 : CALL dbcsr_copy(sinmat(i)%matrix, matrix_s(1)%matrix, 'SIN MAT')
171 756 : CALL dbcsr_set(cosmat(i)%matrix, 0.0_dp)
172 756 : CALL dbcsr_set(sinmat(i)%matrix, 0.0_dp)
173 :
174 3024 : kvec(:) = twopi*cell%h_inv(i, :)
175 1008 : CALL build_berry_moment_matrix(qs_env, cosmat(i)%matrix, sinmat(i)%matrix, kvec)
176 : END DO
177 252 : CALL set_efield_matrices(efield=efield, cosmat=cosmat, sinmat=sinmat)
178 252 : CALL set_qs_env(qs_env=qs_env, efield=efield)
179 252 : CALL timestop(handle)
180 :
181 252 : END SUBROUTINE qs_efield_integrals
182 :
183 : ! **************************************************************************************************
184 : !> \brief ...
185 : !> \param qs_env ...
186 : !> \param just_energy ...
187 : !> \param calculate_forces ...
188 : ! **************************************************************************************************
189 2428 : SUBROUTINE qs_efield_derivatives(qs_env, just_energy, calculate_forces)
190 : TYPE(qs_environment_type), POINTER :: qs_env
191 : LOGICAL, INTENT(IN) :: just_energy, calculate_forces
192 :
193 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_efield_derivatives'
194 :
195 : COMPLEX(dp) :: zdet, zdeta, zi(3)
196 : INTEGER :: atom_a, atom_b, handle, i, ia, iatom, icol, idir, ikind, irow, iset, ispin, j, &
197 : jatom, jkind, jset, ldab, ldsa, ldsb, lsab, n1, n2, nao, natom, ncoa, ncob, nkind, nmo, &
198 : nseta, nsetb, sgfa, sgfb
199 2428 : INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind
200 2428 : INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, npgfa, &
201 2428 : npgfb, nsgfa, nsgfb
202 2428 : INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, first_sgfb
203 : LOGICAL :: found, uniform, use_virial
204 : REAL(dp) :: charge, ci(3), cqi(3), dab, dd, &
205 : ener_field, f0, fab, fieldpol(3), &
206 : focc, fpolvec(3), hmat(3, 3), occ, &
207 : qi(3), strength, ti(3)
208 : REAL(dp), DIMENSION(3) :: forcea, forceb, kvec, ra, rab, rb, ria
209 4856 : REAL(dp), DIMENSION(:, :), POINTER :: cosab, iblock, rblock, sinab, work
210 4856 : REAL(dp), DIMENSION(:, :, :), POINTER :: dcosab, dsinab
211 2428 : REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
212 2428 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: rpgfa, rpgfb, sphi_a, sphi_b, zeta, zetb
213 2428 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
214 43704 : TYPE(block_p_type), DIMENSION(3, 2) :: dcost, dsint
215 : TYPE(cell_type), POINTER :: cell
216 2428 : TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:) :: eigrmat, inv_mat
217 : TYPE(cp_fm_struct_type), POINTER :: tmp_fm_struct
218 2428 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: mo_coeff_tmp, mo_derivs_tmp
219 2428 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :) :: inv_work, op_fm_set, opvec
220 : TYPE(cp_fm_type), POINTER :: mo_coeff
221 2428 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s, mo_derivs
222 2428 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: tempmat
223 : TYPE(dbcsr_type), POINTER :: cosmat, mo_coeff_b, sinmat
224 : TYPE(dft_control_type), POINTER :: dft_control
225 : TYPE(efield_berry_type), POINTER :: efield
226 2428 : TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
227 : TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
228 2428 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
229 : TYPE(mp_para_env_type), POINTER :: para_env
230 : TYPE(neighbor_list_iterator_p_type), &
231 2428 : DIMENSION(:), POINTER :: nl_iterator
232 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
233 2428 : POINTER :: sab_orb
234 2428 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
235 : TYPE(qs_energy_type), POINTER :: energy
236 2428 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
237 2428 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
238 : TYPE(qs_kind_type), POINTER :: qs_kind
239 : TYPE(virial_type), POINTER :: virial
240 :
241 2428 : CALL timeset(routineN, handle)
242 :
243 2428 : NULLIFY (dft_control, cell, particle_set)
244 : CALL get_qs_env(qs_env, dft_control=dft_control, cell=cell, &
245 2428 : particle_set=particle_set, virial=virial)
246 2428 : NULLIFY (qs_kind_set, efield, para_env, sab_orb)
247 : CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set, &
248 2428 : efield=efield, energy=energy, para_env=para_env, sab_orb=sab_orb)
249 :
250 : ! calculate stress only if forces requested also
251 2428 : use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
252 0 : use_virial = use_virial .AND. calculate_forces
253 : ! disable stress calculation
254 : IF (use_virial) THEN
255 0 : CPABORT("Stress tensor for periodic E-field not implemented")
256 : END IF
257 :
258 : ! if an intensities list is given, select the value for the current step
259 2428 : strength = dft_control%period_efield%strength
260 2428 : IF (ALLOCATED(dft_control%period_efield%strength_list)) THEN
261 : strength = dft_control%period_efield%strength_list(MOD(qs_env%sim_step &
262 1152 : - dft_control%period_efield%start_frame, SIZE(dft_control%period_efield%strength_list)) + 1)
263 : END IF
264 :
265 9712 : fieldpol = dft_control%period_efield%polarisation
266 16996 : fieldpol = fieldpol/NORM2(fieldpol)
267 9712 : fieldpol = -fieldpol*strength
268 31564 : hmat = cell%hmat(:, :)/twopi
269 9712 : DO idir = 1, 3
270 9712 : fpolvec(idir) = fieldpol(1)*hmat(1, idir) + fieldpol(2)*hmat(2, idir) + fieldpol(3)*hmat(3, idir)
271 : END DO
272 :
273 : ! nuclear contribution
274 2428 : natom = SIZE(particle_set)
275 2428 : IF (calculate_forces) THEN
276 136 : CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, force=force)
277 136 : CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind)
278 : END IF
279 9712 : zi(:) = CMPLX(1._dp, 0._dp, dp)
280 9104 : DO ia = 1, natom
281 6676 : CALL get_atomic_kind(particle_set(ia)%atomic_kind, kind_number=ikind)
282 6676 : CALL get_qs_kind(qs_kind_set(ikind), core_charge=charge)
283 26704 : ria = particle_set(ia)%r
284 26704 : ria = pbc(ria, cell)
285 26704 : DO idir = 1, 3
286 80112 : kvec(:) = twopi*cell%h_inv(idir, :)
287 80112 : dd = SUM(kvec(:)*ria(:))
288 20028 : zdeta = CMPLX(COS(dd), SIN(dd), KIND=dp)**charge
289 26704 : zi(idir) = zi(idir)*zdeta
290 : END DO
291 6676 : IF (calculate_forces) THEN
292 434 : IF (para_env%mepos == 0) THEN
293 217 : iatom = atom_of_kind(ia)
294 868 : forcea(:) = fieldpol(:)*charge
295 868 : force(ikind)%efield(:, iatom) = force(ikind)%efield(:, iatom) + forcea(:)
296 : END IF
297 : END IF
298 15780 : IF (use_virial) THEN
299 0 : IF (para_env%mepos == 0) THEN
300 0 : CALL virial_pair_force(virial%pv_virial, 1.0_dp, forcea, ria)
301 : END IF
302 : END IF
303 : END DO
304 9712 : qi = AIMAG(LOG(zi))
305 :
306 : ! check uniform occupation
307 2428 : NULLIFY (mos)
308 2428 : CALL get_qs_env(qs_env=qs_env, mos=mos)
309 4958 : DO ispin = 1, dft_control%nspins
310 2530 : CALL get_mo_set(mo_set=mos(ispin), maxocc=occ, uniform_occupation=uniform)
311 4958 : IF (.NOT. uniform) THEN
312 0 : CPABORT("Berry phase moments for non uniform MOs' occupation numbers not implemented")
313 : END IF
314 : END DO
315 :
316 2428 : NULLIFY (mo_derivs)
317 2428 : CALL get_qs_env(qs_env=qs_env, mo_derivs=mo_derivs)
318 : ! initialize all work matrices needed
319 14874 : ALLOCATE (op_fm_set(2, dft_control%nspins))
320 14874 : ALLOCATE (opvec(2, dft_control%nspins))
321 9814 : ALLOCATE (eigrmat(dft_control%nspins))
322 9814 : ALLOCATE (inv_mat(dft_control%nspins))
323 14874 : ALLOCATE (inv_work(2, dft_control%nspins))
324 9814 : ALLOCATE (mo_derivs_tmp(SIZE(mo_derivs)))
325 7386 : ALLOCATE (mo_coeff_tmp(SIZE(mo_derivs)))
326 :
327 : ! Allocate temp matrices for the wavefunction derivatives
328 4958 : DO ispin = 1, dft_control%nspins
329 2530 : NULLIFY (tmp_fm_struct, mo_coeff)
330 2530 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nao=nao, nmo=nmo)
331 : CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=nmo, &
332 2530 : ncol_global=nmo, para_env=para_env, context=mo_coeff%matrix_struct%context)
333 2530 : CALL cp_fm_create(mo_derivs_tmp(ispin), mo_coeff%matrix_struct)
334 2530 : CALL cp_fm_create(mo_coeff_tmp(ispin), mo_coeff%matrix_struct)
335 2530 : CALL copy_dbcsr_to_fm(mo_derivs(ispin)%matrix, mo_derivs_tmp(ispin))
336 7590 : DO i = 1, SIZE(op_fm_set, 1)
337 5060 : CALL cp_fm_create(opvec(i, ispin), mo_coeff%matrix_struct)
338 5060 : CALL cp_fm_create(op_fm_set(i, ispin), tmp_fm_struct)
339 7590 : CALL cp_fm_create(inv_work(i, ispin), op_fm_set(i, ispin)%matrix_struct)
340 : END DO
341 2530 : CALL cp_cfm_create(eigrmat(ispin), op_fm_set(1, ispin)%matrix_struct)
342 2530 : CALL cp_cfm_create(inv_mat(ispin), op_fm_set(1, ispin)%matrix_struct)
343 7488 : CALL cp_fm_struct_release(tmp_fm_struct)
344 : END DO
345 : ! temp matrices for force calculation
346 2428 : IF (calculate_forces) THEN
347 136 : NULLIFY (matrix_s)
348 136 : CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s)
349 840 : ALLOCATE (tempmat(2, dft_control%nspins))
350 280 : DO ispin = 1, dft_control%nspins
351 144 : ALLOCATE (tempmat(1, ispin)%matrix, tempmat(2, ispin)%matrix)
352 144 : CALL dbcsr_copy(tempmat(1, ispin)%matrix, matrix_s(1)%matrix, 'TEMPMAT')
353 144 : CALL dbcsr_copy(tempmat(2, ispin)%matrix, matrix_s(1)%matrix, 'TEMPMAT')
354 144 : CALL dbcsr_set(tempmat(1, ispin)%matrix, 0.0_dp)
355 280 : CALL dbcsr_set(tempmat(2, ispin)%matrix, 0.0_dp)
356 : END DO
357 : ! integration
358 136 : CALL get_qs_kind_set(qs_kind_set, maxco=ldab, maxsgf=lsab)
359 1088 : ALLOCATE (cosab(ldab, ldab), sinab(ldab, ldab), work(ldab, ldab))
360 952 : ALLOCATE (dcosab(ldab, ldab, 3), dsinab(ldab, ldab, 3))
361 136 : lsab = MAX(ldab, lsab)
362 680 : DO i = 1, 3
363 2448 : ALLOCATE (dcost(i, 1)%block(lsab, lsab), dsint(i, 1)%block(lsab, lsab))
364 2176 : ALLOCATE (dcost(i, 2)%block(lsab, lsab), dsint(i, 2)%block(lsab, lsab))
365 : END DO
366 : END IF
367 :
368 : !Start the MO derivative calculation
369 : !loop over all cell vectors
370 9712 : DO idir = 1, 3
371 7284 : ci(idir) = 0.0_dp
372 : zi(idir) = z_zero
373 9712 : IF (ABS(fpolvec(idir)) > 1.0E-12_dp) THEN
374 3518 : cosmat => efield%cosmat(idir)%matrix
375 3518 : sinmat => efield%sinmat(idir)%matrix
376 : !evaluate the expression needed for the derivative (S_berry * C and [C^T S_berry C]^-1)
377 : !first step S_berry * C and C^T S_berry C
378 7138 : DO ispin = 1, dft_control%nspins ! spin
379 3620 : IF (mos(ispin)%use_mo_coeff_b) THEN
380 3620 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, mo_coeff_b=mo_coeff_b, nmo=nmo)
381 3620 : CALL copy_dbcsr_to_fm(mo_coeff_b, mo_coeff_tmp(ispin))
382 : ELSE
383 0 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, mo_coeff=mo_coeff, nmo=nmo)
384 0 : mo_coeff_tmp(ispin) = mo_coeff
385 : END IF
386 3620 : CALL cp_dbcsr_sm_fm_multiply(cosmat, mo_coeff_tmp(ispin), opvec(1, ispin), ncol=nmo)
387 : CALL parallel_gemm("T", "N", nmo, nmo, nao, 1.0_dp, mo_coeff_tmp(ispin), opvec(1, ispin), 0.0_dp, &
388 3620 : op_fm_set(1, ispin))
389 3620 : CALL cp_dbcsr_sm_fm_multiply(sinmat, mo_coeff_tmp(ispin), opvec(2, ispin), ncol=nmo)
390 : CALL parallel_gemm("T", "N", nmo, nmo, nao, 1.0_dp, mo_coeff_tmp(ispin), opvec(2, ispin), 0.0_dp, &
391 7138 : op_fm_set(2, ispin))
392 : END DO
393 : !second step invert C^T S_berry C
394 3518 : zdet = z_one
395 7138 : DO ispin = 1, dft_control%nspins
396 3620 : CALL cp_cfm_scale_and_add_fm(z_zero, eigrmat(ispin), z_one, op_fm_set(1, ispin))
397 3620 : CALL cp_cfm_scale_and_add_fm(z_one, eigrmat(ispin), -gaussi, op_fm_set(2, ispin))
398 3620 : CALL cp_cfm_set_all(inv_mat(ispin), z_zero, z_one)
399 3620 : CALL cp_cfm_solve(eigrmat(ispin), inv_mat(ispin), zdeta)
400 7138 : zdet = zdet*zdeta
401 : END DO
402 3518 : zi(idir) = zdet**occ
403 3518 : ci(idir) = AIMAG(LOG(zdet**occ))
404 :
405 3518 : IF (.NOT. just_energy) THEN
406 : !compute the orbital derivative
407 3026 : focc = fpolvec(idir)
408 6112 : DO ispin = 1, dft_control%nspins
409 40790 : inv_work(1, ispin)%local_data(:, :) = REAL(inv_mat(ispin)%local_data(:, :), dp)
410 40790 : inv_work(2, ispin)%local_data(:, :) = AIMAG(inv_mat(ispin)%local_data(:, :))
411 3086 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, nmo=nmo)
412 : CALL parallel_gemm("N", "N", nao, nmo, nmo, focc, opvec(1, ispin), inv_work(2, ispin), &
413 3086 : 1.0_dp, mo_derivs_tmp(ispin))
414 : CALL parallel_gemm("N", "N", nao, nmo, nmo, -focc, opvec(2, ispin), inv_work(1, ispin), &
415 9198 : 1.0_dp, mo_derivs_tmp(ispin))
416 : END DO
417 : END IF
418 :
419 : !compute nuclear forces
420 3518 : IF (calculate_forces) THEN
421 138 : nkind = SIZE(qs_kind_set)
422 138 : natom = SIZE(particle_set)
423 552 : kvec(:) = twopi*cell%h_inv(idir, :)
424 :
425 : ! calculate: C [C^T S_berry C]^(-1) C^T
426 : ! Store this matrix in DBCSR form (only S overlap blocks)
427 284 : DO ispin = 1, dft_control%nspins
428 146 : CALL dbcsr_set(tempmat(1, ispin)%matrix, 0.0_dp)
429 146 : CALL dbcsr_set(tempmat(2, ispin)%matrix, 0.0_dp)
430 146 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, nmo=nmo)
431 : CALL parallel_gemm("N", "N", nao, nmo, nmo, 1.0_dp, mo_coeff_tmp(ispin), inv_work(1, ispin), 0.0_dp, &
432 146 : opvec(1, ispin))
433 : CALL parallel_gemm("N", "N", nao, nmo, nmo, 1.0_dp, mo_coeff_tmp(ispin), inv_work(2, ispin), 0.0_dp, &
434 146 : opvec(2, ispin))
435 : CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=tempmat(1, ispin)%matrix, &
436 146 : matrix_v=opvec(1, ispin), matrix_g=mo_coeff_tmp(ispin), ncol=nmo)
437 : CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=tempmat(2, ispin)%matrix, &
438 430 : matrix_v=opvec(2, ispin), matrix_g=mo_coeff_tmp(ispin), ncol=nmo)
439 : END DO
440 :
441 : ! Calculation of derivative integrals (da|eikr|b) and (a|eikr|db)
442 690 : ALLOCATE (basis_set_list(nkind))
443 414 : DO ikind = 1, nkind
444 276 : qs_kind => qs_kind_set(ikind)
445 276 : CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a)
446 414 : IF (ASSOCIATED(basis_set_a)) THEN
447 276 : basis_set_list(ikind)%gto_basis_set => basis_set_a
448 : ELSE
449 0 : NULLIFY (basis_set_list(ikind)%gto_basis_set)
450 : END IF
451 : END DO
452 : !
453 138 : CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
454 6646 : DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
455 : CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
456 6508 : iatom=iatom, jatom=jatom, r=rab)
457 6508 : basis_set_a => basis_set_list(ikind)%gto_basis_set
458 6508 : IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
459 6508 : basis_set_b => basis_set_list(jkind)%gto_basis_set
460 6508 : IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
461 : ! basis ikind
462 6508 : first_sgfa => basis_set_a%first_sgf
463 6508 : la_max => basis_set_a%lmax
464 6508 : la_min => basis_set_a%lmin
465 6508 : npgfa => basis_set_a%npgf
466 6508 : nseta = basis_set_a%nset
467 6508 : nsgfa => basis_set_a%nsgf_set
468 6508 : rpgfa => basis_set_a%pgf_radius
469 6508 : set_radius_a => basis_set_a%set_radius
470 6508 : sphi_a => basis_set_a%sphi
471 6508 : zeta => basis_set_a%zet
472 : ! basis jkind
473 6508 : first_sgfb => basis_set_b%first_sgf
474 6508 : lb_max => basis_set_b%lmax
475 6508 : lb_min => basis_set_b%lmin
476 6508 : npgfb => basis_set_b%npgf
477 6508 : nsetb = basis_set_b%nset
478 6508 : nsgfb => basis_set_b%nsgf_set
479 6508 : rpgfb => basis_set_b%pgf_radius
480 6508 : set_radius_b => basis_set_b%set_radius
481 6508 : sphi_b => basis_set_b%sphi
482 6508 : zetb => basis_set_b%zet
483 :
484 6508 : atom_a = atom_of_kind(iatom)
485 6508 : atom_b = atom_of_kind(jatom)
486 :
487 6508 : ldsa = SIZE(sphi_a, 1)
488 6508 : ldsb = SIZE(sphi_b, 1)
489 6508 : ra(:) = pbc(particle_set(iatom)%r(:), cell)
490 26032 : rb(:) = ra + rab
491 6508 : dab = SQRT(rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3))
492 :
493 6508 : IF (iatom <= jatom) THEN
494 4428 : irow = iatom
495 4428 : icol = jatom
496 : ELSE
497 2080 : irow = jatom
498 2080 : icol = iatom
499 : END IF
500 :
501 6508 : IF (iatom == jatom) THEN
502 : fab = 1.0_dp*occ
503 : ELSE
504 3058 : fab = 2.0_dp*occ
505 : END IF
506 :
507 26032 : DO i = 1, 3
508 5330052 : dcost(i, 1)%block = 0.0_dp
509 5330052 : dsint(i, 1)%block = 0.0_dp
510 5330052 : dcost(i, 2)%block = 0.0_dp
511 5336560 : dsint(i, 2)%block = 0.0_dp
512 : END DO
513 :
514 18474 : DO iset = 1, nseta
515 11966 : ncoa = npgfa(iset)*ncoset(la_max(iset))
516 11966 : sgfa = first_sgfa(1, iset)
517 41356 : DO jset = 1, nsetb
518 22882 : IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
519 11156 : ncob = npgfb(jset)*ncoset(lb_max(jset))
520 11156 : sgfb = first_sgfb(1, jset)
521 : ! Calculate the primitive integrals (da|b)
522 : CALL cossin(la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
523 : lb_max(jset), npgfb(jset), zetb(:, jset), rpgfb(:, jset), lb_min(jset), &
524 11156 : ra, rb, kvec, cosab, sinab, dcosab, dsinab)
525 44624 : DO i = 1, 3
526 : CALL contract_all(dcost(i, 1)%block, dsint(i, 1)%block, &
527 : ncoa, nsgfa(iset), sgfa, sphi_a, ldsa, &
528 : ncob, nsgfb(jset), sgfb, sphi_b, ldsb, &
529 44624 : dcosab(:, :, i), dsinab(:, :, i), ldab, work, ldab)
530 : END DO
531 : ! Calculate the primitive integrals (a|db)
532 : CALL cossin(lb_max(jset), npgfb(jset), zetb(:, jset), rpgfb(:, jset), lb_min(jset), &
533 : la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
534 11156 : rb, ra, kvec, cosab, sinab, dcosab, dsinab)
535 56590 : DO i = 1, 3
536 4942992 : dcosab(1:ncoa, 1:ncob, i) = TRANSPOSE(dcosab(1:ncob, 1:ncoa, i))
537 4942992 : dsinab(1:ncoa, 1:ncob, i) = TRANSPOSE(dsinab(1:ncob, 1:ncoa, i))
538 : CALL contract_all(dcost(i, 2)%block, dsint(i, 2)%block, &
539 : ncoa, nsgfa(iset), sgfa, sphi_a, ldsa, &
540 : ncob, nsgfb(jset), sgfb, sphi_b, ldsb, &
541 56350 : dcosab(:, :, i), dsinab(:, :, i), ldab, work, ldab)
542 : END DO
543 : END DO
544 : END DO
545 6508 : forcea = 0.0_dp
546 6508 : forceb = 0.0_dp
547 13417 : DO ispin = 1, dft_control%nspins
548 6909 : NULLIFY (rblock, iblock)
549 : CALL dbcsr_get_block_p(matrix=tempmat(1, ispin)%matrix, &
550 6909 : row=irow, col=icol, BLOCK=rblock, found=found)
551 6909 : CPASSERT(found)
552 : CALL dbcsr_get_block_p(matrix=tempmat(2, ispin)%matrix, &
553 6909 : row=irow, col=icol, BLOCK=iblock, found=found)
554 6909 : CPASSERT(found)
555 6909 : n1 = SIZE(rblock, 1)
556 6909 : n2 = SIZE(rblock, 2)
557 6909 : CPASSERT(SIZE(iblock, 1) == n1)
558 6909 : CPASSERT(SIZE(iblock, 2) == n2)
559 6909 : CPASSERT(lsab >= n1)
560 6909 : CPASSERT(lsab >= n2)
561 27235 : IF (iatom <= jatom) THEN
562 18852 : DO i = 1, 3
563 : forcea(i) = forcea(i) + SUM(rblock(1:n1, 1:n2)*dsint(i, 1)%block(1:n1, 1:n2)) &
564 1955559 : - SUM(iblock(1:n1, 1:n2)*dcost(i, 1)%block(1:n1, 1:n2))
565 : forceb(i) = forceb(i) + SUM(rblock(1:n1, 1:n2)*dsint(i, 2)%block(1:n1, 1:n2)) &
566 1960272 : - SUM(iblock(1:n1, 1:n2)*dcost(i, 2)%block(1:n1, 1:n2))
567 : END DO
568 : ELSE
569 8784 : DO i = 1, 3
570 : forcea(i) = forcea(i) + SUM(TRANSPOSE(rblock(1:n1, 1:n2))*dsint(i, 1)%block(1:n2, 1:n1)) &
571 604908 : - SUM(TRANSPOSE(iblock(1:n1, 1:n2))*dcost(i, 1)%block(1:n2, 1:n1))
572 : forceb(i) = forceb(i) + SUM(TRANSPOSE(rblock(1:n1, 1:n2))*dsint(i, 2)%block(1:n2, 1:n1)) &
573 607104 : - SUM(TRANSPOSE(iblock(1:n1, 1:n2))*dcost(i, 2)%block(1:n2, 1:n1))
574 : END DO
575 : END IF
576 : END DO
577 26032 : force(ikind)%efield(1:3, atom_a) = force(ikind)%efield(1:3, atom_a) - fab*fpolvec(idir)*forcea(1:3)
578 26032 : force(jkind)%efield(1:3, atom_b) = force(jkind)%efield(1:3, atom_b) - fab*fpolvec(idir)*forceb(1:3)
579 6646 : IF (use_virial) THEN
580 0 : f0 = -fab*fpolvec(idir)
581 0 : CALL virial_pair_force(virial%pv_virial, f0, forcea, ra)
582 0 : CALL virial_pair_force(virial%pv_virial, f0, forceb, rb)
583 : END IF
584 :
585 : END DO
586 138 : CALL neighbor_list_iterator_release(nl_iterator)
587 138 : DEALLOCATE (basis_set_list)
588 :
589 : END IF
590 : END IF
591 : END DO
592 :
593 : ! Energy
594 9712 : ener_field = 0.0_dp
595 : ti = 0.0_dp
596 9712 : DO idir = 1, 3
597 : ! make sure the total normalized polarization is within [-1:1]
598 7284 : cqi(idir) = qi(idir) + ci(idir)
599 7284 : IF (cqi(idir) > pi) cqi(idir) = cqi(idir) - twopi
600 7284 : IF (cqi(idir) < -pi) cqi(idir) = cqi(idir) + twopi
601 : ! now check for log branch
602 7284 : IF (ABS(efield%polarisation(idir) - cqi(idir)) > pi) THEN
603 0 : ti(idir) = (efield%polarisation(idir) - cqi(idir))/pi
604 0 : DO i = 1, 10
605 0 : cqi(idir) = cqi(idir) + SIGN(1.0_dp, ti(idir))*twopi
606 0 : IF (ABS(efield%polarisation(idir) - cqi(idir)) < pi) EXIT
607 : END DO
608 : END IF
609 9712 : ener_field = ener_field + fpolvec(idir)*cqi(idir)
610 : END DO
611 :
612 : ! update the references
613 2428 : IF (calculate_forces) THEN
614 : ! check for smoothness of energy surface
615 544 : IF (ABS(efield%field_energy - ener_field) > pi*ABS(SUM(fpolvec))) THEN
616 16 : CPWARN("Large change of e-field energy detected. Correct for non-smooth energy surface")
617 : END IF
618 136 : efield%field_energy = ener_field
619 544 : efield%polarisation(:) = cqi(:)
620 : END IF
621 2428 : energy%efield = ener_field
622 :
623 2428 : IF (.NOT. just_energy) THEN
624 : ! Add the result to mo_derivativs
625 3772 : DO ispin = 1, dft_control%nspins
626 3772 : CALL copy_fm_to_dbcsr(mo_derivs_tmp(ispin), mo_derivs(ispin)%matrix)
627 : END DO
628 1856 : IF (use_virial) THEN
629 0 : ti = 0.0_dp
630 0 : DO i = 1, 3
631 0 : DO j = 1, 3
632 0 : ti(j) = ti(j) + hmat(j, i)*cqi(i)
633 : END DO
634 : END DO
635 0 : DO i = 1, 3
636 0 : DO j = 1, 3
637 0 : virial%pv_virial(i, j) = virial%pv_virial(i, j) - fieldpol(i)*ti(j)
638 : END DO
639 : END DO
640 : END IF
641 : END IF
642 :
643 4958 : DO ispin = 1, dft_control%nspins
644 2530 : CALL cp_cfm_release(eigrmat(ispin))
645 2530 : CALL cp_cfm_release(inv_mat(ispin))
646 2530 : CALL cp_fm_release(mo_derivs_tmp(ispin))
647 2530 : IF (mos(ispin)%use_mo_coeff_b) CALL cp_fm_release(mo_coeff_tmp(ispin))
648 10018 : DO i = 1, SIZE(op_fm_set, 1)
649 5060 : CALL cp_fm_release(opvec(i, ispin))
650 5060 : CALL cp_fm_release(op_fm_set(i, ispin))
651 7590 : CALL cp_fm_release(inv_work(i, ispin))
652 : END DO
653 : END DO
654 2428 : DEALLOCATE (inv_mat, inv_work, op_fm_set, opvec, eigrmat)
655 2428 : DEALLOCATE (mo_coeff_tmp, mo_derivs_tmp)
656 :
657 2428 : IF (calculate_forces) THEN
658 408 : DO ikind = 1, SIZE(atomic_kind_set)
659 3880 : CALL para_env%sum(force(ikind)%efield)
660 : END DO
661 136 : DEALLOCATE (cosab, sinab, work, dcosab, dsinab)
662 544 : DO i = 1, 3
663 408 : DEALLOCATE (dcost(i, 1)%block, dsint(i, 1)%block)
664 544 : DEALLOCATE (dcost(i, 2)%block, dsint(i, 2)%block)
665 : END DO
666 136 : CALL dbcsr_deallocate_matrix_set(tempmat)
667 : END IF
668 2428 : CALL timestop(handle)
669 :
670 9712 : END SUBROUTINE qs_efield_derivatives
671 :
672 : ! **************************************************************************************************
673 : !> \brief ...
674 : !> \param qs_env ...
675 : !> \param just_energy ...
676 : !> \param calculate_forces ...
677 : ! **************************************************************************************************
678 898 : SUBROUTINE qs_dispfield_derivatives(qs_env, just_energy, calculate_forces)
679 : TYPE(qs_environment_type), POINTER :: qs_env
680 : LOGICAL, INTENT(IN) :: just_energy, calculate_forces
681 :
682 : CHARACTER(LEN=*), PARAMETER :: routineN = 'qs_dispfield_derivatives'
683 :
684 : COMPLEX(dp) :: zdet, zdeta, zi(3)
685 : INTEGER :: handle, i, ia, iatom, icol, idir, ikind, iodeb, irow, iset, ispin, jatom, jkind, &
686 : jset, ldab, ldsa, ldsb, lsab, n1, n2, nao, natom, ncoa, ncob, nkind, nmo, nseta, nsetb, &
687 : sgfa, sgfb
688 898 : INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind
689 898 : INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, npgfa, &
690 898 : npgfb, nsgfa, nsgfb
691 898 : INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, first_sgfb
692 : LOGICAL :: found, uniform, use_virial
693 : REAL(dp) :: charge, ci(3), cqi(3), dab, dd, di(3), ener_field, fab, fieldpol(3), focc, &
694 : hmat(3, 3), occ, omega, qi(3), rlog(3), strength, zlog(3)
695 : REAL(dp), DIMENSION(3) :: dfilter, forcea, forceb, kvec, ra, rab, &
696 : rb, ria
697 1796 : REAL(dp), DIMENSION(:, :), POINTER :: cosab, iblock, rblock, sinab, work
698 2694 : REAL(dp), DIMENSION(:, :, :), POINTER :: dcosab, dsinab, force_tmp
699 898 : REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
700 898 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: rpgfa, rpgfb, sphi_a, sphi_b, zeta, zetb
701 898 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
702 16164 : TYPE(block_p_type), DIMENSION(3, 2) :: dcost, dsint
703 : TYPE(cell_type), POINTER :: cell
704 898 : TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:) :: eigrmat, inv_mat
705 : TYPE(cp_fm_struct_type), POINTER :: tmp_fm_struct
706 898 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: mo_coeff_tmp
707 898 : TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :) :: inv_work, mo_derivs_tmp, op_fm_set, opvec
708 : TYPE(cp_fm_type), POINTER :: mo_coeff
709 898 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s, mo_derivs
710 898 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: tempmat
711 : TYPE(dbcsr_type), POINTER :: cosmat, mo_coeff_b, sinmat
712 : TYPE(dft_control_type), POINTER :: dft_control
713 : TYPE(efield_berry_type), POINTER :: efield
714 898 : TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
715 : TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
716 898 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
717 : TYPE(mp_para_env_type), POINTER :: para_env
718 : TYPE(neighbor_list_iterator_p_type), &
719 898 : DIMENSION(:), POINTER :: nl_iterator
720 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
721 898 : POINTER :: sab_orb
722 898 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
723 : TYPE(qs_energy_type), POINTER :: energy
724 898 : TYPE(qs_force_type), DIMENSION(:), POINTER :: force
725 898 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
726 : TYPE(qs_kind_type), POINTER :: qs_kind
727 : TYPE(virial_type), POINTER :: virial
728 :
729 898 : CALL timeset(routineN, handle)
730 :
731 898 : NULLIFY (dft_control, cell, particle_set)
732 : CALL get_qs_env(qs_env, dft_control=dft_control, cell=cell, &
733 898 : particle_set=particle_set, virial=virial)
734 898 : NULLIFY (qs_kind_set, efield, para_env, sab_orb)
735 : CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set, &
736 898 : efield=efield, energy=energy, para_env=para_env, sab_orb=sab_orb)
737 :
738 : ! calculate stress only if forces requested also
739 898 : use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
740 0 : use_virial = use_virial .AND. calculate_forces
741 : ! disable stress calculation
742 : IF (use_virial) THEN
743 0 : CPABORT("Stress tensor for periodic D-field not implemented")
744 : END IF
745 :
746 3592 : dfilter(1:3) = dft_control%period_efield%d_filter(1:3)
747 :
748 : ! if an intensities list is given, select the value for the current step
749 898 : strength = dft_control%period_efield%strength
750 898 : IF (ALLOCATED(dft_control%period_efield%strength_list)) THEN
751 : strength = dft_control%period_efield%strength_list(MOD(qs_env%sim_step &
752 0 : - dft_control%period_efield%start_frame, SIZE(dft_control%period_efield%strength_list)) + 1)
753 : END IF
754 :
755 3592 : fieldpol = dft_control%period_efield%polarisation
756 6286 : fieldpol = fieldpol/NORM2(fieldpol)
757 3592 : fieldpol = fieldpol*strength
758 :
759 898 : omega = cell%deth
760 11674 : hmat = cell%hmat(:, :)/(twopi*omega)
761 :
762 : ! nuclear contribution to polarization
763 898 : natom = SIZE(particle_set)
764 898 : IF (calculate_forces) THEN
765 10 : CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, force=force)
766 10 : CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind)
767 40 : ALLOCATE (force_tmp(natom, 3, 3))
768 310 : force_tmp = 0.0_dp
769 : END IF
770 3592 : zi(:) = CMPLX(1._dp, 0._dp, dp)
771 2694 : DO ia = 1, natom
772 1796 : CALL get_atomic_kind(particle_set(ia)%atomic_kind, kind_number=ikind)
773 1796 : CALL get_qs_kind(qs_kind_set(ikind), core_charge=charge)
774 7184 : ria = particle_set(ia)%r
775 7184 : ria = pbc(ria, cell)
776 7184 : DO idir = 1, 3
777 21552 : kvec(:) = twopi*cell%h_inv(idir, :)
778 21552 : dd = SUM(kvec(:)*ria(:))
779 5388 : zdeta = CMPLX(COS(dd), SIN(dd), KIND=dp)**charge
780 7184 : zi(idir) = zi(idir)*zdeta
781 : END DO
782 4490 : IF (calculate_forces) THEN
783 20 : IF (para_env%mepos == 0) THEN
784 40 : DO i = 1, 3
785 40 : force_tmp(ia, i, i) = force_tmp(ia, i, i) + charge/omega
786 : END DO
787 : END IF
788 : END IF
789 : END DO
790 3592 : rlog = AIMAG(LOG(zi))
791 :
792 : ! check uniform occupation
793 898 : NULLIFY (mos)
794 898 : CALL get_qs_env(qs_env=qs_env, mos=mos)
795 1796 : DO ispin = 1, dft_control%nspins
796 898 : CALL get_mo_set(mo_set=mos(ispin), maxocc=occ, uniform_occupation=uniform)
797 1796 : IF (.NOT. uniform) THEN
798 0 : CPABORT("Berry phase moments for non uniform MO occupation numbers not implemented")
799 : END IF
800 : END DO
801 :
802 : ! initialize all work matrices needed
803 898 : NULLIFY (mo_derivs)
804 898 : CALL get_qs_env(qs_env=qs_env, mo_derivs=mo_derivs)
805 5388 : ALLOCATE (op_fm_set(2, dft_control%nspins))
806 5388 : ALLOCATE (opvec(2, dft_control%nspins))
807 3592 : ALLOCATE (eigrmat(dft_control%nspins))
808 3592 : ALLOCATE (inv_mat(dft_control%nspins))
809 5388 : ALLOCATE (inv_work(2, dft_control%nspins))
810 6286 : ALLOCATE (mo_derivs_tmp(3, SIZE(mo_derivs)))
811 3592 : ALLOCATE (mo_coeff_tmp(SIZE(mo_derivs)))
812 :
813 : ! Allocate temp matrices for the wavefunction derivatives
814 1796 : DO ispin = 1, dft_control%nspins
815 898 : NULLIFY (tmp_fm_struct, mo_coeff)
816 898 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nao=nao, nmo=nmo)
817 : CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=nmo, &
818 898 : ncol_global=nmo, para_env=para_env, context=mo_coeff%matrix_struct%context)
819 898 : CALL cp_fm_create(mo_coeff_tmp(ispin), mo_coeff%matrix_struct)
820 3592 : DO i = 1, 3
821 2694 : CALL cp_fm_create(mo_derivs_tmp(i, ispin), mo_coeff%matrix_struct)
822 3592 : CALL cp_fm_set_all(matrix=mo_derivs_tmp(i, ispin), alpha=0.0_dp)
823 : END DO
824 2694 : DO i = 1, SIZE(op_fm_set, 1)
825 1796 : CALL cp_fm_create(opvec(i, ispin), mo_coeff%matrix_struct)
826 1796 : CALL cp_fm_create(op_fm_set(i, ispin), tmp_fm_struct)
827 2694 : CALL cp_fm_create(inv_work(i, ispin), op_fm_set(i, ispin)%matrix_struct)
828 : END DO
829 898 : CALL cp_cfm_create(eigrmat(ispin), op_fm_set(1, ispin)%matrix_struct)
830 898 : CALL cp_cfm_create(inv_mat(ispin), op_fm_set(1, ispin)%matrix_struct)
831 2694 : CALL cp_fm_struct_release(tmp_fm_struct)
832 : END DO
833 : ! temp matrices for force calculation
834 898 : IF (calculate_forces) THEN
835 10 : NULLIFY (matrix_s)
836 10 : CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s)
837 60 : ALLOCATE (tempmat(2, dft_control%nspins))
838 20 : DO ispin = 1, dft_control%nspins
839 10 : ALLOCATE (tempmat(1, ispin)%matrix, tempmat(2, ispin)%matrix)
840 10 : CALL dbcsr_copy(tempmat(1, ispin)%matrix, matrix_s(1)%matrix, 'TEMPMAT')
841 10 : CALL dbcsr_copy(tempmat(2, ispin)%matrix, matrix_s(1)%matrix, 'TEMPMAT')
842 10 : CALL dbcsr_set(tempmat(1, ispin)%matrix, 0.0_dp)
843 20 : CALL dbcsr_set(tempmat(2, ispin)%matrix, 0.0_dp)
844 : END DO
845 : ! integration
846 10 : CALL get_qs_kind_set(qs_kind_set, maxco=ldab, maxsgf=lsab)
847 80 : ALLOCATE (cosab(ldab, ldab), sinab(ldab, ldab), work(ldab, ldab))
848 70 : ALLOCATE (dcosab(ldab, ldab, 3), dsinab(ldab, ldab, 3))
849 10 : lsab = MAX(lsab, ldab)
850 50 : DO i = 1, 3
851 180 : ALLOCATE (dcost(i, 1)%block(lsab, lsab), dsint(i, 1)%block(lsab, lsab))
852 160 : ALLOCATE (dcost(i, 2)%block(lsab, lsab), dsint(i, 2)%block(lsab, lsab))
853 : END DO
854 : END IF
855 :
856 : !Start the MO derivative calculation
857 : !loop over all cell vectors
858 3592 : DO idir = 1, 3
859 2694 : zi(idir) = z_zero
860 2694 : cosmat => efield%cosmat(idir)%matrix
861 2694 : sinmat => efield%sinmat(idir)%matrix
862 : !evaluate the expression needed for the derivative (S_berry * C and [C^T S_berry C]^-1)
863 : !first step S_berry * C and C^T S_berry C
864 5388 : DO ispin = 1, dft_control%nspins ! spin
865 2694 : IF (mos(ispin)%use_mo_coeff_b) THEN
866 2694 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, mo_coeff_b=mo_coeff_b, nmo=nmo)
867 2694 : CALL copy_dbcsr_to_fm(mo_coeff_b, mo_coeff_tmp(ispin))
868 : ELSE
869 0 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, mo_coeff=mo_coeff, nmo=nmo)
870 0 : mo_coeff_tmp(ispin) = mo_coeff
871 : END IF
872 2694 : CALL cp_dbcsr_sm_fm_multiply(cosmat, mo_coeff_tmp(ispin), opvec(1, ispin), ncol=nmo)
873 : CALL parallel_gemm("T", "N", nmo, nmo, nao, 1.0_dp, mo_coeff_tmp(ispin), opvec(1, ispin), 0.0_dp, &
874 2694 : op_fm_set(1, ispin))
875 2694 : CALL cp_dbcsr_sm_fm_multiply(sinmat, mo_coeff_tmp(ispin), opvec(2, ispin), ncol=nmo)
876 : CALL parallel_gemm("T", "N", nmo, nmo, nao, 1.0_dp, mo_coeff_tmp(ispin), opvec(2, ispin), 0.0_dp, &
877 5388 : op_fm_set(2, ispin))
878 : END DO
879 : !second step invert C^T S_berry C
880 2694 : zdet = z_one
881 5388 : DO ispin = 1, dft_control%nspins
882 2694 : CALL cp_cfm_scale_and_add_fm(z_zero, eigrmat(ispin), z_one, op_fm_set(1, ispin))
883 2694 : CALL cp_cfm_scale_and_add_fm(z_one, eigrmat(ispin), -gaussi, op_fm_set(2, ispin))
884 2694 : CALL cp_cfm_set_all(inv_mat(ispin), z_zero, z_one)
885 2694 : CALL cp_cfm_solve(eigrmat(ispin), inv_mat(ispin), zdeta)
886 5388 : zdet = zdet*zdeta
887 : END DO
888 2694 : zi(idir) = zdet**occ
889 2694 : zlog(idir) = AIMAG(LOG(zi(idir)))
890 :
891 2694 : IF (.NOT. just_energy) THEN
892 : !compute the orbital derivative
893 5388 : DO ispin = 1, dft_control%nspins
894 35022 : inv_work(1, ispin)%local_data(:, :) = REAL(inv_mat(ispin)%local_data(:, :), dp)
895 35022 : inv_work(2, ispin)%local_data(:, :) = AIMAG(inv_mat(ispin)%local_data(:, :))
896 2694 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, nmo=nmo)
897 13470 : DO i = 1, 3
898 8082 : focc = hmat(idir, i)
899 : CALL parallel_gemm("N", "N", nao, nmo, nmo, focc, opvec(1, ispin), inv_work(2, ispin), &
900 8082 : 1.0_dp, mo_derivs_tmp(idir, ispin))
901 : CALL parallel_gemm("N", "N", nao, nmo, nmo, -focc, opvec(2, ispin), inv_work(1, ispin), &
902 10776 : 1.0_dp, mo_derivs_tmp(idir, ispin))
903 : END DO
904 : END DO
905 : END IF
906 :
907 : !compute nuclear forces
908 3592 : IF (calculate_forces) THEN
909 30 : nkind = SIZE(qs_kind_set)
910 30 : natom = SIZE(particle_set)
911 120 : kvec(:) = twopi*cell%h_inv(idir, :)
912 :
913 : ! calculate: C [C^T S_berry C]^(-1) C^T
914 : ! Store this matrix in DBCSR form (only S overlap blocks)
915 60 : DO ispin = 1, dft_control%nspins
916 30 : CALL dbcsr_set(tempmat(1, ispin)%matrix, 0.0_dp)
917 30 : CALL dbcsr_set(tempmat(2, ispin)%matrix, 0.0_dp)
918 30 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, nmo=nmo)
919 : CALL parallel_gemm("N", "N", nao, nmo, nmo, 1.0_dp, mo_coeff_tmp(ispin), inv_work(1, ispin), 0.0_dp, &
920 30 : opvec(1, ispin))
921 : CALL parallel_gemm("N", "N", nao, nmo, nmo, 1.0_dp, mo_coeff_tmp(ispin), inv_work(2, ispin), 0.0_dp, &
922 30 : opvec(2, ispin))
923 : CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=tempmat(1, ispin)%matrix, &
924 30 : matrix_v=opvec(1, ispin), matrix_g=mo_coeff_tmp(ispin), ncol=nmo)
925 : CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=tempmat(2, ispin)%matrix, &
926 90 : matrix_v=opvec(2, ispin), matrix_g=mo_coeff_tmp(ispin), ncol=nmo)
927 : END DO
928 :
929 : ! Calculation of derivative integrals (da|eikr|b) and (a|eikr|db)
930 150 : ALLOCATE (basis_set_list(nkind))
931 90 : DO ikind = 1, nkind
932 60 : qs_kind => qs_kind_set(ikind)
933 60 : CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a)
934 90 : IF (ASSOCIATED(basis_set_a)) THEN
935 60 : basis_set_list(ikind)%gto_basis_set => basis_set_a
936 : ELSE
937 0 : NULLIFY (basis_set_list(ikind)%gto_basis_set)
938 : END IF
939 : END DO
940 : !
941 30 : CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
942 585 : DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
943 : CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
944 555 : iatom=iatom, jatom=jatom, r=rab)
945 555 : basis_set_a => basis_set_list(ikind)%gto_basis_set
946 555 : IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
947 555 : basis_set_b => basis_set_list(jkind)%gto_basis_set
948 555 : IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
949 : ! basis ikind
950 555 : first_sgfa => basis_set_a%first_sgf
951 555 : la_max => basis_set_a%lmax
952 555 : la_min => basis_set_a%lmin
953 555 : npgfa => basis_set_a%npgf
954 555 : nseta = basis_set_a%nset
955 555 : nsgfa => basis_set_a%nsgf_set
956 555 : rpgfa => basis_set_a%pgf_radius
957 555 : set_radius_a => basis_set_a%set_radius
958 555 : sphi_a => basis_set_a%sphi
959 555 : zeta => basis_set_a%zet
960 : ! basis jkind
961 555 : first_sgfb => basis_set_b%first_sgf
962 555 : lb_max => basis_set_b%lmax
963 555 : lb_min => basis_set_b%lmin
964 555 : npgfb => basis_set_b%npgf
965 555 : nsetb = basis_set_b%nset
966 555 : nsgfb => basis_set_b%nsgf_set
967 555 : rpgfb => basis_set_b%pgf_radius
968 555 : set_radius_b => basis_set_b%set_radius
969 555 : sphi_b => basis_set_b%sphi
970 555 : zetb => basis_set_b%zet
971 :
972 555 : ldsa = SIZE(sphi_a, 1)
973 555 : ldsb = SIZE(sphi_b, 1)
974 555 : ra(:) = pbc(particle_set(iatom)%r(:), cell)
975 2220 : rb(:) = ra + rab
976 555 : dab = SQRT(rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3))
977 :
978 555 : IF (iatom <= jatom) THEN
979 354 : irow = iatom
980 354 : icol = jatom
981 : ELSE
982 201 : irow = jatom
983 201 : icol = iatom
984 : END IF
985 :
986 555 : IF (iatom == jatom) THEN
987 : fab = 1.0_dp*occ
988 : ELSE
989 201 : fab = 2.0_dp*occ
990 : END IF
991 :
992 2220 : DO i = 1, 3
993 454545 : dcost(i, 1)%block = 0.0_dp
994 454545 : dsint(i, 1)%block = 0.0_dp
995 454545 : dcost(i, 2)%block = 0.0_dp
996 455100 : dsint(i, 2)%block = 0.0_dp
997 : END DO
998 :
999 1665 : DO iset = 1, nseta
1000 1110 : ncoa = npgfa(iset)*ncoset(la_max(iset))
1001 1110 : sgfa = first_sgfa(1, iset)
1002 3885 : DO jset = 1, nsetb
1003 2220 : IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
1004 1140 : ncob = npgfb(jset)*ncoset(lb_max(jset))
1005 1140 : sgfb = first_sgfb(1, jset)
1006 : ! Calculate the primitive integrals (da|b)
1007 : CALL cossin(la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
1008 : lb_max(jset), npgfb(jset), zetb(:, jset), rpgfb(:, jset), lb_min(jset), &
1009 1140 : ra, rb, kvec, cosab, sinab, dcosab, dsinab)
1010 4560 : DO i = 1, 3
1011 : CALL contract_all(dcost(i, 1)%block, dsint(i, 1)%block, &
1012 : ncoa, nsgfa(iset), sgfa, sphi_a, ldsa, &
1013 : ncob, nsgfb(jset), sgfb, sphi_b, ldsb, &
1014 4560 : dcosab(:, :, i), dsinab(:, :, i), ldab, work, ldab)
1015 : END DO
1016 : ! Calculate the primitive integrals (a|db)
1017 : CALL cossin(lb_max(jset), npgfb(jset), zetb(:, jset), rpgfb(:, jset), lb_min(jset), &
1018 : la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), la_min(iset), &
1019 1140 : rb, ra, kvec, cosab, sinab, dcosab, dsinab)
1020 5670 : DO i = 1, 3
1021 560556 : dcosab(1:ncoa, 1:ncob, i) = TRANSPOSE(dcosab(1:ncob, 1:ncoa, i))
1022 560556 : dsinab(1:ncoa, 1:ncob, i) = TRANSPOSE(dsinab(1:ncob, 1:ncoa, i))
1023 : CALL contract_all(dcost(i, 2)%block, dsint(i, 2)%block, &
1024 : ncoa, nsgfa(iset), sgfa, sphi_a, ldsa, &
1025 : ncob, nsgfb(jset), sgfb, sphi_b, ldsb, &
1026 5640 : dcosab(:, :, i), dsinab(:, :, i), ldab, work, ldab)
1027 : END DO
1028 : END DO
1029 : END DO
1030 555 : forcea = 0.0_dp
1031 555 : forceb = 0.0_dp
1032 1110 : DO ispin = 1, dft_control%nspins
1033 555 : NULLIFY (rblock, iblock)
1034 : CALL dbcsr_get_block_p(matrix=tempmat(1, ispin)%matrix, &
1035 555 : row=irow, col=icol, BLOCK=rblock, found=found)
1036 555 : CPASSERT(found)
1037 : CALL dbcsr_get_block_p(matrix=tempmat(2, ispin)%matrix, &
1038 555 : row=irow, col=icol, BLOCK=iblock, found=found)
1039 555 : CPASSERT(found)
1040 555 : n1 = SIZE(rblock, 1)
1041 555 : n2 = SIZE(rblock, 2)
1042 555 : CPASSERT(SIZE(iblock, 1) == n1)
1043 555 : CPASSERT(SIZE(iblock, 2) == n2)
1044 555 : CPASSERT(lsab >= n1)
1045 555 : CPASSERT(lsab >= n2)
1046 2220 : IF (iatom <= jatom) THEN
1047 1416 : DO i = 1, 3
1048 : forcea(i) = forcea(i) + SUM(rblock(1:n1, 1:n2)*dsint(i, 1)%block(1:n1, 1:n2)) &
1049 160542 : - SUM(iblock(1:n1, 1:n2)*dcost(i, 1)%block(1:n1, 1:n2))
1050 : forceb(i) = forceb(i) + SUM(rblock(1:n1, 1:n2)*dsint(i, 2)%block(1:n1, 1:n2)) &
1051 160896 : - SUM(iblock(1:n1, 1:n2)*dcost(i, 2)%block(1:n1, 1:n2))
1052 : END DO
1053 : ELSE
1054 804 : DO i = 1, 3
1055 : forcea(i) = forcea(i) + SUM(TRANSPOSE(rblock(1:n1, 1:n2))*dsint(i, 1)%block(1:n2, 1:n1)) &
1056 85023 : - SUM(TRANSPOSE(iblock(1:n1, 1:n2))*dcost(i, 1)%block(1:n2, 1:n1))
1057 : forceb(i) = forceb(i) + SUM(TRANSPOSE(rblock(1:n1, 1:n2))*dsint(i, 2)%block(1:n2, 1:n1)) &
1058 85224 : - SUM(TRANSPOSE(iblock(1:n1, 1:n2))*dcost(i, 2)%block(1:n2, 1:n1))
1059 : END DO
1060 : END IF
1061 : END DO
1062 2250 : DO i = 1, 3
1063 6660 : force_tmp(iatom, :, i) = force_tmp(iatom, :, i) - fab*hmat(i, idir)*forcea(:)
1064 7215 : force_tmp(jatom, :, i) = force_tmp(jatom, :, i) - fab*hmat(i, idir)*forceb(:)
1065 : END DO
1066 : END DO
1067 30 : CALL neighbor_list_iterator_release(nl_iterator)
1068 30 : DEALLOCATE (basis_set_list)
1069 : END IF
1070 : END DO
1071 :
1072 : ! make sure the total normalized polarization is within [-1:1]
1073 3592 : DO idir = 1, 3
1074 2694 : cqi(idir) = rlog(idir) + zlog(idir)
1075 2694 : IF (cqi(idir) > pi) cqi(idir) = cqi(idir) - twopi
1076 2694 : IF (cqi(idir) < -pi) cqi(idir) = cqi(idir) + twopi
1077 : ! now check for log branch
1078 3592 : IF (calculate_forces) THEN
1079 30 : IF (ABS(efield%polarisation(idir) - cqi(idir)) > pi) THEN
1080 0 : di(idir) = (efield%polarisation(idir) - cqi(idir))/pi
1081 0 : DO i = 1, 10
1082 0 : cqi(idir) = cqi(idir) + SIGN(1.0_dp, di(idir))*twopi
1083 0 : IF (ABS(efield%polarisation(idir) - cqi(idir)) < pi) EXIT
1084 : END DO
1085 : END IF
1086 : END IF
1087 : END DO
1088 3592 : DO idir = 1, 3
1089 2694 : qi(idir) = 0.0_dp
1090 2694 : ci(idir) = 0.0_dp
1091 11674 : DO i = 1, 3
1092 10776 : ci(idir) = ci(idir) + hmat(idir, i)*cqi(i)
1093 : END DO
1094 : END DO
1095 :
1096 : ! update the references
1097 898 : IF (calculate_forces) THEN
1098 40 : ener_field = SUM(ci)
1099 : ! check for smoothness of energy surface
1100 130 : IF (ABS(efield%field_energy - ener_field) > pi*ABS(SUM(hmat))) THEN
1101 0 : CPWARN("Large change of e-field energy detected. Correct for non-smooth energy surface")
1102 : END IF
1103 10 : efield%field_energy = ener_field
1104 40 : efield%polarisation(:) = cqi(:)
1105 : END IF
1106 :
1107 : ! Energy
1108 898 : ener_field = 0.0_dp
1109 3592 : DO i = 1, 3
1110 3592 : ener_field = ener_field + dfilter(i)*(fieldpol(i) - 2._dp*twopi*ci(i))**2
1111 : END DO
1112 898 : energy%efield = 0.25_dp*omega/twopi*ener_field
1113 :
1114 : ! debugging output
1115 : IF (para_env%is_source()) THEN
1116 898 : iodeb = -1
1117 : IF (iodeb > 0) THEN
1118 : WRITE (iodeb, '(A,T61,F20.10)') " Polarisation Quantum: ", 2._dp*twopi*twopi*hmat(3, 3)
1119 : WRITE (iodeb, '(A,T21,3F20.10)') " Polarisation: ", 2._dp*twopi*ci(1:3)
1120 : WRITE (iodeb, '(A,T21,3F20.10)') " Displacement: ", fieldpol(1:3)
1121 : WRITE (iodeb, '(A,T21,3F20.10)') " E-Field: ", ((fieldpol(i) - 2._dp*twopi*ci(i)), i=1, 3)
1122 : WRITE (iodeb, '(A,T61,F20.10)') " Disp Free Energy:", energy%efield
1123 : END IF
1124 : END IF
1125 :
1126 898 : IF (.NOT. just_energy) THEN
1127 3592 : DO i = 1, 3
1128 3592 : di(i) = -omega*(fieldpol(i) - 2._dp*twopi*ci(i))*dfilter(i)
1129 : END DO
1130 : ! Add the result to mo_derivativs
1131 1796 : DO ispin = 1, dft_control%nspins
1132 898 : CALL copy_dbcsr_to_fm(mo_derivs(ispin)%matrix, mo_coeff_tmp(ispin))
1133 4490 : DO idir = 1, 3
1134 : CALL cp_fm_scale_and_add(1.0_dp, mo_coeff_tmp(ispin), di(idir), &
1135 3592 : mo_derivs_tmp(idir, ispin))
1136 : END DO
1137 : END DO
1138 1796 : DO ispin = 1, dft_control%nspins
1139 1796 : CALL copy_fm_to_dbcsr(mo_coeff_tmp(ispin), mo_derivs(ispin)%matrix)
1140 : END DO
1141 : END IF
1142 :
1143 898 : IF (calculate_forces) THEN
1144 40 : DO i = 1, 3
1145 100 : DO ia = 1, natom
1146 60 : CALL get_atomic_kind(particle_set(ia)%atomic_kind, kind_number=ikind)
1147 60 : iatom = atom_of_kind(ia)
1148 450 : force(ikind)%efield(1:3, iatom) = force(ikind)%efield(1:3, iatom) + di(i)*force_tmp(ia, 1:3, i)
1149 : END DO
1150 : END DO
1151 : END IF
1152 :
1153 1796 : DO ispin = 1, dft_control%nspins
1154 898 : CALL cp_cfm_release(eigrmat(ispin))
1155 898 : CALL cp_cfm_release(inv_mat(ispin))
1156 898 : IF (mos(ispin)%use_mo_coeff_b) CALL cp_fm_release(mo_coeff_tmp(ispin))
1157 3592 : DO i = 1, 3
1158 3592 : CALL cp_fm_release(mo_derivs_tmp(i, ispin))
1159 : END DO
1160 3592 : DO i = 1, SIZE(op_fm_set, 1)
1161 1796 : CALL cp_fm_release(opvec(i, ispin))
1162 1796 : CALL cp_fm_release(op_fm_set(i, ispin))
1163 2694 : CALL cp_fm_release(inv_work(i, ispin))
1164 : END DO
1165 : END DO
1166 898 : DEALLOCATE (inv_mat, inv_work, op_fm_set, opvec, eigrmat)
1167 898 : DEALLOCATE (mo_coeff_tmp, mo_derivs_tmp)
1168 :
1169 898 : IF (calculate_forces) THEN
1170 30 : DO ikind = 1, SIZE(atomic_kind_set)
1171 190 : CALL para_env%sum(force(ikind)%efield)
1172 : END DO
1173 10 : DEALLOCATE (force_tmp)
1174 10 : DEALLOCATE (cosab, sinab, work, dcosab, dsinab)
1175 40 : DO i = 1, 3
1176 30 : DEALLOCATE (dcost(i, 1)%block, dsint(i, 1)%block)
1177 40 : DEALLOCATE (dcost(i, 2)%block, dsint(i, 2)%block)
1178 : END DO
1179 10 : CALL dbcsr_deallocate_matrix_set(tempmat)
1180 : END IF
1181 898 : CALL timestop(handle)
1182 :
1183 3592 : END SUBROUTINE qs_dispfield_derivatives
1184 :
1185 : ! **************************************************************************************************
1186 : !> \brief ...
1187 : !> \param cos_block ...
1188 : !> \param sin_block ...
1189 : !> \param ncoa ...
1190 : !> \param nsgfa ...
1191 : !> \param sgfa ...
1192 : !> \param sphi_a ...
1193 : !> \param ldsa ...
1194 : !> \param ncob ...
1195 : !> \param nsgfb ...
1196 : !> \param sgfb ...
1197 : !> \param sphi_b ...
1198 : !> \param ldsb ...
1199 : !> \param cosab ...
1200 : !> \param sinab ...
1201 : !> \param ldab ...
1202 : !> \param work ...
1203 : !> \param ldwork ...
1204 : ! **************************************************************************************************
1205 73776 : SUBROUTINE contract_all(cos_block, sin_block, &
1206 147552 : ncoa, nsgfa, sgfa, sphi_a, ldsa, &
1207 147552 : ncob, nsgfb, sgfb, sphi_b, ldsb, &
1208 73776 : cosab, sinab, ldab, work, ldwork)
1209 :
1210 : REAL(dp), DIMENSION(:, :), POINTER :: cos_block, sin_block
1211 : INTEGER, INTENT(IN) :: ncoa, nsgfa, sgfa
1212 : REAL(dp), DIMENSION(:, :), INTENT(IN) :: sphi_a
1213 : INTEGER, INTENT(IN) :: ldsa, ncob, nsgfb, sgfb
1214 : REAL(dp), DIMENSION(:, :), INTENT(IN) :: sphi_b
1215 : INTEGER, INTENT(IN) :: ldsb
1216 : REAL(dp), DIMENSION(:, :), INTENT(IN) :: cosab, sinab
1217 : INTEGER, INTENT(IN) :: ldab
1218 : REAL(dp), DIMENSION(:, :) :: work
1219 : INTEGER, INTENT(IN) :: ldwork
1220 :
1221 : ! Calculate cosine
1222 :
1223 : CALL dgemm("N", "N", ncoa, nsgfb, ncob, 1.0_dp, cosab(1, 1), ldab, &
1224 73776 : sphi_b(1, sgfb), ldsb, 0.0_dp, work(1, 1), ldwork)
1225 :
1226 : CALL dgemm("T", "N", nsgfa, nsgfb, ncoa, 1.0_dp, sphi_a(1, sgfa), ldsa, &
1227 73776 : work(1, 1), ldwork, 1.0_dp, cos_block(sgfa, sgfb), SIZE(cos_block, 1))
1228 :
1229 : ! Calculate sine
1230 : CALL dgemm("N", "N", ncoa, nsgfb, ncob, 1.0_dp, sinab(1, 1), ldab, &
1231 73776 : sphi_b(1, sgfb), ldsb, 0.0_dp, work(1, 1), ldwork)
1232 :
1233 : CALL dgemm("T", "N", nsgfa, nsgfb, ncoa, 1.0_dp, sphi_a(1, sgfa), ldsa, &
1234 73776 : work(1, 1), ldwork, 1.0_dp, sin_block(sgfa, sgfb), SIZE(sin_block, 1))
1235 :
1236 73776 : END SUBROUTINE contract_all
1237 :
1238 : END MODULE qs_efield_berry
|