The Elk Code
dforcek.f90
Go to the documentation of this file.
1 
2 ! Copyright (C) 2012 J. K. Dewhurst, S. Sharma and E. K. U. Gross.
3 ! This file is distributed under the terms of the GNU General Public License.
4 ! See the file COPYING for license details.
5 
6 subroutine dforcek(ik,dynibs)
7 use modmain
8 use modphonon
9 use modpw
10 implicit none
11 ! arguments
12 integer, intent(in) :: ik
13 complex(8), intent(inout) :: dynibs(3,natmtot)
14 ! local variables
15 integer ispn0,ispn1,ispn,jspn
16 integer n,nq,nm,nmq
17 integer is,ias,ist,jst,jk
18 integer iv(3),ig,i,j,l
19 real(8) t1
20 complex(8) z1,z2,dt1,dz1,dz2
21 ! automatic arrays
22 real(8) evalfv(nstfv,nspnfv)
23 complex(8) vh(nmatmax),vo(nmatmax),dvh(nmatmax),dvo(nmatmax)
24 complex(8) ffv(nstfv,nstfv),dffv(nstfv,nstfv),y(nstfv),dy(nstfv)
25 ! allocatable arrays
26 integer, allocatable :: ijg(:,:),ijgq(:,:)
27 real(8), allocatable :: dp(:,:),dpq(:,:)
28 complex(8), allocatable :: apwalm(:,:,:,:),apwalmq(:,:,:,:),dapwalm(:,:,:)
29 complex(8), allocatable :: evecfv(:,:,:),devecfv(:,:,:)
30 complex(8), allocatable :: evecsv(:,:),devecsv(:,:)
31 complex(8), allocatable :: h(:,:),o(:,:),dlh(:,:),dlo(:,:)
32 complex(8), allocatable :: hq(:,:),oq(:,:),dh(:,:),od(:,:)
33 complex(8), allocatable :: dlhq(:,:),dloq(:,:),ddlh(:,:),ddlo(:,:)
34 ! external functions
35 complex(8), external :: zdotc
36 ! allocate local arrays
37 allocate(ijg(nmatmax,nmatmax),ijgq(nmatmax,nmatmax))
38 allocate(dp(nmatmax,nmatmax),dpq(nmatmax,nmatmax))
39 allocate(apwalm(ngkmax,apwordmax,lmmaxapw,natmtot))
40 allocate(apwalmq(ngkmax,apwordmax,lmmaxapw,natmtot))
41 allocate(dapwalm(ngkmax,apwordmax,lmmaxapw))
42 allocate(evecfv(nmatmax,nstfv,nspnfv))
43 allocate(devecfv(nmatmax,nstfv,nspnfv))
44 allocate(h(nmatmax,nmatmax),o(nmatmax,nmatmax))
45 allocate(dlh(nmatmax,nmatmax),dlo(nmatmax,nmatmax))
46 allocate(hq(nmatmax,nmatmax),oq(nmatmax,nmatmax))
47 allocate(dh(nmatmax,nmatmax),od(nmatmax,nmatmax))
48 allocate(dlhq(nmatmax,nmatmax),dloq(nmatmax,nmatmax))
49 allocate(ddlh(nmatmax,nmatmax),ddlo(nmatmax,nmatmax))
50 ! equivalent reduced k-point
51 jk=ivkik(ivk(1,ik),ivk(2,ik),ivk(3,ik))
52 ! get the eigenvalues/vectors from file
53 call getevalfv(filext,0,vkl(:,ik),evalfv)
54 call getevecfv(filext,0,vkl(:,ik),vgkl(:,:,:,ik),evecfv)
55 ! get the eigenvalue/vector derivatives from file
56 call getdevecfv(ik,iqph,isph,iaph,ipph,devecfv)
57 if (tevecsv) then
58  allocate(evecsv(nstsv,nstsv),devecsv(nstsv,nstsv))
59  call getevecsv(filext,0,vkl(:,ik),evecsv)
60  call getdevecsv(ik,iqph,isph,iaph,ipph,devecsv)
61 end if
62 ! loop over first-variational spin components
63 do jspn=1,nspnfv
64  if (spinsprl) then
65  ispn0=jspn; ispn1=jspn
66  else
67  ispn0=1; ispn1=nspinor
68  end if
69  n=ngk(jspn,ik)
70  nq=ngkq(jspn,ik)
71  nm=n+nlotot
72  nmq=nq+nlotot
73  do j=1,n
74  do i=1,n
75  iv(:)=ivg(:,igkig(i,jspn,ik))-ivg(:,igkig(j,jspn,ik))
76  iv(:)=modulo(iv(:)-intgv(1,:),ngridg(:))+intgv(1,:)
77  ijg(i,j)=ivgig(iv(1),iv(2),iv(3))
78  dp(i,j)=0.5d0*dot_product(vgkc(1:3,i,jspn,ik),vgkc(1:3,j,jspn,ik))
79  end do
80  end do
81  do j=1,n
82  do i=1,nq
83  iv(:)=ivg(:,igkqig(i,jspn,ik))-ivg(:,igkig(j,jspn,ik))
84  iv(:)=modulo(iv(:)-intgv(1,:),ngridg(:))+intgv(1,:)
85  ijgq(i,j)=ivgig(iv(1),iv(2),iv(3))
86  dpq(i,j)=0.5d0*dot_product(vgkqc(1:3,i,jspn,ik),vgkc(1:3,j,jspn,ik))
87  end do
88  end do
89 ! find the matching coefficients
90  call match(n,vgkc(:,:,jspn,ik),gkc(:,jspn,ik),sfacgk(:,:,jspn,ik),apwalm)
91  call match(nq,vgkqc(:,:,jspn,ik),gkqc(:,jspn,ik),sfacgkq(:,:,jspn,ik), &
92  apwalmq)
93 ! find the matching coefficient derivatives
94  call dmatch(iasph,ipph,n,vgkc(:,:,jspn,ik),apwalm,dapwalm)
95 ! loop over species and atoms
96  do ias=1,natmtot
97  is=idxis(ias)
98 ! Hamiltonian and overlap matrices
99  do j=1,nm
100  h(1:j,j)=0.d0
101  end do
102  call hmlaa(.false.,is,ias,n,apwalm(:,:,:,ias),nmatmax,h)
103  call hmlalo(is,ias,n,apwalm(:,:,:,ias),nmatmax,h)
104  do j=1,nm
105  o(1:j,j)=0.d0
106  end do
107  call olpaa(.false.,is,n,apwalm(:,:,:,ias),nmatmax,o)
108  call olpalo(is,ias,n,apwalm(:,:,:,ias),nmatmax,o)
109  hq(:,:)=0.d0
110  call hmlaaq(is,ias,n,nq,apwalm(:,:,:,ias),apwalmq(:,:,:,ias),nmatmax,hq)
111  call hmlaloq(is,ias,n,nq,apwalm(:,:,:,ias),apwalmq(:,:,:,ias),nmatmax,hq)
112  oq(:,:)=0.d0
113  call olpaaq(is,n,nq,apwalm(:,:,:,ias),apwalmq(:,:,:,ias),nmatmax,oq)
114  call olpaloq(is,ias,n,nq,apwalm(:,:,:,ias),apwalmq(:,:,:,ias),nmatmax,oq)
115 ! Hamiltonian and overlap derivatives
116  dh(:,:)=0.d0
117  call dhmlaa(is,ias,n,n,apwalm(:,:,:,ias),apwalm(:,:,:,ias),dapwalm,dapwalm,&
118  nmatmax,dh)
119  call dhmlalo(is,ias,n,n,apwalm(:,:,:,ias),apwalm(:,:,:,ias),dapwalm, &
120  dapwalm,nmatmax,dh)
121  od(:,:)=0.d0
122  call dolpaa(is,ias,n,n,apwalm(:,:,:,ias),apwalm(:,:,:,ias),dapwalm,dapwalm,&
123  nmatmax,od)
124  call dolpalo(is,ias,n,n,dapwalm,dapwalm,nmatmax,od)
125 ! loop over Cartesian directions
126  do l=1,3
127 ! APW-APW contribution
128  do j=1,n
129  do i=1,j
130  ig=ijg(i,j)
131  t1=vgc(l,ig)
132  z1=-ffacg(ig,is)*conjg(sfacg(ig,ias))
133  z2=t1*(dp(i,j)*z1+h(i,j))
134  dlh(i,j)=cmplx(-z2%im,z2%re,8)
135  z2=t1*(z1+o(i,j))
136  dlo(i,j)=cmplx(-z2%im,z2%re,8)
137  end do
138  end do
139  do j=n+1,nm
140 ! APW-local-orbital contribution
141  do i=1,n
142  t1=vgkc(l,i,jspn,ik)
143  z1=t1*h(i,j)
144  dlh(i,j)=cmplx(-z1%im,z1%re,8)
145  z1=t1*o(i,j)
146  dlo(i,j)=cmplx(-z1%im,z1%re,8)
147  end do
148 ! zero the local-orbital-local-orbital contribution
149  do i=n+1,j
150  dlh(i,j)=0.d0
151  dlo(i,j)=0.d0
152  end do
153  end do
154 ! non-square H/O(G+k+q,G'+k) matrices
155 ! APW-APW contribution
156  do j=1,n
157  do i=1,nq
158  ig=ijgq(i,j)
159  t1=vgqc(l,ig)
160  z1=-ffacgq(ig,is)*conjg(sfacgq(ig,ias))
161  z2=t1*(dpq(i,j)*z1+hq(i,j))
162  dlhq(i,j)=cmplx(-z2%im,z2%re,8)
163  z2=t1*(z1+oq(i,j))
164  dloq(i,j)=cmplx(-z2%im,z2%re,8)
165  end do
166 ! local-orbital-APW derivative
167  t1=-vgkc(l,j,jspn,ik)
168  do i=nq+1,nmq
169  z1=t1*hq(i,j)
170  dlhq(i,j)=cmplx(-z1%im,z1%re,8)
171  z1=t1*oq(i,j)
172  dloq(i,j)=cmplx(-z1%im,z1%re,8)
173  end do
174  end do
175  do j=n+1,nm
176 ! APW-local-orbital contribution
177  do i=1,nq
178  t1=vgkqc(l,i,jspn,ik)
179  z1=t1*hq(i,j)
180  dlhq(i,j)=cmplx(-z1%im,z1%re,8)
181  z1=t1*oq(i,j)
182  dloq(i,j)=cmplx(-z1%im,z1%re,8)
183  end do
184 ! zero the local-orbital-local-orbital contribution
185  do i=nq+1,nmq
186  dlhq(i,j)=0.d0
187  dloq(i,j)=0.d0
188  end do
189  end do
190 ! APW-APW derivative
191  do j=1,n
192  do i=1,n
193  ig=ijg(i,j)
194  t1=vgc(l,ig)
195  if (ias == iasph) then
196  z1=-ffacg(ig,is)*conjg(sfacg(ig,ias))
197  dz1=vgc(ipph,ig)*cmplx(z1%im,-z1%re,8)
198  else
199  dz1=0.d0
200  end if
201  z2=t1*(dp(i,j)*dz1+dh(i,j))
202  ddlh(i,j)=cmplx(-z2%im,z2%re,8)
203  z2=t1*(dz1+od(i,j))
204  ddlo(i,j)=cmplx(-z2%im,z2%re,8)
205  end do
206 ! local-orbital-APW derivative
207  t1=-vgkc(l,j,jspn,ik)
208  do i=n+1,nm
209  z1=t1*dh(i,j)
210  ddlh(i,j)=cmplx(-z1%im,z1%re,8)
211  z1=t1*od(i,j)
212  ddlo(i,j)=cmplx(-z1%im,z1%re,8)
213  end do
214  end do
215 ! APW-local-orbital derivative
216  do j=n+1,nm
217  do i=1,n
218  t1=vgkc(l,i,jspn,ik)
219  z1=t1*dh(i,j)
220  ddlh(i,j)=cmplx(-z1%im,z1%re,8)
221  z1=t1*od(i,j)
222  ddlo(i,j)=cmplx(-z1%im,z1%re,8)
223  end do
224 ! zero the local-orbital-local-orbital derivative
225  do i=n+1,nm
226  ddlh(i,j)=0.d0
227  ddlo(i,j)=0.d0
228  end do
229  end do
230  if (tphq0) then
231 ! compute the force matrix elements in the first-variational basis
232  do jst=1,nstfv
233  call zhemv('U',nm,zone,dlh,nmatmax,evecfv(:,jst,jspn),1,zzero,vh,1)
234  call zhemv('U',nm,zone,dlo,nmatmax,evecfv(:,jst,jspn),1,zzero,vo,1)
235  t1=evalfv(jst,jspn)
236  do ist=1,nstfv
237  z1=zdotc(nm,evecfv(:,ist,jspn),1,vh,1)
238  z2=zdotc(nm,evecfv(:,ist,jspn),1,vo,1)
239  ffv(ist,jst)=z1-t1*z2
240  end do
241  end do
242  end if
243 ! compute the force derivative matrix elements in the first-variational basis
244  dffv(:,:)=0.d0
245  do jst=1,nstfv
246  call zhemv('U',nm,zone,dlo,nmatmax,evecfv(:,jst,jspn),1,zzero,vo,1)
247  call zgemv('N',nm,nm,zone,ddlh,nmatmax,evecfv(:,jst,jspn),1,zzero,dvh,1)
248  call zgemv('N',nm,nm,zone,ddlo,nmatmax,evecfv(:,jst,jspn),1,zzero,dvo,1)
249  t1=evalfv(jst,jspn)
250  dt1=devalfv(jst,jspn,ik)
251  do ist=1,nstfv
252  z2=zdotc(nm,evecfv(:,ist,jspn),1,vo,1)
253  dz1=zdotc(nm,evecfv(:,ist,jspn),1,dvh,1)
254  dz2=zdotc(nm,evecfv(:,ist,jspn),1,dvo,1)
255  dffv(ist,jst)=dffv(ist,jst)+dz1-dt1*z2-t1*dz2
256  end do
257  call zgemv('C',nmq,nm,zone,dlhq,nmatmax,devecfv(:,jst,jspn),1,zzero, &
258  dvh,1)
259  call zgemv('C',nmq,nm,zone,dloq,nmatmax,devecfv(:,jst,jspn),1,zzero, &
260  dvo,1)
261  do ist=1,nstfv
262  dz1=2.d0*zdotc(nm,evecfv(:,ist,jspn),1,dvh,1)
263  dz2=2.d0*zdotc(nm,evecfv(:,ist,jspn),1,dvo,1)
264  dffv(ist,jst)=dffv(ist,jst)+dz1-t1*dz2
265  end do
266  end do
267  z1=0.d0
268  if (tevecsv) then
269 ! spin-polarised case
270  do j=1,nstsv
271  do ispn=ispn0,ispn1
272  i=(ispn-1)*nstfv+1
273  call zgemv('N',nstfv,nstfv,zone,ffv,nstfv,evecsv(i,j),1,zzero,y,1)
274  call zgemv('N',nstfv,nstfv,zone,dffv,nstfv,evecsv(i,j),1,zzero,dy,1)
275  call zgemv('N',nstfv,nstfv,zone,ffv,nstfv,devecsv(i,j),1,zone,dy,1)
276  dz1=zdotc(nstfv,evecsv(i,j),1,dy,1)
277  dz1=dz1+zdotc(nstfv,devecsv(i,j),1,y,1)
278  z1=z1+occsv(j,jk)*dz1
279 !******** doccsv
280  end do
281  end do
282  else
283 ! spin-unpolarised case
284  do j=1,nstsv
285  z1=z1+occsv(j,jk)*dffv(j,j)
286  if (tphq0) then
287  z1=z1+doccsv(j,ik)*dble(ffv(j,j))
288  end if
289  end do
290  end if
291  dynibs(l,ias)=dynibs(l,ias)-wkptnr*z1
292 ! end loop over Cartesian components
293  end do
294 ! end loop over atoms and species
295  end do
296 ! end loop over first-variational spins
297 end do
298 deallocate(ijg,ijgq,dp,dpq)
299 deallocate(apwalm,apwalmq,dapwalm)
300 deallocate(evecfv,devecfv)
301 deallocate(h,o,dlh,dlo,hq,oq,dh,od)
302 deallocate(dlhq,dloq,ddlh,ddlo)
303 if (tevecsv) deallocate(evecsv,devecsv)
304 end subroutine
305 
complex(8), dimension(:,:), allocatable sfacg
Definition: modmain.f90:430
logical tphq0
Definition: modphonon.f90:17
subroutine dhmlaa(is, ias, ngp, ngpq, apwalm, apwalmq, dapwalm, dapwalmq, ld, dh)
Definition: dhmlaa.f90:7
subroutine getevecsv(fext, ikp, vpl, evecsv)
Definition: getevecsv.f90:7
integer, dimension(3) ngridg
Definition: modmain.f90:386
character(256) filext
Definition: modmain.f90:1301
integer lmmaxapw
Definition: modmain.f90:199
real(8), dimension(:,:,:), allocatable gkqc
Definition: modphonon.f90:90
integer nlotot
Definition: modmain.f90:790
integer ngkmax
Definition: modmain.f90:499
integer, dimension(:,:,:), allocatable ivgig
Definition: modmain.f90:402
subroutine getevecfv(fext, ikp, vpl, vgpl, evecfv)
Definition: getevecfv.f90:10
logical tevecsv
Definition: modmain.f90:923
subroutine match(ngp, vgpc, gpc, sfacgp, apwalm)
Definition: match.f90:10
logical spinsprl
Definition: modmain.f90:283
integer iasph
Definition: modphonon.f90:15
integer, dimension(:,:,:), allocatable ivkik
Definition: modmain.f90:467
subroutine hmlaa(thr, is, ias, ngp, apwalm, ld, h)
Definition: hmlaa.f90:10
complex(8), parameter zone
Definition: modmain.f90:1240
subroutine getdevecsv(ik, iq, is, ia, ip, devecsv)
Definition: getdevecsv.f90:7
complex(8), dimension(:,:,:,:), allocatable sfacgk
Definition: modmain.f90:509
subroutine olpaaq(is, ngp, ngpq, apwalm, apwalmq, ld, oq)
Definition: olpaaq.f90:7
subroutine hmlaaq(is, ias, ngp, ngpq, apwalm, apwalmq, ld, hq)
Definition: hmlaaq.f90:7
real(8), dimension(:,:), allocatable vgc
Definition: modmain.f90:420
subroutine olpaa(tor, is, ngp, apwalm, ld, o)
Definition: olpaa.f90:7
integer nstsv
Definition: modmain.f90:889
integer, dimension(:,:), allocatable ngk
Definition: modmain.f90:497
complex(8), dimension(:,:,:,:), allocatable sfacgkq
Definition: modphonon.f90:92
pure subroutine olpalo(is, ias, ngp, apwalm, ld, o)
Definition: olpalo.f90:7
real(8), dimension(:,:), allocatable doccsv
Definition: modphonon.f90:136
subroutine olpaloq(is, ias, ngp, ngpq, apwalm, apwalmq, ld, oq)
Definition: olpaloq.f90:7
real(8), dimension(:,:,:,:), allocatable vgkl
Definition: modmain.f90:503
real(8), dimension(:,:,:), allocatable devalfv
Definition: modphonon.f90:132
integer ipph
Definition: modphonon.f90:15
real(8), dimension(:,:), allocatable ffacg
Definition: modmain.f90:432
real(8), dimension(:,:), allocatable occsv
Definition: modmain.f90:905
real(8), dimension(:,:), allocatable vgqc
Definition: modphonon.f90:62
subroutine getevalfv(fext, ikp, vpl, evalfv)
Definition: getevalfv.f90:7
integer isph
Definition: modphonon.f90:15
integer nspinor
Definition: modmain.f90:267
subroutine dolpaa(is, ias, ngp, ngpq, apwalm, apwalmq, dapwalm, dapwalmq, ld, od)
Definition: dolpaa.f90:7
subroutine dforcek(ik, dynibs)
Definition: dforcek.f90:7
real(8), dimension(:,:,:,:), allocatable vgkc
Definition: modmain.f90:505
integer, dimension(:,:), allocatable ivg
Definition: modmain.f90:400
complex(8), parameter zzero
Definition: modmain.f90:1240
real(8), dimension(:,:), allocatable vkl
Definition: modmain.f90:471
integer apwordmax
Definition: modmain.f90:760
integer, dimension(2, 3) intgv
Definition: modmain.f90:394
integer, dimension(maxatoms *maxspecies) idxis
Definition: modmain.f90:44
real(8), dimension(:,:), allocatable ffacgq
Definition: modphonon.f90:74
real(8), dimension(:,:,:), allocatable gkc
Definition: modmain.f90:507
Definition: modpw.f90:6
integer iqph
Definition: modphonon.f90:15
complex(8), dimension(:,:), allocatable sfacgq
Definition: modphonon.f90:72
subroutine getdevecfv(ik, iq, is, ia, ip, devecfv)
Definition: getdevecfv.f90:7
integer, dimension(:,:), allocatable ngkq
Definition: modphonon.f90:84
real(8) wkptnr
Definition: modmain.f90:477
real(8), dimension(:,:,:,:), allocatable vgkqc
Definition: modphonon.f90:88
integer iaph
Definition: modphonon.f90:15
subroutine hmlaloq(is, ias, ngp, ngpq, apwalm, apwalmq, ld, hq)
Definition: hmlaloq.f90:7
pure subroutine dmatch(ias, ip, ngp, vgpc, apwalm, dapwalm)
Definition: dmatch.f90:7
pure subroutine dolpalo(is, ias, ngp, ngpq, dapwalm, dapwalmq, ld, od)
Definition: dolpalo.f90:7
integer, dimension(:,:,:), allocatable igkig
Definition: modmain.f90:501
pure subroutine dhmlalo(is, ias, ngp, ngpq, apwalm, apwalmq, dapwalm, dapwalmq, ld, dh)
Definition: dhmlalo.f90:7
integer, dimension(:,:), allocatable ivk
Definition: modmain.f90:465
integer, dimension(:,:,:), allocatable igkqig
Definition: modphonon.f90:86
pure subroutine hmlalo(is, ias, ngp, apwalm, ld, h)
Definition: hmlalo.f90:7