-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathcvode.c
More file actions
5005 lines (4294 loc) · 147 KB
/
cvode.c
File metadata and controls
5005 lines (4294 loc) · 147 KB
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
/* -----------------------------------------------------------------
* Programmer(s): Scott D. Cohen, Alan C. Hindmarsh, Radu Serban,
* and Dan Shumaker @ LLNL
* -----------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2025-2026, Lawrence Livermore National Security,
* University of Maryland Baltimore County, and the SUNDIALS contributors.
* Copyright (c) 2013-2025, Lawrence Livermore National Security
* and Southern Methodist University.
* Copyright (c) 2002-2013, Lawrence Livermore National Security.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
* -----------------------------------------------------------------
* This is the implementation file for the main CVODE integrator.
* -----------------------------------------------------------------*/
/*=================================================================*/
/* Import Header Files */
/*=================================================================*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cvode/cvode.h>
#include <sundials/priv/sundials_errors_impl.h>
#include <sundials/sundials_context.h>
#include <sundials/sundials_types.h>
#include <sunnonlinsol/sunnonlinsol_newton.h>
#include "cvode_impl.h"
#include "cvode_ls_impl.h"
#include "sundials_utils.h"
/*=================================================================*/
/* CVODE Private Constants */
/*=================================================================*/
#define ZERO SUN_RCONST(0.0) /* real 0.0 */
#define TINY SUN_RCONST(1.0e-10) /* small number */
#define PT1 SUN_RCONST(0.1) /* real 0.1 */
#define POINT2 SUN_RCONST(0.2) /* real 0.2 */
#define FOURTH SUN_RCONST(0.25) /* real 0.25 */
#define HALF SUN_RCONST(0.5) /* real 0.5 */
#define PT9 SUN_RCONST(0.9) /* real 0.9 */
#define ONE SUN_RCONST(1.0) /* real 1.0 */
#define ONEPT5 SUN_RCONST(1.50) /* real 1.5 */
#define TWO SUN_RCONST(2.0) /* real 2.0 */
#define THREE SUN_RCONST(3.0) /* real 3.0 */
#define FOUR SUN_RCONST(4.0) /* real 4.0 */
#define FIVE SUN_RCONST(5.0) /* real 5.0 */
#define TWELVE SUN_RCONST(12.0) /* real 12.0 */
#define HUNDRED SUN_RCONST(100.0) /* real 100.0 */
/*=================================================================*/
/* CVODE Routine-Specific Constants */
/*=================================================================*/
/*
* Control constants for lower-level rootfinding functions
* -------------------------------------------------------
*
* cvRcheck1 return values:
* CV_SUCCESS
* CV_RTFUNC_FAIL
* cvRcheck2 return values:
* CV_SUCCESS
* CV_RTFUNC_FAIL
* CLOSERT
* RTFOUND
* cvRcheck3 return values:
* CV_SUCCESS
* CV_RTFUNC_FAIL
* RTFOUND
* cvRootfind return values:
* CV_SUCCESS
* CV_RTFUNC_FAIL
* RTFOUND
*/
#define RTFOUND +1
#define CLOSERT +3
/*
* Control constants for tolerances
* --------------------------------
*/
#define CV_NN 0
#define CV_SS 1
#define CV_SV 2
#define CV_WF 3
/*
* Algorithmic constants
* ---------------------
*
* CVodeGetDky and cvStep
*
* FUZZ_FACTOR fuzz factor used to estimate infinitesimal time intervals
*
* cvHin
*
* HLB_FACTOR factor for upper bound on initial step size
* HUB_FACTOR factor for lower bound on initial step size
* H_BIAS bias factor in selection of initial step size
* MAX_ITERS maximum attempts to compute the initial step size
*
* CVodeCreate
*
* CORTES constant in nonlinear iteration convergence test
*
*/
#define FUZZ_FACTOR SUN_RCONST(100.0)
#define HLB_FACTOR SUN_RCONST(100.0)
#define HUB_FACTOR SUN_RCONST(0.1)
#define H_BIAS HALF
#define MAX_ITERS 4
#define CORTES SUN_RCONST(0.1)
/*=================================================================*/
/* Private Helper Functions Prototypes */
/*=================================================================*/
static sunbooleantype cvCheckNvector(N_Vector tmpl);
/* Initial setup */
static int cvInitialSetup(CVodeMem cv_mem, sunrealtype tout);
/* Memory allocation/deallocation */
static sunbooleantype cvAllocVectors(CVodeMem cv_mem, N_Vector tmpl);
static void cvFreeVectors(CVodeMem cv_mem);
static int cvEwtSetSS(CVodeMem cv_mem, N_Vector ycur, N_Vector weight);
static int cvEwtSetSV(CVodeMem cv_mem, N_Vector ycur, N_Vector weight);
/* Initial stepsize calculation */
static int cvHin(CVodeMem cv_mem, sunrealtype tout);
static sunrealtype cvUpperBoundH0(CVodeMem cv_mem, sunrealtype tdist);
static int cvYddNorm(CVodeMem cv_mem, sunrealtype hg, sunrealtype* yddnrm);
/* Main cvStep function */
static int cvStep(CVodeMem cv_mem);
/* Function called at beginning of step */
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 cvPredict(CVodeMem cv_mem);
static void cvSet(CVodeMem cv_mem);
static void cvSetAdams(CVodeMem cv_mem);
static sunrealtype cvAdamsStart(CVodeMem cv_mem, sunrealtype m[]);
static void cvAdamsFinish(CVodeMem cv_mem, sunrealtype m[], sunrealtype M[],
sunrealtype hsum);
static sunrealtype cvAltSum(int iend, sunrealtype a[], int k);
static void cvSetBDF(CVodeMem cv_mem);
static void cvSetTqBDF(CVodeMem cv_mem, sunrealtype hsum, sunrealtype alpha0,
sunrealtype alpha0_hat, sunrealtype xi_inv,
sunrealtype xistar_inv);
/* Nonlinear solver functions */
static int cvNls(CVodeMem cv_mem, int nflag);
static int cvHandleNFlag(CVodeMem cv_mem, int* nflagPtr, sunrealtype saved_t,
int* ncfPtr);
static int cvCheckConstraints(CVodeMem cv_mem, int* nflagPtr,
sunrealtype saved_t, int* step_constraint_fails);
/* Error Test */
static int cvDoErrorTest(CVodeMem cv_mem, int* nflagPtr, sunrealtype saved_t,
int* nefPtr, sunrealtype* dsmPtr);
/* Function called after a successful step */
static void cvCompleteStep(CVodeMem cv_mem);
static void cvPrepareNextStep(CVodeMem cv_mem, sunrealtype dsm);
static void cvSetEta(CVodeMem cv_mem);
static sunrealtype cvComputeEtaqm1(CVodeMem cv_mem);
static sunrealtype cvComputeEtaqp1(CVodeMem cv_mem);
static void cvChooseEta(CVodeMem cv_mem);
/* Function to handle failures */
static int cvHandleFailure(CVodeMem cv_mem, int flag);
/* Functions for BDF Stability Limit Detection */
static void cvBDFStab(CVodeMem cv_mem);
static int cvSLdet(CVodeMem cv_mem);
/* Functions for rootfinding */
static int cvRcheck1(CVodeMem cv_mem);
static int cvRcheck2(CVodeMem cv_mem);
static int cvRcheck3(CVodeMem cv_mem, sunrealtype tout, int itask);
static int cvRootfind(CVodeMem cv_mem);
/*
* =================================================================
* Exported Functions Implementation
* =================================================================
*/
/*
* -----------------------------------------------------------------
* Creation, allocation and re-initialization functions
* -----------------------------------------------------------------
*/
/*
* CVodeCreate
*
* CVodeCreate creates an internal memory block for a problem to
* be solved by CVODE.
* If successful, CVodeCreate returns a pointer to the problem memory.
* This pointer should be passed to CVodeInit.
* If an initialization error occurs, CVodeCreate prints an error
* message to standard err and returns NULL.
*/
void* CVodeCreate(int lmm, SUNContext sunctx)
{
int maxord;
CVodeMem cv_mem;
/* Test inputs */
if ((lmm != CV_ADAMS) && (lmm != CV_BDF))
{
cvProcessError(NULL, 0, __LINE__, __func__, __FILE__, MSGCV_BAD_LMM);
return (NULL);
}
if (sunctx == NULL)
{
cvProcessError(NULL, 0, __LINE__, __func__, __FILE__, MSGCV_NULL_SUNCTX);
return (NULL);
}
cv_mem = NULL;
cv_mem = (CVodeMem)malloc(sizeof(struct CVodeMemRec));
if (cv_mem == NULL)
{
cvProcessError(NULL, 0, __LINE__, __func__, __FILE__, MSGCV_CVMEM_FAIL);
return (NULL);
}
/* Zero out cv_mem */
memset(cv_mem, 0, sizeof(struct CVodeMemRec));
maxord = (lmm == CV_ADAMS) ? ADAMS_Q_MAX : BDF_Q_MAX;
/* Copy input parameters into cv_mem */
cv_mem->cv_sunctx = sunctx;
cv_mem->cv_lmm = lmm;
/* Set uround */
cv_mem->cv_uround = SUN_UNIT_ROUNDOFF;
/* Set default values for integrator optional inputs */
cv_mem->cv_f = NULL;
cv_mem->cv_user_data = NULL;
cv_mem->cv_itol = CV_NN;
cv_mem->cv_atolmin0 = SUNTRUE;
cv_mem->cv_user_efun = SUNFALSE;
cv_mem->cv_efun = NULL;
cv_mem->cv_e_data = NULL;
cv_mem->cv_monitorfun = NULL;
cv_mem->cv_monitor_interval = 0;
cv_mem->cv_qmax = maxord;
cv_mem->cv_mxstep = MXSTEP_DEFAULT;
cv_mem->cv_mxhnil = MXHNIL_DEFAULT;
cv_mem->cv_sldeton = SUNFALSE;
cv_mem->cv_hin = ZERO;
cv_mem->cv_hmin = HMIN_DEFAULT;
cv_mem->cv_hmax_inv = HMAX_INV_DEFAULT;
cv_mem->cv_eta_min_fx = ETA_MIN_FX_DEFAULT;
cv_mem->cv_eta_max_fx = ETA_MAX_FX_DEFAULT;
cv_mem->cv_eta_max_fs = ETA_MAX_FS_DEFAULT;
cv_mem->cv_eta_max_es = ETA_MAX_ES_DEFAULT;
cv_mem->cv_eta_max_gs = ETA_MAX_GS_DEFAULT;
cv_mem->cv_eta_min = ETA_MIN_DEFAULT;
cv_mem->cv_eta_min_ef = ETA_MIN_EF_DEFAULT;
cv_mem->cv_eta_max_ef = ETA_MAX_EF_DEFAULT;
cv_mem->cv_eta_cf = ETA_CF_DEFAULT;
cv_mem->cv_small_nst = SMALL_NST_DEFAULT;
cv_mem->cv_small_nef = SMALL_NEF_DEFAULT;
cv_mem->cv_tstopset = SUNFALSE;
cv_mem->cv_tstopinterp = SUNFALSE;
cv_mem->cv_maxnef = MXNEF;
cv_mem->cv_maxncf = MXNCF;
cv_mem->cv_nlscoef = CORTES;
cv_mem->cv_msbp = MSBP_DEFAULT;
cv_mem->cv_dgmax_lsetup = DGMAX_LSETUP_DEFAULT;
cv_mem->convfail = CV_NO_FAILURES;
/* Initialize inequality constraint variables */
cv_mem->cv_constraints = NULL;
cv_mem->constraint_corrections = 0;
cv_mem->constraint_fails = 0;
cv_mem->max_constraint_fails = MAX_CONSTRAINT_FAILS;
/* Initialize root finding variables */
cv_mem->cv_glo = NULL;
cv_mem->cv_ghi = NULL;
cv_mem->cv_grout = NULL;
cv_mem->cv_iroots = NULL;
cv_mem->cv_rootdir = NULL;
cv_mem->cv_gfun = NULL;
cv_mem->cv_nrtfn = 0;
cv_mem->cv_gactive = NULL;
cv_mem->cv_mxgnull = 1;
/* Initialize projection variables */
cv_mem->proj_mem = NULL;
cv_mem->proj_enabled = SUNFALSE;
cv_mem->proj_applied = SUNFALSE;
/* Initialize resize variables */
cv_mem->first_step_after_resize = SUNFALSE;
/* Set the saved value for qmax_alloc */
cv_mem->cv_qmax_alloc = maxord;
/* Initialize lrw and liw */
cv_mem->cv_lrw = 58 + 2 * L_MAX + NUM_TESTS;
cv_mem->cv_liw = 40;
/* No mallocs have been done yet */
cv_mem->cv_VabstolMallocDone = SUNFALSE;
cv_mem->cv_MallocDone = SUNFALSE;
/* Initialize nonlinear solver variables */
cv_mem->NLS = NULL;
cv_mem->ownNLS = SUNFALSE;
/* Initialize fused operations variable */
cv_mem->cv_usefused = SUNFALSE;
/* Return pointer to CVODE memory block */
return ((void*)cv_mem);
}
/*-----------------------------------------------------------------*/
/*
* CVodeInit
*
* CVodeInit allocates and initializes memory for a problem. All
* problem inputs are checked for errors. If any error occurs during
* initialization, it is reported to the file whose file pointer is
* errfp and an error flag is returned. Otherwise, it returns CV_SUCCESS
*/
int CVodeInit(void* cvode_mem, CVRhsFn f, sunrealtype t0, N_Vector y0)
{
CVodeMem cv_mem;
sunbooleantype nvectorOK, allocOK;
sunindextype lrw1, liw1;
int i, k, retval;
SUNNonlinearSolver NLS;
/* Check cvode_mem */
if (cvode_mem == NULL)
{
cvProcessError(NULL, CV_MEM_NULL, __LINE__, __func__, __FILE__, MSGCV_NO_MEM);
return (CV_MEM_NULL);
}
cv_mem = (CVodeMem)cvode_mem;
SUNDIALS_MARK_FUNCTION_BEGIN(CV_PROFILER);
/* Check for legal input parameters */
if (y0 == NULL)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_NULL_Y0);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
if (f == NULL)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_NULL_F);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* Test if all required vector operations are implemented */
nvectorOK = cvCheckNvector(y0);
if (!nvectorOK)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_NVECTOR);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* Set space requirements for one N_Vector */
if (y0->ops->nvspace != NULL) { N_VSpace(y0, &lrw1, &liw1); }
else
{
lrw1 = 0;
liw1 = 0;
}
cv_mem->cv_lrw1 = lrw1;
cv_mem->cv_liw1 = liw1;
/* Allocate the vectors (using y0 as a template) */
allocOK = cvAllocVectors(cv_mem, y0);
if (!allocOK)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_MEM_FAIL);
}
/* Input checks complete at this point and history array allocated */
/* Copy the input parameters into CVODE state */
cv_mem->cv_f = f;
cv_mem->cv_tn = t0;
/* Initialize zn[0] in the history array */
N_VScale(ONE, y0, cv_mem->cv_zn[0]);
/* create a Newton nonlinear solver object by default */
NLS = SUNNonlinSol_Newton(y0, cv_mem->cv_sunctx);
/* check that nonlinear solver is non-NULL */
if (NLS == NULL)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
cvFreeVectors(cv_mem);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_MEM_FAIL);
}
/* attach the nonlinear solver to the CVODE memory */
retval = CVodeSetNonlinearSolver(cv_mem, NLS);
/* check that the nonlinear solver was successfully attached */
if (retval != CV_SUCCESS)
{
cvProcessError(cv_mem, retval, __LINE__, __func__, __FILE__,
"Setting the nonlinear solver failed");
cvFreeVectors(cv_mem);
SUNNonlinSolFree(NLS);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_MEM_FAIL);
}
/* set ownership flag */
cv_mem->ownNLS = SUNTRUE;
/* All error checking is complete at this point */
/* Set step parameters */
cv_mem->cv_q = 1;
cv_mem->cv_L = 2;
cv_mem->cv_qwait = cv_mem->cv_L;
cv_mem->cv_etamax = cv_mem->cv_eta_max_fs;
cv_mem->cv_qu = 0;
cv_mem->cv_hu = ZERO;
cv_mem->cv_tolsf = ONE;
/* Set the linear solver addresses to NULL.
(We check != NULL later, in CVode) */
cv_mem->cv_linit = NULL;
cv_mem->cv_lreinit = NULL;
cv_mem->cv_lsetup = NULL;
cv_mem->cv_lsolve = NULL;
cv_mem->cv_lfree = NULL;
cv_mem->cv_lmem = NULL;
/* Initialize all the counters */
cv_mem->cv_nst = 0;
cv_mem->cv_nfe = 0;
cv_mem->cv_ncfn = 0;
cv_mem->cv_netf = 0;
cv_mem->cv_nni = 0;
cv_mem->cv_nnf = 0;
cv_mem->cv_nsetups = 0;
cv_mem->cv_nhnil = 0;
cv_mem->cv_nstlp = 0;
cv_mem->cv_nscon = 0;
cv_mem->cv_nge = 0;
cv_mem->cv_irfnd = 0;
/* Initialize other integrator optional outputs */
cv_mem->cv_h0u = ZERO;
cv_mem->cv_next_h = ZERO;
cv_mem->cv_next_q = 0;
/* Initialize Stablilty Limit Detection data */
/* NOTE: We do this even if stab lim det was not
turned on yet. This way, the user can turn it
on at any time */
cv_mem->cv_nor = 0;
for (i = 1; i <= 5; i++)
{
for (k = 1; k <= 3; k++) { cv_mem->cv_ssdat[i - 1][k - 1] = ZERO; }
}
/* Problem has been successfully initialized */
cv_mem->cv_MallocDone = SUNTRUE;
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_SUCCESS);
}
/*-----------------------------------------------------------------*/
/*
* CVodeReInit
*
* CVodeReInit re-initializes CVODE's memory for a problem, assuming
* it has already been allocated in a prior CVodeInit call.
* 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.
* The return value is CV_SUCCESS = 0 if no errors occurred, or
* a negative value otherwise.
*/
int CVodeReInit(void* cvode_mem, sunrealtype t0, N_Vector y0)
{
CVodeMem cv_mem;
int i, k;
/* Check cvode_mem */
if (cvode_mem == NULL)
{
cvProcessError(NULL, CV_MEM_NULL, __LINE__, __func__, __FILE__, MSGCV_NO_MEM);
return (CV_MEM_NULL);
}
cv_mem = (CVodeMem)cvode_mem;
SUNDIALS_MARK_FUNCTION_BEGIN(CV_PROFILER);
/* Check if cvode_mem was allocated */
if (cv_mem->cv_MallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_MALLOC, __LINE__, __func__, __FILE__,
MSGCV_NO_MALLOC);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_NO_MALLOC);
}
/* Check for legal input parameters */
if (y0 == NULL)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_NULL_Y0);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* Copy the input parameters into CVODE state */
cv_mem->cv_tn = t0;
/* Set step parameters */
cv_mem->cv_q = 1;
cv_mem->cv_L = 2;
cv_mem->cv_qwait = cv_mem->cv_L;
cv_mem->cv_etamax = cv_mem->cv_eta_max_fs;
cv_mem->cv_qu = 0;
cv_mem->cv_hu = ZERO;
cv_mem->cv_tolsf = ONE;
/* Initialize zn[0] in the history array */
N_VScale(ONE, y0, cv_mem->cv_zn[0]);
/* Initialize all the counters */
cv_mem->cv_nst = 0;
cv_mem->cv_nfe = 0;
cv_mem->cv_ncfn = 0;
cv_mem->cv_netf = 0;
cv_mem->cv_nni = 0;
cv_mem->cv_nnf = 0;
cv_mem->cv_nsetups = 0;
cv_mem->cv_nhnil = 0;
cv_mem->cv_nstlp = 0;
cv_mem->cv_nscon = 0;
cv_mem->cv_nge = 0;
cv_mem->cv_irfnd = 0;
cv_mem->constraint_corrections = 0;
cv_mem->constraint_fails = 0;
if (cv_mem->cv_lreinit) { cv_mem->cv_lreinit(cv_mem); }
/* Initialize other integrator optional outputs */
cv_mem->cv_h0u = ZERO;
cv_mem->cv_next_h = ZERO;
cv_mem->cv_next_q = 0;
/* Initialize Stablilty Limit Detection data */
cv_mem->cv_nor = 0;
for (i = 1; i <= 5; i++)
{
for (k = 1; k <= 3; k++) { cv_mem->cv_ssdat[i - 1][k - 1] = ZERO; }
}
/* Problem has been successfully re-initialized */
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_SUCCESS);
}
/*-----------------------------------------------------------------*/
/*
* CVodeSStolerances
* CVodeSVtolerances
* CVodeWFtolerances
*
* These functions specify the integration tolerances. One of them
* MUST be called before the first call to CVode.
*
* CVodeSStolerances specifies scalar relative and absolute tolerances.
* CVodeSVtolerances specifies scalar relative tolerance and a vector
* absolute tolerance (a potentially different absolute tolerance
* for each vector component).
* CVodeWFtolerances specifies a user-provides function (of type CVEwtFn)
* which will be called to set the error weight vector.
*/
int CVodeSStolerances(void* cvode_mem, sunrealtype reltol, sunrealtype abstol)
{
CVodeMem cv_mem;
if (cvode_mem == NULL)
{
cvProcessError(NULL, CV_MEM_NULL, __LINE__, __func__, __FILE__, MSGCV_NO_MEM);
return (CV_MEM_NULL);
}
cv_mem = (CVodeMem)cvode_mem;
if (cv_mem->cv_MallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_MALLOC, __LINE__, __func__, __FILE__,
MSGCV_NO_MALLOC);
return (CV_NO_MALLOC);
}
/* Check inputs */
if (reltol < ZERO)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_RELTOL);
return (CV_ILL_INPUT);
}
if (abstol < ZERO)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_ABSTOL);
return (CV_ILL_INPUT);
}
/* Copy tolerances into memory */
cv_mem->cv_reltol = reltol;
cv_mem->cv_Sabstol = abstol;
cv_mem->cv_atolmin0 = (abstol == ZERO);
cv_mem->cv_itol = CV_SS;
cv_mem->cv_user_efun = SUNFALSE;
cv_mem->cv_efun = cvEwtSet;
cv_mem->cv_e_data = NULL; /* will be set to cvode_mem in InitialSetup */
return (CV_SUCCESS);
}
int CVodeSVtolerances(void* cvode_mem, sunrealtype reltol, N_Vector abstol)
{
CVodeMem cv_mem;
sunrealtype atolmin;
if (cvode_mem == NULL)
{
cvProcessError(NULL, CV_MEM_NULL, __LINE__, __func__, __FILE__, MSGCV_NO_MEM);
return (CV_MEM_NULL);
}
cv_mem = (CVodeMem)cvode_mem;
if (cv_mem->cv_MallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_MALLOC, __LINE__, __func__, __FILE__,
MSGCV_NO_MALLOC);
return (CV_NO_MALLOC);
}
/* Check inputs */
if (reltol < ZERO)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_RELTOL);
return (CV_ILL_INPUT);
}
if (abstol->ops->nvmin == NULL)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
"Missing N_VMin routine from N_Vector");
return (CV_ILL_INPUT);
}
atolmin = N_VMin(abstol);
if (atolmin < ZERO)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_ABSTOL);
return (CV_ILL_INPUT);
}
/* Copy tolerances into memory */
if (!(cv_mem->cv_VabstolMallocDone))
{
cv_mem->cv_Vabstol = N_VClone(cv_mem->cv_ewt);
cv_mem->cv_lrw += cv_mem->cv_lrw1;
cv_mem->cv_liw += cv_mem->cv_liw1;
cv_mem->cv_VabstolMallocDone = SUNTRUE;
}
cv_mem->cv_reltol = reltol;
N_VScale(ONE, abstol, cv_mem->cv_Vabstol);
cv_mem->cv_atolmin0 = (atolmin == ZERO);
cv_mem->cv_itol = CV_SV;
cv_mem->cv_user_efun = SUNFALSE;
cv_mem->cv_efun = cvEwtSet;
cv_mem->cv_e_data = NULL; /* will be set to cvode_mem in InitialSetup */
return (CV_SUCCESS);
}
int CVodeWFtolerances(void* cvode_mem, CVEwtFn efun)
{
CVodeMem cv_mem;
if (cvode_mem == NULL)
{
cvProcessError(NULL, CV_MEM_NULL, __LINE__, __func__, __FILE__, MSGCV_NO_MEM);
return (CV_MEM_NULL);
}
cv_mem = (CVodeMem)cvode_mem;
if (cv_mem->cv_MallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_MALLOC, __LINE__, __func__, __FILE__,
MSGCV_NO_MALLOC);
return (CV_NO_MALLOC);
}
cv_mem->cv_itol = CV_WF;
cv_mem->cv_user_efun = SUNTRUE;
cv_mem->cv_efun = efun;
cv_mem->cv_e_data = NULL; /* will be set to user_data in InitialSetup */
return (CV_SUCCESS);
}
/*-----------------------------------------------------------------*/
/*
* CVodeRootInit
*
* CVodeRootInit initializes a rootfinding problem to be solved
* during the integration of the ODE system. It loads the root
* function pointer and the number of root functions, and allocates
* workspace memory. The return value is CV_SUCCESS = 0 if no errors
* occurred, or a negative value otherwise.
*/
int CVodeRootInit(void* cvode_mem, int nrtfn, CVRootFn g)
{
CVodeMem cv_mem;
int i, nrt;
/* Check cvode_mem pointer */
if (cvode_mem == NULL)
{
cvProcessError(NULL, CV_MEM_NULL, __LINE__, __func__, __FILE__, MSGCV_NO_MEM);
return (CV_MEM_NULL);
}
cv_mem = (CVodeMem)cvode_mem;
nrt = (nrtfn < 0) ? 0 : nrtfn;
/* If rerunning CVodeRootInit() with a different number of root
functions (changing number of gfun components), then free
currently held memory resources */
if ((nrt != cv_mem->cv_nrtfn) && (cv_mem->cv_nrtfn > 0))
{
free(cv_mem->cv_glo);
cv_mem->cv_glo = NULL;
free(cv_mem->cv_ghi);
cv_mem->cv_ghi = NULL;
free(cv_mem->cv_grout);
cv_mem->cv_grout = NULL;
free(cv_mem->cv_iroots);
cv_mem->cv_iroots = NULL;
free(cv_mem->cv_rootdir);
cv_mem->cv_rootdir = NULL;
free(cv_mem->cv_gactive);
cv_mem->cv_gactive = NULL;
cv_mem->cv_lrw -= 3 * (cv_mem->cv_nrtfn);
cv_mem->cv_liw -= 3 * (cv_mem->cv_nrtfn);
}
/* If CVodeRootInit() was called with nrtfn == 0, then set cv_nrtfn to
zero and cv_gfun to NULL before returning */
if (nrt == 0)
{
cv_mem->cv_nrtfn = nrt;
cv_mem->cv_gfun = NULL;
return (CV_SUCCESS);
}
/* If rerunning CVodeRootInit() with the same number of root functions
(not changing number of gfun components), then check if the root
function argument has changed */
/* If g != NULL then return as currently reserved memory resources
will suffice */
if (nrt == cv_mem->cv_nrtfn)
{
if (g != cv_mem->cv_gfun)
{
if (g == NULL)
{
free(cv_mem->cv_glo);
cv_mem->cv_glo = NULL;
free(cv_mem->cv_ghi);
cv_mem->cv_ghi = NULL;
free(cv_mem->cv_grout);
cv_mem->cv_grout = NULL;
free(cv_mem->cv_iroots);
cv_mem->cv_iroots = NULL;
free(cv_mem->cv_rootdir);
cv_mem->cv_rootdir = NULL;
free(cv_mem->cv_gactive);
cv_mem->cv_gactive = NULL;
cv_mem->cv_lrw -= 3 * nrt;
cv_mem->cv_liw -= 3 * nrt;
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_NULL_G);
return (CV_ILL_INPUT);
}
else
{
cv_mem->cv_gfun = g;
return (CV_SUCCESS);
}
}
else { return (CV_SUCCESS); }
}
/* Set variable values in CVode memory block */
cv_mem->cv_nrtfn = nrt;
if (g == NULL)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_NULL_G);
return (CV_ILL_INPUT);
}
else { cv_mem->cv_gfun = g; }
/* Allocate necessary memory and return */
cv_mem->cv_glo = NULL;
cv_mem->cv_glo = (sunrealtype*)malloc(nrt * sizeof(sunrealtype));
if (cv_mem->cv_glo == NULL)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
return (CV_MEM_FAIL);
}
cv_mem->cv_ghi = NULL;
cv_mem->cv_ghi = (sunrealtype*)malloc(nrt * sizeof(sunrealtype));
if (cv_mem->cv_ghi == NULL)
{
free(cv_mem->cv_glo);
cv_mem->cv_glo = NULL;
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
return (CV_MEM_FAIL);
}
cv_mem->cv_grout = NULL;
cv_mem->cv_grout = (sunrealtype*)malloc(nrt * sizeof(sunrealtype));
if (cv_mem->cv_grout == NULL)
{
free(cv_mem->cv_glo);
cv_mem->cv_glo = NULL;
free(cv_mem->cv_ghi);
cv_mem->cv_ghi = NULL;
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
return (CV_MEM_FAIL);
}
cv_mem->cv_iroots = NULL;
cv_mem->cv_iroots = (int*)malloc(nrt * sizeof(int));
if (cv_mem->cv_iroots == NULL)
{
free(cv_mem->cv_glo);
cv_mem->cv_glo = NULL;
free(cv_mem->cv_ghi);
cv_mem->cv_ghi = NULL;
free(cv_mem->cv_grout);
cv_mem->cv_grout = NULL;
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
return (CV_MEM_FAIL);
}
cv_mem->cv_rootdir = NULL;
cv_mem->cv_rootdir = (int*)malloc(nrt * sizeof(int));
if (cv_mem->cv_rootdir == NULL)
{
free(cv_mem->cv_glo);
cv_mem->cv_glo = NULL;
free(cv_mem->cv_ghi);
cv_mem->cv_ghi = NULL;
free(cv_mem->cv_grout);
cv_mem->cv_grout = NULL;
free(cv_mem->cv_iroots);
cv_mem->cv_iroots = NULL;
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
return (CV_MEM_FAIL);
}
cv_mem->cv_gactive = NULL;
cv_mem->cv_gactive = (sunbooleantype*)malloc(nrt * sizeof(sunbooleantype));
if (cv_mem->cv_gactive == NULL)
{
free(cv_mem->cv_glo);
cv_mem->cv_glo = NULL;