-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEffective_orbital.f90
2317 lines (1972 loc) · 57.4 KB
/
Effective_orbital.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
!Subroutine for performing the energy minimisation of the EFO
!Objective functional by Nikitas and Toms method
subroutine eff_orbital_NIK_TOM(iscf,x0,y0,z0,x1,y1,z1,pfile)
!..Global
use global; use orbocc; use matrices; use functional_m, only: xmix_OEP, ahyb, VHF, l_hybc, Functional
use energies; use DFT_par, only: Vc_mn_DFT ; implicit none
!..Arguments
real(dp) :: x0,y0,z0,x1,y1,z1
character(10) :: pfile
integer :: iscf
!..Local
integer :: i, a, m, l, info, nvar, meth_case, ist
real(dp) :: ss, xnorm, objf, objf_old=1.D5
real(dp) :: obj1, obj2, obj3, obj4, mid, eps
logical :: l_new_obj
real(dp), allocatable :: X(:), K(:,:), B(:), C(:), U(:,:), XF(:), BU(:), DF(:)
!..Vectors for Conjugate gradient
real(dp), allocatable :: GG(:), HH(:)
integer :: icall=0, Ndiis=6, Nd
real(dp),allocatable :: f_out(:), f_old(:,:), df_old(:,:), ov(:,:), XF_o(:)
real(dp) :: emax
external En_FUN_vec, make_mats_TOM
nvar=nbasis
Allocate( X(nvar), K(nvar,nvar), B(nvar), C(nvar), U(nvar,nvar), XF(nvar), BU(nvar), DF(nvar) )
Allocate( GG(nvar), HH(nvar) )
allocate ( f_out(nvar), f_old(nvar,0:Ndiis), df_old(nvar,Ndiis), XF_o(Nvar), ov(nvar, nvar) )
!..Initialize effective orbital (first call)
if (.not.l_init_efo) then
print*,'Effective orbital initialization'
if( .not. allocated(vec_eff) ) allocate (vec_eff(lnbasis))
call expand_sqdensity(vec_eff)
call norm_vec_eff(vec_eff, xnorm)
l_init_efo=.true.
endif
!..Initial value
X=vec_eff
ov=0._dp
do l=1,nvar
ov(l,l)=1._dp
enddo
!..Loop goes here
do i=1,2000
!..Initialise K, B
if ( Functional == 'DFT' .or. Functional == 'RHF' ) then
call make_mats_TOM(K,B)
else
call make_mats_LRDMFT(K,B)
endif
call diagon_lapack(nvar,K,ovlap,C,U)
!..Loop to transform X to new basis
XF=0._dp
do a=1,nvar
do m=1,nvar
do l=1,nvar
XF(a) = XF(a) + X(m)*ovlap(m,l)*U(l,a)
enddo
enddo
enddo
!..Transform B to new basis
BU=0._dp
do a=1,nvar
do m=1,nvar
BU(a) = BU(a) + B(m) * U(m,a)
enddo
enddo
!..Initial objective function calculation
objf=0._dp
do a=1,nvar
objf = objf + XF(a) * BU(a) - 0.5_dp * C(a) * XF(a)**2
enddo
meth_case=1 ! direct solving for f
! meth_case=2 ! iterative solving with steepest decent (Nikitas)
! meth_case=3 ! iterative solving with DIIS
select case (meth_case)
case(1)
!..Interval bisection initialisation
m=0
obj1=abs(C(nvar))
obj2=-1._dp
obj3=1._dp
obj4=0._dp
ss=0._dp
do a=1,nvar
ss = ss + (BU(a)/min( C(a) + obj2,small ) )**2
enddo
obj4=ss- veffnorm
if ( (obj3.lt.0._dp .and. obj4.lt.0._dp ) .or. ( obj3.gt.0._dp .and. obj4.gt.0._dp )) then
print*, 'Lambda incorrectly bracketed'
stop
endif
if ( obj3 .lt. 0._dp ) then
mid=obj1
ss=obj3
obj1=obj2
obj3=obj4
obj2=mid
obj4=ss
endif
!..Interval bisection loop
do
m=m+1
mid = 0.5 * ( obj1 + obj2 )
ss=0._dp
do a=1,nvar
ss = ss + (BU(a)/( C(a) + mid ))**2
enddo
ss=ss - veffnorm
if ( ss .gt. 0._dp ) then
obj1=mid
obj3 = ss
else
obj2=mid
obj4=ss
endif
if(abs(obj1-obj2).lt.1.D-16) then
ss=0.5*(obj1 + obj2)
exit
endif
enddo
!..End of Interval Bisection
obj4=ss
do a=1,nvar
DF(a) = BU(a) / (ss + C(a))
enddo
!..Mixing
! DF = 0.05_dp * DF + 0.95_dp * XF
! call norm_vec_eff(DF, ss)
XF=DF
! ss=DOT_PRODUCT(DF,DF)
obj4=ss
!XF=veffnorm/ss * DF
objf=0._dp
do a=1,nvar
objf = objf + XF(a) * BU(a) - 0.5_dp * C(a) * XF(a)**2
enddo
!..End of method
!...............................
!..Loop calculating the minimum with B&K held constant
!..Via Conjugate gradient method
case(2)
!..initialise the CG method with the gradient DF
ss=0._dp
do a=1,nvar
ss = ss + XF(a) * BU(a) - C(a) * XF(a)**2
enddo
!..Calculates df
DF = -( BU - C * XF ) + XF * ss/veffnorm
!..Vectors for conjugate gradient
GG=DF
HH=GG
m=0
!..Conjugate gradient loop
do
m=m+1
!..Algerbraic minimum finding in direciton HH
obj2=0._dp
obj3=0._dp
ss=0._dp
do a=1,nvar
obj2 = obj2 + HH(a) * BU(a) - C(a) * HH(a) * XF(a)
obj3 = obj3 + C(a) * HH(a)**2
enddo
ss = obj2/obj3
DF = XF + ss * HH
!..Calculate the objective functional
XF=DF
obj1=objf
objf=0._dp
do a=1,nvar
objf = objf + XF(a) * BU(a) - 0.5_dp * C(a) * XF(a)**2
enddo
!..Escape if convergence is achieved
if ( abs(objf-obj1).lt.1.D-16 ) exit
!..Calculation of the gradient at the new minimum
!..determine lambda
ss=0._dp
do a=1,nvar
ss = ss + XF(a) * BU(a) - C(a) * XF(a)**2
enddo
!..Calculates gradient
DF = -( BU - C * XF ) + XF * ss/veffnorm
!..Conjugate gradient calculation
obj2=0._dp
obj3=0._dp
do a=1,nvar
obj2=obj2 + DF(a)**2!( DF(a) - GG(a) ) * DF(a)
obj3=obj3 + GG(a)**2
enddo
ss = obj2/obj3
GG=DF
HH=GG + ss * HH
enddo
!..End of CG loop
!..Transform f back into original basis
case(3)
do ist=1,123456789
ss=0._dp
do a=1,nvar
ss = ss + XF(a) * BU(a) - C(a) * XF(a)**2
enddo
DF = -( BU - C * XF ) + XF * ss/veffnorm
eps=0._dp; ss=0._dp
do a=1,nvar
eps = eps + DF(a)*BU(a) - C(a) * XF(a) *DF(a)
ss = ss + C(a) * DF(a)*DF(a)
enddo
eps=eps/ss
ss=0._dp
do a=1,nvar
ss = ss+DF(a)*DF(a)
enddo
ss=sqrt(1._dp+eps*eps*ss/veffnorm)
XF=(XF+eps*DF)/ss
! print*,'a'
! print*,XF
if (ist == 100 ) f_old(:,0)=XF_o
if ( ist > 100) then
call DIIS_f(icall, Ndiis, nvar, XF, f_out, f_old, df_old, ov, emax)
XF=f_out
endif
! print*,'b'
! print*,XF
objf=0._dp
do a=1,nvar
objf = objf + XF(a) * BU(a) - 0.5_dp * C(a) * XF(a)**2
enddo
xnorm=0._dp
emax=0._dp
do a=1,nvar
emax=emax+(XF(a)-XF_o(a))**2
xnorm=xnorm+XF(a)**2
enddo
print*, ist, 'emax',sqrt(emax), sqrt(xnorm), objf
! print*,ist, objf
XF_o = XF
enddo
case default
stop 'Effective orbital; unknown case'
end select
!stop
!..End of steepest descent
DF=X
X=0._dp
do l=1,nvar
do a=1,nvar
X(l) = X(l) + XF(a) * U(l,a)
enddo
enddo
!..mixing
X=0.5*X+0.5*DF
!..Now normalize optimal X and vec_eff
call norm_vec_eff(X, ss)
vec_eff = X
!..Print info for each step
print*, i, m, objf , objf - objf_old, C(nvar)
! print*, i, m, C(nvar) , obj4, objf - objf_old
if ( abs(objf - objf_old) < 1.D-14 ) then
print*, 'convergence achieved, Objective function: ', objf
exit
else if ( i == 5000 ) then
print*, 'convergence not achieved after 5000 cycles!'
stop
else
objf_old=objf
endif
enddo
print*,' Objective function: ',objf
print*,'---------------------------------------------------------'
!..prints eigenvalues of K
if (.true.) then
print*, '----------K EIGENVALUES-----------'
print*, C
print*, '----------------------------------'
endif
if( .not. allocated(vec_eff0) ) allocate(vec_eff0(nbasis))
if (iscf >1 ) vec_eff = xmix_OEP * vec_eff + (1.d0-xmix_OEP) * vec_eff0
! if (iscf >1 ) vec_eff = 0.5_dp * (vec_eff + vec_eff0)
! if (iscf >1 ) vec_eff =0.6_dp * vec_eff0 + 0.4_dp * vec_eff
call norm_vec_eff(vec_eff, ss)
vec_eff0 = vec_eff
call build_Veff()
if ( Functional=='DFT' ) then
if ( .not. l_hybc ) then
H_eff = Hcore + Veff + Vc_mn_DFT
else
! H_eff = Hcore + ahyb*Veff + (1._dp - ahyb)*VHF !..original hybrid
H_eff = Hcore + ahyb*Veff + (1._dp - ahyb)*VHF + Vc_mn_DFT !..Hybrid + correlation
endif
else
H_eff = Hcore + Veff
endif
call diagon_H(info)
call construct_f(0)
! call plot_potential_EFO(x0,y0,z0,x1,y1,z1,'potentialf_EFO')
! call grad_E_vec_eff_R(x0,y0,z0,x1,y1,z1,'func_ders')
return
Deallocate( X, K, B, XF, U, C, BU )
end subroutine eff_orbital_NIK_TOM
!..Calculates the Matrix A, and the vector B
subroutine make_mats_TOM(A,B)
use global; use orbocc; use matrices
use DFT_par, only: Vc_mn_DFT
use functional_m, only: Functional
use energies; implicit none
!..Arguments
real(dp) :: A(lnbasis, lnbasis), B(lnbasis)
integer :: m,ia,k,l,ii, n_occ
real(dp) :: ss
real(dp), allocatable :: QQ(:,:,:), ST(:,:,:), FF(:,:), VV(:,:)
allocate ( QQ(nbasis,nbasis,nbasis), ST(nbasis,nbasis,nbasis), &
FF(nbasis,nbasis), VV(nbasis,nbasis) )
n_occ=max(ibond(1),ibond(2))
call make_S_klm()
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(m,ia,k,ss,l)
!$OMP DO
do m=1,nbasis
do ia=n_occ+1,nbasis
do k=1,nbasis
ss=0._dp
do l=1,nbasis
ss = ss + S3(k,l,m)*vecnat(l,ia)
enddo
QQ(k,ia,m) = ss
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(m,ia,ii,k,ss)
!$OMP DO
do m=1,nbasis
do ia=n_occ+1,nbasis
do ii=1,n_occ
ss = 0._dp
do k=1,nbasis
ss = ss + QQ(k,ia,m)*vecnat(k,ii)
enddo
ST(ii,ia,m) = ss
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ia,ii,k,l)
!$OMP DO
do k=1,nbasis
do l=1,nbasis
A(k,l) = 0.d0
do ia=n_occ+1,nbasis
do ii=1, n_occ
A(k,l) = A(k,l) + (ST(ii,ia,k) * ST(ii,ia,l))/max(ennat(ia)-ennat(ii), small)
enddo
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
A=-2._dp * A
!..V_HX
if ( Functional=='RHF' ) then
VV(:,:)=F(:,:,1)-Hcore(:,:)
else
VV(:,:)=F(:,:,1)-Hcore(:,:)-Vc_mn_DFT(:,:)
endif
!..V_HXC
! VV(:,:)=F(:,:,1)-Hcore(:,:)
FF=0._dp
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ia,ii,k,l)
!$OMP DO
do ia=n_occ+1,nbasis
do ii=1, n_occ
FF(ii,ia) = 0.d0
do k=1,nbasis
do l=1,nbasis
FF(ii,ia) = FF(ii,ia) + vecnat(k,ia)*VV(k,l)*vecnat(l,ii)
enddo
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
! write(6,'(15f10.6)')((FF(ia,ii),ia=1,nbasis),ii=1,nbasis)
do m=1,nbasis
ss=0._dp
do ii=1,n_occ
do ia=n_occ+1,nbasis
ss = ss + FF(ii,ia)*ST(ii,ia,m)/max(ennat(ia)-ennat(ii), small)
enddo
enddo
B(m)=-2._dp*ss
enddo
deallocate ( QQ, ST, FF )
return
end subroutine make_mats_TOM
!========================================
!........>>END OF TOMS CHANGES<<.........
!========================================
!..Calculates the Matrix A, and the vector B
subroutine make_mats_LRDMFT(A,B)
use global; use orbocc; use matrices
use DFT_par, only: Vc_mn_DFT
use functional_m, only: Functional, Dn_lim, small_e
use energies; implicit none
!..Arguments
real(dp) :: A(lnbasis, lnbasis), B(lnbasis)
integer :: m,ia,k,l,ii, n_occ
real(dp) :: ss, Delta_e, Delta_n, fac_dif
real(dp), allocatable :: QQ(:,:,:), ST(:,:,:), FF(:,:), VV(:,:), FFp(:,:)
allocate ( QQ(nbasis,nbasis,nbasis), ST(nbasis,nbasis,nbasis), &
FF(nbasis,nbasis), VV(nbasis,nbasis), FFp(nbasis,nbasis))
n_occ=max(ibond(1),ibond(2))
call make_S_klm()
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(m,ia,k,ss,l)
!$OMP DO
do m=1,nbasis
do ia=1,nbasis
do k=1,nbasis
ss=0._dp
do l=1,nbasis
ss = ss + S3(k,l,m)*vecnat(l,ia)
enddo
QQ(k,ia,m) = ss
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(m,ia,ii,k,ss)
!$OMP DO
do m=1,nbasis
do ia=1,nbasis
do ii=1,nbasis; if(ia /= ii ) then
ss = 0._dp
do k=1,nbasis
ss = ss + QQ(k,ia,m)*vecnat(k,ii)
enddo
ST(ii,ia,m) = ss
endif; enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ia,ii,k,l)
!$OMP DO
do k=1,nbasis
do l=1,nbasis
A(k,l) = 0.d0
do ia=1,nbasis
do ii=1,nbasis; if(ia /= ii ) then
Delta_n=occnum(ii,3) - occnum(ia,3)
if ( abs(Delta_n) > 2.d0*Dn_lim ) then
Delta_e=ennat(ia) - ennat(ii)
if(abs(Delta_e) < small_e) then
if ( Delta_e >= 0 ) then
Delta_e=small_e
else
Delta_e=-small_e
endif
endif
A(k,l) = A(k,l) - (ST(ii,ia,k)*ST(ii,ia,l))*Delta_n/Delta_e
endif
endif; enddo
enddo
A(k,l) = 0.5_dp * A(k,l)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ii,fac_dif,k,l,VV,ia)
!$OMP DO
do ii=1, nbasis
fac_dif=0.5d0*fac_h(ii)
do k=1,nbasis
do l=1,k
VV(k,l)=F(k,l,ii)-fac_dif*hcore(k,l)
VV(l,k)=VV(k,l)
enddo
enddo
do ia=1, nbasis
FFp(ii,ia) = 0.d0
do k=1,nbasis
do l=1,nbasis
FFp(ii,ia) = FFp(ii,ia) &
+ vecnat(k,ia)*VV(k,l)*vecnat(l,ii)
enddo
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ii,ia)
!$OMP DO
do ii=1,nbasis
do ia=1,nbasis
FF(ia,ii) = FFp(ia,ii)-FFp(ii,ia)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
do m=1,nbasis
ss=0._dp
do ii=1,nbasis
do ia=1,nbasis; if ( ia /= ii ) then
Delta_n=occnum(ii,3) - occnum(ia,3)
if ( abs(Delta_n) > 2.d0*Dn_lim ) then
Delta_e=ennat(ia)-ennat(ii)
if(abs(Delta_e) < small_e) then
if ( Delta_e >= 0 ) then
Delta_e=small_e
else
Delta_e=-small_e
endif
endif
ss = ss + FF(ii,ia)*ST(ii,ia,m)/Delta_e
endif
endif; enddo
enddo
B(m)=-ss
enddo
deallocate ( QQ, ST, FF, VV, FFp )
return
end subroutine make_mats_LRDMFT
! For RDMFT matrices A and B shoule be defined differently:
subroutine make_mats_RDMFT(A,B)
use global; use orbocc; use matrices
use DFT_par, only: Vc_mn_DFT
use functional_m, only: Functional, Dn_lim, small_e
use energies; implicit none
!..Arguments
real(dp) :: A(lnbasis, lnbasis), B(lnbasis)
integer :: m,ia,k,l,ii, n_occ
real(dp) :: ss, fac_dif, Delta_n, Delta_e
real(dp), allocatable :: QQ(:,:,:), ST(:,:,:), FF(:,:), VV(:,:), FFp(:,:), Fii(:,:)
allocate ( QQ(nbasis,nbasis,nbasis), ST(nbasis,nbasis,nbasis), &
FF(nbasis,nbasis), VV(nbasis,nbasis), FFp(nbasis,nbasis),Fii(nbasis,nbasis) )
n_occ=max(ibond(1),ibond(2))
call make_S_klm()
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(m,ia,k,ss,l)
!$OMP DO
do m=1,nbasis
do ia=1,nbasis
do k=1,nbasis
ss=0._dp
do l=1,nbasis
ss = ss + S3(k,m,l)*vecnat(l,ia)
enddo
QQ(k,m,ia) = ss
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(m,ia,ii,k,ss)
!$OMP DO
do m=1,nbasis
do ia=1,nbasis
do ii=1,nbasis
ss = 0._dp
do k=1,nbasis
ss = ss + QQ(m,k,ia)*vecnat(k,ii)
enddo
ST(ii,ia,m) = ss
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ia,ii,k,l)
!$OMP DO
do k=1,nbasis
do l=1,nbasis
A(k,l) = 0.d0
do ia=1, nbasis
do ii=1, nbasis; if ( ia /= ii ) then
Delta_n=occnum(ii,3) - occnum(ia,3)
if ( abs(Delta_n) > 2.d0*Dn_lim ) then
Delta_e=ennat(ia) - ennat(ii)
if(abs(Delta_e) < small_e) then
if ( Delta_e >= 0 ) then
Delta_e=small_e
else
Delta_e=-small_e
endif
endif
A(k,l) = A(k,l) - (ST(ii,ia,l)*ST(ii,ia,k))*Delta_n/Delta_e
endif
endif; enddo
enddo
A(k,l) = 0.5_dp * A(k,l)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ii,fac_dif,k,l,Fii,ia)
!$OMP DO
do ii=1, nbasis
fac_dif=0.5d0*fac_h(ii)
do k=1,nbasis
do l=1,k
Fii(k,l)=F(k,l,ii)-fac_dif*hcore(k,l)
Fii(l,k)=Fii(k,l)
enddo
enddo
do ia=1, nbasis
FFp(ia,ii)=0.d0
do k=1,nbasis
do l=1,nbasis
FFp(ia,ii) = FFp(ia,ii) &
+ vecnat(k,ia)*Fii(k,l)*vecnat(l,ii)
enddo
enddo
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP PRIVATE(ii,ia)
!$OMP DO
do ii=1,nbasis
do ia=1,nbasis
FF(ia,ii) = FFp(ii,ia)-FFp(ia,ii)
enddo
enddo
!$OMP END DO
!$OMP END PARALLEL
do m=1,nbasis
ss=0._dp
do ii=1,nbasis
do ia=1,nbasis; if (ii /= ia) then
Delta_n=occnum(ii,3) - occnum(ia,3)
if ( abs(Delta_n) > 2.d0*Dn_lim ) then
Delta_e=ennat(ia)-ennat(ii)
if(abs(Delta_e) < small_e) then
if ( Delta_e >= 0 ) then
Delta_e=small_e
else
Delta_e=-small_e
endif
endif
ss = ss + FF(ii,ia)*ST(ii,ia,m)/Delta_e
endif
endif; enddo
enddo
B(m)=-ss
enddo
deallocate ( QQ, ST, FF, Fii, FFp )
return
end subroutine make_mats_RDMFT
!========================================
!........>>END OF TOMS CHANGES<<.........
!========================================
! Find the effective orbital using NAG minimization routines.
! Screening charge constraint already incorporated so minimization
! is unconstrained
subroutine eff_orbital_NAG(iscf,x0,y0,z0,x1,y1,z1,pfile)
!..Global
use global; use orbocc; use matrices; use functional_m, only: xmix_OEP
use energies; implicit none
!..Arguments
real(dp) :: x0,y0,z0,x1,y1,z1
character(10) :: pfile
integer :: iscf
!..Parameters for the NAG routine:
integer :: nclin, ncnln
integer :: lda, liwork, lwork, ldr, iuser(1), ldcj
integer :: ifail=0, nvar, nstate, iter=0
real, parameter :: BIGBND=1.e20_dp
integer, allocatable :: istate(:), iwork(:)
real(dp), allocatable :: A(:, :), bl(:), bu(:)
real(dp), allocatable :: X(:), work(:), grd(:)
real(dp), allocatable :: clamda(:)
real(dp), allocatable :: objgrd(:), r(:, :)
real(dp), allocatable :: c(:), cjac(:,:), Xs(:)
real(dp), allocatable :: Bmat(:,:),Emat(:),Psimat(:,:),Dveceff(:)
real(dp) :: user(1), objf
!..Local
integer :: k, info
real(dp) :: ss, xnorm
logical :: l_new_obj
!..TOMS THING
character(len=30) :: Func_pres , format_string
integer :: pres
external En_FUN_vec, e04udm, e04uef, e04ucf, OBJ_LRDMFT, CONFUN1, OBJ_NIK
nvar=nbasis
nclin=0
ncnln=0
!----TOMS CHANGE
! ncnln=1
! ifail=1
lda=max(1,nclin)
ldr=nvar
ldcj=max(1,ncnln)
nstate=1
liwork=3*nvar+nclin+2*ncnln
lwork=2*nvar*nvar+nvar*nclin+2*nvar*ncnln+20*nvar+11*nclin+21*ncnln+1
allocate ( A(lda,1), bl(nvar+nclin+ncnln), bu(nvar+nclin+ncnln), &
X(nvar), istate(nvar+nclin+ncnln), c(ldcj), cjac(ldcj,nvar),&
clamda(nvar+nclin+ncnln), objgrd(nvar), r(ldr,nvar), iwork(liwork),&
work(lwork), grd(nvar), Xs(Nvar) )
allocate ( Bmat(nvar,nvar), Emat(nvar), Psimat(nvar,nvar), Dveceff(nvar) )
!..Unconstrained minimization:
do k=1,nvar
bl(k)=-BIGBND; bu(k)=BIGBND
enddo
!....TOMS CHANGE
! bl(nvar+1)=veffnorm ; bu(nvar+1)=veffnorm
pres=6
if ( pres .gt. 9 ) then
format_string= "(A24,I2)"
else
format_string = "(A24,I1)"
endif
write (Func_pres,format_string) 'Function Precision = 1d-',pres
print*, 'LOOK HERE'
print*, Func_pres
! call e04uef("Difference interval = 1d-6")
call e04uef("Major Iteration Limit = 2000")
call e04uef("Minor Iteration Limit = 1000")
! call e04uef("Central difference interval = 1d-4")
!..accuracy of solution
call e04uef("Function Precision = 1d-12")
! call e04uef(Func_pres)
call e04uef("Optimality Tolerance = 1d-10")
!..How accurately a variable should obey a constraint:
! call e04uef("Feasibility Tolerance = 1d-10")
! call e04uef("Nonlinear feasibility = 1d-12")
!..print level
call e04uef("Major print level = 5")
call e04uef("Minor print level = 0")
!..Initialize effective orbital (first call)
if (.not.l_init_efo) then
print*,'Effective orbital initialization'
if( .not. allocated(vec_eff) ) allocate (vec_eff(lnbasis))
call expand_sqdensity(vec_eff)
call norm_vec_eff(vec_eff, xnorm)
l_init_efo=.true.
call build_Veff()
H_eff = Hcore + Veff
call diagon_H(info)
call construct_f(0)
call total_energy_MO(0)
Print*,'------------- ENERGIES with INITIAL Vec_eff ---------'
write(6,113) Bare_energy, Coul_energy, Exch_energy, &
Totel_energy, Rep_energy, TS_ener, Tot_energy
Print*,'-------------END INITIAL ENERGIES --------------------'
endif
!..Initial value
X=vec_eff
l_new_obj=.true.
print*,X
print*,'-----------------------------'
if ( .not.l_new_obj ) then !Energy is the objective function
call e04ucf(nvar, nclin, ncnln, lda, ldcj, ldr, A, &
bl, bu, e04udm, En_FUN_vec, iter, istate, c, &
! bl, bu, CONFUN1, En_FUN_vec, iter, istate, c, &
cjac, clamda, objf, objgrd, r, X, iwork, liwork, &
work, lwork, iuser, user, ifail)
print*,'objgrd', (objgrd(k),k=1,Nvar)
print*,'----------------------------------------'
else !Nikitas objective function frozen orbitals
call e04ucf(nvar, nclin, ncnln, lda, ldcj, ldr, A, &
bl, bu, e04udm, OBJ_LRDMFT, iter, istate, c, &
! bl, bu, e04udm, OBJ_NIK, iter, istate, c, &
! bl, bu, CONFUN1, OBJ_LRDMFT, iter, istate, c, &
cjac, clamda, objf, objgrd, r, X, iwork, liwork, &
work, lwork, iuser, user, ifail)
endif
!..Now normalize optimal X
call norm_vec_eff(X, ss)
!..Final optimal vec_eff
vec_eff=X
print*,X
print*,'-----------------------------'
if( .not. allocated(vec_eff0) ) allocate(vec_eff0(nbasis))
if (iscf >1 ) vec_eff = xmix_OEP * vec_eff + (1.d0-xmix_OEP) * vec_eff0
call norm_vec_eff(vec_eff, ss)
vec_eff0 = vec_eff
print*,' Objective function: ',objf
print*,'---------------------------------------------------------'
call build_Veff()
H_eff = Hcore + Veff
call diagon_H(info)
call construct_f(0)
call total_energy_MO(0)
Print*,'------------- ENERGIES after veff step ---------'
write(6,113) Bare_energy, Coul_energy, Exch_energy, &
Totel_energy, Rep_energy, TS_ener, Tot_energy
113 format(' One-electron Energy: ',f18.10,/, &
' Hartree Energy: ',f18.10,/, &
' Exch./Corr. Energy: ',f18.10,/, &
' Total Electronic Energy: ',f18.10,/, &
' Repulsive nuclei Energy: ',f18.10,/, &
' Entropy term (TS): ',f18.10,/, &
' *****TOTAL ENERGY*****: ',f18.10,/)
call plot_potential_EFO(x0,y0,z0,x1,y1,z1,'potentialf_EFO')
! call grad_E_vec_eff_R(x0,y0,z0,x1,y1,z1,'func_ders')
return
end subroutine eff_orbital_NAG
!------TOMS NONLINEAR CONSTRAINT FOR THE SCREENING DENSITY
!------------------------------------------------------------------------------
subroutine CONFUN1(MODE,NCNLN,N,LDCJ,NEEDC,X,C,CJAC,NSTATE,IUSER,USER)
use global; use matrices;
integer :: LDCJ, MODE, N, NCNLN, NSTATE, NEEDC(*), IUSER(*)
real(dp) :: X(N), C(NCNLN), CJAC(LDCJ,N), USER(*)
integer i,j
if (NSTATE.eq.1) then
do i=1,NCNLN
do j=1,N
CJAC(i,j) = 0.d0
enddo
enddo
endif
if (NEEDC(1) .gt. 0 ) then
if (MODE.eq.0 .or. MODE.eq.2) then
C(1)=0.d0
do i=1,N
do j=1,N
C(1)=C(1) + X(i) * X(j) * ovlap(i,j)
enddo
enddo
endif
if (MODE.eq.1 .or. MODE.eq.2 ) then
CJAC=0.d0
do i=1,N
do j=1,N
CJAC(1,i)=CJAC(1,i) + X(j) * ovlap(i,j)
enddo
enddo
endif
endif
return
end subroutine CONFUN1
!------------------------------------------------------------------------------
subroutine expand_sqdensity(vec_eff)
!..Global
use global; use matrices, only:ovlap; use orbocc; use grid_params; use functional_m, only:maxorb; implicit none
!..Arguments
real(dp),intent(out) :: vec_eff(lnbasis)
!..Local
real(dp) :: delta(lnbasis,lnbasis), oinv(lnbasis,lnbasis), eigs(lnbasis),vectr(lnbasis,lnbasis)
real(dp), allocatable :: sqrho(:), b(:)
real(dp) :: fc, sqrho_e, Obj, x_m, y_m, z_m, sqdif, sqdif_m
real(dp),external :: electron_density, f_bas