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: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
11 
12 #include "Tempus_StepperTrapezoidal.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 
29 // ************************************************************
30 // ************************************************************
31 TEUCHOS_UNIT_TEST(Trapezoidal, Default_Construction)
32 {
33  auto model = rcp(new Tempus_Test::SinCosModel<double>());
34 
35  // Default construction.
36  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
37  stepper->setModel(model);
38  stepper->initialize();
39  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
40 
41  // Default values for construction.
43  auto modifierX =
45  auto solver = rcp(new Thyra::NOXNonlinearSolver());
46  solver->setParameterList(Tempus::defaultSolverParameters());
47 
48  bool useFSAL = stepper->getUseFSAL();
49  std::string ICConsistency = stepper->getICConsistency();
50  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
51  bool zeroInitialGuess = stepper->getZeroInitialGuess();
52 
53  // Test the set functions.
54  stepper->setAppAction(modifier);
55  stepper->initialize();
56  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
57  stepper->setAppAction(modifierX);
58  stepper->initialize();
59  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
60  stepper->setSolver(solver);
61  stepper->initialize();
62  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
63  stepper->setUseFSAL(useFSAL);
64  stepper->initialize();
65  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
66  stepper->setICConsistency(ICConsistency);
67  stepper->initialize();
68  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
69  stepper->setICConsistencyCheck(ICConsistencyCheck);
70  stepper->initialize();
71  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
72  stepper->setZeroInitialGuess(zeroInitialGuess);
73  stepper->initialize();
74  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
75 
76  // Full argument list construction.
78  model, solver, useFSAL, ICConsistency, ICConsistencyCheck,
79  zeroInitialGuess, modifier));
80  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
81 
82  // Test stepper properties.
83  TEUCHOS_ASSERT(stepper->getOrder() == 2);
84 }
85 
86 // ************************************************************
87 // ************************************************************
88 TEUCHOS_UNIT_TEST(Trapezoidal, StepperFactory_Construction)
89 {
90  auto model = rcp(new Tempus_Test::SinCosModel<double>());
91  testFactoryConstruction("Trapezoidal Method", model);
92 }
93 
94 // ************************************************************
95 // ************************************************************
96 class StepperTrapezoidalModifierTest
97  : virtual public Tempus::StepperTrapezoidalModifierBase<double> {
98  public:
100  StepperTrapezoidalModifierTest()
101  : testBEGIN_STEP(false),
102  testBEFORE_SOLVE(false),
103  testAFTER_SOLVE(false),
104  testEND_STEP(false),
105  testCurrentValue(-0.99),
106  testWorkingValue(-0.99),
107  testDt(-1.5),
108  testName("")
109  {
110  }
111 
113  virtual ~StepperTrapezoidalModifierTest() {}
114 
116  virtual void modify(Teuchos::RCP<Tempus::SolutionHistory<double> > sh,
119  double>::ACTION_LOCATION actLoc)
120  {
121  switch (actLoc) {
123  testBEGIN_STEP = true;
124  auto x = sh->getCurrentState()->getX();
125  testCurrentValue = get_ele(*(x), 0);
126  break;
127  }
129  testBEFORE_SOLVE = true;
130  testDt = sh->getWorkingState()->getTimeStep() / 10.0;
131  sh->getWorkingState()->setTimeStep(testDt);
132  break;
133  }
135  testAFTER_SOLVE = true;
136  testName = "Trapezoidal - Modifier";
137  stepper->setStepperName(testName);
138  break;
139  }
141  testEND_STEP = true;
142  auto x = sh->getWorkingState()->getX();
143  testWorkingValue = get_ele(*(x), 0);
144  break;
145  }
146  default:
147  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
148  "Error - unknown action location.\n");
149  }
150  }
151 
152  bool testBEGIN_STEP;
153  bool testBEFORE_SOLVE;
154  bool testAFTER_SOLVE;
155  bool testEND_STEP;
156  double testCurrentValue;
157  double testWorkingValue;
158  double testDt;
159  std::string testName;
160 };
161 
162 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Modifier)
163 {
166 
167  // Setup Stepper for field solve ----------------------------
168  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
169  stepper->setModel(model);
170  auto modifier = rcp(new StepperTrapezoidalModifierTest());
171  stepper->setAppAction(modifier);
172  stepper->initialize();
173 
174  // Create a SolutionHistory.
175  auto solutionHistory = Tempus::createSolutionHistoryME(model);
176 
177  // Take one time step.
178  stepper->setInitialConditions(solutionHistory);
179  solutionHistory->initWorkingState();
180  double dt = 0.1;
181  solutionHistory->getWorkingState()->setTimeStep(dt);
182  stepper->takeStep(solutionHistory);
183 
184  // Testing that each ACTION_LOCATION has been called.
185  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
186  TEST_COMPARE(modifier->testBEFORE_SOLVE, ==, true);
187  TEST_COMPARE(modifier->testAFTER_SOLVE, ==, true);
188  TEST_COMPARE(modifier->testEND_STEP, ==, true);
189 
190  // Testing that values can be set through the Modifier.
191  auto x = solutionHistory->getCurrentState()->getX();
192  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
193  x = solutionHistory->getWorkingState()->getX();
194  TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
195  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
196  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
197  TEST_COMPARE(modifier->testName, ==, "Trapezoidal - Modifier");
198 }
199 
200 // ************************************************************
201 // ************************************************************
202 class StepperTrapezoidalObserverTest
203  : virtual public Tempus::StepperTrapezoidalObserverBase<double> {
204  public:
206  StepperTrapezoidalObserverTest()
207  : testBEGIN_STEP(false),
208  testBEFORE_SOLVE(false),
209  testAFTER_SOLVE(false),
210  testEND_STEP(false),
211  testCurrentValue(-0.99),
212  testWorkingValue(-0.99),
213  testDt(-1.5),
214  testName("")
215  {
216  }
217 
219  virtual ~StepperTrapezoidalObserverTest() {}
220 
222  virtual void observe(
226  double>::ACTION_LOCATION actLoc)
227  {
228  switch (actLoc) {
230  testBEGIN_STEP = true;
231  auto x = sh->getCurrentState()->getX();
232  testCurrentValue = get_ele(*(x), 0);
233  break;
234  }
236  testBEFORE_SOLVE = true;
237  testDt = sh->getWorkingState()->getTimeStep();
238  break;
239  }
241  testAFTER_SOLVE = true;
242  testName = stepper->getStepperType();
243  break;
244  }
246  testEND_STEP = true;
247  auto x = sh->getWorkingState()->getX();
248  testWorkingValue = get_ele(*(x), 0);
249  break;
250  }
251  default:
252  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
253  "Error - unknown action location.\n");
254  }
255  }
256  bool testBEGIN_STEP;
257  bool testBEFORE_SOLVE;
258  bool testAFTER_SOLVE;
259  bool testEND_STEP;
260  double testCurrentValue;
261  double testWorkingValue;
262  double testDt;
263  std::string testName;
264 };
265 
266 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Observer)
267 {
270 
271  // Setup Stepper for field solve ----------------------------
272  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
273  stepper->setModel(model);
274  auto observer = rcp(new StepperTrapezoidalObserverTest());
275  stepper->setAppAction(observer);
276  stepper->initialize();
277 
278  // Setup a SolutionHistory.
279  auto solutionHistory = Tempus::createSolutionHistoryME(model);
280 
281  // Take one time step.
282  stepper->setInitialConditions(solutionHistory);
283  solutionHistory->initWorkingState();
284  double dt = 0.1;
285  solutionHistory->getWorkingState()->setTimeStep(dt);
286  stepper->takeStep(solutionHistory);
287 
288  // Testing that each ACTION_LOCATION has been called.
289  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
290  TEST_COMPARE(observer->testBEFORE_SOLVE, ==, true);
291  TEST_COMPARE(observer->testAFTER_SOLVE, ==, true);
292  TEST_COMPARE(observer->testEND_STEP, ==, true);
293 
294  // Testing that values can be observed through the observer.
295  auto x = solutionHistory->getCurrentState()->getX();
296  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
297  x = solutionHistory->getWorkingState()->getX();
298  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
299  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-14);
300  TEST_COMPARE(observer->testName, ==, "Trapezoidal Method");
301 }
302 
303 // ************************************************************
304 // ************************************************************
305 class StepperTrapezoidalModifierXTest
306  : virtual public Tempus::StepperTrapezoidalModifierXBase<double> {
307  public:
309  StepperTrapezoidalModifierXTest()
310  : testX_BEGIN_STEP(false),
311  testX_BEFORE_SOLVE(false),
312  testX_AFTER_SOLVE(false),
313  testXDOT_END_STEP(false),
314  testX(-0.99),
315  testXDot(-0.99),
316  testDt(-1.5),
317  testTime(-1.5)
318  {
319  }
320 
322  virtual ~StepperTrapezoidalModifierXTest() {}
325  const double time, const double dt,
327  double>::MODIFIER_TYPE modType)
328  {
329  switch (modType) {
331  testX_BEGIN_STEP = true;
332  testX = get_ele(*(x), 0);
333  break;
334  }
336  testX_BEFORE_SOLVE = true;
337  testDt = dt;
338  break;
339  }
341  testX_AFTER_SOLVE = true;
342  testTime = time;
343  break;
344  }
346  testXDOT_END_STEP = true;
347  testXDot = get_ele(*(x), 0);
348  break;
349  }
350  default:
351  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
352  "Error - unknown action location.\n");
353  }
354  }
355  bool testX_BEGIN_STEP;
356  bool testX_BEFORE_SOLVE;
357  bool testX_AFTER_SOLVE;
358  bool testXDOT_END_STEP;
359  double testX;
360  double testXDot;
361  double testDt;
362  double testTime;
363 };
364 
365 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_ModifierX)
366 {
369 
370  // Setup Stepper for field solve ----------------------------
371  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
372  stepper->setModel(model);
373  auto modifierX = rcp(new StepperTrapezoidalModifierXTest());
374  stepper->setAppAction(modifierX);
375  stepper->initialize();
376 
377  // Setup a SolutionHistory.
378  auto solutionHistory = Tempus::createSolutionHistoryME(model);
379 
380  // Take one time step.
381  stepper->setInitialConditions(solutionHistory);
382  solutionHistory->initWorkingState();
383  double dt = 0.1;
384  solutionHistory->getWorkingState()->setTimeStep(dt);
385  stepper->takeStep(solutionHistory);
386 
387  // Testing that each ACTION_LOCATION has been called.
388  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
389  TEST_COMPARE(modifierX->testX_BEFORE_SOLVE, ==, true);
390  TEST_COMPARE(modifierX->testX_AFTER_SOLVE, ==, true);
391  TEST_COMPARE(modifierX->testXDOT_END_STEP, ==, true);
392 
393  // Testing that values can be set through the Modifier.
394  auto x = solutionHistory->getCurrentState()->getX();
395  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
396  // Temporary memory for xDot is not guarranteed to exist outside the Stepper.
397  auto xDot = solutionHistory->getWorkingState()->getXDot();
398  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
399 
400  TEST_FLOATING_EQUALITY(modifierX->testXDot, get_ele(*(xDot), 0), 1.0e-14);
401  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
402  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
403  auto time = solutionHistory->getWorkingState()->getTime();
404  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-14);
405 }
406 
407 } // 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)