The Elk Code
force.f90
Go to the documentation of this file.
1 
2 ! Copyright (C) 2002-2005 J. K. Dewhurst, S. Sharma and C. Ambrosch-Draxl.
3 ! This file is distributed under the terms of the GNU General Public License.
4 ! See the file COPYING for license details.
5 
6 !BOP
7 ! !ROUTINE: force
8 ! !INTERFACE:
9 subroutine force
10 ! !USES:
11 use modmain
12 use modtddft
13 use modtest
14 use modmpi
15 use modomp
16 ! !DESCRIPTION:
17 ! Computes the various contributions to the atomic forces. In principle, the
18 ! force acting on a nucleus is simply the gradient at that site of the
19 ! classical electrostatic potential from the other nuclei and the electronic
20 ! density. This is a result of the Hellmann-Feynman theorem. However because
21 ! the basis set is dependent on the nuclear coordinates and is not complete,
22 ! the Hellman-Feynman force is inaccurate and corrections to it are required.
23 ! The first is the core correction which arises because the core wavefunctions
24 ! were determined by neglecting the non-spherical parts of the Kohn-Sham
25 ! potential $v_s$. Explicitly this is given by
26 ! $$ {\bf F}_{\rm core}^{\alpha}=\int_{\rm MT_{\alpha}} v_s({\bf r})
27 ! \nabla\rho_{\rm core}^{\alpha}({\bf r})\,d{\bf r} $$
28 ! for atom $\alpha$. The second, which is the incomplete basis set (IBS)
29 ! correction, is due to the position dependence of the APW functions, and is
30 ! derived by considering the change in total energy if the eigenvector
31 ! coefficients were fixed and the APW functions themselves were changed. This
32 ! would result in changes to the first-variational Hamiltonian and overlap
33 ! matrices given by
34 ! \begin{align*}
35 ! \delta H_{\bf G,G'}^{\alpha}&=i({\bf G-G'})
36 ! \left(H^{\alpha}_{\bf G+k,G'+k}-\frac{1}{2}({\bf G+k})\cdot({\bf G'+k})
37 ! \tilde{\Theta}_{\alpha}({\bf G-G'})e^{-i({\bf G-G'})\cdot{\bf r}_{\alpha}}
38 ! \right)\\
39 ! \delta O_{\bf G,G'}^{\alpha}&=i({\bf G-G'})\left(O^{\alpha}_{\bf G+k,G'+k}
40 ! -\tilde{\Theta}_{\alpha}({\bf G-G'})e^{-i({\bf G-G'})\cdot{\bf r}_{\alpha}}
41 ! \right)
42 ! \end{align*}
43 ! where both ${\bf G}$ and ${\bf G'}$ run over the APW indices;
44 ! $\tilde{\Theta}_{\alpha}$ is the form factor of the smooth step function for
45 ! muffin-tin $\alpha$; and $H^{\alpha}$ and $O^{\alpha}$ are the muffin-tin
46 ! Hamiltonian and overlap matrices, respectively. The APW-local-orbital part
47 ! is given by
48 ! \begin{align*}
49 ! \delta H_{\bf G,G'}^{\alpha}&=i({\bf G+k})H^{\alpha}_{\bf G+k,G'+k}\\
50 ! \delta O_{\bf G,G'}^{\alpha}&=i({\bf G+k})O^{\alpha}_{\bf G+k,G'+k}
51 ! \end{align*}
52 ! where ${\bf G}$ runs over the APW indices and ${\bf G'}$ runs over the
53 ! local-orbital indices. There is no contribution from the
54 ! local-orbital-local-orbital part of the matrices. We can now write the IBS
55 ! correction in terms of the basis of first-variational states as
56 ! \begin{align*}
57 ! {\bf F}_{ij}^{\alpha{\bf k}}=\sum_{\bf G,G'}
58 ! b^{i{\bf k}*}_{\bf G}b^{j{\bf k}}_{\bf G'}\left(
59 ! \delta H_{\bf G,G'}^{\alpha}-\epsilon_j\delta O_{\bf G,G'}^{\alpha}\right),
60 ! \end{align*}
61 ! where $b^{i{\bf k}}$ is the first-variational eigenvector.
62 ! Finally, the ${\bf F}_{ij}^{\alpha{\bf k}}$ matrix elements can be
63 ! multiplied by the second-variational coefficients, and contracted over all
64 ! indices to obtain the IBS force:
65 ! \begin{align*}
66 ! {\bf F}_{\rm IBS}^{\alpha}=\sum_{\bf k}w_{\bf k}\sum_{l\sigma}n_{l{\bf k}}
67 ! \sum_{ij}c_{\sigma i}^{l{\bf k}*}c_{\sigma j}^{l{\bf k}}
68 ! {\bf F}_{ij}^{\alpha{\bf k}}
69 ! +\int_{\rm MT_{\alpha}}v_s({\bf r})\nabla\left[\rho({\bf r})
70 ! -\rho^{\alpha}_{\rm core}({\bf r})\right]\,d{\bf r},
71 ! \end{align*}
72 ! where $c^{l{\bf k}}$ are the second-variational coefficients, $w_{\bf k}$
73 ! are the $k$-point weights, $n_{l{\bf k}}$ are the occupation numbers.
74 !
75 ! !REVISION HISTORY:
76 ! Created January 2004 (JKD)
77 ! Fixed problem with second-variational forces, May 2008 (JKD)
78 !EOP
79 !BOC
80 implicit none
81 ! local variables
82 integer ik,idm,is,ias
83 integer nr,nri,i,j,nthd
84 real(8) fav(3),ca,t1,t2
85 real(8) ts0,ts1
86 ! automatic arrays
87 real(8) forceibs(3,natmtot),grfmt(npmtmax,3)
88 ! allocatable arrays
89 real(8), allocatable :: rfmt(:,:)
90 ! external functions
91 real(8), external :: rfmtinp,rfmtint
92 call timesec(ts0)
93 ! coupling constant of the external A-field (-1/c)
94 ca=-1.d0/solsc
95 !---------------------------------!
96 ! Hellmann-Feynman forces !
97 !---------------------------------!
98 do ias=1,natmtot
99  is=idxis(ias)
100  nr=nrmt(is)
101  nri=nrmti(is)
102  call gradrfmt(nr,nri,rlmt(:,-1,is),wcrmt(:,:,is),vclmt(:,ias),npmtmax,grfmt)
103 ! force from Coulomb potential
104  forcehf(1:3,ias)=-spzn(is)*grfmt(1,1:3)*y00
105 ! force on nuclei from time-dependent E-field
106  if (tafieldt) then
107  do i=1,3
108  t1=(chgsmt(ias,i)+spzn(is))*efieldt(i)
109  forcehf(i,ias)=forcehf(i,ias)+t1
110  end do
111  end if
112 end do
113 ! symmetrise Hellmann-Feynman forces
114 call symveca(forcehf)
115 !----------------------------------!
116 ! IBS correction to forces !
117 !----------------------------------!
118 ! set the IBS forces to zero
119 forceibs(:,:)=0.d0
120 ! compute k-point dependent contribution to the IBS forces
121 call holdthd(nkpt/np_mpi,nthd)
122 !$OMP PARALLEL DO DEFAULT(SHARED) &
123 !$OMP REDUCTION(+:forceibs) &
124 !$OMP SCHEDULE(DYNAMIC) &
125 !$OMP NUM_THREADS(nthd)
126 do ik=1,nkpt
127 ! distribute among MPI processes
128  if (mod(ik-1,np_mpi) /= lp_mpi) cycle
129  call forcek(ik,forceibs)
130 end do
131 !$OMP END PARALLEL DO
132 call freethd(nthd)
133 ! add IBS forces from each process and redistribute
134 if (np_mpi > 1) then
135  call mpi_allreduce(mpi_in_place,forceibs,3*natmtot,mpi_double_precision, &
136  mpi_sum,mpicom,ierror)
137 end if
138 if (tafieldt) then
139  t2=afieldt(1,itimes)**2+afieldt(2,itimes)**2+afieldt(3,itimes)**2
140  t2=t2*ca**2
141 end if
142 ! integral of Kohn-Sham potential with gradient of density
143 do ias=1,natmtot
144  is=idxis(ias)
145  nr=nrmt(is)
146  nri=nrmti(is)
147  call gradrfmt(nr,nri,rlmt(:,-1,is),wcrmt(:,:,is),rhomt(:,ias),npmtmax,grfmt)
148  do i=1,3
149  t1=rfmtinp(nr,nri,wr2mt(:,is),vsmt(:,ias),grfmt(:,i))
150 ! remove contribution from gauge correction to the current density
151  if (tafieldt) then
152  t1=t1-t2*rfmtint(nr,nri,wr2mt(:,is),grfmt(:,i))
153  end if
154  forceibs(i,ias)=forceibs(i,ias)+t1
155  end do
156 end do
157 ! integral of Kohn-Sham magnetic field with magnetisation gradient
158 if (spinpol) then
159  allocate(rfmt(npmtmax,natmtot))
160  do idm=1,ndmag
161  do ias=1,natmtot
162  is=idxis(ias)
163  call rfsht(nrcmt(is),nrcmti(is),bsmt(:,ias,idm),rfmt(:,ias))
164  end do
165  call rfmtctof(rfmt)
166  do ias=1,natmtot
167  is=idxis(ias)
168  nr=nrmt(is)
169  nri=nrmti(is)
170  call gradrfmt(nr,nri,rlmt(:,-1,is),wcrmt(:,:,is),magmt(:,ias,idm), &
171  npmtmax,grfmt)
172  do i=1,3
173  t1=rfmtinp(nr,nri,wr2mt(:,is),rfmt(:,ias),grfmt(:,i))
174  forceibs(i,ias)=forceibs(i,ias)+t1
175  end do
176  end do
177  end do
178  deallocate(rfmt)
179 end if
180 ! time-dependent vector potential times integral of the gradient of the current
181 ! density over the muffin-tin
182 if (tafieldt.and.tjr) then
183  do j=1,3
184  t1=ca*afieldt(j,itimes)
185  do ias=1,natmtot
186  is=idxis(ias)
187  nr=nrmt(is)
188  nri=nrmti(is)
189  call gradrfmt(nr,nri,rlmt(:,-1,is),wcrmt(:,:,is),jrmt(:,ias,j),npmtmax, &
190  grfmt)
191  do i=1,3
192  t2=rfmtint(nr,nri,wr2mt(:,is),grfmt(:,i))
193  forceibs(i,ias)=forceibs(i,ias)+t1*t2
194  end do
195  end do
196  end do
197 end if
198 ! symmetrise IBS forces
199 call symveca(forceibs)
200 ! total force on each atom
201 do ias=1,natmtot
202  forcetot(1:3,ias)=forcehf(1:3,ias)+forceibs(1:3,ias)
203 end do
204 ! symmetrise total forces
205 call symveca(forcetot)
206 ! remove the average force, if required, to prevent translation of atomic basis
207 if (tfav0) then
208  fav(1:3)=0.d0
209  do ias=1,natmtot
210  fav(1:3)=fav(1:3)+forcetot(1:3,ias)
211  end do
212  fav(1:3)=fav(1:3)/dble(natmtot)
213  do ias=1,natmtot
214  forcetot(1:3,ias)=forcetot(1:3,ias)-fav(1:3)
215  end do
216 end if
217 ! zero force on atoms with negative mass
218 do ias=1,natmtot
219  is=idxis(ias)
220  if (spmass(is) <= 0.d0) forcetot(1:3,ias)=0.d0
221 end do
222 ! compute maximum force magnitude over all atoms
223 forcemax=0.d0
224 do ias=1,natmtot
225  t1=norm2(forcetot(1:3,ias))
226  if (t1 > forcemax) forcemax=t1
227 end do
228 ! restrict maximum force magnitude if required
229 if (maxforce >= 0.d0) then
230  if (forcemax > maxforce) then
231  t1=maxforce/forcemax
232  forcetot(1:3,1:natmtot)=t1*forcetot(1:3,1:natmtot)
233  end if
234 end if
235 call timesec(ts1)
236 timefor=timefor+ts1-ts0
237 ! write total forces to test file
238 call writetest(750,'total forces',nv=3*natmtot,tol=1.d-3,rva=forcetot)
239 end subroutine
240 !EOC
241 
subroutine writetest(id, descr, nv, iv, iva, tol, rv, rva, zv, zva)
Definition: modtest.f90:16
logical tjr
Definition: modmain.f90:620
logical spinpol
Definition: modmain.f90:228
integer nkpt
Definition: modmain.f90:461
subroutine symveca(vca)
Definition: symveca.f90:10
integer ndmag
Definition: modmain.f90:238
real(8), dimension(:,:,:), allocatable rlmt
Definition: modmain.f90:179
logical tfav0
Definition: modmain.f90:1003
real(8) forcemax
Definition: modmain.f90:997
real(8), dimension(:,:), allocatable vclmt
Definition: modmain.f90:624
subroutine forcek(ik, forceibs)
Definition: forcek.f90:10
Definition: modomp.f90:6
real(8), dimension(:,:), pointer, contiguous rhomt
Definition: modmain.f90:614
real(8), dimension(:,:), allocatable forcetot
Definition: modmain.f90:993
logical tafieldt
Definition: modtddft.f90:56
subroutine rfmtctof(rfmt)
Definition: rfmtctof.f90:10
real(8) timefor
Definition: modmain.f90:1227
integer np_mpi
Definition: modmpi.f90:13
real(8), dimension(:,:,:), pointer, contiguous magmt
Definition: modmain.f90:616
real(8), dimension(maxspecies) spmass
Definition: modmain.f90:101
real(8) maxforce
Definition: modmain.f90:1000
real(8), dimension(:,:), allocatable forcehf
Definition: modmain.f90:991
real(8) solsc
Definition: modmain.f90:1253
subroutine gradrfmt(nr, nri, ri, wcr, rfmt, ld, grfmt)
Definition: gradrfmt.f90:10
integer, dimension(maxatoms *maxspecies) idxis
Definition: modmain.f90:44
Definition: modmpi.f90:6
real(8), dimension(:,:), allocatable afieldt
Definition: modtddft.f90:58
subroutine timesec(ts)
Definition: timesec.f90:10
subroutine force
Definition: force.f90:10
integer itimes
Definition: modtddft.f90:46
subroutine rfsht(nr, nri, rfmt1, rfmt2)
Definition: rfsht.f90:7
integer lp_mpi
Definition: modmpi.f90:15
subroutine freethd(nthd)
Definition: modomp.f90:106
real(8), dimension(:,:), allocatable chgsmt
Definition: modtddft.f90:86
subroutine holdthd(nloop, nthd)
Definition: modomp.f90:78
real(8), dimension(maxspecies) spzn
Definition: modmain.f90:80
real(8), dimension(:,:,:), allocatable jrmt
Definition: modmain.f90:622
integer, dimension(maxspecies) nrcmt
Definition: modmain.f90:173
integer, dimension(maxspecies) nrcmti
Definition: modmain.f90:211
real(8), dimension(:,:,:), allocatable wcrmt
Definition: modmain.f90:187
real(8), dimension(:,:), pointer, contiguous vsmt
Definition: modmain.f90:649
integer, dimension(maxspecies) nrmti
Definition: modmain.f90:211
real(8), dimension(3) efieldt
Definition: modtddft.f90:74
real(8), parameter y00
Definition: modmain.f90:1236
integer mpicom
Definition: modmpi.f90:11
real(8), dimension(:,:,:), pointer, contiguous bsmt
Definition: modmain.f90:656
real(8), dimension(:,:), allocatable wr2mt
Definition: modmain.f90:183
integer ierror
Definition: modmpi.f90:19
integer, dimension(maxspecies) nrmt
Definition: modmain.f90:150