forked from Ermentrout/xppaut
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cvode.c
executable file
·2402 lines (1857 loc) · 69.7 KB
/
cvode.c
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
/******************************************************************
* *
* File : cvode.c *
* Programmers : Scott D. Cohen and Alan C. Hindmarsh @ LLNL *
* Last Modified : 1 September 1994 *
*----------------------------------------------------------------*
* This is the implementation file for the main CVODE integrator. *
* It is independent of the CVODE linear solver in use. *
* *
******************************************************************/
/************************************************************/
/******************* BEGIN Imports **************************/
/************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "cvode.h"
#include "llnltyps.h"
#include "vector.h"
#include "llnlmath.h"
/************************************************************/
/******************** END Imports ***************************/
/************************************************************/
/***************************************************************/
/*********************** BEGIN Macros **************************/
/***************************************************************/
/* Macro: loop */
#define loop for(;;)
/***************************************************************/
/************************ END Macros ***************************/
/***************************************************************/
/************************************************************/
/************** BEGIN CVODE Private Constants ***************/
/************************************************************/
#define HALF RCONST(0.5) /* real 0.5 */
#define ZERO RCONST(0.0) /* real 0.0 */
#define ONE RCONST(1.0) /* real 1.0 */
#define TWO RCONST(2.0) /* real 2.0 */
#define TWELVE RCONST(12.0) /* real 12.0 */
/***************************************************************/
/************** BEGIN Default Constants ************************/
/***************************************************************/
#define HMIN_DEFAULT ZERO /* hmin default value */
#define HMAX_INV_DEFAULT ZERO /* hmax_inv default value */
#define MXHNIL_DEFAULT 10 /* mxhnil default value */
#define MXSTEP_DEFAULT 2000 /* mxstep default value */
/***************************************************************/
/*************** END Default Constants *************************/
/***************************************************************/
/***************************************************************/
/************ BEGIN Routine-Specific Constants *****************/
/***************************************************************/
/* CVodeDky */
#define FUZZ_FACTOR RCONST(100.0)
/* CVHin */
#define HLB_FACTOR RCONST(100.0)
#define HUB_FACTOR RCONST(0.1)
#define H_BIAS HALF
#define MAX_ITERS 4
/* CVSet */
#define CORTES RCONST(0.1)
/* CVStep return values */
#define SUCCESS_STEP 0
#define REP_ERR_FAIL -1
#define REP_CONV_FAIL -2
#define SETUP_FAILED -3
#define SOLVE_FAILED -4
/* CVStep control constants */
#define PREDICT_AGAIN -5
#define DO_ERROR_TEST 1
/* CVStep */
#define THRESH RCONST(1.5)
#define ETAMX1 RCONST(10000.0)
#define ETAMX2 RCONST(10.0)
#define ETAMX3 RCONST(10.0)
#define ETAMXF RCONST(0.2)
#define ETAMIN RCONST(0.1)
#define ETACF RCONST(0.25)
#define ADDON RCONST(0.000001)
#define BIAS1 RCONST(6.0)
#define BIAS2 RCONST(6.0)
#define BIAS3 RCONST(10.0)
#define ONEPSM RCONST(1.000001)
#define SMALL_NST 10 /* nst > SMALL_NST => use ETAMX3 */
#define MXNCF 10 /* max no. of convergence failures during */
/* one step try */
#define MXNEF 7 /* max no. of error test failures during */
/* one step try */
#define MXNEF1 3 /* max no. of error test failures before */
/* forcing a reduction of order */
#define SMALL_NEF 2 /* if an error failure occurs and */
/* SMALL_NEF <= nef <= MXNEF1, then */
/* reset eta = MIN(eta, ETAMXF) */
#define LONG_WAIT 10 /* number of steps to wait before */
/* considering an order change when */
/* q==1 and MXNEF1 error test failures */
/* have occurred */
/* CVnls return values */
#define SOLVED 0
#define CONV_FAIL -1
#define SETUP_FAIL_UNREC -2
#define SOLVE_FAIL_UNREC -3
/* CVnls input flags */
#define FIRST_CALL 0
#define PREV_CONV_FAIL -1
#define PREV_ERR_FAIL -2
/* CVnls other constants */
#define FUNC_MAXCOR 3 /* maximum no. of corrector iterations */
/* for iter == FUNCTIONAL */
#define NEWT_MAXCOR 3 /* maximum no. of corrector iterations */
/* for iter == NEWTON */
#define CRDOWN RCONST(0.3) /* constant used in the estimation of the */
/* convergence rate (crate) of the */
/* iterates for the nonlinear equation */
#define DGMAX RCONST(0.3) /* iter == NEWTON, |gamma/gammap-1| > DGMAX */
/* => call lsetup */
#define RDIV TWO /* declare divergence if ratio del/delp > RDIV */
#define MSBP 20 /* max no. of steps between lsetup calls */
#define TRY_AGAIN 99 /* control constant for CVnlsNewton - should be */
/* distinct from CVnls return values */
/***************************************************************/
/*************** END Routine-Specific Constants ***************/
/***************************************************************/
/***************************************************************/
/***************** BEGIN Error Messages ************************/
/***************************************************************/
/* CVodeMalloc Error Messages */
#define CVM "CVodeMalloc-- "
#define MSG_Y0_NULL CVM "y0=NULL illegal.\n\n"
#define MSG_BAD_N CVM "N=%ld < 1 illegal.\n\n"
#define MSG_BAD_LMM_1 CVM "lmm=%d illegal.\n"
#define MSG_BAD_LMM_2 "The legal values are ADAMS=%d and BDF=%d.\n\n"
#define MSG_BAD_LMM MSG_BAD_LMM_1 MSG_BAD_LMM_2
#define MSG_BAD_ITER_1 CVM "iter=%d illegal.\n"
#define MSG_BAD_ITER_2 "The legal values are FUNCTIONAL=%d "
#define MSG_BAD_ITER_3 "and NEWTON=%d.\n\n"
#define MSG_BAD_ITER MSG_BAD_ITER_1 MSG_BAD_ITER_2 MSG_BAD_ITER_3
#define MSG_BAD_ITOL_1 CVM "itol=%d illegal.\n"
#define MSG_BAD_ITOL_2 "The legal values are SS=%d and SV=%d.\n\n"
#define MSG_BAD_ITOL MSG_BAD_ITOL_1 MSG_BAD_ITOL_2
#define MSG_F_NULL CVM "f=NULL illegal.\n\n"
#define MSG_RELTOL_NULL CVM "reltol=NULL illegal.\n\n"
#define MSG_BAD_RELTOL CVM "*reltol=%g < 0 illegal.\n\n"
#define MSG_ABSTOL_NULL CVM "abstol=NULL illegal.\n\n"
#define MSG_BAD_ABSTOL CVM "Some abstol component < 0.0 illegal.\n\n"
#define MSG_BAD_OPTIN_1 CVM "optIn=%d illegal.\n"
#define MSG_BAD_OPTIN_2 "The legal values are FALSE=%d and TRUE=%d.\n\n"
#define MSG_BAD_OPTIN MSG_BAD_OPTIN_1 MSG_BAD_OPTIN_2
#define MSG_BAD_OPT CVM "optIn=TRUE, but iopt=ropt=NULL.\n\n"
#define MSG_BAD_HMIN_HMAX_1 CVM "Inconsistent step size limits:\n"
#define MSG_BAD_HMIN_HMAX_2 "ropt[HMIN]=%g > ropt[HMAX]=%g.\n\n"
#define MSG_BAD_HMIN_HMAX MSG_BAD_HMIN_HMAX_1 MSG_BAD_HMIN_HMAX_2
#define MSG_MEM_FAIL CVM "A memory request failed.\n\n"
#define MSG_BAD_EWT CVM "Some initial ewt component = 0.0 illegal.\n\n"
/* CVode error messages */
#define CVODE "CVode-- "
#define NO_MEM "cvode_mem=NULL illegal.\n\n"
#define MSG_CVODE_NO_MEM CVODE NO_MEM
#define MSG_LINIT_NULL CVODE "The linear solver's init routine is NULL.\n\n"
#define MSG_LSETUP_NULL CVODE "The linear solver's setup routine is NULL.\n\n"
#define MSG_LSOLVE_NULL CVODE "The linear solver's solve routine is NULL.\n\n"
#define MSG_LFREE_NULL CVODE "The linear solver's free routine is NULL.\n\n"
#define MSG_LINIT_FAIL CVODE "The linear solver's init routine failed.\n\n"
#define MSG_YOUT_NULL CVODE "yout=NULL illegal.\n\n"
#define MSG_T_NULL CVODE "t=NULL illegal.\n\n"
#define MSG_BAD_ITASK_1 CVODE "itask=%d illegal.\nThe legal values are"
#define MSG_BAD_ITASK_2 " NORMAL=%d and ONE_STEP=%d.\n\n"
#define MSG_BAD_ITASK MSG_BAD_ITASK_1 MSG_BAD_ITASK_2
#define MSG_BAD_H0 CVODE "h0=%g and tout-t0=%g inconsistent.\n\n"
#define MSG_BAD_TOUT_1 CVODE "Trouble interpolating at tout = %g.\n"
#define MSG_BAD_TOUT_2 "tout too far back in direction of integration.\n\n"
#define MSG_BAD_TOUT MSG_BAD_TOUT_1 MSG_BAD_TOUT_2
#define MSG_MAX_STEPS_1 CVODE "At t=%g, mxstep=%d steps taken on "
#define MSG_MAX_STEPS_2 "this call before\nreaching tout=%g.\n\n"
#define MSG_MAX_STEPS MSG_MAX_STEPS_1 MSG_MAX_STEPS_2
#define MSG_EWT_NOW_BAD_1 CVODE "At t=%g, "
#define MSG_EWT_NOW_BAD_2 "some ewt component has become <= 0.0.\n\n"
#define MSG_EWT_NOW_BAD MSG_EWT_NOW_BAD_1 MSG_EWT_NOW_BAD_2
#define MSG_TOO_MUCH_ACC CVODE "At t=%g, too much accuracy requested.\n\n"
#define MSG_HNIL_1 CVODE "Warning.. internal t=%g and step size h=%g\n"
#define MSG_HNIL_2 "are such that t + h == t on the next step.\n"
#define MSG_HNIL_3 "The solver will continue anyway.\n\n"
#define MSG_HNIL MSG_HNIL_1 MSG_HNIL_2 MSG_HNIL_3
#define MSG_HNIL_DONE_1 CVODE "The above warning has been issued %d times "
#define MSG_HNIL_DONE_2 "and will not be\nissued again for this problem.\n\n"
#define MSG_HNIL_DONE MSG_HNIL_DONE_1 MSG_HNIL_DONE_2
#define MSG_ERR_FAILS_1 CVODE "At t=%g and step size h=%g, the error test\n"
#define MSG_ERR_FAILS_2 "failed repeatedly or with |h| = hmin.\n\n"
#define MSG_ERR_FAILS MSG_ERR_FAILS_1 MSG_ERR_FAILS_2
#define MSG_CONV_FAILS_1 CVODE "At t=%g and step size h=%g, the corrector\n"
#define MSG_CONV_FAILS_2 "convergence failed repeatedly or "
#define MSG_CONV_FAILS_3 "with |h| = hmin.\n\n"
#define MSG_CONV_FAILS MSG_CONV_FAILS_1 MSG_CONV_FAILS_2 MSG_CONV_FAILS_3
#define MSG_SETUP_FAILED_1 CVODE "At t=%g, the setup routine failed in an "
#define MSG_SETUP_FAILED_2 "unrecoverable manner.\n\n"
#define MSG_SETUP_FAILED MSG_SETUP_FAILED_1 MSG_SETUP_FAILED_2
#define MSG_SOLVE_FAILED_1 CVODE "At t=%g, the solve routine failed in an "
#define MSG_SOLVE_FAILED_2 "unrecoverable manner.\n\n"
#define MSG_SOLVE_FAILED MSG_SOLVE_FAILED_1 MSG_SOLVE_FAILED_2
#define MSG_TOO_CLOSE_1 CVODE "tout=%g too close to t0=%g to start"
#define MSG_TOO_CLOSE_2 " integration.\n\n"
#define MSG_TOO_CLOSE MSG_TOO_CLOSE_1 MSG_TOO_CLOSE_2
/* CVodeDky Error Messages */
#define DKY "CVodeDky-- "
#define MSG_DKY_NO_MEM DKY NO_MEM
#define MSG_BAD_K DKY "k=%d illegal.\n\n"
#define MSG_BAD_T_1 DKY "t=%g illegal.\n"
#define MSG_BAD_T_2 "t not in interval tcur-hu=%g to tcur=%g.\n\n"
#define MSG_BAD_T MSG_BAD_T_1 MSG_BAD_T_2
#define MSG_BAD_DKY DKY "dky=NULL illegal.\n\n"
/***************************************************************/
/****************** END Error Messages *************************/
/***************************************************************/
/************************************************************/
/*************** END CVODE Private Constants ****************/
/************************************************************/
/**************************************************************/
/********* BEGIN Private Helper Functions Prototypes **********/
/**************************************************************/
static bool CVAllocVectors(CVodeMem cv_mem, integer neq, int maxord,
void *machEnv);
static void CVFreeVectors(CVodeMem cv_mem, int maxord);
static bool CVEwtSet(CVodeMem cv_mem, real *rtol, void *atol, int tol_type,
N_Vector ycur, N_Vector ewtvec, integer neq);
static bool CVEwtSetSS(CVodeMem cv_mem, real *rtol, real *atol,
N_Vector ycur, N_Vector ewtvec, integer neq);
static bool CVEwtSetSV(CVodeMem cv_mem, real *rtol, N_Vector atol,
N_Vector ycur, N_Vector ewtvec, integer neq);
static bool CVHin(CVodeMem cv_mem, real tout);
static real CVUpperBoundH0(CVodeMem cv_mem, real tdist);
static real CVYddNorm(CVodeMem cv_mem, real hg);
static int CVStep(CVodeMem cv_mem);
static void CVAdjustParams(CVodeMem cv_mem);
static void CVAdjustOrder(CVodeMem cv_mem, int deltaq);
static void CVAdjustAdams(CVodeMem cv_mem, int deltaq);
static void CVAdjustBDF(CVodeMem cv_mem, int deltaq);
static void CVIncreaseBDF(CVodeMem cv_mem);
static void CVDecreaseBDF(CVodeMem cv_mem);
static void CVRescale(CVodeMem cv_mem);
static void CVPredict(CVodeMem cv_mem);
static void CVSet(CVodeMem cv_mem);
static void CVSetAdams(CVodeMem cv_mem);
static real CVAdamsStart(CVodeMem cv_mem, real m[]);
static void CVAdamsFinish(CVodeMem cv_mem, real m[], real M[], real hsum);
static real CVAltSum(int iend, real a[], int k);
static void CVSetBDF(CVodeMem cv_mem);
static void CVSetTqBDF(CVodeMem cv_mem, real hsum, real alpha0,
real alpha0_hat, real xi_inv, real xistar_inv);
static int CVnls(CVodeMem cv_mem, int nflag);
static int CVnlsFunctional(CVodeMem cv_mem);
static int CVnlsNewton(CVodeMem cv_mem, int nflag);
static int CVNewtonIteration(CVodeMem cv_mem);
static int CVHandleNFlag(CVodeMem cv_mem, int *nflagPtr, real saved_t,
int *ncfPtr);
static void CVRestore(CVodeMem cv_mem, real saved_t);
static bool CVDoErrorTest(CVodeMem cv_mem, int *nflagPtr, int *kflagPtr,
real saved_t, int *nefPtr, real *dsmPtr);
static void CVCompleteStep(CVodeMem cv_mem);
static void CVPrepareNextStep(CVodeMem cv_mem, real dsm);
static void CVSetEta(CVodeMem cv_mem);
static real CVComputeEtaqm1(CVodeMem cv_mem);
static real CVComputeEtaqp1(CVodeMem cv_mem);
static void CVChooseEta(CVodeMem cv_mem,real etaqm1, real etaq, real etaqp1);
static int CVHandleFailure(CVodeMem cv_mem,int kflag);
/**************************************************************/
/********** END Private Helper Functions Prototypes ***********/
/**************************************************************/
/**************************************************************/
/**************** BEGIN Readability Constants *****************/
/**************************************************************/
#define uround (cv_mem->cv_uround)
#define zn (cv_mem->cv_zn)
#define ewt (cv_mem->cv_ewt)
#define y (cv_mem->cv_y)
#define acor (cv_mem->cv_acor)
#define tempv (cv_mem->cv_tempv)
#define ftemp (cv_mem->cv_ftemp)
#define q (cv_mem->cv_q)
#define qprime (cv_mem->cv_qprime)
#define qwait (cv_mem->cv_qwait)
#define L (cv_mem->cv_L)
#define h (cv_mem->cv_h)
#define hprime (cv_mem->cv_hprime)
#define eta (cv_mem-> cv_eta)
#define hscale (cv_mem->cv_hscale)
#define tn (cv_mem->cv_tn)
#define tau (cv_mem->cv_tau)
#define tq (cv_mem->cv_tq)
#define l (cv_mem->cv_l)
#define rl1 (cv_mem->cv_rl1)
#define gamma (cv_mem->cv_gamma)
#define gammap (cv_mem->cv_gammap)
#define gamrat (cv_mem->cv_gamrat)
#define crate (cv_mem->cv_crate)
#define acnrm (cv_mem->cv_acnrm)
#define mnewt (cv_mem->cv_mnewt)
#define qmax (cv_mem->cv_qmax)
#define mxstep (cv_mem->cv_mxstep)
#define maxcor (cv_mem->cv_maxcor)
#define mxhnil (cv_mem->cv_mxhnil)
#define hmin (cv_mem->cv_hmin)
#define hmax_inv (cv_mem->cv_hmax_inv)
#define etamax (cv_mem->cv_etamax)
#define nst (cv_mem->cv_nst)
#define nfe (cv_mem->cv_nfe)
#define ncfn (cv_mem->cv_ncfn)
#define netf (cv_mem->cv_netf)
#define nni (cv_mem-> cv_nni)
#define nsetups (cv_mem->cv_nsetups)
#define nhnil (cv_mem->cv_nhnil)
#define lrw (cv_mem->cv_lrw)
#define liw (cv_mem->cv_liw)
#define linit (cv_mem->cv_linit)
#define lsetup (cv_mem->cv_lsetup)
#define lsolve (cv_mem->cv_lsolve)
#define lfree (cv_mem->cv_lfree)
#define lmem (cv_mem->cv_lmem)
#define linitOK (cv_mem->cv_linitOK)
#define qu (cv_mem->cv_qu)
#define nstlp (cv_mem->cv_nstlp)
#define hu (cv_mem->cv_hu)
#define saved_tq5 (cv_mem->cv_saved_tq5)
#define jcur (cv_mem->cv_jcur)
#define tolsf (cv_mem->cv_tolsf)
#define setupNonNull (cv_mem->cv_setupNonNull)
#define machenv (cv_mem->cv_machenv)
/**************************************************************/
/***************** END Readability Constants ******************/
/**************************************************************/
/***************************************************************/
/************* BEGIN CVODE Implementation **********************/
/***************************************************************/
/***************************************************************/
/********* BEGIN Exported Functions Implementation *************/
/***************************************************************/
/******************** CVodeMalloc *******************************
CVode Malloc allocates and initializes memory for a problem. All
problem specification inputs are checked for errors. If any
error occurs during initialization, it is reported to the file
whose file pointer is errfp and NULL is returned. Otherwise, the
pointer to successfully initialized problem memory is returned.
*****************************************************************/
void *CVodeMalloc(integer N, RhsFn f, real t0, N_Vector y0, int lmm, int iter,
int itol, real *reltol, void *abstol, void *f_data,
FILE *errfp, bool optIn, int iopt[], real ropt[],
void *machEnv)
{
bool allocOK, ioptExists, roptExists, neg_abstol, ewtsetOK;
int maxord;
CVodeMem cv_mem;
FILE *fp;
/* Check for legal input parameters */
fp = (errfp == NULL) ? stdout : errfp;
if (y0==NULL) {
fprintf(fp, MSG_Y0_NULL);
return(NULL);
}
if (N <= 0) {
fprintf(fp, MSG_BAD_N, (long int)N);
return(NULL);
}
if ((lmm != ADAMS) && (lmm != BDF)) {
fprintf(fp, MSG_BAD_LMM, lmm, ADAMS, BDF);
return(NULL);
}
if ((iter != FUNCTIONAL) && (iter != NEWTON)) {
fprintf(fp, MSG_BAD_ITER, iter, FUNCTIONAL, NEWTON);
return(NULL);
}
if ((itol != SS) && (itol != SV)) {
fprintf(fp, MSG_BAD_ITOL, itol, SS, SV);
return(NULL);
}
if (f == NULL) {
fprintf(fp, MSG_F_NULL);
return(NULL);
}
if (reltol == NULL) {
fprintf(fp, MSG_RELTOL_NULL);
return(NULL);
}
if (*reltol < ZERO) {
fprintf(fp, MSG_BAD_RELTOL, *reltol);
return(NULL);
}
if (abstol == NULL) {
fprintf(fp, MSG_ABSTOL_NULL);
return(NULL);
}
if (itol == SS) {
neg_abstol = (*((real *)abstol) < ZERO);
} else {
neg_abstol = (N_VMin((N_Vector)abstol) < ZERO);
}
if (neg_abstol) {
fprintf(fp, MSG_BAD_ABSTOL);
return(NULL);
}
if ((optIn != FALSE) && (optIn != TRUE)) {
fprintf(fp, MSG_BAD_OPTIN, optIn, FALSE, TRUE);
return(NULL);
}
if ((optIn) && (iopt == NULL) && (ropt == NULL)) {
fprintf(fp, MSG_BAD_OPT);
return(NULL);
}
ioptExists = (iopt != NULL);
roptExists = (ropt != NULL);
if (optIn && roptExists) {
if ((ropt[HMAX] > ZERO) && (ropt[HMIN] > ropt[HMAX])) {
fprintf(fp, MSG_BAD_HMIN_HMAX, ropt[HMIN], ropt[HMAX]);
return(NULL);
}
}
/* compute maxord */
maxord = (lmm == ADAMS) ? ADAMS_Q_MAX : BDF_Q_MAX;
if (optIn && ioptExists) {
if (iopt[MAXORD] > 0) maxord = MIN(maxord, iopt[MAXORD]);
}
cv_mem = (CVodeMem) malloc(sizeof(struct CVodeMemRec));
if (cv_mem == NULL) {
fprintf(fp, MSG_MEM_FAIL);
return(NULL);
}
/* Allocate the vectors */
allocOK = CVAllocVectors(cv_mem, N, maxord, machEnv);
if (!allocOK) {
fprintf(fp, MSG_MEM_FAIL);
free(cv_mem);
return(NULL);
}
/* Set the ewt vector */
ewtsetOK = CVEwtSet(cv_mem, reltol, abstol, itol, y0, ewt, N);
if (!ewtsetOK) {
fprintf(fp, MSG_BAD_EWT);
CVFreeVectors(cv_mem, maxord);
free(cv_mem);
return(NULL);
}
/* All error checking is complete at this point */
/* Copy the input parameters into CVODE state */
cv_mem->cv_N = N; /* readability constants defined below CVodeMalloc */
cv_mem->cv_f = f;
cv_mem->cv_f_data = f_data;
cv_mem->cv_lmm = lmm;
cv_mem->cv_iter = iter;
cv_mem->cv_itol = itol;
cv_mem->cv_reltol = reltol;
cv_mem->cv_abstol = abstol;
cv_mem->cv_iopt = iopt;
cv_mem->cv_ropt = ropt;
cv_mem->cv_errfp = fp;
tn = t0;
machenv = machEnv;
/* Set step parameters */
q = 1;
L = 2;
qwait = L;
qmax = maxord;
etamax = ETAMX1;
/* Set uround */
uround = UnitRoundoff();
/* Set the linear solver addresses to NULL, linitOK to FALSE */
linit = NULL;
lsetup = NULL;
lsolve = NULL;
lfree = NULL;
lmem = NULL;
/* We check != NULL later, in CVode and linit, if using NEWTON */
linitOK = FALSE;
/* Initialize the history array zn */
N_VScale(ONE, y0, zn[0]);
f(N, t0, y0, zn[1], f_data);
nfe = 1;
/* Handle the remaining optional inputs */
hmin = HMIN_DEFAULT;
hmax_inv = HMAX_INV_DEFAULT;
if (optIn && roptExists) {
if (ropt[HMIN] > ZERO) hmin = ropt[HMIN];
if (ropt[HMAX] > ZERO) hmax_inv = ONE/ropt[HMAX];
}
mxhnil = MXHNIL_DEFAULT;
mxstep = MXSTEP_DEFAULT;
if (optIn && ioptExists) {
if (iopt[MXHNIL] > 0) mxhnil = iopt[MXHNIL];
if (iopt[MXSTEP] > 0) mxstep = iopt[MXSTEP];
}
if ((!optIn) && roptExists) ropt[H0] = ZERO;
/* Set maxcor */
maxcor = (iter==NEWTON) ? NEWT_MAXCOR : FUNC_MAXCOR;
/* Initialize all the counters */
nst = ncfn = netf = nni = nsetups = nhnil = nstlp = 0;
/* Initialize all other vars corresponding to optional outputs */
qu = 0;
hu = ZERO;
tolsf = ONE;
/* Initialize optional output locations in iopt, ropt */
if (ioptExists) {
iopt[NST] = iopt[NFE] = iopt[NSETUPS] = iopt[NNI] = 0;
iopt[NCFN] = iopt[NETF] = 0;
iopt[QU] = qu;
iopt[QCUR] = 0;
iopt[LENRW] = lrw;
iopt[LENIW] = liw;
}
if (roptExists) {
ropt[HU] = hu;
ropt[HCUR] = ZERO;
ropt[TCUR] = t0;
ropt[TOLSF] = tolsf;
}
/* Problem has been successfully initialized */
return((void *)cv_mem);
}
/**************************************************************/
/************** BEGIN More Readability Constants **************/
/**************************************************************/
#define N (cv_mem->cv_N)
#define f (cv_mem->cv_f)
#define f_data (cv_mem->cv_f_data)
#define lmm (cv_mem->cv_lmm)
#define iter (cv_mem->cv_iter)
#define itol (cv_mem->cv_itol)
#define reltol (cv_mem->cv_reltol)
#define abstol (cv_mem->cv_abstol)
#define iopt (cv_mem->cv_iopt)
#define ropt (cv_mem->cv_ropt)
#define errfp (cv_mem->cv_errfp)
/**************************************************************/
/*************** END More Readability Constants ***************/
/**************************************************************/
/********************* CVode ****************************************
This routine is the main driver of the CVODE package.
It integrates over a time interval defined by the user, by calling
CVStep to do internal time steps.
The first time that CVode is called for a successfully initialized
problem, it computes a tentative initial step size h.
CVode supports two modes, specified by itask: NORMAL and ONE_STEP.
In the NORMAL mode, the solver steps until it reaches or passes tout
and then interpolates to obtain y(tout).
In the ONE_STEP mode, it takes one internal step and returns.
********************************************************************/
int CVode(void *cvode_mem, real tout, N_Vector yout, real *t, int itask)
{
int nstloc, kflag, istate, next_q, ier;
real rh, next_h;
bool hOK, ewtsetOK;
CVodeMem cv_mem;
/* Check for legal inputs in all cases */
cv_mem = (CVodeMem) cvode_mem;
if (cvode_mem == NULL) {
fprintf(stdout, MSG_CVODE_NO_MEM);
return(CVODE_NO_MEM);
}
if ((y = yout) == NULL) {
fprintf(errfp, MSG_YOUT_NULL);
return(ILL_INPUT);
}
if (t == NULL) {
fprintf(errfp, MSG_T_NULL);
return(ILL_INPUT);
}
*t = tn;
if ((itask != NORMAL) && (itask != ONE_STEP)) {
fprintf(errfp, MSG_BAD_ITASK, itask, NORMAL, ONE_STEP);
return(ILL_INPUT);
}
/* On first call, check solver functions and call linit function */
if (nst == 0) {
if (iter == NEWTON) {
if (linit == NULL) {
fprintf(errfp, MSG_LINIT_NULL);
return(ILL_INPUT);
}
if (lsetup == NULL) {
fprintf(errfp, MSG_LSETUP_NULL);
return(ILL_INPUT);
}
if (lsolve == NULL) {
fprintf(errfp, MSG_LSOLVE_NULL);
return(ILL_INPUT);
}
if (lfree == NULL) {
fprintf(errfp, MSG_LFREE_NULL);
return(ILL_INPUT);
}
linitOK = (linit(cv_mem, &(setupNonNull)) == LINIT_OK);
if (!linitOK) {
fprintf(errfp, MSG_LINIT_FAIL);
return(ILL_INPUT);
}
}
/* On first call, set initial h (from H0 or CVHin) and scale zn[1] */
h = ZERO;
if (ropt != NULL) h = ropt[H0];
if ( (h != ZERO) && ((tout-tn)*h < ZERO) ) {
fprintf(errfp, MSG_BAD_H0, h, tout-tn);
return(ILL_INPUT);
}
if (h == ZERO) {
hOK = CVHin(cv_mem, tout);
if (!hOK) {
fprintf(errfp, MSG_TOO_CLOSE, tout, tn);
return(ILL_INPUT);
}
}
rh = ABS(h)*hmax_inv;
if (rh > ONE) h /= rh;
if (ABS(h) < hmin) h *= hmin/ABS(h);
hscale = h;
N_VScale(h, zn[1], zn[1]);
}
/* If not the first call, check if tout already reached */
if ( (itask == NORMAL) && (nst > 0) && ((tn-tout)*h >= ZERO) ) {
*t = tout;
ier = CVodeDky(cv_mem, tout, 0, yout);
if (ier != OKAY) { /* ier must be == BAD_T */
fprintf(errfp, MSG_BAD_TOUT, tout);
return(ILL_INPUT);
}
return(SUCCESS);
}
/* Looping point for internal steps */
nstloc = 0;
loop {
next_h = h;
next_q = q;
/* Reset and check ewt */
if (nst > 0) {
ewtsetOK = CVEwtSet(cv_mem, reltol, abstol, itol, zn[0], ewt, N);
if (!ewtsetOK) {
fprintf(errfp, MSG_EWT_NOW_BAD, tn);
istate = ILL_INPUT;
*t = tn;
N_VScale(ONE, zn[0], yout);
break;
}
}
/* Check for too many steps */
if (nstloc >= mxstep) {
fprintf(errfp, MSG_MAX_STEPS, tn, mxstep, tout);
istate = TOO_MUCH_WORK;
*t = tn;
N_VScale(ONE, zn[0], yout);
break;
}
/* Check for too much accuracy requested */
if ((tolsf = uround * N_VWrmsNorm(zn[0], ewt)) > ONE) {
fprintf(errfp, MSG_TOO_MUCH_ACC, tn);
istate = TOO_MUCH_ACC;
*t = tn;
N_VScale(ONE, zn[0], yout);
tolsf *= TWO;
break;
}
/* Check for h below roundoff level in tn */
if (tn + h == tn) {
nhnil++;
if (nhnil <= mxhnil) fprintf(errfp, MSG_HNIL, tn, h);
if (nhnil == mxhnil) fprintf(errfp, MSG_HNIL_DONE, mxhnil);
}
/* Call CVStep to take a step */
kflag = CVStep(cv_mem);
/* Process failed step cases, and exit loop */
if (kflag != SUCCESS_STEP) {
istate = CVHandleFailure(cv_mem, kflag);
*t = tn;
N_VScale(ONE, zn[0], yout);
break;
}
nstloc++;
/* Check if in one-step mode, and if so copy y and exit loop */
if (itask == ONE_STEP) {
istate = SUCCESS;
*t = tn;
N_VScale(ONE, zn[0], yout);
next_q = qprime;
next_h = hprime;
break;
}
/* Check if tout reached, and if so interpolate and exit loop */
if ((tn-tout)*h >= ZERO) {
istate = SUCCESS;
*t = tout;
(void) CVodeDky(cv_mem, tout, 0, yout);
next_q = qprime;
next_h = hprime;
break;
}
}
/* End of step loop; load optional outputs and return */
if (iopt != NULL) {
iopt[NST] = nst;
iopt[NFE] = nfe;
iopt[NSETUPS] = nsetups;
iopt[NNI] = nni;
iopt[NCFN] = ncfn;
iopt[NETF] = netf;
iopt[QU] = q;
iopt[QCUR] = next_q;
}
if (ropt != NULL) {
ropt[HU] = h;
ropt[HCUR] = next_h;
ropt[TCUR] = tn;
ropt[TOLSF] = tolsf;
}
return(istate);
}
/*************** CVodeDky ********************************************
This routine computes the k-th derivative of the interpolating
polynomial at the time t and stores the result in the vector dky.
The formula is:
q
dky = SUM c(j,k) * (t - tn)^(j-k) * h^(-j) * zn[j] ,
j=k
where c(j,k) = j*(j-1)*...*(j-k+1), q is the current order, and
zn[j] is the j-th column of the Nordsieck history array.
This function is called by CVode with k = 0 and t = tout, but
may also be called directly by the user.
**********************************************************************/
int CVodeDky(void *cvode_mem, real t, int k, N_Vector dky)
{
real s, c, r;
real tfuzz, tp, tn1;
int i, j;
CVodeMem cv_mem;
cv_mem = (CVodeMem) cvode_mem;
/* Check all inputs for legality */
if (cvode_mem == NULL) {
fprintf(stdout, MSG_DKY_NO_MEM);
return(DKY_NO_MEM);
}
if (dky == NULL) {
fprintf(stdout, MSG_BAD_DKY);
return(BAD_DKY);
}
if ((k < 0) || (k > q)) {
fprintf(errfp, MSG_BAD_K, k);
return(BAD_K);
}
tfuzz = FUZZ_FACTOR * uround * (tn + hu);
tp = tn - hu - tfuzz;
tn1 = tn + tfuzz;
if ((t-tp)*(t-tn1) > ZERO) {
fprintf(errfp, MSG_BAD_T, t, tn-hu, tn);
return(BAD_T);
}
/* Sum the differentiated interpolating polynomial */
s = (t - tn) / h;
for (j=q; j >= k; j--) {
c = ONE;
for (i=j; i >= j-k+1; i--) c *= i;
if (j == q) {
N_VScale(c, zn[q], dky);
} else {
N_VLinearSum(c, zn[j], s, dky, dky);
}
}
if (k == 0) return(OKAY);
r = RPowerI(h,-k);