Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_ForwardEuler.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 
12 #include "Tempus_StepperForwardEuler.hpp"
19 
20 namespace Tempus_Unit_Test {
21 
23 using Teuchos::RCP;
24 using Teuchos::rcp;
25 using Teuchos::rcp_const_cast;
26 using Teuchos::rcp_dynamic_cast;
27 using Teuchos::sublist;
28 
30 
31 // ************************************************************
32 // ************************************************************
33 TEUCHOS_UNIT_TEST(ForwardEuler, Default_Construction)
34 {
35  auto model = rcp(new Tempus_Test::SinCosModel<double>());
36 
37  // Default construction.
39  auto modifierX =
42  auto stepper = rcp(new Tempus::StepperForwardEuler<double>());
43  stepper->setModel(model);
44  stepper->initialize();
45  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
46  // Default values for construction.
47  bool useFSAL = stepper->getUseFSAL();
48  std::string ICConsistency = stepper->getICConsistency();
49  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
50 
51  // Test the set functions.
52  stepper->setAppAction(modifier);
53  stepper->initialize();
54  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
55  stepper->setAppAction(modifierX);
56  stepper->initialize();
57  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
58  stepper->setAppAction(observer);
59  stepper->initialize();
60  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
61  stepper->setUseFSAL(useFSAL);
62  stepper->initialize();
63  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
64  stepper->setICConsistency(ICConsistency);
65  stepper->initialize();
66  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
67  stepper->setICConsistencyCheck(ICConsistencyCheck);
68  stepper->initialize();
69  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
70 
72  model, useFSAL, ICConsistency, ICConsistencyCheck, modifier));
73  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
74 
75  // Test stepper properties.
76  TEUCHOS_ASSERT(stepper->getOrder() == 1);
77 }
78 
79 // ************************************************************
80 // ************************************************************
81 TEUCHOS_UNIT_TEST(ForwardEuler, StepperFactory_Construction)
82 {
83  auto model = rcp(new Tempus_Test::SinCosModel<double>());
84  testFactoryConstruction("Forward Euler", model);
85 }
86 
87 // ************************************************************
88 // ************************************************************
89 class StepperForwardEulerModifierTest
90  : virtual public Tempus::StepperForwardEulerModifierBase<double> {
91  public:
93  StepperForwardEulerModifierTest()
94  : testBEGIN_STEP(false),
95  testBEFORE_EXPLICIT_EVAL(false),
96  testEND_STEP(false),
97  testCurrentValue(-0.99),
98  testWorkingValue(-0.99),
99  testDt(-1.5),
100  testName("")
101  {
102  }
103 
105  virtual ~StepperForwardEulerModifierTest() {}
106 
108  virtual void modify(
112  double>::ACTION_LOCATION actLoc)
113  {
114  switch (actLoc) {
116  testBEGIN_STEP = true;
117  auto x = sh->getCurrentState()->getX();
118  testCurrentValue = get_ele(*(x), 0);
119  break;
120  }
122  testBEFORE_EXPLICIT_EVAL = true;
123  testDt = sh->getWorkingState()->getTimeStep() / 10.0;
124  sh->getWorkingState()->setTimeStep(testDt);
125  testName = "Forward Euler - Modifier";
126  stepper->setStepperName(testName);
127  break;
128  }
130  testEND_STEP = true;
131  auto x = sh->getWorkingState()->getX();
132  testWorkingValue = get_ele(*(x), 0);
133  break;
134  }
135  default:
136  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
137  "Error - unknown action location.\n");
138  }
139  }
140  bool testBEGIN_STEP;
141  bool testBEFORE_EXPLICIT_EVAL;
142  bool testEND_STEP;
143  double testCurrentValue;
144  double testWorkingValue;
145  double testDt;
146  std::string testName;
147 };
148 
149 TEUCHOS_UNIT_TEST(ForwardEuler, AppAction_Modifier)
150 {
151  auto model = rcp(new Tempus_Test::SinCosModel<double>());
152 
153  // Setup Stepper for field solve ----------------------------
154  auto stepper = rcp(new Tempus::StepperForwardEuler<double>());
155  stepper->setModel(model);
156  auto modifier = rcp(new StepperForwardEulerModifierTest());
157  stepper->setAppAction(modifier);
158  stepper->initialize();
159 
160  // Setup initial condition SolutionState --------------------
161  auto inArgsIC = model->getNominalValues();
162  auto icSolution =
163  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
164  auto icState = Tempus::createSolutionStateX(icSolution);
165  icState->setTime(0.0);
166  icState->setIndex(0);
167  icState->setTimeStep(0.0);
168  icState->setOrder(stepper->getOrder());
169  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
170 
171  // Setup SolutionHistory ------------------------------------
172  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
173  solutionHistory->setName("Forward States");
174  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
175  solutionHistory->setStorageLimit(2);
176  solutionHistory->addState(icState);
177 
178  // Take one time step.
179  stepper->setInitialConditions(solutionHistory);
180  solutionHistory->initWorkingState();
181  double dt = 0.1;
182  solutionHistory->getWorkingState()->setTimeStep(dt);
183  stepper->takeStep(solutionHistory);
184 
185  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
186  TEST_COMPARE(modifier->testBEFORE_EXPLICIT_EVAL, ==, true);
187  TEST_COMPARE(modifier->testEND_STEP, ==, true);
188 
189  auto x = solutionHistory->getCurrentState()->getX();
190  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
191  x = solutionHistory->getWorkingState()->getX();
192  TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
193  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
194  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
195 
196  TEST_COMPARE(modifier->testName, ==, "Forward Euler - Modifier");
197 }
198 
199 // ************************************************************
200 // ************************************************************
201 class StepperForwardEulerObserverTest
202  : virtual public Tempus::StepperForwardEulerObserverBase<double> {
203  public:
205  StepperForwardEulerObserverTest()
206  : testBEGIN_STEP(false),
207  testBEFORE_EXPLICIT_EVAL(false),
208  testEND_STEP(false),
209  testCurrentValue(-0.99),
210  testWorkingValue(-0.99),
211  testDt(-1.5),
212  testName("")
213  {
214  }
215 
217  virtual ~StepperForwardEulerObserverTest() {}
218 
220  virtual void observe(
224  double>::ACTION_LOCATION actLoc)
225  {
226  switch (actLoc) {
228  testBEGIN_STEP = true;
229  auto x = sh->getCurrentState()->getX();
230  testCurrentValue = get_ele(*(x), 0);
231  break;
232  }
234  testBEFORE_EXPLICIT_EVAL = true;
235  testDt = sh->getWorkingState()->getTimeStep();
236  testName = stepper->getStepperName();
237  break;
238  }
240  testEND_STEP = true;
241  auto x = sh->getWorkingState()->getX();
242  testWorkingValue = get_ele(*(x), 0);
243  break;
244  }
245  default:
246  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
247  "Error - unknown action location.\n");
248  }
249  }
250 
251  bool testBEGIN_STEP;
252  bool testBEFORE_EXPLICIT_EVAL;
253  bool testEND_STEP;
254  double testCurrentValue;
255  double testWorkingValue;
256  double testDt;
257  std::string testName;
258 };
259 
260 TEUCHOS_UNIT_TEST(ForwardEuler, AppAction_Observer)
261 {
262  auto model = rcp(new Tempus_Test::SinCosModel<double>());
263 
264  // Setup Stepper for field solve ----------------------------
265  auto stepper = rcp(new Tempus::StepperForwardEuler<double>());
266  stepper->setModel(model);
267  auto observer = rcp(new StepperForwardEulerObserverTest());
268  stepper->setAppAction(observer);
269  stepper->initialize();
270 
271  // Setup initial condition SolutionState --------------------
272  auto inArgsIC = model->getNominalValues();
273  auto icSolution =
274  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
275  auto icState = Tempus::createSolutionStateX(icSolution);
276  icState->setTime(0.0);
277  icState->setIndex(0);
278  icState->setTimeStep(0.0);
279  icState->setOrder(stepper->getOrder());
280  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
281 
282  // Setup SolutionHistory ------------------------------------
283  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
284  solutionHistory->setName("Forward States");
285  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
286  solutionHistory->setStorageLimit(2);
287  solutionHistory->addState(icState);
288 
289  // Take one time step.
290  stepper->setInitialConditions(solutionHistory);
291  solutionHistory->initWorkingState();
292  double dt = 0.1;
293  solutionHistory->getWorkingState()->setTimeStep(dt);
294  stepper->takeStep(solutionHistory);
295 
296  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
297  TEST_COMPARE(observer->testBEFORE_EXPLICIT_EVAL, ==, true);
298  TEST_COMPARE(observer->testEND_STEP, ==, true);
299 
300  auto x = solutionHistory->getCurrentState()->getX();
301  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
302  x = solutionHistory->getWorkingState()->getX();
303  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
304  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-14);
305 
306  TEST_COMPARE(observer->testName, ==, "Forward Euler");
307 }
308 
309 // ************************************************************
310 // ************************************************************
311 class StepperForwardEulerModifierXTest
312  : virtual public Tempus::StepperForwardEulerModifierXBase<double> {
313  public:
315  StepperForwardEulerModifierXTest()
316  : testX_BEGIN_STEP(false),
317  testX_BEFORE_EXPLICIT_EVAL(false),
318  testXDOT_END_STEP(false),
319  testX(-0.99),
320  testXDot(-0.99),
321  testDt(-1.5),
322  testTime(-1.5)
323  {
324  }
325 
327  virtual ~StepperForwardEulerModifierXTest() {}
328 
331  const double time, const double dt,
333  double>::MODIFIER_TYPE modType)
334  {
335  switch (modType) {
337  testX_BEGIN_STEP = true;
338  testX = get_ele(*(x), 0);
339  break;
340  }
342  testX_BEFORE_EXPLICIT_EVAL = true;
343  testDt = dt;
344  testTime = time;
345  break;
346  }
348  testXDOT_END_STEP = true;
349  testXDot = get_ele(*(x), 0);
350  break;
351  }
352  default:
353  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
354  "Error - unknown action location.\n");
355  }
356  }
357  bool testX_BEGIN_STEP;
358  bool testX_BEFORE_EXPLICIT_EVAL;
359  bool testXDOT_END_STEP;
360  double testX;
361  double testXDot;
362  double testDt;
363  double testTime;
364 };
365 
366 TEUCHOS_UNIT_TEST(ForwardEuler, AppAction_ModifierX)
367 {
368  auto model = rcp(new Tempus_Test::SinCosModel<double>());
369 
370  // Setup Stepper for field solve ----------------------------
371  auto stepper = rcp(new Tempus::StepperForwardEuler<double>());
372  stepper->setModel(model);
373  auto modifierX = rcp(new StepperForwardEulerModifierXTest());
374  stepper->setAppAction(modifierX);
375  stepper->initialize();
376 
377  // Setup initial condition SolutionState --------------------
378  auto inArgsIC = model->getNominalValues();
379  auto icSolution =
380  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
381  auto icState = Tempus::createSolutionStateX(icSolution);
382  icState->setTime(0.0);
383  icState->setIndex(0);
384  icState->setTimeStep(0.0);
385  icState->setOrder(stepper->getOrder());
386  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
387 
388  // Setup SolutionHistory ------------------------------------
389  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
390  solutionHistory->setName("Forward States");
391  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
392  solutionHistory->setStorageLimit(2);
393  solutionHistory->addState(icState);
394 
395  // Take one time step.
396  stepper->setInitialConditions(solutionHistory);
397  solutionHistory->initWorkingState();
398  double dt = 0.1;
399  solutionHistory->getWorkingState()->setTimeStep(dt);
400  stepper->takeStep(solutionHistory);
401 
402  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
403  TEST_COMPARE(modifierX->testX_BEFORE_EXPLICIT_EVAL, ==, true);
404  TEST_COMPARE(modifierX->testXDOT_END_STEP, ==, true);
405 
406  auto x = solutionHistory->getCurrentState()->getX();
407  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
408  // Temporary memory for xDot is not guarranteed to exist outside the Stepper.
409  auto xDot = solutionHistory->getWorkingState()->getXDot();
410  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
411 
412  TEST_FLOATING_EQUALITY(modifierX->testXDot, get_ele(*(xDot), 0), 1.0e-14);
413  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
414  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
415 
416  auto time = solutionHistory->getWorkingState()->getTime();
417  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-14);
418 }
419 
420 } // namespace Tempus_Unit_Test
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateX(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdot=Teuchos::null, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdotdot=Teuchos::null)
Nonmember constructor from non-const solution vectors, x.
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#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)
Application Action for StepperForwardEuler.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Keep a fix number of states.
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)