Skip to content

Commit 6b9cddd

Browse files
committed
add backward integration test
1 parent 2caa860 commit 6b9cddd

File tree

1 file changed

+138
-12
lines changed

1 file changed

+138
-12
lines changed

test/unit_tests/cvode/C_serial/cv_test_tstop.c

Lines changed: 138 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
#include "sunmatrix/sunmatrix_dense.h"
2626

2727
#if defined(SUNDIALS_EXTENDED_PRECISION)
28-
#define GSYM "Lg"
28+
#define FMT "-7.4Lg"
2929
#else
30-
#define GSYM "g"
30+
#define FMT "-7.4g"
3131
#endif
3232

3333
#define ZERO SUN_RCONST(0.0)
@@ -58,14 +58,15 @@ int main(int argc, char* argv[])
5858

5959
int flag = 0;
6060
int cvode_flag = 0;
61-
int i = 0;
6261
sunrealtype tout = SUN_RCONST(0.10);
6362
sunrealtype dt_tout = SUN_RCONST(0.25);
6463
sunrealtype tstop = SUN_RCONST(0.30);
6564
sunrealtype dt_tstop = SUN_RCONST(0.30);
6665
sunrealtype tret = ZERO;
6766
sunrealtype tcur = ZERO;
6867

68+
long int num_steps = 0;
69+
6970
/* --------------
7071
* Create context
7172
* -------------- */
@@ -116,15 +117,140 @@ int main(int argc, char* argv[])
116117
flag = CVodeSetStopTime(cvode_mem, tstop);
117118
if (flag) { return 1; }
118119

119-
/* ---------------
120-
* Advance in time
121-
* --------------- */
120+
/* -------------------
121+
* Forward Integration
122+
* ------------------- */
123+
124+
printf("0: tout = %" FMT " tstop = %" FMT " tret = %" FMT
125+
" tcur = %" FMT " steps = 0\n",
126+
tout, tstop, tret, tcur);
127+
128+
for (int i = 1; i <= 6; i++)
129+
{
130+
cvode_flag = CVode(cvode_mem, tout, y, &tret, CV_NORMAL);
131+
if (cvode_flag < 0)
132+
{
133+
flag = 1;
134+
break;
135+
}
136+
137+
flag = CVodeGetCurrentTime(cvode_mem, &tcur);
138+
if (flag) { break; }
139+
140+
flag = CVodeGetNumSteps(cvode_mem, &num_steps);
141+
if (flag) { break; }
142+
143+
printf("%i: tout = %" FMT " tstop = %" FMT " tret = %" FMT
144+
" tcur = %" FMT " steps = %3li return = %i\n",
145+
i, tout, tstop, tret, tcur, num_steps, cvode_flag);
146+
147+
/* First return: output time < stop time < current time -- should return at
148+
the output time */
149+
if (i == 1 && cvode_flag != CV_SUCCESS)
150+
{
151+
printf("ERROR: Expected output return!\n");
152+
flag = 1;
153+
break;
154+
}
155+
156+
/* Second return: output time > stop time = current time -- should return at
157+
the stop time */
158+
if (i == 2)
159+
{
160+
if (cvode_flag != CV_TSTOP_RETURN)
161+
{
162+
printf("ERROR: Expected stop return!\n");
163+
flag = 1;
164+
break;
165+
}
166+
167+
/* Update stop time */
168+
tstop += dt_tstop;
169+
flag = CVodeSetStopTime(cvode_mem, tstop);
170+
if (flag) { break; }
171+
}
172+
173+
/* Third return: output time = stop time = current time -- should return at
174+
the stop time */
175+
if (i == 3)
176+
{
177+
if (cvode_flag != CV_TSTOP_RETURN)
178+
{
179+
printf("ERROR: Expected stop return!\n");
180+
flag = 1;
181+
break;
182+
}
183+
184+
/* Update stop time */
185+
tstop += dt_tstop;
186+
flag = CVodeSetStopTime(cvode_mem, tstop);
187+
if (flag) { break; }
188+
}
189+
190+
/* Fourth return: output time < stop time = current time and the output time
191+
is overtaken in the same step that the stop time is reached -- should
192+
return at the output time with current time == stop time */
193+
if (i == 4)
194+
{
195+
if (cvode_flag != CV_SUCCESS)
196+
{
197+
printf("ERROR: Expected output return!\n");
198+
flag = 1;
199+
break;
200+
}
201+
}
202+
203+
/* Fifth return: output time > stop time = current time and the stop time
204+
was already reached but we did not return at that time because of an
205+
earlier output time return -- should return at the stop time without
206+
taking a step */
207+
if (i == 5)
208+
{
209+
if (cvode_flag != CV_TSTOP_RETURN)
210+
{
211+
printf("ERROR: Expected stop return!\n");
212+
flag = 1;
213+
break;
214+
}
215+
}
216+
217+
/* Sixth return: current time > output time > stop time (not updated) --
218+
should return at the output time */
219+
if (i == 6 && cvode_flag != CV_SUCCESS)
220+
{
221+
printf("ERROR: Expected output return!\n");
222+
flag = 1;
223+
break;
224+
}
225+
226+
/* update output time */
227+
tout += dt_tout;
228+
}
229+
230+
/* --------------------
231+
* Backward Integration
232+
* -------------------- */
233+
234+
N_VConst(ONE, y);
235+
236+
CVodeReInit(cvode_mem, ONE, y);
237+
if (flag) { return 1; }
238+
239+
tout = SUN_RCONST(0.30);
240+
dt_tout = -dt_tout;
241+
tstop = SUN_RCONST(0.10);
242+
dt_tstop = -dt_tstop;
243+
tret = ZERO;
244+
tcur = ZERO;
245+
246+
flag = CVodeSetStopTime(cvode_mem, tstop);
247+
if (flag) { return 1; }
122248

123-
printf("0: tout = %" GSYM ", tstop = %" GSYM ", tret = %" GSYM
124-
", tcur = %" GSYM "\n",
249+
printf("0: tout = %" FMT " tstop = %" FMT " tret = %" FMT
250+
" tcur = %" FMT " steps = 0\n",
125251
tout, tstop, tret, tcur);
126252

127-
for (i = 1; i <= 6; i++)
253+
for (int i = 1; i <= 6; i++)
128254
{
129255
cvode_flag = CVode(cvode_mem, tout, y, &tret, CV_NORMAL);
130256
if (cvode_flag < 0)
@@ -136,9 +262,9 @@ int main(int argc, char* argv[])
136262
flag = CVodeGetCurrentTime(cvode_mem, &tcur);
137263
if (flag) { break; }
138264

139-
printf("%i: tout = %" GSYM ", tstop = %" GSYM ", tret = %" GSYM
140-
", tcur = %" GSYM ", return = %i\n",
141-
i, tout, tstop, tret, tcur, cvode_flag);
265+
printf("%i: tout = %" FMT " tstop = %" FMT " tret = %" FMT
266+
" tcur = %" FMT " steps = %3li return = %i\n",
267+
i, tout, tstop, tret, tcur, num_steps, cvode_flag);
142268

143269
/* First return: output time < stop time */
144270
if (i == 1 && cvode_flag != CV_SUCCESS)

0 commit comments

Comments
 (0)