-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynom.f90
2088 lines (1843 loc) · 75.9 KB
/
Polynom.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: 'COPYPOL' FIRST: 22 Dec 2009 LAST EDIT: 11 Feb 2020
!
! PURPOSE:
! Copy [NTER] terms of the operator polynomial from one file to
! another.
!
! CALLED: MAIN (ANCO.for)
!
! INPUT:
! NQ -- The number of vibrational degrees of freedom;
! NTER --
! FILE_SRC --
! FILE_DST --
!
! NOTE(S):
!-----------------------------------------------------------------------
SUBROUTINE COPYPOL (NQ,NTER,FILE_SRC,FILE_DST)
IMPLICIT REAL*16 (A-H,O-Z), INTEGER*4 (I-N)
COMMON /IOLUN/ LOUT,NLOG,NINP,NOUT,NAUX,NSCR
CHARACTER*80 FILE_SRC,FILE_DST
INTEGER*1 IPOW(NQ,2)
INTEGER*4 Q
DATA NOPER /2/
IF (NTER .EQ. 0) THEN
IF (LOUT .GT. 1) WRITE (NOUT,1000)
RETURN
ENDIF
IF (LOUT .GT. 1) WRITE (NOUT,1100) NTER,FILE_SRC,FILE_DST
OPEN (UNIT = NAUX, FILE = FILE_SRC, &
& ACTION = 'READ', FORM = 'FORMATTED')
OPEN (UNIT = NSCR, FILE = FILE_DST, DISPOSE = 'SAVE', &
& ACTION = 'WRITE', FORM = 'UNFORMATTED', BUFFERED = 'YES')
DO 100 I = 1, NTER
READ (NAUX,110) N, COEF,((IPOW(Q,J),J=1,NOPER),Q=1,NQ)
WRITE (NSCR) COEF,((IPOW(Q,J),J=1,NOPER),Q=1,NQ)
100 CONTINUE
CLOSE (NAUX)
CLOSE (NSCR)
110 FORMAT(I4,1X,F32.16,20(1XI1))
RETURN
1000 FORMAT (/'[COPYPOL] Operator Polynomial Has Zero Length.')
1100 FORMAT (/'Copy Polynomial from One File to Another ', &
& '(The Number of Terms = ',I8,'):'/ &
& 'Source File: ',A64/'Destination File: ',A64)
END
!-----------------------------------------------------------------------
! Subroutine: 'ADDPOLF' FIRST: 01 Apr 2009 LAST EDIT: 06 Jan 2018
!
! PURPOSE:
! Evaluate a sum of two Operators Polynomials, save result in file.
! Resulting file may override any of the input files.
!
! ALGORITHM:
! The memory for total numbers of terms for OP1 + OP2 is allocated,
! all terms of OP1 are copied there, followed by terms of OP2.
! Then terms are collected in memory using COLLECT ().
! NTERS may coincide with NTER1 or NTER2.
!
! CALLED: MAINCVPT ();
!
! CALLS: COLLECT (), COLLECT3 ();
!
! INPUT:
! NQ -- The number of vibrational degrees of freedom;
! NTER1 -- The number of terms in the first operator polynomial;
! FILE1 -- File name for the first operator polynomial;
! FACT1 -- Common coefficient applied to the first polynomial;
! NTER2 -- The number of terms in the second operator polynomial;
! FILE2 -- File name for the second operator polynomial;
! FACT2 -- Common coefficient applied to the second polynomial;
! FILE_SUM: CHARACTER*80 file name for the resulting operator;
! Resulting file may override any of input files.
! TOL -- Tolerance parameter determining the accuracy of
! calculations. Terms with |COEF| < TOL are ignored.
!
! OUTPUT:
! NTERS -- The number of terms in the resulting operator polynom.
!
! FILE I/O:
! Result of the sum evaluation is saved in unformatted file FILE12
! in unformatted mode, LUN = NAUX.
!
! PRINTS:
! Single line announcement line and file name of the saved file.
!
! NOTE(S):
! 1. 04/07/2011 -- Routine COLLECT4 () is added as an option.
!-----------------------------------------------------------------------
SUBROUTINE ADDPOLF (NQ,NTERS,FILE_SUM,TOL,NTER1,FILE1, &
& FACT1,NTER2,FILE2,FACT2)
IMPLICIT REAL*16 (A-H,O-Z), INTEGER*4 (I-N)
COMMON /IOLUN/ LOUT,NLOG,NINP,NOUT,NAUX,NSCR
CHARACTER*64 PATH_CUR,PATH_PRO,PATH_OUT,PATH_BIN
COMMON /PATHS/ PATH_CUR,PATH_PRO,PATH_OUT,PATH_BIN
INTEGER*1 IPOW(NQ,2)
DIMENSION NPOWI(0:12) ! Suffix I -- Initial # of terms per power
CHARACTER*80 FILE1,FILE2,FILE_RAW,FILE_SUM
INTEGER*4 A,B,Q,S
DATA A,B /1,2/
IF (LOUT .GT. 1) THEN
WRITE (NOUT,1000)
WRITE (NOUT,1010) NTER1,FACT1,FILE1,NTER2,FACT2,FILE2
WRITE (NOUT,1020) FILE_SUM
ENDIF
FILE_RAW = TRIM(PATH_BIN)// 'ADDPOL.BIN'
OPEN (UNIT = NAUX, FILE = FILE_RAW, DISPOSE = 'SAVE', &
& BUFFERED = 'YES', FORM = 'UNFORMATTED')
!
!-----------------------------------------------------------------------
! Determine total and individual powers of the operator product
!-----------------------------------------------------------------------
MAXTOT = 0 ! Maximum total power in the whole operator product
MAXPOW = 0 ! Maximum power for an individual operator
NPOWI = 0 ! Vector counting the number of terms per SUM(powers)
IF (NTER1 .GT. 0) THEN
OPEN (UNIT = NSCR, FILE = FILE1, ACTION = 'READ', &
& STATUS = 'OLD', BUFFERED = 'YES', FORM = 'UNFORMATTED')
DO 110 I = 1, NTER1
READ (NSCR) COEF,(IPOW(Q,A),IPOW(Q,B),Q=1,NQ)
COEF = FACT1 * COEF
WRITE (NAUX) COEF,(IPOW(Q,A),IPOW(Q,B),Q=1,NQ)
!---- Evaluate total/individual powers of the operator product
S = 0
DO 100 Q = 1, NQ
S = S + IPOW(Q,A) + IPOW(Q,B)
MAXPOW = MAX (MAXPOW,IPOW(Q,A))
MAXPOW = MAX (MAXPOW,IPOW(Q,B))
100 CONTINUE
MAXTOT = MAX (MAXTOT,S)
NPOWI(S) = NPOWI(S) + 1
110 CONTINUE
CLOSE (NSCR)
ENDIF
IF (NTER2 .GT. 0) THEN
OPEN (UNIT = NSCR, FILE = FILE2, ACTION = 'READ', &
& STATUS = 'OLD') !, BUFFERED = 'YES', FORM = 'UNFORMATTED')
DO 210 I = 1, NTER2
READ (NSCR,"(I4,1X,F32.16,20(1XI1))") N, COEF,(IPOW(Q,A),IPOW(Q,B),Q=1,NQ)
COEF = FACT2 * COEF
WRITE (NAUX) COEF,(IPOW(Q,A),IPOW(Q,B),Q=1,NQ)
!---- Evaluate total/individual powers of the operator product
S = 0
DO 200 Q = 1, NQ
S = S + IPOW(Q,A) + IPOW(Q,B)
MAXPOW = MAX (MAXPOW,IPOW(Q,A))
MAXPOW = MAX (MAXPOW,IPOW(Q,B))
200 CONTINUE
MAXTOT = MAX (MAXTOT,S)
NPOWI(S) = NPOWI(S) + 1
210 CONTINUE
CLOSE (NSCR)
ENDIF
CLOSE (NAUX)
!---- Trick: NTERS can coincide with NTER1/NTER2,
!---- must avoid updating NTERS at this stage
NTOTAL = NTER1 + NTER2
!---- Disable removal of terms with MAXPOW, 'ICUT' = 1
IF (NTERS .LE. 10000) THEN
CALL COLLECT (NQ,NTOTAL,FILE_RAW,FILE_SUM,TOL,IERR,1,0)
ELSE
CALL COLLECT4 (NQ,NTOTAL,FILE_RAW,FILE_SUM,TOL, &
& MAXTOT,MAXPOW,NPOWI,IERR,1,0)
ENDIF
!---- Now can possibly override NTER1/NTER2
NTERS = NTOTAL
COEF = 100.0D0 * (1.0D0 - REAL (NTOTAL) / REAL (NTER1 + NTER2))
IF (LOUT .GT. 1) WRITE (NOUT,1030) NTERS,COEF
RETURN
1000 FORMAT (/'[ADDPOLF] -- Summation of Two Operator Polynomials ', &
& 'Stored in Files:')
1010 FORMAT ('Operand 1 (',I9,' terms): ',F8.4,2X,A64/ &
& 'Operand 2 (',I9,' terms): ',F8.4,2X,A64)
1020 FORMAT ('Result of Summation is Here: ',A64)
1030 FORMAT ('Final number of terms:',I9, &
& ', Compression factor =',F6.1,'%')
END
!-----------------------------------------------------------------------
! Subroutine: 'COLLECT' FIRST: 23 Jan 2010 LAST EDIT: 12 Feb 2013
!
! PURPOSE:
! Collect terms of an operator polynomial with identical operators
! with removal of terms with zero coefficients. Save result in file.
!
! FEATURES:
! 1. SINGLE PASS
! This version is using single-pass algorithm with reallocations
! 2. DOUBLE ENCODING
! Each operator is represented using a pair (CODE1,CODE2) of
! long integer numbers INT*8 for fast comparison.
! 3. QUICK SORT
! Resulting operator is sorted by coefficients using RQSORT ().
! 4. MINIMIZED INPUT
! No additonal information about polynomial (MAXTOT, NPOWINI)
! is required on entry to the routine.
!
! THEORY:
! It was empirically found, that for collecting terms appeared from
! commutators, terms with total power = MAXTOT cancel each other.
!
! ALGORITHM:
! Operator polynomial to be simplified is kept in a file LUN = NAUX.
! Each read term is encoded into pair (CODE1,CODE2).
!
! The algorithm of decoding:
! Array A[i1,i2,..iM], Each dimension = K, Order = M, Size = K^M.
!
! Inverse decoding (with special case mod()=0):
! First index is slowest, last index is fastest.
!
! L0 = K^(M-1)*(i1-1) + K^(M-2)*(i2-1) + .. + iM.
! i1 = (L0 - mod(L0,K^(M-1))) / K^(M-1) + 1, mod() > 0, or
! i1 = L0 / K^(M-1), mod() = 0;
! L1 = L0 - K^(M-1) * (i1-1);
! in = (L(n-1) - mod(L(n-1),K^(M-n))) / K^(M-n) + 1, mod() > 0, or
! in = L(n-1) / K^(M-n), mod() = 0;
! Ln = L(n-1) - K^(M-n) * (in-1);
! iM = (L(n-1) - mod(L(n-1),K^(M-n))) / K^(M-n) + 1, mod() > 0, or
! iM = L(n-1) / K^(M-n), mod() = 0.
!
! REFERENCE(S):
! [1] See My Journal 15/01/2010, page 25.
!
! CALLED: COMMUTE (Sibert.for); H_VIBROT (Commute.for).
!
! INPUT:
! NTER -- The number of primitive terms in operator polynomial;
! NQ -- The number of vibrational degrees of freedom;
! FILE_IN-- CHARACTER*80 file name for the original operator;
! FILE_SH-- CHARACTER*80 file name for the resulting operator;
! These filenames may coincide.
! TOL -- Tolerance parameter determining the accuracy of
! calculations. Terms with |COEF| < TOL are ignored;
! ICUT -- =1, collect all terms; =0, ignore total power = MAXTOT;
! IOUT -- =1, Request for output.
!
! OUTPUT:
! NTER -- The number of primitive terms in Operator Polynomial
! after Simplification (summation of equivalent terms);
! <FILE_SH> This File contains the resulting operator.
!
! NOTE(S):
! 1. 12/03/2010: Adding 'BUFFERED' = YES for OPEN () statements
! dramatically improved performance.
! 2. Plan: Write terms with equal total powers into separate files
! and then optimize them individually for saving memory.
!-----------------------------------------------------------------------
SUBROUTINE COLLECT (NQ,NTER,FILE_IN,FILE_SH,TOL,IERR,ICUT,IOUT)
IMPLICIT REAL*16 (A-H,O-Z), INTEGER*4 (I-N)
COMMON /IOLUN/ LOUT,NLOG,NINP,NOUT,NAUX,NSCR
COMMON /NUMBER/ PI,RADIAN,ZERO,HALF,ONE,TWO,THREE,FOUR,TEN(-16:16)
COMMON /PATHS/ PATH_EXE,PATH_PRO,PATH_OUT,PATH_BIN
CHARACTER*64 PATH_EXE,PATH_PRO,PATH_OUT,PATH_BIN
CHARACTER*80 FILE_IN,FILE_SH
CHARACTER*80 FILE_BUF,FILE_SRT
LOGICAL FILESTAT
INTEGER*1 IOPER(NQ,2)
DIMENSION INDEX(NQ,2)
DIMENSION NPOWI(0:12),NPOWF(0:12),NBASE(0:12)
DIMENSION MEMBLK(0:12),LENBUF(0:12)
DIMENSION MAXMEM(0:12),NALLOC(0:12)
LOGICAL TAIL(0:12)
ALLOCATABLE ARRAY(:),INDX(:)
TYPE :: PTR_TERM
REAL*16, DIMENSION(:), POINTER :: COEF
INTEGER*8, DIMENSION(:), POINTER :: CODE1,CODE2
ENDTYPE PTR_TERM
TYPE (PTR_TERM), ALLOCATABLE :: PTROPER(:)
INTEGER*4 A,B,Q
INTEGER*8 CODE1,CODE2,KK,LL,MODLK
INTEGER*8 MAXVAL_8
LOGICAL OVERFLOW
!-----------------------------------------------------------------------
DATA A,B /1,2/
IF (IOUT .GT. 0) THEN
CALL WATCH (ISPLIT)
WRITE (NOUT,1000)
ENDIF
!
!-----------------------------------------------------------------------
! Scan polynomial and find out:
! 1) Maximum total power of operators, MAXTOT;
! 2) The numbers of terms for each power, NPOWI(0..MAXTOT)
!-----------------------------------------------------------------------
INQUIRE (UNIT = NAUX, OPENED = FILESTAT)
IF (FILESTAT) STOP '[COLLECT] LUN = NAUX is engaged.'
OPEN (UNIT = NAUX, FILE = FILE_IN, STATUS = 'OLD', &
& DISPOSE = 'SAVE', FORM = 'UNFORMATTED')
MAXTOT = 0
MAXPOW = 0
NPOWI = 0 ! Vector assignment
DO 110 I = 1, NTER
READ (NAUX, IOSTAT = IERR, ERR = 930, END = 930) &
& COPER,(IOPER(Q,A),IOPER(Q,B),Q=1,NQ)
IF (IERR .NE. 0) THEN
WRITE (*,"(/'Record Number = ',I12)") I
WRITE (*,"(/'Error during READ, IOSTAT = ',I8)") IERR
GO TO 930
ENDIF
IF (ABS (COPER) .LT. TOL) CYCLE
M = 0
DO 100 Q = 1, NQ
M = M + IOPER(Q,A) + IOPER(Q,B)
MAXPOW = MAX (MAXPOW,IOPER(Q,A))
MAXPOW = MAX (MAXPOW,IOPER(Q,B))
100 CONTINUE
MAXTOT = MAX (MAXTOT,M)
NPOWI(M) = NPOWI(M) + 1
110 CONTINUE
REWIND (NAUX)
!---- K^NQ < MAX = 2^63; NQ*LOG(K) < 63*LOG(2); NQ < 63*LOG(2)/LOG(K)
IF (IOUT .GT. 0) THEN
K = MAXTOT + 1
MAXNQ = INT (63.0D0 * LOG (2.0D0) / LOG (REAL (K)))
WRITE (NOUT,1010) NTER,MAXTOT,MAXNQ
IF (ICUT .EQ. 0) THEN
K = MAXTOT - 1
MAXNQ = INT (63.0D0 * LOG (2.0D0) / LOG (REAL (K)))
WRITE (NOUT,1020) MAXNQ
ENDIF
ENDIF
!
!-----------------------------------------------------------------------
! Check for overflow and call COLLECT4 if necessary.
! NOTE: The input file will be deleted inside COLLECT4
!-----------------------------------------------------------------------
MAXVAL_8 = 2; MAXVAL_8 = -((-MAXVAL_8) ** 63 + 1)
MAXVAL_4 = 2; MAXVAL_4 = -((-MAXVAL_4) ** 31 + 1)
OVERFLOW = REAL (MAXTOT + 1) ** NQ .GT. REAL (MAXVAL_8)
IF (OVERFLOW) THEN
WRITE (NOUT,2100) NQ
CLOSE (NAUX)
CALL COLLECT4 (NQ,NTER,FILE_IN,FILE_SH,TOL, &
& MAXTOT,MAXPOW,NPOWI,IERR,ICUT,IOUT)
GO TO 900
ENDIF
2100 FORMAT (/'COLLECT: Unable to handle NQ =',I3,', call COLLECT4.')
!
!-----------------------------------------------------------------------
! Print summary table for all total powers M = 0..MAXTOT.
! Find out how many cases are possible for total power = M
!-----------------------------------------------------------------------
IF (IOUT .GT. 0) THEN
WRITE (NOUT,1100)
DO 120 M = 0, MAXTOT
NBASE(M) = M + 1 ! 0, 1, 2, .. M = M + 1 cases
OVERFLOW = REAL (NBASE(M)) ** NQ .GT. REAL (MAXVAL_8)
IF (.NOT. OVERFLOW) THEN
KK = NBASE(M) ! Convert to INTEGER*8
KK = KK ** NQ ! Max possible number of operators
ELSE
KK = -1
ENDIF
PART = 100.0D0 * REAL (NPOWI(M)) / REAL (NTER)
WRITE (NOUT,1110) M,NBASE(M),NPOWI(M),PART,KK
IF (ICUT .EQ. 0 .AND. M .EQ. MAXTOT) EXIT
IF (OVERFLOW .AND. NPOWI(M) .GT. 0) GO TO 910
120 CONTINUE
WRITE (NOUT,1120)
WRITE (NOUT,1130) REAL (MAXVAL_8),MAXVAL_8
WRITE (NOUT,1140) REAL (MAXVAL_4),MAXVAL_4
ENDIF
!
!-----------------------------------------------------------------------
! Allocate memory for dynamic arrays holding pairs (COEF,OFFSET)
!-----------------------------------------------------------------------
ALLOCATE (PTROPER(0:MAXTOT))
MEMBLK = 0
DO 150 M = 0, MAXTOT
IF (NPOWI(M) .EQ. 0) CYCLE
MEMBLK(M) = 1000 ! Initial size of the memory block
MAXMEM(M) = MEMBLK(M)
ALLOCATE (PTROPER(M)%COEF (MEMBLK(M)), STAT = IERR)
IF (IERR .NE. 0) GO TO 920
ALLOCATE (PTROPER(M)%CODE1(MEMBLK(M)), STAT = IERR)
IF (IERR .NE. 0) GO TO 920
ALLOCATE (PTROPER(M)%CODE2(MEMBLK(M)), STAT = IERR)
IF (IERR .NE. 0) GO TO 920
PTROPER(M)%COEF = 0.0D0
PTROPER(M)%CODE1 = 0
PTROPER(M)%CODE2 = 0
150 CONTINUE
!
!-----------------------------------------------------------------------
! Main Loop over terms of polynomial
!-----------------------------------------------------------------------
LENBUF = 0 ! The size of buffers for each total power
MEMEXT = 1000 ! Memory block extension size added on overflow
NALLOC = 0 ! The number of memory reallocations
TAIL = .FALSE. ! Set the flag for treatment of the trailing zeros
INQUIRE (UNIT = NSCR, OPENED = FILESTAT)
IF (FILESTAT) STOP '[COLLECT] LUN = NSCR ENGAGED.'
FILE_BUF = PATH_BIN(1:LENSTR(PATH_BIN,64)) // 'REALLOC.BIN'
DO 600 II = 1, NTER
READ (NAUX) COPER,(IOPER(Q,A),IOPER(Q,B),Q=1,NQ)
IF (ABS (COPER) .LT. TOL) CYCLE
!
!-----------------------------------------------------------------------
! Calculate total power of operators in term = M.
! Encode operator into set [POWER = M].(COEF,CODE1,CODE2):
!-----------------------------------------------------------------------
M = 0
DO 200 Q = 1, NQ
M = M + IOPER(Q,A) + IOPER(Q,B)
!---- INDEX(Q,A) = IOPER(Q,A) + 1
!---- INDEX(Q,B) = IOPER(Q,B) + 1
200 CONTINUE
IF (ICUT .EQ. 0 .AND. M .EQ. MAXTOT) CYCLE
KK = M + 1 ! INTEGER*8 KK; KK = NBASE(M) = M + 1
KK = KK ** NQ
CODE1 = 1
CODE2 = 1
DO 250 Q = 1, NQ
KK = KK / (M + 1) ! KK = NBASE(M) ** (NQ - Q)
!---- CODE1 = CODE1 + KK * (INDEX(Q,A) - 1)
!---- CODE2 = CODE2 + KK * (INDEX(Q,B) - 1)
CODE1 = CODE1 + KK * IOPER(Q,A)
CODE2 = CODE2 + KK * IOPER(Q,B)
250 CONTINUE
!
!-----------------------------------------------------------------------
! Check if the new operator already exists in the buffer
!-----------------------------------------------------------------------
IF (LENBUF(M) .GT. 0) THEN
!---- LENBUF(M) may shrink afterwards if trailing zeros occur
LBUF = LENBUF(M)
DO 320 I = LENBUF(M), 1, -1
!---- Check if inspection of trailing zeros is enabled
IF (TAIL(M)) THEN
IF (ABS (PTROPER(M)%COEF(I)) .LT. TOL) THEN
LBUF = I - 1 ! Cut trailing zeros
CYCLE
ELSE
TAIL(M) = .FALSE. ! Disable further check
ENDIF
ENDIF
IF (CODE1 .EQ. PTROPER(M)%CODE1(I) .AND. &
& CODE2 .EQ. PTROPER(M)%CODE2(I)) THEN
PTROPER(M)%COEF(I) = PTROPER(M)%COEF(I) + COPER
IF (I .EQ. LENBUF(M)) TAIL(M) = .TRUE.
GO TO 600 ! Global DO-loop over terms
ENDIF
320 CONTINUE
LENBUF(M) = LBUF
TAIL(M) = .FALSE.
ENDIF
!
!-----------------------------------------------------------------------
! Current operator was never encountered, create the new entry
!-----------------------------------------------------------------------
LENBUF(M) = LENBUF(M) + 1
I = LENBUF(M)
PTROPER(M)%COEF(I) = COPER
PTROPER(M)%CODE1(I) = CODE1
PTROPER(M)%CODE2(I) = CODE2
TAIL(M) = .FALSE.
!
!-----------------------------------------------------------------------
! Check if the number of terms reached the size of current block.
! Then save it to the file, allocate extended size, upload to RAM
!-----------------------------------------------------------------------
IF (LENBUF(M) .EQ. MEMBLK(M)) THEN
OPEN (UNIT = NSCR, FILE = FILE_BUF, DISPOSE = 'DELETE',&
& BUFFERED = 'YES', FORM = 'UNFORMATTED')
L = 0
DO 510 I = 1, LENBUF(M)
IF (ABS (PTROPER(M)%COEF(I)) .LT. TOL) CYCLE
L = L + 1
WRITE (NSCR) PTROPER(M)%COEF(I), &
& PTROPER(M)%CODE1(I),PTROPER(M)%CODE2(I)
510 CONTINUE
LENBUF(M) = L
REWIND (NSCR)
DEALLOCATE (PTROPER(M)%COEF)
DEALLOCATE (PTROPER(M)%CODE1,PTROPER(M)%CODE2)
MEMBLK(M) = LENBUF(M) + MEMEXT
MAXMEM(M) = MAX0 (MEMBLK(M),MAXMEM(M))
ALLOCATE (PTROPER(M)%COEF (MEMBLK(M)), STAT = IERR)
IF (IERR .NE. 0) GO TO 920
ALLOCATE (PTROPER(M)%CODE1(MEMBLK(M)), STAT = IERR)
IF (IERR .NE. 0) GO TO 920
ALLOCATE (PTROPER(M)%CODE2(MEMBLK(M)), STAT = IERR)
IF (IERR .NE. 0) GO TO 920
DO 520 I = 1, LENBUF(M)
READ (NSCR) PTROPER(M)%COEF(I), &
& PTROPER(M)%CODE1(I),PTROPER(M)%CODE2(I)
520 CONTINUE
CLOSE (NSCR)
NALLOC(M) = NALLOC(M) + 1
ENDIF
600 CONTINUE
CLOSE (NAUX)
!
!-----------------------------------------------------------------------
! Arrange terms in descending order using quick-sort algorithm.
!-----------------------------------------------------------------------
FILE_SRT = PATH_BIN(1:LENSTR(PATH_BIN,64)) // 'SORTING.BIN'
OPEN (UNIT = NSCR, FILE = FILE_SRT, DISPOSE = 'DELETE', &
& BUFFERED = 'YES', FORM = 'UNFORMATTED')
DO 640 M = MAXTOT, 0, -1
LBUF = LENBUF(M)
IF (LBUF .LT. 1) CYCLE
ALLOCATE (ARRAY(LBUF), STAT = IERR)
IF (IERR .NE. 0) GO TO 920
ALLOCATE (INDX(LBUF), STAT = IERR)
IF (IERR .NE. 0) GO TO 920
DO 610 I = 1, LBUF
ARRAY(I) = ABS (PTROPER(M)%COEF(I))
610 CONTINUE
CALL RQSORT (LBUF,ARRAY,INDX)
!---- Also delete terms |COEF| < TOL and update LENBUF
REWIND (NSCR)
DO 620 I = LBUF, 1, -1
K = INDX(I)
IF (ABS (PTROPER(M)%COEF(K)) .LT. TOL) THEN
LENBUF(M) = LBUF - I
EXIT
ENDIF
WRITE (NSCR) PTROPER(M)%COEF(K), &
& PTROPER(M)%CODE1(K),PTROPER(M)%CODE2(K)
620 CONTINUE
REWIND (NSCR)
DO 630 I = 1, LENBUF(M)
READ (NSCR) PTROPER(M)%COEF(I), &
& PTROPER(M)%CODE1(I),PTROPER(M)%CODE2(I)
630 CONTINUE
DEALLOCATE (ARRAY,INDX)
640 CONTINUE
CLOSE (NSCR)
!
!-----------------------------------------------------------------------
! Decode OFFSETs into operator powers: a^m b^n
!-----------------------------------------------------------------------
OPEN (UNIT = NAUX, FILE = FILE_SH, DISPOSE = 'SAVE', &
& ACTION = 'WRITE', BUFFERED = 'YES', FORM = 'UNFORMATTED')
ICOUNT = 0
NPOWF = 0
CMAX = ZERO
DO 740 M = MAXTOT, 0, -1 ! Write in descending order of powers
IF (NPOWI(M) .EQ. 0) CYCLE
NBASE(M) = M + 1 ! Not needed, already assigned
DO 730 I = 1, LENBUF(M)
COPER = PTROPER(M)%COEF(I)
IF (ABS (COPER) .LT. TOL) CYCLE
CMAX = QMAX1 (CMAX, ABS (COPER))
DO 720 L = 1, 2
IF (L .EQ. 1) LL = PTROPER(M)%CODE1(I)
IF (L .EQ. 2) LL = PTROPER(M)%CODE2(I)
KK = M + 1 ! KK = NBASE(M)
KK = KK ** NQ
DO 710 Q = 1, NQ
IF (Q .LT. NQ) THEN
KK = KK / (M + 1) ! = KK ** (NQ - Q)
MODLK = MOD(LL, KK)
IF (MODLK .EQ. 0) THEN
INDEX(Q,L) = LL / KK
ELSE
INDEX(Q,L) = (LL - MODLK) / KK + 1
ENDIF
LL = LL - KK * (INDEX(Q,L) - 1)
ELSE
INDEX(Q,L) = LL
ENDIF
IOPER(Q,L) = INDEX(Q,L) - 1
710 CONTINUE
720 CONTINUE
WRITE (NAUX) COPER,(IOPER(Q,A),IOPER(Q,B),Q=1,NQ)
ICOUNT = ICOUNT + 1
NPOWF(M) = NPOWF(M) + 1
730 CONTINUE
740 CONTINUE
CLOSE (NAUX)
!---- Delete the original file if it is different from the output
! NOTE: Collection of garbage is performed later in SIBERT ().
! IF (FILE_IN .NE. FILE_SH) THEN
! OPEN (UNIT = NAUX, FILE = FILE_IN, STATUS = 'OLD',
! & DISPOSE = 'DELETE', FORM = 'UNFORMATTED')
! CLOSE (NAUX)
! ENDIF
NINIT = NTER
NTER = ICOUNT
PART = ZERO
IF (NINIT .GT. 0) PART = 100.0D0 * REAL (NTER) / REAL (NINIT)
IF (IOUT .GT. 0) WRITE (NOUT,1300) NINIT,NTER,PART,CMAX
!
!-----------------------------------------------------------------------
! Release allocated memory
!-----------------------------------------------------------------------
DO 800 M = 0, MAXTOT
IF (NPOWI(M) .EQ. 0) CYCLE
DEALLOCATE (PTROPER(M)%COEF)
DEALLOCATE (PTROPER(M)%CODE1,PTROPER(M)%CODE2)
800 CONTINUE
DEALLOCATE (PTROPER)
IF (IOUT .GT. 0) THEN
WRITE (NOUT,1200)
DO 810 M = 0, MAXTOT
IF (NPOWI(M) .EQ. 0) CYCLE
PART = 100.0D0 * (ONE - REAL (NPOWF(M)) / REAL (NPOWI(M)))
WRITE (NOUT,1210) M,NPOWI(M),NPOWF(M),PART,NALLOC(M),MAXMEM(M)
810 CONTINUE
WRITE (NOUT,1220)
CALL WATCH1 (ISPLIT,IThour,ITmin,ITsec,IT100th)
WRITE (NOUT,2000) IThour,ITmin,ITsec,IT100th
2000 FORMAT ('[COLLECT] Time Elapsed: ',I2,':',I2.2,':',I2.2,'.',I2.2)
ENDIF
900 CONTINUE ! Successful return
IERR = 0
RETURN
910 CONTINUE ! Encoding overflow
IERR = 1
WRITE (*,*) 'ENCODING OVERFLOW'
GO TO 990
920 CONTINUE ! Insufficient Memory
IERR = 2
WRITE (*,*) 'INSUFFICIENT MEMORY'
NTER = 0
GO TO 990
930 CONTINUE ! File I/O Error
IERR = 3
WRITE (*,*) 'READ ERROR / END OF FILE'
990 STOP
RETURN
1000 FORMAT (/'[COLLECT]'/'Collect Terms of Operator Polynomial:')
1010 FORMAT ('The Number of Terms in Polynomial =',I12/ &
& 'Maximum Total Power of Operators in a Term = ',I2, &
& ', Max(NQ) =',I3)
1020 FORMAT ('Option to ignore terms with max power is set up', &
& ', Max(NQ) =',I3)
1100 FORMAT (62('-')/'Power Cases Terms Fraction', &
& ' Max.Index = N(case)^NQ'/62('-'))
1110 FORMAT (I4,I10,I12,F10.2,' %',I24)
1120 FORMAT (62('-'))
1130 FORMAT ('Maximum INTEGER*8 Value = ',ES12.4,I24)
1140 FORMAT ('Maximum INTEGER*4 Value = ',ES12.4,I24)
1200 FORMAT ('Summary Table after Collection of Terms:'/78('-')/&
& 'Power Original Resulting Reduction', &
& ' Realloc. Max. Buffer'/78('-'))
1210 FORMAT (I4,I16,I16,F10.2,' %',I14,I16)
1220 FORMAT (78('-'))
1300 FORMAT ('Original Number of Terms Before Collecting = ',I8/&
& 'Resulting Number of Terms After Collecting = ',I8,&
& ' (',F5.1,'%)'/'Maximum Scalar Coefficient Found = ',ES18.8)
!9000 FORMAT ('Stage 3 of 4: Find out Maximum Power and Histogram ...'\)
!9100 FORMAT ('Stage 4 of 4: Collecting Terms with Same Operators ...'\)
END
!-----------------------------------------------------------------------
! Subroutine: 'COLLECT4' FIRST: 17 Jun 2011 LAST EDIT: 04 Apr 2018
!
! PURPOSE:
! Collect terms in long operator polynomials arising in commutators.
! The terms with identical operators are collected with removal of
! zero coefficients. The output operator is saved in file FILE_SH.
!
! FEATURES (For more history, look at COLLECT2 in Commute(0).for):
! 1. Double encoding (CODE1,CODE2) is used for a/b vectors.
! If overflow appears, longer encoding (CODE1-4) is possible.
! 2. Two passes are used, scanning terms first time with restricted
! depth, second time doing full scan.
! 3. The polynomial is sorted to NQ parts, in each part 'Location N'
! is the first one in the list of a(i)^n(i)*b(i)^m(i) terms, for
! which the sum MAXS = n(i) + m(i) is maximum, and among them,
! the power MAXT = n(i) is also maximum.
! 4. Version 3 is different from the Version 2 by the method of
! working with terms, splitted into NQ Locations. In Version 2,
! after the first pass, the optimized parts of the polynomial are
! kept in memory. This is inefficient for the bigger molecules.
! In this Version 3, each Location is preliminary saved in files
! 'COLLECTxx.BIN' and treated in a loop.
! 5. Working with the tail at the Stage 1 is optimized. After adding
! a new term, the buffer is scanned from the end for zeros.
! 6. During Stage 2, when the term is summed with the previous one,
! the coefficient is checked for zero. If it becomes zero, the
! last term is moved to replace the hole and the LENGTH shrinks.
! 7. This Version is MPI-ready, as each file can be treated
! separately.
! 8. Double encoding of long products of PRO[i]a(i)^m(i)*b(i)^n(i).
! This feature is specific for COLLECT4.
!
! DEVELOPMENT:
! 1. Secondary splitting of terms sorted into NQ groups is possible.
! Among a group sorted on i-th Q, split terms according to the first
! b(j)^m(j)-operator with maximum power m(j). These subgroups can be
! kept in memory, look at COLLECT2 ().
! 2. The secondary optimization is possible. Need to print each
! location and analyze the structure of operator a/b powers.
! Must look for structure of powers of b(i)^m(i).
! IDEA: The first operator b(i)^m(i) with maximum m(i).
!
! THEORY:
! 1. Total powers in individual terms are all odd (0,2,4,6,8,...) or
! all even (1,3,5,7,...), as empirically found.
!
! 2. It was empirically found, that terms with maximum total power
! (MAXTOT) cancel each other. In this version the flag ICUT is used,
! it enables or disables automatic removal of such terms.
!
! 3. Instead of comparing two pair of products,
! Product1 = PRO[i=1,NQ] a(i)^n1(i) * b(i)^m1(i), and
! Product2 = PRO[i=1,NQ] a(i)^n2(i) * b(i)^m2(i),
! which would take longer time, each product is encoded in two long
! integer number (8 bytes) and for comparison of two products it is
! necessary to compare just pairs of integers.
! Each product is naturally represented as two lists,
! {n(1),n(2),..,n(NQ)} and {m(1),m(2),..,m(NQ)}
! which are converted to long integer numbers INTEGER*8.
!
! 4. The algorithm of encoding:
!
! MAXPOW = MAX (MAX (n(i)), MAX(m(i))),
!
! K = MAXPOW + 1 is used as the base of the numerical system.
!
! Each list of numbers {n(1),n(2),..,n(NQ)} and {m(1),m(2),..,m(NQ)}
! is converted to an INT*8 positive integer number using:
!
! Code1 = K^(NQ-1)*n(1) + K^(NQ-2)*n(2) + ... + n(NQ)
! Code2 = K^(NQ-1)*m(1) + K^(NQ-2)*m(2) + ... + m(NQ)
!
! Numbers Code1,2 will be converted back to lists at the end.
!
! 5. The algorithm of decoding:
! Vector {n(1),n(2),...,n(M)} was encoded to LongN number using
! K as the base of the numbering system, where K = MAX(n(i)) + 1.
!
! Inverse decoding (with special treatment of case for mod() = 0):
!
! (0) L(0) = Code(1/2);
!
! (1) n(1) = (L(0) - mod(L(0),K^(M-1))) / K^(M-1);
! L(1) = L(0) - K^(M-1) * n(1);
!
! (k) n(k) = (L(k-1) - mod(L(k-1),K^(M-k))) / K^(M-k);
! L(k) = L(k-1) - K^(M-k) * n(k);
!
! (M) n(M) = (L(M-1) - mod(L(M-1),1));
! n(M) = L(M-1) - n(k).
!
! 6. For simplicity, instead of using K = MAXPOW + 1 as the base of
! the numerical system, K = MAXTOT + 1 is used, to guarantee that
! an individual power of an operator will not be bigger than K.
! Although, actual maximum power of an operator can be smaller.
! Here MAXTOT is used instead if MAXPOW for compatibility.
!
! 7. It was empirically found out that:
! (a) MAXPOW = MAXTOT - 1 or - 2;
! (b) maximum power is equal to total power except the terms where
! total power = MAXTOT.
!
! 8. In Version 3, the value MAXTOT is used for two purposes:
! (a) automatic removal of terms with total power = MAXTOT;
! (b) as the base for numbering system - for encoding.
!
! 9. Conclusion: the basis of numbering system NBASE must be equal
! to the maximum power of an individual operator a(i)/b(i) for
! those terms that do not vanish, i.e. to include powers MAXTOT - 2.
! RESULT: The sole basis of the numbering system can be chosen as
! NBASE = (MAXTOT - 2) + 1 = MAXTOT - 1.
! In this case MAXTOT terms must be necessarily cut.
!
! REFERENCE(S):
! [1] See My Journal 15/01/2010, pages 25,68.
!
! CALLED: COMMUTE (Sibert.for), ADDPOLF (ToolsCPT.for),
! H_VIBROT (Commute.for).
!
! INPUT:
! NTER -- The number of primitive terms in operator polynomial;
! NQ -- The number of vibrational degrees of freedom;
! FILE_IN-- CHARACTER*80 file name for the original operator;
! FILE_SH-- CHARACTER*80 file name for the resulting operator;
! TOL -- Tolerance parameter determining the accuracy of
! calculations. Terms with |COEF| < TOL are ignored;
! MAXTOT -- Maximum total power of all operators in an operator
! product;
! MAXPOW -- Maximum power of an individual operator in all products;
! NPOWI -- Array holding the number of terms for all total powers;
! ICUT -- =1, collect all terms; =0, ignore powers = MAXTOT;
! IOUT -- Flag for console output disable/enable 0/1.
!
! OUTPUT:
! NTER -- The number of primitive terms in Operator Polynomial
! after Simplification (summation of equivalent terms);
! <FILE_IN> The file containing the original operator is deleted
! if it does not coincide with the output file;
! <FILE_SH> File containing the resulting operator is saved.
!
! FILE I/O:
! Logical Unit Numbers Used: NAUX, NSCR.
!
! VARIABLES:
! MEMBLK -- ...
! LENBUF --
! NALLOC --
! MAXMEM --
! ...
!
! NOTE(S):
! 1. This is a modified version of COLLECT3.
! 2. Versions 2,3 of this routine is saved in Collect(0).for.
! 3. 27/08/2011 -- If input/output file names coincide, an error
! occurs, the resulting file is deleted. Now file names are
! compared, but deletion may be excluded and done by a separate
! small routine if necessary. This routine is used in various
! cases like V-R_Hamiltonian and commutators, where post-actions
! are different.
!-----------------------------------------------------------------------
SUBROUTINE COLLECT4 (NQ,NTER,FILE_IN,FILE_SH,TOL, &
& MAXTOT,MAXPOW,NPOWI,IERR,ICUT,IOUT)
IMPLICIT REAL*16 (A-H,O-Z), INTEGER*4 (I-N)
COMMON /IOLUN/ LOUT,NLOG,NINP,NOUT,NAUX,NSCR
COMMON /NUMBER/ PI,RADIAN,ZERO,HALF,ONE,TWO,THREE,FOUR,TEN(-16:16)
COMMON /PATHS/ PATH_EXE,PATH_PRO,PATH_OUT,PATH_BIN
CHARACTER*64 PATH_EXE,PATH_PRO,PATH_OUT,PATH_BIN
CHARACTER*80 FILE_IN,FILE_SH
DIMENSION NPOWI(0:12) ! Suffix I -- Initial # of terms per power
CHARACTER*80 FILE_BUF,FILE(NQ),FILENAME
DIMENSION MEMBLK(NQ),LENBUF(NQ),NALLOC(NQ),MAXMEM(NQ)
DIMENSION MASK(NQ),NLOC(NQ),NLOC1(NQ),NLOC2(NQ)
DIMENSION CBUF(100)
INTEGER*1 IOPER(NQ,2),IBUF(100,NQ,2)
INTEGER*1 jOPER(NQ,2)
TYPE :: PTR_TERM
REAL*16, DIMENSION(:), POINTER :: COEF
INTEGER*8, DIMENSION(:), POINTER :: CODE1,CODE2,CODE3,CODE4
ENDTYPE PTR_TERM
TYPE (PTR_TERM), ALLOCATABLE :: PTROPER(:)
TYPE (PTR_TERM) PTROPER1
INTEGER*4 A,B,Q,S
INTEGER*8 MAXVAL_8,MAXNUM,KK,LL,MODLK,KSCALE1(NQ),KSCALE2(NQ)
INTEGER*8 CODE1,CODE2,CODE3,CODE4
INTEGER*8 LSPLIT,LSPLIT1,LSPLIT2
CHARACTER*16 TIMETEXT
LOGICAL OVERFLOW
CHARACTER*12 NUMSTR ! FUNCTION
!-----------------------------------------------------------------------
DATA A,B /1,2/
DATA LMAX /30/ ! The max number in operator terms to print
DATA LBUF /100/ ! I/O Buffer for performance optimization
DATA MDEPTH /200/ ! The depth of look-up list for Stage 1
CALL LWATCH (LSPLIT)
WRITE (NOUT,1000)
IF (IOUT .GT. 0) WRITE (*,9000) NUMSTR(NTER)
1000 FORMAT (/'[COLLECT4] -- Collect Terms of Operator Polynomial:')
!
!-----------------------------------------------------------------------
! The value of maximum possible NQ is defined via: MAX(INT*8) = 2^63
! K^NQ < MAX = 2^63; NQ*LOG(K) < 63*LOG(2); NQ < 63*LOG(2)/LOG(K).
!-----------------------------------------------------------------------
WRITE (NOUT,1010) NTER
IF (ICUT .EQ. 1) THEN
NBASE = MAXTOT + 1
WRITE (NOUT,1020)
ELSE
NBASE = MAXTOT - 1
! >>> WRITE (NOUT,1025)
ENDIF
MAXNQ = INT (63.0D0 * LOG (2.0D0) / LOG (REAL (NBASE)))
MAXVAL_4 = 2; MAXVAL_4 = -((-MAXVAL_4) ** 31 + 1)
MAXVAL_8 = 2; MAXVAL_8 = -((-MAXVAL_8) ** 63 + 1)
WRITE (NOUT,1030) MAXTOT,NQ,NBASE,MAXNQ
WRITE (NOUT,1040) MAXVAL_4,REAL (MAXVAL_4)
WRITE (NOUT,1050) MAXVAL_8,REAL (MAXVAL_8)
1010 FORMAT (/'The Original Number of Terms in the Polynomial =',I14)
1020 FORMAT ('All terms will be collected, including ones with the', &
& 'maximum total power.')
1030 FORMAT (/'Maximum total power in operator products = ',I4/ &
& 'The number of degrees of freedom = ',I4/ &
& 'The chosen base of numbering system = ',I4/ &
& 'Maximum possible degrees of freedom = ',I4)
1040 FORMAT (/'Maximum INTEGER*4 Value = ',I24,ES12.4)
1050 FORMAT ( 'Maximum INTEGER*8 Value = ',I24,ES12.4)
!
!-----------------------------------------------------------------------
! Split into two ranges: 1 ... NQ1, LQ2 ... NQ (in total = NQ2)
!-----------------------------------------------------------------------
DMAXNUM1 = REAL (NBASE) ** NQ
DMAXNUM2 = REAL (NBASE) ** ((NQ + 1) / 2)
IF (DMAXNUM1 .LT. REAL (MAXVAL_8)) THEN
LONG = 1
NQ1 = NQ
NQ2 = 0
ELSE IF (DMAXNUM2 .LT. REAL (MAXVAL_8)) THEN
LONG = 2
NQ1 = (NQ + 1) / 2
NQ2 = NQ - NQ1
LQ1 = 1
LQ2 = NQ1 + 1
WRITE (NOUT,1060) NQ1,LQ2,NQ
ELSE
LONG = 0
WRITE (NOUT,1070) DMAXNUM2
GO TO 910
ENDIF
!==== Test using smaller molecule:
! LONG = 2
! WRITE (NOUT,1060) NQ1,LQ2,NQ
!====
!---- NQ1 is used as a default value of NQ
DMAXNUM1 = REAL (NBASE) ** NQ1 ! The value must be representable
MAXNUM = NBASE ! INTEGER*8 MAXNUM
MAXNUM = MAXNUM ** NQ1 ! In two steps to avoid overflow