The Elk Code
gndstate.f90
Go to the documentation of this file.
1 
2 ! Copyright (C) 2002-2013 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: gndstate
8 ! !INTERFACE:
9 subroutine gndstate
10 ! !USES:
11 use modmain
12 use moddftu
13 use modxcifc
14 use modulr
15 use modgw
16 use modmpi
17 use modomp
18 use modvars
19 use modramdisk
20 ! !DESCRIPTION:
21 ! Computes the self-consistent Kohn-Sham ground-state. General information is
22 ! written to the file {\tt INFO.OUT}. First- and second-variational
23 ! eigenvalues, eigenvectors and occupation numbers are written to the
24 ! unformatted files {\tt EVALFV.OUT}, {\tt EVALSV.OUT}, {\tt EVECFV.OUT},
25 ! {\tt EVECSV.OUT} and {\tt OCCSV.OUT}. The density, magnetisation, Kohn-Sham
26 ! potential and magnetic field are written to {\tt STATE.OUT}.
27 !
28 ! !REVISION HISTORY:
29 ! Created October 2002 (JKD)
30 ! Added MPI, August 2010 (JKD)
31 !EOP
32 !BOC
33 implicit none
34 ! local variables
35 logical trs,twrite
36 integer ik,iscl0,nmix,nwork
37 real(8) dv,etp,de,timetot
38 character(64) str
39 ! allocatable arrays
40 real(8), allocatable :: work(:)
41 ! initialise global variables
42 call init0
43 ! initialise k- and G+k-vector-dependent variables
44 call init1
45 ! initialise q-vector-dependent variables if required
46 if ((xctype(1) < 0).or.ksgwrho) call init2
47 ! initialise GW variables if required
48 if (ksgwrho) call init3
50 if (task == 0) trdstate=.false.
51 if (task == 1) trdstate=.true.
52 ! only the MPI master process should write files
53 if (mp_mpi) then
54 ! write the real and reciprocal lattice vectors to file
55  call writelat
56 ! write symmetry matrices to file
57  call writesym
58 ! output the k-point set to file
59  call writekpts
60 ! write lattice vectors and atomic positions to file
61  open(50,file='GEOMETRY'//trim(filext),form='FORMATTED')
62  call writegeom(50)
63  close(50)
64 ! write interatomic distances to file
65  open(50,file='IADIST'//trim(filext),form='FORMATTED')
66  call writeiad(50)
67  close(50)
68 ! open INFO.OUT file
69  open(60,file='INFO'//trim(filext),form='FORMATTED')
70 ! write out general information to INFO.OUT
71  call writeinfo(60)
72  write(60,*)
73 ! open TOTENERGY.OUT
74  open(61,file='TOTENERGY'//trim(filext),form='FORMATTED')
75 ! open FERMIDOS.OUT
76  open(62,file='FERMIDOS'//trim(filext),form='FORMATTED')
77 ! open MOMENT.OUT if required
78  if (spinpol) open(63,file='MOMENT'//trim(filext),form='FORMATTED')
79 ! open GAP.OUT
80  open(64,file='GAP'//trim(filext),form='FORMATTED')
81 ! open RMSDVS.OUT
82  open(65,file='RMSDVS'//trim(filext),form='FORMATTED')
83 ! open DTOTENERGY.OUT
84  open(66,file='DTOTENERGY'//trim(filext),form='FORMATTED')
85 ! open MOMENTM.OUT
86  if (spinpol) open(68,file='MOMENTM'//trim(filext),form='FORMATTED')
87 ! open RESIDUAL.OUT
88  if (xctype(1) < 0) open(69,file='RESIDUAL'//trim(filext),form='FORMATTED')
89 end if
90 iscl=0
91 if (trdstate) then
92 ! read the Kohn-Sham potential and fields from file
93  call readstate
94  if (mp_mpi) then
95  write(60,'("Potential read in from STATE.OUT")')
96  end if
97 else
98 ! initialise the density and magnetisation from atomic data
99  call rhoinit
100  if (spinpol) call maginit
101 ! compute the Kohn-Sham potential and magnetic field
102  call potks(.true.)
103  if (mp_mpi) then
104  write(60,'("Kohn-Sham potential initialised from atomic data")')
105  end if
106 end if
107 if (mp_mpi) flush(60)
108 call genvsig
109 ! size of mixing vector
110 nmix=size(vmixer)
111 ! determine the size of the mixer work array
112 nwork=-1
113 call mixerifc(mixtype,nmix,vmixer,dv,nwork,vmixer)
114 allocate(work(nwork))
115 ! initialise the mixer
116 iscl=0
117 call mixerifc(mixtype,nmix,vmixer,dv,nwork,work)
118 iscl0=1
119 if (mixsave.and.trdstate) then
120 ! read in starting loop and mixer work array from file if required
121  call readmix(trs,iscl,nwork,work)
122  if (trs) iscl0=min(iscl,mixsdb)
123 end if
124 ! set the stop signal to .false.
125 tstop=.false.
126 ! set last self-consistent loop flag
127 tlast=.false.
128 etp=0.d0
129 ! begin the self-consistent loop
130 if (mp_mpi) then
131  call writebox(60,"Self-consistent loop started")
132 end if
133 do iscl=iscl0,maxscl
134  if (mp_mpi) then
135  write(str,'("Loop number : ",I0)') iscl
136  call writebox(60,trim(str))
137  end if
138  if (iscl >= maxscl) then
139  if (mp_mpi) then
140  write(60,*)
141  write(60,'("Reached self-consistent loops maximum")')
142  end if
143  if (maxscl > 1) then
144  write(*,*)
145  write(*,'("Warning(gndstate): failed to reach self-consistency after ", &
146  &I0," loops")') iscl
147  end if
148  tlast=.true.
149  end if
150  if (mp_mpi) flush(60)
151 ! always write the eigenvectors to disk on the last loop
152  if (tlast) wrtdisk=.true.
153 ! generate the core wavefunctions and densities
154  call gencore
155 ! find the new linearisation energies
156  call linengy
157 ! write out the linearisation energies
158  if (mp_mpi) call writelinen
159 ! generate the APW and local-orbital radial functions and integrals
160  call genapwlofr
161 ! generate the spin-orbit coupling radial functions
162  call gensocfr
163 ! generate the first- and second-variational eigenvectors and eigenvalues
164  call genevfsv
165 ! find the occupation numbers and Fermi energy
166  call occupy
167  if (mp_mpi) then
168  if (autoswidth) then
169  write(60,*)
170  write(60,'("New smearing width : ",G18.10)') swidth
171  end if
172 ! write the occupation numbers to file
173  do ik=1,nkpt
174  call putoccsv(filext,ik,occsv(:,ik))
175  end do
176 ! write eigenvalues to file
177  call writeeval
178 ! write the Fermi energy to file
179  call writeefm
180  end if
181 ! synchronise MPI processes
182  call mpi_barrier(mpicom,ierror)
183 ! generate the density and magnetisation
184  if (ksgwrho) then
185 ! density calculated from the GW approximation
186  call gwrhomag
187  else
188 ! density calculated directly from the Kohn-Sham states
189  call rhomag
190  end if
191 ! DFT+U or fixed tensor moment calculation
192  if ((dftu /= 0).or.(ftmtype /= 0)) then
193 ! generate the muffin-tin density matrix used for computing the potential matrix
194  call gendmatmt
195 ! write the FTM tensor moments to file
196  if (ftmtype /= 0) call writeftm
197 ! generate the DFT+U or FTM muffin-tin potential matrices
198  call genvmatmt
199  end if
200  if (dftu /= 0) then
201  if (mp_mpi) then
202 ! write the DFT+U matrices to file
203  call writedftu
204 ! calculate and write tensor moments to file
205  if (tmwrite) call writetm3
206  end if
207  end if
208 ! compute the Kohn-Sham potential and magnetic field before potential mixing
209  if (.not.mixrho) call potks(.true.)
210 ! mix the old density/magnetisation or potential/field with the new
211  call mixerifc(mixtype,nmix,vmixer,dv,nwork,work)
212 ! compute the Kohn-Sham potential and magnetic field after density mixing
213  if (mixrho) call potks(.true.)
214 ! calculate and add the fixed spin moment effective field (after mixing)
215  call bfieldfsm
216  call addbfsm
217 ! Fourier transform Kohn-Sham potential to G-space
218  call genvsig
219 ! reduce the external magnetic fields if required
220  if (reducebf < 1.d0) then
221  bfieldc(:)=bfieldc(:)*reducebf
222  bfcmt(:,:,:)=bfcmt(:,:,:)*reducebf
223  end if
224 ! compute the paramagnetic current density and total current if required
225  if (tjr.and.tlast) then
226  call genjpr
227  call genjtot
228  end if
229 ! compute the energy components
230  call energy
231  if (mp_mpi) then
232 ! output energy components
233  call writeengy(60)
234  write(60,*)
235  write(60,'("Density of states at Fermi energy : ",G18.10)') fermidos
236  write(60,'(" (states/Hartree/unit cell)")')
237  write(60,*)
238  write(60,'("Estimated indirect band gap : ",G18.10)') bandgap(1)
239  write(60,'(" from k-point ",I0," to k-point ",I0)') ikgap(1),ikgap(2)
240  write(60,'("Estimated direct band gap : ",G18.10)') bandgap(2)
241  write(60,'(" at k-point ",I0)') ikgap(3)
242 ! write total energy to TOTENERGY.OUT
243  write(61,'(G24.14)') engytot
244  flush(61)
245 ! write DOS at Fermi energy to FERMIDOS.OUT
246  write(62,'(G18.10)') fermidos
247  flush(62)
248 ! output charges and moments
249  call writechg(60)
250  if (spinpol) then
251  call writemom(60)
252 ! write total moment to MOMENT.OUT
253  write(63,'(3G18.10)') momtot(1:ndmag)
254  flush(63)
255 ! write total moment magnitude to MOMENTM.OUT
256  write(68,'(G18.10)') momtotm
257  flush(68)
258  end if
259 ! write estimated Kohn-Sham indirect band gap
260  write(64,'(G24.14)') bandgap(1)
261  flush(64)
262 ! output effective fields for fixed spin moment calculations
263  if (fsmtype /= 0) call writefsm(60)
264 ! write the average electric field in each muffin-tin
265  if (tefield) call writeefield(60)
266 ! write the Tran-Blaha functional constant
267  if (xctype(2) == xc_mgga_x_tb09) then
268  write(60,*)
269  write(60,'("Tran-Blaha ''09 constant c : ",G18.10)') c_tb09
270  end if
271 ! check for existence of the WRITE file
272  call checkwrite(twrite)
273 ! check self-consistent loop number modulo nwrite
274  if (nwrite >= 1) then
275  if (mod(iscl,nwrite) == 0) twrite=.true.
276  end if
277 ! write STATE.OUT and mixer work array if required
278  if (twrite) then
279  call writestate
280  write(60,*)
281  write(60,'("Wrote STATE.OUT")')
282  if (mixsave) call writemix(nwork,work)
283  end if
284 ! write OEP step size and residual
285  if (xctype(1) < 0) then
286  write(60,*)
287  write(60,'("OEP iterative solver step size : ",G18.10)') tauoep
288  write(60,'("Magnitude of OEP residual : ",G18.10)') resoep
289  write(69,'(G18.10)') resoep
290  flush(69)
291  end if
292  end if
293 ! exit self-consistent loop if required
294  if (tlast) goto 10
295 ! check for convergence
296  if (iscl >= iscl0+1) then
297  de=abs(engytot-etp)
298  if (mp_mpi) then
299  write(60,*)
300  write(60,'("RMS change in Kohn-Sham potential (target) : ",G18.10," (",&
301  &G18.10,")")') dv,epspot
302  write(65,'(G18.10)') dv
303  flush(65)
304  write(60,'("Absolute change in total energy (target) : ",G18.10," (",&
305  &G18.10,")")') de,epsengy
306  write(66,'(G18.10)') de
307  flush(66)
308  if ((dv < epspot).and.(de < epsengy)) then
309  write(60,*)
310  write(60,'("Convergence targets achieved")')
311  tlast=.true.
312  end if
313  end if
314  end if
315 ! average the current and previous total energies and store
316  if (iscl == iscl0) etp=engytot
317  etp=0.75d0*engytot+0.25d0*etp
318 ! check for STOP file
319  call checkstop
320  if (tstop) tlast=.true.
321 ! broadcast tlast from master process to all other processes
322  call mpi_bcast(tlast,1,mpi_logical,0,mpicom,ierror)
323 ! output the current total CPU time
325  if (mp_mpi) then
326  write(60,*)
327  write(60,'("Time (CPU seconds) : ",F12.2)') timetot
328  end if
329 ! end the self-consistent loop
330 end do
331 10 continue
332 ! synchronise MPI processes
333 call mpi_barrier(mpicom,ierror)
334 if (mp_mpi) then
335  call writebox(60,"Self-consistent loop stopped")
336 ! write STATE.OUT and mixer work array only if maxscl > 1
337  if (maxscl > 1) then
338  call writestate
339  write(60,*)
340  write(60,'("Wrote STATE.OUT")')
341  if (mixsave) call writemix(nwork,work)
342  end if
343 end if
344 ! compute forces if required
345 if (tforce) then
346  call force
347 ! output forces to INFO.OUT
348  if (mp_mpi) call writeforces(60)
349 end if
350 ! output the paramagnetic current
351 if (tjr.and.mp_mpi) then
352  write(60,*)
353  write(60,'("Total paramagnetic current per unit cell")')
354  write(60,'(3G18.10)') jtot
355  write(60,'(" magnitude : ",G18.10)') jtotm
356 end if
357 ! total time used
359 if (mp_mpi) then
360 ! output timing information
361  write(60,*)
362  write(60,'("Timings (CPU seconds) :")')
363  write(60,'(" initialisation",T40,": ",F12.2)') timeinit
364  write(60,'(" Hamiltonian and overlap matrix set up",T40,": ",F12.2)') timemat
365  write(60,'(" first-variational eigenvalue equation",T40,": ",F12.2)') timefv
366  if (tevecsv) then
367  write(60,'(" second-variational calculation",T40,": ",F12.2)') timesv
368  end if
369  write(60,'(" charge density calculation",T40,": ",F12.2)') timerho
370  write(60,'(" potential calculation",T40,": ",F12.2)') timepot
371  if (tforce) then
372  write(60,'(" force calculation",T40,": ",F12.2)') timefor
373  end if
374  write(60,'(" total",T40,": ",F12.2)') timetot
375 ! close the INFO.OUT file
376  close(60)
377 ! close the TOTENERGY.OUT file
378  close(61)
379 ! close the FERMIDOS.OUT file
380  close(62)
381 ! close the MOMENT.OUT and MOMENTM.OUT files
382  if (spinpol) then
383  close(63); close(68)
384  end if
385 ! close the GAP.OUT file
386  close(64)
387 ! close the RMSDVS.OUT file
388  close(65)
389 ! close the DTOTENERGY.OUT file
390  close(66)
391 ! close the RESIDUAL.OUT file
392  if (xctype(1) < 0) close(69)
393 ! write to VARIABLES.OUT if required
394  if (wrtvars) call writegsvars
395 end if
396 deallocate(work)
398 ! synchronise MPI processes
399 call mpi_barrier(mpicom,ierror)
400 end subroutine
401 !EOC
402 
subroutine writemix(nwork, work)
Definition: writemix.f90:7
subroutine genevfsv
Definition: genevfsv.f90:7
logical tjr
Definition: modmain.f90:620
subroutine gndstate
Definition: gndstate.f90:10
integer mixtype
Definition: modmain.f90:695
real(8), dimension(3) momtot
Definition: modmain.f90:738
integer, dimension(3) ikgap
Definition: modmain.f90:913
character(256) filext
Definition: modmain.f90:1295
integer task
Definition: modmain.f90:1293
logical mp_mpi
Definition: modmpi.f90:17
real(8), dimension(:), pointer, contiguous vmixer
Definition: modmain.f90:689
subroutine occupy
Definition: occupy.f90:10
integer, dimension(3) xctype
Definition: modmain.f90:588
real(8), dimension(3) jtot
Definition: modmain.f90:748
real(8) resoep
Definition: modmain.f90:1144
logical spinpol
Definition: modmain.f90:228
integer nkpt
Definition: modmain.f90:461
real(8) reducebf
Definition: modmain.f90:279
integer ndmag
Definition: modmain.f90:238
logical wrtdisk
Definition: modramdisk.f90:15
real(8) momtotm
Definition: modmain.f90:740
subroutine writekpts
Definition: writekpts.f90:10
subroutine writeinfo(fnum)
Definition: writeinfo.f90:10
logical tevecsv
Definition: modmain.f90:917
real(8) tauoep
Definition: modmain.f90:1138
integer iscl
Definition: modmain.f90:1045
subroutine gencore
Definition: gencore.f90:10
Definition: modomp.f90:6
real(8) swidth
Definition: modmain.f90:891
logical wrtdisk0
Definition: modramdisk.f90:15
type(file_t), dimension(:), allocatable, private file
Definition: modramdisk.f90:29
logical tstop
Definition: modmain.f90:1049
real(8) fermidos
Definition: modmain.f90:909
subroutine writestate
Definition: writestate.f90:10
subroutine gendmatmt
Definition: gendmatmt.f90:7
real(8) timemat
Definition: modmain.f90:1211
logical mixrho
Definition: modmain.f90:687
subroutine rhomag
Definition: rhomag.f90:7
logical tmwrite
Definition: moddftu.f90:74
subroutine genvsig
Definition: genvsig.f90:10
subroutine readmix(trs, iscl0, nwork, work)
Definition: readmix.f90:7
real(8) c_tb09
Definition: modmain.f90:678
subroutine writeftm
Definition: writeftm.f90:7
subroutine genapwlofr
Definition: genapwlofr.f90:7
subroutine checkstop
Definition: checkstop.f90:7
real(8) timefor
Definition: modmain.f90:1221
subroutine gensocfr
Definition: gensocfr.f90:7
real(8) timerho
Definition: modmain.f90:1217
integer mixsdb
Definition: modmain.f90:704
subroutine linengy
Definition: linengy.f90:10
subroutine mixerifc(mtype, n, v, dv, nwork, work)
Definition: mixerifc.f90:7
logical tforce
Definition: modmain.f90:983
subroutine gwrhomag
Definition: gwrhomag.f90:7
real(8) timeinit
Definition: modmain.f90:1209
real(8) timepot
Definition: modmain.f90:1219
logical tefield
Definition: modmain.f90:310
subroutine potks(txc)
Definition: potks.f90:10
logical tlast
Definition: modmain.f90:1047
subroutine writefsm(fnum)
Definition: writefsm.f90:7
subroutine writelinen
Definition: writelinen.f90:10
subroutine energy
Definition: energy.f90:10
real(8) jtotm
Definition: modmain.f90:748
subroutine checkwrite(twrite)
Definition: checkwrite.f90:7
subroutine writetm3
Definition: writetm3.f90:10
integer ftmtype
Definition: moddftu.f90:78
real(8) engytot
Definition: modmain.f90:977
subroutine bfieldfsm
Definition: bfieldfsm.f90:10
subroutine init3
Definition: init3.f90:7
subroutine init2
Definition: init2.f90:7
subroutine genjtot
Definition: genjtot.f90:7
real(8), dimension(:,:), allocatable occsv
Definition: modmain.f90:901
subroutine writegeom(fnum)
Definition: writegeom.f90:10
subroutine writesym
Definition: writesym.f90:10
logical mixsave
Definition: modmain.f90:700
subroutine init1
Definition: init1.f90:10
real(8), dimension(3, maxatoms, maxspecies) bfcmt
Definition: modmain.f90:273
Definition: modgw.f90:6
integer nwrite
Definition: modmain.f90:1053
logical wrtvars
Definition: modvars.f90:9
subroutine writeiad(fnum)
Definition: writeiad.f90:10
Definition: modmpi.f90:6
subroutine writechg(fnum)
Definition: writechg.f90:7
integer dftu
Definition: moddftu.f90:35
real(8) epspot
Definition: modmain.f90:1055
subroutine force
Definition: force.f90:10
subroutine writebox(fnum, str)
Definition: writebox.f90:7
real(8) timefv
Definition: modmain.f90:1213
subroutine maginit
Definition: maginit.f90:7
subroutine readstate
Definition: readstate.f90:10
subroutine writeforces(fnum)
Definition: writeforces.f90:7
subroutine rhoinit
Definition: rhoinit.f90:10
subroutine writeefield(fnum)
Definition: writeefield.f90:7
real(8) timesv
Definition: modmain.f90:1215
logical trdstate
Definition: modmain.f90:682
logical autoswidth
Definition: modmain.f90:893
subroutine putoccsv(fext, ik, occsvp)
Definition: putoccsv.f90:7
subroutine init0
Definition: init0.f90:10
logical ksgwrho
Definition: modgw.f90:38
subroutine genjpr
Definition: genjpr.f90:7
Definition: modulr.f90:6
subroutine writedftu
Definition: writedftu.f90:7
subroutine writeeval
Definition: writeeval.f90:10
subroutine addbfsm
Definition: addbfsm.f90:7
real(8) epsengy
Definition: modmain.f90:1057
real(8), dimension(2) bandgap
Definition: modmain.f90:911
subroutine writegsvars
Definition: writegsvars.f90:7
subroutine writemom(fnum)
Definition: writemom.f90:7
integer maxscl
Definition: modmain.f90:1043
subroutine writelat
Definition: writelat.f90:7
integer mpicom
Definition: modmpi.f90:11
subroutine genvmatmt
Definition: genvmatmt.f90:10
subroutine writeefm
Definition: writeefm.f90:10
real(8), dimension(3) bfieldc
Definition: modmain.f90:269
integer fsmtype
Definition: modmain.f90:251
integer ierror
Definition: modmpi.f90:19
subroutine writeengy(fnum)
Definition: writeengy.f90:7