The Elk Code
energy.f90
Go to the documentation of this file.
1 
2 ! Copyright (C) 2002-2006 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: energy
8 ! !INTERFACE:
9 subroutine energy
10 ! !USES:
11 use modmain
12 use moddftu
13 use modtest
14 ! !DESCRIPTION:
15 ! Computes the total energy and its individual contributions. The kinetic
16 ! energy is given by
17 ! $$ T_s=\sum_i n_i\epsilon_i-\int\rho({\bf r})[v_{\rm C}({\bf r})
18 ! +v_{\rm xc}({\bf r})]d{\bf r}-\int {\bf m}({\bf r})\cdot
19 ! ({\bf B}_{\rm xc}({\bf r})+{\bf B}_{\rm ext}({\bf r}))d{\bf r}, $$
20 ! where $n_i$ are the occupation numbers and $\epsilon_i$ are the eigenvalues
21 ! of both the core and valence states; $\rho$ is the density; ${\bf m}$ is the
22 ! magnetisation density; $v_{\rm C}$ is the Coulomb potential; $v_{\rm xc}$
23 ! and ${\bf B}_{\rm xc}$ are the exchange-correlation potential and magnetic
24 ! field, respectively; and ${\bf B}_{\rm ext}$ is the external magnetic field.
25 ! The Hartree, electron-nuclear and nuclear-nuclear electrostatic energies are
26 ! combined into the Coulomb energy:
27 ! \begin{align*}
28 ! E_{\rm C}&=E_{\rm H}+E_{\rm en}+E_{\rm nn} \\
29 ! &=\frac{1}{2}V_{\rm C}+E_{\rm Mad},
30 ! \end{align*}
31 ! where
32 ! $$ V_{\rm C}=\int\rho({\bf r})v_{\rm C}({\bf r})d{\bf r} $$
33 ! is the Coulomb potential energy. The Madelung energy is given by
34 ! $$ E_{\rm Mad}=\frac{1}{2}\sum_{\alpha}z_{\alpha}R_{\alpha}, $$
35 ! where
36 ! $$ R_{\alpha}=\lim_{r\rightarrow 0}\left(v^{\rm C}_{\alpha;00}(r)Y_{00}
37 ! +\frac{z_{\alpha}}{r}\right) $$
38 ! for atom $\alpha$, with $v^{\rm C}_{\alpha;00}$ being the $l=0$ component of
39 ! the spherical harmonic expansion of $v_{\rm C}$ in the muffin-tin, and
40 ! $z_{\alpha}$ is the nuclear charge. Using the nuclear-nuclear energy
41 ! determined at the start of the calculation, the electron-nuclear and Hartree
42 ! energies can be isolated with
43 ! $$ E_{\rm en}=2\left(E_{\rm Mad}-E_{\rm nn}\right) $$
44 ! and
45 ! $$ E_{\rm H}=\frac{1}{2}(E_{\rm C}-E_{\rm en}). $$
46 ! Finally, the total energy is
47 ! $$ E=T_s+E_{\rm C}+E_{\rm xc}, $$
48 ! where $E_{\rm xc}$ is obtained either by integrating the
49 ! exchange-correlation energy density, or in the case of exact exchange, the
50 ! explicit calculation of the Fock exchange integral. The energy from the
51 ! external magnetic fields in the muffin-tins, {\tt bfcmt}, is always removed
52 ! from the total since these fields are non-physical: their field lines do not
53 ! close. The energy of the physical external field, {\tt bfieldc}, is also not
54 ! included in the total because this field, like those in the muffin-tins, is
55 ! used for breaking spin symmetry and taken to be infintesimal. If this field
56 ! is intended to be finite, then the associated energy, {\tt engybext}, should
57 ! be added to the total by hand. See {\tt potxc}, {\tt exxengy} and related
58 ! subroutines.
59 !
60 ! !REVISION HISTORY:
61 ! Created May 2003 (JKD)
62 !EOP
63 !BOC
64 implicit none
65 ! local variables
66 integer ik,ist,idm,jdm
67 integer is,ias,n2,idu
68 real(8) ca,cb,sm,w,f
69 complex(8) z1
70 ! allocatable arrays
71 real(8), allocatable :: rfmt(:,:)
72 complex(8), allocatable :: evecsv(:,:),kmat(:,:),c(:,:)
73 ! external functions
74 real(8), external :: rfinp
75 complex(8), external :: zdotc
76 ! coupling constant of the external field (gₑ/4c)
77 cb=gfacte/(4.d0*solsc)
78 !-----------------------!
79 ! core energies !
80 !-----------------------!
81 call energycore
82 !--------------------------------------------!
83 ! valence Kohn-Sham potential energy !
84 !--------------------------------------------!
86 !-----------------------------------------------------!
87 ! exchange-correlation effective field energy !
88 !-----------------------------------------------------!
89 engybxc=0.d0
90 do idm=1,ndmag
91  engybxc=engybxc+rfinp(magmt(:,:,idm),magir(:,idm),bxcmt(:,:,idm),bxcir(:,idm))
92 end do
93 !------------------------------------------!
94 ! external magnetic field energies !
95 !------------------------------------------!
96 engybext=0.d0
97 do idm=1,ndmag
98  jdm=merge(idm,3,ncmag)
99 ! energy of physical global field
100  engybext=engybext+cb*momtot(idm)*bfieldc(jdm)
101 end do
102 !----------------------------------!
103 ! Coulomb potential energy !
104 !----------------------------------!
106 !-------------------------!
107 ! Madelung energy !
108 !-------------------------!
109 engymad=0.d0
110 do ias=1,natmtot
111  is=idxis(ias)
112  engymad=engymad+spzn(is)*(vclmt(1,ias)-vcln(1,is))*y00/2.d0
113 end do
114 !---------------------------------------------!
115 ! electron-nuclear interaction energy !
116 !---------------------------------------------!
117 engyen=2.d0*(engymad-engynn)
118 !------------------------!
119 ! Hartree energy !
120 !------------------------!
121 engyhar=(engyvcl-engyen)/2.d0
122 !------------------------!
123 ! Coulomb energy !
124 !------------------------!
126 !-----------------------------------------------!
127 ! exchange-correlation potential energy !
128 !-----------------------------------------------!
130 !-------------------------!
131 ! exchange energy !
132 !-------------------------!
133 if ((xctype(1) < 0).or.(task == 5)) then
134 ! exact exchange for OEP-EXX or Hartree-Fock on last self-consistent loop
135  if (tlast) then
136  call exxengy
137 ! mix exact and DFT exchange energies for hybrid functionals
138  if (hybrid) then
140  engyx=engyx+rfinp(rhomt,rhoir,exmt,exir)
141  end if
142  else
143  engyx=0.d0
144  end if
145 else
146 ! exchange energy from the density
147  engyx=rfinp(rhomt,rhoir,exmt,exir)
148 end if
149 !----------------------------!
150 ! correlation energy !
151 !----------------------------!
152 if ((task == 5).and.(.not.hybrid)) then
153 ! zero correlation energy for pure Hartree-Fock
154  engyc=0.d0
155 else
156 ! correlation energy from the density
157  engyc=rfinp(rhomt,rhoir,ecmt,ecir)
158 end if
159 !----------------------!
160 ! DFT+U energy !
161 !----------------------!
162 engydu=0.d0
163 if (dftu /= 0) then
164  do idu=1,ndftu
165  is=isldu(1,idu)
166  engydu=engydu+sum(engyadu(1:natoms(is),idu))
167  end do
168 end if
169 !---------------------------------------------!
170 ! sum of occupied valence eigenvalues !
171 !---------------------------------------------!
172 evalsmv=0.d0
173 do ik=1,nkpt
174  evalsmv=evalsmv+wkpt(ik)*sum(occsv(1:nstsv,ik)*evalsv(1:nstsv,ik))
175 end do
176 !------------------------!
177 ! kinetic energy !
178 !------------------------!
179 if (task == 5) then
180 ! Hartree-Fock case
181  allocate(evecsv(nstsv,nstsv),kmat(nstsv,nstsv),c(nstsv,nstsv))
182 ! core kinetic energy
184 ! add valence kinetic energy
185  do ik=1,nkpt
186  w=wkpt(ik)
187  call getevecsv(filext,ik,vkl(:,ik),evecsv)
188  call getkmat(ik,kmat)
189  call zgemm('N','N',nstsv,nstsv,nstsv,zone,kmat,nstsv,evecsv,nstsv,zzero,c, &
190  nstsv)
191  do ist=1,nstsv
192  z1=zdotc(nstsv,evecsv(:,ist),1,c(:,ist),1)
193  engykn=engykn+w*occsv(ist,ik)*z1%re
194  end do
195  end do
196  deallocate(evecsv,kmat,c)
197 else
198 ! Kohn-Sham case
199  allocate(rfmt(npmtmax,natmtot))
200  sm=0.d0
201 ! remove magnetic field contribution
202  do idm=1,ndmag
203  do ias=1,natmtot
204  is=idxis(ias)
205  call rfsht(nrcmt(is),nrcmti(is),bsmt(:,ias,idm),rfmt(:,ias))
206  end do
207  call rfmtctof(rfmt)
208  sm=sm+rfinp(magmt(:,:,idm),magir(:,idm),rfmt,bsir(:,idm))
209  end do
210 ! remove fixed tensor moment potential matrix contribution
211  if (ftmtype /= 0) then
212  n2=(lmmaxdm*nspinor)**2
213  do ias=1,natmtot
214  z1=zdotc(n2,dmatmt(:,:,:,:,ias),1,vmftm(:,:,:,:,ias),1)
215  sm=sm+z1%re
216  end do
217  end if
219  deallocate(rfmt)
220 end if
221 ! remove vector potential energy term if required
222 if (tafield.and.tlast) then
223  ca=1.d0/(2.d0*solsc)
224  engykn=engykn+ca*dot_product(afieldc(:),jtot(:))
225 end if
226 ! remove spin-dependent vector potential energy term if required
227 if (tafsp.and.tlast) call energyafsp
228 !-------------------------------!
229 ! entropic contribution !
230 !-------------------------------!
231 entrpy=0.d0
232 engyts=0.d0
233 ! non-zero only for the Fermi-Dirac smearing function
234 if (stype == 3) then
235  sm=0.d0
236  do ik=1,nkpt
237  w=wkpt(ik)
238  do ist=1,nstsv
239  f=occsv(ist,ik)/occmax
240  if ((f > 0.d0).and.(f < 1.d0)) sm=sm+w*(f*log(f)+(1.d0-f)*log(1.d0-f))
241  end do
242  end do
243 ! entropy
244  entrpy=-occmax*kboltz*sm
245 ! contribution to free energy
247 end if
248 !----------------------!
249 ! total energy !
250 !----------------------!
252 ! add the DFT+U correction if required
253 if (dftu /= 0) engytot=engytot+engydu
254 ! write total energy to test file
255 call writetest(0,'total energy',tol=1.d-5,rv=engytot)
256 end subroutine
257 !EOC
258 
subroutine writetest(id, descr, nv, iv, iva, tol, rv, rva, zv, zva)
Definition: modtest.f90:16
subroutine getevecsv(fext, ikp, vpl, evecsv)
Definition: getevecsv.f90:7
real(8), dimension(3) momtot
Definition: modmain.f90:741
character(256) filext
Definition: modmain.f90:1301
real(8) engyx
Definition: modmain.f90:977
real(8), dimension(:,:), allocatable evalsv
Definition: modmain.f90:915
integer task
Definition: modmain.f90:1299
subroutine energyafsp
Definition: energyafsp.f90:7
integer, dimension(3) xctype
Definition: modmain.f90:591
real(8), dimension(3) jtot
Definition: modmain.f90:751
real(8), dimension(:), allocatable ecir
Definition: modmain.f90:635
real(8), dimension(:), pointer, contiguous rhoir
Definition: modmain.f90:617
logical hybrid
Definition: modmain.f90:1152
integer nkpt
Definition: modmain.f90:464
integer, parameter lmmaxdm
Definition: moddftu.f90:15
real(8), dimension(:,:), allocatable vcln
Definition: modmain.f90:97
integer ndmag
Definition: modmain.f90:240
real(8) engykn
Definition: modmain.f90:957
real(8), dimension(:,:), allocatable vclmt
Definition: modmain.f90:627
real(8) engydu
Definition: moddftu.f90:55
real(8), parameter kboltz
Definition: modmain.f90:1263
real(8), dimension(:,:), pointer, contiguous rhomt
Definition: modmain.f90:617
complex(8), parameter zone
Definition: modmain.f90:1240
subroutine exxengy
Definition: exxengy.f90:7
subroutine getkmat(ik, kmat)
Definition: getkmat.f90:7
real(8), dimension(:,:), allocatable ecmt
Definition: modmain.f90:635
subroutine rfmtctof(rfmt)
Definition: rfmtctof.f90:10
complex(8), dimension(:,:,:,:,:), allocatable dmatmt
Definition: moddftu.f90:17
real(8), dimension(:), allocatable vsir
Definition: modmain.f90:654
real(8) engyvxc
Definition: modmain.f90:971
real(8), dimension(:,:), allocatable exmt
Definition: modmain.f90:633
real(8) engynn
Definition: modmain.f90:959
integer, dimension(2, maxdftu) isldu
Definition: moddftu.f90:49
integer nstsv
Definition: modmain.f90:885
logical tlast
Definition: modmain.f90:1055
real(8), dimension(:), allocatable wkpt
Definition: modmain.f90:478
integer ndftu
Definition: moddftu.f90:47
subroutine energy
Definition: energy.f90:10
real(8), dimension(:,:,:), allocatable bxcmt
Definition: modmain.f90:639
real(8), dimension(:,:), allocatable bsir
Definition: modmain.f90:661
real(8), dimension(:,:,:), pointer, contiguous magmt
Definition: modmain.f90:619
real(8) occmax
Definition: modmain.f90:897
integer ftmtype
Definition: moddftu.f90:79
real(8) engytot
Definition: modmain.f90:985
real(8), dimension(:,:), allocatable engyadu
Definition: moddftu.f90:53
real(8) tempk
Definition: modmain.f90:687
subroutine energycore
Definition: energycore.f90:7
real(8) engybext
Definition: modmain.f90:975
real(8), dimension(:,:), allocatable occsv
Definition: modmain.f90:901
real(8) engyvcl
Definition: modmain.f90:967
real(8), dimension(:), allocatable vclir
Definition: modmain.f90:627
integer nspinor
Definition: modmain.f90:269
real(8), parameter gfacte
Definition: modmain.f90:1277
real(8), dimension(3) afieldc
Definition: modmain.f90:327
real(8), dimension(:,:), allocatable bxcir
Definition: modmain.f90:639
complex(8), dimension(:,:,:,:,:), allocatable vmftm
Definition: moddftu.f90:91
real(8) engyc
Definition: modmain.f90:979
real(8) solsc
Definition: modmain.f90:1253
real(8) engymad
Definition: modmain.f90:969
complex(8), parameter zzero
Definition: modmain.f90:1240
real(8), dimension(:,:), allocatable vkl
Definition: modmain.f90:474
integer, dimension(maxspecies) natoms
Definition: modmain.f90:36
real(8) engyen
Definition: modmain.f90:961
integer, dimension(maxatoms *maxspecies) idxis
Definition: modmain.f90:44
integer stype
Definition: modmain.f90:887
integer dftu
Definition: moddftu.f90:36
real(8) engyts
Definition: modmain.f90:983
real(8) entrpy
Definition: modmain.f90:981
real(8) engyknc
Definition: modmain.f90:955
subroutine rfsht(nr, nri, rfmt1, rfmt2)
Definition: rfsht.f90:7
real(8) engyvsc
Definition: modmain.f90:951
real(8) engyhar
Definition: modmain.f90:963
real(8) engybxc
Definition: modmain.f90:973
logical tafsp
Definition: modmain.f90:331
real(8), dimension(maxspecies) spzn
Definition: modmain.f90:80
real(8) engycl
Definition: modmain.f90:965
integer npmtmax
Definition: modmain.f90:218
real(8), dimension(:,:), pointer, contiguous magir
Definition: modmain.f90:619
integer natmtot
Definition: modmain.f90:40
integer, dimension(maxspecies) nrcmt
Definition: modmain.f90:175
integer, dimension(maxspecies) nrcmti
Definition: modmain.f90:213
logical ncmag
Definition: modmain.f90:242
logical tafield
Definition: modmain.f90:324
real(8), dimension(:,:), pointer, contiguous vsmt
Definition: modmain.f90:652
real(8) hybridc
Definition: modmain.f90:1154
real(8), parameter y00
Definition: modmain.f90:1236
real(8), dimension(:), allocatable exir
Definition: modmain.f90:633
real(8) evalsmv
Definition: modmain.f90:949
real(8), dimension(3) bfieldc
Definition: modmain.f90:271
real(8) engyvsv
Definition: modmain.f90:953
real(8), dimension(:,:,:), pointer, contiguous bsmt
Definition: modmain.f90:659