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