Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_IMEX_RK.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 
13 #include "../TestModels/DahlquistTestModel.hpp"
14 #include "../TestModels/VanDerPol_IMEX_ExplicitModel.hpp"
15 #include "../TestModels/VanDerPol_IMEX_ImplicitModel.hpp"
16 
17 namespace Tempus_Unit_Test {
18 
20 using Teuchos::RCP;
21 using Teuchos::rcp;
22 using Teuchos::rcp_const_cast;
23 using Teuchos::rcp_dynamic_cast;
24 using Teuchos::sublist;
25 
28 
29 // ************************************************************
30 // ************************************************************
31 TEUCHOS_UNIT_TEST(IMEX_RK, Default_Construction)
32 {
33  // Setup the IMEX Pair ModelEvaluator
34  auto explicitModel =
36  auto implicitModel =
39  explicitModel, implicitModel));
40 
41  // Default construction.
42  auto stepper = rcp(new Tempus::StepperIMEX_RK<double>());
43  stepper->setModel(model);
44  stepper->initialize();
45  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
46 
47  // Default values for construction.
48  auto modifier = rcp(new Tempus::StepperRKModifierDefault<double>());
49  auto modifierX = rcp(new Tempus::StepperRKModifierXDefault<double>());
50  auto observer = rcp(new Tempus::StepperRKObserverDefault<double>());
51  auto solver = rcp(new Thyra::NOXNonlinearSolver());
52  solver->setParameterList(Tempus::defaultSolverParameters());
53 
54  bool useFSAL = stepper->getUseFSAL();
55  std::string ICConsistency = stepper->getICConsistency();
56  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
57  bool zeroInitialGuess = stepper->getZeroInitialGuess();
58  std::string stepperType = "IMEX RK SSP2";
60  auto explicitTableau = stepperERK->getTableau();
61  auto stepperSDIRK =
63  auto implicitTableau = stepperSDIRK->getTableau();
64  int order = 2;
65 
66  // Test the set functions.
67  stepper->setAppAction(modifier);
68  stepper->initialize();
69  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
70  stepper->setAppAction(modifierX);
71  stepper->initialize();
72  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
73  stepper->setAppAction(observer);
74  stepper->initialize();
75  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
76  stepper->setSolver(solver);
77  stepper->initialize();
78  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
79  stepper->setUseFSAL(useFSAL);
80  stepper->initialize();
81  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
82  stepper->setICConsistency(ICConsistency);
83  stepper->initialize();
84  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
85  stepper->setICConsistencyCheck(ICConsistencyCheck);
86  stepper->initialize();
87  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
88  stepper->setZeroInitialGuess(zeroInitialGuess);
89  stepper->initialize();
90  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
91 
92  stepper->setStepperName(stepperType);
93  stepper->initialize();
94  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
95  stepper->setExplicitTableau(explicitTableau);
96  stepper->initialize();
97  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
98  stepper->setImplicitTableau(implicitTableau);
99  stepper->initialize();
100  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
101  stepper->setOrder(order);
102  stepper->initialize();
103  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
104 
105  TEUCHOS_TEST_FOR_EXCEPT(explicitTableau != stepper->getTableau());
106  TEUCHOS_TEST_FOR_EXCEPT(explicitTableau != stepper->getExplicitTableau());
107  TEUCHOS_TEST_FOR_EXCEPT(implicitTableau != stepper->getImplicitTableau());
108 
109  // Full argument list construction.
110  stepper = rcp(new Tempus::StepperIMEX_RK<double>(
111  model, solver, useFSAL, ICConsistency, ICConsistencyCheck,
112  zeroInitialGuess, modifier, stepperType, explicitTableau, implicitTableau,
113  order));
114  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
115 
116  // Test stepper properties.
117  TEUCHOS_ASSERT(stepper->getOrder() == 2);
118 }
119 
120 // ************************************************************
121 // ************************************************************
122 TEUCHOS_UNIT_TEST(IMEX_RK, StepperFactory_Construction)
123 {
124  // Setup the IMEX Pair ModelEvaluator
125  auto explicitModel =
127  auto implicitModel =
130  explicitModel, implicitModel));
131 
132  testFactoryConstruction("IMEX RK SSP2", model);
133 }
134 
135 // ************************************************************
136 // ************************************************************
137 TEUCHOS_UNIT_TEST(IMEX_RK, StepperFactory_Construction_General_wo_Parameterlist)
138 {
139  // Setup the IMEX Pair ModelEvaluator
140  auto explicitModel =
142  auto implicitModel =
145  explicitModel, implicitModel));
146 
148 
149  auto stepper = sf->createStepper("General IMEX RK", model);
150  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
151 }
152 
153 // ************************************************************
154 // ************************************************************
156  StepperFactory_Construction_General_wo_Parameterlist_Model)
157 {
158  // Setup the IMEX Pair ModelEvaluator
159  auto explicitModel =
161  auto implicitModel =
164  explicitModel, implicitModel));
165 
167 
168  auto stepper = sf->createStepper("General IMEX RK");
169  stepper->setModel(model);
170  stepper->initialize();
171  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
172 }
173 
174 // ************************************************************
175 // ************************************************************
176 TEUCHOS_UNIT_TEST(IMEX_RK, AppAction)
177 {
178  auto stepper = rcp(new Tempus::StepperIMEX_RK<double>());
179  // Setup the IMEX Pair ModelEvaluator
180  auto explicitModel =
182  auto implicitModel =
185  explicitModel, implicitModel));
186 
187  testRKAppAction(stepper, model, out, success);
188 }
189 
190 class StepperRKModifierIMEX_TrapezoidaTest
191  : virtual public Tempus::StepperRKModifierBase<double> {
192  public:
194  StepperRKModifierIMEX_TrapezoidaTest(Teuchos::FancyOStream &Out,
195  bool &Success)
196  : out(Out), success(Success)
197  {
198  }
199 
200  // FSAL
202  virtual ~StepperRKModifierIMEX_TrapezoidaTest() {}
203 
205  virtual void modify(
209  {
210  const double relTol = 1.0e-14;
211  auto stepper_imex =
212  Teuchos::rcp_dynamic_cast<const Tempus::StepperIMEX_RK<double>>(stepper,
213  true);
214  auto stageNumber = stepper->getStageNumber();
215  Teuchos::SerialDenseVector<int, double> c = stepper_imex->getTableau()->c();
217  stepper_imex->getImplicitTableau()->c();
218 
219  auto currentState = sh->getCurrentState();
220  auto workingState = sh->getWorkingState();
221  const double dt = workingState->getTimeStep();
222  double time = currentState->getTime();
223  double imp_time = time;
224  if (stageNumber >= 0) {
225  time += c(stageNumber) * dt;
226  imp_time += chat(stageNumber) * dt;
227  }
228 
229  auto x = workingState->getX();
230  auto xDot = workingState->getXDot();
231  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
232 
233  switch (actLoc) {
235  {
236  auto imex_me = Teuchos::rcp_dynamic_cast<
238  stepper->getModel(), true);
239  auto explicitModel = Teuchos::rcp_dynamic_cast<
241  imex_me->getExplicitModel(), true);
242  auto implicitModel = Teuchos::rcp_dynamic_cast<
244  imex_me->getImplicitModel(), true);
245 
246  TEST_FLOATING_EQUALITY(explicitModel->getLambda(), 1.0, relTol);
247  TEST_FLOATING_EQUALITY(implicitModel->getLambda(), 2.0, relTol);
248  }
249  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
250 
251  const double x_0 = get_ele(*(x), 0);
252  TEST_FLOATING_EQUALITY(x_0, 1.0, relTol);
253  TEST_ASSERT(std::abs(time) < relTol);
254  TEST_ASSERT(std::abs(imp_time) < relTol);
255  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
256  TEST_COMPARE(stageNumber, ==, -1);
257  break;
258  }
261  const double X_i = get_ele(*(x), 0);
262  const double f_i = get_ele(*(xDot), 0);
263 
264  if (stageNumber == 0) {
265  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol); // 1
266  TEST_FLOATING_EQUALITY(f_i, 1.0, relTol); // 1
267  TEST_FLOATING_EQUALITY(imp_time, 0.78867513459481275, relTol); // 1
268  TEST_ASSERT(std::abs(time) < relTol);
269  }
270  else if (stageNumber == 1) {
271  TEST_FLOATING_EQUALITY(X_i, -std::sqrt(3), relTol); // -sqrt(3)
272  TEST_FLOATING_EQUALITY(f_i, 1.0, relTol); // 1
273  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
274  TEST_FLOATING_EQUALITY(imp_time, 0.21132486540518725, relTol); // 1
275  }
276  else {
277  TEUCHOS_TEST_FOR_EXCEPT(!(-1 < stageNumber && stageNumber < 2));
278  }
279 
280  break;
281  }
285  const double X_i = get_ele(*(x), 0);
286  const double f_i = get_ele(*(xDot), 0);
287 
288  if (stageNumber == 0) {
289  // X_i = 1, f_1 = 0
290  TEST_FLOATING_EQUALITY(X_i, -std::sqrt(3), relTol); // -sqrt(3)
291  TEST_FLOATING_EQUALITY(f_i, 1.0, relTol); // 1
292  TEST_FLOATING_EQUALITY(imp_time, 0.78867513459481275, relTol); // 1
293  TEST_ASSERT(std::abs(time) < relTol);
294  }
295  else if (stageNumber == 1) {
296  // X_i = , f_i =
297  TEST_FLOATING_EQUALITY(X_i, 3.0 - 3.0 * std::sqrt(3.0),
298  relTol); // -3sqrt(3) - 3
299  TEST_FLOATING_EQUALITY(f_i, 1.0, relTol); // 1
300  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
301  }
302  else {
303  TEUCHOS_TEST_FOR_EXCEPT(!(-1 < stageNumber && stageNumber < 2));
304  }
305 
306  break;
307  }
309  const double x_1 = get_ele(*(x), 0);
310  time = workingState->getTime();
311  TEST_FLOATING_EQUALITY(x_1, -(6.0 * std::sqrt(3) - 11.0 / 2.0),
312  relTol); // -( 6sqrt(3) - 11/2)
313  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
314  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
315  TEST_COMPARE(stageNumber, ==, -1);
316  }
317  }
318  }
319 
320  private:
322  bool &success;
323 };
324 
325 // ************************************************************
326 // ************************************************************
327 TEUCHOS_UNIT_TEST(IMEX_RK, IMEX_RK_Modifier)
328 {
333 
335  explicitModel, implicitModel));
336 
337  auto modifier = rcp(new StepperRKModifierIMEX_TrapezoidaTest(out, success));
338 
339  // Default construction.
340  auto stepper = rcp(new Tempus::StepperIMEX_RK<double>());
341  stepper->setModel(model);
342 
343  // Default values for construction.
344  auto solver = rcp(new Thyra::NOXNonlinearSolver());
345  solver->setParameterList(Tempus::defaultSolverParameters());
346 
347  bool useFSAL = stepper->getUseFSAL();
348  std::string ICConsistency = stepper->getICConsistency();
349  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
350  bool zeroInitialGuess = stepper->getZeroInitialGuess();
351  std::string stepperType = "IMEX RK SSP2";
352  auto stepperERK = Teuchos::rcp(new Tempus::StepperERK_Trapezoidal<double>());
353  auto explicitTableau = stepperERK->getTableau();
354  auto stepperSDIRK =
356  auto implicitTableau = stepperSDIRK->getTableau();
357  int order = 2;
358 
359  stepper->setStepperName(stepperType);
360  stepper->setExplicitTableau(explicitTableau);
361  stepper->setImplicitTableau(implicitTableau);
362  stepper->setOrder(order);
363  stepper->setSolver(solver);
364  stepper->setUseFSAL(useFSAL);
365  stepper->setICConsistency(ICConsistency);
366  stepper->setICConsistencyCheck(ICConsistencyCheck);
367  stepper->setZeroInitialGuess(zeroInitialGuess);
368 
369  stepper->setModel(model);
370  stepper->setAppAction(modifier);
371  stepper->setUseFSAL(false);
372  stepper->initialize();
373 
374  // Create a SolutionHistory.
375  auto solutionHistory = Tempus::createSolutionHistoryME(explicitModel);
376 
378  stepper->setInitialConditions(solutionHistory);
379  solutionHistory->initWorkingState();
380  double dt = 1.0;
381  solutionHistory->getWorkingState()->setTimeStep(dt);
382  solutionHistory->getWorkingState()->setTime(dt);
383  stepper->takeStep(solutionHistory);
384 
385  // Test stepper properties.
386  TEUCHOS_ASSERT(stepper->getOrder() == 2);
387 }
388 
389 } // namespace Tempus_Unit_Test
Explicit Runge-Kutta time stepper.
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
The classic Dahlquist Test Problem.
ModelEvaluator pair for implicit and explicit (IMEX) evaulations.
Implicit-Explicit Runge-Kutta (IMEX-RK) time stepper.
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
#define TEST_ASSERT(v1)
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
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.
void testRKAppAction(const Teuchos::RCP< Tempus::StepperRKBase< double >> &stepper, const Teuchos::RCP< const Thyra::ModelEvaluator< double >> &model, Teuchos::FancyOStream &out, bool &success)
Unit test utility for Stepper RK AppAction.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
virtual void modify(Teuchos::RCP< SolutionHistory< double > >, Teuchos::RCP< StepperRKBase< double > >, const typename StepperRKAppAction< double >::ACTION_LOCATION actLoc)=0
Modify RK Stepper.
virtual int getStageNumber() const
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
ACTION_LOCATION
Indicates the location of application action (see algorithm).
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)