-
Notifications
You must be signed in to change notification settings - Fork 132
/
cvodea.c
3520 lines (2859 loc) · 89.7 KB
/
cvodea.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
/*
* -----------------------------------------------------------------
* Programmer(s): Radu Serban @ LLNL
* -----------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2024, Lawrence Livermore National Security
* and Southern Methodist University.
* 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 CVODEA adjoint integrator.
* -----------------------------------------------------------------
*/
/*
* =================================================================
* IMPORTED HEADER FILES
* =================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <sundials/sundials_math.h>
#include <sundials/sundials_types.h>
#include "cvodes_impl.h"
/*
* =================================================================
* CVODEA PRIVATE CONSTANTS
* =================================================================
*/
#define ZERO SUN_RCONST(0.0) /* real 0.0 */
#define ONE SUN_RCONST(1.0) /* real 1.0 */
#define TWO SUN_RCONST(2.0) /* real 2.0 */
#define HUNDRED SUN_RCONST(100.0) /* real 100.0 */
#define FUZZ_FACTOR SUN_RCONST(1000000.0) /* fuzz factor for IMget */
/*=================================================================*/
/* Shortcuts */
/*=================================================================*/
#define CV_PROFILER cv_mem->cv_sunctx->profiler
/*
* =================================================================
* PRIVATE FUNCTION PROTOTYPES
* =================================================================
*/
static CVckpntMem CVAckpntInit(CVodeMem cv_mem);
static CVckpntMem CVAckpntNew(CVodeMem cv_mem);
static void CVAckpntDelete(CVckpntMem* ck_memPtr);
static void CVAbckpbDelete(CVodeBMem* cvB_memPtr);
static int CVAdataStore(CVodeMem cv_mem, CVckpntMem ck_mem);
static int CVAckpntGet(CVodeMem cv_mem, CVckpntMem ck_mem);
static int CVAfindIndex(CVodeMem cv_mem, sunrealtype t, long int* indx,
sunbooleantype* newpoint);
static sunbooleantype CVAhermiteMalloc(CVodeMem cv_mem);
static void CVAhermiteFree(CVodeMem cv_mem);
static int CVAhermiteGetY(CVodeMem cv_mem, sunrealtype t, N_Vector y,
N_Vector* yS);
static int CVAhermiteStorePnt(CVodeMem cv_mem, CVdtpntMem d);
static sunbooleantype CVApolynomialMalloc(CVodeMem cv_mem);
static void CVApolynomialFree(CVodeMem cv_mem);
static int CVApolynomialGetY(CVodeMem cv_mem, sunrealtype t, N_Vector y,
N_Vector* yS);
static int CVApolynomialStorePnt(CVodeMem cv_mem, CVdtpntMem d);
/* Wrappers */
static int CVArhs(sunrealtype t, N_Vector yB, N_Vector yBdot, void* cvode_mem);
static int CVArhsQ(sunrealtype t, N_Vector yB, N_Vector qBdot, void* cvode_mem);
/*
* =================================================================
* EXPORTED FUNCTIONS IMPLEMENTATION
* =================================================================
*/
/*
* CVodeAdjInit
*
* This routine initializes ASA and allocates space for the adjoint
* memory structure.
*/
int CVodeAdjInit(void* cvode_mem, long int steps, int interp)
{
CVadjMem ca_mem;
CVodeMem cv_mem;
long int i, ii;
/* ---------------
* Check arguments
* --------------- */
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);
if (steps <= 0)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_STEPS);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
if ((interp != CV_HERMITE) && (interp != CV_POLYNOMIAL))
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_INTERP);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* ----------------------------
* Allocate CVODEA memory block
* ---------------------------- */
ca_mem = NULL;
ca_mem = (CVadjMem)malloc(sizeof(struct CVadjMemRec));
if (ca_mem == NULL)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_MEM_FAIL);
}
/* Attach ca_mem to CVodeMem structure */
cv_mem->cv_adj_mem = ca_mem;
/* ------------------------------
* Initialization of check points
* ------------------------------ */
/* Set Check Points linked list to NULL */
ca_mem->ck_mem = NULL;
/* Initialize nckpnts to ZERO */
ca_mem->ca_nckpnts = 0;
/* No interpolation data is available */
ca_mem->ca_ckpntData = NULL;
/* ------------------------------------
* Initialization of interpolation data
* ------------------------------------ */
/* Interpolation type */
ca_mem->ca_IMtype = interp;
/* Number of steps between check points */
ca_mem->ca_nsteps = steps;
/* Last index used in CVAfindIndex, initialize to invalid value */
ca_mem->ca_ilast = -1;
/* Allocate space for the array of Data Point structures */
ca_mem->dt_mem = NULL;
ca_mem->dt_mem =
(CVdtpntMem*)malloc((steps + 1) * sizeof(struct CVdtpntMemRec*));
if (ca_mem->dt_mem == NULL)
{
free(ca_mem);
ca_mem = NULL;
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_MEM_FAIL);
}
for (i = 0; i <= steps; i++)
{
ca_mem->dt_mem[i] = NULL;
ca_mem->dt_mem[i] = (CVdtpntMem)malloc(sizeof(struct CVdtpntMemRec));
if (ca_mem->dt_mem[i] == NULL)
{
for (ii = 0; ii < i; ii++)
{
free(ca_mem->dt_mem[ii]);
ca_mem->dt_mem[ii] = NULL;
}
free(ca_mem->dt_mem);
ca_mem->dt_mem = NULL;
free(ca_mem);
ca_mem = NULL;
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_MEM_FAIL);
}
}
/* Attach functions for the appropriate interpolation module */
switch (interp)
{
case CV_HERMITE:
ca_mem->ca_IMmalloc = CVAhermiteMalloc;
ca_mem->ca_IMfree = CVAhermiteFree;
ca_mem->ca_IMget = CVAhermiteGetY;
ca_mem->ca_IMstore = CVAhermiteStorePnt;
break;
case CV_POLYNOMIAL:
ca_mem->ca_IMmalloc = CVApolynomialMalloc;
ca_mem->ca_IMfree = CVApolynomialFree;
ca_mem->ca_IMget = CVApolynomialGetY;
ca_mem->ca_IMstore = CVApolynomialStorePnt;
break;
}
/* The interpolation module has not been initialized yet */
ca_mem->ca_IMmallocDone = SUNFALSE;
/* By default we will store but not interpolate sensitivities
* - IMstoreSensi will be set in CVodeF to SUNFALSE if FSA is not enabled
* or if the user can force this through CVodeSetAdjNoSensi
* - IMinterpSensi will be set in CVodeB to SUNTRUE if IMstoreSensi is
* SUNTRUE and if at least one backward problem requires sensitivities */
ca_mem->ca_IMstoreSensi = SUNTRUE;
ca_mem->ca_IMinterpSensi = SUNFALSE;
/* ------------------------------------
* Initialize list of backward problems
* ------------------------------------ */
ca_mem->cvB_mem = NULL;
ca_mem->ca_bckpbCrt = NULL;
ca_mem->ca_nbckpbs = 0;
/* --------------------------------
* CVodeF and CVodeB not called yet
* -------------------------------- */
ca_mem->ca_firstCVodeFcall = SUNTRUE;
ca_mem->ca_tstopCVodeFcall = SUNFALSE;
ca_mem->ca_firstCVodeBcall = SUNTRUE;
ca_mem->ca_rootret = SUNFALSE;
/* ---------------------------------------------
* ASA initialized and allocated
* --------------------------------------------- */
cv_mem->cv_adj = SUNTRUE;
cv_mem->cv_adjMallocDone = SUNTRUE;
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_SUCCESS);
}
/* CVodeAdjReInit
*
* This routine reinitializes the CVODEA memory structure assuming that the
* the number of steps between check points and the type of interpolation
* remain unchanged.
* The list of check points (and associated memory) is deleted.
* The list of backward problems is kept (however, new backward problems can
* be added to this list by calling CVodeCreateB).
* The CVODES memory for the forward and backward problems can be reinitialized
* separately by calling CVodeReInit and CVodeReInitB, respectively.
* NOTE: if a completely new list of backward problems is also needed, then
* simply free the adjoint memory (by calling CVodeAdjFree) and reinitialize
* ASA with CVodeAdjInit.
*/
int CVodeAdjReInit(void* cvode_mem)
{
CVadjMem ca_mem;
CVodeMem cv_mem;
/* 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);
/* Was ASA initialized? */
if (cv_mem->cv_adjMallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_ADJ, __LINE__, __func__, __FILE__, MSGCV_NO_ADJ);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_NO_ADJ);
}
ca_mem = cv_mem->cv_adj_mem;
/* Free current list of Check Points */
while (ca_mem->ck_mem != NULL) { CVAckpntDelete(&(ca_mem->ck_mem)); }
/* Initialization of check points */
ca_mem->ck_mem = NULL;
ca_mem->ca_nckpnts = 0;
ca_mem->ca_ckpntData = NULL;
/* CVodeF and CVodeB not called yet */
ca_mem->ca_firstCVodeFcall = SUNTRUE;
ca_mem->ca_tstopCVodeFcall = SUNFALSE;
ca_mem->ca_firstCVodeBcall = SUNTRUE;
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_SUCCESS);
}
/*
* CVodeAdjFree
*
* This routine frees the memory allocated by CVodeAdjInit.
*/
void CVodeAdjFree(void* cvode_mem)
{
CVodeMem cv_mem;
CVadjMem ca_mem;
long int i;
if (cvode_mem == NULL) { return; }
cv_mem = (CVodeMem)cvode_mem;
if (cv_mem->cv_adjMallocDone)
{
ca_mem = cv_mem->cv_adj_mem;
/* Delete check points one by one */
while (ca_mem->ck_mem != NULL) { CVAckpntDelete(&(ca_mem->ck_mem)); }
/* Free vectors at all data points */
if (ca_mem->ca_IMmallocDone) { ca_mem->ca_IMfree(cv_mem); }
for (i = 0; i <= ca_mem->ca_nsteps; i++)
{
free(ca_mem->dt_mem[i]);
ca_mem->dt_mem[i] = NULL;
}
free(ca_mem->dt_mem);
ca_mem->dt_mem = NULL;
/* Delete backward problems one by one */
while (ca_mem->cvB_mem != NULL) { CVAbckpbDelete(&(ca_mem->cvB_mem)); }
/* Free CVODEA memory */
free(ca_mem);
cv_mem->cv_adj_mem = NULL;
}
}
/*
* CVodeF
*
* This routine integrates to tout and returns solution into yout.
* In the same time, it stores check point data every 'steps' steps.
*
* CVodeF can be called repeatedly by the user.
*
* ncheckPtr points to the number of check points stored so far.
*/
int CVodeF(void* cvode_mem, sunrealtype tout, N_Vector yout, sunrealtype* tret,
int itask, int* ncheckPtr)
{
CVadjMem ca_mem;
CVodeMem cv_mem;
CVckpntMem tmp;
CVdtpntMem* dt_mem;
long int nstloc;
int flag, i;
sunbooleantype allocOK, earlyret;
sunrealtype ttest;
/* Check if cvode_mem exists */
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);
/* Was ASA initialized? */
if (cv_mem->cv_adjMallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_ADJ, __LINE__, __func__, __FILE__, MSGCV_NO_ADJ);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_NO_ADJ);
}
ca_mem = cv_mem->cv_adj_mem;
/* Check for yout != NULL */
if (yout == NULL)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_YOUT_NULL);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* Check for tret != NULL */
if (tret == NULL)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_TRET_NULL);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* Check for valid itask */
if ((itask != CV_NORMAL) && (itask != CV_ONE_STEP))
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_ITASK);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* All error checking done */
dt_mem = ca_mem->dt_mem;
/* If tstop is enabled, store some info */
if (cv_mem->cv_tstopset)
{
ca_mem->ca_tstopCVodeFcall = SUNTRUE;
ca_mem->ca_tstopCVodeF = cv_mem->cv_tstop;
}
/* On the first step:
* - set tinitial
* - initialize list of check points
* - if needed, initialize the interpolation module
* - load dt_mem[0]
* On subsequent steps, test if taking a new step is necessary.
*/
if (ca_mem->ca_firstCVodeFcall)
{
ca_mem->ca_tinitial = cv_mem->cv_tn;
ca_mem->ck_mem = CVAckpntInit(cv_mem);
if (ca_mem->ck_mem == NULL)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_MEM_FAIL);
}
if (!ca_mem->ca_IMmallocDone)
{
/* Do we need to store sensitivities? */
if (!cv_mem->cv_sensi) { ca_mem->ca_IMstoreSensi = SUNFALSE; }
/* Allocate space for interpolation data */
allocOK = ca_mem->ca_IMmalloc(cv_mem);
if (!allocOK)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_MEM_FAIL);
}
/* Rename zn and, if needed, znS for use in interpolation */
for (i = 0; i < L_MAX; i++) { ca_mem->ca_Y[i] = cv_mem->cv_zn[i]; }
if (ca_mem->ca_IMstoreSensi)
{
for (i = 0; i < L_MAX; i++) { ca_mem->ca_YS[i] = cv_mem->cv_znS[i]; }
}
ca_mem->ca_IMmallocDone = SUNTRUE;
}
dt_mem[0]->t = ca_mem->ck_mem->ck_t0;
ca_mem->ca_IMstore(cv_mem, dt_mem[0]);
ca_mem->ca_firstCVodeFcall = SUNFALSE;
}
else if (itask == CV_NORMAL)
{
/* When in normal mode, check if tout was passed or if a previous root was
not reported and return an interpolated solution. No changes to ck_mem
or dt_mem are needed. */
/* flag to signal if an early return is needed */
earlyret = SUNFALSE;
/* if a root needs to be reported compare tout to troot otherwise compare
to the current time tn */
ttest = (ca_mem->ca_rootret) ? ca_mem->ca_troot : cv_mem->cv_tn;
if ((ttest - tout) * cv_mem->cv_h >= ZERO)
{
/* ttest is after tout, interpolate to tout */
*tret = tout;
flag = CVodeGetDky(cv_mem, tout, 0, yout);
earlyret = SUNTRUE;
}
else if (ca_mem->ca_rootret)
{
/* tout is after troot, interpolate to troot */
*tret = ca_mem->ca_troot;
flag = CVodeGetDky(cv_mem, ca_mem->ca_troot, 0, yout);
flag = CV_ROOT_RETURN;
ca_mem->ca_rootret = SUNFALSE;
earlyret = SUNTRUE;
}
/* return if necessary */
if (earlyret)
{
*ncheckPtr = ca_mem->ca_nckpnts;
ca_mem->ca_IMnewData = SUNTRUE;
ca_mem->ca_ckpntData = ca_mem->ck_mem;
ca_mem->ca_np = cv_mem->cv_nst % ca_mem->ca_nsteps + 1;
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (flag);
}
}
/* Integrate to tout (in CV_ONE_STEP mode) while loading check points */
nstloc = 0;
for (;;)
{
/* Check for too many steps */
if ((cv_mem->cv_mxstep > 0) && (nstloc >= cv_mem->cv_mxstep))
{
cvProcessError(cv_mem, CV_TOO_MUCH_WORK, __LINE__, __func__, __FILE__,
MSGCV_MAX_STEPS, cv_mem->cv_tn);
flag = CV_TOO_MUCH_WORK;
break;
}
/* Perform one step of the integration */
flag = CVode(cv_mem, tout, yout, tret, CV_ONE_STEP);
if (flag < 0) { break; }
nstloc++;
/* Test if a new check point is needed */
if (cv_mem->cv_nst % ca_mem->ca_nsteps == 0)
{
ca_mem->ck_mem->ck_t1 = cv_mem->cv_tn;
/* Create a new check point, load it, and append it to the list */
tmp = CVAckpntNew(cv_mem);
if (tmp == NULL)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
flag = CV_MEM_FAIL;
break;
}
tmp->ck_next = ca_mem->ck_mem;
ca_mem->ck_mem = tmp;
ca_mem->ca_nckpnts++;
cv_mem->cv_forceSetup = SUNTRUE;
/* Reset i=0 and load dt_mem[0] */
dt_mem[0]->t = ca_mem->ck_mem->ck_t0;
ca_mem->ca_IMstore(cv_mem, dt_mem[0]);
}
else
{
/* Load next point in dt_mem */
dt_mem[cv_mem->cv_nst % ca_mem->ca_nsteps]->t = cv_mem->cv_tn;
ca_mem->ca_IMstore(cv_mem, dt_mem[cv_mem->cv_nst % ca_mem->ca_nsteps]);
}
/* Set t1 field of the current check point structure
for the case in which there will be no future
check points */
ca_mem->ck_mem->ck_t1 = cv_mem->cv_tn;
/* tfinal is now set to tn */
ca_mem->ca_tfinal = cv_mem->cv_tn;
/* Return if in CV_ONE_STEP mode */
if (itask == CV_ONE_STEP) { break; }
/* CV_NORMAL_STEP returns */
/* Return if tout reached */
if ((*tret - tout) * cv_mem->cv_h >= ZERO)
{
/* If this was a root return, save the root time to return later */
if (flag == CV_ROOT_RETURN)
{
ca_mem->ca_rootret = SUNTRUE;
ca_mem->ca_troot = *tret;
}
/* Get solution value at tout to return now */
*tret = tout;
flag = CVodeGetDky(cv_mem, tout, 0, yout);
/* Reset tretlast in cv_mem so that CVodeGetQuad and CVodeGetSens
* evaluate quadratures and/or sensitivities at the proper time */
cv_mem->cv_tretlast = tout;
break;
}
/* Return if tstop or a root was found */
if ((flag == CV_TSTOP_RETURN) || (flag == CV_ROOT_RETURN)) { break; }
} /* end of for(;;) */
/* Get ncheck from ca_mem */
*ncheckPtr = ca_mem->ca_nckpnts;
/* Data is available for the last interval */
ca_mem->ca_IMnewData = SUNTRUE;
ca_mem->ca_ckpntData = ca_mem->ck_mem;
ca_mem->ca_np = cv_mem->cv_nst % ca_mem->ca_nsteps + 1;
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (flag);
}
/*
* =================================================================
* FUNCTIONS FOR BACKWARD PROBLEMS
* =================================================================
*/
int CVodeCreateB(void* cvode_mem, int lmmB, int* which)
{
CVodeMem cv_mem;
CVadjMem ca_mem;
CVodeBMem new_cvB_mem;
void* cvodeB_mem;
/* Check if cvode_mem exists */
if (cvode_mem == NULL)
{
cvProcessError(NULL, CV_MEM_NULL, __LINE__, __func__, __FILE__, MSGCV_NO_MEM);
return (CV_MEM_NULL);
}
cv_mem = (CVodeMem)cvode_mem;
/* Was ASA initialized? */
if (cv_mem->cv_adjMallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_ADJ, __LINE__, __func__, __FILE__, MSGCV_NO_ADJ);
return (CV_NO_ADJ);
}
ca_mem = cv_mem->cv_adj_mem;
/* Allocate space for new CVodeBMem object */
new_cvB_mem = NULL;
new_cvB_mem = (CVodeBMem)malloc(sizeof(struct CVodeBMemRec));
if (new_cvB_mem == NULL)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
return (CV_MEM_FAIL);
}
/* Create and set a new CVODES object for the backward problem */
cvodeB_mem = CVodeCreate(lmmB, cv_mem->cv_sunctx);
if (cvodeB_mem == NULL)
{
cvProcessError(cv_mem, CV_MEM_FAIL, __LINE__, __func__, __FILE__,
MSGCV_MEM_FAIL);
return (CV_MEM_FAIL);
}
CVodeSetUserData(cvodeB_mem, cvode_mem);
CVodeSetMaxHnilWarns(cvodeB_mem, -1);
/* Set/initialize fields in the new CVodeBMem object, new_cvB_mem */
new_cvB_mem->cv_index = ca_mem->ca_nbckpbs;
new_cvB_mem->cv_mem = (CVodeMem)cvodeB_mem;
new_cvB_mem->cv_f = NULL;
new_cvB_mem->cv_fs = NULL;
new_cvB_mem->cv_fQ = NULL;
new_cvB_mem->cv_fQs = NULL;
new_cvB_mem->cv_user_data = NULL;
new_cvB_mem->cv_lmem = NULL;
new_cvB_mem->cv_lfree = NULL;
new_cvB_mem->cv_pmem = NULL;
new_cvB_mem->cv_pfree = NULL;
new_cvB_mem->cv_y = NULL;
new_cvB_mem->cv_f_withSensi = SUNFALSE;
new_cvB_mem->cv_fQ_withSensi = SUNFALSE;
/* Attach the new object to the linked list cvB_mem */
new_cvB_mem->cv_next = ca_mem->cvB_mem;
ca_mem->cvB_mem = new_cvB_mem;
/* Return the index of the newly created CVodeBMem object.
* This must be passed to CVodeInitB and to other ***B
* functions to set optional inputs for this backward problem */
*which = ca_mem->ca_nbckpbs;
ca_mem->ca_nbckpbs++;
return (CV_SUCCESS);
}
int CVodeInitB(void* cvode_mem, int which, CVRhsFnB fB, sunrealtype tB0,
N_Vector yB0)
{
CVodeMem cv_mem;
CVadjMem ca_mem;
CVodeBMem cvB_mem;
void* cvodeB_mem;
int flag;
/* Check if cvode_mem exists */
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);
/* Was ASA initialized? */
if (cv_mem->cv_adjMallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_ADJ, __LINE__, __func__, __FILE__, MSGCV_NO_ADJ);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_NO_ADJ);
}
ca_mem = cv_mem->cv_adj_mem;
/* Check the value of which */
if (which >= ca_mem->ca_nbckpbs)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_WHICH);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* Find the CVodeBMem entry in the linked list corresponding to which */
cvB_mem = ca_mem->cvB_mem;
while (cvB_mem != NULL)
{
if (which == cvB_mem->cv_index) { break; }
cvB_mem = cvB_mem->cv_next;
}
cvodeB_mem = (void*)(cvB_mem->cv_mem);
/* Allocate and set the CVODES object */
flag = CVodeInit(cvodeB_mem, CVArhs, tB0, yB0);
if (flag != CV_SUCCESS)
{
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (flag);
}
/* Copy fB function in cvB_mem */
cvB_mem->cv_f_withSensi = SUNFALSE;
cvB_mem->cv_f = fB;
/* Allocate space and initialize the y Nvector in cvB_mem */
cvB_mem->cv_t0 = tB0;
cvB_mem->cv_y = N_VClone(yB0);
N_VScale(ONE, yB0, cvB_mem->cv_y);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_SUCCESS);
}
int CVodeInitBS(void* cvode_mem, int which, CVRhsFnBS fBs, sunrealtype tB0,
N_Vector yB0)
{
CVodeMem cv_mem;
CVadjMem ca_mem;
CVodeBMem cvB_mem;
void* cvodeB_mem;
int flag;
/* Check if cvode_mem exists */
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);
/* Was ASA initialized? */
if (cv_mem->cv_adjMallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_ADJ, __LINE__, __func__, __FILE__, MSGCV_NO_ADJ);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_NO_ADJ);
}
ca_mem = cv_mem->cv_adj_mem;
/* Check the value of which */
if (which >= ca_mem->ca_nbckpbs)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_WHICH);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* Find the CVodeBMem entry in the linked list corresponding to which */
cvB_mem = ca_mem->cvB_mem;
while (cvB_mem != NULL)
{
if (which == cvB_mem->cv_index) { break; }
cvB_mem = cvB_mem->cv_next;
}
cvodeB_mem = (void*)(cvB_mem->cv_mem);
/* Allocate and set the CVODES object */
flag = CVodeInit(cvodeB_mem, CVArhs, tB0, yB0);
if (flag != CV_SUCCESS)
{
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (flag);
}
/* Copy fBs function in cvB_mem */
cvB_mem->cv_f_withSensi = SUNTRUE;
cvB_mem->cv_fs = fBs;
/* Allocate space and initialize the y Nvector in cvB_mem */
cvB_mem->cv_t0 = tB0;
cvB_mem->cv_y = N_VClone(yB0);
N_VScale(ONE, yB0, cvB_mem->cv_y);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_SUCCESS);
}
int CVodeReInitB(void* cvode_mem, int which, sunrealtype tB0, N_Vector yB0)
{
CVodeMem cv_mem;
CVadjMem ca_mem;
CVodeBMem cvB_mem;
void* cvodeB_mem;
int flag;
/* Check if cvode_mem exists */
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);
/* Was ASA initialized? */
if (cv_mem->cv_adjMallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_ADJ, __LINE__, __func__, __FILE__, MSGCV_NO_ADJ);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_NO_ADJ);
}
ca_mem = cv_mem->cv_adj_mem;
/* Check the value of which */
if (which >= ca_mem->ca_nbckpbs)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_WHICH);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (CV_ILL_INPUT);
}
/* Find the CVodeBMem entry in the linked list corresponding to which */
cvB_mem = ca_mem->cvB_mem;
while (cvB_mem != NULL)
{
if (which == cvB_mem->cv_index) { break; }
cvB_mem = cvB_mem->cv_next;
}
cvodeB_mem = (void*)(cvB_mem->cv_mem);
/* Reinitialize CVODES object */
flag = CVodeReInit(cvodeB_mem, tB0, yB0);
SUNDIALS_MARK_FUNCTION_END(CV_PROFILER);
return (flag);
}
int CVodeSStolerancesB(void* cvode_mem, int which, sunrealtype reltolB,
sunrealtype abstolB)
{
CVodeMem cv_mem;
CVadjMem ca_mem;
CVodeBMem cvB_mem;
void* cvodeB_mem;
int flag;
/* Check if cvode_mem exists */
if (cvode_mem == NULL)
{
cvProcessError(NULL, CV_MEM_NULL, __LINE__, __func__, __FILE__, MSGCV_NO_MEM);
return (CV_MEM_NULL);
}
cv_mem = (CVodeMem)cvode_mem;
/* Was ASA initialized? */
if (cv_mem->cv_adjMallocDone == SUNFALSE)
{
cvProcessError(cv_mem, CV_NO_ADJ, __LINE__, __func__, __FILE__, MSGCV_NO_ADJ);
return (CV_NO_ADJ);
}
ca_mem = cv_mem->cv_adj_mem;
/* Check the value of which */
if (which >= ca_mem->ca_nbckpbs)
{
cvProcessError(cv_mem, CV_ILL_INPUT, __LINE__, __func__, __FILE__,
MSGCV_BAD_WHICH);
return (CV_ILL_INPUT);
}
/* Find the CVodeBMem entry in the linked list corresponding to which */
cvB_mem = ca_mem->cvB_mem;
while (cvB_mem != NULL)
{