Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_Trapezoidal.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 
11 #include "Tempus_StepperTrapezoidal.hpp"
18 
19 namespace Tempus_Unit_Test {
20 
22 using Teuchos::RCP;
23 using Teuchos::rcp;
24 using Teuchos::rcp_const_cast;
25 using Teuchos::rcp_dynamic_cast;
26 using Teuchos::sublist;
27 
28 // ************************************************************
29 // ************************************************************
30 TEUCHOS_UNIT_TEST(Trapezoidal, Default_Construction)
31 {
32  auto model = rcp(new Tempus_Test::SinCosModel<double>());
33 
34  // Default construction.
35  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
36  stepper->setModel(model);
37  stepper->initialize();
38  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
39 
40  // Default values for construction.
42  auto modifierX =
44  auto solver = rcp(new Thyra::NOXNonlinearSolver());
45  solver->setParameterList(Tempus::defaultSolverParameters());
46 
47  bool useFSAL = stepper->getUseFSAL();
48  std::string ICConsistency = stepper->getICConsistency();
49  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
50  bool zeroInitialGuess = stepper->getZeroInitialGuess();
51 
52  // Test the set functions.
53  stepper->setAppAction(modifier);
54  stepper->initialize();
55  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
56  stepper->setAppAction(modifierX);
57  stepper->initialize();
58  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
59  stepper->setSolver(solver);
60  stepper->initialize();
61  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
62  stepper->setUseFSAL(useFSAL);
63  stepper->initialize();
64  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
65  stepper->setICConsistency(ICConsistency);
66  stepper->initialize();
67  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
68  stepper->setICConsistencyCheck(ICConsistencyCheck);
69  stepper->initialize();
70  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
71  stepper->setZeroInitialGuess(zeroInitialGuess);
72  stepper->initialize();
73  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
74 
75  // Full argument list construction.
77  model, solver, useFSAL, ICConsistency, ICConsistencyCheck,
78  zeroInitialGuess, modifier));
79  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
80 
81  // Test stepper properties.
82  TEUCHOS_ASSERT(stepper->getOrder() == 2);
83 }
84 
85 // ************************************************************
86 // ************************************************************
87 TEUCHOS_UNIT_TEST(Trapezoidal, StepperFactory_Construction)
88 {
89  auto model = rcp(new Tempus_Test::SinCosModel<double>());
90  testFactoryConstruction("Trapezoidal Method", model);
91 }
92 
93 // ************************************************************
94 // ************************************************************
95 class StepperTrapezoidalModifierTest
96  : virtual public Tempus::StepperTrapezoidalModifierBase<double> {
97  public:
99  StepperTrapezoidalModifierTest()
100  : testBEGIN_STEP(false),
101  testBEFORE_SOLVE(false),
102  testAFTER_SOLVE(false),
103  testEND_STEP(false),
104  testCurrentValue(-0.99),
105  testWorkingValue(-0.99),
106  testDt(-1.5),
107  testName("")
108  {
109  }
110 
112  virtual ~StepperTrapezoidalModifierTest() {}
113 
115  virtual void modify(Teuchos::RCP<Tempus::SolutionHistory<double> > sh,
118  double>::ACTION_LOCATION actLoc)
119  {
120  switch (actLoc) {
122  testBEGIN_STEP = true;
123  auto x = sh->getCurrentState()->getX();
124  testCurrentValue = get_ele(*(x), 0);
125  break;
126  }
128  testBEFORE_SOLVE = true;
129  testDt = sh->getWorkingState()->getTimeStep() / 10.0;
130  sh->getWorkingState()->setTimeStep(testDt);
131  break;
132  }
134  testAFTER_SOLVE = true;
135  testName = "Trapezoidal - Modifier";
136  stepper->setStepperName(testName);
137  break;
138  }
140  testEND_STEP = true;
141  auto x = sh->getWorkingState()->getX();
142  testWorkingValue = get_ele(*(x), 0);
143  break;
144  }
145  default:
146  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
147  "Error - unknown action location.\n");
148  }
149  }
150 
151  bool testBEGIN_STEP;
152  bool testBEFORE_SOLVE;
153  bool testAFTER_SOLVE;
154  bool testEND_STEP;
155  double testCurrentValue;
156  double testWorkingValue;
157  double testDt;
158  std::string testName;
159 };
160 
161 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Modifier)
162 {
165 
166  // Setup Stepper for field solve ----------------------------
167  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
168  stepper->setModel(model);
169  auto modifier = rcp(new StepperTrapezoidalModifierTest());
170  stepper->setAppAction(modifier);
171  stepper->initialize();
172 
173  // Create a SolutionHistory.
174  auto solutionHistory = Tempus::createSolutionHistoryME(model);
175 
176  // Take one time step.
177  stepper->setInitialConditions(solutionHistory);
178  solutionHistory->initWorkingState();
179  double dt = 0.1;
180  solutionHistory->getWorkingState()->setTimeStep(dt);
181  stepper->takeStep(solutionHistory);
182 
183  // Testing that each ACTION_LOCATION has been called.
184  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
185  TEST_COMPARE(modifier->testBEFORE_SOLVE, ==, true);
186  TEST_COMPARE(modifier->testAFTER_SOLVE, ==, true);
187  TEST_COMPARE(modifier->testEND_STEP, ==, true);
188 
189  // Testing that values can be set through the Modifier.
190  auto x = solutionHistory->getCurrentState()->getX();
191  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
192  x = solutionHistory->getWorkingState()->getX();
193  TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
194  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
195  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
196  TEST_COMPARE(modifier->testName, ==, "Trapezoidal - Modifier");
197 }
198 
199 // ************************************************************
200 // ************************************************************
201 class StepperTrapezoidalObserverTest
202  : virtual public Tempus::StepperTrapezoidalObserverBase<double> {
203  public:
205  StepperTrapezoidalObserverTest()
206  : testBEGIN_STEP(false),
207  testBEFORE_SOLVE(false),
208  testAFTER_SOLVE(false),
209  testEND_STEP(false),
210  testCurrentValue(-0.99),
211  testWorkingValue(-0.99),
212  testDt(-1.5),
213  testName("")
214  {
215  }
216 
218  virtual ~StepperTrapezoidalObserverTest() {}
219 
221  virtual void observe(
225  double>::ACTION_LOCATION actLoc)
226  {
227  switch (actLoc) {
229  testBEGIN_STEP = true;
230  auto x = sh->getCurrentState()->getX();
231  testCurrentValue = get_ele(*(x), 0);
232  break;
233  }
235  testBEFORE_SOLVE = true;
236  testDt = sh->getWorkingState()->getTimeStep();
237  break;
238  }
240  testAFTER_SOLVE = true;
241  testName = stepper->getStepperType();
242  break;
243  }
245  testEND_STEP = true;
246  auto x = sh->getWorkingState()->getX();
247  testWorkingValue = get_ele(*(x), 0);
248  break;
249  }
250  default:
251  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
252  "Error - unknown action location.\n");
253  }
254  }
255  bool testBEGIN_STEP;
256  bool testBEFORE_SOLVE;
257  bool testAFTER_SOLVE;
258  bool testEND_STEP;
259  double testCurrentValue;
260  double testWorkingValue;
261  double testDt;
262  std::string testName;
263 };
264 
265 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Observer)
266 {
269 
270  // Setup Stepper for field solve ----------------------------
271  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
272  stepper->setModel(model);
273  auto observer = rcp(new StepperTrapezoidalObserverTest());
274  stepper->setAppAction(observer);
275  stepper->initialize();
276 
277  // Setup a SolutionHistory.
278  auto solutionHistory = Tempus::createSolutionHistoryME(model);
279 
280  // Take one time step.
281  stepper->setInitialConditions(solutionHistory);
282  solutionHistory->initWorkingState();
283  double dt = 0.1;
284  solutionHistory->getWorkingState()->setTimeStep(dt);
285  stepper->takeStep(solutionHistory);
286 
287  // Testing that each ACTION_LOCATION has been called.
288  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
289  TEST_COMPARE(observer->testBEFORE_SOLVE, ==, true);
290  TEST_COMPARE(observer->testAFTER_SOLVE, ==, true);
291  TEST_COMPARE(observer->testEND_STEP, ==, true);
292 
293  // Testing that values can be observed through the observer.
294  auto x = solutionHistory->getCurrentState()->getX();
295  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
296  x = solutionHistory->getWorkingState()->getX();
297  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
298  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-14);
299  TEST_COMPARE(observer->testName, ==, "Trapezoidal Method");
300 }
301 
302 // ************************************************************
303 // ************************************************************
304 class StepperTrapezoidalModifierXTest
305  : virtual public Tempus::StepperTrapezoidalModifierXBase<double> {
306  public:
308  StepperTrapezoidalModifierXTest()
309  : testX_BEGIN_STEP(false),
310  testX_BEFORE_SOLVE(false),
311  testX_AFTER_SOLVE(false),
312  testXDOT_END_STEP(false),
313  testX(-0.99),
314  testXDot(-0.99),
315  testDt(-1.5),
316  testTime(-1.5)
317  {
318  }
319 
321  virtual ~StepperTrapezoidalModifierXTest() {}
324  const double time, const double dt,
326  double>::MODIFIER_TYPE modType)
327  {
328  switch (modType) {
330  testX_BEGIN_STEP = true;
331  testX = get_ele(*(x), 0);
332  break;
333  }
335  testX_BEFORE_SOLVE = true;
336  testDt = dt;
337  break;
338  }
340  testX_AFTER_SOLVE = true;
341  testTime = time;
342  break;
343  }
345  testXDOT_END_STEP = true;
346  testXDot = get_ele(*(x), 0);
347  break;
348  }
349  default:
350  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
351  "Error - unknown action location.\n");
352  }
353  }
354  bool testX_BEGIN_STEP;
355  bool testX_BEFORE_SOLVE;
356  bool testX_AFTER_SOLVE;
357  bool testXDOT_END_STEP;
358  double testX;
359  double testXDot;
360  double testDt;
361  double testTime;
362 };
363 
364 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_ModifierX)
365 {
368 
369  // Setup Stepper for field solve ----------------------------
370  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
371  stepper->setModel(model);
372  auto modifierX = rcp(new StepperTrapezoidalModifierXTest());
373  stepper->setAppAction(modifierX);
374  stepper->initialize();
375 
376  // Setup a SolutionHistory.
377  auto solutionHistory = Tempus::createSolutionHistoryME(model);
378 
379  // Take one time step.
380  stepper->setInitialConditions(solutionHistory);
381  solutionHistory->initWorkingState();
382  double dt = 0.1;
383  solutionHistory->getWorkingState()->setTimeStep(dt);
384  stepper->takeStep(solutionHistory);
385 
386  // Testing that each ACTION_LOCATION has been called.
387  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
388  TEST_COMPARE(modifierX->testX_BEFORE_SOLVE, ==, true);
389  TEST_COMPARE(modifierX->testX_AFTER_SOLVE, ==, true);
390  TEST_COMPARE(modifierX->testXDOT_END_STEP, ==, true);
391 
392  // Testing that values can be set through the Modifier.
393  auto x = solutionHistory->getCurrentState()->getX();
394  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
395  // Temporary memory for xDot is not guarranteed to exist outside the Stepper.
396  auto xDot = solutionHistory->getWorkingState()->getXDot();
397  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
398 
399  TEST_FLOATING_EQUALITY(modifierX->testXDot, get_ele(*(xDot), 0), 1.0e-14);
400  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
401  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
402  auto time = solutionHistory->getWorkingState()->getTime();
403  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-14);
404 }
405 
406 } // namespace Tempus_Unit_Test
Trapezoidal method time stepper.
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
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_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::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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Application Action for StepperTrapezoidal.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)