-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModule_Numerical_Integration.f90
1537 lines (1241 loc) · 47.2 KB
/
Module_Numerical_Integration.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 constants_NSWC
! Contains the NSWC functions IPMPAR, SPMPAR, DPMPAR, EPSLN, DEPSLN,
! EXPARG & DXPARG
!-----------------------------------------------------------------------
! WRITTEN using F90 intrinsics by
! Alan Miller
! amiller @ bigpond.net.au
! Latest revision - 1 February 1997
!-----------------------------------------------------------------------
IMPLICIT NONE
INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(15, 60)
CONTAINS
FUNCTION ipmpar (i) RESULT(fn_val)
!-----------------------------------------------------------------------
! IPMPAR PROVIDES THE INTEGER MACHINE CONSTANTS FOR THE COMPUTER
! THAT IS USED. IT IS ASSUMED THAT THE ARGUMENT I IS AN INTEGER
! HAVING ONE OF THE VALUES 1-10. IPMPAR(I) HAS THE VALUE ...
! INTEGERS.
! ASSUME INTEGERS ARE REPRESENTED IN THE N-DIGIT, BASE-A FORM
! SIGN ( X(N-1)*A**(N-1) + ... + X(1)*A + X(0) )
! WHERE 0 .LE. X(I) .LT. A FOR I=0,...,N-1.
! IPMPAR(1) = A, THE BASE (radix).
! IPMPAR(2) = N, THE NUMBER OF BASE-A DIGITS (digits).
! IPMPAR(3) = A**N - 1, THE LARGEST MAGNITUDE (huge).
! FLOATING-POINT NUMBERS.
! IT IS ASSUMED THAT THE SINGLE AND DOUBLE PRECISION FLOATING
! POINT ARITHMETICS HAVE THE SAME BASE, SAY B, AND THAT THE
! NONZERO NUMBERS ARE REPRESENTED IN THE FORM
! SIGN (B**E) * (X(1)/B + ... + X(M)/B**M)
! WHERE X(I) = 0,1,...,B-1 FOR I=1,...,M,
! X(1) .GE. 1, AND EMIN .LE. E .LE. EMAX.
! IPMPAR(4) = B, THE BASE.
! SINGLE-PRECISION
! IPMPAR(5) = M, THE NUMBER OF BASE-B DIGITS.
! IPMPAR(6) = EMIN, THE SMALLEST EXPONENT E.
! IPMPAR(7) = EMAX, THE LARGEST EXPONENT E.
! DOUBLE-PRECISION
! IPMPAR(8) = M, THE NUMBER OF BASE-B DIGITS.
! IPMPAR(9) = EMIN, THE SMALLEST EXPONENT E.
! IPMPAR(10) = EMAX, THE LARGEST EXPONENT E.
!-----------------------------------------------------------------------
IMPLICIT NONE
INTEGER, INTENT(IN) :: i
INTEGER :: fn_val
SELECT CASE(i)
CASE( 1)
fn_val = RADIX(i)
CASE( 2)
fn_val = DIGITS(i)
CASE( 3)
fn_val = HUGE(i)
CASE( 4)
fn_val = RADIX(1.0)
CASE( 5)
fn_val = DIGITS(1.0)
CASE( 6)
fn_val = MINEXPONENT(1.0)
CASE( 7)
fn_val = MAXEXPONENT(1.0)
CASE( 8)
fn_val = DIGITS(1.0D0)
CASE( 9)
fn_val = MINEXPONENT(1.0D0)
CASE(10)
fn_val = MAXEXPONENT(1.0D0)
CASE DEFAULT
RETURN
END SELECT
RETURN
END FUNCTION ipmpar
FUNCTION spmpar (i) RESULT(fn_val)
!-----------------------------------------------------------------------
! SPMPAR PROVIDES THE SINGLE PRECISION MACHINE CONSTANTS FOR
! THE COMPUTER BEING USED. IT IS ASSUMED THAT THE ARGUMENT
! I IS AN INTEGER HAVING ONE OF THE VALUES 1, 2, OR 3. IF THE
! SINGLE PRECISION ARITHMETIC BEING USED HAS M BASE B DIGITS AND
! ITS SMALLEST AND LARGEST EXPONENTS ARE EMIN AND EMAX, THEN
! SPMPAR(1) = B**(1 - M), THE MACHINE PRECISION,
! SPMPAR(2) = B**(EMIN - 1), THE SMALLEST MAGNITUDE,
! SPMPAR(3) = B**EMAX*(1 - B**(-M)), THE LARGEST MAGNITUDE.
!-----------------------------------------------------------------------
IMPLICIT NONE
INTEGER, INTENT(IN) :: i
REAL :: fn_val
! Local variable
REAL :: one = 1.0
SELECT CASE (i)
CASE (1)
fn_val = EPSILON(one)
CASE (2)
fn_val = TINY(one)
CASE (3)
fn_val = HUGE(one)
END SELECT
RETURN
END FUNCTION spmpar
FUNCTION dpmpar (i) RESULT(fn_val)
!-----------------------------------------------------------------------
! DPMPAR PROVIDES THE DOUBLE PRECISION MACHINE CONSTANTS FOR
! THE COMPUTER BEING USED. IT IS ASSUMED THAT THE ARGUMENT
! I IS AN INTEGER HAVING ONE OF THE VALUES 1, 2, OR 3. IF THE
! DOUBLE PRECISION ARITHMETIC BEING USED HAS M BASE B DIGITS AND
! ITS SMALLEST AND LARGEST EXPONENTS ARE EMIN AND EMAX, THEN
! DPMPAR(1) = B**(1 - M), THE MACHINE PRECISION,
! DPMPAR(2) = B**(EMIN - 1), THE SMALLEST MAGNITUDE,
! DPMPAR(3) = B**EMAX*(1 - B**(-M)), THE LARGEST MAGNITUDE.
!-----------------------------------------------------------------------
IMPLICIT NONE
INTEGER, INTENT(IN) :: i
REAL (dp) :: fn_val
! Local variable
REAL (dp) :: one = 1._dp
SELECT CASE (i)
CASE (1)
fn_val = EPSILON(one)
CASE (2)
fn_val = TINY(one)
CASE (3)
fn_val = HUGE(one)
END SELECT
RETURN
END FUNCTION dpmpar
FUNCTION epsln () RESULT(fn_val)
!--------------------------------------------------------------------
! THE EVALUATION OF LN(EPS) WHERE EPS IS THE SMALLEST NUMBER
! SUCH THAT 1.0 + EPS .GT. 1.0 . L IS A DUMMY ARGUMENT.
!--------------------------------------------------------------------
IMPLICIT NONE
REAL :: fn_val
! Local variable
REAL :: one = 1.0
fn_val = LOG( EPSILON(one) )
RETURN
END FUNCTION epsln
FUNCTION exparg (l) RESULT(fn_val)
!--------------------------------------------------------------------
! IF L = 0 THEN EXPARG(L) = THE LARGEST POSITIVE W FOR WHICH
! EXP(W) CAN BE COMPUTED.
!
! IF L IS NONZERO THEN EXPARG(L) = THE LARGEST NEGATIVE W FOR
! WHICH THE COMPUTED VALUE OF EXP(W) IS NONZERO.
!
! NOTE... ONLY AN APPROXIMATE VALUE FOR EXPARG(L) IS NEEDED.
!--------------------------------------------------------------------
IMPLICIT NONE
INTEGER, INTENT(IN) :: l
REAL :: fn_val
! Local variable
REAL :: one = 1.0
IF (l == 0) THEN
fn_val = LOG( HUGE(one) )
ELSE
fn_val = LOG( TINY(one) )
END IF
RETURN
END FUNCTION exparg
FUNCTION depsln () RESULT(fn_val)
!--------------------------------------------------------------------
! THE EVALUATION OF LN(EPS) WHERE EPS IS THE SMALLEST NUMBER
! SUCH THAT 1.D0 + EPS .GT. 1.D0 . L IS A DUMMY ARGUMENT.
!--------------------------------------------------------------------
IMPLICIT NONE
REAL (dp) :: fn_val
! Local variable
REAL (dp) :: one = 1._dp
fn_val = LOG( EPSILON(one) )
RETURN
END FUNCTION depsln
FUNCTION dxparg (l) RESULT(fn_val)
!--------------------------------------------------------------------
! IF L = 0 THEN DXPARG(L) = THE LARGEST POSITIVE W FOR WHICH
! DEXP(W) CAN BE COMPUTED.
!
! IF L IS NONZERO THEN DXPARG(L) = THE LARGEST NEGATIVE W FOR
! WHICH THE COMPUTED VALUE OF DEXP(W) IS NONZERO.
!
! NOTE... ONLY AN APPROXIMATE VALUE FOR DXPARG(L) IS NEEDED.
!--------------------------------------------------------------------
IMPLICIT NONE
INTEGER, INTENT(IN) :: l
REAL (dp) :: fn_val
! Local variable
REAL (dp) :: one = 1._dp
IF (l == 0) THEN
fn_val = LOG( HUGE(one) )
ELSE
fn_val = LOG( TINY(one) )
END IF
RETURN
END FUNCTION dxparg
END MODULE constants_NSWC
MODULE MODULE_INTEGRATION
! Module for adaptive quadrature.
! Adapted from TOMS algorithm 691.
! This version uses the ELF90 subset of Fortran 90.
! Conversion by Alan Miller
! amiller @ bigpond.net.au
CONTAINS
SUBROUTINE qpsrt(limit, last, maxerr, ermax, elist, iord, nrmax)
! ..................................................................
! 1. QPSRT
! ORDERING ROUTINE
! STANDARD FORTRAN SUBROUTINE
! REAL VERSION
! 2. PURPOSE
! THIS ROUTINE MAINTAINS THE DESCENDING ORDERING IN THE LIST OF THE
! LOCAL ERROR ESTIMATES RESULTING FROM THE INTERVAL SUBDIVISION
! PROCESS. AT EACH CALL TWO ERROR ESTIMATES ARE INSERTED USING THE
! SEQUENTIAL SEARCH METHOD, TOP-DOWN FOR THE LARGEST ERROR ESTIMATE
! AND BOTTOM-UP FOR THE SMALLEST ERROR ESTIMATE.
! 3. CALLING SEQUENCE
! CALL QPSRT(LIMIT, LAST, MAXERR, ERMAX, ELIST, IORD, NRMAX)
! PARAMETERS (MEANING AT OUTPUT)
! LIMIT - INTEGER
! MAXIMUM NUMBER OF ERROR ESTIMATES THE LIST CAN CONTAIN
! LAST - INTEGER
! NUMBER OF ERROR ESTIMATES CURRENTLY IN THE LIST
! MAXERR - INTEGER
! MAXERR POINTS TO THE NRMAX-TH LARGEST ERROR ESTIMATE
! CURRENTLY IN THE LIST
! ERMAX - REAL
! NRMAX-TH LARGEST ERROR ESTIMATE
! ERMAX = ELIST(MAXERR)
! ELIST - REAL
! VECTOR OF DIMENSION LAST CONTAINING THE ERROR ESTIMATES
! IORD - INTEGER
! VECTOR OF DIMENSION LAST, THE FIRST K ELEMENTS OF
! WHICH CONTAIN POINTERS TO THE ERROR ESTIMATES,
! SUCH THAT ELIST(IORD(1)), ... , ELIST(IORD(K))
! FORM A DECREASING SEQUENCE, WITH K = LAST IF
! LAST <= (LIMIT/2+2), AND K = LIMIT+1-LAST OTHERWISE
! NRMAX - INTEGER
! MAXERR = IORD(NRMAX)
! 4. NO SUBROUTINES OR FUNCTIONS NEEDED
! ..................................................................
USE constants_NSWC
IMPLICIT NONE
INTEGER, INTENT(IN) :: limit, last
REAL (dp), DIMENSION(:), INTENT(IN) :: elist
INTEGER, INTENT(IN OUT) :: nrmax
INTEGER, DIMENSION(:), INTENT(OUT) :: iord
INTEGER, INTENT(OUT) :: maxerr
REAL (dp), INTENT(OUT) :: ermax
REAL (dp) :: errmax, errmin
INTEGER :: i, ibeg, ido, isucc, j, jbnd, jupbn, k
! CHECK WHETHER THE LIST CONTAINS MORE THAN TWO ERROR ESTIMATES.
!***FIRST EXECUTABLE STATEMENT QPSRT
IF(last > 2) GO TO 10
iord(1) = 1
iord(2) = 2
GO TO 90
! THIS PART OF THE ROUTINE IS ONLY EXECUTED IF,
! DUE TO A DIFFICULT INTEGRAND, SUBDIVISION INCREASED
! THE ERROR ESTIMATE. IN THE NORMAL CASE THE INSERT PROCEDURE
! SHOULD START AFTER THE NRMAX-TH LARGEST ERROR ESTIMATE.
10 errmax = elist(maxerr)
IF(nrmax == 1) GO TO 30
ido = nrmax-1
DO i = 1, ido
isucc = iord(nrmax-1)
! ***JUMP OUT OF DO-LOOP
IF(errmax <= elist(isucc)) EXIT
iord(nrmax) = isucc
nrmax = nrmax-1
END DO
! COMPUTE THE NUMBER OF ELEMENTS IN THE LIST TO BE MAINTAINED
! IN DESCENDING ORDER. THIS NUMBER DEPENDS ON THE NUMBER OF
! SUBDIVISIONS STILL ALLOWED.
30 jupbn = last
IF(last > (limit/2+2)) jupbn = limit+3-last
errmin = elist(last)
! INSERT ERRMAX BY TRAVERSING THE LIST TOP-DOWN,
! STARTING COMPARISON FROM THE ELEMENT ELIST(IORD(NRMAX+1)).
jbnd = jupbn-1
ibeg = nrmax+1
DO i=ibeg, jbnd
isucc = iord(i)
! ***JUMP OUT OF DO-LOOP
IF(errmax >= elist(isucc)) GO TO 60
iord(i-1) = isucc
END DO
iord(jbnd) = maxerr
iord(jupbn) = last
GO TO 90
! INSERT ERRMIN BY TRAVERSING THE LIST BOTTOM-UP.
60 iord(i-1) = maxerr
k = jbnd
DO j=i, jbnd
isucc = iord(k)
! ***JUMP OUT OF DO-LOOP
IF(errmin < elist(isucc)) GO TO 80
iord(k+1) = isucc
k = k-1
END DO
iord(i) = last
GO TO 90
80 iord(k+1) = last
! SET MAXERR AND ERMAX.
90 maxerr = iord(nrmax)
ermax = elist(maxerr)
RETURN
END SUBROUTINE qpsrt
SUBROUTINE qelg (n, epstab, result, abserr, res3la, nres, epmach, oflow)
!-----------------------------------------------------------------------
! 1. PURPOSE
! THE ROUTINE DETERMINES THE LIMIT OF A GIVEN SEQUENCE OF
! APPROXIMATIONS, BY MEANS OF THE EPSILON ALGORITHM OF P. WYNN.
! AN ESTIMATE OF THE ABSOLUTE ERROR IS ALSO GIVEN.
! THE CONDENSED EPSILON TABLE IS COMPUTED. ONLY THOSE ELEMENTS
! NEEDED FOR THE COMPUTATION OF THE NEXT DIAGONAL ARE PRESERVED.
! 2. PARAMETERS
! N - INTEGER
! EPSTAB(N) CONTAINS THE NEW ELEMENT IN THE
! FIRST COLUMN OF THE EPSILON TABLE.
! EPSTAB - REAL
! VECTOR OF DIMENSION 52 CONTAINING THE ELEMENTS OF THE
! TWO LOWER DIAGONALS OF THE TRIANGULAR EPSILON TABLE.
! THE ELEMENTS ARE NUMBERED STARTING AT THE RIGHT-HAND
! CORNER OF THE TRIANGLE.
! RESULT - REAL
! RESULTING APPROXIMATION TO THE INTEGRAL
! ABSERR - REAL
! ESTIMATE OF THE ABSOLUTE ERROR COMPUTED FROM
! RESULT AND THE 3 PREVIOUS RESULTS
! RES3LA - REAL
! VECTOR OF DIMENSION 3 CONTAINING THE LAST 3 RESULTS
! NRES - INTEGER
! NUMBER OF CALLS TO THE ROUTINE
! (SHOULD BE ZERO AT FIRST CALL)
! EPMACH - REAL
! THE RELATIVE PRECISION OF THE FLOATING ARITHMETIC
! BEING USED.
! OFLOW - REAL
! THE LARGEST POSITIVE MAGNITUDE.
! 3. NO SUBROUTINES OR FUNCTIONS USED
!-----------------------------------------------------------------------
USE constants_NSWC
IMPLICIT NONE
INTEGER, INTENT(IN OUT) :: n, nres
REAL (dp), INTENT(IN) :: epmach, oflow
REAL (dp), INTENT(OUT) :: abserr, result
REAL (dp), DIMENSION(:), INTENT(IN OUT) :: epstab, res3la
!---------------------
! LIST OF MAJOR VARIABLES
! -----------------------
! E0 - THE 4 ELEMENTS ON WHICH THE
! E1 COMPUTATION OF A NEW ELEMENT IN
! E2 THE EPSILON TABLE IS BASED
! E3 E0
! E3 E1 NEW
! E2
! NEWELM - NUMBER OF ELEMENTS TO BE COMPUTED IN THE NEW DIAGONAL
! ERROR - ERROR = ABS(E1-E0)+ABS(E2-E1)+ABS(NEW-E2)
! RESULT - THE ELEMENT IN THE NEW DIAGONAL WITH LEAST VALUE OF ERROR
! LIMEXP IS THE MAXIMUM NUMBER OF ELEMENTS THE EPSILON
! TABLE CAN CONTAIN. IF THIS NUMBER IS REACHED, THE UPPER
! DIAGONAL OF THE EPSILON TABLE IS DELETED.
REAL (dp) :: delta1, delta2, delta3, epsinf, error, err1, err2, err3, e0, &
e1, e1abs, e2, e3, res, ss, tol1, tol2, tol3
INTEGER :: i, ib, ib2, ie, indx, k1, k2, k3, limexp, newelm, num
nres = nres + 1
abserr = oflow
result = epstab(n)
IF (n < 3) GO TO 100
limexp = 50
epstab(n + 2) = epstab(n)
newelm = (n - 1)/2
epstab(n) = oflow
num = n
k1 = n
DO i = 1, newelm
k2 = k1 - 1
k3 = k1 - 2
res = epstab(k1 + 2)
e0 = epstab(k3)
e1 = epstab(k2)
e2 = res
e1abs = ABS(e1)
delta2 = e2 - e1
err2 = ABS(delta2)
tol2 = MAX(ABS(e2),e1abs)*epmach
delta3 = e1 - e0
err3 = ABS(delta3)
tol3 = MAX(e1abs,ABS(e0))*epmach
IF (err2 > tol2 .OR. err3 > tol3) GO TO 10
! IF E0, E1 AND E2 ARE EQUAL TO WITHIN MACHINE
! ACCURACY, CONVERGENCE IS ASSUMED.
! RESULT = E2
! ABSERR = ABS(E1-E0) + ABS(E2-E1)
result = res
abserr = err2 + err3
! ***JUMP OUT OF DO-LOOP
GO TO 100
10 e3 = epstab(k1)
epstab(k1) = e1
delta1 = e1 - e3
err1 = ABS(delta1)
tol1 = MAX(e1abs,ABS(e3))*epmach
! IF TWO ELEMENTS ARE VERY CLOSE TO EACH OTHER, OMIT
! A PART OF THE TABLE BY ADJUSTING THE VALUE OF N
IF (err1 <= tol1 .OR. err2 <= tol2 .OR. err3 <= tol3) GO TO 20
ss = 1.0D0/delta1 + 1.0D0/delta2 - 1.0D0/delta3
epsinf = ABS(ss*e1)
! TEST TO DETECT IRREGULAR BEHAVIOUR IN THE TABLE, AND EVENTUALLY
! OMIT A PART OF THE TABLE ADJUSTING THE VALUE OF N.
IF (epsinf > 0.1D-03) GO TO 30
20 n = i + i - 1
! ***JUMP OUT OF DO-LOOP
GO TO 50
! COMPUTE A NEW ELEMENT AND EVENTUALLY ADJUST THE VALUE OF RESULT.
30 res = e1 + 1.0D0/ss
epstab(k1) = res
k1 = k1 - 2
error = err2 + ABS(res - e2) + err3
IF (error > abserr) CYCLE
abserr = error
result = res
END DO
! SHIFT THE TABLE.
50 IF (n == limexp) n = 2*(limexp/2) - 1
ib = 1
IF ((num/2)*2 == num) ib = 2
ie = newelm + 1
DO i = 1, ie
ib2 = ib + 2
epstab(ib) = epstab(ib2)
ib = ib2
END DO
IF (num == n) GO TO 80
indx = num - n + 1
DO i = 1, n
epstab(i) = epstab(indx)
indx = indx + 1
END DO
80 IF (nres >= 4) GO TO 90
res3la(nres) = result
abserr = oflow
GO TO 100
! COMPUTE ERROR ESTIMATE
90 abserr = ABS(result - res3la(3)) + ABS(result - res3la(2)) + &
ABS(result - res3la(1))
res3la(1) = res3la(2)
res3la(2) = res3la(3)
res3la(3) = result
100 abserr = MAX(abserr, 5.0D0*epmach*ABS(result))
RETURN
END SUBROUTINE qelg
SUBROUTINE qxgs (f, a, b, epsabs, epsrel, result, abserr, ier, limit, last)
! THE ROUTINE CALCULATES AN APPROXIMATION RESULT TO A GIVEN
! DEFINITE INTEGRAL I = INTEGRAL OF F OVER (A,B),
! HOPEFULLY SATISFYING FOLLOWING CLAIM FOR ACCURACY
! ABS(I-RESULT) <= MAX(EPSABS, EPSREL*ABS(I)).
! PARAMETERS
! ON ENTRY
! F - REAL
! FUNCTION SUBPROGRAM DEFINING THE INTEGRAND
! FUNCTION F(X). THE ACTUAL NAME FOR F NEEDS TO BE
! DECLARED E X T E R N A L IN THE DRIVER PROGRAM.
! A - REAL
! LOWER LIMIT OF INTEGRATION
! B - REAL
! UPPER LIMIT OF INTEGRATION
! EPSABS - REAL
! ABSOLUTE ACCURACY REQUESTED
! EPSREL - REAL
! RELATIVE ACCURACY REQUESTED
! ON RETURN
! RESULT - REAL
! APPROXIMATION TO THE INTEGRAL
! ABSERR - REAL
! ESTIMATE OF THE MODULUS OF THE ABSOLUTE ERROR,
! WHICH SHOULD EQUAL OR EXCEED ABS(I-RESULT)
! IER - INTEGER
! IER = 0 NORMAL AND RELIABLE TERMINATION OF THE ROUTINE.
! IT IS ASSUMED THAT THE REQUESTED ACCURACY HAS
! BEEN ACHIEVED.
! IER > 0 ABNORMAL TERMINATION OF THE ROUTINE
! THE ESTIMATES FOR INTEGRAL AND ERROR ARE
! LESS RELIABLE. IT IS ASSUMED THAT THE
! REQUESTED ACCURACY HAS NOT BEEN ACHIEVED.
! ERROR MESSAGES
! IER = 1 MAXIMUM NUMBER OF SUBDIVISIONS ALLOWED HAS BEEN
! ACHIEVED. ONE CAN ALLOW MORE SUB-DIVISIONS BY
! INCREASING THE VALUE OF LIMIT (AND TAKING THE ACCORDING
! DIMENSION ADJUSTMENTS INTO ACCOUNT. HOWEVER, IF THIS
! YIELDS NO IMPROVEMENT IT IS ADVISED TO ANALYZE THE
! INTEGRAND IN ORDER TO DETERMINE THE INTEGRATION
! DIFFICULTIES. IF THE POSITION OF A LOCAL DIFFICULTY
! CAN BE DETERMINED (E.G. SINGULARITY, DISCONTINUITY
! WITHIN THE INTERVAL) ONE WILL PROBABLY GAIN FROM
! SPLITTING UP THE INTERVAL AT THIS POINT AND CALLING THE
! INTEGRATOR ON THE SUBRANGES. IF POSSIBLE, AN
! APPROPRIATE SPECIAL-PURPOSE INTEGRATOR SHOULD BE USED,
! WHICH IS DESIGNED FOR HANDLING THE TYPE OF DIFFICULTY
! INVOLVED.
! = 2 THE OCCURRENCE OF ROUNDOFF ERROR IS DETECTED,
! WHICH PREVENTS THE REQUESTED TOLERANCE FROM BEING
! ACHIEVED.
! THE ERROR MAY BE UNDER-ESTIMATED.
! = 3 EXTREMELY BAD INTEGRAND BEHAVIOUR
! OCCURS AT SOME POINTS OF THE INTEGRATION INTERVAL.
! = 4 THE ALGORITHM DOES NOT CONVERGE.
! ROUNDOFF ERROR IS DETECTED IN THE EXTRAPOLATION TABLE.
! IT IS PRESUMED THAT THE REQUESTED TOLERANCE CANNOT BE
! ACHIEVED, AND THAT THE RETURNED RESULT IS THE BEST
! WHICH CAN BE OBTAINED.
! = 5 THE INTEGRAL IS PROBABLY DIVERGENT, OR SLOWLY CONVERGENT.
! IT MUST BE NOTED THAT DIVERGENCE CAN OCCUR WITH ANY
! OTHER VALUE OF IER.
! = 6 THE INPUT IS INVALID BECAUSE EPSABS OR EPSREL IS
! NEGATIVE, LIMIT < 1, LENW < 46*LIMIT, OR LENIW < 3*LIMIT.
! RESULT, ABSERR, LAST ARE SET TO ZERO.
! EXCEPT WHEN LIMIT OR LENW OR LENIW IS INVALID, IWORK(1),
! WORK(LIMIT*2+1) AND WORK(LIMIT*3+1) ARE SET TO ZERO,
! WORK(1) IS SET TO A, AND WORK(LIMIT+1) TO B.
! DIMENSIONING PARAMETERS
! LIMIT - INTEGER
! LIMIT DETERMINES THE MAXIMUM NUMBER OF SUBINTERVALS IN THE
! PARTITION OF THE GIVEN INTEGRATION INTERVAL (A,B), LIMIT >= 1.
! IF LIMIT < 1, THE ROUTINE WILL END WITH IER = 6.
! LAST - INTEGER
! ON RETURN, LAST EQUALS THE NUMBER OF SUBINTERVALS PRODUCED
! IN THE SUBDIVISION PROCESS, WHICH DETERMINES THE NUMBER OF
! SIGNIFICANT ELEMENTS ACTUALLY IN THE WORK ARRAYS.
USE constants_NSWC
IMPLICIT NONE
REAL (dp), INTENT(IN) :: a, b, epsabs, epsrel
REAL (dp), INTENT(OUT) :: result, abserr
INTEGER, INTENT(IN) :: limit
INTEGER, INTENT(OUT) :: ier, last
INTERFACE
FUNCTION f(x) RESULT(fx)
USE constants_NSWC
IMPLICIT NONE
REAL (dp), INTENT(IN) :: x
REAL (dp) :: fx
END FUNCTION f
END INTERFACE
! CHECK VALIDITY OF LIMIT
ier = 6
last = 0
result = 0.0D0
abserr = 0.0D0
IF (limit < 1) RETURN
! PREPARE CALL FOR QXGSE.
CALL qxgse(f, a, b, epsabs, epsrel, limit, result, abserr, ier, last)
RETURN
END SUBROUTINE qxgs
SUBROUTINE qxgse(f, a, b, epsabs, epsrel, limit, result, abserr, ier, last)
! THE ROUTINE CALCULATES AN APPROXIMATION RESULT TO A
! DEFINITE INTEGRAL I = INTEGRAL OF F OVER (A,B),
! HOPEFULLY SATISFYING FOLLOWING CLAIM FOR ACCURACY
! ABS(I-RESULT).LE.MAX(EPSABS,EPSREL*ABS(I)).
! PARAMETERS
! ON ENTRY
! F - REAL
! FUNCTION SUBPROGRAM DEFINING THE INTEGRAND
! FUNCTION F(X). THE ACTUAL NAME FOR F NEEDS TO BE
! DECLARED E X T E R N A L IN THE DRIVER PROGRAM.
! A - REAL
! LOWER LIMIT OF INTEGRATION
! B - REAL
! UPPER LIMIT OF INTEGRATION
! EPSABS - REAL
! ABSOLUTE ACCURACY REQUESTED
! EPSREL - REAL
! RELATIVE ACCURACY REQUESTED
! LIMIT - INTEGER
! GIVES AN UPPERBOUND ON THE NUMBER OF SUBINTERVALS
! IN THE PARTITION OF (A,B)
! ON RETURN
! RESULT - REAL
! APPROXIMATION TO THE INTEGRAL
! ABSERR - REAL
! ESTIMATE OF THE MODULUS OF THE ABSOLUTE ERROR,
! WHICH SHOULD EQUAL OR EXCEED ABS(I-RESULT)
! IER - INTEGER
! IER = 0 NORMAL AND RELIABLE TERMINATION OF THE
! ROUTINE. IT IS ASSUMED THAT THE REQUESTED
! ACCURACY HAS BEEN ACHIEVED.
! IER > 0 ABNORMAL TERMINATION OF THE ROUTINE
! THE ESTIMATES FOR INTEGRAL AND ERROR ARE
! LESS RELIABLE. IT IS ASSUMED THAT THE
! REQUESTED ACCURACY HAS NOT BEEN ACHIEVED.
! ERROR MESSAGES
! = 1 MAXIMUM NUMBER OF SUBDIVISIONS ALLOWED
! HAS BEEN ACHIEVED. ONE CAN ALLOW MORE SUB-
! DIVISIONS BY INCREASING THE VALUE OF LIMIT
! (AND TAKING THE ACCORDING DIMENSION
! ADJUSTMENTS INTO ACCOUNT). HOWEVER, IF
! THIS YIELDS NO IMPROVEMENT IT IS ADVISED
! TO ANALYZE THE INTEGRAND IN ORDER TO
! DETERMINE THE INTEGRATION DIFFICULTIES. IF
! THE POSITION OF A LOCAL DIFFICULTY CAN BE
! DETERMINED (E.G. SINGULARITY,
! DISCONTINUITY WITHIN THE INTERVAL) ONE
! WILL PROBABLY GAIN FROM SPLITTING UP THE
! INTERVAL AT THIS POINT AND CALLING THE
! INTEGRATOR ON THE SUBRANGES. IF POSSIBLE,
! AN APPROPRIATE SPECIAL-PURPOSE INTEGRATOR
! SHOULD BE USED, WHICH IS DESIGNED FOR
! HANDLING THE TYPE OF DIFFICULTY INVOLVED.
! = 2 THE OCCURRENCE OF ROUNDOFF ERROR IS DETEC-
! TED, WHICH PREVENTS THE REQUESTED
! TOLERANCE FROM BEING ACHIEVED.
! THE ERROR MAY BE UNDER-ESTIMATED.
! = 3 EXTREMELY BAD INTEGRAND BEHAVIOUR OCCURS AT
! SOME POINTS OF THE INTEGRATION INTERVAL.
! = 4 THE ALGORITHM DOES NOT CONVERGE.
! ROUNDOFF ERROR IS DETECTED IN THE
! EXTRAPOLATION TABLE.
! IT IS PRESUMED THAT THE REQUESTED TOLERANCE
! CANNOT BE ACHIEVED, AND THAT THE RETURNED RESULT
! IS THE BEST WHICH CAN BE OBTAINED.
! = 5 THE INTEGRAL IS PROBABLY DIVERGENT, OR SLOWLY
! CONVERGENT. IT MUST BE NOTED THAT DIVERGENCE
! CAN OCCUR WITH ANY OTHER VALUE OF IER.
! = 6 THE INPUT IS INVALID BECAUSE EPSABS OR
! EPSREL IS NEGATIVE. RESULT, ABSERR,
! LAST, RLIST(1), IORD(1), AND ELIST(1)
! ARE SET TO ZERO. ALIST(1) AND BLIST(1)
! ARE SET TO A AND B RESPECTIVELY.
! ALIST - REAL
! VECTOR OF DIMENSION AT LEAST LIMIT, THE FIRST
! LAST ELEMENTS OF WHICH ARE THE LEFT END POINTS
! OF THE SUBINTERVALS IN THE PARTITION OF THE
! GIVEN INTEGRATION RANGE (A,B)
! BLIST - REAL
! VECTOR OF DIMENSION AT LEAST LIMIT, THE FIRST
! LAST ELEMENTS OF WHICH ARE THE RIGHT END POINTS
! OF THE SUBINTERVALS IN THE PARTITION OF THE GIVEN
! INTEGRATION RANGE (A,B)
! RLIST - REAL
! VECTOR OF DIMENSION AT LEAST LIMIT, THE FIRST `LAST'
! ELEMENTS OF WHICH ARE THE INTEGRAL APPROXIMATIONS ON
! THE SUBINTERVALS
! ELIST - REAL
! VECTOR OF DIMENSION AT LEAST LIMIT, THE FIRST
! LAST ELEMENTS OF WHICH ARE THE MODULI OF THE
! ABSOLUTE ERROR ESTIMATES ON THE SUBINTERVALS
! IORD - INTEGER
! VECTOR OF DIMENSION AT LEAST LIMIT, THE FIRST K ELEMENTS
! OF WHICH ARE POINTERS TO THE ERROR ESTIMATES OVER THE
! SUBINTERVALS, SUCH THAT ELIST(IORD(1)), ..., ELIST(IORD(K))
! FORM A DECREASING SEQUENCE, WITH K = LAST IF
! LAST <= (LIMIT/2+2), AND K = LIMIT+1-LAST OTHERWISE
! LAST - INTEGER
! NUMBER OF SUBINTERVALS ACTUALLY PRODUCED IN THE
! SUBDIVISION PROCESS
! VALP - REAL
! VALN ARRAYS OF DIMENSION AT LEAST (21,LIMIT) USED TO
! SAVE THE FUNCTIONAL VALUES
! LP - INTEGER
! LN VECTORS OF DIMENSION AT LEAST LIMIT, USED TO
! STORE THE ACTUAL NUMBER OF FUNCTIONAL VALUES
! SAVED IN THE CORRESPONDING COLUMN OF VALP,VALN
!***ROUTINES CALLED F, SPMPAR, QELG, QXLQM, QPSRT, QXRRD, QXCPY
USE constants_NSWC
IMPLICIT NONE
REAL (dp), INTENT(IN) :: a, b, epsabs, epsrel
REAL (dp), INTENT(OUT) :: result, abserr
INTEGER, INTENT(IN) :: limit
INTEGER, INTENT(OUT) :: ier, last
INTERFACE
FUNCTION f(x) RESULT(fx)
USE constants_NSWC
IMPLICIT NONE
REAL (dp), INTENT(IN) :: x
REAL (dp) :: fx
END FUNCTION f
END INTERFACE
REAL (dp) :: abseps, alist(limit), area, area1, area12, area2, a1, a2, b1, &
b2, blist(limit), correc, defab1, defab2, dres, elist(limit), &
epmach, erlarg, erlast, errbnd, errmax, error1, error2, &
erro12, errsum, ertest, oflow, rerr, resabs, reseps, res3la(3), &
rlist(limit), rlist2(52), small, t, uflow, valp(21,limit), &
valn(21,limit), vp1(21), vp2(21), vn1(21), vn2(21), defabs
INTEGER :: id, ierro, iord(limit), iroff1, iroff2, iroff3, jupbnd, k, &
ksgn, lp(limit), ln(limit), ktmin, maxerr, nres, nrmax, numrl2, &
lp1, lp2, ln1, ln2
LOGICAL :: extrap, noext
! MACHINE DEPENDENT CONSTANTS
! ---------------------------
! EPMACH IS THE LARGEST RELATIVE SPACING.
! UFLOW IS THE SMALLEST POSITIVE MAGNITUDE.
! OFLOW IS THE LARGEST POSITIVE MAGNITUDE.
epmach = dpmpar(1)
uflow = dpmpar(2)
oflow = dpmpar(3)
! TEST ON VALIDITY OF PARAMETERS
! ------------------------------
last = 0
result = 0.0D0
abserr = 0.0D0
alist(1) = a
blist(1) = b
rlist(1) = 0.0D0
elist(1) = 0.0D0
ier = 6
IF (epsabs < 0.0D0 .OR. epsrel < 0.0D0) GO TO 999
ier = 0
rerr = MAX(epsrel, 50.0D0*epmach)
! FIRST APPROXIMATION TO THE INTEGRAL
! -----------------------------------
ierro = 0
lp(1) = 1
ln(1) = 1
valp(1,1) = f((a + b)*0.5D0)
valn(1,1) = valp(1,1)
CALL qxlqm(f, a, b, result, abserr, resabs, defabs, valp(1:,1), valn(1:,1), &
lp(1), ln(1), 2, epmach, uflow, oflow)
! TEST ON ACCURACY.
dres = ABS(result)
errbnd = MAX(epsabs,rerr*dres)
last = 1
rlist(1) = result
elist(1) = abserr
iord(1) = 1
IF (abserr <= 100.0D0*epmach*defabs .AND. abserr > errbnd) ier = 2
IF (limit == 1) ier = 1
IF (ier /= 0 .OR. (abserr <= errbnd .AND. abserr /= resabs) .OR. &
abserr == 0.0D0) GO TO 999
! INITIALIZATION
! --------------
rlist2(1) = result
errmax = abserr
maxerr = 1
area = result
errsum = abserr
abserr = oflow
nrmax = 1
nres = 0
numrl2 = 2
ktmin = 0
extrap = .false.
noext = .false.
iroff1 = 0
iroff2 = 0
iroff3 = 0
ksgn = -1
IF (dres >= (1.0D0 - 50.0D0*epmach)*defabs) ksgn = 1
t = 1.0D0 + 100.0D0*epmach
! MAIN DO-LOOP
! ------------
DO last = 2, limit
! BISECT THE SUBINTERVAL WITH THE NRMAX-TH LARGEST ERROR ESTIMATE.
a1 = alist(maxerr)
b1 = 0.5D0*(alist(maxerr) + blist(maxerr))
a2 = b1
b2 = blist(maxerr)
erlast = errmax
CALL qxrrd(f, valn(1:,maxerr), ln(maxerr), b1, a1, vn1, vp1, ln1, lp1)
CALL qxlqm(f, a1, b1, area1, error1, resabs, defab1, vp1, vn1, lp1, ln1, &
2, epmach, uflow, oflow)
CALL qxrrd(f, valp(1:,maxerr), lp(maxerr), a2, b2, vp2, vn2, lp2, ln2)
CALL qxlqm(f, a2, b2, area2, error2, resabs, defab2, vp2, vn2, lp2, ln2, &
2, epmach, uflow, oflow)
! IMPROVE PREVIOUS APPROXIMATIONS TO INTEGRAL
! AND ERROR AND TEST FOR ACCURACY.
area12 = area1 + area2
erro12 = error1 + error2
errsum = errsum + erro12 - errmax
area = area + area12 - rlist(maxerr)
IF (defab1 == error1 .OR. defab2 == error2) GO TO 15
IF (ABS(rlist(maxerr) - area12) > 0.1D-04*ABS(area12) &
.OR. erro12 < 0.99D0*errmax) GO TO 10
IF (extrap) iroff2 = iroff2 + 1
IF (.NOT.extrap) iroff1 = iroff1 + 1
10 IF (last > 10 .AND. erro12 > errmax) iroff3 = iroff3 + 1
15 rlist(maxerr) = area1
rlist(last) = area2
errbnd = MAX(epsabs,rerr*ABS(area))
! TEST FOR ROUNDOFF ERROR AND EVENTUALLY SET ERROR FLAG.
IF (iroff1 + iroff2 >= 10 .OR. iroff3 >= 20) ier = 2
IF (iroff2 >= 5) ierro = 3
! SET ERROR FLAG IN THE CASE THAT THE NUMBER OF SUBINTERVALS EQUALS LIMIT.
IF (last == limit) ier = 1
! SET ERROR FLAG IN THE CASE OF BAD INTEGRAND BEHAVIOUR
! AT A POINT OF THE INTEGRATION RANGE.
IF (MAX(ABS(a1),ABS(b2)) <= t*(ABS(a2) + 1.d+03*uflow)) ier = 4
! APPEND THE NEWLY-CREATED INTERVALS TO THE LIST.
IF (error2 > error1) GO TO 20
alist(last) = a2
blist(maxerr) = b1
blist(last) = b2
elist(maxerr) = error1
elist(last) = error2
CALL qxcpy(valp(1:,maxerr), vp1, lp1)
lp(maxerr) = lp1
CALL qxcpy(valn(1:,maxerr), vn1, ln1)
ln(maxerr) = ln1
CALL qxcpy(valp(1:,last), vp2, lp2)
lp(last) = lp2
CALL qxcpy(valn(1:,last), vn2, ln2)
ln(last) = ln2
GO TO 30
20 alist(maxerr) = a2
alist(last) = a1
blist(last) = b1
rlist(maxerr) = area2
rlist(last) = area1