Octopus
forces.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
21module forces_oct_m
22 use accel_oct_m
23 use batch_oct_m
27 use comm_oct_m
28 use debug_oct_m
33 use epot_oct_m
36 use global_oct_m
37 use grid_oct_m
40 use io_oct_m
41 use ions_oct_m
42 use, intrinsic :: iso_fortran_env
46 use lasers_oct_m
47 use lda_u_oct_m
50 use math_oct_m
51 use mesh_oct_m
54 use mpi_oct_m
57 use parser_oct_m
58 use phase_oct_m
61 use ps_oct_m
63 use space_oct_m
71 use unit_oct_m
73 use utils_oct_m
74 use v_ks_oct_m
75 use vdw_ts_oct_m
77
78 implicit none
79
80 private
81 public :: &
94
95contains
96
97 ! ---------------------------------------------------------
100 subroutine total_force_calculate(space, namespace, gr, ions, hm, st, x)
101 class(space_t), intent(in) :: space
102 class(namespace_t), intent(in) :: namespace
103 type(grid_t), intent(in) :: gr
104 type(ions_t), intent(in) :: ions
105 type(hamiltonian_elec_t), intent(in) :: hm
106 type(states_elec_t), intent(in) :: st
107 real(real64), contiguous, intent(inout) :: x(:)
108
109 push_sub(total_force_calculate)
110 call profiling_in("TOTAL_FORCE")
111
112 x = m_zero
113 if (states_are_real(st)) then
114 call dtotal_force_from_potential(space, namespace, gr, ions, hm, st, x)
115 else
116 call ztotal_force_from_potential(space, namespace, gr, ions, hm, st, x)
117 end if
118
119 call profiling_out("TOTAL_FORCE")
120 pop_sub(total_force_calculate)
121 end subroutine total_force_calculate
122
123 ! -------------------------------------------------------
124
125 subroutine forces_costate_calculate(gr, namespace, ions, hm, psi, chi, ff, qq)
126 type(grid_t), intent(in) :: gr
127 type(namespace_t), intent(in) :: namespace
128 type(ions_t), intent(inout) :: ions
129 type(hamiltonian_elec_t), intent(in) :: hm
130 type(states_elec_t), intent(in) :: psi
131 type(states_elec_t), intent(in) :: chi
132 real(real64), contiguous, intent(inout) :: ff(:, :)
133 real(real64), intent(in) :: qq(:, :)
134
135 integer :: jatom, idim, jdim
136 integer, target :: j, ist, ik, iatom
137 real(real64) :: rr, w2r, w1r, xx(ions%space%dim), dq, pdot3p, pdot3m, pdot3p2, pdot3m2, dforce1, dforce2
138 complex(real64), allocatable :: zpsi(:, :), derpsi(:, :, :)
139 real(real64), allocatable :: forceks1p(:), forceks1m(:), forceks1p2(:), forceks1m2(:), dforceks1(:)
140
141 call profiling_in("FORCES_COSTATE")
143
144 ! FIXME: is the next section not basically the same as the routine ion_internamespace, action_calculate?
145
146 ff = m_zero
147 do iatom = 1, ions%natoms
148 do jatom = 1, ions%natoms
149 if (jatom == iatom) cycle
150 xx(1:ions%space%dim) = ions%pos(:, jatom) - ions%pos(:, iatom)
151 rr = norm2(xx(1:ions%space%dim))
152 w1r = - ions%charge(iatom) * ions%charge(jatom) / rr**2
153 w2r = m_two * ions%charge(iatom) * ions%charge(jatom) / rr**3
154 do idim = 1, ions%space%dim
155 do jdim = 1, ions%space%dim
156 ff(iatom, idim) = ff(iatom, idim) + (qq(jatom, jdim) - qq(iatom, jdim)) * w2r * (m_one/rr**2) * xx(idim) * xx(jdim)
157 ff(iatom, idim) = ff(iatom, idim) - (qq(jatom, jdim) - qq(iatom, jdim)) * w1r * (m_one/rr**3) * xx(idim) * xx(jdim)
158 if (jdim == idim) then
159 ff(iatom, idim) = ff(iatom, idim) + (qq(jatom, jdim) - qq(iatom, jdim)) * w1r * (m_one/rr)
160 end if
161 end do
162 end do
163 end do
164 end do
165
166 safe_allocate(derpsi(1:gr%np_part, 1:ions%space%dim, 1:psi%d%dim))
167
168 dq = 0.001_real64
169
170 safe_allocate(forceks1p(1:ions%space%dim))
171 safe_allocate(forceks1m(1:ions%space%dim))
172 safe_allocate(forceks1p2(1:ions%space%dim))
173 safe_allocate(forceks1m2(1:ions%space%dim))
174 safe_allocate(dforceks1(1:ions%space%dim))
175 safe_allocate(zpsi(1:gr%np_part, 1:psi%d%dim))
176
177 do ist = 1, psi%nst
178 do ik = 1, psi%nik
179 derpsi = m_z0
180 call states_elec_get_state(psi, gr, ist, ik, zpsi)
181 call zderivatives_grad(gr%der, zpsi(:, 1), derpsi(:, :, 1))
182 do iatom = 1, ions%natoms
183 do j = 1, ions%space%dim
184 call force1(ions%pos(j, iatom) + dq, forceks1p, pdot3p)
185 call force1(ions%pos(j, iatom) - dq, forceks1m, pdot3m)
186 call force1(ions%pos(j, iatom) + dq/m_two, forceks1p2, pdot3p2)
187 call force1(ions%pos(j, iatom) - dq/m_two, forceks1m2, pdot3m2)
188 dforceks1 = ((m_four/m_three) * (forceks1p2 - forceks1m2) - (m_one / 6.0_real64) * (forceks1p - forceks1m)) / dq
189 dforce1 = sum(qq(iatom, :) * dforceks1(:))
190 dforce2 = ((m_four/m_three) * (pdot3p2 - pdot3m2) - (m_one / 6.0_real64) * (pdot3p - pdot3m)) / dq
191 ff(iatom, j) = ff(iatom, j) - m_two * psi%occ(ist, ik) * dforce1 + m_two * dforce2
192 end do
193 end do
194 end do
195 end do
196
197 safe_deallocate_a(zpsi)
198 safe_deallocate_a(forceks1p)
199 safe_deallocate_a(forceks1m)
200 safe_deallocate_a(forceks1p2)
201 safe_deallocate_a(forceks1m2)
202 safe_deallocate_a(dforceks1)
203 safe_deallocate_a(derpsi)
204
206 call profiling_out("FORCES_COSTATE")
207
208 contains
209
210 subroutine force1(qq, res, pdot3)
211 real(real64), intent(in) :: qq
212 real(real64), contiguous, intent(inout) :: res(:)
213 real(real64), intent(inout) :: pdot3
214
215 integer :: m
216 real(real64) :: qold
217 complex(real64), allocatable :: viapsi(:, :), zpsi(:, :)
218
219 qold = ions%pos(j, iatom)
220 ions%pos(j, iatom) = qq
221 safe_allocate(viapsi(1:gr%np_part, 1:psi%d%dim))
222 safe_allocate(zpsi(1:gr%np_part, 1:psi%d%dim))
223 viapsi = m_z0
224 call states_elec_get_state(psi, gr, ist, ik, zpsi)
225 call zhamiltonian_elec_apply_atom(hm, namespace, ions%space, ions%latt, ions%atom(iatom)%species, &
226 ions%pos(:, iatom), iatom, gr, zpsi, viapsi)
227
228 res(:) = m_zero
229 do m = 1, ubound(res, 1)
230 res(m) = real( zmf_dotp(gr, viapsi(:, 1), derpsi(:, m, 1), reduce = .false.), real64)
231 end do
232 call gr%allreduce(res)
233
234 call states_elec_get_state(chi, gr, ist, ik, zpsi)
235 pdot3 = real(m_zi * zmf_dotp(gr, zpsi(:, 1), viapsi(:, 1)), real64)
236 ions%pos(j, iatom) = qold
237
238 safe_deallocate_a(viapsi)
239 end subroutine force1
240
241 end subroutine forces_costate_calculate
242 ! ---------------------------------------------------------
243
244
245 ! ---------------------------------------------------------
246 subroutine forces_calculate(gr, namespace, ions, hm, ext_partners, st, ks, vhxc_old, t, dt)
247 type(grid_t), intent(in) :: gr
248 type(namespace_t), intent(in) :: namespace
249 type(ions_t), intent(inout) :: ions
250 type(hamiltonian_elec_t), intent(inout) :: hm
251 type(partner_list_t),intent(in) :: ext_partners
252 type(states_elec_t), intent(inout) :: st
253 type(v_ks_t), intent(in) :: ks
254 real(real64), contiguous, optional, intent(in) :: vhxc_old(:,:)
255 real(real64), optional, intent(in) :: t
256 real(real64), optional, intent(in) :: dt
257
258 integer :: j, iatom, idir
259 real(real64) :: xx(ions%space%dim), time, global_force(ions%space%dim)
260 real(real64), allocatable :: force(:, :), force_loc(:, :), force_nl(:, :), force_u(:, :)
261 real(real64), allocatable :: force_nlcc(: ,:)
262 real(real64), allocatable :: force_scf(:, :)
263 type(lasers_t), pointer :: lasers
264
265 call profiling_in("FORCES")
266 push_sub(forces_calculate)
267
268 ! The spin-coupled overlap charges and complex projectors of fully-relativistic USPP are
269 ! not handled by the ultrasoft force terms.
270 if (hm%ep%requires_generalized_overlap .and. hm%ep%reltype == spin_orbit) then
271 call messages_not_implemented("Forces with spin-orbit coupling and ultrasoft/PAW", namespace=namespace)
272 end if
273 ! The ultrasoft force terms have not been validated with the PAW one-center contributions.
274 if (hm%ep%has_paw) then
275 call messages_not_implemented("Forces with PAW", namespace=namespace)
276 end if
277
278 time = m_zero
279 if (present(t)) time = t
280
281 !We initialize the different components of the force to zero
282 do iatom = 1, ions%natoms
283 ions%atom(iatom)%f_ii(1:ions%space%dim) = m_zero
284 ions%atom(iatom)%f_vdw(1:ions%space%dim) = m_zero
285 ions%atom(iatom)%f_loc(1:ions%space%dim) = m_zero
286 ions%atom(iatom)%f_nl(1:ions%space%dim) = m_zero
287 ions%atom(iatom)%f_u(1:ions%space%dim) = m_zero
288 ions%atom(iatom)%f_fields(1:ions%space%dim) = m_zero
289 ions%atom(iatom)%f_nlcc(1:ions%space%dim) = m_zero
290 ions%atom(iatom)%f_scf(1:ions%space%dim) = m_zero
291 ions%atom(iatom)%f_photons(1:ions%space%dim) = m_zero
292 end do
293
294 ! the ion-ion and vdw terms are already calculated
295 ! if we use vdw TS, we need to compute it now
296 if (ks%vdw%vdw_correction == option__vdwcorrection__vdw_ts) then
297 call vdw_ts_force_calculate(ks%vdw%vdw_ts, hm%ep%vdw_forces, ions, gr, st%d%spin_channels, st%rho)
298 end if
299
300 do iatom = 1, ions%natoms
301 ions%tot_force(:, iatom) = hm%ep%fii(1:ions%space%dim, iatom) + hm%ep%vdw_forces(1:ions%space%dim, iatom)
302 if (ks%v_ks_photons%active()) then
303 ions%tot_force(:, iatom) = ions%tot_force(:, iatom) &
304 - p_proton_charge*ions%charge(iatom)*hm%ep%photon_forces(1:ions%space%dim)
305 end if
306 ions%atom(iatom)%f_ii(1:ions%space%dim) = hm%ep%fii(1:ions%space%dim, iatom)
307 ions%atom(iatom)%f_vdw(1:ions%space%dim) = hm%ep%vdw_forces(1:ions%space%dim, iatom)
308 ions%atom(iatom)%f_photons(1:ions%space%dim) = - p_proton_charge*ions%charge(iatom)*hm%ep%photon_forces(1:ions%space%dim)
309 end do
310
311 if (present(t)) then
312 global_force = ions%global_force(time)
313
314 ! the ion-ion term is already calculated
315 do iatom = 1, ions%natoms
316 ions%tot_force(:, iatom) = ions%tot_force(:, iatom) + global_force
317 ions%atom(iatom)%f_ii(1:ions%space%dim) = ions%atom(iatom)%f_ii(1:ions%space%dim) + global_force
318 end do
319 end if
320
321 safe_allocate(force(1:ions%space%dim, 1:ions%natoms))
322 safe_allocate(force_loc(1:ions%space%dim, 1:ions%natoms))
323 safe_allocate(force_nl(1:ions%space%dim, 1:ions%natoms))
324 safe_allocate(force_u(1:ions%space%dim, 1:ions%natoms))
325 safe_allocate(force_scf(1:ions%space%dim, 1:ions%natoms))
326 safe_allocate(force_nlcc(1:ions%space%dim, 1:ions%natoms))
327
328 if (states_are_real(st)) then
329 call dforces_from_potential(gr, namespace, ions%space, ions, hm, st, force, force_loc, force_nl, force_u)
330 else
331 call zforces_from_potential(gr, namespace, ions%space, ions, hm, st, force, force_loc, force_nl, force_u)
332 end if
333
334 if (allocated(st%rho_core) .and. ks%theory_level /= independent_particles &
335 .and. ks%theory_level /= hartree .and. ks%theory_level /= rdmft) then
336 call forces_from_nlcc(gr, ions, st%d%spin_channels, hm%ks_pot%vxc, force_nlcc, hm%ep%dgrid)
337 else
338 force_nlcc(:, :) = m_zero
339 end if
340 if (present(vhxc_old)) then
341 call forces_from_scf(gr, ions, st%d%spin_channels, hm%ks_pot%vhxc, vhxc_old, force_scf)
342 else
343 force_scf = m_zero
344 end if
345
346 if (ions%force_total_enforce) then
347 call forces_set_total_to_zero(ions, force)
348 call forces_set_total_to_zero(ions, force_loc)
349 call forces_set_total_to_zero(ions, force_nl)
350 call forces_set_total_to_zero(ions, force_u)
351 call forces_set_total_to_zero(ions, force_scf)
352 call forces_set_total_to_zero(ions, force_nlcc)
353 end if
354
355 do iatom = 1, ions%natoms
356 do idir = 1, ions%space%dim
357 ions%tot_force(idir, iatom) = ions%tot_force(idir, iatom) + force(idir, iatom) &
358 + force_scf(idir, iatom) + force_nlcc(idir, iatom)
359 ions%atom(iatom)%f_loc(idir) = force_loc(idir, iatom)
360 ions%atom(iatom)%f_nl(idir) = force_nl(idir, iatom)
361 ions%atom(iatom)%f_u(idir) = force_u(idir, iatom)
362 ions%atom(iatom)%f_nlcc(idir) = force_nlcc(idir, iatom)
363 ions%atom(iatom)%f_scf(idir) = force_scf(idir, iatom)
364 end do
365 end do
366
367 safe_deallocate_a(force)
368 safe_deallocate_a(force_loc)
369 safe_deallocate_a(force_nl)
370 safe_deallocate_a(force_u)
371 safe_deallocate_a(force_nlcc)
372 safe_deallocate_a(force_scf)
373
374 !\todo forces due to the magnetic fields (static and time-dependent)
375 lasers => list_get_lasers(ext_partners)
376 if (present(t) .and. associated(lasers)) then
377 do j = 1, lasers%no_lasers
378 select case (laser_kind(lasers%lasers(j)))
379 case (e_field_electric)
380 xx(:) = m_zero
381 call laser_field(lasers%lasers(j), xx, t)
382 do iatom = 1, ions%natoms
383 ! Here the proton charge is +1, since the electric field has the usual sign.
384 ions%tot_force(:, iatom) = ions%tot_force(:, iatom) + ions%charge(iatom)*xx(:)
385 ions%atom(iatom)%f_fields(1:ions%space%dim) = ions%atom(iatom)%f_fields(1:ions%space%dim) &
386 + ions%charge(iatom)*xx(:)
387 end do
388
390 ! Forces are correctly calculated only if the time-dependent
391 ! vector potential has no spatial dependence.
392 ! The full force taking account of the spatial dependence of A should be:
393 ! F = q [- dA/dt + v x \nabla x A]
394
395 !TODO: Add the gauge-field here
396 xx(:) = m_zero
397 call laser_electric_field(lasers%lasers(j), xx(:), t, dt) !convert in E field (E = -dA/ c dt)
398 do iatom = 1, ions%natoms
399 ! Also here the proton charge is +1
400 ions%tot_force(:, iatom) = ions%tot_force(:, iatom) + ions%charge(iatom)*xx(:)
401 ions%atom(iatom)%f_fields(1:ions%space%dim) = ions%atom(iatom)%f_fields(1:ions%space%dim) &
402 + ions%charge(iatom)*xx(:)
403 end do
404
405 if (lasers_with_nondipole_field(lasers)) then
406 write(message(1),'(a)') 'The forces are currently not supported for nondipole '
407 write(message(2),'(a)') 'strong-field approximation Hamiltonian approach.'
408 call messages_fatal(2, namespace=namespace)
409 end if
410
412 write(message(1),'(a)') 'The forces are currently not properly calculated if time-dependent'
413 write(message(2),'(a)') 'magnetic fields are present.'
414 call messages_fatal(2, namespace=namespace)
415 end select
416 end do
417 end if
418
419 if (allocated(hm%ep%e_field)) then
420 do iatom = 1, ions%natoms
421 ! Here the proton charge is +1, since the electric field has the usual sign.
422 ions%tot_force(:, iatom) = ions%tot_force(:, iatom) + ions%charge(iatom)*hm%ep%e_field(1:ions%space%dim)
423 ions%atom(iatom)%f_fields(1:ions%space%dim) = ions%atom(iatom)%f_fields(1:ions%space%dim) &
424 + ions%charge(iatom)*hm%ep%e_field(1:ions%space%dim)
425 end do
426 end if
427
428 if (allocated(hm%ep%b_field) .or. allocated(hm%ep%a_static)) then
429 write(message(1),'(a)') 'The forces are currently not properly calculated if static'
430 write(message(2),'(a)') 'magnetic fields or static vector potentials are present.'
431 call messages_fatal(2, namespace=namespace)
432 end if
433
434 if (list_has_gauge_field(ext_partners)) then
435 write(message(1),'(a)') 'The forces are currently not properly calculated if gauge-field'
436 write(message(2),'(a)') 'is applied.'
437 call messages_fatal(2, namespace=namespace)
438 end if
439
440 ! As the constrain is attached to the position of the atom, it contribute to the forces
441 if (hm%magnetic_constrain%level /= constrain_none) then
442 call messages_not_implemented("Forces with MagneticConstrain /= constrain_none")
443 end if
444
445 if (hm%kpoints%use_symmetries .or. st%symmetrize_density) then
446 call symmetrize_force(ions, ions%tot_force)
447 end if
448
449 ! For periodic systems, we substract the averaged force
450 if (ions%space%is_periodic()) then
451 call forces_set_total_to_zero(ions, ions%tot_force)
452 end if
453
454 pop_sub(forces_calculate)
455 call profiling_out("FORCES")
456
457 end subroutine forces_calculate
458
459 ! ----------------------------------------------------------------------
460
461 subroutine forces_set_total_to_zero(ions, force)
462 type(ions_t), intent(in) :: ions
463 real(real64), contiguous, intent(inout) :: force(:, :)
464
465 real(real64), allocatable :: total_force(:)
466 integer :: iatom
467
469
470 safe_allocate(total_force(1:ions%space%dim))
471
472 total_force(1:ions%space%dim) = sum(force, dim=2) / ions%natoms
473
474 do iatom = 1, ions%natoms
475 force(1:ions%space%dim, iatom) = force(1:ions%space%dim, iatom) - total_force(1:ions%space%dim)
476 end do
477
478 safe_deallocate_a(total_force)
480 end subroutine forces_set_total_to_zero
481
482 ! ----------------------------------------------------------------------
484 subroutine forces_compute_total_torque(ions, total_torque)
485 type(ions_t), intent(in) :: ions
486 real(real64), contiguous, intent(inout) :: total_torque(:)
487
488 real(real64) :: center_of_mass(ions%space%dim), rr(3), ff(3)
489 integer :: iatom
490
492
493 center_of_mass = ions%center_of_mass()
494
495 total_torque = m_zero
496 rr = m_zero
497 ff = m_zero
498 do iatom = 1, ions%natoms
499 rr(1:ions%space%dim) = ions%pos(1:ions%space%dim, iatom) - center_of_mass
500 ff(1:ions%space%dim) = ions%tot_force(1:ions%space%dim, iatom)
501 total_torque(1:3) = total_torque(1:3) + dcross_product(rr, ff)
502 end do
503
505 end subroutine forces_compute_total_torque
506
507
508 ! ----------------------------------------------------------------------
509
510 subroutine forces_write_info(iunit, ions, dir, namespace)
511 integer, intent(in) :: iunit
512 type(ions_t), intent(in) :: ions
513 character(len=*), intent(in) :: dir
514 type(namespace_t), intent(in) :: namespace
515
516 integer :: iatom, idir, ii, iunit2
517 real(real64) :: torque(1:3)
518
519 if (.not. ions%grp%is_root()) return
520
521 push_sub(forces_write_info)
522
523 write(iunit,'(3a)') 'Forces on the ions [', trim(units_abbrev(units_out%force)), "]"
524 write(iunit,'(a,10x,99(14x,a))') ' Ion', (index2axis(idir), idir = 1, ions%space%dim)
525 do iatom = 1, ions%natoms
526 write(iunit,'(i4,a10,10es17.8)') iatom, trim(ions%atom(iatom)%species%get_label()), &
527 units_from_atomic(units_out%force, ions%tot_force(:, iatom))
528 end do
529 write(iunit,'(1x,100a1)') ("-", ii = 1, 13 + ions%space%dim * 15)
530 write(iunit,'(a14, 10es17.8)') " Max abs force", &
531 (units_from_atomic(units_out%force, maxval(abs(ions%tot_force(idir, 1:ions%natoms)))), idir=1, ions%space%dim)
532 write(iunit,'(a14, 10es17.8)') " Total force", &
533 (units_from_atomic(units_out%force, sum(ions%tot_force(idir, 1:ions%natoms))), idir=1, ions%space%dim)
534
535 if ((ions%space%dim == 2 .or. ions%space%dim == 3) .and. .not. ions%space%is_periodic()) then
536 call forces_compute_total_torque(ions, torque)
537 write(iunit,'(a14, 10es17.8)') ' Total torque', &
538 (units_from_atomic(units_out%force*units_out%length, torque(idir)), idir = 1, 3)
539 end if
540 write(iunit,'(1x)')
541
542
543 iunit2 = io_open(trim(dir)//'/forces', namespace, action='write', position='asis')
544 write(iunit2,'(a)') &
545 ' # Total force (x,y,z) Ion-Ion (x,y,z) VdW (x,y,z) Local (x,y,z) NL (x,y,z)' // &
546 ' Fields (x,y,z) Hubbard(x,y,z) SCF(x,y,z) NLCC(x,y,z) Phot (x,y,z)'
547 do iatom = 1, ions%natoms
548 write(iunit2,'(i4,a10,30es17.8)') iatom, trim(ions%atom(iatom)%species%get_label()), &
549 (units_from_atomic(units_out%force, ions%tot_force(idir, iatom)), idir=1, ions%space%dim), &
550 (units_from_atomic(units_out%force, ions%atom(iatom)%f_ii(idir)), idir=1, ions%space%dim), &
551 (units_from_atomic(units_out%force, ions%atom(iatom)%f_vdw(idir)), idir=1, ions%space%dim), &
552 (units_from_atomic(units_out%force, ions%atom(iatom)%f_loc(idir)), idir=1, ions%space%dim), &
553 (units_from_atomic(units_out%force, ions%atom(iatom)%f_nl(idir)), idir=1, ions%space%dim), &
554 (units_from_atomic(units_out%force, ions%atom(iatom)%f_fields(idir)), idir=1, ions%space%dim), &
555 (units_from_atomic(units_out%force, ions%atom(iatom)%f_u(idir)), idir=1, ions%space%dim), &
556 (units_from_atomic(units_out%force, ions%atom(iatom)%f_scf(idir)), idir=1, ions%space%dim), &
557 (units_from_atomic(units_out%force, ions%atom(iatom)%f_nlcc(idir)), idir=1, ions%space%dim), &
558 (units_from_atomic(units_out%force, ions%atom(iatom)%f_photons(idir)), idir=1, ions%space%dim)
559 end do
560 call io_close(iunit2)
561
562 pop_sub(forces_write_info)
563
564 end subroutine forces_write_info
565
566 ! ----------------------------------------------------------------------
567 ! This routine add the contribution to the forces from the nonlinear core correction
568 ! see Eq. 9 of Kronik et al., J. Chem. Phys. 115, 4322 (2001)
569 !
570 ! An integration by part is used to avoid the gradient of the NLCC, which is not defined at the ionic center
571 subroutine forces_from_nlcc(mesh, ions, spin_channels, vxc, force_nlcc, dgrid)
572 type(grid_t), intent(in) :: mesh
573 type(ions_t), intent(inout) :: ions
574 integer, intent(in) :: spin_channels
575 real(real64), contiguous, intent(in) :: vxc(:,:)
576 real(real64), contiguous, intent(out) :: force_nlcc(:, :)
577 type(double_grid_t), intent(in) :: dgrid
578
579 integer :: is, iatom, idir
580 real(real64), allocatable :: pot(:), grad_vxc(:, :)
581
583
584 call profiling_in("FORCES_NLCC")
585
586 safe_allocate(pot(1:mesh%np_part))
587 safe_allocate(grad_vxc(1:mesh%np, 1:ions%space%dim))
588
589 call lalg_copy(mesh%np, vxc(:, 1), pot)
590 do is = 2, spin_channels
591 call lalg_axpy(mesh%np, m_one, vxc(:, is), pot)
592 end do
593 call lalg_scal(mesh%np, m_one / spin_channels, pot)
594 call dderivatives_grad(mesh%der, pot, grad_vxc)
595
596 force_nlcc = m_zero
597
598 do iatom = ions%atoms_dist%start, ions%atoms_dist%end
599 call species_get_nlcc(ions%atom(iatom)%species, ions%space, ions%latt, ions%pos(:, iatom), mesh, pot, &
600 dgrid=dgrid)
601
602 do idir = 1, ions%space%dim
603 force_nlcc(idir, iatom) = force_nlcc(idir, iatom) &
604 - dmf_dotp(mesh, pot(:), grad_vxc(:, idir), reduce = .false.)
605 end do
606 end do
607
608 safe_deallocate_a(pot)
609 safe_deallocate_a(grad_vxc)
610
611 if (ions%atoms_dist%parallel) call dforces_gather(ions, force_nlcc)
612
613 call profiling_in("FORCES_COMM")
614 call mesh%allreduce(force_nlcc)
615 call profiling_out("FORCES_COMM")
616
617 call profiling_out("FORCES_NLCC")
618
619 pop_sub(forces_from_nlcc)
620 end subroutine forces_from_nlcc
621
622 ! Implementation of the term from Chan et al., Phys. Rev. B 47, 4771 (1993).
623 ! Here we make the approximation that the "atomic densities" are just the one
624 ! from the pseudopotential.
625 ! NTD : No idea if this is good or bad, but this is easy to implement
626 ! and works well in practice
627 subroutine forces_from_scf(mesh, ions, spin_channels, vhxc, vhxc_old, force_scf)
628 class(mesh_t), intent(in) :: mesh
629 type(ions_t), intent(inout) :: ions
630 integer, intent(in) :: spin_channels
631 real(real64), contiguous, intent(in) :: vhxc(:,:)
632 real(real64), contiguous, intent(in) :: vhxc_old(:,:)
633 real(real64), contiguous, intent(out) :: force_scf(:, :)
634
635 integer :: is, iatom, idir, ip
636 real(real64), allocatable :: dvhxc(:,:), drho(:,:,:)
637
638 push_sub(forces_from_scf)
639
640 call profiling_in("FORCES_SCF")
641
642 safe_allocate(dvhxc(1:mesh%np, 1:spin_channels))
643 safe_allocate(drho(1:mesh%np, 1:spin_channels, 1:ions%space%dim))
644
645 !We average over spin channels
646 !$omp parallel private(is, ip)
647 do is = 1, spin_channels
648 !$omp do
649 do ip = 1, mesh%np
650 dvhxc(ip, is) = vhxc(ip, is) - vhxc_old(ip, is)
651 end do
652 end do
653 !$omp end parallel
654
655 force_scf = m_zero
656
657 do iatom = ions%atoms_dist%start, ions%atoms_dist%end
658 select type(spec=>ions%atom(iatom)%species)
659 type is(pseudopotential_t)
660
661 if (ps_has_density(spec%ps)) then
662
663 call species_atom_density_grad(spec, ions%namespace, ions%space, ions%latt, &
664 ions%pos(:, iatom), mesh, spin_channels, drho)
665
666 do idir = 1, ions%space%dim
667 do is = 1, spin_channels
668 force_scf(idir, iatom) = force_scf(idir, iatom) &
669 - dmf_dotp(mesh, drho(:,is,idir), dvhxc(:,is), reduce = .false.)
670 end do
671 end do
672 end if
673 end select
674 end do
675
676 safe_deallocate_a(dvhxc)
677 safe_deallocate_a(drho)
678
679 if (ions%atoms_dist%parallel) call dforces_gather(ions, force_scf)
680
681 call profiling_in("FORCES_COMM")
682 call mesh%allreduce(force_scf)
683 call profiling_out("FORCES_COMM")
684
685 call profiling_out("FORCES_SCF")
686
687 pop_sub(forces_from_scf)
688 end subroutine forces_from_scf
689
690 !---------------------------------------------------------------------------
691 subroutine total_force_from_local_potential(mesh, space, vpsl, gdensity, force)
692 class(mesh_t), intent(in) :: mesh
693 class(space_t), intent(in) :: space
694 real(real64), contiguous, intent(in) :: vpsl(:)
695 real(real64), contiguous, intent(in) :: gdensity(:, :)
696 real(real64), contiguous, intent(inout) :: force(:)
697
698 integer :: idir
699 real(real64) :: force_tmp(1:space%dim)
700
701 push_sub_with_profile(total_force_from_local_potential)
702
703 do idir = 1, space%dim
704 force_tmp(idir) = dmf_dotp(mesh, vpsl(1:mesh%np), gdensity(:, idir), reduce = .false.)
705 end do
706
707 call mesh%allreduce(force_tmp)
708
709 force(1:space%dim) = force(1:space%dim) + force_tmp(1:space%dim)
710
711 pop_sub_with_profile(total_force_from_local_potential)
713
714 !---------------------------------------------------------------------------
716 subroutine symmetrize_force(ions, force)
717 type(ions_t), intent(in) :: ions
718 real(real64), contiguous, intent(inout) :: force(:,:)
719
720 integer :: iatom, iop, iatom_symm
721 real(real64) :: symmetrized_force(ions%space%dim), force_tmp(ions%space%dim)
722 real(real64), allocatable :: force_sym(:,:)
723
724 push_sub(symmetrize_force)
725
726 safe_allocate(force_sym(1:ions%space%dim,1:ions%natoms))
727 do iatom = 1, ions%natoms
728 symmetrized_force = m_zero
729 do iop = 1, ions%symm%nops
730 iatom_symm = ions%inv_map_symm_atoms(iatom, iop)
731 force_tmp = symm_op_apply_inv_cart(ions%symm%ops(iop), force(:, iatom_symm))
732 symmetrized_force = symmetrized_force + force_tmp
733 end do
734
735 do iop = 1, ions%symm%nops_nonsymmorphic
736 iatom_symm = ions%inv_map_symm_atoms(iatom, iop + ions%symm%nops)
737 force_tmp = symm_op_apply_inv_cart(ions%symm%non_symmorphic_ops(iop), force(:, iatom_symm), rotation_only=.true.)
738 symmetrized_force = symmetrized_force + force_tmp
739 end do
740
741 force_sym(:, iatom) = symmetrized_force / (ions%symm%nops + ions%symm%nops_nonsymmorphic)
742 end do
743 force = force_sym
744
745 safe_deallocate_a(force_sym)
746
747 pop_sub(symmetrize_force)
748 end subroutine symmetrize_force
749
750 !---------------------------------------------------------------------------
755 subroutine symmetrize_born_charges(ions, charge)
756 type(ions_t), intent(in) :: ions
757 complex(real64), contiguous, intent(inout) :: charge(:, :, :)
758
759 integer :: iatom, iop, iatom_symm, dim
760 real(real64) :: rot(ions%space%dim, ions%space%dim)
761 complex(real64), allocatable :: charge_sym(:, :, :)
762
764
765 dim = ions%space%dim
766 safe_allocate(charge_sym(1:dim, 1:dim, 1:ions%natoms))
767 charge_sym = m_z0
768 do iatom = 1, ions%natoms
769 do iop = 1, ions%symm%nops
770 iatom_symm = ions%inv_map_symm_atoms(iatom, iop)
771 rot = symm_op_rotation_matrix_cart(ions%symm%ops(iop))
772 charge_sym(:, :, iatom) = charge_sym(:, :, iatom) + &
773 matmul(transpose(rot), matmul(charge(:, :, iatom_symm), rot))
774 end do
775
776 ! translations drop out for tensors, so only the rotation part is needed
777 do iop = 1, ions%symm%nops_nonsymmorphic
778 iatom_symm = ions%inv_map_symm_atoms(iatom, iop + ions%symm%nops)
779 rot = symm_op_rotation_matrix_cart(ions%symm%non_symmorphic_ops(iop))
780 charge_sym(:, :, iatom) = charge_sym(:, :, iatom) + &
781 matmul(transpose(rot), matmul(charge(:, :, iatom_symm), rot))
782 end do
783 end do
784 charge = charge_sym / (ions%symm%nops + ions%symm%nops_nonsymmorphic)
785
786 safe_deallocate_a(charge_sym)
787
789 end subroutine symmetrize_born_charges
790
791
792#include "undef.F90"
793#include "real.F90"
794#include "forces_inc.F90"
795
796#include "undef.F90"
797#include "complex.F90"
798#include "forces_inc.F90"
799
800end module forces_oct_m
801
802!! Local Variables:
803!! mode: f90
804!! coding: utf-8
805!! End:
subroutine force1(qq, res, pdot3)
Definition: forces.F90:283
constant times a vector plus a vector
Definition: lalg_basic.F90:171
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:197
scales a vector by a constant
Definition: lalg_basic.F90:147
This module implements batches of mesh functions.
Definition: batch.F90:112
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:95
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:101
This module implements a calculator for the density and defines related functions.
Definition: density.F90:99
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public dderivatives_grad(der, ff, op_ff, ghost_update, set_bc, to_cartesian)
apply the gradient to a mesh function
subroutine, public zderivatives_grad(der, ff, op_ff, ghost_update, set_bc, to_cartesian)
apply the gradient to a mesh function
The Ono-Hirose double-grid technique [T. Ono and K. Hirose, Phys. Rev. Lett. 82, 5016 (1999); Phys....
integer, parameter, public spin_orbit
Definition: epot.F90:150
logical function, public list_has_gauge_field(partners)
type(lasers_t) function, pointer, public list_get_lasers(partners)
subroutine, public zforces_from_potential(gr, namespace, space, ions, hm, st, force, force_loc, force_nl, force_u)
Ref: Kikuji Hirose, Tomoya Ono, Yoshitaka Fujimoto, and Shigeru Tsukamoto, First-principles calculati...
Definition: forces.F90:1804
subroutine, public dforces_derivative(gr, namespace, space, ions, ep, st, kpoints, lr, lr2, force_deriv, lda_u_level, vxc_response)
Computes the derivative of the ionic forces with respect to the strength of an external perturbation...
Definition: forces.F90:1400
subroutine, public forces_costate_calculate(gr, namespace, ions, hm, psi, chi, ff, qq)
Definition: forces.F90:198
subroutine, public zforces_derivative(gr, namespace, space, ions, ep, st, kpoints, lr, lr2, force_deriv, lda_u_level, vxc_response)
Computes the derivative of the ionic forces with respect to the strength of an external perturbation...
Definition: forces.F90:2172
subroutine, public total_force_calculate(space, namespace, gr, ions, hm, st, x)
This computes the total forces on the ions created by the electrons (it excludes the force due to pos...
Definition: forces.F90:173
subroutine forces_from_scf(mesh, ions, spin_channels, vhxc, vhxc_old, force_scf)
Definition: forces.F90:700
subroutine ztotal_force_from_potential(space, namespace, gr, ions, hm, st, x)
Definition: forces.F90:2082
subroutine forces_from_nlcc(mesh, ions, spin_channels, vxc, force_nlcc, dgrid)
Definition: forces.F90:644
subroutine symmetrize_force(ions, force)
Given the forces on all atoms, this symmetrizes them using symmorphic and non-symmorphic operations.
Definition: forces.F90:789
subroutine symmetrize_born_charges(ions, charge)
Symmetrizes the Born charge tensors using symmorphic and non-symmorphic operations.
Definition: forces.F90:828
subroutine forces_compute_total_torque(ions, total_torque)
Computes the total torque acting on the system.
Definition: forces.F90:557
subroutine, public zforces_born_charges(gr, namespace, space, ions, ep, st, kpoints, lr, lr2, born_charges, lda_u_level, vxc_response)
lr, lr2 are wfns from electric perturbation; lr is for +omega, lr2 is for -omega. for each atom,...
Definition: forces.F90:2365
subroutine, public forces_write_info(iunit, ions, dir, namespace)
Definition: forces.F90:583
subroutine, public forces_set_total_to_zero(ions, force)
Definition: forces.F90:534
subroutine, public dforces_from_potential(gr, namespace, space, ions, hm, st, force, force_loc, force_nl, force_u)
Ref: Kikuji Hirose, Tomoya Ono, Yoshitaka Fujimoto, and Shigeru Tsukamoto, First-principles calculati...
Definition: forces.F90:1032
subroutine, public forces_calculate(gr, namespace, ions, hm, ext_partners, st, ks, vhxc_old, t, dt)
Definition: forces.F90:319
subroutine, public dforces_born_charges(gr, namespace, space, ions, ep, st, kpoints, lr, lr2, born_charges, lda_u_level, vxc_response)
lr, lr2 are wfns from electric perturbation; lr is for +omega, lr2 is for -omega. for each atom,...
Definition: forces.F90:1586
subroutine dtotal_force_from_potential(space, namespace, gr, ions, hm, st, x)
Definition: forces.F90:1310
subroutine total_force_from_local_potential(mesh, space, vpsl, gdensity, force)
Definition: forces.F90:764
subroutine dforces_gather(ions, force)
Definition: forces.F90:932
real(real64), parameter, public m_two
Definition: global.F90:175
real(real64), parameter, public p_proton_charge
Definition: global.F90:218
real(real64), parameter, public m_zero
Definition: global.F90:173
real(real64), parameter, public m_four
Definition: global.F90:177
integer, parameter, public rdmft
Definition: global.F90:223
integer, parameter, public independent_particles
Theory level.
Definition: global.F90:223
complex(real64), parameter, public m_z0
Definition: global.F90:183
complex(real64), parameter, public m_zi
Definition: global.F90:187
real(real64), parameter, public m_one
Definition: global.F90:174
real(real64), parameter, public m_three
Definition: global.F90:176
integer, parameter, public hartree
Definition: global.F90:223
This module implements the underlying real-space grid.
Definition: grid.F90:96
subroutine, public zhamiltonian_elec_apply_atom(hm, namespace, space, latt, species, pos, ia, mesh, psi, vpsi)
This module defines classes and functions for interaction partners.
Definition: io.F90:93
subroutine, public io_close(iunit, grp)
Definition: io.F90:444
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:379
A module to handle KS potential, without the external potential.
logical function, public lasers_with_nondipole_field(lasers)
Check if a nondipole SFA correction should be computed for the given laser.
Definition: lasers.F90:724
integer, parameter, public e_field_electric
Definition: lasers.F90:158
integer, parameter, public e_field_vector_potential
Definition: lasers.F90:158
subroutine, public laser_electric_field(laser, field, time, dt)
Returns a vector with the electric field, no matter whether the laser is described directly as an ele...
Definition: lasers.F90:1178
integer, parameter, public e_field_scalar_potential
Definition: lasers.F90:158
integer pure elemental function, public laser_kind(laser)
Definition: lasers.F90:700
subroutine, public laser_field(laser, field, time)
Retrieves the value of either the electric or the magnetic field. If the laser is given by a scalar p...
Definition: lasers.F90:1102
integer, parameter, public e_field_magnetic
Definition: lasers.F90:158
This modules implements the routines for doing constrain DFT for noncollinear magnetism.
integer, parameter, public constrain_none
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
pure real(real64) function, dimension(1:3), public dcross_product(a, b)
Definition: math.F90:2053
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1048
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604
Definition: ps.F90:93
pure logical function, public ps_has_density(ps)
Definition: ps.F90:1809
subroutine, public species_get_nlcc(species, space, latt, pos, mesh, rho_core, accumulate, dgrid)
subroutine, public species_atom_density_grad(species, namespace, space, latt, pos, mesh, spin_channels, drho)
pure logical function, public states_are_real(st)
This module handles spin dimensions of the states and the k-point distribution.
real(real64) function, dimension(1:this%dim, 1:this%dim), public symm_op_rotation_matrix_cart(this)
Definition: symm_op.F90:292
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:111
character(len=20) pure function, public units_abbrev(this)
Definition: unit.F90:202
This module defines the unit system, used for input and output.
type(unit_system_t), public units_out
This module is intended to contain simple general-purpose utility functions and procedures.
Definition: utils.F90:99
character pure function, public index2axis(idir)
Definition: utils.F90:191
Tkatchenko-Scheffler pairwise method for van der Waals (vdW, dispersion) interactions.
Definition: vdw_ts.F90:98
subroutine, public vdw_ts_force_calculate(this, force_vdw, ions, mesh, nspin, density)
Definition: vdw_ts.F90:405
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:148
Describes mesh distribution to nodes.
Definition: mesh.F90:165
The states_elec_t class contains all electronic wave functions.
int true(void)