-
Notifications
You must be signed in to change notification settings - Fork 1
/
VCA_AUX_FUNX.f90
917 lines (833 loc) · 31.8 KB
/
VCA_AUX_FUNX.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
MODULE VCA_AUX_FUNX
USE VCA_INPUT_VARS
USE VCA_VARS_GLOBAL
USE SF_TIMER
USE SF_LINALG
USE SF_MISC, only: assert_shape
USE SF_IOTOOLS, only:free_unit,reg
implicit none
private
interface vca_lso2nnn_reshape
module procedure :: d_nlso2nnn_scalar
module procedure :: c_nlso2nnn_scalar
end interface vca_lso2nnn_reshape
interface vca_nnn2lso_reshape
module procedure :: d_nnn2nlso_scalar
module procedure :: c_nnn2nlso_scalar
end interface vca_nnn2lso_reshape
interface vca_rectangular_n2j_reshape
module procedure :: d_rectangular_n2j_scalar
module procedure :: c_rectangular_n2j_scalar
end interface vca_rectangular_n2j_reshape
interface vca_rectangular_j2n_reshape
module procedure :: d_rectangular_j2n_scalar
module procedure :: c_rectangular_j2n_scalar
end interface vca_rectangular_j2n_reshape
interface vca_so2nn_reshape
module procedure d_nso2nn
module procedure c_nso2nn
end interface vca_so2nn_reshape
interface vca_nn2so_reshape
module procedure d_nn2nso
module procedure c_nn2nso
end interface vca_nn2so_reshape
interface vca_set_Hcluster
module procedure :: set_Hcluster_nnn
module procedure :: set_Hcluster_lso
end interface vca_set_Hcluster
interface vca_set_Hk
module procedure :: set_Hk_nnn
module procedure :: set_Hk_lso
end interface vca_set_Hk
#if __GNUC__ > 6
interface read(unformatted)
procedure read_unformatted
end interface read(unformatted)
interface write(unformatted)
procedure write_unformatted
end interface write(unformatted)
interface read(formatted)
procedure read_formatted
end interface read(formatted)
interface write(formatted)
procedure write_formatted
end interface write(formatted)
#endif
public :: vca_get_cluster_dimension
public :: vca_rectangular_n2j_reshape
public :: vca_rectangular_j2n_reshape
!
public :: vca_set_Hcluster
public :: vca_set_Hk
!
public :: vca_lso2nnn_reshape
public :: vca_nnn2lso_reshape
public :: vca_so2nn_reshape
public :: vca_nn2so_reshape
!
public :: save_gfprime
public :: read_gfprime
!
public :: vca_search_variable
public :: search_chemical_potential
!
contains
!##################################################################
! DIMENSION PROCEDURES
!##################################################################
function vca_get_cluster_dimension(with_bath) result(Ncluster)
logical,optional :: with_bath
logical :: bool
integer :: Ns
integer :: Ncluster
!
bool = .false. ; if(present(with_bath))bool = with_bath
!
!Count how many levels are there in the cluster:
Ns = Nlat*Norb
if(bool)then
Ns = Nlat*Norb + Nlat_bath*Norb_bath
endif
!
!Count the spin:
Ncluster = Nspin*Ns
!
end function vca_get_cluster_dimension
!+------------------------------------------------------------------+
!PURPOSE : allocate code-internal hopping matrices
!+------------------------------------------------------------------+
subroutine set_Hcluster_nnn(hloc)
complex(8),dimension(:,:,:,:,:,:) :: Hloc ![Nlat][Nlat][Nspin][Nspin][Norb][Norb]
call assert_shape(Hloc,[Nlat,Nlat,Nspin,Nspin,Norb,Norb],"set_Hcluster_nnn","Hloc")
!
impHloc = Hloc
!
write(LOGfile,"(A)")"Set Hcluster: done"
!if(verbose>2)call vca_print_Hcluster(impHloc)
end subroutine set_Hcluster_nnn
subroutine set_Hcluster_lso(hloc)
complex(8),dimension(:,:) :: Hloc ![Nlat*Nspin*Norb][Nlat*Nspin*Norb]
call assert_shape(Hloc,[Nlat*Nspin*Norb,Nlat*Nspin*Norb],"set_Hcluster_lso","Hloc")
!
impHloc = vca_lso2nnn_reshape(Hloc,Nlat,Nspin,Norb)
!
write(LOGfile,"(A)")"Set Hcluster: done"
!if(verbose>2)call vca_print_Hcluster(impHloc)
end subroutine set_Hcluster_lso
subroutine set_Hk_nnn(hloc)
complex(8),dimension(:,:,:,:,:,:,:) :: Hloc ![Nlat][Nlat][Nspin][Nspin][Norb][Norb][Nk]
!
impHk = Hloc
!
write(LOGfile,"(A)")"Set Hk: done"
!if(verbose>2)call vca_print_Hcluster(impHk)
end subroutine set_Hk_nnn
subroutine set_Hk_lso(hloc)
complex(8),dimension(:,:,:) :: Hloc ![Nlat*Nspin*Norb][Nlat*Nspin*Norb][Nk]
integer :: i
!
do i=1,size(Hloc,3)
impHk(:,:,:,:,:,:,i) = vca_lso2nnn_reshape(Hloc(:,:,i),Nlat,Nspin,Norb)
enddo
!
write(LOGfile,"(A)")"Set Hk: done"
!if(verbose>2)call vca_print_Hcluster(impHk)
end subroutine set_Hk_lso
!+-----------------------------------------------------------------------------+!
!PURPOSE:
! reshape a matrix from/to the
! [Nlso][Nlso] and
! [Nlat][Nlat][Norb][Norb][Nspin][Nspin]
! shapes.
! - dble & cmplx
! 0-Reshape a scalar array, dble & cmplx
! 1-Reshape a function array (:,:,:,:,:,:,1:L)
!+-----------------------------------------------------------------------------+!
function d_nlso2nnn_scalar(Hlso,Nlat,Nspin,Norb) result(Hnnn)
integer :: Nlat,Nspin,Norb
real(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb) :: Hlso
real(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb) :: Hnnn
integer :: ilat,jlat
integer :: iorb,jorb
integer :: ispin,jspin
integer :: is,js
Hnnn=zero
do ilat=1,Nlat
do jlat=1,Nlat
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
is = iorb + (ilat-1)*Norb + (ispin-1)*Norb*Nlat
js = jorb + (jlat-1)*Norb + (jspin-1)*Norb*Nlat
Hnnn(ilat,jlat,ispin,jspin,iorb,jorb) = Hlso(is,js)
enddo
enddo
enddo
enddo
enddo
enddo
end function d_nlso2nnn_scalar
!
function d_nnn2nlso_scalar(Hnnn,Nlat,Nspin,Norb) result(Hlso)
integer :: Nlat,Nspin,Norb
real(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb) :: Hnnn
real(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb) :: Hlso
integer :: ilat,jlat
integer :: iorb,jorb
integer :: ispin,jspin
integer :: is,js
Hlso=zero
do ilat=1,Nlat
do jlat=1,Nlat
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
is = iorb + (ilat-1)*Norb + (ispin-1)*Norb*Nlat
js = jorb + (jlat-1)*Norb + (jspin-1)*Norb*Nlat
Hlso(is,js) = Hnnn(ilat,jlat,ispin,jspin,iorb,jorb)
enddo
enddo
enddo
enddo
enddo
enddo
end function d_nnn2nlso_scalar
!
function c_nlso2nnn_scalar(Hlso,Nlat,Nspin,Norb) result(Hnnn)
integer :: Nlat,Nspin,Norb
complex(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb) :: Hlso
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb) :: Hnnn
integer :: ilat,jlat
integer :: iorb,jorb
integer :: ispin,jspin
integer :: is,js
Hnnn=zero
do ilat=1,Nlat
do jlat=1,Nlat
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
is = iorb + (ilat-1)*Norb + (ispin-1)*Norb*Nlat
js = jorb + (jlat-1)*Norb + (jspin-1)*Norb*Nlat
Hnnn(ilat,jlat,ispin,jspin,iorb,jorb) = Hlso(is,js)
enddo
enddo
enddo
enddo
enddo
enddo
end function c_nlso2nnn_scalar
!
function c_nnn2nlso_scalar(Hnnn,Nlat,Nspin,Norb) result(Hlso)
integer :: Nlat,Nspin,Norb
complex(8),dimension(Nlat,Nlat,Nspin,Nspin,Norb,Norb) :: Hnnn
complex(8),dimension(Nlat*Nspin*Norb,Nlat*Nspin*Norb) :: Hlso
integer :: ilat,jlat
integer :: iorb,jorb
integer :: ispin,jspin
integer :: is,js
Hlso=zero
do ilat=1,Nlat
do jlat=1,Nlat
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
is = iorb + (ilat-1)*Norb + (ispin-1)*Norb*Nlat
js = jorb + (jlat-1)*Norb + (jspin-1)*Norb*Nlat
Hlso(is,js) = Hnnn(ilat,jlat,ispin,jspin,iorb,jorb)
enddo
enddo
enddo
enddo
enddo
enddo
end function c_nnn2nlso_scalar
function d_nso2nn(Hso,Nspin,Norb) result(Hnn)
integer :: Nspin,Norb
real(8),dimension(Nspin*Norb,Nspin*Norb) :: Hso
real(8),dimension(Nspin,Nspin,Norb,Norb) :: Hnn
integer :: iorb,ispin,is
integer :: jorb,jspin,js
Hnn=zero
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
is = iorb + (ispin-1)*Norb !spin-orbit stride
js = jorb + (jspin-1)*Norb !spin-orbit stride
Hnn(ispin,jspin,iorb,jorb) = Hso(is,js)
enddo
enddo
enddo
enddo
end function d_nso2nn
function c_nso2nn(Hso,Nspin,Norb) result(Hnn)
integer :: Nspin,Norb
complex(8),dimension(Nspin*Norb,Nspin*Norb) :: Hso
complex(8),dimension(Nspin,Nspin,Norb,Norb) :: Hnn
integer :: iorb,ispin,is
integer :: jorb,jspin,js
Hnn=zero
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
is = iorb + (ispin-1)*Norb !spin-orbit stride
js = jorb + (jspin-1)*Norb !spin-orbit stride
Hnn(ispin,jspin,iorb,jorb) = Hso(is,js)
enddo
enddo
enddo
enddo
end function c_nso2nn
function d_nn2nso(Hnn,Nspin,Norb) result(Hso)
integer :: Nspin,Norb
real(8),dimension(Nspin,Nspin,Norb,Norb) :: Hnn
real(8),dimension(Nspin*Norb,Nspin*Norb) :: Hso
integer :: iorb,ispin,is
integer :: jorb,jspin,js
Hso=zero
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
is = iorb + (ispin-1)*Norb !spin-orbit stride
js = jorb + (jspin-1)*Norb !spin-orbit stride
Hso(is,js) = Hnn(ispin,jspin,iorb,jorb)
enddo
enddo
enddo
enddo
end function d_nn2nso
function c_nn2nso(Hnn,Nspin,Norb) result(Hso)
integer :: Nspin,Norb
complex(8),dimension(Nspin,Nspin,Norb,Norb) :: Hnn
complex(8),dimension(Nspin*Norb,Nspin*Norb) :: Hso
integer :: iorb,ispin,is
integer :: jorb,jspin,js
Hso=zero
do ispin=1,Nspin
do jspin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
is = iorb + (ispin-1)*Norb !spin-orbit stride
js = jorb + (jspin-1)*Norb !spin-orbit stride
Hso(is,js) = Hnn(ispin,jspin,iorb,jorb)
enddo
enddo
enddo
enddo
end function c_nn2nso
!+-----------------------------------------------------------------------------+!
!PURPOSE:
! reshape rectangular
!+-----------------------------------------------------------------------------+!
function d_rectangular_j2n_scalar(Hlso,Nlat,Nlat2,Nspin,Nspin2,Norb,Norb2) result(Hnnn)
integer :: Nlat,Nspin,Norb,Nlat2,Nspin2,Norb2
real(8),dimension(Nlat*Nspin*Norb,Nlat2*Nspin2*Norb2) :: Hlso
real(8),dimension(Nlat,Nlat2,Nspin,Nspin2,Norb,Norb2) :: Hnnn
integer :: ilat,jlat
integer :: iorb,jorb
integer :: ispin,jspin
integer :: is,js
Hnnn=zero
do ilat=1,Nlat
do jlat=1,Nlat2
do ispin=1,Nspin
do jspin=1,Nspin2
do iorb=1,Norb
do jorb=1,Norb2
is = iorb + (ilat-1)*Norb + (ispin-1)*Norb*Nlat
js = jorb + (jlat-1)*Norb2 + (jspin-1)*Norb2*Nlat2
Hnnn(ilat,jlat,ispin,jspin,iorb,jorb) = Hlso(is,js)
enddo
enddo
enddo
enddo
enddo
enddo
end function d_rectangular_j2n_scalar
!
function d_rectangular_n2j_scalar(Hnnn,Nlat,Nlat2,Nspin,Nspin2,Norb,Norb2) result(Hlso)
integer :: Nlat,Nspin,Norb,Nlat2,Nspin2,Norb2
real(8),dimension(Nlat*Nspin*Norb,Nlat2*Nspin2*Norb2) :: Hlso
real(8),dimension(Nlat,Nlat2,Nspin,Nspin2,Norb,Norb2) :: Hnnn
integer :: ilat,jlat
integer :: iorb,jorb
integer :: ispin,jspin
integer :: is,js
Hlso=zero
do ilat=1,Nlat
do jlat=1,Nlat2
do ispin=1,Nspin
do jspin=1,Nspin2
do iorb=1,Norb
do jorb=1,Norb2
is = iorb + (ilat-1)*Norb + (ispin-1)*Norb*Nlat
js = jorb + (jlat-1)*Norb2 + (jspin-1)*Norb2*Nlat2
Hlso(is,js) = Hnnn(ilat,jlat,ispin,jspin,iorb,jorb)
enddo
enddo
enddo
enddo
enddo
enddo
end function d_rectangular_n2j_scalar
!
function c_rectangular_j2n_scalar(Hlso,Nlat,Nlat2,Nspin,Nspin2,Norb,Norb2) result(Hnnn)
integer :: Nlat,Nspin,Norb,Nlat2,Nspin2,Norb2
complex(8),dimension(Nlat*Nspin*Norb,Nlat2*Nspin2*Norb2) :: Hlso
complex(8),dimension(Nlat,Nlat2,Nspin,Nspin2,Norb,Norb2) :: Hnnn
integer :: ilat,jlat
integer :: iorb,jorb
integer :: ispin,jspin
integer :: is,js
Hnnn=zero
do ilat=1,Nlat
do jlat=1,Nlat2
do ispin=1,Nspin
do jspin=1,Nspin2
do iorb=1,Norb
do jorb=1,Norb2
is = iorb + (ilat-1)*Norb + (ispin-1)*Norb*Nlat
js = jorb + (jlat-1)*Norb2 + (jspin-1)*Norb2*Nlat2
Hnnn(ilat,jlat,ispin,jspin,iorb,jorb) = Hlso(is,js)
enddo
enddo
enddo
enddo
enddo
enddo
end function c_rectangular_j2n_scalar
!
function c_rectangular_n2j_scalar(Hnnn,Nlat,Nlat2,Nspin,Nspin2,Norb,Norb2) result(Hlso)
integer :: Nlat,Nspin,Norb,Nlat2,Nspin2,Norb2
complex(8),dimension(Nlat,Nlat2,Nspin,Nspin2,Norb,Norb2) :: Hnnn
complex(8),dimension(Nlat*Nspin*Norb,Nlat2*Nspin2*Norb2) :: Hlso
integer :: ilat,jlat
integer :: iorb,jorb
integer :: ispin,jspin
integer :: is,js
Hlso=zero
do ilat=1,Nlat
do jlat=1,Nlat2
do ispin=1,Nspin
do jspin=1,Nspin2
do iorb=1,Norb
do jorb=1,Norb2
is = iorb + (ilat-1)*Norb + (ispin-1)*Norb*Nlat
js = jorb + (jlat-1)*Norb2 + (jspin-1)*Norb2*Nlat2
Hlso(is,js) = Hnnn(ilat,jlat,ispin,jspin,iorb,jorb)
enddo
enddo
enddo
enddo
enddo
enddo
end function c_rectangular_n2j_scalar
#if __GNUC__ > 6
!##################################################################
!##################################################################
! ROUTINES TO READ AND WRITE CLUSTER GREEN FUNCTION
! unformatted and formatted I/O
!##################################################################
!##################################################################
!+-------------------------------------------------------------------+
!PURPOSE : write overload for GFmatrix type (formatted)
!+-------------------------------------------------------------------+
subroutine write_formatted(dtv, unit, iotype, v_list, iostat, iomsg)
class(GFmatrix), intent(in) :: dtv
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(in) :: iotype
integer, intent(in) :: v_list(:)
integer :: Nexc,iexc,Ichan,ilat,jlat,iorb,ispin,istate
integer :: Nchan,Nstates
character(*), intent(inout) :: iomsg
!
!
Nstates = size(dtv%state)
write (unit, *,IOSTAT=iostat, IOMSG=iomsg) Nstates
do istate=1,Nstates
Nchan = size(dtv%state(istate)%channel)
write (unit, *,IOSTAT=iostat, IOMSG=iomsg) Nchan
do ichan=1,Nchan
write (unit, *,IOSTAT=iostat, IOMSG=iomsg) size(dtv%state(istate)%channel(ichan)%poles)
write (unit, *,IOSTAT=iostat, IOMSG=iomsg) dtv%state(istate)%channel(ichan)%poles
write (unit, *,IOSTAT=iostat, IOMSG=iomsg) dtv%state(istate)%channel(ichan)%weight
enddo
write (unit, *,IOSTAT=iostat, IOMSG=iomsg) "\n"
enddo
!
end subroutine write_formatted
!+-------------------------------------------------------------------+
!PURPOSE : read overload for GFmatrix type (formatted)
!+-------------------------------------------------------------------+
subroutine read_formatted(dtv, unit,iotype, v_list, iostat, iomsg)
class(GFmatrix), intent(inout) :: dtv
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(in) :: iotype
integer, intent(in) :: v_list(:)
character(*), intent(inout) :: iomsg
logical :: alloc
integer :: ichan,Nchan,Nlanc,istate,Nstates
!
read (unit,*,IOSTAT=iostat, IOMSG=iomsg) Nstates
call GFmatrix_allocate(dtv,Nstate=Nstates)
do istate=1,Nstates
read (unit,*,IOSTAT=iostat, IOMSG=iomsg) Nchan
call GFmatrix_allocate(dtv,istate=istate,Nchan=Nchan)
do ichan=1,Nchan
read (unit,*, IOSTAT=iostat, IOMSG=iomsg) Nlanc
call GFmatrix_allocate(dtv,istate=istate,ichan=ichan,Nexc=Nlanc)
read (unit, *, IOSTAT=iostat, IOMSG=iomsg) dtv%state(istate)%channel(ichan)%poles
read (unit, *, IOSTAT=iostat, IOMSG=iomsg) dtv%state(istate)%channel(ichan)%weight
enddo
enddo
!
end subroutine read_formatted
!+-------------------------------------------------------------------+
!PURPOSE : write overload for GFmatrix type (unformatted)
!+-------------------------------------------------------------------+
subroutine write_unformatted(dtv, unit, iostat, iomsg)
class(GFmatrix), intent(in) :: dtv
integer, intent(in) :: unit
integer, intent(out) :: iostat
integer :: Nexc,iexc,Ichan,ilat,jlat,iorb,ispin,istate
integer :: Nchan,Nstates
character(*), intent(inout) :: iomsg
!
!
Nstates = size(dtv%state)
write (unit, IOSTAT=iostat, IOMSG=iomsg) Nstates
do istate=1,Nstates
Nchan = size(dtv%state(istate)%channel)
write (unit, IOSTAT=iostat, IOMSG=iomsg) Nchan
do ichan=1,Nchan
write (unit, IOSTAT=iostat, IOMSG=iomsg) size(dtv%state(istate)%channel(ichan)%poles), dtv%state(istate)%channel(ichan)%poles, dtv%state(istate)%channel(ichan)%weight
enddo
enddo
!
end subroutine write_unformatted
!+-------------------------------------------------------------------+
!PURPOSE : read overload for GFmatrix type (unformatted)
!+-------------------------------------------------------------------+
subroutine read_unformatted(dtv, unit, iostat, iomsg)
class(GFmatrix), intent(inout) :: dtv
integer, intent(in) :: unit
integer, intent(out) :: iostat
character(*), intent(inout) :: iomsg
logical :: alloc
integer :: ichan,Nchan,Nlanc,istate,Nstates
!
read (unit, IOSTAT=iostat, IOMSG=iomsg) Nstates
call GFmatrix_allocate(dtv,Nstate=Nstates)
do istate=1,Nstates
read (unit, IOSTAT=iostat, IOMSG=iomsg) Nchan
call GFmatrix_allocate(dtv,istate=istate,Nchan=Nchan)
do ichan=1,Nchan
read (unit, IOSTAT=iostat, IOMSG=iomsg) Nlanc
call GFmatrix_allocate(dtv,istate=istate,ichan=ichan,Nexc=Nlanc)
read (unit, IOSTAT=iostat, IOMSG=iomsg) dtv%state(istate)%channel(ichan)%poles
read (unit, IOSTAT=iostat, IOMSG=iomsg) dtv%state(istate)%channel(ichan)%weight
enddo
enddo
!
end subroutine read_unformatted
#endif
!+-------------------------------------------------------------------+
!PURPOSE : Save cluster GF to file
!+-------------------------------------------------------------------+
subroutine save_gfprime(file,used,use_formatted)
character(len=*),optional :: file
character(len=256) :: file_
logical,optional :: used
logical :: used_
logical,optional :: use_formatted
logical :: use_formatted_
character(len=16) :: extension
integer :: unit_,Nchannel,Nexc,ichan,iexc,ilat,jlat,ispin,iorb,jorb
!
#if __GNUC__ > 6
if(.not.allocated(impGmatrix))stop "vca_gf_cluster ERROR: impGmatrix not allocated!"
used_=.false.;if(present(used))used_=used
use_formatted_=.false.;if(present(use_formatted))use_formatted_=use_formatted
extension=".restart";if(used_)extension=".used"
file_=str(str(file)//str(file_suffix)//str(extension))
unit_=free_unit()
!
if(use_formatted_)then
open(unit_,file=str(file_),access='sequential')
else
open(unit_,file=str(file_),form='unformatted',access='sequential')
endif
!
do ilat=1,Nlat
do jlat=1,Nlat
do ispin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
if(use_formatted_)then
write(unit_,*)impGmatrix(ilat,jlat,ispin,ispin,iorb,jorb)
else
write(unit_)impGmatrix(ilat,jlat,ispin,ispin,iorb,jorb)
endif
enddo
enddo
enddo
enddo
enddo
close(unit_)
#else
print*,"Rear/write overloading requires Gfortran 6+"
#endif
end subroutine save_gfprime
!+-------------------------------------------------------------------+
!PURPOSE : Read cluster GF from file
!+-------------------------------------------------------------------+
subroutine read_gfprime(file,used,use_formatted)
character(len=*),optional :: file
character(len=256) :: file_
logical,optional :: used
logical :: used_
logical,optional :: use_formatted
logical :: use_formatted_
character(len=16) :: extension
integer :: unit_,Nchannel,Nexc,ichan,iexc,ilat,jlat,ispin,iorb,jorb
!
#if __GNUC__ > 6
if(allocated(impGmatrix))deallocate(impGmatrix)
allocate(impGmatrix(Nlat,Nlat,Nspin,Nspin,Norb,Norb))
used_=.false.;if(present(used))used_=used
use_formatted_=.false.;if(present(use_formatted))use_formatted_=use_formatted
extension=".restart";if(used_)extension=".used"
file_=str(str(file)//str(file_suffix)//str(extension))
unit_=free_unit()
!
if(use_formatted_)then
open(unit_,file=str(file_),access='sequential')
else
open(unit_,file=str(file_),form='unformatted',access='sequential')
endif
!
rewind(unit_)
!
do ilat=1,Nlat
do jlat=1,Nlat
do ispin=1,Nspin
do iorb=1,Norb
do jorb=1,Norb
if(use_formatted_)then
read(unit_,*)impGmatrix(ilat,jlat,ispin,ispin,iorb,jorb)
else
read(unit_)impGmatrix(ilat,jlat,ispin,ispin,iorb,jorb)
endif
enddo
enddo
enddo
enddo
enddo
close(unit_)
#else
print*,"Rear/write overloading requires Gfortran 6+"
#endif
end subroutine read_gfprime
!##################################################################
!##################################################################
! ROUTINES TO SEARCH CHEMICAL POTENTIAL UP TO SOME ACCURACY
! can be used to fix any other *var so that *ntmp == nread
!##################################################################
!##################################################################
!+------------------------------------------------------------------+
!PURPOSE :
!+------------------------------------------------------------------+
subroutine vca_search_variable(var,ntmp,converged)
real(8),intent(inout) :: var
real(8),intent(in) :: ntmp
logical,intent(inout) :: converged
logical :: bool
real(8),save :: chich
real(8),save :: nold
real(8),save :: var_new
real(8),save :: var_old
real(8) :: var_sign
!
real(8) :: ndiff
integer,save :: count=0,totcount=0,i
integer :: unit
!
!check actual value of the density *ntmp* with respect to goal value *nread*
count=count+1
totcount=totcount+1
!
if(count==1)then
chich = ndelta !~0.2
inquire(file="var_compressibility.restart",EXIST=bool)
if(bool)then
open(free_unit(unit),file="var_compressibility.restart")
read(unit,*)chich
close(unit)
endif
var_old = var
endif
!
ndiff=ntmp-nread
!
!Get 'charge compressibility"
if(count>1)chich = (ntmp-nold)/(var-var_old)
!
!Add here controls on chich: not to be too small....
!
!update chemical potential
var_new = var - ndiff/chich
!
!
!re-define variables:
nold = ntmp
var_old = var
var = var_new
!
!Print information
write(LOGfile,"(A9,F16.9,A,F15.9)") "n = ",ntmp,"| instead of",nread
write(LOGfile,"(A9,ES16.9,A,ES16.9)")"dn = ",ndiff,"/",nerr
var_sign = (var-var_old)/abs(var-var_old)
if(var_sign>0d0)then
write(LOGfile,"(A9,ES16.9,A4)")"shift = ",ndiff/chich," ==>"
else
write(LOGfile,"(A9,ES16.9,A4)")"shift = ",ndiff/chich," <=="
end if
write(LOGfile,"(A9,F16.9)")"var = ",var
!
!Save info about search variable iteration:
open(free_unit(unit),file="search_variable_iteration_info"//reg(file_suffix)//".ed",position="append")
if(count==1)write(unit,*)"#var,ntmp,ndiff"
write(unit,*)var,ntmp,ndiff
close(unit)
!
!If density is not converged set convergence to .false.
if(abs(ndiff)>nerr)converged=.false.
!
write(LOGfile,"(A18,I5)")"Search var count= ",count
write(LOGfile,"(A19,L2)")"Converged = ",converged
print*,""
!
open(free_unit(unit),file="var_compressibility.used")
write(unit,*)chich
close(unit)
!
end subroutine vca_search_variable
!+------------------------------------------------------------------+
!PURPOSE :
!+------------------------------------------------------------------+
subroutine search_chemical_potential(var,ntmp,converged)
real(8),intent(inout) :: var
real(8),intent(in) :: ntmp
logical,intent(inout) :: converged
logical :: bool
real(8) :: ndiff
integer,save :: count=0,totcount=0,i
integer,save :: nindex=0
integer :: nindex_old(3)
real(8) :: ndelta_old,nratio
integer,save :: nth_magnitude=-2,nth_magnitude_old=-2
real(8),save :: nth=1.d-2
logical,save :: ireduce=.true.
integer :: unit
!
ndiff=ntmp-nread
nratio = 0.5d0;!nratio = 1.d0/(6.d0/11.d0*pi)
!
!check actual value of the density *ntmp* with respect to goal value *nread*
count=count+1
totcount=totcount+1
if(count>2)then
do i=1,2
nindex_old(i+1)=nindex_old(i)
enddo
endif
nindex_old(1)=nindex
!
if(ndiff >= nth)then
nindex=-1
elseif(ndiff <= -nth)then
nindex=1
else
nindex=0
endif
!
ndelta_old=ndelta
bool=nindex/=0.AND.( (nindex+nindex_old(1)==0).OR.(nindex+sum(nindex_old(:))==0) )
!if(nindex_old(1)+nindex==0.AND.nindex/=0)then !avoid loop forth and back
if(bool)then
ndelta=ndelta_old*nratio !decreasing the step
else
ndelta=ndelta_old
endif
!
if(ndelta_old<1.d-9)then
ndelta_old=0.d0
nindex=0
endif
!update chemical potential
var=var+dble(nindex)*ndelta
!xmu=xmu+dble(nindex)*ndelta
!
!Print information
write(LOGfile,"(A,f16.9,A,f15.9)")"n = ",ntmp," /",nread
if(nindex>0)then
write(LOGfile,"(A,es16.9,A)")"shift= ",nindex*ndelta," ==>"
elseif(nindex<0)then
write(LOGfile,"(A,es16.9,A)")"shift= ",nindex*ndelta," <=="
else
write(LOGfile,"(A,es16.9,A)")"shift= ",nindex*ndelta," == "
endif
write(LOGfile,"(A,f15.9)")"var = ",var
write(LOGfile,"(A,ES16.9,A,ES16.9)")"dn = ",ndiff,"/",nth
unit=free_unit()
open(unit,file="search_mu_iteration"//reg(file_suffix)//".ed",position="append")
write(unit,*)var,ntmp,ndiff
close(unit)
!
!check convergence within actual threshold
!if reduce is activetd
!if density is in the actual threshold
!if DMFT is converged
!if threshold is larger than nerror (i.e. this is not last loop)
bool=ireduce.AND.(abs(ndiff)<nth).AND.converged.AND.(nth>nerr)
if(bool)then
nth_magnitude_old=nth_magnitude !save old threshold magnitude
nth_magnitude=nth_magnitude_old-1 !decrease threshold magnitude || floor(log10(abs(ntmp-nread)))
nth=max(nerr,10.d0**(nth_magnitude)) !set the new threshold
count=0 !reset the counter
converged=.false. !reset convergence
ndelta=ndelta_old*nratio !reduce the delta step
!
endif
!
!if density is not converged set convergence to .false.
if(abs(ntmp-nread)>nth)converged=.false.
!
!check convergence for this threshold
!!---if smallest threshold-- NO MORE
!if reduce is active (you reduced the treshold at least once)
!if # iterations > max number
!if not yet converged
!set threshold back to the previous larger one.
!bool=(nth==nerr).AND.ireduce.AND.(count>niter).AND.(.not.converged)
bool=ireduce.AND.(count>niter).AND.(.not.converged)
if(bool)then
ireduce=.false.
nth=10.d0**(nth_magnitude_old)
endif
!
write(LOGfile,"(A,I5)")"count= ",count
write(LOGfile,"(A,L2)")"Converged=",converged
print*,""
!
end subroutine search_chemical_potential
END MODULE VCA_AUX_FUNX