-
Notifications
You must be signed in to change notification settings - Fork 0
/
radau5.f
1154 lines (1154 loc) · 41.3 KB
/
radau5.f
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 RADAU5(N,FCN,X,Y,XEND,H,
& RTOL,ATOL,ITOL,
& JAC ,IJAC,MLJAC,MUJAC,
& MAS ,IMAS,MLMAS,MUMAS,
& SOLOUT,IOUT,
& WORK,LWORK,IWORK,LIWORK,RPAR,IPAR,IDID)
C ----------------------------------------------------------
C NUMERICAL SOLUTION OF A STIFF (OR DIFFERENTIAL ALGEBRAIC)
C SYSTEM OF FIRST 0RDER ORDINARY DIFFERENTIAL EQUATIONS
C M*Y'=F(X,Y).
C THE SYSTEM CAN BE (LINEARLY) IMPLICIT (MASS-MATRIX M .NE. I)
C OR EXPLICIT (M=I).
C THE METHOD USED IS AN IMPLICIT RUNGE-KUTTA METHOD (RADAU IIA)
C OF ORDER 5 WITH STEP SIZE CONTROL AND CONTINUOUS OUTPUT.
C CF. SECTION IV.8
C
C AUTHORS: E. HAIRER AND G. WANNER
C UNIVERSITE DE GENEVE, DEPT. DE MATHEMATIQUES
C CH-1211 GENEVE 24, SWITZERLAND
C E-MAIL: Ernst.Hairer@math.unige.ch
C Gerhard.Wanner@math.unige.ch
C
C THIS CODE IS PART OF THE BOOK:
C E. HAIRER AND G. WANNER, SOLVING ORDINARY DIFFERENTIAL
C EQUATIONS II. STIFF AND DIFFERENTIAL-ALGEBRAIC PROBLEMS.
C SPRINGER SERIES IN COMPUTATIONAL MATHEMATICS 14,
C SPRINGER-VERLAG 1991, SECOND EDITION 1996.
C
C VERSION OF JULY 9, 1996
C (latest small correction: January 18, 2002)
C
C INPUT PARAMETERS
C ----------------
C N DIMENSION OF THE SYSTEM
C
C FCN NAME (EXTERNAL) OF SUBROUTINE COMPUTING THE
C VALUE OF F(X,Y):
C SUBROUTINE FCN(N,X,Y,F,RPAR,IPAR)
C DOUBLE PRECISION X,Y(N),F(N)
C F(1)=... ETC.
C RPAR, IPAR (SEE BELOW)
C
C X INITIAL X-VALUE
C
C Y(N) INITIAL VALUES FOR Y
C
C XEND FINAL X-VALUE (XEND-X MAY BE POSITIVE OR NEGATIVE)
C
C H INITIAL STEP SIZE GUESS;
C FOR STIFF EQUATIONS WITH INITIAL TRANSIENT,
C H=1.D0/(NORM OF F'), USUALLY 1.D-3 OR 1.D-5, IS GOOD.
C THIS CHOICE IS NOT VERY IMPORTANT, THE STEP SIZE IS
C QUICKLY ADAPTED. (IF H=0.D0, THE CODE PUTS H=1.D-6).
C
C RTOL,ATOL RELATIVE AND ABSOLUTE ERROR TOLERANCES. THEY
C CAN BE BOTH SCALARS OR ELSE BOTH VECTORS OF LENGTH N.
C
C ITOL SWITCH FOR RTOL AND ATOL:
C ITOL=0: BOTH RTOL AND ATOL ARE SCALARS.
C THE CODE KEEPS, ROUGHLY, THE LOCAL ERROR OF
C Y(I) BELOW RTOL*ABS(Y(I))+ATOL
C ITOL=1: BOTH RTOL AND ATOL ARE VECTORS.
C THE CODE KEEPS THE LOCAL ERROR OF Y(I) BELOW
C RTOL(I)*ABS(Y(I))+ATOL(I).
C
C JAC NAME (EXTERNAL) OF THE SUBROUTINE WHICH COMPUTES
C THE PARTIAL DERIVATIVES OF F(X,Y) WITH RESPECT TO Y
C (THIS ROUTINE IS ONLY CALLED IF IJAC=1; SUPPLY
C A DUMMY SUBROUTINE IN THE CASE IJAC=0).
C FOR IJAC=1, THIS SUBROUTINE MUST HAVE THE FORM
C SUBROUTINE JAC(N,X,Y,DFY,LDFY,RPAR,IPAR)
C DOUBLE PRECISION X,Y(N),DFY(LDFY,N)
C DFY(1,1)= ...
C LDFY, THE COLUMN-LENGTH OF THE ARRAY, IS
C FURNISHED BY THE CALLING PROGRAM.
C IF (MLJAC.EQ.N) THE JACOBIAN IS SUPPOSED TO
C BE FULL AND THE PARTIAL DERIVATIVES ARE
C STORED IN DFY AS
C DFY(I,J) = PARTIAL F(I) / PARTIAL Y(J)
C ELSE, THE JACOBIAN IS TAKEN AS BANDED AND
C THE PARTIAL DERIVATIVES ARE STORED
C DIAGONAL-WISE AS
C DFY(I-J+MUJAC+1,J) = PARTIAL F(I) / PARTIAL Y(J).
C
C IJAC SWITCH FOR THE COMPUTATION OF THE JACOBIAN:
C IJAC=0: JACOBIAN IS COMPUTED INTERNALLY BY FINITE
C DIFFERENCES, SUBROUTINE "JAC" IS NEVER CALLED.
C IJAC=1: JACOBIAN IS SUPPLIED BY SUBROUTINE JAC.
C
C MLJAC SWITCH FOR THE BANDED STRUCTURE OF THE JACOBIAN:
C MLJAC=N: JACOBIAN IS A FULL MATRIX. THE LINEAR
C ALGEBRA IS DONE BY FULL-MATRIX GAUSS-ELIMINATION.
C 0<=MLJAC<N: MLJAC IS THE LOWER BANDWITH OF JACOBIAN
C MATRIX (>= NUMBER OF NON-ZERO DIAGONALS BELOW
C THE MAIN DIAGONAL).
C
C MUJAC UPPER BANDWITH OF JACOBIAN MATRIX (>= NUMBER OF NON-
C ZERO DIAGONALS ABOVE THE MAIN DIAGONAL).
C NEED NOT BE DEFINED IF MLJAC=N.
C
C ---- MAS,IMAS,MLMAS, AND MUMAS HAVE ANALOG MEANINGS -----
C ---- FOR THE "MASS MATRIX" (THE MATRIX "M" OF SECTION IV.8): -
C
C MAS NAME (EXTERNAL) OF SUBROUTINE COMPUTING THE MASS-
C MATRIX M.
C IF IMAS=0, THIS MATRIX IS ASSUMED TO BE THE IDENTITY
C MATRIX AND NEEDS NOT TO BE DEFINED;
C SUPPLY A DUMMY SUBROUTINE IN THIS CASE.
C IF IMAS=1, THE SUBROUTINE MAS IS OF THE FORM
C SUBROUTINE MAS(N,AM,LMAS,RPAR,IPAR)
C DOUBLE PRECISION AM(LMAS,N)
C AM(1,1)= ....
C IF (MLMAS.EQ.N) THE MASS-MATRIX IS STORED
C AS FULL MATRIX LIKE
C AM(I,J) = M(I,J)
C ELSE, THE MATRIX IS TAKEN AS BANDED AND STORED
C DIAGONAL-WISE AS
C AM(I-J+MUMAS+1,J) = M(I,J).
C
C IMAS GIVES INFORMATION ON THE MASS-MATRIX:
C IMAS=0: M IS SUPPOSED TO BE THE IDENTITY
C MATRIX, MAS IS NEVER CALLED.
C IMAS=1: MASS-MATRIX IS SUPPLIED.
C
C MLMAS SWITCH FOR THE BANDED STRUCTURE OF THE MASS-MATRIX:
C MLMAS=N: THE FULL MATRIX CASE. THE LINEAR
C ALGEBRA IS DONE BY FULL-MATRIX GAUSS-ELIMINATION.
C 0<=MLMAS<N: MLMAS IS THE LOWER BANDWITH OF THE
C MATRIX (>= NUMBER OF NON-ZERO DIAGONALS BELOW
C THE MAIN DIAGONAL).
C MLMAS IS SUPPOSED TO BE .LE. MLJAC.
C
C MUMAS UPPER BANDWITH OF MASS-MATRIX (>= NUMBER OF NON-
C ZERO DIAGONALS ABOVE THE MAIN DIAGONAL).
C NEED NOT BE DEFINED IF MLMAS=N.
C MUMAS IS SUPPOSED TO BE .LE. MUJAC.
C
C SOLOUT NAME (EXTERNAL) OF SUBROUTINE PROVIDING THE
C NUMERICAL SOLUTION DURING INTEGRATION.
C IF IOUT=1, IT IS CALLED AFTER EVERY SUCCESSFUL STEP.
C SUPPLY A DUMMY SUBROUTINE IF IOUT=0.
C IT MUST HAVE THE FORM
C SUBROUTINE SOLOUT (NR,XOLD,X,Y,CONT,LRC,N,
C RPAR,IPAR,IRTRN)
C DOUBLE PRECISION X,Y(N),CONT(LRC)
C ....
C SOLOUT FURNISHES THE SOLUTION "Y" AT THE NR-TH
C GRID-POINT "X" (THEREBY THE INITIAL VALUE IS
C THE FIRST GRID-POINT).
C "XOLD" IS THE PRECEEDING GRID-POINT.
C "IRTRN" SERVES TO INTERRUPT THE INTEGRATION. IF IRTRN
C IS SET <0, RADAU5 RETURNS TO THE CALLING PROGRAM.
C
C ----- CONTINUOUS OUTPUT: -----
C DURING CALLS TO "SOLOUT", A CONTINUOUS SOLUTION
C FOR THE INTERVAL [XOLD,X] IS AVAILABLE THROUGH
C THE FUNCTION
C >>> CONTR5(I,S,CONT,LRC) <<<
C WHICH PROVIDES AN APPROXIMATION TO THE I-TH
C COMPONENT OF THE SOLUTION AT THE POINT S. THE VALUE
C S SHOULD LIE IN THE INTERVAL [XOLD,X].
C DO NOT CHANGE THE ENTRIES OF CONT(LRC), IF THE
C DENSE OUTPUT FUNCTION IS USED.
C
C IOUT SWITCH FOR CALLING THE SUBROUTINE SOLOUT:
C IOUT=0: SUBROUTINE IS NEVER CALLED
C IOUT=1: SUBROUTINE IS AVAILABLE FOR OUTPUT.
C
C WORK ARRAY OF WORKING SPACE OF LENGTH "LWORK".
C WORK(1), WORK(2),.., WORK(20) SERVE AS PARAMETERS
C FOR THE CODE. FOR STANDARD USE OF THE CODE
C WORK(1),..,WORK(20) MUST BE SET TO ZERO BEFORE
C CALLING. SEE BELOW FOR A MORE SOPHISTICATED USE.
C WORK(21),..,WORK(LWORK) SERVE AS WORKING SPACE
C FOR ALL VECTORS AND MATRICES.
C "LWORK" MUST BE AT LEAST
C N*(LJAC+LMAS+3*LE+12)+20
C WHERE
C LJAC=N IF MLJAC=N (FULL JACOBIAN)
C LJAC=MLJAC+MUJAC+1 IF MLJAC<N (BANDED JAC.)
C AND
C LMAS=0 IF IMAS=0
C LMAS=N IF IMAS=1 AND MLMAS=N (FULL)
C LMAS=MLMAS+MUMAS+1 IF MLMAS<N (BANDED MASS-M.)
C AND
C LE=N IF MLJAC=N (FULL JACOBIAN)
C LE=2*MLJAC+MUJAC+1 IF MLJAC<N (BANDED JAC.)
C
C IN THE USUAL CASE WHERE THE JACOBIAN IS FULL AND THE
C MASS-MATRIX IS THE INDENTITY (IMAS=0), THE MINIMUM
C STORAGE REQUIREMENT IS
C LWORK = 4*N*N+12*N+20.
C IF IWORK(9)=M1>0 THEN "LWORK" MUST BE AT LEAST
C N*(LJAC+12)+(N-M1)*(LMAS+3*LE)+20
C WHERE IN THE DEFINITIONS OF LJAC, LMAS AND LE THE
C NUMBER N CAN BE REPLACED BY N-M1.
C
C LWORK DECLARED LENGTH OF ARRAY "WORK".
C
C IWORK INTEGER WORKING SPACE OF LENGTH "LIWORK".
C IWORK(1),IWORK(2),...,IWORK(20) SERVE AS PARAMETERS
C FOR THE CODE. FOR STANDARD USE, SET IWORK(1),..,
C IWORK(20) TO ZERO BEFORE CALLING.
C IWORK(21),...,IWORK(LIWORK) SERVE AS WORKING AREA.
C "LIWORK" MUST BE AT LEAST 3*N+20.
C
C LIWORK DECLARED LENGTH OF ARRAY "IWORK".
C
C RPAR, IPAR REAL AND INTEGER PARAMETERS (OR PARAMETER ARRAYS) WHICH
C CAN BE USED FOR COMMUNICATION BETWEEN YOUR CALLING
C PROGRAM AND THE FCN, JAC, MAS, SOLOUT SUBROUTINES.
C
C ----------------------------------------------------------------------
C
C SOPHISTICATED SETTING OF PARAMETERS
C -----------------------------------
C SEVERAL PARAMETERS OF THE CODE ARE TUNED TO MAKE IT WORK
C WELL. THEY MAY BE DEFINED BY SETTING WORK(1),...
C AS WELL AS IWORK(1),... DIFFERENT FROM ZERO.
C FOR ZERO INPUT, THE CODE CHOOSES DEFAULT VALUES:
C
C IWORK(1) IF IWORK(1).NE.0, THE CODE TRANSFORMS THE JACOBIAN
C MATRIX TO HESSENBERG FORM. THIS IS PARTICULARLY
C ADVANTAGEOUS FOR LARGE SYSTEMS WITH FULL JACOBIAN.
C IT DOES NOT WORK FOR BANDED JACOBIAN (MLJAC<N)
C AND NOT FOR IMPLICIT SYSTEMS (IMAS=1).
C
C IWORK(2) THIS IS THE MAXIMAL NUMBER OF ALLOWED STEPS.
C THE DEFAULT VALUE (FOR IWORK(2)=0) IS 100000.
C
C IWORK(3) THE MAXIMUM NUMBER OF NEWTON ITERATIONS FOR THE
C SOLUTION OF THE IMPLICIT SYSTEM IN EACH STEP.
C THE DEFAULT VALUE (FOR IWORK(3)=0) IS 7.
C
C IWORK(4) IF IWORK(4).EQ.0 THE EXTRAPOLATED COLLOCATION SOLUTION
C IS TAKEN AS STARTING VALUE FOR NEWTON'S METHOD.
C IF IWORK(4).NE.0 ZERO STARTING VALUES ARE USED.
C THE LATTER IS RECOMMENDED IF NEWTON'S METHOD HAS
C DIFFICULTIES WITH CONVERGENCE (THIS IS THE CASE WHEN
C NSTEP IS LARGER THAN NACCPT + NREJCT; SEE OUTPUT PARAM.).
C DEFAULT IS IWORK(4)=0.
C
C THE FOLLOWING 3 PARAMETERS ARE IMPORTANT FOR
C DIFFERENTIAL-ALGEBRAIC SYSTEMS OF INDEX > 1.
C THE FUNCTION-SUBROUTINE SHOULD BE WRITTEN SUCH THAT
C THE INDEX 1,2,3 VARIABLES APPEAR IN THIS ORDER.
C IN ESTIMATING THE ERROR THE INDEX 2 VARIABLES ARE
C MULTIPLIED BY H, THE INDEX 3 VARIABLES BY H**2.
C
C IWORK(5) DIMENSION OF THE INDEX 1 VARIABLES (MUST BE > 0). FOR
C ODE'S THIS EQUALS THE DIMENSION OF THE SYSTEM.
C DEFAULT IWORK(5)=N.
C
C IWORK(6) DIMENSION OF THE INDEX 2 VARIABLES. DEFAULT IWORK(6)=0.
C
C IWORK(7) DIMENSION OF THE INDEX 3 VARIABLES. DEFAULT IWORK(7)=0.
C
C IWORK(8) SWITCH FOR STEP SIZE STRATEGY
C IF IWORK(8).EQ.1 MOD. PREDICTIVE CONTROLLER (GUSTAFSSON)
C IF IWORK(8).EQ.2 CLASSICAL STEP SIZE CONTROL
C THE DEFAULT VALUE (FOR IWORK(8)=0) IS IWORK(8)=1.
C THE CHOICE IWORK(8).EQ.1 SEEMS TO PRODUCE SAFER RESULTS;
C FOR SIMPLE PROBLEMS, THE CHOICE IWORK(8).EQ.2 PRODUCES
C OFTEN SLIGHTLY FASTER RUNS
C
C IF THE DIFFERENTIAL SYSTEM HAS THE SPECIAL STRUCTURE THAT
C Y(I)' = Y(I+M2) FOR I=1,...,M1,
C WITH M1 A MULTIPLE OF M2, A SUBSTANTIAL GAIN IN COMPUTERTIME
C CAN BE ACHIEVED BY SETTING THE PARAMETERS IWORK(9) AND IWORK(10).
C E.G., FOR SECOND ORDER SYSTEMS P'=V, V'=G(P,V), WHERE P AND V ARE
C VECTORS OF DIMENSION N/2, ONE HAS TO PUT M1=M2=N/2.
C FOR M1>0 SOME OF THE INPUT PARAMETERS HAVE DIFFERENT MEANINGS:
C - JAC: ONLY THE ELEMENTS OF THE NON-TRIVIAL PART OF THE
C JACOBIAN HAVE TO BE STORED
C IF (MLJAC.EQ.N-M1) THE JACOBIAN IS SUPPOSED TO BE FULL
C DFY(I,J) = PARTIAL F(I+M1) / PARTIAL Y(J)
C FOR I=1,N-M1 AND J=1,N.
C ELSE, THE JACOBIAN IS BANDED ( M1 = M2 * MM )
C DFY(I-J+MUJAC+1,J+K*M2) = PARTIAL F(I+M1) / PARTIAL Y(J+K*M2)
C FOR I=1,MLJAC+MUJAC+1 AND J=1,M2 AND K=0,MM.
C - MLJAC: MLJAC=N-M1: IF THE NON-TRIVIAL PART OF THE JACOBIAN IS FULL
C 0<=MLJAC<N-M1: IF THE (MM+1) SUBMATRICES (FOR K=0,MM)
C PARTIAL F(I+M1) / PARTIAL Y(J+K*M2), I,J=1,M2
C ARE BANDED, MLJAC IS THE MAXIMAL LOWER BANDWIDTH
C OF THESE MM+1 SUBMATRICES
C - MUJAC: MAXIMAL UPPER BANDWIDTH OF THESE MM+1 SUBMATRICES
C NEED NOT BE DEFINED IF MLJAC=N-M1
C - MAS: IF IMAS=0 THIS MATRIX IS ASSUMED TO BE THE IDENTITY AND
C NEED NOT BE DEFINED. SUPPLY A DUMMY SUBROUTINE IN THIS CASE.
C IT IS ASSUMED THAT ONLY THE ELEMENTS OF RIGHT LOWER BLOCK OF
C DIMENSION N-M1 DIFFER FROM THAT OF THE IDENTITY MATRIX.
C IF (MLMAS.EQ.N-M1) THIS SUBMATRIX IS SUPPOSED TO BE FULL
C AM(I,J) = M(I+M1,J+M1) FOR I=1,N-M1 AND J=1,N-M1.
C ELSE, THE MASS MATRIX IS BANDED
C AM(I-J+MUMAS+1,J) = M(I+M1,J+M1)
C - MLMAS: MLMAS=N-M1: IF THE NON-TRIVIAL PART OF M IS FULL
C 0<=MLMAS<N-M1: LOWER BANDWIDTH OF THE MASS MATRIX
C - MUMAS: UPPER BANDWIDTH OF THE MASS MATRIX
C NEED NOT BE DEFINED IF MLMAS=N-M1
C
C IWORK(9) THE VALUE OF M1. DEFAULT M1=0.
C
C IWORK(10) THE VALUE OF M2. DEFAULT M2=M1.
C
C ----------
C
C WORK(1) UROUND, THE ROUNDING UNIT, DEFAULT 1.D-16.
C
C WORK(2) THE SAFETY FACTOR IN STEP SIZE PREDICTION,
C DEFAULT 0.9D0.
C
C WORK(3) DECIDES WHETHER THE JACOBIAN SHOULD BE RECOMPUTED;
C INCREASE WORK(3), TO 0.1 SAY, WHEN JACOBIAN EVALUATIONS
C ARE COSTLY. FOR SMALL SYSTEMS WORK(3) SHOULD BE SMALLER
C (0.001D0, SAY). NEGATIV WORK(3) FORCES THE CODE TO
C COMPUTE THE JACOBIAN AFTER EVERY ACCEPTED STEP.
C DEFAULT 0.001D0.
C
C WORK(4) STOPPING CRITERION FOR NEWTON'S METHOD, USUALLY CHOSEN <1.
C SMALLER VALUES OF WORK(4) MAKE THE CODE SLOWER, BUT SAFER.
C DEFAULT MIN(0.03D0,RTOL(1)**0.5D0)
C
C WORK(5) AND WORK(6) : IF WORK(5) < HNEW/HOLD < WORK(6), THEN THE
C STEP SIZE IS NOT CHANGED. THIS SAVES, TOGETHER WITH A
C LARGE WORK(3), LU-DECOMPOSITIONS AND COMPUTING TIME FOR
C LARGE SYSTEMS. FOR SMALL SYSTEMS ONE MAY HAVE
C WORK(5)=1.D0, WORK(6)=1.2D0, FOR LARGE FULL SYSTEMS
C WORK(5)=0.99D0, WORK(6)=2.D0 MIGHT BE GOOD.
C DEFAULTS WORK(5)=1.D0, WORK(6)=1.2D0 .
C
C WORK(7) MAXIMAL STEP SIZE, DEFAULT XEND-X.
C
C WORK(8), WORK(9) PARAMETERS FOR STEP SIZE SELECTION
C THE NEW STEP SIZE IS CHOSEN SUBJECT TO THE RESTRICTION
C WORK(8) <= HNEW/HOLD <= WORK(9)
C DEFAULT VALUES: WORK(8)=0.2D0, WORK(9)=8.D0
C
C-----------------------------------------------------------------------
C
C OUTPUT PARAMETERS
C -----------------
C X X-VALUE FOR WHICH THE SOLUTION HAS BEEN COMPUTED
C (AFTER SUCCESSFUL RETURN X=XEND).
C
C Y(N) NUMERICAL SOLUTION AT X
C
C H PREDICTED STEP SIZE OF THE LAST ACCEPTED STEP
C
C IDID REPORTS ON SUCCESSFULNESS UPON RETURN:
C IDID= 1 COMPUTATION SUCCESSFUL,
C IDID= 2 COMPUT. SUCCESSFUL (INTERRUPTED BY SOLOUT)
C IDID=-1 INPUT IS NOT CONSISTENT,
C IDID=-2 LARGER NMAX IS NEEDED,
C IDID=-3 STEP SIZE BECOMES TOO SMALL,
C IDID=-4 MATRIX IS REPEATEDLY SINGULAR.
C
C IWORK(14) NFCN NUMBER OF FUNCTION EVALUATIONS (THOSE FOR NUMERICAL
C EVALUATION OF THE JACOBIAN ARE NOT COUNTED)
C IWORK(15) NJAC NUMBER OF JACOBIAN EVALUATIONS (EITHER ANALYTICALLY
C OR NUMERICALLY)
C IWORK(16) NSTEP NUMBER OF COMPUTED STEPS
C IWORK(17) NACCPT NUMBER OF ACCEPTED STEPS
C IWORK(18) NREJCT NUMBER OF REJECTED STEPS (DUE TO ERROR TEST),
C (STEP REJECTIONS IN THE FIRST STEP ARE NOT COUNTED)
C IWORK(19) NDEC NUMBER OF LU-DECOMPOSITIONS OF BOTH MATRICES
C IWORK(20) NSOL NUMBER OF FORWARD-BACKWARD SUBSTITUTIONS, OF BOTH
C SYSTEMS; THE NSTEP FORWARD-BACKWARD SUBSTITUTIONS,
C NEEDED FOR STEP SIZE SELECTION, ARE NOT COUNTED
C IWORK(21) NITMAX MAXIMUM NUMBER OF ITERATIONS AMONG ALL STEPS
C-----------------------------------------------------------------------
C *** *** *** *** *** *** *** *** *** *** *** *** ***
C DECLARATIONS
C *** *** *** *** *** *** *** *** *** *** *** *** ***
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
DIMENSION Y(N),ATOL(*),RTOL(*),WORK(LWORK),IWORK(LIWORK)
DIMENSION RPAR(*),IPAR(*)
LOGICAL IMPLCT,JBAND,ARRET,STARTN,PRED
EXTERNAL FCN,JAC,MAS,SOLOUT
C *** *** *** *** *** *** ***
C SETTING THE PARAMETERS
C *** *** *** *** *** *** ***
NFCN=0
NJAC=0
NSTEP=0
NACCPT=0
NREJCT=0
NDEC=0
NSOL=0
NITMAX=0
ARRET=.FALSE.
C -------- UROUND SMALLEST NUMBER SATISFYING 1.0D0+UROUND>1.0D0
IF (WORK(1).EQ.0.0D0) THEN
UROUND=1.0D-16
ELSE
UROUND=WORK(1)
IF (UROUND.LE.1.0D-19.OR.UROUND.GE.1.0D0) THEN
WRITE(6,*)' COEFFICIENTS HAVE 20 DIGITS, UROUND=',WORK(1)
ARRET=.TRUE.
END IF
END IF
C -------- CHECK AND CHANGE THE TOLERANCES
EXPM=2.0D0/3.0D0
IF (ITOL.EQ.0) THEN
IF (ATOL(1).LE.0.D0.OR.RTOL(1).LE.10.D0*UROUND) THEN
WRITE (6,*) ' TOLERANCES ARE TOO SMALL'
ARRET=.TRUE.
ELSE
QUOT=ATOL(1)/RTOL(1)
RTOL(1)=0.1D0*RTOL(1)**EXPM
ATOL(1)=RTOL(1)*QUOT
END IF
ELSE
DO I=1,N
IF (ATOL(I).LE.0.D0.OR.RTOL(I).LE.10.D0*UROUND) THEN
WRITE (6,*) ' TOLERANCES(',I,') ARE TOO SMALL'
ARRET=.TRUE.
ELSE
QUOT=ATOL(I)/RTOL(I)
RTOL(I)=0.1D0*RTOL(I)**EXPM
ATOL(I)=RTOL(I)*QUOT
END IF
END DO
END IF
C -------- NMAX , THE MAXIMAL NUMBER OF STEPS -----
IF (IWORK(2).EQ.0) THEN
NMAX=100000
ELSE
NMAX=IWORK(2)
IF (NMAX.LE.0) THEN
WRITE(6,*)' WRONG INPUT IWORK(2)=',IWORK(2)
ARRET=.TRUE.
END IF
END IF
C -------- NIT MAXIMAL NUMBER OF NEWTON ITERATIONS
IF (IWORK(3).EQ.0) THEN
NIT=7
ELSE
NIT=IWORK(3)
IF (NIT.LE.0) THEN
WRITE(6,*)' CURIOUS INPUT IWORK(3)=',IWORK(3)
ARRET=.TRUE.
END IF
END IF
C -------- STARTN SWITCH FOR STARTING VALUES OF NEWTON ITERATIONS
IF(IWORK(4).EQ.0)THEN
STARTN=.FALSE.
ELSE
STARTN=.TRUE.
END IF
C -------- PARAMETER FOR DIFFERENTIAL-ALGEBRAIC COMPONENTS
NIND1=IWORK(5)
NIND2=IWORK(6)
NIND3=IWORK(7)
IF (NIND1.EQ.0) NIND1=N
IF (NIND1+NIND2+NIND3.NE.N) THEN
WRITE(6,*)' CURIOUS INPUT FOR IWORK(5,6,7)=',NIND1,NIND2,NIND3
ARRET=.TRUE.
END IF
C -------- PRED STEP SIZE CONTROL
IF(IWORK(8).LE.1)THEN
PRED=.TRUE.
ELSE
PRED=.FALSE.
END IF
C -------- PARAMETER FOR SECOND ORDER EQUATIONS
M1=IWORK(9)
M2=IWORK(10)
NM1=N-M1
IF (M1.EQ.0) M2=N
IF (M2.EQ.0) M2=M1
IF (M1.LT.0.OR.M2.LT.0.OR.M1+M2.GT.N) THEN
WRITE(6,*)' CURIOUS INPUT FOR IWORK(9,10)=',M1,M2
ARRET=.TRUE.
END IF
C --------- SAFE SAFETY FACTOR IN STEP SIZE PREDICTION
IF (WORK(2).EQ.0.0D0) THEN
SAFE=0.9D0
ELSE
SAFE=WORK(2)
IF (SAFE.LE.0.001D0.OR.SAFE.GE.1.0D0) THEN
WRITE(6,*)' CURIOUS INPUT FOR WORK(2)=',WORK(2)
ARRET=.TRUE.
END IF
END IF
C ------ THET DECIDES WHETHER THE JACOBIAN SHOULD BE RECOMPUTED;
IF (WORK(3).EQ.0.D0) THEN
THET=0.001D0
ELSE
THET=WORK(3)
IF (THET.GE.1.0D0) THEN
WRITE(6,*)' CURIOUS INPUT FOR WORK(3)=',WORK(3)
ARRET=.TRUE.
END IF
END IF
C --- FNEWT STOPPING CRITERION FOR NEWTON'S METHOD, USUALLY CHOSEN <1.
TOLST=RTOL(1)
IF (WORK(4).EQ.0.D0) THEN
FNEWT=MAX(10*UROUND/TOLST,MIN(0.03D0,TOLST**0.5D0))
ELSE
FNEWT=WORK(4)
IF (FNEWT.LE.UROUND/TOLST) THEN
WRITE(6,*)' CURIOUS INPUT FOR WORK(4)=',WORK(4)
ARRET=.TRUE.
END IF
END IF
C --- QUOT1 AND QUOT2: IF QUOT1 < HNEW/HOLD < QUOT2, STEP SIZE = CONST.
IF (WORK(5).EQ.0.D0) THEN
QUOT1=1.D0
ELSE
QUOT1=WORK(5)
END IF
IF (WORK(6).EQ.0.D0) THEN
QUOT2=1.2D0
ELSE
QUOT2=WORK(6)
END IF
IF (QUOT1.GT.1.0D0.OR.QUOT2.LT.1.0D0) THEN
WRITE(6,*)' CURIOUS INPUT FOR WORK(5,6)=',QUOT1,QUOT2
ARRET=.TRUE.
END IF
C -------- MAXIMAL STEP SIZE
IF (WORK(7).EQ.0.D0) THEN
HMAX=XEND-X
ELSE
HMAX=WORK(7)
END IF
C ------- FACL,FACR PARAMETERS FOR STEP SIZE SELECTION
IF(WORK(8).EQ.0.D0)THEN
FACL=5.D0
ELSE
FACL=1.D0/WORK(8)
END IF
IF(WORK(9).EQ.0.D0)THEN
FACR=1.D0/8.0D0
ELSE
FACR=1.D0/WORK(9)
END IF
IF (FACL.LT.1.0D0.OR.FACR.GT.1.0D0) THEN
WRITE(6,*)' CURIOUS INPUT WORK(8,9)=',WORK(8),WORK(9)
ARRET=.TRUE.
END IF
C *** *** *** *** *** *** *** *** *** *** *** *** ***
C COMPUTATION OF ARRAY ENTRIES
C *** *** *** *** *** *** *** *** *** *** *** *** ***
C ---- IMPLICIT, BANDED OR NOT ?
IMPLCT=IMAS.NE.0
JBAND=MLJAC.LT.NM1
C -------- COMPUTATION OF THE ROW-DIMENSIONS OF THE 2-ARRAYS ---
C -- JACOBIAN AND MATRICES E1, E2
IF (JBAND) THEN
LDJAC=MLJAC+MUJAC+1
LDE1=MLJAC+LDJAC
ELSE
MLJAC=NM1
MUJAC=NM1
LDJAC=NM1
LDE1=NM1
END IF
C -- MASS MATRIX
IF (IMPLCT) THEN
IF (MLMAS.NE.NM1) THEN
LDMAS=MLMAS+MUMAS+1
IF (JBAND) THEN
IJOB=4
ELSE
IJOB=3
END IF
ELSE
MUMAS=NM1
LDMAS=NM1
IJOB=5
END IF
C ------ BANDWITH OF "MAS" NOT SMALLER THAN BANDWITH OF "JAC"
IF (MLMAS.GT.MLJAC.OR.MUMAS.GT.MUJAC) THEN
WRITE (6,*) 'BANDWITH OF "MAS" NOT SMALLER THAN BANDWITH OF
& "JAC"'
ARRET=.TRUE.
END IF
ELSE
LDMAS=0
IF (JBAND) THEN
IJOB=2
ELSE
IJOB=1
IF (N.GT.2.AND.IWORK(1).NE.0) IJOB=7
END IF
END IF
LDMAS2=MAX(1,LDMAS)
C ------ HESSENBERG OPTION ONLY FOR EXPLICIT EQU. WITH FULL JACOBIAN
IF ((IMPLCT.OR.JBAND).AND.IJOB.EQ.7) THEN
WRITE(6,*)' HESSENBERG OPTION ONLY FOR EXPLICIT EQUATIONS WITH
&FULL JACOBIAN'
ARRET=.TRUE.
END IF
C ------- PREPARE THE ENTRY-POINTS FOR THE ARRAYS IN WORK -----
IEZ1=21
IEZ2=IEZ1+N
IEZ3=IEZ2+N
IEY0=IEZ3+N
IESCAL=IEY0+N
IEF1=IESCAL+N
IEF2=IEF1+N
IEF3=IEF2+N
IECON=IEF3+N
IEJAC=IECON+4*N
IEMAS=IEJAC+N*LDJAC
IEE1=IEMAS+NM1*LDMAS
IEE2R=IEE1+NM1*LDE1
IEE2I=IEE2R+NM1*LDE1
C ------ TOTAL STORAGE REQUIREMENT -----------
ISTORE=IEE2I+NM1*LDE1-1
IF(ISTORE.GT.LWORK)THEN
WRITE(6,*)' INSUFFICIENT STORAGE FOR WORK, MIN. LWORK=',ISTORE
ARRET=.TRUE.
END IF
C ------- ENTRY POINTS FOR INTEGER WORKSPACE -----
IEIP1=21
IEIP2=IEIP1+NM1
IEIPH=IEIP2+NM1
C --------- TOTAL REQUIREMENT ---------------
ISTORE=IEIPH+NM1-1
IF (ISTORE.GT.LIWORK) THEN
WRITE(6,*)' INSUFF. STORAGE FOR IWORK, MIN. LIWORK=',ISTORE
ARRET=.TRUE.
END IF
C ------ WHEN A FAIL HAS OCCURED, WE RETURN WITH IDID=-1
IF (ARRET) THEN
IDID=-1
RETURN
END IF
C -------- CALL TO CORE INTEGRATOR ------------
CALL RADCOR(N,FCN,X,Y,XEND,HMAX,H,RTOL,ATOL,ITOL,
& JAC,IJAC,MLJAC,MUJAC,MAS,MLMAS,MUMAS,SOLOUT,IOUT,IDID,
& NMAX,UROUND,SAFE,THET,FNEWT,QUOT1,QUOT2,NIT,IJOB,STARTN,
& NIND1,NIND2,NIND3,PRED,FACL,FACR,M1,M2,NM1,
& IMPLCT,JBAND,LDJAC,LDE1,LDMAS2,WORK(IEZ1),WORK(IEZ2),
& WORK(IEZ3),WORK(IEY0),WORK(IESCAL),WORK(IEF1),WORK(IEF2),
& WORK(IEF3),WORK(IEJAC),WORK(IEE1),WORK(IEE2R),WORK(IEE2I),
& WORK(IEMAS),IWORK(IEIP1),IWORK(IEIP2),IWORK(IEIPH),
& WORK(IECON),NFCN,NJAC,NSTEP,NACCPT,NREJCT,NDEC,NSOL,NITMAX,
& RPAR,IPAR)
IWORK(14)=NFCN
IWORK(15)=NJAC
IWORK(16)=NSTEP
IWORK(17)=NACCPT
IWORK(18)=NREJCT
IWORK(19)=NDEC
IWORK(20)=NSOL
IWORK(21)=NITMAX
C -------- RESTORE TOLERANCES
EXPM=1.0D0/EXPM
IF (ITOL.EQ.0) THEN
QUOT=ATOL(1)/RTOL(1)
RTOL(1)=(10.0D0*RTOL(1))**EXPM
ATOL(1)=RTOL(1)*QUOT
ELSE
DO I=1,N
QUOT=ATOL(I)/RTOL(I)
RTOL(I)=(10.0D0*RTOL(I))**EXPM
ATOL(I)=RTOL(I)*QUOT
END DO
END IF
C ----------- RETURN -----------
RETURN
END
C
C END OF SUBROUTINE RADAU5
C
C ***********************************************************
C
SUBROUTINE RADCOR(N,FCN,X,Y,XEND,HMAX,H,RTOL,ATOL,ITOL,
& JAC,IJAC,MLJAC,MUJAC,MAS,MLMAS,MUMAS,SOLOUT,IOUT,IDID,
& NMAX,UROUND,SAFE,THET,FNEWT,QUOT1,QUOT2,NIT,IJOB,STARTN,
& NIND1,NIND2,NIND3,PRED,FACL,FACR,M1,M2,NM1,
& IMPLCT,BANDED,LDJAC,LDE1,LDMAS,Z1,Z2,Z3,
& Y0,SCAL,F1,F2,F3,FJAC,E1,E2R,E2I,FMAS,IP1,IP2,IPHES,
& CONT,NFCN,NJAC,NSTEP,NACCPT,NREJCT,NDEC,NSOL,NITMAX,
& RPAR,IPAR)
C ----------------------------------------------------------
C CORE INTEGRATOR FOR RADAU5
C PARAMETERS SAME AS IN RADAU5 WITH WORKSPACE ADDED
C ----------------------------------------------------------
C DECLARATIONS
C ----------------------------------------------------------
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
DIMENSION Y(N),Z1(N),Z2(N),Z3(N),Y0(N),SCAL(N),F1(N),F2(N),F3(N)
DIMENSION FJAC(LDJAC,N),FMAS(LDMAS,NM1),CONT(4*N)
DIMENSION E1(LDE1,NM1),E2R(LDE1,NM1),E2I(LDE1,NM1)
DIMENSION ATOL(*),RTOL(*),RPAR(*),IPAR(*)
INTEGER IP1(NM1),IP2(NM1),IPHES(NM1)
COMMON /CONRA5/NN,NN2,NN3,NN4,XSOL,HSOL,C2M1,C1M1
COMMON/LINAL/MLE,MUE,MBJAC,MBB,MDIAG,MDIFF,MBDIAG
LOGICAL REJECT,FIRST,IMPLCT,BANDED,CALJAC,STARTN,CALHES
LOGICAL INDEX1,INDEX2,INDEX3,LAST,PRED
EXTERNAL FCN
C *** *** *** *** *** *** ***
C INITIALISATIONS
C *** *** *** *** *** *** ***
C --------- DUPLIFY N FOR COMMON BLOCK CONT -----
NN=N
NN2=2*N
NN3=3*N
LRC=4*N
C -------- CHECK THE INDEX OF THE PROBLEM -----
INDEX1=NIND1.NE.0
INDEX2=NIND2.NE.0
INDEX3=NIND3.NE.0
C ------- COMPUTE MASS MATRIX FOR IMPLICIT CASE ----------
IF (IMPLCT) CALL MAS(NM1,FMAS,LDMAS,RPAR,IPAR)
C ---------- CONSTANTS ---------
SQ6=DSQRT(6.D0)
C1=(4.D0-SQ6)/10.D0
C2=(4.D0+SQ6)/10.D0
C1M1=C1-1.D0
C2M1=C2-1.D0
C1MC2=C1-C2
DD1=-(13.D0+7.D0*SQ6)/3.D0
DD2=(-13.D0+7.D0*SQ6)/3.D0
DD3=-1.D0/3.D0
U1=(6.D0+81.D0**(1.D0/3.D0)-9.D0**(1.D0/3.D0))/30.D0
ALPH=(12.D0-81.D0**(1.D0/3.D0)+9.D0**(1.D0/3.D0))/60.D0
BETA=(81.D0**(1.D0/3.D0)+9.D0**(1.D0/3.D0))*DSQRT(3.D0)/60.D0
CNO=ALPH**2+BETA**2
U1=1.0D0/U1
ALPH=ALPH/CNO
BETA=BETA/CNO
T11=9.1232394870892942792D-02
T12=-0.14125529502095420843D0
T13=-3.0029194105147424492D-02
T21=0.24171793270710701896D0
T22=0.20412935229379993199D0
T23=0.38294211275726193779D0
T31=0.96604818261509293619D0
TI11=4.3255798900631553510D0
TI12=0.33919925181580986954D0
TI13=0.54177053993587487119D0
TI21=-4.1787185915519047273D0
TI22=-0.32768282076106238708D0
TI23=0.47662355450055045196D0
TI31=-0.50287263494578687595D0
TI32=2.5719269498556054292D0
TI33=-0.59603920482822492497D0
IF (M1.GT.0) IJOB=IJOB+10
POSNEG=SIGN(1.D0,XEND-X)
HMAXN=MIN(ABS(HMAX),ABS(XEND-X))
IF (ABS(H).LE.10.D0*UROUND) H=1.0D-6
H=MIN(ABS(H),HMAXN)
H=SIGN(H,POSNEG)
HOLD=H
REJECT=.FALSE.
FIRST=.TRUE.
LAST=.FALSE.
IF ((X+H*1.0001D0-XEND)*POSNEG.GE.0.D0) THEN
H=XEND-X
LAST=.TRUE.
END IF
HOPT=H
FACCON=1.D0
CFAC=SAFE*(1+2*NIT)
NSING=0
XOLD=X
IF (IOUT.NE.0) THEN
IRTRN=1
NRSOL=1
XOSOL=XOLD
XSOL=X
DO I=1,N
CONT(I)=Y(I)
END DO
NSOLU=N
HSOL=HOLD
CALL SOLOUT(NRSOL,XOSOL,XSOL,Y,CONT,LRC,NSOLU,
& RPAR,IPAR,IRTRN)
IF (IRTRN.LT.0) GOTO 179
END IF
MLE=MLJAC
MUE=MUJAC
MBJAC=MLJAC+MUJAC+1
MBB=MLMAS+MUMAS+1
MDIAG=MLE+MUE+1
MDIFF=MLE+MUE-MUMAS
MBDIAG=MUMAS+1
N2=2*N
N3=3*N
IF (ITOL.EQ.0) THEN
DO I=1,N
SCAL(I)=ATOL(1)+RTOL(1)*ABS(Y(I))
END DO
ELSE
DO I=1,N
SCAL(I)=ATOL(I)+RTOL(I)*ABS(Y(I))
END DO
END IF
HHFAC=H
CALL FCN(N,X,Y,Y0,RPAR,IPAR)
NFCN=NFCN+1
C --- BASIC INTEGRATION STEP
10 CONTINUE
C *** *** *** *** *** *** ***
C COMPUTATION OF THE JACOBIAN
C *** *** *** *** *** *** ***
NJAC=NJAC+1
IF (IJAC.EQ.0) THEN
C --- COMPUTE JACOBIAN MATRIX NUMERICALLY
IF (BANDED) THEN
C --- JACOBIAN IS BANDED
MUJACP=MUJAC+1
MD=MIN(MBJAC,M2)
DO MM=1,M1/M2+1
DO K=1,MD
J=K+(MM-1)*M2
12 F1(J)=Y(J)
F2(J)=DSQRT(UROUND*MAX(1.D-5,ABS(Y(J))))
Y(J)=Y(J)+F2(J)
J=J+MD
IF (J.LE.MM*M2) GOTO 12
CALL FCN(N,X,Y,CONT,RPAR,IPAR)
J=K+(MM-1)*M2
J1=K
LBEG=MAX(1,J1-MUJAC)+M1
14 LEND=MIN(M2,J1+MLJAC)+M1
Y(J)=F1(J)
MUJACJ=MUJACP-J1-M1
DO L=LBEG,LEND
FJAC(L+MUJACJ,J)=(CONT(L)-Y0(L))/F2(J)
END DO
J=J+MD
J1=J1+MD
LBEG=LEND+1
IF (J.LE.MM*M2) GOTO 14
END DO
END DO
ELSE
C --- JACOBIAN IS FULL
DO I=1,N
YSAFE=Y(I)
DELT=DSQRT(UROUND*MAX(1.D-5,ABS(YSAFE)))
Y(I)=YSAFE+DELT
CALL FCN(N,X,Y,CONT,RPAR,IPAR)
DO J=M1+1,N
FJAC(J-M1,I)=(CONT(J)-Y0(J))/DELT
END DO
Y(I)=YSAFE
END DO
END IF
ELSE
C --- COMPUTE JACOBIAN MATRIX ANALYTICALLY
CALL JAC(N,X,Y,FJAC,LDJAC,RPAR,IPAR)
END IF
CALJAC=.TRUE.
CALHES=.TRUE.
20 CONTINUE
C --- COMPUTE THE MATRICES E1 AND E2 AND THEIR DECOMPOSITIONS
FAC1=U1/H
ALPHN=ALPH/H
BETAN=BETA/H
CALL DECOMR(N,FJAC,LDJAC,FMAS,LDMAS,MLMAS,MUMAS,
& M1,M2,NM1,FAC1,E1,LDE1,IP1,IER,IJOB,CALHES,IPHES)
IF (IER.NE.0) GOTO 78
CALL DECOMC(N,FJAC,LDJAC,FMAS,LDMAS,MLMAS,MUMAS,
& M1,M2,NM1,ALPHN,BETAN,E2R,E2I,LDE1,IP2,IER,IJOB)
IF (IER.NE.0) GOTO 78
NDEC=NDEC+1
30 CONTINUE
NSTEP=NSTEP+1
IF (NSTEP.GT.NMAX) GOTO 178
IF (0.1D0*ABS(H).LE.ABS(X)*UROUND) GOTO 177
IF (INDEX2) THEN
DO I=NIND1+1,NIND1+NIND2
SCAL(I)=SCAL(I)/HHFAC
END DO
END IF
IF (INDEX3) THEN
DO I=NIND1+NIND2+1,NIND1+NIND2+NIND3
SCAL(I)=SCAL(I)/(HHFAC*HHFAC)
END DO
END IF
XPH=X+H
C *** *** *** *** *** *** ***
C STARTING VALUES FOR NEWTON ITERATION
C *** *** *** *** *** *** ***
IF (FIRST.OR.STARTN) THEN
DO I=1,N
Z1(I)=0.D0
Z2(I)=0.D0
Z3(I)=0.D0
F1(I)=0.D0
F2(I)=0.D0
F3(I)=0.D0
END DO
ELSE
C3Q=H/HOLD
C1Q=C1*C3Q
C2Q=C2*C3Q
DO I=1,N
AK1=CONT(I+N)
AK2=CONT(I+N2)
AK3=CONT(I+N3)
Z1I=C1Q*(AK1+(C1Q-C2M1)*(AK2+(C1Q-C1M1)*AK3))
Z2I=C2Q*(AK1+(C2Q-C2M1)*(AK2+(C2Q-C1M1)*AK3))
Z3I=C3Q*(AK1+(C3Q-C2M1)*(AK2+(C3Q-C1M1)*AK3))
Z1(I)=Z1I
Z2(I)=Z2I
Z3(I)=Z3I
F1(I)=TI11*Z1I+TI12*Z2I+TI13*Z3I
F2(I)=TI21*Z1I+TI22*Z2I+TI23*Z3I
F3(I)=TI31*Z1I+TI32*Z2I+TI33*Z3I
END DO
END IF
C *** *** *** *** *** *** ***
C LOOP FOR THE SIMPLIFIED NEWTON ITERATION
C *** *** *** *** *** *** ***
NEWT=0
FACCON=MAX(FACCON,UROUND)**0.8D0
THETA=ABS(THET)
40 CONTINUE
IF (NEWT.GE.NIT) GOTO 78
C --- COMPUTE THE RIGHT-HAND SIDE
DO I=1,N
CONT(I)=Y(I)+Z1(I)
END DO
CALL FCN(N,X+C1*H,CONT,Z1,RPAR,IPAR)
DO I=1,N
CONT(I)=Y(I)+Z2(I)
END DO
CALL FCN(N,X+C2*H,CONT,Z2,RPAR,IPAR)
DO I=1,N
CONT(I)=Y(I)+Z3(I)
END DO
CALL FCN(N,XPH,CONT,Z3,RPAR,IPAR)
NFCN=NFCN+3
C --- SOLVE THE LINEAR SYSTEMS
DO I=1,N
A1=Z1(I)
A2=Z2(I)
A3=Z3(I)
Z1(I)=TI11*A1+TI12*A2+TI13*A3
Z2(I)=TI21*A1+TI22*A2+TI23*A3
Z3(I)=TI31*A1+TI32*A2+TI33*A3
END DO
CALL SLVRAD(N,FJAC,LDJAC,MLJAC,MUJAC,FMAS,LDMAS,MLMAS,MUMAS,
& M1,M2,NM1,FAC1,ALPHN,BETAN,E1,E2R,E2I,LDE1,Z1,Z2,Z3,
& F1,F2,F3,CONT,IP1,IP2,IPHES,IER,IJOB)
NSOL=NSOL+1
NEWT=NEWT+1
DYNO=0.D0
DO I=1,N
DENOM=SCAL(I)
DYNO=DYNO+(Z1(I)/DENOM)**2+(Z2(I)/DENOM)**2
& +(Z3(I)/DENOM)**2
END DO
DYNO=DSQRT(DYNO/N3)
if (NEWT.GT.NITMAX) NITMAX=NEWT
C --- BAD CONVERGENCE OR NUMBER OF ITERATIONS TO LARGE
IF (NEWT.GT.1.AND.NEWT.LT.NIT) THEN
THQ=DYNO/DYNOLD
IF (NEWT.EQ.2) THEN
THETA=THQ
ELSE
THETA=SQRT(THQ*THQOLD)
END IF
THQOLD=THQ
IF (THETA.LT.0.99D0) THEN
FACCON=THETA/(1.0D0-THETA)
DYTH=FACCON*DYNO*THETA**(NIT-1-NEWT)/FNEWT
IF (DYTH.GE.1.0D0) THEN
QNEWT=DMAX1(1.0D-4,DMIN1(20.0D0,DYTH))
HHFAC=.8D0*QNEWT**(-1.0D0/(4.0D0+NIT-1-NEWT))
H=HHFAC*H
REJECT=.TRUE.
LAST=.FALSE.
IF (CALJAC) GOTO 20
GOTO 10
END IF
ELSE
GOTO 78
END IF
END IF
DYNOLD=MAX(DYNO,UROUND)
DO I=1,N
F1I=F1(I)+Z1(I)
F2I=F2(I)+Z2(I)
F3I=F3(I)+Z3(I)
F1(I)=F1I
F2(I)=F2I
F3(I)=F3I
Z1(I)=T11*F1I+T12*F2I+T13*F3I
Z2(I)=T21*F1I+T22*F2I+T23*F3I
Z3(I)=T31*F1I+ F2I
END DO
IF (FACCON*DYNO.GT.FNEWT) GOTO 40
C --- ERROR ESTIMATION
CALL ESTRAD (N,FJAC,LDJAC,MLJAC,MUJAC,FMAS,LDMAS,MLMAS,MUMAS,
& H,DD1,DD2,DD3,FCN,NFCN,Y0,Y,IJOB,X,M1,M2,NM1,
& E1,LDE1,Z1,Z2,Z3,CONT,F1,F2,IP1,IPHES,SCAL,ERR,
& FIRST,REJECT,FAC1,RPAR,IPAR)
C --- COMPUTATION OF HNEW
C --- WE REQUIRE .2<=HNEW/H<=8.
FAC=MIN(SAFE,CFAC/(NEWT+2*NIT))
QUOT=MAX(FACR,MIN(FACL,ERR**.25D0/FAC))