-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model_OEP.f90
1582 lines (1374 loc) · 46.3 KB
/
Model_OEP.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!-------------------------------------------------------------------------------
! Implements in RDMFT the model potential:
! Ryabinkin, Kananenka, Staroverov, Phys. Rev. Lett 111, 013001 (2013) (for HF)
! Ryabinkin, Kohut, Staroverov, Phys. Rev. Lett 115, 083001 (2015) (for any WF)
!-------------------------------------------------------------------------------
subroutine model_local_pot(x0,y0,z0,x1,y1,z1)
use global; use orbocc; use grid_params; use matrices; use functional_m; use energies; implicit none
!..Arguments
real(dp), intent(in) :: x0,y0,z0,x1,y1,z1
!..Local
integer :: ig, ia, ib,k, l, iter, Niter, Nist=1500, ist, icall=0, Ndiis, Nmix, mode, maxorb_0, IP_meth, id, jd
real(dp) :: x, y, z, delt=1.e10_dp, delta_rho, emax=1.e10_dp
real(dp), allocatable :: rho(:), pot_grid_0(:), pot_grid(:), Vloc_0(:,:), Vloc(:,:), rho_WF(:)
real(dp), allocatable :: DD(:,:,:), H_0(:,:), H_eff_0(:,:), I_aver_WF(:), tau_WF(:), I_aver(:), tau(:)
real(dp), allocatable :: rho_p(:), pot_p(:), pot_p_0(:), rho_p_WF(:), H_extrap(:,:)
real(dp), allocatable :: I_aver_p_WF(:), tau_p_WF(:), r_d(:)
real(dp), allocatable :: I_aver_p(:), tau_p(:), x_p(:), y_p(:), z_p(:), vec_can(:,:), ennat_can(:)
real(dp) :: xstep, ystep, zstep, xmix_sta, conv, conv_diis, Total_Energy, e_IP
real(dp) :: x_infty(1),y_infty(1),z_infty(1),rho_infty(1),I_aver_infty(1),tau_infty(1), E_kin
real(dp), external :: electron_density
logical :: print_in_all_steps
!..Parameters
Niter=500 ! Number of iterations for Staroverov potential
Nmix=50! Number of steps with only mixing (before DIIS)
xmix_sta=0.2_dp !Mixing for convergence of Staroverov potential
print_in_all_steps=.false. !Print Staroverov potential in all steps
Ndiis=6 ! Number of previous Hamiltonians used in the extrapolation
conv=1e-9_dp ! convergence in potential change
conv_diis=1e-12_dp ! convergence in DIIS error
IP_meth=1 ! 1: usual EKT, 2: asymptotic behavior of epsilon 3: shift
call read_basis()
call orbs_on_grid(maxorb)
allocate ( rho(ngrid), rho_WF(ngrid), pot_grid_0(ngrid), pot_grid(ngrid) )
allocate ( I_aver_WF(ngrid), tau_WF(ngrid), I_aver(ngrid), tau(ngrid) )
allocate ( Vloc_0(lnbasis,lnbasis), Vloc(lnbasis,lnbasis), DD(lnbasis,lnbasis, lnnatorb) )
allocate ( H_0(lnbasis, lnbasis), H_eff_0(lnbasis,lnbasis), H_extrap(lnbasis,lnbasis) )
allocate ( rho_p(Nist), pot_p_0(Nist), pot_p(Nist), rho_p_WF(Nist) )
allocate ( I_aver_p(Nist), tau_p(Nist), x_p(Nist), y_p(Nist), z_p(Nist) )
allocate ( I_aver_p_WF(Nist), tau_p_WF(Nist), r_d(Nist), vec_can(lnbasis, lnnatorb), ennat_can(lnnatorb) )
!..The density matrix
call construct_DD(nbasis,maxorb,vecnat,DD)
!..Generate initial effective Hamiltonian (except xc part)
call Calc_H0(DD,H_0)
!..The density
do ig=1,ngrid
! rho_WF(ig) = 0._dp
! do ia=1,maxorb
! rho_WF(ig) = rho_WF(ig) + occnum(ia,3)*orbgridsq(ig,ia)
! enddo
! rho_WF(ig) = max(rho_WF(ig),small)
rho_WF(ig) = electron_density(x_grid(ig),y_grid(ig),z_grid(ig),3)
enddo
!..Find the Ionization Potential
x_infty=0._dp; y_infty=0._dp; z_infty=10._dp
if (functional == 'RHF' .or. functional == 'RHA' ) then
mode=0 ! Use the KS/HF definition of I_aver
rho_infty(1)=electron_density(x_infty(1),y_infty(1),z_infty(1),3)
call average_local_IE_tau(mode, 1,x_infty,y_infty,z_infty,rho_infty,DD,I_aver_infty,tau_infty)
print*,'*********************************************************'
print*,' E_HOMO HF = ', ennat(nele(1))
print*,'Iaver at infinity= ', I_aver_infty, ' a.u.; Tau at infinity= ', tau_infty(1)
print*,'*********************************************************'
if (IP_meth == 1) then
e_IP=ennat(nele(1))
print*,'IP target selected from Koopmans:', e_IP, ' a.u.'
print*,'*********************************************************'
else
e_IP=I_aver_infty(1)
print*,'IP target selected from Iaver at Infinity:', e_IP, ' a.u.'
print*,'*********************************************************'
endif
else !(functional /= 'RHF')
mode=1 ! Use the WF for the calculation of I_aver
call canonical_orbitals(IP_meth, e_IP) ! EKT
rho_infty(1)=electron_density(x_infty(1),y_infty(1),z_infty(1),3)
call average_local_IE_tau(mode, 1,x_infty,y_infty,z_infty,rho_infty,DD,I_aver_infty,tau_infty)
print*,'*********************************************************'
print*,' E_HOMO from EKT=', e_IP, ' a.u.'
print*,'Iaver at infinity= ', I_aver_infty(1), ' a.u.; Tau at infinity= ', tau_infty(1)
print*,'*********************************************************'
if (IP_meth == 1) then
print*,'IP target selected from EKT=', e_IP, ' a.u.'
print*,'*********************************************************'
else
e_IP=I_aver_infty(1)
print*,'IP target selected from Iaver at Infinity:', e_IP, ' a.u.'
print*,'*********************************************************'
endif
endif !(functional == 'RHF')
!..Slater potential, I_aver_WF, tau_WF on the grid
pot_grid=0._dp
call Slater_potential(Ngrid,x_grid,y_grid,z_grid,pot_grid,rho_WF)
call average_local_IE_tau(mode, Ngrid,x_grid,y_grid,z_grid,rho_WF,DD,I_aver_WF,tau_WF)
do ig=1,ngrid
pot_grid_0(ig) = pot_grid(ig) - I_aver_WF(ig) + tau_WF(ig)
enddo
!..Now: pot_grid is the Slater potential; pot_grid_0 is a constant that we add quntities below
!..FOR PLOTTING
!..Slater potential, I_aver_WF, tau_WF on the plotting line
xstep=(x1-x0)/real(Nist-1); ystep=(y1-y0)/real(Nist-1); zstep=(z1-z0)/real(Nist-1)
x=x0; y=y0; z=z0
do ist=1,Nist
x_p(ist)=x; y_p(ist)=y; z_p(ist)=z
rho_p_WF(ist) = electron_density(x,y,z,3)
! rho_p_WF(ist) = max(small,rho_p_WF(ist))
r_d(ist) = sqrt((x-x0)**2+(y-y0)**2+(z-z0)**2)
x=x+xstep; y=y+ystep; z=z+zstep
enddo
pot_p=0._dp
call Slater_potential(Nist,x_p,y_p,z_p,pot_p,rho_p_WF)
call average_local_IE_tau(mode, Nist,x_p,y_p,z_p,rho_p_WF,DD,I_aver_p_WF,tau_p_WF)
open(unit=861,file='Staroverov.dat',status='unknown')
open(unit=860,file='Slater.dat',status='unknown')
do ist=1,Nist
pot_p_0(ist)=pot_p(ist) - I_aver_p_WF(ist) + tau_p_WF(ist)
write(860,'(4e20.10)')r_d(ist), pot_p(ist), I_aver_p_WF(ist), tau_p_WF(ist)
! rho_p_WF(ist)*4.d0*Pi*r_d(ist)**2, tau_p_WF(ist)*rho_p_WF(ist)*4.d0*Pi*r_d(ist)**2
enddo
close(860)
!..END PLOTTING
!..Matrix elements of the model potential (grid integration) / add to effective Hamiltonian
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(k,l,ig)
!$OMP DO
do k=1,nbasis
do l=1,k
Vloc(k,l)=0._dp
do ig=1,ngrid
Vloc(k,l)=Vloc(k,l)+w_grid(ig)*pot_grid(ig)*bas_f_grid(ig,k)*bas_f_grid(ig,l)
enddo
Vloc(l,k)=Vloc(k,l)
H_eff_0(k,l) = H_0(k,l)+Vloc(k,l)
H_eff_0(l,k) = H_eff_0(k,l)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!..Now read the initial guess (starting orbitals for iteration 1)
!..Orbitals and energies will replace WF values
call read_vec_e()
!..Idempotent KS system:
maxorb_0=maxorb
mode=0; maxorb=nele(1); occnum(1:maxorb,1)=1._dp; occnum(1+maxorb:nnatorb,1)=zero
occnum(:,2)=occnum(:,1); occnum(:,3)=occnum(:,1)+occnum(:,2)
call calc_occ_factors(occnum, fac_h, fac_c, fac_e)
!..Iterate the potential
do iter=1, Niter
print*,'----------------------------------------------------------------------------'
print*,'Iteration:', iter
!.....Redefine DD
call construct_DD(nbasis,maxorb,vecnat,DD)
!.....Generate effective Hamiltonian (except xc part)
call Calc_H0(DD,H_0)
!.....Shift orbital energies
if ( iter > 1) then
do ia=1,maxorb
ennat(ia)=ennat(ia)-ennat(maxorb) + e_IP! Shift energies to match EKT IP
enddo
endif
!.....Recalculate the density, I_aver, tau, for the new orbitals, ennat
do ig=1, ngrid
rho(ig) = electron_density(x_grid(ig),y_grid(ig),z_grid(ig),3)
enddo
call average_local_IE_tau(mode,Ngrid,x_grid,y_grid,z_grid,rho,DD,I_aver,tau)
delta_rho=0._dp
do ig=1, ngrid
pot_grid(ig) = pot_grid_0(ig) + I_aver(ig) - tau(ig)
delta_rho=delta_rho+w_grid(ig)*abs(rho(ig)-rho_WF(ig))
enddo
delta_rho=delta_rho/real(nele(3))
!.....Print Staroverov potential in every step (1 set for xmgrace)
if ( print_in_all_steps ) then
do ist=1,Nist
rho_p(ist) = electron_density(x_p(ist),y_p(ist),z_p(ist),3)
enddo
call average_local_IE_tau(mode,Nist,x_p,y_p,z_p,rho_p,DD,I_aver_p,tau_p)
do ist=1,Nist
pot_p(ist)=pot_p_0(ist) + I_aver_p(ist) - tau_p(ist)
write(861,'(5e20.10)')r_d(ist), pot_p(ist), I_aver_p(ist), tau_p(ist)
enddo
write(861,*) ' '
endif
!..Matrix elements of the model potential (grid integration) / add to effective Hamiltonian
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(k,l,ig)
!$OMP DO
do k=1,nbasis
do l=1,k
Vloc(k,l)=0._dp
do ig=1,ngrid
Vloc(k,l)=Vloc(k,l)+w_grid(ig)*pot_grid(ig)*bas_f_grid(ig,k)*bas_f_grid(ig,l)
enddo
Vloc(l,k)=Vloc(k,l)
H_eff(k,l) = H_0(k,l)+Vloc(k,l)
H_eff(l,k) = H_eff(k,l)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
if (iter < Nmix) then
!.....Just Mixing
if (iter > 1) then
H_eff=xmix_sta*H_eff+(1._dp - xmix_sta)*H_eff_0
endif
H_eff_0=H_eff
else
!.....Apply DIIS convergence extrapolation
call DIIS_extrapolation(icall, Ndiis, H_eff, H_extrap, emax)
H_eff_0=H_eff
H_eff=H_extrap
endif
!.....The effective KS Hamiltonian/diagonalization
call diagon_lapack(lnbasis,H_eff,ovlap,ennat,vecnat)
print*,'e_IP',e_IP
print*,'Eigenvalues:'
write(6,'(5F20.12)')(ennat(ia),ia=1,maxorb)
print*, ' '
write(6,'(" E_HOMO: ",F20.12," a.u.,",F20.12," eV")')ennat(maxorb), ennat(maxorb)*ha_ev
print*, ' '
!.....Check convergence
if(iter > 1) then
delt=0._dp
do k=1,nbasis
do l=1,k
delt=delt+(Vloc(k,l)-Vloc_0(k,l))**2
enddo
enddo
delt=2._dp*sqrt(delt)/real(nbasis*(nbasis-1))
print*,'Delt:',delt,' Delta_rho:',delta_rho
if(iter> Nmix) then
print*,'Maximum DIIS error:',emax
print*,' '
if ( iter > Nmix) print*,'Emax,conv_diis:',emax , conv_diis
endif
if ( delt < conv .or. emax < conv_diis ) exit
endif
Vloc_0 = Vloc
print*,'............................................................................'
enddo ! iter=1,Niter
!..Print final Staroverov potential in unit 861 (Staroverov.dat):
do ist=1,Nist
rho_p(ist) = electron_density(x_p(ist),y_p(ist),z_p(ist),3)
enddo
call average_local_IE_tau(mode,Nist,x_p,y_p,z_p,rho_p,DD,I_aver_p,tau_p)
do ist=1,Nist
pot_p(ist)=pot_p_0(ist) + I_aver_p(ist) - tau_p(ist)
write(861,'(5e20.10)')r_d(ist), pot_p(ist), I_aver_p(ist), tau_p(ist)
enddo
call Kinetic_energy(E_kin)
Print*,'T_s: ',E_kin
close(861)
maxorb=maxorb_0
deallocate ( rho, rho_WF, pot_grid_0, pot_grid, I_aver_WF, tau_WF, I_aver, tau, Vloc_0)
deallocate ( Vloc, DD, H_0, H_eff_0, H_extrap, rho_p, pot_p_0, pot_p, rho_p_WF)
deallocate ( I_aver_p, tau_p, I_aver_p_WF, tau_p_WF, r_d, x_p, y_p, z_p )
print*,'Target IP=', e_IP
end subroutine model_local_pot
!-----------------------------------------------------------------------------------------------
! Calculates the slater potential (pot) at the point (x,y,z) in real space
subroutine Slater_potential(Np,x,y,z, pot, rho)
use global; use grid_params; use orbocc; use functional_m, ONLY:maxorb;implicit none
!..Arguments
integer, intent(in) :: Np
real(dp), intent(in) :: x(Np),y(Np),z(Np)
real(dp), intent(in) :: rho(Np)
real(dp), intent(out) :: pot(Np)
!..Local
integer :: ia, ib, ig , k, l, ip
real(dp) :: xint, rr, ff, ss, xx, yy, zz
real(dp) :: vc(lnnatorb), vc2(lnnatorb), r(ngrid), x_int_kl(lnbasis, lnbasis), x_int_ka(lnbasis, lnnatorb)
real(dp) :: xint_b(lnnatorb), xint_c, fac_h_HF(lnnatorb), fac_c_HF(lnnatorb, lnnatorb)
real(dp) :: fac_e_HF(lnnatorb,lnnatorb), fac_n(lnnatorb,lnnatorb)
real(dp), external :: f_bas, electron_density
logical :: analytic_integral
!..To do analytically the integrals I_kl(r)= \int dr_1 \frac{\chi_k(r_1) \chi_l(r_1)}{|r-r_1|}
!..where chi_k and chi_l are basis functions.
analytic_integral=.true.
call RHF(occnum, fac_h_HF, fac_c_HF, fac_e_HF)
fac_n=fac_c-fac_c_HF
if ( analytic_integral ) then
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ip, l, k, ia, ib, ff, vc, vc2, xint, rr, xx, yy, zz, ss, r, x_int_kl, x_int_ka, xint_b, xint_c)
!$OMP DO
do ip=1,Np
pot(ip)=0._dp
xx=x(ip); yy=y(ip); zz=z(ip)
vc=0._dp
do l=1,nbasis
ff=f_bas(l, xx, yy, zz)
do ia=1,maxorb
vc(ia)=vc(ia)+vecnat(l,ia)*ff
enddo
enddo
do ia=1,maxorb
vc2(ia)=vc(ia)*vc(ia)
enddo
do k=1,nbasis
do l=1,k
xint=0._dp
call Xintegral2(k,l,xx,yy,zz,xint)
x_int_kl(k,l)=xint
x_int_kl(l,k)=xint
enddo
enddo
do ia=1,maxorb
do k=1,nbasis
ss=0._dp
do l=1,nbasis
ss=ss+vecnat(l,ia)*x_int_kl(k,l)
enddo
x_int_ka(k,ia)=ss
enddo
enddo
do ia=1,maxorb
xint_b(ia)=0._dp
do k=1,nbasis
xint_b(ia)=xint_b(ia)+x_int_ka(k,ia)*vecnat(k,ia)
enddo
enddo
do ia=1,maxorb
do ib=1,maxorb
xint=0._dp
do k=1,nbasis
xint=xint+vecnat(k,ia)*x_int_ka(k,ib)
enddo
xint = fac_e(ia,ib)*vc(ia)*vc(ib)*xint
xint_c= fac_n(ia,ib)*vc2(ia)*xint_b(ib)
pot(ip)=pot(ip) + xint + xint_c
enddo
enddo
pot(ip)=2._dp*pot(ip)/rho(ip)
enddo
!$OMP END DO
!$OMP END PARALLEL
else ! not analytic_integral
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ip, l, k, ia, ib, ig, ff, vc, xint, rr, xx, yy, zz, ss, r, vc2, xint_b, xint_c )
!$OMP DO
do ip=1,Np
pot(ip)=0._dp
xx=x(ip); yy=y(ip); zz=z(ip)
vc=0._dp
do l=1,nbasis
if (Np == Ngrid) then
ff = bas_f_grid(ip,l)
else
ff=f_bas(l, xx, yy, zz)
endif
do ia=1,maxorb
vc(ia)=vc(ia)+vecnat(l,ia)*ff
enddo
enddo
do ia=1,maxorb
vc2(ia)=vc(ia)*vc(ia)
enddo
do ig=1,ngrid
r(ig)=0._dp
rr=sqrt( (x_grid(ig)-xx)**2&
+ (y_grid(ig)-yy)**2&
+ (z_grid(ig)-zz)**2)
if (rr > small) r(ig)=w_grid(ig)/rr
enddo
do ia=1,maxorb
xint_b(ia)=0._dp
do ig=1,ngrid
if(r(ig)>zero) xint_b(ia)=xint_b(ia)+r(ig)*vec_nat_grid(ig,ia)*vec_nat_grid(ig,ia)
enddo
enddo
do ia=1,maxorb
do ib=1,maxorb
xint=0._dp
do ig=1,ngrid
if(r(ig) > zero) xint=xint+r(ig)*vec_nat_grid(ig,ia)*vec_nat_grid(ig,ib)
enddo
! xint= occnum(ia,3)*occnum(ib,3)*vc(ia)*vc(ib)*xint
xint= fac_e(ia,ib)*vc(ia)*vc(ib)*xint
xint_c= fac_n(ia,ib)*vc2(ia)*xint_b(ib)
pot(ip) = pot(ip) + xint + xint_c
enddo
enddo
! pot(ip)=-pot(ip)/(2.d0*rho(ip))
pot(ip)=2._dp*pot(ip)/rho(ip)
enddo
!$OMP END DO
!$OMP END PARALLEL
endif
return
end subroutine Slater_potential
!----------------------------------------------------------------------------------------------
subroutine average_local_IE_tau(mode,Np,x,y,z,rho,DD,I_aver,tau)
use global; use files; use grid_params; use matrices, only:e_ab
use orbocc, only:occnum, vecnat, ennat, fac_h;use functional_m, only:maxorb; implicit none
!..Arguments
integer, intent(in) :: Np, mode
real(dp), INTENT(in) :: x(Np),y(Np),z(Np), rho(Np)
real(dp), INTENT(out) :: I_aver(Np)
real(dp), INTENT(out) :: tau(Np)
real(dp), INTENT(in) :: DD(lnbasis,lnbasis, lnnatorb)
!..Local
integer :: ip, ia, ib, k, l
real(dp) :: BB(lnbasis,lnbasis), Bs(Np,lnbasis)
real(dp) :: gx(lnbasis), gy(lnbasis), gz(lnbasis)
real(dp) :: G_lap, psum, rr
real(dp) , external :: f_bas_Laplacian, f_bas
real(dp) :: pr, Q, ss, rrn(Np)
real(dp) :: phi_ia, phi_ib, g_ia_x, g_ia_y, g_ia_z, g_ib_x, g_ib_y, g_ib_z,rho_orb
integer :: I_kinet
integer :: i, j, nblock, imin, imax
real(dp) :: vecekt(lnbasis,lnnatorb), e_a(lnbasis), vecmcscf(lnbasis,lnnatorb), den
real(dp), allocatable :: H_lang(:,:), delta_mat(:,:), en_lang(:), vec_lang(:, :)
character(22) :: dumm
logical :: ffile
I_aver=0._dp
tau =0._dp
!..constructs the average local ionization potential on the grid
do ip=1,Np
do k=1,nbasis
Bs(ip,k)= f_bas(k,x(ip),y(ip),z(ip))
enddo
enddo
if (mode == 0) then !I_KS (using ennat)
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ip, ia, l, k, psum, BB)
!$OMP DO
do ip=1,Np
do l=1,nbasis
do k=1,nbasis
BB(k,l)= Bs(ip,k)*Bs(ip,l)
enddo
enddo
do ia = 1, maxorb !only for closed shell implemented
psum=0._dp
do l=1, nbasis
do k=1, nbasis
psum = psum + DD(k,l,ia)*BB(k,l)
enddo
enddo
psum = psum*ennat(ia)
I_aver(ip) = I_aver(ip) + psum
enddo
I_aver(ip)=2._dp*I_aver(ip)/rho(ip)
enddo
!$OMP END DO
!$OMP END PARALLEL
elseif (mode == 1) then !I_WF (using non diagonal lagrange multipliers)
!.... Diagonalize lagrangian matrix
allocate ( H_lang(maxorb,maxorb), delta_mat(maxorb,maxorb), en_lang(maxorb), vec_lang(maxorb,maxorb) )
do ia=1,maxorb
do ib=1,ia
delta_mat(ia,ib)=0._dp
! den=max(sqrt(0.5d0*(occnum(ia,1)*occnum(ib,1)+occnum(ia,2)*occnum(ib,2))),zero)
H_lang(ia,ib)=0.5_dp*(e_ab(ia,ib)+e_ab(ib,ia)) !/den
H_lang(ib,ia)=H_lang(ia,ib)
enddo
delta_mat(ia,ia)=1._dp
enddo
call diagon_lapack(maxorb,H_lang,delta_mat,en_lang,vec_lang)
! print*,'--------------------------------------------------------------'
! print*,'Eigenvalues of Lagrangian:'
! print*,en_lang
! print*,'--------------------------------------------------------------'
!.....Convert vec_lang to vecekt
do ia=1,maxorb
do k=1,nbasis
vecekt(k,ia)=0._dp
do ib=1,maxorb
vecekt(k,ia)=vecekt(k,ia) + vec_lang(ib,ia)*vecnat(k,ib)
enddo
enddo
enddo
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ip, ia, ib, l, k, psum, BB)
!$OMP DO
do ip=1,Np
do l=1,nbasis
do k=1,nbasis
BB(k,l)= Bs(ip,k)*Bs(ip,l)
enddo
enddo
do ia=1, maxorb
psum=0._dp
do l=1, nbasis
do k=1, nbasis
psum = psum + vecekt(k,ia)*vecekt(l,ia)*BB(k,l)
enddo
enddo
psum = psum*en_lang(ia)
I_aver(ip) = I_aver(ip) + psum
enddo
I_aver(ip)=2._dp*I_aver(ip)/rho(ip)
enddo
!$OMP END DO
!$OMP END PARALLEL
else !(mode ne 1,2)
stop 'model_OEP: Wrong mode'
endif !mode = 0
I_kinet = 2 !recommended
if ( I_kinet == 1 ) then !I_kinet == 1: Never worked properly
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ip,ia, l, k, G_lap, BB, psum)
!$OMP DO
do ip=1,Np
do k=1,nbasis
G_lap = f_bas_Laplacian(k,x(ip),y(ip),z(ip))
do l=1,nbasis
BB(k,l)= Bs(ip,l)*G_lap
enddo
enddo
do ia = 1 , maxorb !only for closed shell implemented
psum=0._dp
do k=1,nbasis
do l=1, nbasis
psum = psum+BB(k,l)*DD(k,l,ia)
enddo
enddo
psum=psum*occnum(ia,3)
tau(ip) = tau(ip) + psum
enddo
tau(ip)=-0.5_dp*tau(ip)/rho(ip)
enddo
!$OMP END DO
!$OMP END PARALLEL
elseif ( I_kinet == 2 ) then !use grad**2 : tested and working
!!$OMP PARALLEL DEFAULT(SHARED) &
!!$OMP PRIVATE(ip,ia, l, k, psum, gx, gy, gz, BB, rr)
!!$OMP DO REDUCTION(+:tau)
do ip=1,Np
do k=1,nbasis
gx(k)=0._dp; gy(k)=0._dp; gz(k)=0._dp
call gradient_f_bas(k, x(ip), y(ip), z(ip), gx(k), gy(k), gz(k))
enddo
do k=1,nbasis
do l=1,k
BB(k,l)=gx(k)*gx(l)+gy(k)*gy(l)+gz(k)*gz(l)
BB(l,k)=BB(k,l)
enddo
enddo
do ia = 1, maxorb !only for closed shell implemented
psum = 0._dp
do k=1,nbasis
do l=1, nbasis
psum = psum + DD(k,l,ia)*BB(k,l)
enddo
enddo
psum = 0.5_dp*fac_h(ia)*psum
tau(ip) = tau(ip) + psum
enddo
tau(ip)=tau(ip)/rho(ip)
enddo
!!$OMP END DO
!!$OMP END PARALLEL
else ! Use t_P for modified RKS method : To be corrected! Never worked properly
!!$OMP PARALLEL DEFAULT(SHARED) &
!!$OMP PRIVATE(ip,ia, ib, k, psum, gx, gy, gz, Q, phi_ia, phi_ib, g_ia_x, g_ia_y, g_ia_z, g_ib_x, g_ib_y, g_ib_z)
do ip=1,Np
do k=1,nbasis
gx(k)=0._dp; gy(k)=0._dp; gz(k)=0._dp
call gradient_f_bas(k, x(ip), y(ip), z(ip), gx(k), gy(k), gz(k))
enddo
psum=0._dp
do ia=2,maxorb
phi_ia=0._dp; g_ia_x=0._dp; g_ia_y=0._dp; g_ia_z=0._dp
do k=1,nbasis
phi_ia=phi_ia+vecnat(k,ia)*Bs(ip,k)
g_ia_x=g_ia_x+vecnat(k,ia)*gx(k)
g_ia_y=g_ia_y+vecnat(k,ia)*gy(k)
g_ia_z=g_ia_z+vecnat(k,ia)*gz(k)
enddo
do ib=1,ia-1
phi_ib=0._dp; g_ib_x=0._dp; g_ib_y=0._dp; g_ib_z=0._dp
do k=1,nbasis
phi_ib=phi_ib+vecnat(k,ib)*Bs(ip,k)
g_ib_x=g_ib_x+vecnat(k,ib)*gx(k)
g_ib_y=g_ib_y+vecnat(k,ib)*gy(k)
g_ib_z=g_ib_z+vecnat(k,ib)*gz(k)
enddo
Q=(phi_ia*g_ib_x-phi_ib*g_ia_x)**2 &
+(phi_ia*g_ib_y-phi_ib*g_ia_y)**2 &
+(phi_ia*g_ib_z-phi_ib*g_ia_z)**2
psum=psum+0.25_dp*fac_h(ia)*fac_h(ib)*Q
enddo
enddo
tau(ip)=0.5_dp*psum/rho(ip)
enddo
!!$OMP END DO
!!$OMP END PARALLEL
endif ! use_laplacian
if ( allocated(H_lang) ) deallocate ( H_lang, delta_mat, en_lang, vec_lang )
end subroutine average_local_IE_tau
subroutine Calc_H0(DD,H_0)
! Calculates the local Hamiltonian Kinetic/External/Hartree
!..Global
use global; use integrals; use matrices
use orbocc, ONLY: fac_c, fac_h
use functional_m, ONLY: maxorb
implicit none
!..Arguments
real(dp), intent(in) :: DD(lnbasis,lnbasis,lnnatorb)
real(dp), intent(out) :: H_0(lnbasis,lnbasis)
!..Local variables
integer :: m, n, l, s
integer(8) :: ind_p
integer :: ia, ib, nrec, nchunk, nrec_last, ichunk, irec
logical :: inv_pairs, read_int
real(dp) :: two_int, wmn, wls
nrec=50000000 ! x20 = 1GB
nchunk=nintgr/nrec
nchunk=nchunk+1
nrec_last=mod(nintgr,nrec)
read_int=.true.
if(nchunk==1) then
nrec=nrec_last
read_int=.false.
endif
if(.not.allocated(twoin)) then
allocate( indpacked(nrec), mu(nrec), nu(nrec), lambda(nrec), sigma(nrec), twoin(nrec) )
read_int=.true.
endif
Coul=0._dp
rewind(42)
!..Do loop over the nonzero and unique orbitals
do ichunk=1,nchunk
if(ichunk==nchunk) nrec=nrec_last
if ( read_int ) then
read(42,err=140) (indpacked(irec), twoin(irec), irec=1,nrec)
endif
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(irec,ind_p,m,n,l,s)
!$OMP DO
do irec = 1, nrec
ind_p=indpacked(irec)
call unpack4(m,n,l,s,ind_p)
mu(irec)=m
nu(irec)=n
lambda(irec)=l
sigma(irec)=s
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ib,irec,ind_p,m,n,l,s,two_int,wmn,wls,inv_pairs)
!$OMP DO
do ib=1,maxorb
do irec = 1, nrec
m=mu(irec)
n=nu(irec)
l=lambda(irec)
s=sigma(irec)
two_int = twoin(irec)
!.....Create the weights due to symmetry of (mn|ls)
if(m==n) then
wmn = 1._dp
else
wmn = 2._dp
endif
if(l==s) then
wls = 1._dp
else
wls = 2._dp
endif
!.....The coulomb and exchange should be added to different F elements:
!.....Coulomb
inv_pairs = (l/=m.or.n/=s)
Coul(m,n,ib) = Coul(m,n,ib) + DD(l,s,ib) * wls * two_int
if(inv_pairs) Coul(l,s,ib) = Coul(l,s,ib) + DD(m,n,ib) * wmn * two_int
enddo ! irec
enddo !ib
!$OMP END DO
!$OMP END PARALLEL
enddo ! ichunk
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ia,m,n)
!$OMP DO
do ia=1,maxorb
do m=1,nbasis
do n=1,m
Coul(n,m,ia)=Coul(m,n,ia)
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
do m=1,nbasis
do n=1,m
H_0(m,n)=Hcore(m,n)
do ib=1,maxorb
H_0(m,n)= H_0(m,n) + 2._dp*Coul(m,n,ib)
enddo
H_0(n,m)=H_0(m,n)
enddo
enddo
return
140 stop 'Staroverov:sum_intg_sta: error reading 2-e intergral file'
end subroutine Calc_H0
!..................................................................................
subroutine read_vec_e()
!..Global
use global; use files; use orbocc; implicit none
!..Local
integer :: limit=10000000, linear_ind, nblock, i, j, ia, ib, iamin, iamax
logical :: linear_depend
character(20) :: line
character(45) :: dumm
character(30) :: dumm4
print*,'Reading orbilats from ',gam_out_pot
open(unit=11, file=gam_out_pot, status='old')
!..Check for Linearly dependent orbitals (less orbitals printed)
rewind(11)
linear_depend=.false.
do i=1, limit
read(11,'(a20)',end=135) line
if(line == ' NUMBER OF LINEARLY ') then
linear_depend=.true.
exit
endif
enddo
135 continue
rewind(11)
if (linear_depend) then
do i=1,limit
read(11,'(a20)') line
if(line == ' NUMBER OF LINEARLY ') exit
enddo
read(11,'(a45,i5)')dumm, linear_ind
end if ! end if linar_depend=true
rewind(11)
!..linear_ind is the number of linearly independent orbitals to be read
if (.not.linear_depend) linear_ind = nbasis
do i=1,limit
read(11,'(a22)',end=55) dumm4
if(dumm4 == ' EIGENVECTORS') then
read(11,*)
goto 56
endif
enddo
55 stop 'Model_OEP:read_vec_e: string: EIGENVECTORS not found in log file'
56 continue
nblock = linear_ind/5
do ib=0,nblock
read(11,*)
read(11,*)
read(11,*)
read(11,'(a22)')dumm4
do j=1,nbasis
iamin= ib*5 +1
iamax= min(iamin+4,linear_ind)
read(11,'(15x,5e21.12)') (vecnat(j,i),i=iamin,iamax)
enddo
enddo
rewind(11)
do i=1,limit
read(11,'(a22)') dumm4
if(dumm4 == ' EIGENVECTORS') goto 156
enddo
stop 'gamess:string: EIGENVECTORS not found in log file'
156 continue
read(11,*)
iamin=1
iamax=min(5,linear_ind)
do ib=0,linear_ind/5
read(11,*)
read(11,*)
read(11,*) (ennat(ia),ia=iamin,iamax)
read(11,*)
do j=1,nbasis
read(11,*)
enddo
iamin=iamin+5
iamax=iamax+5
iamax=min(iamax,linear_ind)
enddo
close(11)
end subroutine read_vec_e
!--------------------------------------------------------------------------------------------------
! The improved DIIS extrapolation method: P. Pulay, J. Comput. Chem. 3, 556 (1982).
! icall (in/out) : how many times DIIS_extrapolation has been called before/ number of present call
! Ndiis (in) : number of previous runs to be used / dimension of B matrix
! H (in): the hamiltonian to be extrapolated
! H_extrap (out): the extrapolated Hamiltonian
! emax : the maximum error of the e vector
subroutine DIIS_extrapolation(icall, Ndiis, H, H_extrap, emax)
!..Global
use global; use orbocc; use matrices, only: ovlap; use functional_m, only: maxorb; implicit none
!..Arguments
integer, intent(in) :: Ndiis
integer :: icall
real(dp), intent(in) :: H(lnbasis, lnbasis)
real(dp), intent(out) :: H_extrap(lnbasis, lnbasis), emax
!..Local
real(dp) :: Dm(lnbasis,lnbasis), abe
real(dp) :: Ds(lnbasis,lnbasis), FDS(lnbasis,lnbasis), e_t(lnbasis, lnbasis)
real(dp), allocatable :: B(:,:), C(:)
real(dp), allocatable, save :: e(:,:,:), Fs(:,:,:), sqov(:,:), B_o(:,:)
integer :: i, k, l, m, n, Np, ip
if (.not.allocated(e) ) allocate( e(lnbasis, lnbasis, Ndiis) )
if (.not.allocated(Fs) ) allocate( Fs(lnbasis, lnbasis, Ndiis) )
if (.not.allocated(sqov) ) allocate( sqov(lnbasis, lnbasis) )
if (.not.allocated(B_o) ) allocate( B_o(Ndiis, Ndiis) )
icall=icall+1
if (icall==1) then
call sqrt_mat(nbasis,ovlap,sqov)
endif
!..The density matrix
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(l, k, i)
!$OMP DO
do k=1,nbasis
do l=1,k
Dm(k,l)=0._dp
do i=1,maxorb
Dm(k,l) = Dm(k,l) + vecnat(k,i)*vecnat(l,i)
enddo
Dm(l,k)=Dm(k,l)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!..DS
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(l, k, m)
!$OMP DO
do k=1,nbasis
do l=1,nbasis
Ds(k,l)=0._dp
do m=1,nbasis
Ds(k,l)=Ds(k,l)+Dm(k,m)*ovlap(m,l)
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!..FDS
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(l, k, m)
!$OMP DO
do k=1,nbasis
do l=1,nbasis
FDS(k,l)=0._dp
do m=1,nbasis
FDS(k,l)=FDS(k,l)+H(k,m)*Ds(m,l)
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!..For testing
! do i=1,nbasis
! do j=1, nbasis
! Ds(i,j) = 0.d0
! do ia=1,maxorb
! do k=1,nbasis
! do l=1, nbasis
! Ds(i,j)=Ds(i,j) + ovlap(i,k)*vecnat(k,ia)*vecnat(l,ia)*ovlap(l,j)*ennat(ia)
! enddo
! enddo
! enddo
! print*,i,j,Ds(i,j),FDS(i,j)
! enddo
! enddo
! stop 'a'
Np=Ndiis
if ( icall < Ndiis) Np=icall
allocate( B(Np+1, Np+1), C(Np+1) )
!..Save present hamiltonian
if( icall > Ndiis) then
do ip=1,Np-1
Fs(:,:,ip)=Fs(:,:,ip+1)
e(:,:,ip)=e(:,:,ip+1)
enddo
! Fs = CSHIFT(Fs, 1, 3)
! e = CSHIFT(e, 1, 3)
endif
Fs(:,:,Np) = H
!..FDS-SDF
do k=1,nbasis
do l=1,nbasis
e_t(k,l) = FDS(k,l)-FDS(l,k)
enddo
enddo