~ubuntu-branches/ubuntu/utopic/nwchem/utopic

« back to all changes in this revision

Viewing changes to .pc/09_backported_6.1.1_fixes.patch/src/nwdft/so_dft/dft_scf_so.F

  • Committer: Package Import Robot
  • Author(s): Michael Banck, Daniel Leidert, Andreas Tille, Michael Banck
  • Date: 2013-07-04 12:14:55 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130704121455-5tvsx2qabor3nrui
Tags: 6.3-1
* New upstream release.
* Fixes anisotropic properties (Closes: #696361).
* New features include:
  + Multi-reference coupled cluster (MRCC) approaches
  + Hybrid DFT calculations with short-range HF 
  + New density-functionals including Minnesota (M08, M11) and HSE hybrid
    functionals
  + X-ray absorption spectroscopy (XAS) with TDDFT
  + Analytical gradients for the COSMO solvation model
  + Transition densities from TDDFT 
  + DFT+U and Electron-Transfer (ET) methods for plane wave calculations
  + Exploitation of space group symmetry in plane wave geometry optimizations
  + Local density of states (LDOS) collective variable added to Metadynamics
  + Various new XC functionals added for plane wave calculations, including
    hybrid and range-corrected ones
  + Electric field gradients with relativistic corrections 
  + Nudged Elastic Band optimization method
  + Updated basis sets and ECPs 

[ Daniel Leidert ]
* debian/watch: Fixed.

[ Andreas Tille ]
* debian/upstream: References

[ Michael Banck ]
* debian/upstream (Name): New field.
* debian/patches/02_makefile_flags.patch: Refreshed.
* debian/patches/06_statfs_kfreebsd.patch: Likewise.
* debian/patches/07_ga_target_force_linux.patch: Likewise.
* debian/patches/05_avoid_inline_assembler.patch: Removed, no longer needed.
* debian/patches/09_backported_6.1.1_fixes.patch: Likewise.
* debian/control (Build-Depends): Added gfortran-4.7 and gcc-4.7.
* debian/patches/10_force_gcc-4.7.patch: New patch, explicitly sets
  gfortran-4.7 and gcc-4.7, fixes test suite hang with gcc-4.8 (Closes:
  #701328, #713262).
* debian/testsuite: Added tests for COSMO analytical gradients and MRCC.
* debian/rules (MRCC_METHODS): New variable, required to enable MRCC methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
c
2
 
c    Main spin-orbit DFT driver
3
 
c
4
 
      logical function dft_scf_so
5
 
     &                 (rtdb, Etold, Enuc, iVcoul_opt, iVxc_opt, 
6
 
     &                  iter, g_dens, g_dens_at, g_movecs, g_vxc, 
7
 
     &                  g_fock, g_svecs, svals, g_xcinv, g_s)
8
 
c     
9
 
c     $Id: dft_scf_so.F 21279 2011-10-24 03:13:09Z niri $
10
 
c     
11
 
      implicit none
12
 
#include "errquit.fh"
13
 
c     
14
 
      integer rtdb              ! [input]
15
 
      double precision Etold, Enuc, trace
16
 
      integer iVcoul_opt
17
 
      integer iVxc_opt
18
 
      integer iter, swap(20),nswap, ndet, idet
19
 
      integer g_dens(2), g_movecs(2), g_vxc(4), 
20
 
     &     g_fock, g_svecs,
21
 
     &     g_xcinv,  g_scr
22
 
      integer g_dens_at(2)
23
 
      double precision  svals(*)
24
 
c     so
25
 
      integer g_densso(2), g_tmp_ri,   
26
 
     &     g_moso(2), g_old(2),
27
 
     &     g_fockso(2), g_scr2, g_damp_so(2), g_gmovecs(2)
28
 
c
29
 
      integer la, ia            ! complex*16 a(nbf_ao, nbf_ao)
30
 
      integer lw, iw            ! double precision w(nbf_ao)
31
 
      integer llwork 
32
 
      integer lwork, iwork      ! complex*16 work(3) 
33
 
      integer lrwork, irwork    ! double precision rwork
34
 
      integer info 
35
 
      integer lbuff, ibuff
36
 
      integer nbf_mo 
37
 
c      logical numerical 
38
 
      integer g_s
39
 
c
40
 
c     declarations for fractional occupation 
41
 
c
42
 
      integer nmo_fon  ! number of fractionally occupied orbitals
43
 
      integer ncore_fon ! number of fully occupied orbitals
44
 
      double precision nel_fon  ! fractional electron number
45
 
      double precision avg_fon  ! fractional occupancy (averaged)
46
 
      integer nTotOcc           ! nTotOcc: no. of (occupied maybe f.o.) mo's 
47
 
      logical fon 
48
 
      integer ntmp_fon(2)
49
 
      double precision rtmp_fon(2), pstrace
50
 
      double precision scale, ncheck
51
 
      integer kfon_occ, lfon_occ
52
 
      logical debug_fon, det_eng 
53
 
      integer iswap,jswap
54
 
c
55
 
c     so
56
 
      double precision rho_n, toll_s
57
 
c     
58
 
c     == zora related ==
59
 
      double precision ener_scal, ener_kin
60
 
      double precision numelecs
61
 
      integer ncanorg
62
 
      logical ldmix
63
 
      character*7 vecs_or_dens
64
 
      integer icalczora
65
 
      logical do_purescalar
66
 
c
67
 
c     == vdw contrib ==
68
 
      double precision dum
69
 
      logical xc_chkdispauto
70
 
      external xc_chkdispauto
71
 
      logical disp
72
 
c
73
 
#include "bas.fh"
74
 
#include "geom.fh"
75
 
#include "mafdecls.fh"
76
 
#include "stdio.fh"
77
 
#include "rtdb.fh"
78
 
#include "cdft.fh"
79
 
#include "global.fh"
80
 
#include "msgids.fh"
81
 
#include "util.fh"
82
 
#include "zora.fh"  ! zora contribution
83
 
#include "case.fh"  ! coulomb attenuation
84
 
c     
85
 
      Logical movecs_write_so, movecs_converged
86
 
      External movecs_write_so, movecs_converged
87
 
c     
88
 
      Logical movecs_read_header_so, movecs_read_so
89
 
      External movecs_read_header_so, movecs_read_so 
90
 
c
91
 
      Logical spinor_guess
92
 
      External spinor_guess
93
 
      integer  ga_create_atom_blocked
94
 
      external ga_create_atom_blocked
95
 
c     
96
 
      logical oprint, status
97
 
      double precision Exc(2), rms(2), derr(2)
98
 
      integer nmo(2), icall(2)
99
 
c      integer nva
100
 
      integer n3c_dbl, n3c_int, n_batch
101
 
      integer iwhat_max
102
 
      integer l_3cwhat, k_3cwhat, l_3cERI, k_3cERI
103
 
      integer dft_n3cint, n_semi_bufs, fd
104
 
      external dft_n3cint
105
 
      double precision dft_n3cdbl
106
 
      external dft_n3cdbl
107
 
      Integer l_eval
108
 
      integer k_eval(2)
109
 
      integer natoms, nTotEl
110
 
 
111
 
      integer l_occ, k_occ
112
 
      integer i, j,  i1, jstart 
113
 
      integer me, nproc
114
 
      integer g_tmp, g_fockt, g_wght, g_xyz,g_nq
115
 
c     so
116
 
      integer g_so(3)
117
 
c     so
118
 
      integer nheap, nstack
119
 
      integer ispin, idone
120
 
      integer nexc
121
 
      integer iswitc
122
 
      integer itol_max, iaoacc_max
123
 
      integer itol_min, iAOacc_min
124
 
      double precision tol_rho_min, tol_rho_max
125
 
      integer npol
126
 
      integer leneval, lcd_coef, icd_coef
127
 
      integer lcntoce, icntoce, lcntobfr, icntobfr,
128
 
     &     lcetobfr, icetobfr, lrdens_atom, irdens_atom,
129
 
     &     nscr, lscr, iscr
130
 
      double precision start_wall, current_wall, elapsed_wall,
131
 
     &     save_wall, current_cpu, start_cpu,
132
 
     &     wall_time_reqd
133
 
      integer int_wall_time_reqd
134
 
      double precision ecoul, ecore, noso
135
 
      double precision pp, delta
136
 
      double precision anucl_charg, anel
137
 
      double precision anoca, anocb, onempp
138
 
      double precision etnew, tol2e, tol2e_sleazy
139
 
c     convergence declarations
140
 
      double precision rlshift_input, rlshift_def
141
 
      integer ndamp_input, ndamp_def
142
 
c     
143
 
c     Note, damping, levelshifting, and diising logicals
144
 
c     are used to turn on/off these procedures per
145
 
c     iteration.  The alternative logicals nodamping, 
146
 
c     nolevelshifting, and nodiis are specified and held
147
 
c     for the entire convergence sequence.
148
 
c     
149
 
      logical diising, damping, levelshifting
150
 
      logical keep_damp_on,keep_levl_on, keep_diis_on
151
 
      Logical  IOLGC, mulliken
152
 
      logical converged, wght_GA
153
 
c      logical oconverged 
154
 
      logical oprint_parm, oprint_conv, oprint_vecs, 
155
 
     &     oprint_eval, oprint_syma, oprint_time, 
156
 
     &     oprint_info, oprint_tol, oprint_final_vecs, 
157
 
     &     oprint_energy_step, oprint_intermediate_fock,
158
 
     &     oprint_3c2e, oprint_interm_overlap, oprint_interm_S2,
159
 
     &     oprint_conv_details
160
 
      double precision zero, onem, one, mone
161
 
      parameter(zero = 0.d0, one = 1.d0, mone=-1.0d0, onem = -one)
162
 
c     
163
 
      integer ilo, ihi          ! For printing movecs analysis
164
 
      double precision eval_pr_tol_lo, eval_pr_tol_hi
165
 
      parameter (eval_pr_tol_lo = -1.5d0, eval_pr_tol_hi=0.5)
166
 
C     
167
 
c     
168
 
c     early convergence tolerances
169
 
c     
170
 
      parameter(itol_min = 7, iAOacc_min = 12, tol_rho_min = 1.d-7)
171
 
c     
172
 
      double precision dft_dencvg, dft_time
173
 
      external dft_dencvg
174
 
      double precision homo, lumo, homo_lumo_gap
175
 
      integer l_ir, k_ir
176
 
      logical last_time_energy
177
 
      logical check_shift, lmaxov_sv
178
 
      character*7 name
179
 
      character*4 scftype
180
 
      character*255 basis_name, basis_trans
181
 
      integer nopen, nclosed
182
 
c     !!! BGJ
183
 
      logical cphf_poliz, do_poliz
184
 
      external cphf_poliz
185
 
c     !!! BGJ
186
 
      character*255 title1       ! Returns title of job that created vectors
187
 
      character*255 basis_name1  ! Returns name of basis set
188
 
      character*255 scftype1     ! Returns the SCF type of the vectors
189
 
      integer nbf1               ! Returns no. of functions in basis
190
 
      integer g_oep
191
 
c     integer ijk
192
 
c     
193
 
c     == zora related ==
194
 
      logical dft_zora_read_so, dft_zora_write_so
195
 
      external dft_zora_read_so, dft_zora_write_so
196
 
 
197
 
      logical dft_zora_inquire_file_so
198
 
      external dft_zora_inquire_file_so
199
 
 
200
 
      character*255 zorafilename
201
 
      integer g_zora_sf(2)
202
 
      integer g_zora_scale_sf(2)
203
 
      integer g_zora_so(3)
204
 
      integer g_zora_scale_so(3)
205
 
      double precision Ezora_sf
206
 
      integer switch_sclMO_so ! switch =1,0 ON,OFF sclMO-FA-02-18-11
207
 
c
208
 
      logical spinor
209
 
      logical dft_mem3c
210
 
      external dft_mem3c
211
 
      external dft_scaleMO_so ! FA-occupations from input script 02-15-11
212
 
c
213
 
      icd_coef = 0
214
 
      k_3cERI  = 0
215
 
      k_3cwhat = 0
216
 
      me = ga_nodeid()
217
 
      nproc = ga_nnodes()
218
 
c
219
 
c     to do scalar relativistic calculations within the two-component framework
220
 
c
221
 
      do_purescalar = .false.
222
 
      if (.not.rtdb_get(rtdb,'sodft:scalar',mt_log,1,do_purescalar))
223
 
     &     do_purescalar = .false.
224
 
c
225
 
      if (me.eq.0.and.do_purescalar) then
226
 
         call util_print_centered(LuOut,
227
 
     $        'Neglecting spin-orbit terms', 23, .true.)
228
 
         write(LuOut,*)
229
 
       endif ! me
230
 
c
231
 
      call ecce_print_module_entry('dft')
232
 
      dft_scf_so = .false.
233
 
      nbf_mo = 2*nbf_ao
234
 
      lmaxov_sv = lmaxov
235
 
      oprint = util_print('information', print_low)
236
 
      oprint_info = util_print('common', print_debug)
237
 
      oprint_parm = util_print('parameters', print_default)
238
 
      oprint_3c2e = util_print('3c 2e integrals', print_default)
239
 
      oprint_conv = util_print('convergence', print_default)
240
 
      oprint_conv_details = util_print('convergence details', 
241
 
     &     print_high)
242
 
      oprint_vecs = util_print('intermediate vectors', print_high)
243
 
      oprint_eval = util_print('intermediate evals', print_high)
244
 
      oprint_syma = util_print('interm vector symm', print_high)
245
 
      oprint_time = util_print('dft timings', print_high)
246
 
      oprint_tol = util_print('screening parameters', print_high)
247
 
      oprint_energy_step = util_print('intermediate energy info',
248
 
     &     print_high)
249
 
      oprint_intermediate_fock = util_print('intermediate fock matrix',
250
 
     &     print_high)
251
 
      oprint_interm_S2 = util_print('intermediate S2',print_high)
252
 
      oprint_interm_overlap = util_print('intermediate overlap',
253
 
     &     print_high)
254
 
      oprint_final_vecs = util_print('final vectors', print_high)
255
 
c
256
 
      ispin=1
257
 
      call int_1e_uncache_ga()      
258
 
c     !!! BGJ
259
 
c     Store SCF hamiltonian type as DFT for use in BGJ routines
260
 
      if (.not. rtdb_put(rtdb, 'bgj:scf_type', MT_INT, 1, 2))
261
 
     $     call errquit('dft_scf_so: put of bgj:scf_type failed',0,
262
 
     &       RTDB_ERR)
263
 
c     !!! BGJ
264
 
c     
265
 
c     see if levelshifting monitoring is desired
266
 
c     
267
 
      if (.not. rtdb_get(rtdb, 'dft:check_shift', mt_log, 1,
268
 
     &     check_shift))then
269
 
         check_shift = .false.      
270
 
      endif
271
 
c     
272
 
      if (.not. geom_ncent(geom, natoms))
273
 
     &     call errquit('dft_scf_so: geom_ncent failed',73, GEOM_ERR)
274
 
      if (.not. geom_nuc_charge(geom, anucl_charg))
275
 
     &     call errquit('dft_scf_so: geom_nuc_charge failed', 
276
 
     & 0, GEOM_ERR)
277
 
c     
278
 
      anel = int(anucl_charg) - rcharge
279
 
c     
280
 
c     Pre-compute mapping vectors
281
 
c     
282
 
      if (.not.ma_push_get
283
 
     &     (mt_int,nshells_ao,'cntoce map',lcntoce,icntoce))
284
 
     &     call errquit('dft_scf_so:push_get failed', 13, MA_ERR)
285
 
      if (.not.ma_push_get
286
 
     &     (mt_int,nshells_ao*2,'cntoce map',lcntobfr,icntobfr))
287
 
     &     call errquit('dft_scf_so:push_get failed', 13, MA_ERR)
288
 
      if (.not.ma_push_get
289
 
     &     (mt_int,natoms*2,'cntoce map',lcetobfr,icetobfr))
290
 
     &     call errquit('dft_scf_so:push_get failed', 13, MA_ERR)
291
 
c     
292
 
      call build_maps(ao_bas_han, int_mb(icntoce), int_mb(icntobfr), 
293
 
     &     int_mb(icetobfr), natoms, nshells_ao)
294
 
c     
295
 
c     Set aside some memory for reduced density matrix
296
 
c     
297
 
      if (.not.MA_Push_Get(MT_Dbl,2*natoms*natoms,'rdens_atom',
298
 
     &     lrdens_atom,irdens_atom))
299
 
     &     call errquit('dft_scf_so: cannot allocate rdens_atom',
300
 
     & 0, MA_ERR)
301
 
c     
302
 
c     determine pattern of orbitals' occupancy
303
 
c     
304
 
      if (.not. MA_Push_Get(MT_Dbl,nbf_ao*2,'mo occ',l_occ,k_occ))
305
 
     &     call errquit('dft_scf_so: failed to alloc',999, MA_ERR)
306
 
c     
307
 
c     get orbital overlap tolerance
308
 
c     
309
 
      if (.not. rtdb_get(rtdb, 'dft:toll_s', MT_DBL, 1, toll_s))
310
 
     .     call errquit('dft_scf_so: lost toll_s ',0, RTDB_ERR)
311
 
c     
312
 
      nTotEl = noc(1) + noc(2)
313
 
      nmo(1) = nbf_ao
314
 
      nmo(2) = nbf_ao
315
 
c     
316
 
      anoca = noc(1)
317
 
      anocb = noc(2)
318
 
c     
319
 
c     UHF occupations
320
 
c     
321
 
      call dfill(nbf_mo, 0.0d0, dbl_mb(k_occ), 1)
322
 
      do i = 1, noc(1)
323
 
         dbl_mb(i-1+k_occ) = 1.0d0
324
 
      enddo
325
 
      do i = nbf_ao+1, nbf_ao+noc(2)
326
 
         dbl_mb(i-1+k_occ) = 1.0d0
327
 
      enddo
328
 
c     
329
 
      wght_GA = .false.
330
 
c     
331
 
c     Determine whether to fit the electronic charge density.
332
 
c     
333
 
      CDFIT = .FALSE.
334
 
      if (iVcoul_opt.eq.1)CDFIT = .TRUE.
335
 
      XCFIT = .FALSE.
336
 
      if (iVxc_opt.eq.1)XCFIT = .TRUE.
337
 
c
338
 
c     Define various constants.
339
 
c     
340
 
      npol = (ipol*(ipol+1))/2
341
 
c     
342
 
      itol_max = itol2e
343
 
      iaoacc_max = iaoacc
344
 
      tol_rho_max = tol_rho
345
 
      if (oprint_time)
346
 
     &     call dft_tstamp(' Before 3c-2e initialize.')
347
 
c     
348
 
      if (CDFIT)then
349
 
         if(dft_mem3c(
350
 
     I     natoms,npol,oprint_parm,oprint_3c2e,
351
 
     O     n3c_int,n3c_dbl,n_semi_bufs,
352
 
     O     l_3ceri,k_3ceri, l_3cwhat,k_3cwhat)) then
353
 
            call dft_3cincor(n_batch, n3c_int, int_mb(k_3cwhat), 
354
 
     &                       dbl_mb(k_3cERI), n3c_dbl, iwhat_max, 
355
 
     &                       n_semi_bufs, fd)
356
 
            incore=.true.
357
 
         else
358
 
            if (me.eq.0 .and. oprint_3c2e)write(LuOut,3230)
359
 
            incore=.false.
360
 
         endif
361
 
      endif
362
 
 3230 format(/,10x,'Incore memory use for 3-center 2e- integrals is ',
363
 
     &     'turned off. ')
364
 
      mulliken = .false.
365
 
      if (imull.eq.1)mulliken = .true.
366
 
      IOLGC = .TRUE.
367
 
      if (noio.eq.1)IOLGC = .FALSE.
368
 
c     
369
 
c     Energy decomposition switch
370
 
c     
371
 
      nExc    = idecomp + 1
372
 
      Etnew = 0.d0
373
 
c     
374
 
c     SCF energy convergence criterion. 
375
 
c     
376
 
      g_tmp = ga_create_atom_blocked(geom, AO_bas_han, 'ga_temp')
377
 
      g_fockt = ga_create_atom_blocked(geom, AO_bas_han, 'fock tr')
378
 
c     
379
 
c     Set up local convergence parameters
380
 
c     
381
 
      diising = diis
382
 
      damping = damp
383
 
      levelshifting = levelshift
384
 
      keep_damp_on = .false.
385
 
      keep_levl_on = .false.
386
 
      keep_diis_on = .false.
387
 
      ndamp_input = ndamp
388
 
      rlshift_input = rlshift
389
 
      ndamp_def = 0
390
 
      rlshift_def = 0.0
391
 
      rlshift = rlshift_def
392
 
c     
393
 
      if (nodamping)damping = .false.
394
 
      if (nolevelshifting) then 
395
 
         levelshifting = .false.
396
 
         rlshift = rlshift_def
397
 
      endif
398
 
      if (nodiis) then
399
 
         diising = .false.
400
 
      endif
401
 
      if (ncydp.ne.0)then
402
 
         damping = .true. 
403
 
         ndamp = ndamp_input
404
 
      endif
405
 
      if (ncysh.ne.0)then
406
 
         levelshifting = .true.
407
 
         rlshift = rlshift_input
408
 
      endif
409
 
      if (ncyds.ne.0)then
410
 
         diising = .true.
411
 
      endif
412
 
c     
413
 
c     Initialize DIIS call counter.
414
 
c     
415
 
      icall(1) = 0
416
 
      icall(2) = 0
417
 
c     
418
 
c     Begin the SCF cycle.
419
 
c     
420
 
c     allocate eigenvalue array
421
 
c     
422
 
      leneval = 4*nbf_ao 
423
 
      if (.not.MA_Push_Get(MT_Dbl,leneval,'eval',l_eval,k_eval(1)))
424
 
     &     call errquit('dft_scf_so: cannot allocate eval',0, MA_ERR)
425
 
      k_eval(2) = k_eval(1) + nbf_mo
426
 
c     
427
 
c     Dump DFT parameters (if debugging) to see if they make sense
428
 
c     
429
 
      if (me.eq.0.and.oprint_info)call dft_dump_info(me)
430
 
c     
431
 
c     Get initial density.
432
 
c     
433
 
      if (oprint_time)
434
 
     &     call dft_tstamp(' Before call to DFT_INIT.')
435
 
      scftype = 'UHF'
436
 
c     
437
 
c     allocate array for irreps
438
 
c     
439
 
      if (.not.MA_Push_Get(mt_int,2*nbf_ao,'dft:irreps',l_ir,k_ir))
440
 
     &     call errquit('dft_scf_so: cannot allocate irreps',0, MA_ERR)
441
 
      nopen = mult - 1
442
 
      nclosed = (nTotEl - nopen) / 2
443
 
c     
444
 
      if (.not. bas_name(ao_bas_han, basis_name, basis_trans))
445
 
     $     call errquit('dft_scf_so: bas_name?', 0, BASIS_ERR)
446
 
c     
447
 
c     get info for int2e_ and set sleazy tolerance
448
 
c     
449
 
      tol2e_sleazy = 1.d-3
450
 
      call scf_get_fock_param(rtdb, tol2e_sleazy)
451
 
c     
452
 
c     Force sleazy SCF into "direct" mode.
453
 
c     
454
 
      call fock_force_direct(rtdb)
455
 
cso
456
 
cso   allocate Fock matrix and movecs 
457
 
cso
458
 
c
459
 
c     real molecular orbital vectors
460
 
      if(.not.ga_create(mt_dbl,nbf_mo,nbf_mo,'Movecs Re',0,0, 
461
 
     &     g_moso(1)))     
462
 
     &     call errquit('dft_scf_so: error creating Movecs Re',0,
463
 
     &       GA_ERR)
464
 
      if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'Movecs Re',0,0, 
465
 
     &     g_gmovecs(1)))     
466
 
     &     call errquit('dft_scf_so: error creating Movecs Re',0,
467
 
     &       GA_ERR)
468
 
c
469
 
c     imaginary molecular orbital vectors
470
 
      if(.not.ga_create(mt_dbl,nbf_mo, nbf_mo,'Movecs Im',0,0, 
471
 
     &     g_moso(2)))
472
 
     &     call errquit('dft_scf_so: error creating Movecs Im',0,
473
 
     &       GA_ERR)
474
 
      if(.not.ga_create(mt_dbl,nbf_ao, nbf_ao,'Movecs Im',0,0, 
475
 
     &     g_gmovecs(2)))
476
 
     &     call errquit('dft_scf_so: error creating Movecs Im',0,
477
 
     &       GA_ERR)
478
 
c
479
 
      call ga_zero(g_moso(1))
480
 
      call ga_zero(g_moso(2))
481
 
      call ga_zero(g_gmovecs(1))
482
 
      call ga_zero(g_gmovecs(2))
483
 
c
484
 
      call ga_sync() 
485
 
c
486
 
c     real part of the fock matrix
487
 
      if(.not.ga_create(mt_dbl,nbf_mo,nbf_mo,'Fock Re',0,0, 
488
 
     &     g_fockso(1)))
489
 
     &     call errquit('dft_scf_so: error creating Fock Re',0, GA_ERR)
490
 
      call ga_zero(g_fockso(1))
491
 
c
492
 
c     imaginary part of the fock matrix
493
 
      if(.not.ga_create(mt_dbl,nbf_mo,nbf_mo,'Fock Im',0,0, 
494
 
     &     g_fockso(2)))
495
 
     &     call errquit('dft_scf_so: error creating Fock Im',0, GA_ERR)
496
 
      call ga_zero(g_fockso(2))
497
 
c
498
 
c     extra arrays
499
 
      if(.not.ga_create(mt_dbl,nbf_mo,nbf_mo,'old re',0,0, 
500
 
     &     g_old(1)))
501
 
     &     call errquit('dft_scf_so: error creating Old Re',0, GA_ERR)
502
 
      call ga_zero(g_old(1))
503
 
      if(.not.ga_create(mt_dbl,nbf_mo,nbf_mo,'old Im',0,0, 
504
 
     &     g_old(2)))
505
 
     &     call errquit('dft_scf_so: error creating Old Im',0, GA_ERR)
506
 
      call ga_zero(g_old(2))
507
 
c
508
 
c     real part of the spin-orbit density matrix
509
 
      if(.not.ga_create(mt_dbl,nbf_mo,nbf_mo,'DenMx Re',0,0, 
510
 
     &     g_densso(1)))
511
 
     &     call errquit('dft_scf_so: error creating DenMx Re',0, GA_ERR)
512
 
      call ga_zero(g_densso(1))
513
 
c
514
 
c     imaginary part of the spin-orbit density matrix
515
 
      if(.not.ga_create(mt_dbl,nbf_mo,nbf_mo,'DenMx Im',0,0, 
516
 
     &     g_densso(2)))
517
 
     &     call errquit('dft_scf_so: error creating DenMx Im',0, GA_ERR)
518
 
      call ga_zero(g_densso(2))
519
 
c
520
 
c     extra arrays
521
 
      if(.not.ga_create(mt_dbl,nbf_mo,nbf_mo,'Tmp ReIm',0,0, 
522
 
     &     g_tmp_ri))
523
 
     &     call errquit('dft_scf_so: error creating Tmp ReIm',0, GA_ERR)
524
 
      call ga_zero(g_tmp_ri)
525
 
c
526
 
c     spin-orbit matrices: 1->z, 2->y, 3->x
527
 
      if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'so z',0,0, 
528
 
     &     g_so(1)))
529
 
     &     call errquit('dft_scf_so: error creating so z',0, GA_ERR)
530
 
      call ga_zero(g_so(1))
531
 
      if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'so y',0,0, 
532
 
     &     g_so(2)))
533
 
     &     call errquit('dft_scf_so: error creating so y',0, GA_ERR)
534
 
      call ga_zero(g_so(2))
535
 
      if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'so x',0,0, 
536
 
     &     g_so(3)))
537
 
     &     call errquit('dft_scf_so: error creating so x',0, GA_ERR)
538
 
      call ga_zero(g_so(3))
539
 
c
540
 
c     extra arrays
541
 
      if(.not.ga_create(mt_dbl, 2*nbf, 2*nbf,'old den', 0, 0, 
542
 
     &     g_damp_so(1)))
543
 
     &     call errquit('dft_scf_so: error creating damp ga', 0, GA_ERR)
544
 
      call ga_zero(g_damp_so(1))
545
 
      if(.not.ga_create(mt_dbl, 2*nbf, 2*nbf,'old den', 0, 0, 
546
 
     &     g_damp_so(2)))
547
 
     &     call errquit('dft_scf_so: error creating damp ga', 0, GA_ERR)
548
 
      call ga_zero(g_damp_so(2))
549
 
c
550
 
c     == zora arrays ==
551
 
      if (do_zora) then
552
 
       if (me.eq.0) then
553
 
         call util_print_centered(LuOut,
554
 
     $        'Performing ZORA calculations', 23, .true.)
555
 
         write(LuOut,*)
556
 
       endif ! me
557
 
c
558
 
c      == get filename for zora data ==
559
 
       call util_file_name('zora_so',.false.,.false.,zorafilename)
560
 
c
561
 
c      == zora: scalar arrays ==
562
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_sf',0,0,
563
 
     &    g_zora_sf(1)))
564
 
     &    call errquit('dft_scf_so: error creating g_zora_sf',0, 
565
 
     &       GA_ERR)
566
 
       call ga_zero(g_zora_sf(1))
567
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_sf',0,0,
568
 
     &    g_zora_sf(2)))
569
 
     &    call errquit('dft_scf_so: error creating g_zora_sf',0, 
570
 
     &       GA_ERR)
571
 
       call ga_zero(g_zora_sf(2))
572
 
c
573
 
c      == zora: scalar energy scaling arrays ==
574
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_scale_sf',0,0,
575
 
     &    g_zora_scale_sf(1)))
576
 
     & call errquit('dft_scf_so: error creating g_zora_scale_sf',0, 
577
 
     &    GA_ERR)
578
 
       call ga_zero(g_zora_scale_sf(1))
579
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_scale_sf',0,0,
580
 
     &    g_zora_scale_sf(2)))
581
 
     & call errquit('dft_scf_so: error creating g_zora_scale_sf',0, 
582
 
     &    GA_ERR)
583
 
       call ga_zero(g_zora_scale_sf(2))
584
 
c
585
 
c      == zora: spin-orbit arrays ==
586
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_so 1',0,0,
587
 
     &    g_zora_so(1)))
588
 
     & call errquit('dft_scf_so: error creating g_zora_so 1',0, GA_ERR)
589
 
       call ga_zero(g_zora_so(1))
590
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_so 2',0,0,
591
 
     & g_zora_so(2)))
592
 
     & call errquit('dft_scf_so: error creating g_zora_so 2',0, GA_ERR)
593
 
       call ga_zero(g_zora_so(2))
594
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_so 3',0,0,
595
 
     & g_zora_so(3)))
596
 
     & call errquit('dft_scf_so: error creating g_zora_so 3',0, GA_ERR)
597
 
       call ga_zero(g_zora_so(3))
598
 
c
599
 
c      == zora: spin-orbit energy scaling arrays ==
600
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_scale_so 1',0,0,
601
 
     & g_zora_scale_so(1)))
602
 
     & call errquit('dft_scf_so: error creating g_zora_scale_so 1',0, 
603
 
     &    GA_ERR)
604
 
       call ga_zero(g_zora_scale_so(1))
605
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_scale_so 2',0,0,
606
 
     & g_zora_scale_so(2)))
607
 
     & call errquit('dft_scf_so: error creating g_zora_scale_so 2',0, 
608
 
     &    GA_ERR)
609
 
       call ga_zero(g_zora_scale_so(2))
610
 
       if(.not.ga_create(mt_dbl,nbf_ao,nbf_ao,'g_zora_scale_so 3',0,0,
611
 
     &   g_zora_scale_so(3)))
612
 
     &   call errquit('dft_scf_so: error creating g_zora_scale_so',0, 
613
 
     &    GA_ERR)
614
 
       call ga_zero(g_zora_scale_so(3))
615
 
c
616
 
c      == generate an superposition of atomic densities ==
617
 
       call ga_zero(g_dens_at(1))
618
 
       if (ipol.gt.1) call ga_zero(g_dens_at(2))
619
 
       call guess_dens(rtdb, geom, ao_bas_han, g_dens_at)
620
 
       if (oskel) call ga_symmetrize(g_dens_at(1))
621
 
       if(ipol.gt.1) then
622
 
            call ga_copy(g_dens_at(1),g_dens_at(2))
623
 
            call ga_dscal(g_dens_at(1),dble(ntotel-nclosed)/(ntotel))
624
 
            call ga_dscal(g_dens_at(2),dble(nclosed)/(ntotel))
625
 
            if(oskel) call ga_symmetrize(g_dens_at(2))
626
 
       end if
627
 
c
628
 
c       == in case fon is used together with zora ==
629
 
c       == pstrace is queried in the grid code ==
630
 
c
631
 
       fon = .false.
632
 
       if (rtdb_get(rtdb,'dft:fon',mt_log,1,fon)) then 
633
 
         pstrace=ga_ddot(g_dens_at(1),g_s)
634
 
         pstrace=pstrace + ga_ddot(g_dens_at(2),g_s)
635
 
         if(ga_nodeid().eq.0) write (luout,'(5x,a,1x,e15.7)')
636
 
     &      'tr(P*S): ',pstrace
637
 
         if (.not. rtdb_put(rtdb, 'dft:pstrace', mt_dbl, 1, pstrace))
638
 
     &     call errquit('dft_scf: rtdb_put pstrace failed', 1, RTDB_ERR)
639
 
       end if ! fon check
640
 
c
641
 
c      == try reading the zora contributions from file ==
642
 
       icalczora = 0
643
 
       if (.not.dft_zora_read_so(zorafilename, nbf_ao, ipol, nmo, mult,
644
 
     &     g_zora_sf, g_zora_scale_sf, g_zora_so, g_zora_scale_so)) 
645
 
     &     icalczora = 1
646
 
647
 
       if (icalczora.eq.1) then
648
 
        if (me.eq.0) then
649
 
         call util_print_centered(LuOut,
650
 
     $        'Generating atomic ZORA corrections', 23, .true.)
651
 
         write(LuOut,*)
652
 
        end if ! me = 0
653
 
c
654
 
c       == calculate the zora atomic corrections ==
655
 
        call zora_getv_so(rtdb, g_dens_at, g_zora_sf, g_zora_scale_sf,
656
 
     &                      g_zora_so, g_zora_scale_so, nexc)
657
 
c
658
 
c       == write out the atomic zora corrections to file ==
659
 
        if (.not.dft_zora_write_so(rtdb, ao_bas_han, zorafilename,
660
 
     &    nbf_ao, ipol, nmo, mult, g_zora_sf, g_zora_scale_sf, 
661
 
     &    g_zora_so, g_zora_scale_so))
662
 
     &    call errquit('dft_scf_so: dft_zora_write_so failed', 0, 
663
 
     &            DISK_ERR)
664
 
c
665
 
       end if ! icalczora
666
 
c
667
 
c       == for scalar calculations via the 2 component formalism
668
 
        if (do_purescalar) then 
669
 
            call ga_zero(g_zora_so(1))
670
 
            call ga_zero(g_zora_so(2))
671
 
            call ga_zero(g_zora_so(3))
672
 
            call ga_zero(g_zora_scale_so(1))
673
 
            call ga_zero(g_zora_scale_so(2))
674
 
            call ga_zero(g_zora_scale_so(3))
675
 
        end if
676
 
c
677
 
      end if  ! do_zora
678
 
c
679
 
c     check for fon input
680
 
c
681
 
      fon = .false.
682
 
      if (rtdb_get(rtdb,'dft:fon',mt_log,1,fon)) then 
683
 
c
684
 
c       variable 'fon' should be true, otherwise there's 
685
 
c       something fishy going on:
686
 
        if (.not.fon) call errquit(
687
 
     &     'dft_scf_so: fon stored in RTDB but not .true.', 1,
688
 
     &       RTDB_ERR)
689
 
 
690
 
c       note: *_fon variables are read here not as arrays with
691
 
c       two elements, assuming that the input didn't specify
692
 
c       spects for alpha and beta separately
693
 
 
694
 
        if (.not.rtdb_get(rtdb,'dft:nmo_fon',mt_int,2,ntmp_fon)) then
695
 
          if (me.eq.0) then
696
 
            write(LuOut,*)"Error: fractional occupation 
697
 
     &         calculation specified without setting number of orbitals"
698
 
          end if
699
 
          call errquit('dft_scf_so: nmo_fon rtdb_get failed', 1,
700
 
     &       RTDB_ERR)
701
 
        else
702
 
          nmo_fon = ntmp_fon(1)
703
 
        end if
704
 
        if (.not.rtdb_get(rtdb,'dft:nel_fon',mt_dbl,2,rtmp_fon)) then
705
 
          if (me.eq.0) then
706
 
            write(LuOut,*)"Error: fractional occupation 
707
 
     &         specified without setting number of electrons"  
708
 
          end if
709
 
          call errquit('dft_scf_so: nel_fon rtdb_get failed', 1,
710
 
     &       RTDB_ERR)
711
 
        else
712
 
          nel_fon = rtmp_fon(1)
713
 
        end if
714
 
        if (.not.rtdb_get(rtdb,'dft:ncore_fon',mt_int,2,ntmp_fon)) then
715
 
          if (me.eq.0) then
716
 
            write(LuOut,*)"Error: fractional occupation 
717
 
     &         calculation specified without setting filled levels"  
718
 
          end if
719
 
          call errquit('dft_scf_so:  nel_core rtdb_get failed', 1,
720
 
     &       RTDB_ERR)
721
 
        else
722
 
          ncore_fon = ntmp_fon(1)
723
 
        end if
724
 
        if (rtdb_get(rtdb, 'dft:debugfon', mt_log, 1,
725
 
     &     debug_fon)) continue
726
 
 
727
 
      else ! keyword not in RTDB
728
 
        fon = .false.
729
 
        debug_fon = .false.
730
 
      end if ! fon
731
 
c
732
 
      call ga_sync()
733
 
      call ga_zero(g_densso(1))
734
 
      call ga_zero(g_densso(2))
735
 
      spinor = .false. 
736
 
      spinor=spinor_guess(movecs_in)
737
 
c
738
 
      if(.not.spinor)then 
739
 
        
740
 
c       fractional occupations:
741
 
        if (fon) then
742
 
c ... jochen: presumably good enough for initial guess. We won't tinker
743
 
c         with that. later, the fractional occupations
744
 
c         are caluclated explicitly
745
 
          anoca = noc(1)
746
 
          anocb = noc(2)
747
 
          noc(1)=(noc(1)+noc(2)-nel_fon)/2
748
 
          noc(2)=(noc(1)+noc(2)-nel_fon)/2
749
 
        endif                   ! fon
750
 
c
751
 
         call dft_guessin(movecs_in,ldmix,ncanorg,fon,
752
 
     &     vecs_or_dens, ipol,nbf_ao,g_movecs,g_gmovecs,
753
 
     &     toll_s,svals)
754
 
c
755
 
         call scf_vectors_guess(rtdb, tol2e_sleazy, geom, ao_bas_han, 
756
 
     &        basis_trans, movecs_in, movecs_out, 
757
 
     &        movecs_guess, scftype, nclosed, nopen, 
758
 
     &        nbf, nmo, noc(1), noc(2),  k_eval, k_occ, 
759
 
     &        k_ir, g_gmovecs, g_dens, 'density', 
760
 
     &        'dft', title, oskel, oadapt, 
761
 
     &        .true.) 
762
 
c
763
 
         call dft_guessout(nmo,nbf_ao,g_gmovecs,g_movecs,ipol)
764
 
c
765
 
c        fon: undo temp setting of noc(:)
766
 
         if (fon)then 
767
 
           noc(1)=anoca
768
 
           noc(2)=anocb
769
 
         endif
770
 
c     
771
 
c     spinor occupancies
772
 
c     
773
 
         call dfill(nbf_mo, 0.0d0, dbl_mb(k_occ), 1)
774
 
         do i = 1, nTotEl
775
 
            dbl_mb(i-1+k_occ) = 1.0d0
776
 
         enddo
777
 
c     
778
 
c     map initial guess movecs from spin-free calculations g_moso(1) 
779
 
c     noc(1).ge.noc(2) is assumed
780
 
c         
781
 
         do i=1,min(noc(1),noc(2))
782
 
            call ga_dadd_patch(1.d0,g_movecs(1),1,nbf_ao,i,i, 
783
 
     $           0.d0,g_moso(1),1,nbf_ao,2*(i-1)+1,2*(i-1)+1,
784
 
     $           g_moso(1),1,nbf_ao,2*(i-1)+1,2*(i-1)+1) 
785
 
            call ga_dadd_patch(1.d0,g_movecs(2),1,nbf_ao,i,i, 
786
 
     $           0.d0,g_moso(1),1+nbf_ao,nbf_mo,2*(i-1)+2,2*(i-1)+2,
787
 
     $           g_moso(1),1+nbf_ao,nbf_mo,2*(i-1)+2,2*(i-1)+2)
788
 
         enddo
789
 
         do i=noc(2)+1,noc(1)
790
 
            call ga_dadd_patch(1.d0,g_movecs(1),1,nbf_ao,i,i, 
791
 
     $           0.d0,g_moso(1),1,nbf_ao,noc(2)+i,noc(2)+i,
792
 
     $           g_moso(1),1,nbf_ao,noc(2)+i,noc(2)+i) 
793
 
         enddo
794
 
         do i=noc(2)+1,noc(1)
795
 
            call ga_dadd_patch(1.d0,g_movecs(2),1,nbf_ao,i,i, 
796
 
     $           0.d0,g_moso(1),1+nbf_ao,nbf_mo,noc(1)+i,noc(1)+i,
797
 
     $           g_moso(1),1+nbf_ao,nbf_mo,noc(1)+i,noc(1)+i) 
798
 
         enddo
799
 
         do i=noc(1)+1,nbf_ao
800
 
            call ga_dadd_patch(1.d0,g_movecs(1),1,nbf_ao,i,i, 
801
 
     $           0.d0,g_moso(1),1,nbf_ao,2*(i-1)+1,2*(i-1)+1,
802
 
     $           g_moso(1),1,nbf_ao,2*(i-1)+1,2*(i-1)+1) 
803
 
            call ga_dadd_patch(1.d0,g_movecs(2),1,nbf_ao,i,i, 
804
 
     $           0.d0,g_moso(1),1+nbf_ao,nbf_mo,2*(i-1)+2,2*(i-1)+2,
805
 
     $           g_moso(1),1+nbf_ao,nbf_mo,2*(i-1)+2,2*(i-1)+2)
806
 
         enddo
807
 
      endif  !if not spinor
808
 
 
809
 
      if(spinor)then 
810
 
c     
811
 
c     read spinors from files 
812
 
c     
813
 
c     get MO vectors from file
814
 
c     
815
 
c         if (.not. rtdb_cget(rtdb, 'dft:input vectors', 1, movecs_in))
816
 
c     $        call errquit('dft_scf_so: DFT MO vectors not defined',0)
817
 
         status = movecs_read_header_so(movecs_in, title1, basis_name1,
818
 
     $        scftype1, nbf1)
819
 
c     
820
 
c     Should check much more info than just nbf for consistency
821
 
c     
822
 
c     
823
 
c     get mo eigevectors
824
 
c     
825
 
         if (2*nbf_ao .ne. nbf1)then
826
 
            write(6,*)'dft_scf_so movecs output = ',movecs_in
827
 
            call errquit('dft_scf_so: could not read mo vectors',911,
828
 
     &       DISK_ERR)
829
 
         else 
830
 
            status = .true.
831
 
            status = status .and.
832
 
     $           movecs_read_so(movecs_in, dbl_mb(k_occ),
833
 
     $           dbl_mb(k_eval(1)), g_moso)
834
 
         endif
835
 
c     
836
 
         if (.not.status)then
837
 
            write(6,*)'dft_scf_so movecs output = ',movecs_in
838
 
            call errquit('dft_scf_so: could not read mo vectors',917,
839
 
     &       DISK_ERR)
840
 
         endif
841
 
c     
842
 
         call movecs_swap_so(rtdb,'dft',scftype,g_moso,
843
 
     &        dbl_mb(k_occ),dbl_mb(k_eval(1)))
844
 
      endif  !spinor
845
 
c     
846
 
c     Form Re and Im of density matrix
847
 
c     
848
 
c ... jochen 10/11: implemented new way of applying 
849
 
c     fractional occupations. See dft_densm.F of the scalar 
850
 
c     branch of the code
851
 
 
852
 
c$$$      if (.not.rtdb_get(rtdb,'sodft:fon',mt_log,1,fon))
853
 
c$$$     &    fon = .false.
854
 
c$$$      if (.not.rtdb_get(rtdb,'sodft:nmo_fon',mt_int,1,nmo_fon))
855
 
c$$$     &    nmo_fon = 0
856
 
c$$$      if (.not.rtdb_get(rtdb,'sodft:nel_fon',mt_int,1,nel_fon))
857
 
c$$$     &    nel_fon = 0
858
 
c$$$      nTotOcc = (nTotEl-nel_fon) + nmo_fon 
859
 
c
860
 
      nTotOcc = nTotEl  ! if there is no fractional occupation 
861
 
      if(fon)then 
862
 
c$$$         avg_fon = dble(nel_fon)/dble(nmo_fon) 
863
 
c$$$         do i = (nTotEl-nel_fon)+1, nTotOcc  
864
 
c$$$            dbl_mb(i-1+k_occ) = avg_fon 
865
 
c$$$         enddo
866
 
 
867
 
        if (nmo_fon.lt.1) call errquit(
868
 
     &     'dft_scf_so:fon nmo_fon <1',
869
 
     &     1, INPUT_ERR)
870
 
        if (nel_fon.lt.0d0) call errquit(
871
 
     &     'dft_scf_so:fon nel_fon <0',
872
 
     &     1, INPUT_ERR)
873
 
 
874
 
        avg_fon = nel_fon/dble(nmo_fon)
875
 
        nTotOcc = ncore_fon + nmo_fon 
876
 
 
877
 
c       debug code: 
878
 
c$$$        write (luout,*) 'DEBUG: occupations before applying FON'
879
 
c$$$        do i = 1,nbf_ao
880
 
c$$$          write (luout,*) i, dbl_mb(i-1+k_occ), dbl_mb(i-1+nbf_ao+k_occ)
881
 
c$$$        end do
882
 
 
883
 
        ncheck = 0d0
884
 
        do i = 1, ncore_fon
885
 
          if (i> 2*nbf_ao) call errquit(
886
 
     &       'dft_densm:fon focc index exceeds 2nbf error 1',
887
 
     &       i, INPUT_ERR)
888
 
          dbl_mb(i-1+k_occ) = 1d0
889
 
          ncheck = ncheck + 1d0
890
 
        end do
891
 
        do i = ncore_fon + 1, ncore_fon + nmo_fon
892
 
          if (i> 2*nbf_ao) call errquit(
893
 
     &       'dft_densm:fon focc index exceeds 2nbf error 2',
894
 
     &       i, INPUT_ERR)
895
 
          dbl_mb(i-1+k_occ) = avg_fon
896
 
          ncheck = ncheck + avg_fon
897
 
        end do
898
 
 
899
 
        if(abs(ncheck-dble(nTotEl)).gt.1d-3 .and. me.eq.0) then
900
 
          write(luout,*) ' frac. electrons ',ncheck,' vs ',nTotEl
901
 
        end if
902
 
 
903
 
c$$$        write (luout,*) 'DEBUG: occupations AFTER applying FON'
904
 
c$$$        do i = 1,nbf_ao
905
 
c$$$          write (luout,*) i, dbl_mb(i-1+k_occ), dbl_mb(i-1+nbf_ao+k_occ)
906
 
c$$$        end do
907
 
      endif ! fon
908
 
c
909
 
      switch_sclMO_so=0 ! FA-09-26-11
910
 
c     
911
 
c     the fractionally occupied mo's are scaled by the sqrt of the fon
912
 
c     
913
 
      if (fon) then 
914
 
         do i = ncore_fon + 1, ncore_fon + nmo_fon
915
 
           if (i> nbf_mo) call errquit(
916
 
     &        'dft_densm:fon g_moso index exceeds nbf_mo',
917
 
     &        i, INPUT_ERR)
918
 
           scale = sqrt(dbl_mb(i-1+k_occ)) ! sqrt(occupation number)
919
 
           call ga_scale_patch(g_moso(1), 
920
 
     &        1, nbf_mo, i, i, scale)
921
 
           call ga_scale_patch(g_moso(2), 
922
 
     &        1, nbf_mo, i, i, scale)
923
 
         end do
924
 
         if(me.eq.0) write(luout,'(5x,a)')  'FON applied'
925
 
       end if ! fon
926
 
c      else
927
 
c ---- FA-02-18-11 : occupations keyword ---- START
928
 
c       call dft_scaleMO_so(rtdb,g_moso,dbl_mb(k_occ),g_densso,
929
 
c     &                     nbf_mo,nTotOcc,switch_sclMO_so)
930
 
c      endif
931
 
c
932
 
c     calculate the spin-orbit density matrix using the scaled mo's
933
 
934
 
c       if (switch_sclMO_so.ne.1) then
935
 
c         if (ga_nodeid().eq.0)
936
 
c     &   write(*,*) 'ENTER calc so-density matrix std-way'
937
 
         call dft_densm_so(g_densso, g_moso, nbf_ao, nTotOcc)
938
 
c       endif
939
 
c
940
 
c     restore the scaled mo's
941
 
c
942
 
      if (fon) then 
943
 
         do i = ncore_fon + 1, ncore_fon + nmo_fon
944
 
           if (i> nbf_mo) call errquit(
945
 
     &        'dft_densm:fon g_moso index exceeds nbf_mo',
946
 
     &        i, INPUT_ERR)
947
 
           if (dbl_mb(i-1+k_occ) < 1d-4) call errquit(
948
 
     &        'dft_densm:fon frac occup < 1E-4. Aborting suspisciously',
949
 
     &        i, INPUT_ERR)           
950
 
           scale = 1d0/sqrt(dbl_mb(i-1+k_occ)) ! 1/sqrt(occupation number)
951
 
           call ga_scale_patch(g_moso(1), 
952
 
     &        1, nbf_mo, i, i, scale)
953
 
           call ga_scale_patch(g_moso(2), 
954
 
     &        1, nbf_mo, i, i, scale)
955
 
         end do
956
 
       end if ! fon
957
 
c      
958
 
c     calculate the spin-free density matrix from the spin-orbit density matrix
959
 
c
960
 
      call ga_zero(g_dens(1))
961
 
      call ga_zero(g_dens(2))
962
 
      call ga_dens_sf(g_dens, g_densso(1), nbf_ao)
963
 
 
964
 
c     check fon, calculate tr[P S] and print
965
 
c      if (debug_fon) call dft_pstrace(g_dens,ao_bas_han,nbf_ao,oskel)
966
 
      if (fon) then
967
 
        pstrace=ga_ddot(g_dens(1),g_s)
968
 
        pstrace=pstrace + ga_ddot(g_dens(2),g_s)
969
 
        if(ga_nodeid().eq.0) write (luout,'(5x,a,1x,e15.7)')
970
 
     &     'tr(P*S): ',pstrace
971
 
        if (.not. rtdb_put(rtdb, 'dft:pstrace', mt_dbl, 1, pstrace))
972
 
     &     call errquit('dft_scf: rtdb_put pstrace failed', 1, RTDB_ERR)
973
 
      end if
974
 
c
975
 
c     Tidy up SCF
976
 
c     
977
 
      call fock_2e_tidy(rtdb)
978
 
c     
979
 
c     set initial coulomb acc
980
 
c     
981
 
c     write(6,*)' movecs_guess = ',movecs_guess
982
 
      if (movecs_guess.eq.'restart')ltight=.true.
983
 
c     
984
 
c     May not want levelshifting initially until sure that the
985
 
c     transformed Fock matrix will be diagonally dominant, or
986
 
c     alternatively shift the piss out of it.
987
 
c     
988
 
      if (movecs_guess.eq.'restart')then
989
 
         levelshifting = .true.
990
 
      else
991
 
         levelshifting = .false.
992
 
 
993
 
c     rlshift = 2.0
994
 
      endif
995
 
      iswitc = 0
996
 
      if (ltight)then
997
 
         itol2e = itol_max
998
 
         iAOacc = iAOacc_max
999
 
         tol_rho = tol_rho_max
1000
 
         iswitc = 1
1001
 
      else
1002
 
         itol2e = min(itol_min,itol_max)
1003
 
         iAOacc = min(iAOacc_min,iAOacc_max)
1004
 
         tol_rho = max(tol_rho_min,tol_rho_max)
1005
 
      endif
1006
 
c     
1007
 
      tol2e = 10.d0**(-itol_max)
1008
 
c     
1009
 
c     Restore SCF parameters
1010
 
c     
1011
 
      call scf_get_fock_param(rtdb, tol2e)
1012
 
c     
1013
 
c     If open shell put the total density matrix in g_dens(1)
1014
 
c     
1015
 
      call ga_dadd(one,g_dens(1),one,g_dens(2),g_dens(1))
1016
 
c
1017
 
c     
1018
 
c     Call to Mulliken Pop Analysis for initial density
1019
 
c     
1020
 
      if (mulliken)then
1021
 
         if (me.eq.0)call dft_header
1022
 
     &        (' Total Density - Mulliken Population Analysis')
1023
 
         call mull_pop(geom,ao_bas_han,g_dens(1),g_s,'total')
1024
 
c     
1025
 
c     analysis of spin density
1026
 
c     
1027
 
         if (me.eq.0) call dft_header
1028
 
     &        (' Spin Density - Mulliken Population Analysis')
1029
 
         call ga_dadd(one,g_dens(1),-2.d0,g_dens(2),g_dens(2))
1030
 
         call mull_pop(geom,ao_bas_han,g_dens(2),g_s,'spin') 
1031
 
c     
1032
 
c     restore beta density in g_dens(2)
1033
 
c     
1034
 
         call ga_dadd(one,g_dens(1),-1.d0,g_dens(2),g_dens(2))
1035
 
         call ga_dscal(g_dens(2),0.5d0)
1036
 
      endif
1037
 
 
1038
 
      iter = 1 
1039
 
 
1040
 
cso   ma for complex diagonalizer 
1041
 
      if (.not.MA_Push_Get(MT_DCpl,nbf_mo*nbf_mo,'cpl a',la,ia))
1042
 
     &     call errquit('dft_scf: cannot allocate cpl a',0, MA_ERR)
1043
 
      if (.not.MA_Push_Get(MT_Dbl,nbf_mo,'cpl eval',lw,iw))
1044
 
     &     call errquit('dft_scf: cannot allocate cpl eval',0, MA_ERR)
1045
 
      llwork = max(1, 2*nbf_mo-1)
1046
 
      if (.not.MA_Push_Get(MT_DCpl,llwork,'cpl work',lwork,iwork))
1047
 
     &     call errquit('dft_scf: cannot allocate cpl work',0, MA_ERR)
1048
 
      if (.not.MA_Push_Get(MT_Dbl,max(1,3*nbf_mo-2),'w.s',lrwork,
1049
 
     &     irwork))
1050
 
     &     call errquit('dft_scf: cannot allocate w.s',0, MA_ERR)
1051
 
      if (.not.ma_push_get(mt_dbl,nbf_mo,'buff',lbuff,ibuff))
1052
 
     &     call errquit('dft_scf:push_get failed', 13, MA_ERR)
1053
 
cso
1054
 
cso     
1055
 
c     
1056
 
c     Top of infinite SCF iteration loop
1057
 
c
1058
 
c     Write prep time required
1059
 
c
1060
 
      call ga_sync()
1061
 
      if (me.eq.0.and.oprint)then
1062
 
         current_cpu = util_cpusec()
1063
 
         write(LuOut,20)current_cpu
1064
 
   20    format(2x,' Time prior to 1st pass: ',f8.1)
1065
 
      endif
1066
 
c     
1067
 
c     start DFT_SCF timer
1068
 
c     
1069
 
      start_wall = util_wallsec()
1070
 
      start_cpu = util_cpusec()
1071
 
      dft_time = -start_cpu
1072
 
c
1073
 
      if (oprint_time)
1074
 
     &     call dft_tstamp('   Before SCF iter loop. ')
1075
 
c
1076
 
      last_time_energy = .false.
1077
 
c
1078
 
      det_eng = .false. 
1079
 
      idet = 0 
1080
 
 1000 continue   ! top of the scf loop
1081
 
 
1082
 
      if (me.eq.0 .and. oprint_conv_details)
1083
 
     &   write(LuOut,124)damping, levelshifting, diising
1084
 
 124  format(10x,' DAMPING=',l1,' LEVELSHIFTING=',l1,
1085
 
     &           ' DIISING=',l1)
1086
 
c
1087
 
      if (me.eq.0.and.oprint_tol)write(LuOut,3234)itol2e,iAOacc,iXCacc
1088
 
 3234 format(10x,'itol2e=',i2,' iAOacc=',i2,' iXCacc=',i2)
1089
 
 
1090
 
      Ecoul  = ZERO
1091
 
      Exc(1) = ZERO
1092
 
      Exc(2) = ZERO
1093
 
 
1094
 
      call ga_zero(g_fockso(1))
1095
 
      call ga_zero(g_fockso(2))
1096
 
c     
1097
 
c     Accumulate core hamiltonian into Fock matrix; 
1098
 
c     compute core energy
1099
 
c     
1100
 
      call ga_zero(g_fock)
1101
 
      call int_1e_ga(ao_bas_han, ao_bas_han, g_fock, 'kinetic', oskel)
1102
 
c
1103
 
      call int_1e_ga(ao_bas_han, ao_bas_han, g_fock, 'potential', oskel)
1104
 
      call ga_fock_sf(g_fock, g_fockso(1), nbf_ao) 
1105
 
cso
1106
 
cso   Re(Dsf)=Re(Daa)+Re(Dbb)=g_dens(1) 
1107
 
cso   <Hsf> = Re(Dsf) dot T+Vsf 
1108
 
      Ecore = ga_ddot(g_dens(1), g_fock)
1109
 
cso  
1110
 
cso   Accumulate s.o. contribution to fock matrix 
1111
 
cso   
1112
 
      noso=Ecore 
1113
 
      call ga_zero(g_so(1))
1114
 
      call ga_zero(g_so(2))
1115
 
      call ga_zero(g_so(3))
1116
 
cso
1117
 
cso   Calculate the spin-orbit contributions from the ecp
1118
 
      if( .not. do_zora .and. .not. do_purescalar) then
1119
 
         call int_1e_ga(ao_bas_han, ao_bas_han, g_so, 'so', oskel)
1120
 
         call ga_scale(g_so(1),dble(0.5d0))  ! z
1121
 
         call ga_scale(g_so(2),dble(0.5d0))  ! y
1122
 
         call ga_scale(g_so(3),dble(0.5d0))  ! x
1123
 
cso
1124
 
cso   Add in the s.o. contribution to the fock matrix
1125
 
         call ga_fock_so(g_so, g_fockso, nbf_ao)
1126
 
      end if
1127
 
cso  
1128
 
cso   Accumulate z-component s.o. contribution
1129
 
cso   Re(Dz)=-Im(Daa)+Im(Dbb) 
1130
 
cso   <Hz>=Re(Dz) dot Vz 
1131
 
cso   
1132
 
      call ga_zero(g_tmp) 
1133
 
      call ga_dens_so(g_tmp, g_densso, nbf_ao, 'z') 
1134
 
      Ecore = Ecore + ga_ddot(g_tmp, g_so(1)) 
1135
 
!     write(*,*)"Ecore+=so(z)", ecore
1136
 
cso
1137
 
c     == add in the spin-orbit zora contribution (z) ==
1138
 
      if (do_zora) Ecore = Ecore + ga_ddot(g_tmp, g_zora_so(1))
1139
 
cso  
1140
 
cso   Accumulate y-component s.o. contribution 
1141
 
cso   Re(Dy)=Re(Dab)-Re(Dba) 
1142
 
cso   <Hy>=Re(Dy) dot Vy 
1143
 
cso   
1144
 
      call ga_zero(g_tmp) 
1145
 
      call ga_dens_so(g_tmp, g_densso, nbf_ao, 'y') 
1146
 
      Ecore = Ecore + ga_ddot(g_tmp, g_so(2)) 
1147
 
!     write(*,*)"Ecore+=so(y)", ecore
1148
 
cso
1149
 
c     == add in the spin-orbit zora contribution (y) ==
1150
 
      if (do_zora) Ecore = Ecore + ga_ddot(g_tmp, g_zora_so(2))
1151
 
cso  
1152
 
cso   Accumulate x-component s.o. contribution 
1153
 
cso   Re(Dx)=-Im(Dab)-Im(Dba) 
1154
 
cso   <Hx>=Re(Dx) dot Vx 
1155
 
cso
1156
 
      call ga_zero(g_tmp) 
1157
 
      call ga_dens_so(g_tmp, g_densso, nbf_ao, 'x') 
1158
 
      Ecore = Ecore + ga_ddot(g_tmp, g_so(3))
1159
 
!     write(*,*)"Ecore+=so(x)", ecore
1160
 
cso
1161
 
c     == add in the spin-orbit zora contribution (x) ==
1162
 
      if (do_zora) Ecore = Ecore + ga_ddot(g_tmp, g_zora_so(3))
1163
 
cso
1164
 
       noso = Ecore-noso 
1165
 
c
1166
 
c     Pre-compute reduced total density matrices over atoms
1167
 
1168
 
      call dfill(ipol*natoms*natoms, 0.0d0, dbl_mb(irdens_atom), 1)
1169
 
      nscr = nbf_ao_mxnbf_ce*nbf_ao_mxnbf_ce
1170
 
      if (.not.MA_Push_Get(MT_Dbl,nscr,'scr',lscr,iscr))
1171
 
     &   call errquit('dft_scf: cannot allocate scr',0, MA_ERR)
1172
 
      call util_ga_mat_reduce(nbf_ao, natoms, int_mb(icetobfr), g_dens, 
1173
 
     &                        ipol, dbl_mb(irdens_atom), 'rms', 
1174
 
     &                        dbl_mb(iscr), nbf_ao_mxnbf_ce,.true.)
1175
 
c      write(*,'("irdens",5f10.7)')
1176
 
c     &     (dbl_mb(irdens_atom+i),i=0,ipol*natoms*natoms-1)
1177
 
      if (.not.ma_pop_stack(lscr))
1178
 
     &   call errquit('dft_scf: cannot pop stack:lscr',0, MA_ERR)
1179
 
c
1180
 
c
1181
 
      if (CDFIT)then
1182
 
c
1183
 
c        == attenuation == 
1184
 
         if (cam_exch) call case_setflags(.false.)
1185
 
c     
1186
 
c        Fit the electron charge density.
1187
 
c     
1188
 
         if (.not.MA_Push_Get(MT_Dbl,nbf_cd,'cd_coef',lcd_coef,
1189
 
     &        icd_coef))
1190
 
     &        call errquit('dft_scf: cannot allocate cd_coef',0, MA_ERR)
1191
 
         if (oprint_time)
1192
 
     &        call dft_tstamp(' Before call to FITCD.   ')
1193
 
         call dft_fitcd(1,Dbl_MB(icd_coef), dbl_mb(k_3cERI), Ecoul, 
1194
 
     &        g_dens, nTotEl, n_batch, n3c_int,
1195
 
     &        int_mb(k_3cwhat), n3c_dbl, iwhat_max, 
1196
 
     &        n_semi_bufs, fd, IOLGC, 
1197
 
     .        natoms,
1198
 
     &        .false., 0d0, .false.)
1199
 
      endif
1200
 
c     
1201
 
      if (oprint_time)
1202
 
     &     call dft_tstamp(' Before call to GETVCOUL.')
1203
 
      call dft_getvc(Dbl_MB(icd_coef), dbl_mb(k_3cERI), Ecoul,
1204
 
     &     g_tmp, iVcoul_opt, n_batch, 
1205
 
     &     n3c_int, int_mb(k_3cwhat), n3c_dbl, iwhat_max,
1206
 
     &     n_semi_bufs, fd, IOLGC,
1207
 
     &     .false., 1)
1208
 
c     
1209
 
c     Add V coul to Fock Matrix
1210
 
c     
1211
 
cso   call ga_dadd(one, g_tmp, one, g_fock, g_fock)
1212
 
      call ga_fock_sf(g_tmp, g_fockso(1), nbf_ao) 
1213
 
      if (CDFIT)then
1214
 
         if (.not.ma_pop_stack(lcd_coef))
1215
 
     &        call errquit('dft_scf: cannot pop stacklcd_coef',0,
1216
 
     &       MA_ERR)
1217
 
      endif
1218
 
c     
1219
 
c     Restore alpha and beta densities.
1220
 
c     
1221
 
      call ga_dadd(one, g_dens(1), onem, g_dens(2), g_dens(1))
1222
 
c     
1223
 
c     Note that g_dens(1) now contains the alpha density
1224
 
c     matrix and g_dens(2) contains the beta
1225
 
c     
1226
 
c     Pre-compute reduced alpha and beta density matrices over atoms
1227
 
c     
1228
 
      call dfill(ipol*natoms*natoms, 0.0d0, dbl_mb(irdens_atom), 1)
1229
 
      nscr = nbf_ao_mxnbf_ce*nbf_ao_mxnbf_ce
1230
 
      if (.not.MA_Push_Get(MT_Dbl,nscr,'scr',lscr,iscr))
1231
 
     &     call errquit('dft_scf: cannot allocate scr',0, MA_ERR)
1232
 
      call util_ga_mat_reduce(nbf_ao, natoms, int_mb(icetobfr), 
1233
 
     &     g_dens, ipol, dbl_mb(irdens_atom), 
1234
 
     &     'rms', dbl_mb(iscr), nbf_ao_mxnbf_ce,.true.)
1235
 
      if (.not.ma_pop_stack(lscr))
1236
 
     &  call errquit('dft_scf: cannot pop stacklscr:',0, MA_ERR)
1237
 
c     
1238
 
c     Compute the XC potential and energy.
1239
 
c     
1240
 
      g_vxc(1) = g_tmp
1241
 
      call ga_zero(g_vxc(1))
1242
 
      rho_n = 0.0d0
1243
 
      call ga_zero(g_vxc(2))
1244
 
c
1245
 
c     == attenuation ==
1246
 
      if (cam_exch) call case_setflags(.true.) ! set attenuation
1247
 
c
1248
 
      if (oprint_time)call dft_tstamp(' Before call to GETVXC.  ')
1249
 
      call xc_getv
1250
 
     &   (rtdb, Exc, Ecoul,nExc, iVxc_opt, g_xcinv, g_dens, 
1251
 
     &   g_vxc, IOLGC, g_wght, g_xyz, g_nq,wght_GA, rho_n,
1252
 
     &   dbl_mb(irdens_atom), int_mb(icetobfr), natoms)
1253
 
c     write(*,*)"Ecoul, Exc(1)", Ecoul, Exc(1)
1254
 
c
1255
 
c     == attenuation ==
1256
 
      if (cam_exch) call case_setflags(.false.) ! unset attenuation if set
1257
 
c
1258
 
c     == add in zora contributions ==
1259
 
      if (do_zora) then
1260
 
c
1261
 
c        == calculate scalar energy ==
1262
 
         Ezora_sf = ga_ddot(g_dens(1),g_zora_sf(1))
1263
 
     &      + ga_ddot(g_dens(2),g_zora_sf(2))
1264
 
         Ecore = Ecore + Ezora_sf
1265
 
c
1266
 
c        == combine scalar part with the xc matrices ==
1267
 
         call ga_dadd(1.d0,g_vxc(1),1.d0,g_zora_sf(1),g_vxc(1))
1268
 
         call ga_dadd(1.d0,g_vxc(2),1.d0,g_zora_sf(2),g_vxc(2))
1269
 
c
1270
 
c        == add spin-orbit zora to fock matrix ==
1271
 
         call ga_fock_so(g_zora_so, g_fockso, nbf_ao)
1272
 
 
1273
 
      end if  ! do_zora
1274
 
c
1275
 
c     == add in the exchange-correlation to the fock matrix ==
1276
 
      call ga_sync()
1277
 
      call ga_dadd_patch( 1.d0, g_fockso(1), 1, nbf_ao, 
1278
 
     &     1, nbf_ao, 
1279
 
     &     1.0d0, g_vxc(1),  1, nbf_ao, 
1280
 
     &     1, nbf_ao,
1281
 
     &     g_fockso(1), 1, nbf_ao, 
1282
 
     &     1, nbf_ao) 
1283
 
      call ga_dadd_patch( 1.d0, g_fockso(1), 1+nbf_ao, nbf_mo, 
1284
 
     &     1+nbf_ao, nbf_mo,
1285
 
     &     1.0d0, g_vxc(2),  1, nbf_ao, 
1286
 
     &     1, nbf_ao,
1287
 
     &     g_fockso(1), 1+nbf_ao, nbf_mo, 
1288
 
     &     1+nbf_ao, nbf_mo)
1289
 
c     
1290
 
c     == get the exact exchange contribution ==
1291
 
      call xc_exso(rtdb,Exc,Ecoul,nExc,g_densso,g_fockso)
1292
 
c
1293
 
      if (oprint_time)
1294
 
     &     call dft_tstamp(' End of parallel region. ')
1295
 
c     
1296
 
c     Calculate the total electronic energy.
1297
 
c     
1298
 
      if (nExc.eq.1)then
1299
 
         Etnew = Ecore + Ecoul + Exc(1)
1300
 
         if(det_eng)goto 2001
1301
 
      else
1302
 
         Etnew = Ecore + Ecoul + Exc(1) + Exc(2)
1303
 
         if(det_eng)goto 2001
1304
 
      endif
1305
 
 
1306
 
      if (last_time_energy)then
1307
 
c     
1308
 
c     If open shell put the total density matrix back in 
1309
 
c     g_dens(1) and quit.
1310
 
c     
1311
 
         call ga_dadd(one, g_dens(1), one, g_dens(2), g_dens(1))
1312
 
         goto 2000
1313
 
      endif
1314
 
c     
1315
 
      delta = abs(etold-etnew)
1316
 
c     
1317
 
      call ga_sync
1318
 
      rms(1) = 0.d0
1319
 
      rms(2) = 0.d0
1320
 
      homo_lumo_gap = 200.0d0
1321
 
c     
1322
 
c     Symmetrize the Fock matrix
1323
 
c
1324
 
      if (oskel)
1325
 
     &   call sym_symmetrize(geom, AO_bas_han, .false., g_fock)
1326
 
c
1327
 
      call ga_symmetrize(g_fock)
1328
 
c
1329
 
c     DIIS step taken here.
1330
 
c     
1331
 
      if (diising)then
1332
 
        call diis_driver_so(toll_s, derr, icall, nfock, 
1333
 
     &           nbf_mo, g_fockso, g_densso, 
1334
 
     &           g_svecs, svals, diising, nodiis)
1335
 
        derr(2)=derr(1)
1336
 
      endif
1337
 
c     
1338
 
      g_scr = ga_create_atom_blocked(geom, AO_bas_han, 'ga scr')
1339
 
c     
1340
 
c     Put s-1/2 in g_scr.
1341
 
c     
1342
 
      iw = 2
1343
 
      call diis_bld12_so(toll_s, svals, g_svecs, g_scr, 
1344
 
     &     g_tmp, nbf_ao, iw)
1345
 
c     
1346
 
c     map s-1/2 to the nbf_mo by nbf_mo g_scr2 
1347
 
c    
1348
 
      if(.not.ga_create(mt_dbl, 2*nbf, 2*nbf,'scr2', 0, 0, g_scr2))
1349
 
     &     call errquit('dft_scf_so: error creating scr2',0, GA_ERR)
1350
 
      call ga_zero(g_scr2)
1351
 
      call ga_fock_sf(g_scr, g_scr2, nbf_ao)
1352
 
c     
1353
 
c     Transform Fock matrix.
1354
 
c     
1355
 
      call ga_zero(g_tmp_ri)   
1356
 
      call ga_dgemm('T', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1357
 
     &                 g_scr2, g_fockso(1), zero, g_tmp_ri)
1358
 
      call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1359
 
     &                 g_tmp_ri, g_scr2, zero, g_fockso(1))
1360
 
 
1361
 
      call ga_zero(g_tmp_ri)   
1362
 
      call ga_dgemm('T', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1363
 
     &                 g_scr2, g_fockso(2), zero, g_tmp_ri)
1364
 
      call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1365
 
     &                 g_tmp_ri, g_scr2, zero, g_fockso(2)) 
1366
 
c     
1367
 
c     Level shifting is implemented here (similarity 
1368
 
c     transformation before standard eigensolver).  Note,
1369
 
c     levelshifting is appropriate once a transformation
1370
 
c     is available which makes the resulting Fock matrix 
1371
 
c     diagonally dominant, e.g., in an approximate MO basis.  
1372
 
c     Also note, there are many matrix multiplies with S^+-1/2 
1373
 
c     which are redundant if one is sure that the former basis
1374
 
c     is orthonormal.
1375
 
c     
1376
 
c     levelshifting = .false. 
1377
 
      if (levelshifting)then
1378
 
c     
1379
 
c     save the old vectors 
1380
 
c     
1381
 
         call ga_copy(g_moso(1), g_old(1))
1382
 
         call ga_copy(g_moso(2), g_old(2))
1383
 
c     
1384
 
c        Build a matrix which is diagonal in the "MO" rep,
1385
 
c        back-transform, and shift the current Fock matrix
1386
 
c     
1387
 
c        Use S^+1/2 * old movecs (as a transform).
1388
 
c     
1389
 
         iw = 3
1390
 
         call diis_bld12_so(toll_s, svals, g_svecs, g_scr, 
1391
 
     &                   g_tmp, nbf_ao, iw)
1392
 
         call ga_zero(g_scr2)
1393
 
         call ga_fock_sf(g_scr, g_scr2, nbf_ao)
1394
 
         call ga_zero(g_tmp_ri)
1395
 
         call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1396
 
     &                 g_scr2, g_moso(1), zero, g_tmp_ri)
1397
 
         call ga_copy(g_tmp_ri,  g_moso(1)) 
1398
 
 
1399
 
         call ga_zero(g_tmp_ri) 
1400
 
         call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1401
 
     &                 g_scr2, g_moso(2), zero, g_tmp_ri)
1402
 
         call ga_copy(g_tmp_ri,  g_moso(2)) 
1403
 
c     
1404
 
c        Build diagonal matrix.
1405
 
c     
1406
 
         call ga_zero(g_tmp_ri)
1407
 
         do j = nTotOcc+1+me, nbf_mo, nproc
1408
 
            call ga_put(g_tmp_ri, j, j, j, j, rlshift, 1)
1409
 
         enddo
1410
 
c     
1411
 
c        Transform this into "AO" basis and add to current 
1412
 
c        Fock matrix
1413
 
c     
1414
 
         call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1415
 
     &                 g_moso(1), g_tmp_ri, zero, g_scr2)
1416
 
         call ga_dgemm('N', 'T', nbf_mo, nbf_mo, nbf_mo, one, 
1417
 
     &                 g_scr2, g_moso(1), one, g_fockso(1))
1418
 
         call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1419
 
     &                 g_moso(2), g_tmp_ri, zero, g_scr2)
1420
 
         call ga_dgemm('N', 'T', nbf_mo, nbf_mo, nbf_mo, one, 
1421
 
     &                 g_scr2, g_moso(2), one, g_fockso(1))
1422
 
 
1423
 
         call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1424
 
     &                 g_moso(1), g_tmp_ri, zero, g_scr2)
1425
 
         call ga_dgemm('N', 'T', nbf_mo, nbf_mo, nbf_mo, mone, 
1426
 
     &                 g_scr2, g_moso(2), one, g_fockso(2))
1427
 
         call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1428
 
     &                 g_moso(2), g_tmp_ri, zero, g_scr2)
1429
 
         call ga_dgemm('N', 'T', nbf_mo, nbf_mo, nbf_mo, one, 
1430
 
     &                 g_scr2, g_moso(1), one, g_fockso(2))
1431
 
      else
1432
 
        rlshift = 0.0
1433
 
      endif
1434
 
c     
1435
 
c     Solve for the eigenvalues and eigenvectors of the Hamiltonian.
1436
 
c     
1437
 
      call ga_symmetrize(g_fock)
1438
 
      if (oprint_intermediate_fock)then     
1439
 
      endif
1440
 
cso#if defined(PARALLEL_DIAG)
1441
 
cso      call ga_diag_std(g_fock, g_tmp, Dbl_MB(k_eval(ispin)))
1442
 
cso#else 
1443
 
cso      call ga_diag_std_seq(g_fock, g_tmp, Dbl_MB(k_eval(ispin)))
1444
 
cso#endif
1445
 
cso      call ga_diag_compl(g_fockso(1), g_fockso(2), g_moso(1), 
1446
 
cso     &                   g_moso(2),  Dbl_MB(k_eval(1))) 
1447
 
c      write(*,*)"compare"
1448
 
      do i = 1, nbf_mo 
1449
 
         do j = 1, nbf_mo
1450
 
            DCpl_mb(ia+(nbf_mo)*(i-1)+(j-1))=dcmplx(0.0, 0.0)
1451
 
         enddo
1452
 
      enddo
1453
 
      do i = 1, nbf_mo 
1454
 
         call ga_get(g_fockso(1), 1,i, i,i, dbl_mb(ibuff),1)
1455
 
c         write(*,*)"i=", i 
1456
 
c         write(*,*)(dbl_mb(ibuff+ijk), ijk=0,nbf_mo-1)
1457
 
         do j=1,i 
1458
 
            DCpl_mb(ia+(nbf_mo)*(i-1)+(j-1))=
1459
 
     =           dcmplx(dbl_mb(ibuff+j-1),0d0)
1460
 
         enddo 
1461
 
         call ga_get(g_fockso(2), 1,i, i,i, dbl_mb(ibuff),1)
1462
 
         do j=1,i 
1463
 
            DCpl_mb(ia+(nbf_mo)*(i-1)+(j-1))=
1464
 
     $               DCpl_mb(ia+(nbf_mo)*(i-1)+(j-1))
1465
 
     $           +dcmplx(0d0,dbl_mb(ibuff+j-1))
1466
 
         enddo 
1467
 
      enddo
1468
 
      call ga_zero(g_moso(1))
1469
 
      call ga_zero(g_moso(2))
1470
 
      call zheev( 'V', 'U', nbf_mo, DCpl_mb(ia), nbf_mo, 
1471
 
     $            Dbl_mb(k_eval(1)), 
1472
 
     $            DCpl_mb(iwork), LLWORK, Dbl_mb(irwork), INFO )
1473
 
      do i = 1, nbf_mo
1474
 
         do j = 1, nbf_mo 
1475
 
            dbl_mb(ibuff+j-1)=0.0d0
1476
 
            dbl_mb(ibuff+j-1)=dble(DCpl_mb(ia+nbf_mo*(i-1)+(j-1)))
1477
 
         enddo 
1478
 
         i1=i
1479
 
         call ga_put(g_moso(1),1,nbf_mo,i1,i1,dbl_mb(ibuff),1)
1480
 
         trace = ddot(nbf_mo,dbl_mb(ibuff),1,dbl_mb(ibuff),1) 
1481
 
         do j = 1, nbf_mo 
1482
 
            dbl_mb(ibuff+j-1)=0.0d0
1483
 
            dbl_mb(ibuff+j-1)=
1484
 
     $             dimag(dcmplx(DCpl_mb(ia+nbf_mo*(i-1)+(j-1))))
1485
 
         enddo
1486
 
         i1=i 
1487
 
         call ga_put(g_moso(2),1,nbf_mo,i1,i1,dbl_mb(ibuff),1)
1488
 
         trace = ddot(nbf_mo,dbl_mb(ibuff),1,dbl_mb(ibuff),1) 
1489
 
      enddo
1490
 
c      write(*,'("before transform")')
1491
 
c      write(*,*)(Dbl_mb(k_eval(1)+i),i=0,nbf_mo-1)
1492
 
c      call ga_print(g_moso(1))
1493
 
c      call ga_print(g_moso(2))
1494
 
c     
1495
 
c     Check HOMO/LUMO gap.
1496
 
c     
1497
 
      homo = Dbl_MB(k_eval(1)+nTotEl-1)
1498
 
      lumo = Dbl_MB(k_eval(1)+nTotEl)
1499
 
c     
1500
 
c     If levelshifting then tidy up.
1501
 
c  
1502
 
      if (levelshifting)then
1503
 
c     
1504
 
c        Put S^-1/2 back in g_scr2 (use g_fock as temp scr).
1505
 
c     
1506
 
         iw = 2
1507
 
         call diis_bld12_so(toll_s, svals, g_svecs, g_scr, 
1508
 
     &                   g_fock, nbf_ao, iw)
1509
 
         call ga_zero(g_scr2)
1510
 
         call ga_fock_sf(g_scr, g_scr2, nbf_ao)
1511
 
      endif
1512
 
c     
1513
 
c     Back-transform eigenvectors with S^-1/2.
1514
 
c     
1515
 
      call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1516
 
     &     g_scr2, g_moso(1), zero, g_fockso(1))
1517
 
      call ga_dgemm('N', 'N', nbf_mo, nbf_mo, nbf_mo, one, 
1518
 
     &     g_scr2, g_moso(2), zero, g_fockso(2))
1519
 
      if (.not. ga_destroy(g_scr)) call errquit
1520
 
     &     ('dft_scf: could not destroy g_scr', 0, GA_ERR)
1521
 
      if (.not. ga_destroy(g_scr2)) call errquit
1522
 
     &     ('dft_scf_so: could not destroy g_scr2', 0, GA_ERR)
1523
 
c     
1524
 
c     Keep orbital ordering according to principle
1525
 
c     of maximum overlap with previous iteration.
1526
 
c
1527
 
      if (lmaxov)
1528
 
     .     call dft_mxovl(ao_bas_han, dbl_mb(k_eval(ispin)),
1529
 
     &                  g_tmp, g_movecs(ispin), g_s,g_fock,
1530
 
     .        noc,oprint_conv_details,homo,lumo)
1531
 
 
1532
 
      call ga_zero(g_moso(1))
1533
 
      call ga_zero(g_moso(2))
1534
 
      call ga_copy(g_fockso(1), g_moso(1))
1535
 
      call ga_copy(g_fockso(2), g_moso(2))
1536
 
c        
1537
 
c     determine homo-lumo gap 
1538
 
c
1539
 
      homo_lumo_gap = min(homo_lumo_gap, (lumo-homo-rlshift))
1540
 
      if (me.eq.0 .and. oprint_conv_details)
1541
 
     &   write(LuOut,4224)homo,lumo,rlshift, homo_lumo_gap
1542
 
 4224 format(10x,' HOMO = ',f6.2,' LUMO = ',f6.2,
1543
 
     &           ' RLSHIFT = ',f6.2,' HL_GAP = ',f6.2)
1544
 
c     
1545
 
      call ga_sync
1546
 
c     
1547
 
c     Save previous density for convergence check.
1548
 
c     
1549
 
      call ga_copy(g_dens(1), g_movecs(1))
1550
 
      call ga_copy(g_dens(2), g_movecs(2))
1551
 
c
1552
 
c     symmetry adapt vectors?
1553
 
c
1554
 
      if (oadapt)then
1555
 
         call scf_sym_adapt_so(ao_bas_han, g_moso,
1556
 
     &        oprint_syma, 2*nbf_ao, name,
1557
 
     &        .true., 
1558
 
     &        int_mb(k_ir))
1559
 
      endif      
1560
 
c
1561
 
c     save the old density matrix for damping 
1562
 
c
1563
 
      call ga_copy(g_densso(1), g_damp_so(1))
1564
 
      call ga_copy(g_densso(2), g_damp_so(2))
1565
 
c     
1566
 
c     Form a new density matrix.
1567
 
c     
1568
 
      call ga_sync 
1569
 
      call ga_zero(g_densso(1))
1570
 
      call ga_zero(g_densso(2)) 
1571
 
 
1572
 
      switch_sclMO_so=0 ! FA=09-26-11 set OFF scaleMO_so
1573
 
c     
1574
 
c     the fractionally occupied mo's are scaled by the sqrt of the fon
1575
 
c     
1576
 
      if(fon)then 
1577
 
c         scale = sqrt(avg_fon)
1578
 
         do i = ncore_fon + 1, ncore_fon + nmo_fon
1579
 
           if (i> nbf_mo) call errquit(
1580
 
     &        'dft_densm:fon g_moso index exceeds nbf_mo',
1581
 
     &        i, INPUT_ERR)
1582
 
           scale = sqrt(dbl_mb(i-1+k_occ)) ! sqrt(occupation number)
1583
 
           call ga_scale_patch(g_moso(1), 
1584
 
     &        1, nbf_mo, i, i, scale)
1585
 
           call ga_scale_patch(g_moso(2), 
1586
 
     &        1, nbf_mo, i, i, scale)
1587
 
         end do
1588
 
         if(me.eq.0) write(luout,'(5x,a)')  'FON applied'
1589
 
c      else
1590
 
c ---- FA-02-15-11 : occupations keyword ---- START
1591
 
c       call dft_scaleMO_so(rtdb,g_moso,dbl_mb(k_occ),g_densso,
1592
 
c     &                     nbf_mo,nTotOcc,switch_sclMO_so)
1593
 
c ---- FA-02-15-11 : occupations keyword ---- END
1594
 
      endif
1595
 
 
1596
 
c       if (switch_sclMO_so.ne.1) then
1597
 
c         if (ga_nodeid().eq.0)
1598
 
c     &   write(*,*) 'ENTER calc so-density matrix std-way'
1599
 
         call dft_densm_so(g_densso, g_moso, nbf_ao, nTotOcc)
1600
 
c       endif
1601
 
c     
1602
 
c     restore the scaled mo's
1603
 
c     
1604
 
      if (fon) then 
1605
 
c         scale = sqrt(avg_fon)
1606
 
         do i = ncore_fon + 1, ncore_fon + nmo_fon
1607
 
           if (i> nbf_mo) call errquit(
1608
 
     &        'dft_densm:fon g_moso index exceeds nbf_mo',
1609
 
     &        i, INPUT_ERR)
1610
 
           if (dbl_mb(i-1+k_occ) < 1d-4) call errquit(
1611
 
     &        'dft_densm:fon frac occup < 1E-4. Aborting suspisciously',
1612
 
     &        i, INPUT_ERR)           
1613
 
           scale = 1d0/sqrt(dbl_mb(i-1+k_occ)) ! 1/sqrt(occupation number)
1614
 
           call ga_scale_patch(g_moso(1), 
1615
 
     &        1, nbf_mo, i, i, scale)
1616
 
           call ga_scale_patch(g_moso(2), 
1617
 
     &        1, nbf_mo, i, i, scale)
1618
 
         end do
1619
 
       end if ! fon
1620
 
cso
1621
 
cso   g_dens(1)=Re(Daa)+Re(Dbb) and g_dens(2)=Re(Dbb)
1622
 
cso   For coulomb and xc potentials only the alpha, Re(Daa), and  
1623
 
cso   beta, Re(Dbb) densities are needed 
1624
 
cso
1625
 
      call ga_zero(g_dens(1))
1626
 
      call ga_zero(g_dens(2))
1627
 
      call ga_dens_sf(g_dens, g_densso(1), nbf_ao)
1628
 
 
1629
 
c     check g_dens, calculate tr[P S] and print
1630
 
c     if (debug_fon) call dft_pstrace(g_dens,ao_bas_han,nbf_ao,oskel)
1631
 
      if (fon) then
1632
 
        pstrace=ga_ddot(g_dens(1),g_s)
1633
 
        pstrace=pstrace + ga_ddot(g_dens(2),g_s)
1634
 
        if(ga_nodeid().eq.0) write (luout,'(5x,a,1x,e15.7)')
1635
 
     &       'tr(P*S): ',pstrace 
1636
 
        if (.not. rtdb_put(rtdb, 'dft:pstrace', mt_dbl, 1, pstrace))
1637
 
     &     call errquit('dft_scf: rtdb_put pstrace failed', 1, RTDB_ERR)
1638
 
      end if
1639
 
c
1640
 
      call ga_sync
1641
 
c     
1642
 
c     Check convergence on Density.
1643
 
c     
1644
 
      rms(1) = dft_dencvg(g_dens(1), g_movecs(1), nbf_ao)
1645
 
      rms(2) = dft_dencvg(g_dens(2), g_movecs(2), nbf_ao)
1646
 
      call ga_sync
1647
 
c     
1648
 
      if (oprint_conv.and.iter.eq.1.and.me.eq.0)then
1649
 
         nheap = MA_Inquire_Heap(MT_Dbl)
1650
 
         nstack = MA_Inquire_Stack(MT_Dbl)
1651
 
         write(LuOut,21)
1652
 
         write(LuOut,'(10x,a,f10.2,i20)')
1653
 
     &        ' Heap Space remaining (MW):  ',dble(nheap)*1.D-06,nheap
1654
 
         write(LuOut,'(10x,a,f10.2,i20)')
1655
 
     &        'Stack Space remaining (MW):  ',dble(nstack)*1.D-06,nstack
1656
 
         call util_flush(LuOut)
1657
 
         write(LuOut,1)
1658
 
      endif
1659
 
 21   format(/,10x,' Memory utilization after 1st SCF pass: ')
1660
 
    1 format(/,
1661
 
     &     1x,'  convergence    iter        energy       DeltaE   ',
1662
 
     &     'RMS-Dens  Diis-err    time'/
1663
 
     &     1x,'---------------- ----- ----------------- --------- ',
1664
 
     &     '--------- ---------  ------')
1665
 
      if (oprint_conv.and.me.eq.0)then
1666
 
         current_cpu = util_cpusec()
1667
 
         if (diising)then
1668
 
            write(LuOut,2)ndamp,rlshift,
1669
 
     &           iter, Etnew+Enuc,
1670
 
     &           -etold+etnew,sqrt(rms(1)),derr(1),current_cpu
1671
 
            if (ipol.eq.2)write(LuOut,3)sqrt(rms(2)),derr(2)
1672
 
         else
1673
 
            write(LuOut,22)ndamp,rlshift,
1674
 
     &           iter, Etnew+Enuc,
1675
 
     &           -etold+etnew,sqrt(rms(1)), current_cpu
1676
 
            if (ipol.eq.2)write(LuOut,23)sqrt(rms(2))
1677
 
         endif
1678
 
         call util_flush(LuOut)
1679
 
      endif
1680
 
    2 format(1x,'d=',i2,',ls=',f3.1,',diis',1x,i5,f18.10,
1681
 
     &     1p,3d10.2,0p,f8.1)
1682
 
    3 format(51x,1p,2d10.2)
1683
 
 22   format(1x,'d=',i2,',ls=',f3.1,6x,i5,f18.10,
1684
 
     &     1p,2d10.2,10x,0p,f8.1)
1685
 
 23   format(51x,1p,1d10.2)
1686
 
c
1687
 
c     ecce ouput
1688
 
c
1689
 
      call ecce_print1 ('iteration counter', mt_int, iter, 1)
1690
 
      call ecce_print1 ('iterative total energy difference', 
1691
 
     &                  mt_dbl, -etold+etnew, 1)
1692
 
      call ecce_print1 ('iterative total density difference', 
1693
 
     &                  mt_dbl, sqrt(rms(1)), 1)
1694
 
c
1695
 
      call ga_sync
1696
 
c     
1697
 
c     save eigenvectors to movecs file
1698
 
c     
1699
 
      if (.not.movecs_write_so
1700
 
     $     (rtdb, ao_bas_han, movecs_out, 'sodft', title,
1701
 
     &     nbf_mo, dbl_mb(k_occ), dbl_mb(k_eval(1)), g_moso))
1702
 
     &     call errquit('dft_scf_so: movec_write failed', 0, DISK_ERR)
1703
 
c     
1704
 
      call ga_sync
1705
 
c     
1706
 
      if (me .eq. 0.and.oprint_eval)then
1707
 
         if (util_print('intermediate evals', print_default))then
1708
 
            call util_print_centered(LuOut,'eigenvalues',
1709
 
     &           20,.true.)
1710
 
            call output(dbl_mb(k_eval(1)), 1, min(nTotEl+10,nbf_mo),
1711
 
     &           1, 1, nbf_mo, 1, 1)
1712
 
            call util_flush(6)
1713
 
         endif
1714
 
      endif
1715
 
      if (oprint_vecs)then
1716
 
         if (me .eq. 0)then
1717
 
            write(LuOut,*)
1718
 
            call util_print_centered(LuOut,
1719
 
     &           'Intermediate MO vectors',40,.true.)
1720
 
            write(LuOut,*)
1721
 
            call util_flush(LuOut)
1722
 
         endif
1723
 
      endif
1724
 
c     
1725
 
c     If open shell compute overlap of alpha orbitals with beta 
1726
 
c     orbitals.
1727
 
c     
1728
 
      if ((ipol.gt.1).and.(oprint_interm_overlap)) then
1729
 
         call dft_mxspin_ovlp(nbf_ao,nmo,ao_bas_han,g_movecs(1), 
1730
 
     &       g_movecs(2), g_tmp)
1731
 
      endif
1732
 
c     
1733
 
c     computation of <S2> for open shell
1734
 
c     
1735
 
      if ((ipol.gt.1).and.(oprint_interm_S2)) then
1736
 
         
1737
 
         call dft_s2_value(geom, AO_bas_han, .false., noc(1), noc(2),
1738
 
     &        nbf_ao, g_dens(1), g_dens(2))
1739
 
      endif
1740
 
c     
1741
 
c     Form the total density matrix.
1742
 
c     
1743
 
      call ga_dadd(one, g_dens(1), one, g_dens(2), g_dens(1))
1744
 
      call ga_sync
1745
 
c     
1746
 
c     Check for SCF convergence.
1747
 
c     
1748
 
      call ga_sync
1749
 
      if (do_zora) then
1750
 
        call dft_zora_scfcvg(rms, derr, Etold, Etnew, 
1751
 
     &     e_conv, d_conv, g_conv, ipol, 
1752
 
     &     iter, iterations, idone, rtdb,
1753
 
     &     converged, diising)
1754
 
      else
1755
 
        call dft_scfcvg(rms, derr, Etold, Etnew, 
1756
 
     &     e_conv, d_conv, g_conv, ipol, 
1757
 
     &     iter, iterations, idone, rtdb,
1758
 
     &     converged, diising)
1759
 
      end if ! do_zora
1760
 
      if (delta.lt.1.d-3)then
1761
 
c     
1762
 
c     Set coulomb acc to max (e.g., input parameter).
1763
 
c     (note, may also require re-initializing DIIS)
1764
 
c     
1765
 
         itol2e = itol_max
1766
 
         iAOacc = iAOacc_max
1767
 
         tol_rho = tol_rho_max
1768
 
         iswitc = iswitc+1
1769
 
      endif
1770
 
c     
1771
 
c     Damping implemented here.
1772
 
c    
1773
 
      if (damping)then
1774
 
         pp = dble(ndamp)*1.d-2
1775
 
         onempp = 1.0d0 - pp
1776
 
         call ga_dadd(pp, g_damp_so(1), onempp, g_densso(1), 
1777
 
     &        g_densso(1))
1778
 
         call ga_dadd(pp, g_damp_so(2), onempp, g_densso(2), 
1779
 
     &        g_densso(2))
1780
 
         call ga_zero(g_dens(1))
1781
 
         call ga_zero(g_dens(2))
1782
 
         call ga_dens_sf(g_dens, g_densso(1), nbf_ao)
1783
 
         call ga_dadd(one, g_dens(1), one, g_dens(2), g_dens(1))
1784
 
         call ga_sync
1785
 
      else
1786
 
         ndamp = 0
1787
 
      endif
1788
 
      call ga_sync
1789
 
      iter = iter + 1
1790
 
c     
1791
 
c     Check convergence parameters.
1792
 
c     
1793
 
      if ((delta.lt.dampon.and.delta.gt.dampoff).or.iter.le.ncydp)then
1794
 
         damping = .true.
1795
 
         ndamp = ndamp_input
1796
 
      else
1797
 
         damping = .false.
1798
 
         ndamp = ndamp_def
1799
 
      endif
1800
 
c     
1801
 
      if ((delta.lt.levlon.and.delta.gt.levloff).or.
1802
 
     &     (iter.le.ncysh))then
1803
 
         if (homo_lumo_gap.lt.hl_tol)then
1804
 
            levelshifting = .true.
1805
 
            rlshift = rlshift_input
1806
 
            if (check_shift)then
1807
 
               if (lumo .lt. homo)then
1808
 
                  levelshifting = .false.
1809
 
                  if (me.eq.0 .and. oprint_conv_details)
1810
 
     &                 write(LuOut,2224)homo, lumo
1811
 
               endif
1812
 
            endif
1813
 
         else
1814
 
            levelshifting = .false.
1815
 
            rlshift = rlshift_def
1816
 
         endif 
1817
 
      else
1818
 
         levelshifting = .false.
1819
 
         rlshift = rlshift_def
1820
 
      endif
1821
 
 2224 format(10x,'HOMO = ',f6.2,' LUMO (with shift) = ',f6.2,
1822
 
     &     /,10x,'Unshifted LUMO is less than HOMO.',
1823
 
     &     /,10x,'Turning levelshifting OFF this iteration.')
1824
 
c     
1825
 
      if ((delta.lt.diison.and.delta.gt.diisoff).or.
1826
 
     &     iter.le.ncyds.or.keep_diis_on)then
1827
 
         diising = .true.
1828
 
c     
1829
 
c     Once started, keep DIIS on until diisoff threshold.
1830
 
c     
1831
 
         keep_diis_on = .true.
1832
 
      else
1833
 
         diising = .false.
1834
 
      endif
1835
 
      if (delta.lt.diisoff.or.(ncyds.gt.0.and.iter.gt.ncyds))then
1836
 
         diising = .false.
1837
 
         keep_diis_on = .false.
1838
 
      endif
1839
 
c     
1840
 
      if (nodamping)damping = .false.
1841
 
      if (nolevelshifting) then 
1842
 
         levelshifting = .false.
1843
 
         rlshift=rlshift_def
1844
 
      endif        
1845
 
      if (nodiis)diising = .false.
1846
 
c     
1847
 
c     vdw bit
1848
 
c
1849
 
c     activate disp if is present in rtdb
1850
 
c     or if dispersion is automatically included in the xc functional
1851
 
      if (.not.rtdb_get(rtdb, 'dft:disp', mt_log, 1, disp))
1852
 
     &   disp=.false.
1853
 
c
1854
 
      if(disp.or.xc_chkdispauto())
1855
 
     &     call xc_vdw(rtdb,geom,Etnew,dum,'energy')
1856
 
c
1857
 
      Etold = Etnew
1858
 
c     
1859
 
      lmaxov = lmaxov_sv
1860
 
      if ((lumo - homo).lt.-hl_tol.and.lmaxov)then
1861
 
         lmaxov = .false.
1862
 
         if (me.eq.0 .and. oprint_conv_details)
1863
 
     &        write(LuOut,224)homo, lumo
1864
 
 224     format(10x,' HOMO = ',f6.2,' LUMO = ',f6.2,
1865
 
     &        /,10x,'Significant orbital reordering with',
1866
 
     &        ' maximum overlap',
1867
 
     &        /,10x,'turned ON.  Turning max_ovl OFF.')
1868
 
      endif
1869
 
c
1870
 
      if (oprint_energy_step.and.me.eq.0)then         
1871
 
         current_cpu = util_cpusec()
1872
 
         if (nexc.le.1)then
1873
 
            write(LuOut,222)etnew+enuc, ecore, Ecoul, Exc(1), enuc, 
1874
 
     &           rho_n, current_cpu
1875
 
         else
1876
 
            write(LuOut,223)etnew+enuc, ecore, Ecoul, Exc(1), Exc(2),
1877
 
     &           enuc, rho_n, current_cpu
1878
 
         endif
1879
 
      endif
1880
 
c     
1881
 
c     Check for remaining time to exit "gracefully"
1882
 
c     
1883
 
      current_wall = util_wallsec()
1884
 
      if ((iter-1).gt.1)then
1885
 
         elapsed_wall = current_wall - save_wall
1886
 
         save_wall = current_wall
1887
 
      else
1888
 
         elapsed_wall = current_wall - start_wall
1889
 
         save_wall = current_wall
1890
 
      endif
1891
 
c     
1892
 
      if (converged)then
1893
 
c     
1894
 
c     If converged probably need a few seconds to clean things up 
1895
 
c     and calculate a few properties.
1896
 
c     
1897
 
         wall_time_reqd = 5.0
1898
 
c     
1899
 
c        == scale zora eigenvalues and energy ==
1900
 
         ener_scal = 0.d0
1901
 
         if (do_zora) then
1902
 
            call dft_zora_scale_so(
1903
 
     &                   rtdb,g_dens_at,nexc, ! Added by FA
1904
 
     &                   geom,
1905
 
     &                   ao_bas_han,
1906
 
     &                   nbf,
1907
 
     &                   nbf_ao,
1908
 
     &                   nbf_mo,
1909
 
     &                   g_dens,
1910
 
     &                   g_s,
1911
 
     &                   g_moso,
1912
 
     &                   g_zora_scale_sf,
1913
 
     &                   g_zora_scale_so,
1914
 
     &                   dbl_mb(k_eval(1)),
1915
 
     &                   dbl_mb(k_occ),
1916
 
     &                   nTotOcc,
1917
 
     &                   noc, ! FA
1918
 
     &                   ipol,
1919
 
     &                   ener_scal)
1920
 
         end if
1921
 
c
1922
 
      else
1923
 
c     
1924
 
c     If not converged probably need at least the amount time
1925
 
c     required for previous iteration (multiply by 1.2 to be on the safe side).
1926
 
c     
1927
 
         wall_time_reqd = elapsed_wall*1.2d0
1928
 
      endif
1929
 
      int_wall_time_reqd = wall_time_reqd
1930
 
      if (.not.util_test_time_remaining(rtdb, int_wall_time_reqd))then
1931
 
         if (me.eq.0)then
1932
 
            write(LuOut,*)
1933
 
            call util_print_centered(LuOut,
1934
 
     &           'Exiting due to time limitations.', 20, .true.)
1935
 
            write(LuOut,*)
1936
 
            goto 2000
1937
 
         endif
1938
 
      endif
1939
 
      if (idone.eq.0.or.(iswitc.lt.2.and.iter.lt.iterations))
1940
 
     &     go to 1000           ! begin new iteration
1941
 
      if (idone.eq.1.and.(.not.last_time_energy))then
1942
 
         last_time_energy = .true.
1943
 
         go to 1000             ! build final total energies
1944
 
      endif
1945
 
c     
1946
 
 2000 continue
1947
 
c    
1948
 
      if (me.eq.0.and.oprint)then
1949
 
         if (.not.converged)then
1950
 
            write(LuOut,*)
1951
 
            call util_print_centered(LuOut,
1952
 
     &           'Calculation failed to converge', 20, .true.)
1953
 
            write(LuOut,*)
1954
 
         endif
1955
 
         dft_time = dft_time+util_cpusec()
1956
 
 
1957
 
         if (nexc.le.1)then
1958
 
          write(LuOut,222)etnew+enuc,
1959
 
     &                      ecore,
1960
 
     &                      ecoul,
1961
 
     &                      exc(1),
1962
 
     &                      enuc
1963
 
         else
1964
 
          write(LuOut,223)etnew+enuc,
1965
 
     &                      ecore,
1966
 
     &                      ecoul,
1967
 
     &                      exc(1),
1968
 
     &                      exc(2),
1969
 
     &                      enuc
1970
 
         end if
1971
 
         if (do_zora) write(luout,2221) ener_scal
1972
 
         write(luout,2222) rho_n
1973
 
         write(luout,2223) dft_time
1974
 
c
1975
 
 222  format(//
1976
 
     &     '         Total DFT energy =', f20.12/
1977
 
     &     '      One electron energy =', f20.12/
1978
 
     &     '           Coulomb energy =', f20.12/
1979
 
     &     '    Exchange-Corr. energy =', f20.12/
1980
 
     &     ' Nuclear repulsion energy =', f20.12/)
1981
 
c
1982
 
 223  format(//
1983
 
     &     '         Total DFT energy =', f20.12/
1984
 
     &     '      One electron energy =', f20.12/
1985
 
     &     '           Coulomb energy =', f20.12/
1986
 
     &     '          Exchange energy =', f20.12/
1987
 
     &     '       Correlation energy =', f20.12/
1988
 
     &     ' Nuclear repulsion energy =', f20.12/)
1989
 
c
1990
 
 2221 format('       Scaling correction =', f20.12/)
1991
 
 2222 format(' Numeric. integr. density =', f20.12/)
1992
 
 2223 format('     Total iterative time =', f9.1,'s'//)
1993
 
c
1994
 
         call util_flush(LuOut)
1995
 
      endif
1996
 
c
1997
 
c     print out the determinantal energies 
1998
 
c
1999
 
 2001 continue 
2000
 
      if (me.eq.0.and.oprint.and.det_eng)then
2001
 
         write(LuOut,*)
2002
 
c         call util_print_centered(LuOut,
2003
 
c     &        'Calculation failed to converge', 20, .true.)
2004
 
         write(LuOut,*)
2005
 
         dft_time = dft_time+util_cpusec()
2006
 
         if (nexc.le.1)then
2007
 
            write(LuOut,232)etnew+enuc, ecore, Ecoul, Exc(1), enuc, 
2008
 
     &           rho_n, dft_time
2009
 
            if (do_zora) write(luout,2221) ener_scal
2010
 
         else
2011
 
            write(LuOut,233)etnew+enuc, ecore, Ecoul, Exc(1), Exc(2),
2012
 
     &           enuc, rho_n, dft_time
2013
 
            if (do_zora) write(luout,2221) ener_scal
2014
 
         endif
2015
 
 232     format(//
2016
 
     &        '       Determinant Energy'/
2017
 
     &        '         Total DFT energy =', f20.12/
2018
 
     &        '      One electron energy =', f20.12/
2019
 
     &        '           Coulomb energy =', f20.12/
2020
 
     &        '    Exchange-Corr. energy =', f20.12/
2021
 
     &        ' Nuclear repulsion energy =', f20.12//
2022
 
     &        ' Numeric. integr. density =', f20.12//
2023
 
     &        '     Total iterative time =', f9.1,'s'//)
2024
 
 233     format(//
2025
 
     &        '       Determinant Energy'/
2026
 
     &        '         Total DFT energy =', f20.12/
2027
 
     &        '      One electron energy =', f20.12/
2028
 
     &        '           Coulomb energy =', f20.12/
2029
 
     &        '          Exchange energy =', f20.12/
2030
 
     &        '       Correlation energy =', f20.12/
2031
 
     &        ' Nuclear repulsion energy =', f20.12//
2032
 
     &        ' Numeric. integr. density =', f20.12//
2033
 
     &        '     Total iterative time =', f9.1,'s'//)
2034
 
         call util_flush(LuOut)
2035
 
      endif 
2036
 
c
2037
 
c     calculate the determinantal energy
2038
 
c     
2039
 
      if(.not.fon) goto 2002 
2040
 
      if (.not.rtdb_get(rtdb,'sodft:ndet',mt_int,1,ndet)) ndet = 0
2041
 
c
2042
 
      if(idet .eq. 0 .and. ndet. gt. 0)then 
2043
 
           if(.not.ma_push_get(mt_int,nmo_fon*ndet,'det',kfon_occ,
2044
 
     &        lfon_occ))
2045
 
     &        call errquit('cannot alloctate lfon_occ',0, MA_ERR)
2046
 
           if (.not.rtdb_get(rtdb,'sodft:occupancy',mt_int,
2047
 
     &        nmo_fon*ndet,int_mb(lfon_occ)))
2048
 
     &        call errquit('no occupancy specified',0, RTDB_ERR)
2049
 
       endif  ! (idet.eq.0) and (ndet.gt.0)
2050
 
c
2051
 
      if(idet.eq.ndet) goto 2002    ! all determinant energies have been calculated
2052
 
c
2053
 
c     Switch the mo order to match the determinantal occupancy 
2054
 
c
2055
 
      iswap = ncore_fon + 1  ! first partially filled orbital
2056
 
      jswap = iswap + idet ! forms the pair (i,j) to be swapped
2057
 
      swap(1) = iswap  ! assign the pair
2058
 
      swap(2) = jswap  ! assign the pair
2059
 
      if (jswap.ge.iswap) then
2060
 
         if(.not.rtdb_put(rtdb,'sodft:swap',mt_int,2,swap))  ! swap is queried in movecs_swap_so
2061
 
     &        call errquit('swap: failed to put nelem in rtdb', 0,
2062
 
     &       RTDB_ERR)
2063
 
         call movecs_swap_so(rtdb,'dft',scftype,g_moso,   ! do the swap
2064
 
     &        dbl_mb(k_occ),dbl_mb(k_eval(1)))
2065
 
      end if
2066
 
c     
2067
 
c     form a new density matrix and calculate the corresponding energy 
2068
 
c     
2069
 
      call dft_densm_so(g_densso,g_moso,nbf_ao,nTotEl) 
2070
 
      call ga_zero(g_dens(1))
2071
 
      call ga_zero(g_dens(2))
2072
 
      call ga_dens_sf(g_dens, g_densso(1), nbf_ao)
2073
 
      call ga_dadd(one, g_dens(1), one, g_dens(2), g_dens(1))
2074
 
      call ga_sync
2075
 
      det_eng = .true. 
2076
 
c     
2077
 
c     Restore the mo order so that we have a fixed reference
2078
 
c     
2079
 
      if (me.eq.0) write(luout,4443)
2080
 
 4443 format(/,1x,'Restoring orbital order')
2081
 
c
2082
 
      if (jswap.ge.iswap) then
2083
 
         if(.not.rtdb_put(rtdb,'sodft:swap',mt_int,2,swap))  ! swap is queried in movecs_swap_so
2084
 
     &        call errquit('swap: failed to put nelem in rtdb', 0,
2085
 
     &       RTDB_ERR)
2086
 
         call movecs_swap_so(rtdb,'dft',scftype,g_moso,  ! do the swap
2087
 
     &        dbl_mb(k_occ),dbl_mb(k_eval(1)))
2088
 
      end if
2089
 
c
2090
 
c     Write determinant configuration     
2091
 
c
2092
 
      if(me .eq. 0)then 
2093
 
         write(Luout,4444)
2094
 
     &    (int_mb(lfon_occ+idet*(nmo_fon)+i-1), i=1,nmo_fon)
2095
 
      endif
2096
 
      idet = idet + 1   ! determinant counter
2097
 
      go to 1000 
2098
 
 4444 format(/,1x,'Determinant Occupancy: ',8(1x,i3,1x))
2099
 
c
2100
 
      if(fon.and.(ndet.gt.0))then 
2101
 
       if (.not.ma_pop_stack(kfon_occ))
2102
 
     &   call errquit('dft_scf: cannot pop stack:lfon_occ',0,MA_ERR)
2103
 
      endif
2104
 
c     
2105
 
 2002 continue
2106
 
 
2107
 
      if (.not. ga_destroy(g_damp_so(1))) call errquit
2108
 
     &     ('dft_scf_so: could not destroy g_damp_so', 0, GA_ERR)
2109
 
      if (.not. ga_destroy(g_damp_so(2))) call errquit
2110
 
     &     ('dft_scf_so: could not destroy g_damp_so', 0, GA_ERR)
2111
 
c     
2112
 
c     symmetry adapt vectors last time print symmetries, etc.
2113
 
c     
2114
 
c     if (oadapt)then
2115
 
c     call scf_movecs_sym_adapt(ao_bas_han, g_movecs, oprint, 
2116
 
c     &                             nbf_ao, '- alpha', .true., 
2117
 
c     &                             int_mb(k_ir))
2118
 
c         if (ipol.eq.2)
2119
 
c     &      call scf_movecs_sym_adapt(ao_bas_han, g_movecs(2), oprint, 
2120
 
c     &                                nbf_ao, '- beta', .true., 
2121
 
c     &                                int_mb(k_ir+nbf_ao))
2122
 
c      endif      
2123
 
c
2124
 
c     Vector analysis stolen from rohf.F
2125
 
c
2126
 
      if (util_print('final vectors analysis', print_default)) then
2127
 
         do ilo = 1,max(1,nTotEl-10)
2128
 
            if (dbl_mb(k_eval(1)+ilo-1) .ge. eval_pr_tol_lo) 
2129
 
     &           goto 961
2130
 
         enddo
2131
 
 961     do ihi = min(nTotEl+10,nbf_mo), nbf_mo
2132
 
            if (dbl_mb(k_eval(1)+ihi-1) .ge. eval_pr_tol_hi) 
2133
 
     &           goto 9611
2134
 
         enddo
2135
 
         ihi = max(ihi-1,1)
2136
 
 9611    continue
2137
 
         if (util_print('final vectors analysis', print_high)) then
2138
 
            ilo = 1
2139
 
            ihi = nbf_mo
2140
 
         endif
2141
 
         call movecs_anal_so(ao_bas_han, ilo, ihi, 0.15d0, 
2142
 
     &        g_moso, 
2143
 
     &        'DFT Final Molecular Orbital Analysis', 
2144
 
     &        .true., dbl_mb(k_eval(1)), oadapt, 
2145
 
     &        int_mb(k_ir), .true., dbl_mb(k_occ))
2146
 
      endif
2147
 
c     
2148
 
c     call to Mulliken Pop Ananlysis
2149
 
c     
2150
 
      if (mulliken)then
2151
 
         if (me.eq.0)
2152
 
     &      call dft_header
2153
 
     &      (' Total Density - Mulliken Population Analysis')
2154
 
         call mull_pop(geom,ao_bas_han,g_dens(1),g_s, 'total')
2155
 
         if (ipol.eq.2)then
2156
 
c     
2157
 
c           analysis of spin density
2158
 
c     
2159
 
            if (me.eq.0)call dft_header
2160
 
     &         (' Spin Density - Mulliken Population Analysis')
2161
 
            call ga_dadd(one,g_dens(1),-2.d0,g_dens(2),g_dens(2))
2162
 
            call mull_pop(geom,ao_bas_han,g_dens(2),g_s,'spin')
2163
 
            call ga_dadd(one,g_dens(1),-1.d0,g_dens(2),g_dens(2))
2164
 
            call ga_dscal(g_dens(2),0.5d0)
2165
 
         endif
2166
 
      endif
2167
 
c     
2168
 
c     end infinite loop for SCF iterations
2169
 
c     
2170
 
c     Store energy and convergence status ... must store before
2171
 
c     write movecs since date of insertion is used.
2172
 
c     
2173
 
      if (.not. rtdb_put(rtdb, 'sodft:energy',MT_DBL,1,(Etnew+Enuc)))
2174
 
     &   call errquit('dft_scf: failed to store energy in rtdb', 0,
2175
 
     &       RTDB_ERR)
2176
 
      if (.not. rtdb_put(rtdb, 'sodft:converged',MT_LOG,1,converged))
2177
 
     &   call errquit('dft_scf: failed to store converged in rtdb',0,
2178
 
     &       RTDB_ERR)
2179
 
c      if (rtdb_get(rtdb, 'sodft:converged', mt_log, 1, oconverged))
2180
 
c     &     write(*,*)"converged=", converged
2181
 
c
2182
 
c     output energies and eigenvectors to disk
2183
 
c     
2184
 
      if (.not.movecs_write_so
2185
 
     $     (rtdb, ao_bas_han, movecs_out, 'sodft', title,
2186
 
     &     nbf_mo, dbl_mb(k_occ), dbl_mb(k_eval(1)), g_moso))
2187
 
     &     call errquit('dft_scf_so: movec_write failed', 0, DISK_ERR)
2188
 
      call ga_sync()
2189
 
c     
2190
 
c     Shut down DIIS.
2191
 
c     
2192
 
      if (icall(1).gt.0)then
2193
 
         icall(1) = -1
2194
 
         call diis_driver_so(toll_s, derr, icall, nfock, 
2195
 
     &        nbf_mo, g_fockso, g_densso, 
2196
 
     &        g_svecs, svals, diising, nodiis)
2197
 
      endif
2198
 
c     
2199
 
c     If open shell compute overlap of alpha orbitals with beta orbitals.
2200
 
c     
2201
 
      if (ipol.gt.1)then
2202
 
         call dft_mxspin_ovlp(nbf_ao,nmo,ao_bas_han,g_movecs(1), 
2203
 
     &        g_movecs(2), g_tmp)
2204
 
      endif
2205
 
c
2206
 
      if (wght_GA)then
2207
 
         if (.not. ga_destroy(g_wght)) call errquit
2208
 
     &      ('dft_scf: could not destroy g_wght', 0, GA_ERR)
2209
 
         if (.not. ga_destroy(g_xyz)) call errquit
2210
 
     &      ('dft_scf: could not destroy g_xyz', 0, GA_ERR)
2211
 
         if (.not. ga_destroy(g_nq)) call errquit
2212
 
     &      ('dft_scf: could not destroy g_nq', 0, GA_ERR)
2213
 
      endif
2214
 
c     
2215
 
c     Restore alpha and beta densities.
2216
 
c
2217
 
      if (ipol .gt. 1)
2218
 
     &   call ga_dadd(one,g_dens(1),onem,g_dens(2),g_dens(1))
2219
 
c     
2220
 
c     computation of <S2> for open shell
2221
 
c     
2222
 
      if (ipol.gt.1)then
2223
 
         call dft_s2_value(geom,AO_bas_han,.false.,noc(1),noc(2),
2224
 
     &        nbf_ao,g_dens(1),g_dens(2))
2225
 
      endif
2226
 
c     
2227
 
c     computation of multipole moments
2228
 
c
2229
 
      if (natoms .gt. 1)
2230
 
     &   call dft_mpole(rtdb, ao_bas_han, ipol, g_dens(1), g_dens(2))
2231
 
c     
2232
 
c     print stolen for uhf.F
2233
 
c     
2234
 
      if (util_print('schwarz',print_high).and.(.not.CDFIT))then
2235
 
         call schwarz_print(natoms, nshells_ao)
2236
 
      endif
2237
 
c     
2238
 
      if (me .eq. 0)then
2239
 
         if (util_print('final evals', print_high))then
2240
 
            call util_print_centered(LuOut,'Final eigenvalues',
2241
 
     &           20,.false.)
2242
 
            call util_print_centered(LuOut,
2243
 
     &           '(all occupied plus 10 virtual)',20,.true.)
2244
 
            call output(dbl_mb(k_eval(1)),
2245
 
     &           1, min(noc(1)+10,nbf_ao),
2246
 
     &           1, 1, nbf_ao, 1, 1)
2247
 
            call util_flush(6)
2248
 
         endif  ! util_print
2249
 
c
2250
 
         if (oprint_final_vecs)then
2251
 
            write(LuOut,*)
2252
 
            call util_print_centered(
2253
 
     &           LuOut,'Final MO vectors',40,.true.)
2254
 
            write(LuOut,*)
2255
 
            call util_flush(LuOut)
2256
 
         endif
2257
 
      endif
2258
 
c
2259
 
      if (oprint_final_vecs)then
2260
 
cso         call ga_print(g_movecs)
2261
 
cso         if (ipol.eq.2)call ga_print(g_movecs(2))
2262
 
      endif
2263
 
c     
2264
 
c     ECCE printout
2265
 
c     
2266
 
      call movecs_ecce(nbf_ao, nmo, 1, nmo(1), dbl_mb(k_eval(1)),
2267
 
     &                 dbl_mb(k_occ), int_mb(k_ir), 
2268
 
     &                 g_movecs(1), 'dft', 'alpha')
2269
 
      if (ipol.eq.2)then ! spin-unrestricted
2270
 
         call movecs_ecce(nbf_ao, nmo, 1, nmo(2), dbl_mb(k_eval(2)),
2271
 
     &                    dbl_mb(k_occ+nbf_ao), int_mb(k_ir+nbf_ao), 
2272
 
     &                    g_movecs(2), 'dft', 'beta')
2273
 
      endif
2274
 
      call ecce_print1 ('total energy', mt_dbl, (Etold+Enuc), 1)
2275
 
      call ecce_print1 ('nuclear repulsion energy', mt_dbl, Enuc, 1)
2276
 
      call ecce_print1 ('coulomb energy', mt_dbl, Ecoul, 1)
2277
 
      call ecce_print1 ('exchange energy', mt_dbl, Exc(1), 1)
2278
 
      if (nexc.gt.1) then
2279
 
         call ecce_print1 ('correlation energy', mt_dbl, Exc(2), 1)
2280
 
      endif
2281
 
c
2282
 
      if (.not.ma_chop_stack(lbuff)) then
2283
 
        call ma_summarize_allocated_blocks()
2284
 
        call util_flush(6)
2285
 
        call util_flush(0)
2286
 
        call errquit('dft_scf: cannot pop stack:lbuff',lbuff, MA_ERR)
2287
 
      endif
2288
 
c
2289
 
      if (.not.ma_pop_stack(lrwork))
2290
 
     &   call errquit('dft_scf: cannot pop stacklrwork',0, MA_ERR)
2291
 
      if (.not.ma_pop_stack(lwork))
2292
 
     &   call errquit('dft_scf: cannot pop stack:lwork',0, MA_ERR)
2293
 
      if (.not.ma_pop_stack(lw))
2294
 
     &   call errquit('dft_scf: cannot pop stack:lw',0, MA_ERR)
2295
 
      if (.not.ma_pop_stack(la))
2296
 
cso
2297
 
     &   call errquit('dft_scf: cannot pop stack:la',0, MA_ERR)
2298
 
      if (.not.ma_pop_stack(l_ir))
2299
 
     &   call errquit('dft_scf: cannot pop stack:l_ir',0, MA_ERR)
2300
 
c     
2301
 
      if (ipol.gt.1)then
2302
 
         if (.not. ga_destroy(g_fockt)) call errquit
2303
 
     &      ('dft_scf: could not destroy g_fockt', 0, GA_ERR)
2304
 
      endif
2305
 
      if (.not. ga_destroy(g_tmp)) call errquit
2306
 
     &   ('dft_scf: could not destroy g_tmp', 0, GA_ERR)
2307
 
c
2308
 
      call fock_2e_tidy(rtdb)
2309
 
c     
2310
 
      if (converged)then
2311
 
         call ecce_print_module_exit('dft', 'ok')
2312
 
      else
2313
 
         call ecce_print_module_exit('dft', 'failed')
2314
 
      endif
2315
 
c     
2316
 
c     eval deallocation moved here from inside iteration loop
2317
 
c     
2318
 
      if (.not.ma_pop_stack(l_eval))
2319
 
     &   call errquit('dft_scf: cannot pop stack:l_eval',0, MA_ERR)
2320
 
      if (CDFIT)then
2321
 
         if (.not.ma_pop_stack(l_3cwhat))
2322
 
     &      call errquit('dft_scf: cannot pop stack:l_3cwhat',0, MA_ERR)
2323
 
         if (.not.ma_pop_stack(l_3cERI))
2324
 
     &      call errquit('dft_scf: cannot pop stack:l_3cERI',0, MA_ERR)
2325
 
      endif
2326
 
      if (.not.ma_pop_stack(l_occ))
2327
 
     &   call errquit('dft_scf: cannot pop stack:l_occ',0, MA_ERR)
2328
 
      if (.not.ma_pop_stack(lrdens_atom))
2329
 
     &   call errquit('dft_scf: cannot pop stack:lrdens_atom',0, MA_ERR)
2330
 
      if (.not.ma_pop_stack(lcetobfr))
2331
 
     &   call errquit('dft_scf: cannot pop stack:lcetobfr',0, MA_ERR)
2332
 
      if (.not.ma_pop_stack(lcntobfr))
2333
 
     &   call errquit('dft_scf: cannot pop stack:lcntobfr',0, MA_ERR)
2334
 
      if (.not.ma_pop_stack(lcntoce))
2335
 
     &   call errquit('dft_scf: cannot pop stack:lcntoce',0, MA_ERR)
2336
 
c
2337
 
      if(.not.ga_destroy(g_moso(1)))     
2338
 
     &     call errquit('dft_scf_so: error destroy Movecs Re',0, GA_ERR)
2339
 
      if(.not.ga_destroy(g_moso(2)))     
2340
 
     &     call errquit('dft_scf_so: error destroy Movecs Im',0, GA_ERR)
2341
 
      if(.not.ga_destroy(g_fockso(1)))     
2342
 
     &     call errquit('dft_scf_so: error destroy Fock Re',0, GA_ERR)
2343
 
      if(.not.ga_destroy(g_fockso(2)))     
2344
 
     &     call errquit('dft_scf_so: error destroy Fock Im',0, GA_ERR)
2345
 
      if(.not.ga_destroy(g_densso(1)))     
2346
 
     &     call errquit('dft_scf_so: error destroy DenMx Re',0, GA_ERR)
2347
 
      if(.not.ga_destroy(g_densso(2)))     
2348
 
     &     call errquit('dft_scf_so: error destroy DenMx Im',0, GA_ERR)
2349
 
      if(.not.ga_destroy(g_tmp_ri))     
2350
 
     &     call errquit('dft_scf_so: error destroy old re',0, GA_ERR)
2351
 
      if(.not.ga_destroy(g_old(1)))     
2352
 
     &     call errquit('dft_scf_so: error destroy old im',0, GA_ERR)
2353
 
      if(.not.ga_destroy(g_old(2)))     
2354
 
     &     call errquit('dft_scf_so: error destroy Tmp ReIm',0, GA_ERR)
2355
 
      if(.not.ga_destroy(g_so(1)))     
2356
 
     &     call errquit('dft_scf_so: error destroy so z',0, GA_ERR)
2357
 
      if(.not.ga_destroy(g_so(2)))     
2358
 
     &     call errquit('dft_scf_so: error destroy so y',0, GA_ERR)
2359
 
      if(.not.ga_destroy(g_so(3)))     
2360
 
     &     call errquit('dft_scf_so: error destroy so x',0, GA_ERR)
2361
 
c
2362
 
c     == deallocate zora arrays ==
2363
 
      if (do_zora) then
2364
 
c
2365
 
c      == spin-free parts ==
2366
 
       if (.not. ga_destroy(g_zora_sf(1))) call errquit(
2367
 
     &   'dft_scf_so: ga_destroy failed zora sf 1',0, GA_ERR)
2368
 
       if (.not. ga_destroy(g_zora_sf(2))) call errquit(
2369
 
     &   'dft_scf_so: ga_destroy failed zora sf 2',0, GA_ERR)
2370
 
c
2371
 
       if (.not. ga_destroy(g_zora_scale_sf(1))) call errquit(
2372
 
     &   'dft_scf_so: ga_destroy failed zora scale sf 1',0, GA_ERR)
2373
 
       if (.not. ga_destroy(g_zora_scale_sf(2))) call errquit(
2374
 
     &   'dft_scf_so: ga_destroy failed zora scale sf 2',0, GA_ERR)
2375
 
c
2376
 
c      == spin-orbit parts ==
2377
 
       if(.not.ga_destroy(g_zora_so(1))) call errquit(
2378
 
     &   'dft_scf_so: ga_destroy failed zora so z',0, GA_ERR)
2379
 
       if(.not.ga_destroy(g_zora_so(2))) call errquit(
2380
 
     &   'dft_scf_so: ga_destroy failed zora so y',0, GA_ERR)
2381
 
       if(.not.ga_destroy(g_zora_so(3))) call errquit(
2382
 
     &   'dft_scf_so: ga_destroy failed zora so x',0, GA_ERR)
2383
 
c
2384
 
       if(.not.ga_destroy(g_zora_scale_so(1))) call errquit(
2385
 
     &   'dft_scf_so: ga_destroy failed zora so scale z',0, GA_ERR)
2386
 
       if(.not.ga_destroy(g_zora_scale_so(2))) call errquit(
2387
 
     &   'dft_scf_so: ga_destroy failed zora so scale y',0, GA_ERR)
2388
 
       if(.not.ga_destroy(g_zora_scale_so(3))) call errquit(
2389
 
     &   'dft_scf_so: ga_destroy failed zora so scale x',0, GA_ERR)
2390
 
c
2391
 
      end if  !do_zora
2392
 
c
2393
 
      dft_scf_so = converged
2394
 
c
2395
 
c !!! BGJ
2396
 
      if (.not. rtdb_get(rtdb, 'bgj:poliz', mt_log,
2397
 
     &     1, do_poliz)) then
2398
 
         do_poliz = .false.
2399
 
      endif
2400
 
      if (do_poliz) then
2401
 
c         write(*,*)'*** dft_scf: calling cphf_poliz'
2402
 
         if (.not. cphf_poliz(rtdb)) ! Never executed.
2403
 
     $        call errquit(' cphf_poliz: failed from dft_scf !',0,
2404
 
     &       CALC_ERR)
2405
 
      endif
2406
 
c !!! BGJ
2407
 
      return
2408
 
c     
2409
 
 1111 format(15x,'Core Energy:              ',f20.10)
2410
 
c     
2411
 
      end
2412