Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeStepControl.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 
14 #include "Thyra_VectorStdOps.hpp"
15 
16 #include "Tempus_TimeStepControl.hpp"
21 
22 #include "../TestModels/SinCosModel.hpp"
23 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
24 
25 #include <fstream>
26 #include <vector>
27 
28 namespace Tempus_Unit_Test {
29 
30 using Teuchos::RCP;
31 using Teuchos::rcp;
32 using Teuchos::rcp_const_cast;
33 using Teuchos::rcp_dynamic_cast;
35 using Teuchos::sublist;
36 using Teuchos::getParametersFromXmlFile;
37 
38 
39 // ************************************************************
40 // ************************************************************
41 TEUCHOS_UNIT_TEST(TimeStepControl, Default_Construction)
42 {
43  auto tsc = rcp(new Tempus::TimeStepControl<double>());
44  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
45 
46  // Test the get functions (i.e., defaults).
47  TEST_COMPARE ( tsc->getStepType() , ==, "Constant" );
48  TEST_FLOATING_EQUALITY( tsc->getInitTime() , 0.0 , 1.0e-14);
49  TEST_FLOATING_EQUALITY( tsc->getFinalTime() , 1.0e+99, 1.0e-14);
50  TEST_FLOATING_EQUALITY( tsc->getMinTimeStep() , 0.0 , 1.0e-14);
51  TEST_FLOATING_EQUALITY( tsc->getInitTimeStep() , 1.0 , 1.0e-14);
52  TEST_FLOATING_EQUALITY( tsc->getMaxTimeStep() , 1.0e+99, 1.0e-14);
53  TEST_COMPARE ( tsc->getInitIndex() , ==, 0 );
54  TEST_COMPARE ( tsc->getFinalIndex() , ==, 1000000 );
55  TEST_FLOATING_EQUALITY( tsc->getMaxAbsError() , 1.0e-08, 1.0e-14);
56  TEST_FLOATING_EQUALITY( tsc->getMaxRelError() , 1.0e-08, 1.0e-14);
57  TEST_COMPARE ( tsc->getMaxFailures() , ==, 10 );
58  TEST_COMPARE ( tsc->getMaxConsecFailures() , ==, 5 );
59  TEST_COMPARE ( tsc->getNumTimeSteps() , ==, -1 );
60  TEST_COMPARE ( tsc->getPrintDtChanges() , ==, true );
61  TEST_COMPARE ( tsc->getOutputExactly() , ==, true );
62  TEST_COMPARE ( tsc->getOutputIndexInterval(), ==, 1000000 );
63  TEST_FLOATING_EQUALITY( tsc->getOutputTimeInterval() , 1.0e+99, 1.0e-14);
64 
65  // Test the set functions.
66  tsc->setInitTime(1.0); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
67  tsc->setFinalTime(100.0); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
68  tsc->setMinTimeStep(0.01); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
69  tsc->setInitTimeStep(0.02); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
70  tsc->setMaxTimeStep(0.05); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
71  tsc->setInitIndex(-100); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
72  tsc->setFinalIndex(100); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
73  tsc->setMaxAbsError(1.0e-06); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
74  tsc->setMaxRelError(1.0e-06); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
75  tsc->setMaxFailures(8); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
76  tsc->setMaxConsecFailures(4); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
77  tsc->setNumTimeSteps(-1); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
78  tsc->setPrintDtChanges(false); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
79  tsc->setOutputExactly(false); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
80  tsc->setOutputIndexInterval(9); tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
81  tsc->setOutputTimeInterval(0.1);tsc->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
82 
83  TEST_COMPARE ( tsc->getStepType() , ==, "Constant" );
84  TEST_FLOATING_EQUALITY( tsc->getInitTime() , 1.0 , 1.0e-14);
85  TEST_FLOATING_EQUALITY( tsc->getFinalTime() , 100.0 , 1.0e-14);
86  TEST_FLOATING_EQUALITY( tsc->getMinTimeStep() , 0.01 , 1.0e-14);
87  TEST_FLOATING_EQUALITY( tsc->getInitTimeStep() , 0.02 , 1.0e-14);
88  TEST_FLOATING_EQUALITY( tsc->getMaxTimeStep() , 0.05 , 1.0e-14);
89  TEST_COMPARE ( tsc->getInitIndex() , ==, -100 );
90  TEST_COMPARE ( tsc->getFinalIndex() , ==, 100 );
91  TEST_FLOATING_EQUALITY( tsc->getMaxAbsError() , 1.0e-06, 1.0e-14);
92  TEST_FLOATING_EQUALITY( tsc->getMaxRelError() , 1.0e-06, 1.0e-14);
93  TEST_COMPARE ( tsc->getMaxFailures() , ==, 8 );
94  TEST_COMPARE ( tsc->getMaxConsecFailures() , ==, 4 );
95  TEST_COMPARE ( tsc->getNumTimeSteps() , ==, -1 );
96  TEST_COMPARE ( tsc->getPrintDtChanges() , ==, false );
97  TEST_COMPARE ( tsc->getOutputExactly() , ==, false );
98  TEST_COMPARE ( tsc->getOutputIndexInterval(), ==, 9 );
99  TEST_FLOATING_EQUALITY( tsc->getOutputTimeInterval() , 0.1 , 1.0e-14);
100 }
101 
102 
103 // ************************************************************
104 // ************************************************************
105 TEUCHOS_UNIT_TEST(TimeStepControl, Full_Construction)
106 {
107  std::vector<int> outputIndices;
108  outputIndices.push_back(7);
109  outputIndices.push_back(11);
110  outputIndices.push_back(13);
111 
112  std::vector<double> outputTimes;
113  outputTimes.push_back(0.3);
114  outputTimes.push_back(0.7);
115  outputTimes.push_back(1.3);
116  outputTimes.push_back(1.7);
117 
118  auto tscsc =
120 
121  auto tsc = rcp(new Tempus::TimeStepControl<double>(
122  1.0, 100.0, 0.01, 0.02, 0.05, -100,
123  100, 1.0e-06, 1.0e-06, 8, 4, -1, false, false,
124  outputIndices, outputTimes, 9, 0.011, tscsc));
125 
126  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
127 
128  TEST_COMPARE ( tsc->getStepType() , ==, "Constant" );
129  TEST_FLOATING_EQUALITY( tsc->getInitTime() , 1.0 , 1.0e-14);
130  TEST_FLOATING_EQUALITY( tsc->getFinalTime() , 100.0 , 1.0e-14);
131  TEST_FLOATING_EQUALITY( tsc->getMinTimeStep() , 0.01 , 1.0e-14);
132  TEST_FLOATING_EQUALITY( tsc->getInitTimeStep() , 0.02 , 1.0e-14);
133  TEST_FLOATING_EQUALITY( tsc->getMaxTimeStep() , 0.05 , 1.0e-14);
134  TEST_COMPARE ( tsc->getInitIndex() , ==, -100 );
135  TEST_COMPARE ( tsc->getFinalIndex() , ==, 100 );
136  TEST_FLOATING_EQUALITY( tsc->getMaxAbsError() , 1.0e-06, 1.0e-14);
137  TEST_FLOATING_EQUALITY( tsc->getMaxRelError() , 1.0e-06, 1.0e-14);
138  TEST_COMPARE ( tsc->getMaxFailures() , ==, 8 );
139  TEST_COMPARE ( tsc->getMaxConsecFailures() , ==, 4 );
140  TEST_COMPARE ( tsc->getNumTimeSteps() , ==, -1 );
141  TEST_COMPARE ( tsc->getPrintDtChanges() , ==, false );
142  TEST_COMPARE ( tsc->getOutputExactly() , ==, false );
143  TEST_COMPARE ( tsc->getOutputIndexInterval(), ==, 9 );
144  TEST_FLOATING_EQUALITY( tsc->getOutputTimeInterval() , 0.011 , 1.0e-14);
145 
146 }
147 
148 
149 // ************************************************************
150 // ************************************************************
151 TEUCHOS_UNIT_TEST(TimeStepControl, Create_Construction)
152 {
154  Tempus::getTimeStepControlPL<double>();
155 
156  pl->set<double> ("Initial Time" , 1.0);
157  pl->set<double> ("Final Time" , 100.0);
158  pl->set<double> ("Minimum Time Step" , 0.01);
159  pl->set<double> ("Initial Time Step" , 0.02);
160  pl->set<double> ("Maximum Time Step" , 0.05);
161  pl->set<int> ("Initial Time Index" , -100);
162  pl->set<int> ("Final Time Index" , 100);
163  pl->set<double> ("Maximum Absolute Error", 1.0e-06);
164  pl->set<double> ("Maximum Relative Error", 1.0e-06);
165  pl->set<int> ("Maximum Number of Stepper Failures", 8);
166  pl->set<int> ("Maximum Number of Consecutive Stepper Failures", 4);
167  pl->set<int> ("Number of Time Steps" , -1);
168  pl->set<bool> ("Print Time Step Changes",false);
169  pl->set<bool> ("Output Exactly On Output Times", false);
170  pl->set<std::string>("Output Index List" , "7, 11, 13" );
171  pl->set<std::string>("Output Time List" , "0.3, 0.7, 1.3, 1.7");
172  pl->set<int> ("Output Index Interval" , 9);
173  pl->set<double> ("Output Time Interval" , 0.011);
174 
176  auto tscsPL = tscs->getValidParameters();
177  pl->set("Time Step Control Strategy", *tscsPL);
178  //std::cout << " pl = " << *pl << std::endl;
179 
180  auto tsc = Tempus::createTimeStepControl<double>(pl);
181  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
182 
183  //Teuchos::RCP<Teuchos::FancyOStream> my_out =
184  // Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
185  //tsc->describe(*my_out, Teuchos::VERB_EXTREME);
186 
187  TEST_COMPARE ( tsc->getStepType() , ==, "Constant" );
188  TEST_FLOATING_EQUALITY( tsc->getInitTime() , 1.0 , 1.0e-14);
189  TEST_FLOATING_EQUALITY( tsc->getFinalTime() , 100.0 , 1.0e-14);
190  TEST_FLOATING_EQUALITY( tsc->getMinTimeStep() , 0.01 , 1.0e-14);
191  TEST_FLOATING_EQUALITY( tsc->getInitTimeStep() , 0.02 , 1.0e-14);
192  TEST_FLOATING_EQUALITY( tsc->getMaxTimeStep() , 0.05 , 1.0e-14);
193  TEST_COMPARE ( tsc->getInitIndex() , ==, -100 );
194  TEST_COMPARE ( tsc->getFinalIndex() , ==, 100 );
195  TEST_FLOATING_EQUALITY( tsc->getMaxAbsError() , 1.0e-06, 1.0e-14);
196  TEST_FLOATING_EQUALITY( tsc->getMaxRelError() , 1.0e-06, 1.0e-14);
197  TEST_COMPARE ( tsc->getMaxFailures() , ==, 8 );
198  TEST_COMPARE ( tsc->getMaxConsecFailures() , ==, 4 );
199  TEST_COMPARE ( tsc->getNumTimeSteps() , ==, -1 );
200  TEST_COMPARE ( tsc->getPrintDtChanges() , ==, false );
201  TEST_COMPARE ( tsc->getOutputExactly() , ==, false );
202  TEST_COMPARE ( tsc->getOutputIndices()[0] , ==, 7 );
203  TEST_COMPARE ( tsc->getOutputIndices()[1] , ==, 11 );
204  TEST_COMPARE ( tsc->getOutputIndices()[2] , ==, 13 );
205  TEST_FLOATING_EQUALITY( tsc->getOutputTimes()[0] , 0.3 , 1.0e-14);
206  TEST_FLOATING_EQUALITY( tsc->getOutputTimes()[1] , 0.7 , 1.0e-14);
207  TEST_FLOATING_EQUALITY( tsc->getOutputTimes()[2] , 1.3 , 1.0e-14);
208  TEST_FLOATING_EQUALITY( tsc->getOutputTimes()[3] , 1.7 , 1.0e-14);
209  TEST_COMPARE ( tsc->getOutputIndexInterval(), ==, 9 );
210  TEST_FLOATING_EQUALITY( tsc->getOutputTimeInterval() , 0.011 , 1.0e-14);
211 
212 }
213 
214 
215 // ************************************************************
216 // ************************************************************
217 TEUCHOS_UNIT_TEST(TimeStepControl, Accessors)
218 {
219  auto tsc = rcp(new Tempus::TimeStepControl<double>());
220  int iSet = 17;
221  double dSet = 0.989;
222 
223  tsc->setInitTime(dSet); TEST_COMPARE( tsc->getInitTime(), ==, dSet);
224  tsc->setFinalTime(dSet); TEST_COMPARE( tsc->getFinalTime(), ==, dSet);
225  tsc->setMinTimeStep(dSet); TEST_COMPARE( tsc->getMinTimeStep(), ==, dSet);
226  tsc->setInitTimeStep(dSet); TEST_COMPARE( tsc->getInitTimeStep(), ==, dSet);
227  tsc->setMaxTimeStep(dSet); TEST_COMPARE( tsc->getMaxTimeStep(), ==, dSet);
228  tsc->setInitIndex(iSet); TEST_COMPARE( tsc->getInitIndex(), ==, iSet);
229  tsc->setFinalIndex(iSet); TEST_COMPARE( tsc->getFinalIndex(), ==, iSet);
230  tsc->setMaxAbsError(dSet); TEST_COMPARE( tsc->getMaxAbsError(), ==, dSet);
231  tsc->setMaxRelError(dSet); TEST_COMPARE( tsc->getMaxRelError(), ==, dSet);
232  tsc->setOutputExactly(false); TEST_COMPARE( tsc->getOutputExactly(), ==, false);
233  tsc->setOutputExactly(true); TEST_COMPARE( tsc->getOutputExactly(), ==, true);
234 
235  std::vector<int> iVSet{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
236  tsc->setOutputIndices(iVSet); TEUCHOS_TEST_FOR_EXCEPT(tsc->getOutputIndices() != iVSet);
237 
238 
239  tsc->setOutputIndexInterval(iSet); TEST_COMPARE( tsc->getOutputIndexInterval(), ==, iSet);
240  tsc->setOutputTimeInterval(dSet); TEST_COMPARE( tsc->getOutputTimeInterval(), ==, dSet);
241 }
242 
243 
244 // ************************************************************
245 // ************************************************************
246 TEUCHOS_UNIT_TEST(TimeStepControl, setOutputTimes)
247 {
248  auto tsc = rcp(new Tempus::TimeStepControl<double>());
249 
250  std::vector<double> times_in;
251  times_in.push_back(0.0000000000000000e-11);
252  times_in.push_back(0.1001384570000000e-11);
253  times_in.push_back(0.2002769140000000e-11);
254  times_in.push_back(0.3004153710000000e-11);
255  times_in.push_back(0.4005538280000000e-11);
256  times_in.push_back(0.5006922850000000e-11);
257  times_in.push_back(0.6008307420000000e-11);
258  times_in.push_back(0.7009691990000000e-11);
259  times_in.push_back(0.8011076560000000e-11);
260  times_in.push_back(0.9012461130000000e-11);
261  times_in.push_back(1.0013845700000000e-11);
262 
263  tsc->setOutputTimes(times_in);
264  tsc->initialize();
265  auto times_out = tsc->getOutputTimes();
266  double maxDiff = 0.0;
267 
268  //std::cout << "\n times_in, times_out = " << std::endl;
269  for (size_t i=0; i < times_in.size(); ++i) {
270  //std::cout << std::setw(25) << std::setprecision(16) << times_in[i] << ","
271  // << std::setw(25) << std::setprecision(16) << times_out[i]
272  // << std::endl;
273  maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
274  }
275  //std::cout << " maxDiff = " << maxDiff << std::endl;
276 
277  TEST_COMPARE(maxDiff, <, 1.0e-25);
278 
279 
280  times_in.clear();
281  times_in.push_back(0.00000000000000000000000000000000);
282  times_in.push_back(0.00000000000100138457000000009381);
283  times_in.push_back(0.00000000000200276914000000018762);
284  times_in.push_back(0.00000000000300415371000000007949);
285  times_in.push_back(0.00000000000400553828000000037525);
286  times_in.push_back(0.00000000000500692284999999986321);
287  times_in.push_back(0.00000000000600830742000000015898);
288  times_in.push_back(0.00000000000700969198999999964694);
289  times_in.push_back(0.00000000000801107656000000075050);
290  times_in.push_back(0.00000000000901246112999999943067);
291  times_in.push_back(0.00000000001001384569999999972643);
292 
293  tsc->setOutputTimes(times_in);
294  tsc->initialize();
295  times_out = tsc->getOutputTimes();
296  maxDiff = 0.0;
297 
298  //std::cout << "\n times_in, times_out = " << std::endl;
299  for (size_t i=0; i < times_in.size(); ++i) {
300  //std::cout << std::setw(30) << std::setprecision(20) << times_in[i] << ","
301  // << std::setw(30) << std::setprecision(20) << times_out[i]
302  // << std::endl;
303  maxDiff = std::max(std::fabs(times_in[i] - times_out[i]), maxDiff);
304  }
305  //std::cout << " maxDiff = " << maxDiff << std::endl;
306 
307  TEST_COMPARE(maxDiff, <, 1.0e-25);
308 }
309 
310 
311 // ************************************************************
312 // ************************************************************
313 TEUCHOS_UNIT_TEST(TimeStepControl, getOutputIndicesandIntervals)
314 {
315  auto tsc = rcp(new Tempus::TimeStepControl<double>());
316  int setOutputTimeIndex = 17;
317  double setOutputTimeInterval = 1.101001000100001e-7;
318 
319  tsc->setOutputIndexInterval(setOutputTimeIndex);
320  tsc->setOutputTimeInterval(setOutputTimeInterval);
321 
322  int getOutputTimeIndex = tsc->getOutputIndexInterval();
323  double getOutputTimeInterval = tsc->getOutputTimeInterval();
324  TEST_COMPARE(getOutputTimeInterval, ==, setOutputTimeInterval);
325  TEST_COMPARE(getOutputTimeIndex, ==, setOutputTimeIndex);
326 }
327 
328 
329 // ************************************************************
330 // ************************************************************
331 TEUCHOS_UNIT_TEST(TimeStepControl, timeInRange)
332 {
333  auto tsc = rcp(new Tempus::TimeStepControl<double>());
334  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
335 
336  // Testing lambda function
337  auto testTimeInRange = [=] (double initTime, double finalTime)
338  {
339  tsc->setInitTime (initTime);
340  tsc->setFinalTime(finalTime);
341  tsc->initialize();
342  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
343 
344  const int i = (initTime == 0) ? 0 : 1 + (int)std::floor(std::log10(std::fabs(initTime) ) );
345  const double absTolInit10 = std::pow(10, i-10);
346  const double absTolInit15 = std::pow(10, i-15);
347  const int j = (finalTime == 0) ? 0 : 1 + (int)std::floor(std::log10(std::fabs(finalTime) ) );
348  const double absTolFinal10 = std::pow(10, j-10);
349  const double absTolFinal15 = std::pow(10, j-15);
350 
351 
352  // Classic unit testing of critical values.
353  if ( initTime == 0.0 ) {
354  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange( initTime - 0.1));
355  } else {
356  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange( initTime - 0.1*std::fabs(initTime)));
357  }
358  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange( initTime - absTolInit10));
359  TEUCHOS_TEST_FOR_EXCEPT(!tsc->timeInRange( initTime - absTolInit15));
360  TEUCHOS_TEST_FOR_EXCEPT(!tsc->timeInRange( initTime ));
361  TEUCHOS_TEST_FOR_EXCEPT(!tsc->timeInRange( initTime + absTolInit15));
362  TEUCHOS_TEST_FOR_EXCEPT(!tsc->timeInRange( initTime + absTolInit10));
363  TEUCHOS_TEST_FOR_EXCEPT(!tsc->timeInRange( initTime + 0.3*(std::fabs(finalTime-initTime))));
364  TEUCHOS_TEST_FOR_EXCEPT(!tsc->timeInRange(finalTime - absTolFinal10));
365  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange(finalTime - absTolFinal15));
366  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange(finalTime ));
367  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange(finalTime + absTolFinal15));
368  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange(finalTime + absTolFinal10));
369  if ( finalTime == 0.0 ) {
370  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange(finalTime + 0.1));
371  } else {
372  TEUCHOS_TEST_FOR_EXCEPT( tsc->timeInRange(finalTime + 0.1*std::fabs(finalTime)));
373  }
374  };
375 
376  // Test with initTime = 0.0
377  testTimeInRange (0.0, 1.0);
378 
379  // Test with finalTime = 0.0
380  testTimeInRange (-1.0, 0.0);
381 
382  // Test large and small times
383  testTimeInRange ( 9.9e-20, 3.3e+20);
384  testTimeInRange (-1.9e+20, 2.3e-20);
385 }
386 
387 
388 // ************************************************************
389 // ************************************************************
390 TEUCHOS_UNIT_TEST(TimeStepControl, indexInRange)
391 {
392  auto tsc = rcp(new Tempus::TimeStepControl<double>());
393  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
394 
395  // Testing lambda function
396  auto testIndexInRange = [=] (double initIndex, double finalIndex)
397  {
398  tsc->setInitIndex (initIndex);
399  tsc->setFinalIndex(finalIndex);
400  tsc->initialize();
401  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
402 
403  // Classic unit testing of critical values.
404  TEUCHOS_TEST_FOR_EXCEPT( tsc->indexInRange( initIndex - 7));
405  TEUCHOS_TEST_FOR_EXCEPT( tsc->indexInRange( initIndex - 1));
406  TEUCHOS_TEST_FOR_EXCEPT(!tsc->indexInRange( initIndex ));
407  TEUCHOS_TEST_FOR_EXCEPT(!tsc->indexInRange( initIndex + 1));
408  TEUCHOS_TEST_FOR_EXCEPT(!tsc->indexInRange( initIndex + (int)0.3*(std::fabs(finalIndex-initIndex))));
409  TEUCHOS_TEST_FOR_EXCEPT(!tsc->indexInRange(finalIndex - 1));
410  TEUCHOS_TEST_FOR_EXCEPT( tsc->indexInRange(finalIndex ));
411  TEUCHOS_TEST_FOR_EXCEPT( tsc->indexInRange(finalIndex + 1));
412  TEUCHOS_TEST_FOR_EXCEPT( tsc->indexInRange(finalIndex + 7));
413  };
414 
415  // Test with initIndex = 0.0
416  testIndexInRange (0, 10);
417 
418  // Test with finalIndex = 0.0
419  testIndexInRange (-10, 0);
420 
421  // Test large and small indices
422  testIndexInRange (-190000, 20);
423  testIndexInRange (-19, 200000);
424 }
425 
426 
427 // ************************************************************
428 // ************************************************************
429 TEUCHOS_UNIT_TEST(TimeStepControl, getValidParameters)
430 {
431  std::vector<int> outputIndices;
432  outputIndices.push_back(7);
433  outputIndices.push_back(11);
434  outputIndices.push_back(13);
435 
436  std::vector<double> outputTimes;
437  outputTimes.push_back(0.3);
438  outputTimes.push_back(0.7);
439  outputTimes.push_back(1.3);
440  outputTimes.push_back(1.7);
441 
442  auto tscsc =
444 
445  auto tsc = rcp(new Tempus::TimeStepControl<double>(
446  1.0, 100.0, 0.01, 0.02, 0.05, -100,
447  100, 1.0e-06, 1.0e-06, 8, 4, -1, false, false,
448  outputIndices, outputTimes, 9, 0.011, tscsc));
449  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
450 
451  auto pl = tsc->getValidParameters();
452 
453  TEST_FLOATING_EQUALITY( pl->get<double>("Initial Time") , 1.0 , 1.0e-14);
454  TEST_FLOATING_EQUALITY( pl->get<double>("Final Time") , 100.0 , 1.0e-14);
455  TEST_FLOATING_EQUALITY( pl->get<double>("Minimum Time Step") , 0.01 , 1.0e-14);
456  TEST_FLOATING_EQUALITY( pl->get<double>("Initial Time Step") , 0.02 , 1.0e-14);
457  TEST_FLOATING_EQUALITY( pl->get<double>("Maximum Time Step") , 0.05 , 1.0e-14);
458  TEST_COMPARE ( pl->get<int> ("Initial Time Index") , ==, -100 );
459  TEST_COMPARE ( pl->get<int> ("Final Time Index") , ==, 100 );
460  TEST_FLOATING_EQUALITY( pl->get<double>("Maximum Absolute Error") , 1.0e-06, 1.0e-14);
461  TEST_FLOATING_EQUALITY( pl->get<double>("Maximum Relative Error") , 1.0e-06, 1.0e-14);
462  TEST_COMPARE ( pl->get<int> ("Maximum Number of Stepper Failures") , ==, 8);
463  TEST_COMPARE ( pl->get<int> ("Maximum Number of Consecutive Stepper Failures"), ==, 4);
464  TEST_COMPARE ( pl->get<int> ("Number of Time Steps") , ==, -1 );
465  TEST_COMPARE ( pl->get<bool> ("Print Time Step Changes") , ==, false );
466  TEST_COMPARE ( pl->get<bool> ("Output Exactly On Output Times"), ==, false );
467  TEST_COMPARE ( pl->get<std::string>("Output Index List") , ==, "7, 11, 13" );
468  TEST_COMPARE ( pl->get<std::string>("Output Time List") , ==, "0.3, 0.7, 1.3, 1.7");
469  TEST_COMPARE ( pl->get<int> ("Output Index Interval") , ==, 9 );
470  TEST_FLOATING_EQUALITY( pl->get<double>("Output Time Interval") , 0.011 , 1.0e-14);
471 
472  { // Ensure that parameters are "used", excluding sublists.
473  std::ostringstream unusedParameters;
474  pl->unused(unusedParameters);
475  TEST_COMPARE ( unusedParameters.str(), ==,
476  "WARNING: Parameter \"Time Step Control Strategy\" [unused] is unused\n");
477  }
478 
479  auto tscs_PL = pl->sublist("Time Step Control Strategy");
480  TEST_COMPARE ( tscs_PL.get<std::string>("Strategy Type") , ==, "Constant");
481  TEST_FLOATING_EQUALITY( tscs_PL.get<double>("Time Step"), 0.0, 1.0e-14);
482 
483  { // Ensure that parameters are "used", excluding sublists.
484  std::ostringstream unusedParameters;
485  tscs_PL.unused(unusedParameters);
486  TEST_COMPARE ( unusedParameters.str(), ==, "");
487  }
488 
489 }
490 
491 
492 // ************************************************************
493 // ************************************************************
494 TEUCHOS_UNIT_TEST(TimeStepControl, setNumTimeSteps)
495 {
496  auto tsc = rcp(new Tempus::TimeStepControl<double>());
497  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
498 
499  tsc->setInitTime(0.0);
500  tsc->setFinalTime(100.0);
501  tsc->setMinTimeStep(0.01);
502  tsc->setInitTimeStep(0.02);
503  tsc->setMaxTimeStep(0.05);
504  tsc->setNumTimeSteps(-1);
505  tsc->initialize();
506  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
507 
508  TEST_FLOATING_EQUALITY( tsc->getInitTime() , 0.0 , 1.0e-14);
509  TEST_FLOATING_EQUALITY( tsc->getFinalTime() , 100.0, 1.0e-14);
510  TEST_FLOATING_EQUALITY( tsc->getMinTimeStep() , 0.01 , 1.0e-14);
511  TEST_FLOATING_EQUALITY( tsc->getInitTimeStep(), 0.02 , 1.0e-14);
512  TEST_FLOATING_EQUALITY( tsc->getMaxTimeStep() , 0.05 , 1.0e-14);
513  TEST_COMPARE ( tsc->getNumTimeSteps(), ==, -1 );
514 
515  tsc->setNumTimeSteps(100);
516  tsc->initialize();
517 
518  TEST_FLOATING_EQUALITY( tsc->getInitTime() , 0.0 , 1.0e-14);
519  TEST_FLOATING_EQUALITY( tsc->getFinalTime() , 100.0, 1.0e-14);
520  TEST_FLOATING_EQUALITY( tsc->getMinTimeStep() , 1.0 , 1.0e-14);
521  TEST_FLOATING_EQUALITY( tsc->getInitTimeStep(), 1.0 , 1.0e-14);
522  TEST_FLOATING_EQUALITY( tsc->getMaxTimeStep() , 1.0 , 1.0e-14);
523  TEST_COMPARE ( tsc->getNumTimeSteps(), ==, 100 );
524 
525 }
526 
527 
528 // ************************************************************
529 // ************************************************************
530 TEUCHOS_UNIT_TEST(TimeStepControl, SetDtAfterOutput_Variable)
531 {
532  // Setup the SolutionHistory --------------------------------
533  auto model = rcp(new Tempus_Test::SinCosModel<double>());
534  auto inArgsIC = model->getNominalValues();
535  auto icSolution = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
536  auto icState = Tempus::createSolutionStateX<double>(icSolution);
537  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
538  solutionHistory->addState(icState);
539  double dt = 0.9;
540  solutionHistory->getCurrentState()->setTimeStep(dt);
541 
542  // Setup the TimeStepControl --------------------------------
543  auto tsc = rcp(new Tempus::TimeStepControl<double>());
544  std::vector<double> outputTimes;
545  double outputTime = 0.8;
546  outputTimes.push_back(outputTime);
547  tsc->setOutputTimes(outputTimes);
548  tsc->setOutputExactly(true);
550  tsc->setTimeStepControlStrategy(tscs);
551  tsc->setMinTimeStep (dt/2.0);
552  tsc->setInitTimeStep(dt);
553  tsc->setMaxTimeStep (2.0*dt);
554  tsc->setPrintDtChanges(true);
555  tsc->initialize();
556  TEST_COMPARE(tsc->getOutputExactly(), ==, true);
558 
559  // ** First Timestep ** //
560  // Set dt to hit outputTime.
561  solutionHistory->initWorkingState();
562  auto currentState = solutionHistory->getCurrentState();
563  auto workingState = solutionHistory->getWorkingState();
564 
565  tsc->setNextTimeStep(solutionHistory, status);
566 
567  TEST_FLOATING_EQUALITY( workingState->getTimeStep(), outputTime, 1.0e-14);
568  TEST_FLOATING_EQUALITY( workingState->getTime(), outputTime, 1.0e-14);
569  TEST_COMPARE(workingState->getOutput(), ==, true);
570 
571  // ** Successful timestep !! ** //
572  workingState->setSolutionStatus(Tempus::Status::PASSED);
573 
574  solutionHistory->promoteWorkingState();
575 
576  // ** Second Timestep ** //
577  // Set dt to timestep before output.
578  solutionHistory->initWorkingState();
579  currentState = solutionHistory->getCurrentState();
580  workingState = solutionHistory->getWorkingState();
581 
582  tsc->setNextTimeStep(solutionHistory, status);
583 
584  TEST_FLOATING_EQUALITY( workingState->getTimeStep(), dt, 1.0e-14);
586  currentState->getTime() + workingState->getTimeStep(),
587  workingState->getTime(), 1.0e-14);
588 
589  TEST_COMPARE(workingState->getOutput(), ==, false);
590 }
591 
592 
593 // ************************************************************
594 // ************************************************************
595 TEUCHOS_UNIT_TEST(TimeStepControl, SetDtAfterOutput_Constant)
596 {
597  // Setup the SolutionHistory --------------------------------
598  auto model = rcp(new Tempus_Test::SinCosModel<double>());
599  auto inArgsIC = model->getNominalValues();
600  auto icSolution =rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
601  auto icState = Tempus::createSolutionStateX<double>(icSolution);
602  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
603  solutionHistory->addState(icState);
604  double dt = 0.9;
605  solutionHistory->getCurrentState()->setTimeStep(dt);
606 
607  // Setup the TimeStepControl --------------------------------
608  auto tsc = rcp(new Tempus::TimeStepControl<double>());
609  std::vector<double> outputTimes;
610  double outputTime = 0.8;
611  outputTimes.push_back(outputTime);
612  tsc->setOutputTimes(outputTimes);
613  tsc->setOutputExactly(true);
614  tsc->setMinTimeStep (dt/2.0);
615  tsc->setInitTimeStep(dt);
616  tsc->setMaxTimeStep (2.0*dt);
617  tsc->setOutputExactly(false);
618  tsc->initialize();
619  TEST_COMPARE(tsc->getOutputExactly(), ==, false);
621 
622  // Set dt to hit outputTime for first timestep.
623  solutionHistory->initWorkingState();
624  auto currentState = solutionHistory->getCurrentState();
625  auto workingState = solutionHistory->getWorkingState();
626 
627  tsc->setNextTimeStep(solutionHistory, status);
628  double timeN = workingState->getTime();
629  TEST_COMPARE(timeN, ==, dt);
630  //TEST_COMPARE( std::fabs(timeN-dt)/dt, <, 1.0e-15);
631  TEST_COMPARE(workingState->getOutput(), ==, true);
632 
633  // ** Successful timestep !! ** //
634  workingState->setSolutionStatus(Tempus::Status::PASSED);
635 
636  solutionHistory->promoteWorkingState();
637 
638  // Set dt to timestep before output for second timestep.
639  solutionHistory->initWorkingState();
640  // Set local RCPs for WS and CS after initialize.
641  currentState = solutionHistory->getCurrentState();
642  workingState = solutionHistory->getWorkingState();
643 
644  tsc->setNextTimeStep(solutionHistory, status);
645  timeN = workingState->getTime();
646  TEST_COMPARE( (timeN), ==, 2*dt);
647 
648  double dtN = workingState->getTimeStep();
649  TEST_COMPARE( dt, ==, dtN);
650 
651  TEST_COMPARE(workingState->getOutput(), ==, false);
652 }
653 
654 
655 // ************************************************************
656 // ************************************************************
657 TEUCHOS_UNIT_TEST(TimeStepControl, ConstantTimeStep_Roundoff)
658 {
659  // Setup the SolutionHistory --------------------------------
660  auto model = rcp(new Tempus_Test::SinCosModel<double>());
661  auto inArgsIC = model->getNominalValues();
662  auto icSolution =rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
663  auto icState = Tempus::createSolutionStateX<double>(icSolution);
664  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
665  solutionHistory->addState(icState);
666  double dt = 1.0e-04;
667  solutionHistory->getCurrentState()->setTimeStep(dt);
668 
669  // Setup the TimeStepControl --------------------------------
670  auto tsc = rcp(new Tempus::TimeStepControl<double>());
671  std::vector<double> outputTimes;
672  double outputTime = 0.8;
673  outputTimes.push_back(outputTime);
674  tsc->setOutputTimes(outputTimes);
675  tsc->setOutputExactly(true);
676  tsc->setTimeStepControlStrategy();
677  tsc->setInitTimeStep(dt);
678  tsc->initialize();
680 
681 
682  // Take 10000 timesteps.
683  for (int i=0; i < 10000; ++i) {
684  solutionHistory->initWorkingState();
685  tsc->setNextTimeStep(solutionHistory, status);
686 
687  // ** Successful timestep !! ** //
688  solutionHistory->getWorkingState()->setSolutionStatus(Tempus::Status::PASSED);
689 
690  solutionHistory->promoteWorkingState();
691  }
692 
693  auto currentState = solutionHistory->getCurrentState();
694  double time = currentState->getTime();
695  TEST_COMPARE( std::fabs(time-1.0), <, 1.0e-15);
696 }
697 
698 
699 // ************************************************************
700 // ************************************************************
701 TEUCHOS_UNIT_TEST(TimeStepControl, Setting_Strategies_PLs)
702 {
703 
704  { // Test without "Time Step Control Strategy"
705  auto pl = Tempus::getTimeStepControlPL<double>();
706  pl->remove("Time Step Control Strategy");
707 
708  auto tsc = Tempus::createTimeStepControl<double>(pl,false);
709  TEUCHOS_TEST_FOR_EXCEPT(tsc->isInitialized());
710  tsc->initialize();
711  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
712 
713  // Default strategy is "Constant"
714  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getStepType() == "Constant"));
715  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() == "Constant"));
716  }
717 
718  { // Test with "Basic VS" strategy
719  auto pl = Tempus::getTimeStepControlPL<double>();
720  pl->remove("Time Step Control Strategy");
721  pl->set("Time Step Control Strategy",
722  *(Tempus::getTimeStepControlStrategyBasicVS_PL<double>()));
723 
724  auto tsc = Tempus::createTimeStepControl<double>(pl);
725  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
726 
727  // Strategy should be "Basic VS"
728  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getStepType() == "Variable"));
729  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() == "Basic VS"));
730  }
731 
732  { // Test with "Integral Controller" strategy
733  auto pl = Tempus::getTimeStepControlPL<double>();
734  pl->remove("Time Step Control Strategy");
735  pl->set("Time Step Control Strategy",
736  *(Tempus::getTimeStepControlStrategyIntegralControllerPL<double>()));
737 
738  auto tsc = Tempus::createTimeStepControl<double>(pl);
739  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
740 
741  // Strategy should be "Integral Controller"
742  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getStepType() == "Variable"));
743  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() == "Integral Controller"));
744  }
745 
746  { // Test with "Composite" strategy
747  auto pl = Tempus::getTimeStepControlPL<double>();
748  pl->remove("Time Step Control Strategy");
749  pl->set("Time Step Control Strategy",
750  *(Tempus::getTimeStepControlStrategyCompositePL<double>()));
751 
752  auto tsc = Tempus::createTimeStepControl<double>(pl);
753  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
754 
755  // Strategy should be "Composite"
756  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getStepType() == "Constant"));
757  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() == "Composite"));
758  }
759 
760  { // Test with a non-Tempus strategy
761  auto pl = Tempus::getTimeStepControlPL<double>();
762  pl->remove("Time Step Control Strategy");
763 
764  auto nonTempusStrategyPL =
765  Teuchos::parameterList("Time Step Control Strategy");
766  nonTempusStrategyPL->set<std::string>("Strategy Type", "Application Strategy");
767  nonTempusStrategyPL->set<double>("Secret Sauce", 1.2345);
768 
769  pl->set("Time Step Control Strategy", *nonTempusStrategyPL);
770 
771  auto tsc = Tempus::createTimeStepControl<double>(pl);
772  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
773 
774  // Without finding a Tempus Strategy, the strategy should be the default strategy "Constant"
775  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getStepType() == "Constant"));
776  TEUCHOS_TEST_FOR_EXCEPT(!(tsc->getTimeStepControlStrategy()->getStrategyType() == "Constant"));
777  }
778 
779 }
780 
781 
782 // ************************************************************
783 // ************************************************************
784 TEUCHOS_UNIT_TEST(TimeStepControl, Cast_Composite)
785 {
786  // Setup Composite for this test.
788  auto tscsBasicVS = rcp(new Tempus::TimeStepControlStrategyBasicVS<double>());
789  temp->addStrategy(tscsBasicVS);
791  temp->addStrategy(tscsIntCtrl);
792  temp->initialize();
793  TEUCHOS_TEST_FOR_EXCEPT(!temp->isInitialized());
794 
795  auto tsc = rcp(new Tempus::TimeStepControl<double>());
796  tsc->setTimeStepControlStrategy(temp);
797  tsc->initialize();
798  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
799 
801  tsc->getTimeStepControlStrategy();
802 
803  // Test that we can cast this to a TimeStepControlStrategyComposite.
804  auto tscsc = rcp_dynamic_cast<Tempus::TimeStepControlStrategyComposite<double> >(strategy);
805 
806  TEST_COMPARE(tscsc->size(), ==, 2);
807 
808  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
809  strategies = tscsc->getStrategies();
810 
811  auto strategyBasicVS = Teuchos::rcp_dynamic_cast<Tempus::TimeStepControlStrategyBasicVS<double> > (strategies[0]);
812 
813  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getStepType() != "Variable");
814  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getAmplFactor() != 1.75);
815  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getReductFactor() != 0.5);
816  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMinEta() != 0.0);
817  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMaxEta() != 1.0e+16);
818 
819  auto strategyIC = Teuchos::rcp_dynamic_cast<Tempus::TimeStepControlStrategyIntegralController<double> > (strategies[1]);
820 
821  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getStepType() != "Variable");
822  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getController() != "PID");
823  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKI() != 0.58);
824  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKP() != 0.21);
825  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKD() != 0.10);
826  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactor() != 0.90);
827  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactorAfterReject() != 0.90);
828  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMax() != 5.0);
829  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMin() != 0.5);
830 
831 }
832 
833 
834 } // namespace Tempus_Test
StepControlStrategy class for TimeStepControl.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Status
Status for the Integrator, the Stepper and the SolutionState.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
StepControlStrategy class for TimeStepControl.
TimeStepControlStrategyComposite loops over a vector of TimeStepControlStrategies.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)