Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_ERK.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 
10 
12 #include "Tempus_StepperRKBase.hpp"
15 
16 #include "../TestModels/DahlquistTestModel.hpp"
17 
18 namespace Tempus_Unit_Test {
19 
20 using Teuchos::RCP;
21 using Teuchos::rcp;
22 using Teuchos::rcp_const_cast;
23 using Teuchos::rcp_dynamic_cast;
24 
117 class StepperRKModifierBogackiShampineTest
118  : virtual public Tempus::StepperRKModifierBase<double> {
119  public:
121  StepperRKModifierBogackiShampineTest(Teuchos::FancyOStream &Out,
122  bool &Success)
123  : out(Out), success(Success)
124  {
125  }
126 
128  virtual ~StepperRKModifierBogackiShampineTest() {}
129 
131  virtual void modify(
135  {
136  const double relTol = 1.0e-14;
137  auto stageNumber = stepper->getStageNumber();
138  Teuchos::SerialDenseVector<int, double> c = stepper->getTableau()->c();
139 
140  auto currentState = sh->getCurrentState();
141  auto workingState = sh->getWorkingState();
142  const double dt = workingState->getTimeStep();
143  double time = currentState->getTime();
144  if (stageNumber >= 0) time += c(stageNumber) * dt;
145 
146  auto x = workingState->getX();
147  auto xDot = workingState->getXDot();
148  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
149 
151  {
152  auto DME = Teuchos::rcp_dynamic_cast<
154  stepper->getModel());
155  TEST_FLOATING_EQUALITY(DME->getLambda(), -1.0, relTol);
156  }
157  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
158 
159  const double x_0 = get_ele(*(x), 0);
160  const double xDot_0 = get_ele(*(xDot), 0);
161  TEST_FLOATING_EQUALITY(x_0, 1.0, relTol); // Should be x_0
162  TEST_FLOATING_EQUALITY(xDot_0, -1.0, relTol); // Should be xDot_0
163  TEST_ASSERT(std::abs(time) < relTol);
164  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
165  TEST_COMPARE(stageNumber, ==, -1);
166  }
167  else if (actLoc == StepperRKAppAction<double>::BEGIN_STAGE ||
172  const double X_i = get_ele(*(x), 0);
173  const double f_i = get_ele(*(xDot), 0);
174  if (stageNumber == 0) {
175  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol); // Should be X_1
176  TEST_ASSERT(std::abs(time) < relTol);
178  !stepper->getUseFSAL()) {
179  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol); // Should be \bar{f}_1
180  }
181  else {
182  TEST_ASSERT(std::abs(f_i) < relTol); // Not set yet.
183  }
184  }
185  else if (stageNumber == 1) {
186  TEST_FLOATING_EQUALITY(X_i, 0.5, relTol); // Should be X_2
187  TEST_FLOATING_EQUALITY(time, 0.5, relTol);
189  TEST_FLOATING_EQUALITY(f_i, -0.5, relTol); // Should be \bar{f}_2
190  }
191  else {
192  TEST_ASSERT(std::abs(f_i) < relTol); // Not set yet.
193  }
194  }
195  else if (stageNumber == 2) {
196  TEST_FLOATING_EQUALITY(X_i, 5.0 / 8.0, relTol); // Should be X_3
197  TEST_FLOATING_EQUALITY(time, 0.75, relTol);
199  TEST_FLOATING_EQUALITY(f_i, -5.0 / 8.0,
200  relTol); // Should be \bar{f}_3
201  }
202  else {
203  TEST_ASSERT(std::abs(f_i) < relTol); // Not set yet.
204  }
205  }
206  else if (stageNumber == 3) {
207  TEST_FLOATING_EQUALITY(X_i, 1.0 / 3.0, relTol); // Should be X_4
208  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
210  TEST_FLOATING_EQUALITY(f_i, -1.0 / 3.0,
211  relTol); // Should be \bar{f}_4
212  }
213  else if (workingState->getNConsecutiveFailures() > 0) {
214  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol); // From IC and FSAL swap.
215  }
216  else {
217  TEST_ASSERT(std::abs(f_i) < relTol); // Not set yet.
218  }
219  }
220  else {
221  TEUCHOS_TEST_FOR_EXCEPT(!(-1 < stageNumber && stageNumber < 4));
222  }
223  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
224  }
225  else if (actLoc == StepperRKAppAction<double>::END_STEP) {
226  const double x_1 = get_ele(*(x), 0);
227  time = workingState->getTime();
228  TEST_FLOATING_EQUALITY(x_1, 1.0 / 3.0, relTol); // Should be x_1
229  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
230  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
231  TEST_COMPARE(stageNumber, ==, -1);
232 
233  if (stepper->getUseEmbedded() == true) {
234  TEST_FLOATING_EQUALITY(workingState->getTolRel(), 1.0, relTol);
235  TEST_ASSERT(std::abs(workingState->getTolAbs()) < relTol);
236  // e = 0 from doxygen above.
237  TEST_ASSERT(std::abs(workingState->getErrorRel()) < relTol);
238  }
239  }
240  else {
241  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
242  "Error - unknown action location.\n");
243  }
244  }
245 
246  private:
248  bool &success;
249 };
250 
251 // ************************************************************
252 // ************************************************************
253 TEUCHOS_UNIT_TEST(ERK, Modifier)
254 {
258  auto modifier = rcp(new StepperRKModifierBogackiShampineTest(out, success));
259 
260  stepper->setModel(model);
261  stepper->setAppAction(modifier);
262  stepper->setICConsistency("Consistent");
263  stepper->setUseFSAL(false);
264  stepper->initialize();
265 
266  // Create a SolutionHistory.
267  auto solutionHistory = Tempus::createSolutionHistoryME(model);
268 
269  // Take one time step.
270  stepper->setInitialConditions(solutionHistory);
271  solutionHistory->initWorkingState();
272  double dt = 1.0;
273  solutionHistory->getWorkingState()->setTimeStep(dt);
274  solutionHistory->getWorkingState()->setTime(dt);
275  stepper->takeStep(solutionHistory); // Primary testing occurs in
276  // modifierX during takeStep().
277  // Test stepper properties.
278  TEUCHOS_ASSERT(stepper->getOrder() == 3);
279 }
280 
281 // ************************************************************
282 // ************************************************************
283 TEUCHOS_UNIT_TEST(ERK, Modifier_FSAL)
284 {
288  auto modifier = rcp(new StepperRKModifierBogackiShampineTest(out, success));
289 
290  stepper->setModel(model);
291  stepper->setAppAction(modifier);
292  stepper->setICConsistency("Consistent");
293  stepper->setUseFSAL(true);
294  stepper->initialize();
295 
296  // Create a SolutionHistory.
297  auto solutionHistory = Tempus::createSolutionHistoryME(model);
298 
299  // Take one time step.
300  stepper->setInitialConditions(solutionHistory);
301  solutionHistory->initWorkingState();
302  double dt = 1.0;
303  solutionHistory->getWorkingState()->setTimeStep(dt);
304  solutionHistory->getWorkingState()->setTime(dt);
305  stepper->takeStep(solutionHistory); // Primary testing occurs in
306  // modifierX during takeStep().
307  // Test stepper properties.
308  TEUCHOS_ASSERT(stepper->getOrder() == 3);
309 }
310 
311 // ************************************************************
312 // ************************************************************
313 TEUCHOS_UNIT_TEST(ERK, Modifier_FSAL_Failure)
314 {
318  auto modifier = rcp(new StepperRKModifierBogackiShampineTest(out, success));
319 
320  stepper->setModel(model);
321  stepper->setAppAction(modifier);
322  stepper->setICConsistency("Consistent");
323  stepper->setUseFSAL(true);
324  stepper->initialize();
325 
326  // Create a SolutionHistory.
327  auto solutionHistory = Tempus::createSolutionHistoryME(model);
328 
329  // Take one time step.
330  stepper->setInitialConditions(solutionHistory);
331  solutionHistory->initWorkingState();
332  double dt = 1.0;
333  solutionHistory->getWorkingState()->setTimeStep(dt);
334  solutionHistory->getWorkingState()->setTime(dt);
335  solutionHistory->getWorkingState()->setNConsecutiveFailures(1);
336  stepper->takeStep(solutionHistory); // Primary testing occurs in
337  // modifierX during takeStep().
338  // Test stepper properties.
339  TEUCHOS_ASSERT(stepper->getOrder() == 3);
340 }
341 
342 // ************************************************************
343 // ************************************************************
344 TEUCHOS_UNIT_TEST(ERK, Modifier_Embedded)
345 {
349  auto modifier = rcp(new StepperRKModifierBogackiShampineTest(out, success));
350 
351  stepper->setModel(model);
352  stepper->setAppAction(modifier);
353  stepper->setICConsistency("Consistent");
354  stepper->setUseFSAL(false);
355  stepper->setUseEmbedded(true);
356  stepper->initialize();
357 
358  // Create a SolutionHistory.
359  auto solutionHistory = Tempus::createSolutionHistoryME(model);
360 
361  // Take one time step.
362  stepper->setInitialConditions(solutionHistory);
363  solutionHistory->initWorkingState();
364  double dt = 1.0;
365  solutionHistory->getWorkingState()->setTimeStep(dt);
366  solutionHistory->getWorkingState()->setTime(dt);
367  solutionHistory->getWorkingState()->setTolRel(1.0);
368  solutionHistory->getWorkingState()->setTolAbs(0.0);
369  stepper->takeStep(solutionHistory); // Primary testing occurs in
370  // modifierX during takeStep().
371  // Test stepper properties.
372  TEUCHOS_ASSERT(stepper->getOrder() == 3);
373 }
374 
382 class StepperRKModifierXBogackiShampineTest
383  : virtual public Tempus::StepperRKModifierXBase<double> {
384  public:
386  StepperRKModifierXBogackiShampineTest(Teuchos::FancyOStream &Out,
387  bool &Success)
388  : out(Out), success(Success)
389  {
390  }
391 
393  virtual ~StepperRKModifierXBogackiShampineTest() {}
394 
396  virtual void modify(
397  Teuchos::RCP<Thyra::VectorBase<double> > x, const double time,
398  const double dt, const int stageNumber,
400  modType)
401  {
402  const double relTol = 1.0e-14;
404  const double x_0 = get_ele(*(x), 0); // Should be x_0
405  TEST_FLOATING_EQUALITY(x_0, 1.0, relTol);
406  TEST_FLOATING_EQUALITY(time, 0.0, relTol);
407  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
408  TEST_COMPARE(stageNumber, ==, -1);
409  }
410  else if (modType == StepperRKModifierXBase<double>::X_BEGIN_STAGE ||
413  modType ==
416  const double X_i = get_ele(*(x), 0);
417  if (stageNumber == 0) {
418  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol); // Should be X_1
419  TEST_FLOATING_EQUALITY(time, 0.0, relTol);
420  }
421  else if (stageNumber == 1) {
422  TEST_FLOATING_EQUALITY(X_i, 0.5, relTol); // Should be X_2
423  TEST_FLOATING_EQUALITY(time, 0.5, relTol);
424  }
425  else if (stageNumber == 2) {
426  TEST_FLOATING_EQUALITY(X_i, 5.0 / 8.0, relTol); // Should be X_3
427  TEST_FLOATING_EQUALITY(time, 0.75, relTol);
428  }
429  else if (stageNumber == 3) {
430  TEST_FLOATING_EQUALITY(X_i, 1.0 / 3.0, relTol); // Should be X_4
431  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
432  }
433  else {
434  TEUCHOS_TEST_FOR_EXCEPT(!(-1 < stageNumber && stageNumber < 4));
435  }
436  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
437  }
438  else if (modType == StepperRKModifierXBase<double>::X_END_STEP) {
439  const double x_1 = get_ele(*(x), 0);
440  TEST_FLOATING_EQUALITY(x_1, 1.0 / 3.0, relTol); // Should be x_1
441  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
442  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
443  TEST_COMPARE(stageNumber, ==, -1);
444  }
445  else {
446  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
447  "Error - unknown action location.\n");
448  }
449  }
450 
451  private:
453  bool &success;
454 };
455 
456 // ************************************************************
457 // ************************************************************
458 TEUCHOS_UNIT_TEST(ERK, ModifierX)
459 {
463  auto modifierX = rcp(new StepperRKModifierXBogackiShampineTest(out, success));
464 
465  stepper->setModel(model);
466  stepper->setAppAction(modifierX);
467  stepper->setICConsistency("Consistent");
468  stepper->setUseFSAL(false);
469  stepper->initialize();
470 
471  // Create a SolutionHistory.
472  auto solutionHistory = Tempus::createSolutionHistoryME(model);
473 
474  // Take one time step.
475  stepper->setInitialConditions(solutionHistory);
476  solutionHistory->initWorkingState();
477  double dt = 1.0;
478  solutionHistory->getWorkingState()->setTimeStep(dt);
479  solutionHistory->getWorkingState()->setTime(dt);
480  stepper->takeStep(solutionHistory); // Primary testing occurs in
481  // modifierX during takeStep().
482  // Test stepper properties.
483  TEUCHOS_ASSERT(stepper->getOrder() == 3);
484 }
485 
486 // ************************************************************
487 // ************************************************************
488 TEUCHOS_UNIT_TEST(ERK, ModifierX_FSAL)
489 {
493  auto modifierX = rcp(new StepperRKModifierXBogackiShampineTest(out, success));
494 
495  stepper->setModel(model);
496  stepper->setAppAction(modifierX);
497  stepper->setICConsistency("Consistent");
498  stepper->setUseFSAL(true);
499  stepper->initialize();
500 
501  // Create a SolutionHistory.
502  auto solutionHistory = Tempus::createSolutionHistoryME(model);
503 
504  // Take one time step.
505  stepper->setInitialConditions(solutionHistory);
506  solutionHistory->initWorkingState();
507  double dt = 1.0;
508  solutionHistory->getWorkingState()->setTimeStep(dt);
509  solutionHistory->getWorkingState()->setTime(dt);
510  stepper->takeStep(solutionHistory); // Primary testing occurs in
511  // modifierX during takeStep().
512  // Test stepper properties.
513  TEUCHOS_ASSERT(stepper->getOrder() == 3);
514 }
515 
516 // ************************************************************
517 // ************************************************************
518 TEUCHOS_UNIT_TEST(ERK, ModifierX_FSAL_Failure)
519 {
523  auto modifierX = rcp(new StepperRKModifierXBogackiShampineTest(out, success));
524 
525  stepper->setModel(model);
526  stepper->setAppAction(modifierX);
527  stepper->setICConsistency("Consistent");
528  stepper->setUseFSAL(true);
529  stepper->initialize();
530 
531  // Create a SolutionHistory.
532  auto solutionHistory = Tempus::createSolutionHistoryME(model);
533 
534  // Take one time step.
535  stepper->setInitialConditions(solutionHistory);
536  solutionHistory->initWorkingState();
537  double dt = 1.0;
538  solutionHistory->getWorkingState()->setTimeStep(dt);
539  solutionHistory->getWorkingState()->setTime(dt);
540  solutionHistory->getWorkingState()->setNConsecutiveFailures(1);
541  stepper->takeStep(solutionHistory); // Primary testing occurs in
542  // modifierX during takeStep().
543  // Test stepper properties.
544  TEUCHOS_ASSERT(stepper->getOrder() == 3);
545 }
546 
547 } // namespace Tempus_Unit_Test
The classic Dahlquist Test Problem.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
#define TEST_ASSERT(v1)
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryME(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Nonmember contructor from a Thyra ModelEvaluator.
Base class for Runge-Kutta methods, ExplicitRK, DIRK and IMEX.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Explicit RK Bogacki-Shampine Butcher Tableau.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
MODIFIER_TYPE
Indicates the location of application action (see algorithm).
ACTION_LOCATION
Indicates the location of application action (see algorithm).
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const int, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.