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) write(60,'("Potential read in from STATE.OUT")')
95 else
96 ! initialise the density and magnetisation from atomic data
97  call rhoinit
98  if (spinpol) call maginit
99 ! compute the Kohn-Sham potential and magnetic field
100  call potks(.true.)
101  if (mp_mpi) write(60,'("Kohn-Sham potential initialised from atomic data")')
102 end if
103 if (mp_mpi) flush(60)
104 call genvsig
105 ! size of mixing vector
106 nmix=size(vmixer)
107 ! determine the size of the mixer work array
108 nwork=-1
109 call mixerifc(mixtype,nmix,vmixer,dv,nwork,vmixer)
110 allocate(work(nwork))
111 ! initialise the mixer
112 iscl=0
113 call mixerifc(mixtype,nmix,vmixer,dv,nwork,work)
114 iscl0=1
115 if (mixsave.and.trdstate) then
116 ! read in starting loop and mixer work array from file if required
117  call readmix(trs,iscl,nwork,work)
118  if (trs) iscl0=min(iscl,mixsdb)
119 end if
120 ! set the stop signal to .false.
121 tstop=.false.
122 ! set last self-consistent loop flag
123 tlast=.false.
124 ! begin the self-consistent loop
125 if (mp_mpi) call writebox(60,"Self-consistent loop started")
126 do iscl=iscl0,maxscl
127  if (mp_mpi) then
128  write(str,'("Loop number : ",I0)') iscl
129  call writebox(60,trim(str))
130  end if
131  if (iscl >= maxscl) then
132  if (mp_mpi) then
133  write(60,*)
134  write(60,'("Reached self-consistent loops maximum")')
135  end if
136  if (maxscl > 1) then
137  write(*,*)
138  write(*,'("Warning(gndstate): failed to reach self-consistency after ", &
139  &I0," loops")') iscl
140  end if
141  tlast=.true.
142  end if
143  if (mp_mpi) flush(60)
144 ! always write the eigenvectors to disk on the last loop
145  if (tlast) wrtdisk=.true.
146 ! generate the core wavefunctions and densities
147  call gencore
148 ! find the new linearisation energies
149  call linengy
150 ! write out the linearisation energies
151  if (mp_mpi) call writelinen
152 ! generate the APW and local-orbital radial functions and integrals
153  call genapwlofr
154 ! generate the spin-orbit coupling radial functions
155  call gensocfr
156 ! generate the first- and second-variational eigenvectors and eigenvalues
157  call genevfsv
158 ! find the occupation numbers and Fermi energy
159  call occupy
160  if (mp_mpi) then
161  if (autoswidth) then
162  write(60,*)
163  write(60,'("New smearing width : ",G18.10)') swidth
164  end if
165 ! write the occupation numbers to file
166  do ik=1,nkpt
167  call putoccsv(filext,ik,occsv(:,ik))
168  end do
169 ! write eigenvalues to file
170  call writeeval
171 ! write the Fermi energy to file
172  call writeefm
173  end if
174 ! synchronise MPI processes
175  call mpi_barrier(mpicom,ierror)
176 ! generate the density and magnetisation
177  if (ksgwrho) then
178 ! density calculated from the GW approximation
179  call gwrhomag
180  else
181 ! density calculated directly from the Kohn-Sham states
182  call rhomag
183  end if
184 ! DFT+U or fixed tensor moment calculation
185  if ((dftu /= 0).or.(ftmtype /= 0)) then
186 ! generate the muffin-tin density matrix used for computing the potential matrix
187  call gendmatmt
188 ! write the FTM tensor moments to file
189  if (ftmtype /= 0) call writeftm
190 ! generate the DFT+U or FTM muffin-tin potential matrices
191  call genvmatmt
192  end if
193  if (dftu /= 0) then
194  if (mp_mpi) then
195 ! write the DFT+U matrices to file
196  call writedftu
197 ! calculate and write tensor moments to file
198  if (tmwrite) call writetm3
199  end if
200  end if
201 ! compute the Kohn-Sham potential and magnetic field before potential mixing
202  if (.not.mixrho) call potks(.true.)
203 ! mix the old density/magnetisation or potential/field with the new
204  call mixerifc(mixtype,nmix,vmixer,dv,nwork,work)
205 ! compute the Kohn-Sham potential and magnetic field after density mixing
206  if (mixrho) call potks(.true.)
207 ! calculate and add the fixed spin moment effective field (after mixing)
208  call bfieldfsm
209  call addbfsm
210 ! Fourier transform Kohn-Sham potential to G-space
211  call genvsig
212 ! reduce the external magnetic fields if required
213  if (reducebf < 1.d0) then
214  bfieldc(:)=bfieldc(:)*reducebf
215  bfcmt(:,:,:)=bfcmt(:,:,:)*reducebf
216  end if
217 ! compute the paramagnetic current density and total current if required
218  if (tjr.and.tlast) then
219  call genjpr
220  call genjtot
221  end if
222 ! compute the energy components
223  call energy
224  if (iscl > iscl0) then
225 ! change in energy between loops
226  de=abs(engytot-etp)
227 ! average the current and previous energies
228  etp=(3.d0*engytot+etp)/4.d0
229  else
230  etp=engytot
231  end if
232  if (mp_mpi) then
233 ! output energy components
234  call writeengy(60)
235  write(60,*)
236  write(60,'("Density of states at Fermi energy : ",G18.10)') fermidos
237  write(60,'(" (states/Hartree/unit cell)")')
238  write(60,*)
239  write(60,'("Estimated indirect band gap : ",G18.10)') bandgap(1)
240  write(60,'(" from k-point ",I0," to k-point ",I0)') ikgap(1),ikgap(2)
241  write(60,'("Estimated direct band gap : ",G18.10)') bandgap(2)
242  write(60,'(" at k-point ",I0)') ikgap(3)
243 ! write total energy to TOTENERGY.OUT
244  write(61,'(G24.14)') engytot
245  flush(61)
246 ! write DOS at Fermi energy to FERMIDOS.OUT
247  write(62,'(G18.10)') fermidos
248  flush(62)
249 ! output charges and moments
250  call writechg(60)
251  if (spinpol) then
252  call writemom(60)
253 ! write total moment to MOMENT.OUT
254  write(63,'(3G18.10)') momtot(1:ndmag)
255  flush(63)
256 ! write total moment magnitude to MOMENTM.OUT
257  write(68,'(G18.10)') momtotm
258  flush(68)
259  end if
260 ! write estimated Kohn-Sham indirect band gap
261  write(64,'(G24.14)') bandgap(1)
262  flush(64)
263 ! output effective fields for fixed spin moment calculations
264  if (fsmtype /= 0) call writefsm(60)
265 ! write the average electric field in each muffin-tin
266  if (tefield) call writeefield(60)
267 ! write the Tran-Blaha functional constant
268  if (xctype(2) == xc_mgga_x_tb09) then
269  write(60,*)
270  write(60,'("Tran-Blaha ''09 constant c : ",G18.10)') c_tb09
271  end if
272 ! check for existence of the WRITE file
273  call checkwrite(twrite)
274 ! check self-consistent loop number modulo nwrite
275  if (nwrite >= 1) then
276  if (mod(iscl,nwrite) == 0) twrite=.true.
277  end if
278 ! write STATE.OUT and mixer work array if required
279  if (twrite) then
280  call writestate
281  write(60,*)
282  write(60,'("Wrote STATE.OUT")')
283  if (mixsave) call writemix(nwork,work)
284  end if
285 ! write OEP step size and residual
286  if (xctype(1) < 0) then
287  write(60,*)
288  write(60,'("OEP iterative solver step size : ",G18.10)') tauoep
289  write(60,'("Magnitude of OEP residual : ",G18.10)') resoep
290  write(69,'(G18.10)') resoep
291  flush(69)
292  end if
293  end if
294 ! exit self-consistent loop if required
295  if (tlast) goto 10
296 ! check for convergence
297  if (iscl > iscl0) then
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 ! check for STOP file
316  call checkstop
317  if (tstop) tlast=.true.
318 ! broadcast tlast from master process to all other processes
319  call mpi_bcast(tlast,1,mpi_logical,0,mpicom,ierror)
320 ! output the current total CPU time
322  if (mp_mpi) then
323  write(60,*)
324  write(60,'("Time (CPU seconds) : ",F12.2)') timetot
325  end if
326 ! end the self-consistent loop
327 end do
328 10 continue
329 ! synchronise MPI processes
330 call mpi_barrier(mpicom,ierror)
331 if (mp_mpi) then
332  call writebox(60,"Self-consistent loop stopped")
333 ! write STATE.OUT and mixer work array only if maxscl > 1
334  if (maxscl > 1) then
335  call writestate
336  write(60,*)
337  write(60,'("Wrote STATE.OUT")')
338  if (mixsave) call writemix(nwork,work)
339  end if
340 end if
341 ! compute forces if required
342 if (tforce) then
343  call force
344 ! output forces to INFO.OUT
345  if (mp_mpi) call writeforces(60)
346 end if
347 ! output the paramagnetic current
348 if (tjr.and.mp_mpi) then
349  write(60,*)
350  write(60,'("Total paramagnetic current per unit cell")')
351  write(60,'(3G18.10)') jtot
352  write(60,'(" magnitude : ",G18.10)') jtotm
353 end if
354 ! total time used
356 if (mp_mpi) then
357 ! output timing information
358  write(60,*)
359  write(60,'("Timings (CPU seconds) :")')
360  write(60,'(" initialisation",T40,": ",F12.2)') timeinit
361  write(60,'(" Hamiltonian and overlap matrix set up",T40,": ",F12.2)') timemat
362  write(60,'(" first-variational eigenvalue equation",T40,": ",F12.2)') timefv
363  if (tevecsv) then
364  write(60,'(" second-variational calculation",T40,": ",F12.2)') timesv
365  end if
366  write(60,'(" charge density calculation",T40,": ",F12.2)') timerho
367  write(60,'(" potential calculation",T40,": ",F12.2)') timepot
368  if (tforce) then
369  write(60,'(" force calculation",T40,": ",F12.2)') timefor
370  end if
371  write(60,'(" total",T40,": ",F12.2)') timetot
372 ! close the INFO.OUT file
373  close(60)
374 ! close the TOTENERGY.OUT file
375  close(61)
376 ! close the FERMIDOS.OUT file
377  close(62)
378 ! close the MOMENT.OUT and MOMENTM.OUT files
379  if (spinpol) then
380  close(63); close(68)
381  end if
382 ! close the GAP.OUT file
383  close(64)
384 ! close the RMSDVS.OUT file
385  close(65)
386 ! close the DTOTENERGY.OUT file
387  close(66)
388 ! close the RESIDUAL.OUT file
389  if (xctype(1) < 0) close(69)
390 ! write to VARIABLES.OUT if required
391  if (wrtvars) call writegsvars
392 end if
393 deallocate(work)
395 ! synchronise MPI processes
396 call mpi_barrier(mpicom,ierror)
397 end subroutine
398 !EOC
399 
subroutine writemix(nwork, work)
Definition: writemix.f90:7
subroutine genevfsv
Definition: genevfsv.f90:7
logical tjr
Definition: modmain.f90:623
subroutine gndstate
Definition: gndstate.f90:10
integer mixtype
Definition: modmain.f90:698
real(8), dimension(3) momtot
Definition: modmain.f90:741
integer, dimension(3) ikgap
Definition: modmain.f90:913
character(256) filext
Definition: modmain.f90:1301
integer task
Definition: modmain.f90:1299
logical mp_mpi
Definition: modmpi.f90:17
real(8), dimension(:), pointer, contiguous vmixer
Definition: modmain.f90:692
subroutine occupy
Definition: occupy.f90:10
integer, dimension(3) xctype
Definition: modmain.f90:591
real(8), dimension(3) jtot
Definition: modmain.f90:751
real(8) resoep
Definition: modmain.f90:1150
logical spinpol
Definition: modmain.f90:230
integer nkpt
Definition: modmain.f90:464
real(8) reducebf
Definition: modmain.f90:281
integer ndmag
Definition: modmain.f90:240
logical wrtdisk
Definition: modramdisk.f90:15
real(8) momtotm
Definition: modmain.f90:743
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:1144
integer iscl
Definition: modmain.f90:1053
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:1057
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:1217
logical mixrho
Definition: modmain.f90:690
subroutine rhomag
Definition: rhomag.f90:7
logical tmwrite
Definition: moddftu.f90:75
subroutine genvsig
Definition: genvsig.f90:10
subroutine readmix(trs, iscl0, nwork, work)
Definition: readmix.f90:7
real(8) c_tb09
Definition: modmain.f90:681
subroutine writeftm
Definition: writeftm.f90:7
subroutine genapwlofr
Definition: genapwlofr.f90:7
subroutine checkstop
Definition: checkstop.f90:7
real(8) timefor
Definition: modmain.f90:1227
subroutine gensocfr
Definition: gensocfr.f90:10
real(8) timerho
Definition: modmain.f90:1223
integer mixsdb
Definition: modmain.f90:707
subroutine linengy
Definition: linengy.f90:10
subroutine mixerifc(mtype, n, v, dv, nwork, work)
Definition: mixerifc.f90:7
logical tforce
Definition: modmain.f90:991
subroutine gwrhomag
Definition: gwrhomag.f90:7
real(8) timeinit
Definition: modmain.f90:1215
real(8) timepot
Definition: modmain.f90:1225
logical tefield
Definition: modmain.f90:312
subroutine potks(txc)
Definition: potks.f90:10
logical tlast
Definition: modmain.f90:1055
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:751
subroutine checkwrite(twrite)
Definition: checkwrite.f90:7
subroutine writetm3
Definition: writetm3.f90:10
integer ftmtype
Definition: moddftu.f90:79
real(8) engytot
Definition: modmain.f90:985
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:703
subroutine init1
Definition: init1.f90:10
real(8), dimension(3, maxatoms, maxspecies) bfcmt
Definition: modmain.f90:275
Definition: modgw.f90:6
integer nwrite
Definition: modmain.f90:1061
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:36
real(8) epspot
Definition: modmain.f90:1063
subroutine force
Definition: force.f90:10
subroutine writebox(fnum, str)
Definition: writebox.f90:7
real(8) timefv
Definition: modmain.f90:1219
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:1221
logical trdstate
Definition: modmain.f90:685
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:1065
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:1051
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:271
integer fsmtype
Definition: modmain.f90:253
integer ierror
Definition: modmpi.f90:19
subroutine writeengy(fnum)
Definition: writeengy.f90:7