-
Notifications
You must be signed in to change notification settings - Fork 12
/
Elec_Str.F90
executable file
·1105 lines (1041 loc) · 27.9 KB
/
Elec_Str.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
MODULE EIGENVAL_mod
USE KINDS
IMPLICIT NONE
TYPE eigen_t
INTEGER:: NIONS
INTEGER:: ISPIN,NELECT,NKPTS,NB_TOT
REAL(dp),POINTER:: VKPT(:,:)=>NULL(),WTKPT(:)=>NULL()
REAL(dp),POINTER:: CELTOT(:,:,:)=>NULL() !(NB_TOT,NKPTS,ISPIN)
END TYPE eigen_t
CONTAINS
SUBROUTINE read_EIGENVAL(iu,eigen_data,lgood)
IMPLICIT NONE
INTEGER,INTENT(in):: iu
TYPE(eigen_t),INTENT(out):: eigen_data
LOGICAL,INTENT(out):: lgood
!local var
INTEGER:: it,NK,N
!=======================
lgood=.TRUE.
READ(iu,'(4I5)',ERR=100,END=100)eigen_data%NIONS,it,it,eigen_data%ISPIN
READ(iu,'(5E15.7)',ERR=100,END=100)
READ(iu,*,ERR=100,END=100)
READ(iu,*,ERR=100,END=100)
READ(iu,*,ERR=100,END=100)
READ(iu,*,ERR=100,END=100) eigen_data%NELECT,eigen_data%NKPTS,eigen_data%NB_TOT
ALLOCATE(eigen_data%VKPT(3,eigen_data%NKPTS))
ALLOCATE(eigen_data%WTKPT(eigen_data%NKPTS))
ALLOCATE(eigen_data%CELTOT(eigen_data%NB_TOT,eigen_data%NKPTS,eigen_data%ISPIN))
DO NK=1,eigen_data%NKPTS
READ(iu,*,ERR=100,END=100)
READ(iu,'(4E15.7)',ERR=100,END=100) eigen_data%VKPT(1,NK),eigen_data%VKPT(2,NK),eigen_data%VKPT(3,NK),&
& eigen_data%WTKPT(NK)
DO N=1,eigen_data%NB_TOT
IF (eigen_data%ISPIN==1) READ(iu,852,ERR=100,END=100)eigen_data%CELTOT(N,NK,1)
IF (eigen_data%ISPIN==2) &
READ(iu,8852,ERR=100,END=100)eigen_data%CELTOT(N,NK,1),eigen_data%CELTOT(N,NK,2)
ENDDO
ENDDO
!852 FORMAT(1X,I3,4X,F10.4)
!8852 FORMAT(1X,I3,4X,F10.4,2X,F10.4)
852 FORMAT(1X,3X,4X,F10.4)
8852 FORMAT(1X,3X,4X,F10.4,2X,F10.4)
RETURN
100 CONTINUE
lgood=.FALSE.
END SUBROUTINE read_EIGENVAL
SUBROUTINE del_eigen_t(eigen_data)
IMPLICIT NONE
TYPE(eigen_t),INTENT(inout):: eigen_data
IF(ASSOCIATED(eigen_data%VKPT))DEALLOCATE(eigen_data%VKPT)
IF(ASSOCIATED(eigen_data%WTKPT))DEALLOCATE(eigen_data%WTKPT)
IF(ASSOCIATED(eigen_data%CELTOT))DEALLOCATE(eigen_data%CELTOT)
END SUBROUTINE del_eigen_t
END MODULE EIGENVAL_mod
MODULE sort_mod
USE KINDS
IMPLICIT NONE
INTERFACE SSORT
MODULE PROCEDURE SSORT_I,SSORT_F
END INTERFACE
CONTAINS
SUBROUTINE SSORT_I (X, Y, N, KFLAG)
!***BEGIN PROLOGUE SSORT
!***PURPOSE Sort an array and optionally make the same interchanges in
! an auxiliary array. The array may be sorted in increasing
! or decreasing order. A slightly modified QUICKSORT
! algorithm is used.
!***LIBRARY SLATEC
!***CATEGORY N6A2B
!***TYPE SINGLE PRECISION (SSORT-S, DSORT-D, ISORT-I)
!***KEYWORDS SINGLETON QUICKSORT, SORT, SORTING
!***AUTHOR Jones, R. E., (SNLA)
! Wisniewski, J. A., (SNLA)
!***DESCRIPTION
!
! SSORT sorts array X and optionally makes the same interchanges in
! array Y. The array X may be sorted in increasing order or
! decreasing order. A slightly modified quicksort algorithm is used.
!
! Description of Parameters
! X - array of values to be sorted (usually abscissas)
! Y - array to be (optionally) carried along
! N - number of values in array X to be sorted
! KFLAG - control parameter
! = 2 means sort X in increasing order and carry Y along.
! = 1 means sort X in increasing order (ignoring Y)
! = -1 means sort X in decreasing order (ignoring Y)
! = -2 means sort X in decreasing order and carry Y along.
!
!***REFERENCES R. C. Singleton, Algorithm 347, An efficient algorithm
! for sorting with minimal storage, Communications of
! the ACM, 12, 3 (1969), pp. 185-187.
!***REVISION HISTORY (YYMMDD)
! 761101 DATE WRITTEN
! 761118 Modified to use the Singleton quicksort algorithm. (JAW)
! 890531 Changed all specific intrinsics to generic. (WRB)
! 890831 Modified array declarations. (WRB)
! 891009 Removed unreferenced statement labels. (WRB)
! 891024 Changed category. (WRB)
! 891024 REVISION DATE from Version 3.2
! 891214 Prologue converted to Version 4.0 format. (BAB)
! 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
! 901012 Declared all variables; changed X,Y to SX,SY. (M. McClain)
! 920501 Reformatted the REFERENCES section. (DWL, WRB)
! 920519 Clarified error messages. (DWL)
! 920801 Declarations section rebuilt and code restructured to use
! IF-THEN-ELSE-ENDIF. (RWC, WRB)
!***END PROLOGUE SSORT
! .. Scalar Arguments ..
INTEGER KFLAG, N
! .. Array Arguments ..
! xhj change here
! REAL X(*), Y(*)
INTEGER :: X(*)
REAL(dp):: Y(*)
!xhj change end here
! .. Local Scalars ..
REAL(dp):: R, T, TT, TTY, TY
INTEGER I, IJ, J, K, KK, L, M, NN
! .. Local Arrays ..
INTEGER IL(21), IU(21)
! .. External Subroutines ..
! None
! .. Intrinsic Functions ..
INTRINSIC ABS, INT
!***FIRST EXECUTABLE STATEMENT SSORT
NN = N
IF (NN .LT. 1) THEN
PRINT *, &
& 'The number of values to be sorted is not positive.'
RETURN
ENDIF
!
KK = ABS(KFLAG)
IF (KK.NE.1 .AND. KK.NE.2) THEN
PRINT *, &
& 'The sort control parameter, K, is not 2, 1, -1, or -2.'
RETURN
ENDIF
!
! Alter array X to get decreasing order if needed
!
IF (KFLAG .LE. -1) THEN
DO 10 I=1,NN
X(I) = -X(I)
10 END DO
! 10 CONTINUE
ENDIF
!
IF (KK .EQ. 2) GO TO 100
!
! Sort X only
!
M = 1
I = 1
J = NN
R = 0.375E0
!
20 IF (I .EQ. J) GO TO 60
IF (R .LE. 0.5898437E0) THEN
R = R+3.90625E-2
ELSE
R = R-0.21875E0
ENDIF
!
30 K = I
!
! Select a central element of the array and save it in location T
!
IJ = I + INT((J-I)*R)
T = X(IJ)
!
! If first element of array is greater than T, interchange with T
!
IF (X(I) .GT. T) THEN
X(IJ) = X(I)
X(I) = T
T = X(IJ)
ENDIF
L = J
!
! If last element of array is less than than T, interchange with T
!
IF (X(J) .LT. T) THEN
X(IJ) = X(J)
X(J) = T
T = X(IJ)
!
! If first element of array is greater than T, interchange with T
!
IF (X(I) .GT. T) THEN
X(IJ) = X(I)
X(I) = T
T = X(IJ)
ENDIF
ENDIF
!
! Find an element in the second half of the array which is smaller
! than T
!
40 L = L-1
IF (X(L) .GT. T) GO TO 40
!
! Find an element in the first half of the array which is greater
! than T
!
50 K = K+1
IF (X(K) .LT. T) GO TO 50
!
! Interchange these elements
!
IF (K .LE. L) THEN
TT = X(L)
X(L) = X(K)
X(K) = TT
GO TO 40
ENDIF
!
! Save upper and lower subscripts of the array yet to be sorted
!
IF (L-I .GT. J-K) THEN
IL(M) = I
IU(M) = L
I = K
M = M+1
ELSE
IL(M) = K
IU(M) = J
J = L
M = M+1
ENDIF
GO TO 70
!
! Begin again on another portion of the unsorted array
!
60 M = M-1
IF (M .EQ. 0) GO TO 190
I = IL(M)
J = IU(M)
!
70 IF (J-I .GE. 1) GO TO 30
IF (I .EQ. 1) GO TO 20
I = I-1
!
80 I = I+1
IF (I .EQ. J) GO TO 60
T = X(I+1)
IF (X(I) .LE. T) GO TO 80
K = I
!
90 X(K+1) = X(K)
K = K-1
IF (T .LT. X(K)) GO TO 90
X(K+1) = T
GO TO 80
!
! Sort X and carry Y along
!
100 M = 1
I = 1
J = NN
R = 0.375E0
!
110 IF (I .EQ. J) GO TO 150
IF (R .LE. 0.5898437E0) THEN
R = R+3.90625E-2
ELSE
R = R-0.21875E0
ENDIF
!
120 K = I
!
! Select a central element of the array and save it in location T
!
IJ = I + INT((J-I)*R)
T = X(IJ)
TY = Y(IJ)
!
! If first element of array is greater than T, interchange with T
!
IF (X(I) .GT. T) THEN
X(IJ) = X(I)
X(I) = T
T = X(IJ)
Y(IJ) = Y(I)
Y(I) = TY
TY = Y(IJ)
ENDIF
L = J
!
! If last element of array is less than T, interchange with T
!
IF (X(J) .LT. T) THEN
X(IJ) = X(J)
X(J) = T
T = X(IJ)
Y(IJ) = Y(J)
Y(J) = TY
TY = Y(IJ)
!
! If first element of array is greater than T, interchange with T
!
IF (X(I) .GT. T) THEN
X(IJ) = X(I)
X(I) = T
T = X(IJ)
Y(IJ) = Y(I)
Y(I) = TY
TY = Y(IJ)
ENDIF
ENDIF
!
! Find an element in the second half of the array which is smaller
! than T
!
130 L = L-1
IF (X(L) .GT. T) GO TO 130
!
! Find an element in the first half of the array which is greater
! than T
!
140 K = K+1
IF (X(K) .LT. T) GO TO 140
!
! Interchange these elements
!
IF (K .LE. L) THEN
TT = X(L)
X(L) = X(K)
X(K) = TT
TTY = Y(L)
Y(L) = Y(K)
Y(K) = TTY
GO TO 130
ENDIF
!
! Save upper and lower subscripts of the array yet to be sorted
!
IF (L-I .GT. J-K) THEN
IL(M) = I
IU(M) = L
I = K
M = M+1
ELSE
IL(M) = K
IU(M) = J
J = L
M = M+1
ENDIF
GO TO 160
!
! Begin again on another portion of the unsorted array
!
150 M = M-1
IF (M .EQ. 0) GO TO 190
I = IL(M)
J = IU(M)
!
160 IF (J-I .GE. 1) GO TO 120
IF (I .EQ. 1) GO TO 110
I = I-1
!
170 I = I+1
IF (I .EQ. J) GO TO 150
T = X(I+1)
TY = Y(I+1)
IF (X(I) .LE. T) GO TO 170
K = I
!
180 X(K+1) = X(K)
Y(K+1) = Y(K)
K = K-1
IF (T .LT. X(K)) GO TO 180
X(K+1) = T
Y(K+1) = TY
GO TO 170
!
! Clean up
!
190 IF (KFLAG .LE. -1) THEN
DO 200 I=1,NN
X(I) = -X(I)
200 END DO
! 200 CONTINUE
ENDIF
RETURN
END SUBROUTINE SSORT_I
SUBROUTINE SSORT_F (X, Y, N, KFLAG)
!***BEGIN PROLOGUE SSORT
!***PURPOSE Sort an array and optionally make the same interchanges in
! an auxiliary array. The array may be sorted in increasing
! or decreasing order. A slightly modified QUICKSORT
! algorithm is used.
!***LIBRARY SLATEC
!***CATEGORY N6A2B
!***TYPE SINGLE PRECISION (SSORT-S, DSORT-D, ISORT-I)
!***KEYWORDS SINGLETON QUICKSORT, SORT, SORTING
!***AUTHOR Jones, R. E., (SNLA)
! Wisniewski, J. A., (SNLA)
!***DESCRIPTION
!
! SSORT sorts array X and optionally makes the same interchanges in
! array Y. The array X may be sorted in increasing order or
! decreasing order. A slightly modified quicksort algorithm is used.
!
! Description of Parameters
! X - array of values to be sorted (usually abscissas)
! Y - array to be (optionally) carried along
! N - number of values in array X to be sorted
! KFLAG - control parameter
! = 2 means sort X in increasing order and carry Y along.
! = 1 means sort X in increasing order (ignoring Y)
! = -1 means sort X in decreasing order (ignoring Y)
! = -2 means sort X in decreasing order and carry Y along.
!
!***REFERENCES R. C. Singleton, Algorithm 347, An efficient algorithm
! for sorting with minimal storage, Communications of
! the ACM, 12, 3 (1969), pp. 185-187.
!***REVISION HISTORY (YYMMDD)
! 761101 DATE WRITTEN
! 761118 Modified to use the Singleton quicksort algorithm. (JAW)
! 890531 Changed all specific intrinsics to generic. (WRB)
! 890831 Modified array declarations. (WRB)
! 891009 Removed unreferenced statement labels. (WRB)
! 891024 Changed category. (WRB)
! 891024 REVISION DATE from Version 3.2
! 891214 Prologue converted to Version 4.0 format. (BAB)
! 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
! 901012 Declared all variables; changed X,Y to SX,SY. (M. McClain)
! 920501 Reformatted the REFERENCES section. (DWL, WRB)
! 920519 Clarified error messages. (DWL)
! 920801 Declarations section rebuilt and code restructured to use
! IF-THEN-ELSE-ENDIF. (RWC, WRB)
!***END PROLOGUE SSORT
! .. Scalar Arguments ..
INTEGER KFLAG, N
! .. Array Arguments ..
! xhj change here
! REAL X(*), Y(*)
! INTEGER :: X(*)
!change back to real
REAL(dp):: X(*)
REAL(dp):: Y(*)
!xhj change end here
! .. Local Scalars ..
REAL(dp):: R, T, TT, TTY, TY
INTEGER I, IJ, J, K, KK, L, M, NN
! .. Local Arrays ..
INTEGER IL(21), IU(21)
! .. External Subroutines ..
! None
! .. Intrinsic Functions ..
INTRINSIC ABS, INT
!***FIRST EXECUTABLE STATEMENT SSORT
NN = N
IF (NN .LT. 1) THEN
PRINT *, &
& 'The number of values to be sorted is not positive.'
RETURN
ENDIF
!
KK = ABS(KFLAG)
IF (KK.NE.1 .AND. KK.NE.2) THEN
PRINT *, &
& 'The sort control parameter, K, is not 2, 1, -1, or -2.'
RETURN
ENDIF
!
! Alter array X to get decreasing order if needed
!
IF (KFLAG .LE. -1) THEN
DO 10 I=1,NN
X(I) = -X(I)
10 END DO
! 10 CONTINUE
ENDIF
!
IF (KK .EQ. 2) GO TO 100
!
! Sort X only
!
M = 1
I = 1
J = NN
R = 0.375E0
!
20 IF (I .EQ. J) GO TO 60
IF (R .LE. 0.5898437E0) THEN
R = R+3.90625E-2
ELSE
R = R-0.21875E0
ENDIF
!
30 K = I
!
! Select a central element of the array and save it in location T
!
IJ = I + INT((J-I)*R)
T = X(IJ)
!
! If first element of array is greater than T, interchange with T
!
IF (X(I) .GT. T) THEN
X(IJ) = X(I)
X(I) = T
T = X(IJ)
ENDIF
L = J
!
! If last element of array is less than than T, interchange with T
!
IF (X(J) .LT. T) THEN
X(IJ) = X(J)
X(J) = T
T = X(IJ)
!
! If first element of array is greater than T, interchange with T
!
IF (X(I) .GT. T) THEN
X(IJ) = X(I)
X(I) = T
T = X(IJ)
ENDIF
ENDIF
!
! Find an element in the second half of the array which is smaller
! than T
!
40 L = L-1
IF (X(L) .GT. T) GO TO 40
!
! Find an element in the first half of the array which is greater
! than T
!
50 K = K+1
IF (X(K) .LT. T) GO TO 50
!
! Interchange these elements
!
IF (K .LE. L) THEN
TT = X(L)
X(L) = X(K)
X(K) = TT
GO TO 40
ENDIF
!
! Save upper and lower subscripts of the array yet to be sorted
!
IF (L-I .GT. J-K) THEN
IL(M) = I
IU(M) = L
I = K
M = M+1
ELSE
IL(M) = K
IU(M) = J
J = L
M = M+1
ENDIF
GO TO 70
!
! Begin again on another portion of the unsorted array
!
60 M = M-1
IF (M .EQ. 0) GO TO 190
I = IL(M)
J = IU(M)
!
70 IF (J-I .GE. 1) GO TO 30
IF (I .EQ. 1) GO TO 20
I = I-1
!
80 I = I+1
IF (I .EQ. J) GO TO 60
T = X(I+1)
IF (X(I) .LE. T) GO TO 80
K = I
!
90 X(K+1) = X(K)
K = K-1
IF (T .LT. X(K)) GO TO 90
X(K+1) = T
GO TO 80
!
! Sort X and carry Y along
!
100 M = 1
I = 1
J = NN
R = 0.375E0
!
110 IF (I .EQ. J) GO TO 150
IF (R .LE. 0.5898437E0) THEN
R = R+3.90625E-2
ELSE
R = R-0.21875E0
ENDIF
!
120 K = I
!
! Select a central element of the array and save it in location T
!
IJ = I + INT((J-I)*R)
T = X(IJ)
TY = Y(IJ)
!
! If first element of array is greater than T, interchange with T
!
IF (X(I) .GT. T) THEN
X(IJ) = X(I)
X(I) = T
T = X(IJ)
Y(IJ) = Y(I)
Y(I) = TY
TY = Y(IJ)
ENDIF
L = J
!
! If last element of array is less than T, interchange with T
!
IF (X(J) .LT. T) THEN
X(IJ) = X(J)
X(J) = T
T = X(IJ)
Y(IJ) = Y(J)
Y(J) = TY
TY = Y(IJ)
!
! If first element of array is greater than T, interchange with T
!
IF (X(I) .GT. T) THEN
X(IJ) = X(I)
X(I) = T
T = X(IJ)
Y(IJ) = Y(I)
Y(I) = TY
TY = Y(IJ)
ENDIF
ENDIF
!
! Find an element in the second half of the array which is smaller
! than T
!
130 L = L-1
IF (X(L) .GT. T) GO TO 130
!
! Find an element in the first half of the array which is greater
! than T
!
140 K = K+1
IF (X(K) .LT. T) GO TO 140
!
! Interchange these elements
!
IF (K .LE. L) THEN
TT = X(L)
X(L) = X(K)
X(K) = TT
TTY = Y(L)
Y(L) = Y(K)
Y(K) = TTY
GO TO 130
ENDIF
!
! Save upper and lower subscripts of the array yet to be sorted
!
IF (L-I .GT. J-K) THEN
IL(M) = I
IU(M) = L
I = K
M = M+1
ELSE
IL(M) = K
IU(M) = J
J = L
M = M+1
ENDIF
GO TO 160
!
! Begin again on another portion of the unsorted array
!
150 M = M-1
IF (M .EQ. 0) GO TO 190
I = IL(M)
J = IU(M)
!
160 IF (J-I .GE. 1) GO TO 120
IF (I .EQ. 1) GO TO 110
I = I-1
!
170 I = I+1
IF (I .EQ. J) GO TO 150
T = X(I+1)
TY = Y(I+1)
IF (X(I) .LE. T) GO TO 170
K = I
!
180 X(K+1) = X(K)
Y(K+1) = Y(K)
K = K-1
IF (T .LT. X(K)) GO TO 180
X(K+1) = T
Y(K+1) = TY
GO TO 170
!
! Clean up
!
190 IF (KFLAG .LE. -1) THEN
DO 200 I=1,NN
X(I) = -X(I)
200 ENDDO
!200 CONTINUE
ENDIF
RETURN
END SUBROUTINE SSORT_F
END MODULE sort_mod
MODULE OUTCAR_mod
USE KINDS
IMPLICIT NONE
TYPE outcar_t
INTEGER:: na
!Startparameter for this run
INTEGER:: ISPIN
LOGICAL:: LNONCOLLINEAR
LOGICAL:: LSORBIT
REAL(dp):: NELECT
!output
REAL(dp):: EFERMI
END TYPE outcar_t
CONTAINS
SUBROUTINE read_OUTCAR(iu,out_data,lgood)
!read na
INTEGER,INTENT(in):: iu
TYPE(outcar_t),INTENT(out):: out_data
LOGICAL,INTENT(out):: lgood
!local var
CHARACTER(LEN=100):: tag,ch_read
LOGICAL:: lmatch
!==================================================
lgood=.TRUE.
tag='NIONS ='
CALL read_val(iu,tag,ch_read,0,lmatch)
IF(lmatch)THEN
READ(ch_read,*)out_data%na
ELSE
PRINT*,"read_OUTCAR:",tag, " not found"
!STOP
lgood=.FALSE.
RETURN
END IF
tag='ISPIN ='
CALL read_val(iu,tag,ch_read,0,lmatch)
IF(lmatch)THEN
READ(ch_read,*)out_data%ISPIN
ELSE
PRINT*,"read_OUTCAR:",tag, " not found"
lgood=.FALSE.
RETURN
END IF
tag='LNONCOLLINEAR ='
CALL read_val(iu,tag,ch_read,0,lmatch)
IF(lmatch)THEN
READ(ch_read,*)out_data%LNONCOLLINEAR
ELSE
PRINT*,"read_OUTCAR:",tag, " not found"
lgood=.FALSE.
RETURN
END IF
tag='LSORBIT ='
CALL read_val(iu,tag,ch_read,0,lmatch)
IF(lmatch)THEN
READ(ch_read,*)out_data%LSORBIT
ELSE
PRINT*,"read_OUTCAR:",tag, " not found"
lgood=.FALSE.
RETURN
END IF
tag='NELECT ='
CALL read_val(iu,tag,ch_read,0,lmatch)
IF(lmatch)THEN
READ(ch_read,*)out_data%NELECT
ELSE
PRINT*,"read_OUTCAR:",tag, " not found"
lgood=.FALSE.
RETURN
END IF
END SUBROUTINE read_OUTCAR
SUBROUTINE read_val(iu,tag,ch_read,ind,lmatch)
IMPLICIT NONE
INTEGER,INTENT(in):: iu,ind
!ind means which one you need
!sometimes, in a file, there are several occurences of the same tag
CHARACTER(LEN=*),INTENT(in):: tag
CHARACTER(LEN=*),INTENT(out):: ch_read
LOGICAL,INTENT(out):: lmatch
!local var
CHARACTER(LEN=200)::linestring
LOGICAL:: found,I_OPENED
!==========================================
INQUIRE (iu, OPENED=I_OPENED)
IF( .NOT. I_OPENED)THEN
STOP "read_val: file not open"
END IF
IF(ind .NE. 0)THEN
IF(ind .EQ. -1)THEN
!the last occurence of the tag in this file
found=.FALSE.
DO
READ(iu,'(A)',END=100)linestring
CALL match_line(linestring,tag,ch_read,lmatch)
IF(lmatch)THEN
found=.TRUE.
END IF
END DO
100 CONTINUE
IF(.NOT. found)THEN
REWIND(iu)
DO
READ(iu,'(A)',END=200)linestring
CALL match_line(linestring,tag,ch_read,lmatch)
IF(lmatch)THEN
found=.TRUE.
END IF
END DO
200 CONTINUE
END IF
IF(found)lmatch=.TRUE.
END IF
ELSE
!the default behavior is to find one place with the tag
DO
READ(iu,'(A)',END=300)linestring
CALL match_line(linestring,tag,ch_read,lmatch)
IF(lmatch)RETURN
END DO
300 CONTINUE
IF(.NOT. lmatch)THEN
REWIND(iu)
!another try
DO
READ(iu,'(A)',END=400)linestring
CALL match_line(linestring,tag,ch_read,lmatch)
IF(lmatch)RETURN
END DO
400 CONTINUE
END IF
END IF
END SUBROUTINE read_val
SUBROUTINE match_line(linestring,tag,ch_read,lmatch)
IMPLICIT NONE
CHARACTER(LEN=*),INTENT(in):: linestring,tag
CHARACTER(LEN=*),INTENT(out):: ch_read
LOGICAL,INTENT(out):: lmatch
!local var
INTEGER:: j,k,istart,n
!===============================================
ch_read=''
j = INDEX(linestring, TRIM(tag))
IF (j /= 0) THEN
DO k=j+LEN_TRIM(tag),LEN_TRIM(linestring)
IF(linestring(k:k) .NE. ' ')THEN
istart=k
EXIT
END IF
END DO
n=0
DO k=istart,LEN_TRIM(linestring)
IF(linestring(k:k) .NE. ' ')THEN
n=n+1
ch_read(n:n)=linestring(k:k)
ELSE
EXIT
END IF
END DO
lmatch=.TRUE.
ELSE
lmatch=.FALSE.
END IF
END SUBROUTINE match_line
END MODULE OUTCAR_mod
MODULE Elec_Str_mod
IMPLICIT NONE
CONTAINS
SUBROUTINE ES_gap(tag)
USE EIGENVAL_mod,ONLY: read_EIGENVAL,eigen_t,del_eigen_t
USE sort_mod,ONLY: SSORT
USE OUTCAR_mod,ONLY: read_OUTCAR,outcar_t
USE parameters, ONLY : pstruct,ES_mod
USE KINDS
!USE parameters , ONLY : Icode
IMPLICIT NONE
INTEGER(I4B), INTENT(IN) :: tag
TYPE(eigen_t):: eigen_data
TYPE(outcar_t):: out_data
INTEGER:: NK,is,iu,NB_OCC
LOGICAL:: lmetal,lgoodeig,lgoodOUTCAR,lexist
REAL(dp):: Eg_d,Eg_id,VBM,CBM,tran_direct_gap(3)
!=================================================
!IF(icode .EQ. 5)THEN
! RETURN
!END IF
lmetal=.FALSE.
!CALL io_assign(iu)
iu = 4411
OPEN(iu,file='EIGENVAL')
CALL read_EIGENVAL(iu,eigen_data,lgoodeig)
!CALL io_close(iu)
close(iu)
IF(lgoodeig)THEN
!sort energy in eigen_data%CELTOT
DO NK=1,eigen_data%NKPTS
DO is=1,eigen_data%ISPIN
CALL SSORT(eigen_data%CELTOT(:,NK,is), eigen_data%CELTOT(:,NK,is), &
& eigen_data%NB_TOT, 1)
END DO
END DO
END IF
!now read OUTCAR
!CALL io_assign(iu)
iu = 4412
OPEN(iu,file='OUTCAR')
CALL read_OUTCAR(iu,out_data,lgoodOUTCAR)
!CALL io_close(iu)
close(iu)
!now calculate the band gap
!for the global band gap (including direct and indirect gap)
IF(lgoodeig .AND. lgoodOUTCAR)THEN
IF(out_data%LNONCOLLINEAR)THEN
NB_OCC=eigen_data%NELECT
ELSEIF(out_data%ISPIN == 1)THEN
IF(MOD(eigen_data%NELECT,2) .EQ. 1)THEN
!we can not have a gap in the case of spin unpolarized calculation with odd number of electrons
lmetal=.TRUE.
ELSE
NB_OCC=eigen_data%NELECT/2
END IF
ELSEIF(out_data%ISPIN == 2)THEN
STOP "ES_gap: spin polarized case not implemented yet"
END IF
END IF
IF(lgoodeig .AND. lgoodOUTCAR)THEN
IF(.NOT. lmetal)THEN
Eg_d=1.0d9
Eg_id=1.0d9
!spin polarized system is not supported yet
IF(eigen_data%ISPIN .EQ. 1)THEN
DO NK=1,eigen_data%NKPTS
Eg_d=MIN(eigen_data%CELTOT(NB_OCC+1,NK,1)-eigen_data%CELTOT(NB_OCC,NK,1), Eg_d)
END DO
VBM=MAXVAL(eigen_data%CELTOT(NB_OCC,1:eigen_data%NKPTS,1))
CBM=MINVAL(eigen_data%CELTOT(NB_OCC+1,1:eigen_data%NKPTS,1))
Eg_id=MINVAL(eigen_data%CELTOT(NB_OCC+1,:,1))-MAXVAL(eigen_data%CELTOT(NB_OCC,:,1))
!!$ IF(ABS(Eg_d-Eg_id) .LT. 1.0d-9)THEN
!!$ WRITE(6,'(a,f20.12,a)')"Direct band gap: ",Eg_d, " eV"
!!$ ELSE