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 
20 namespace Tempus_Unit_Test {
21 
22 using Teuchos::RCP;
23 using Teuchos::rcp;
24 using Teuchos::rcp_const_cast;
25 using Teuchos::rcp_dynamic_cast;
27 using Teuchos::sublist;
28 
29 
30 // ************************************************************
31 // ************************************************************
32 TEUCHOS_UNIT_TEST(Trapezoidal, Default_Construction)
33 {
34  auto model = rcp(new Tempus_Test::SinCosModel<double>());
35 
36  // Default construction.
37  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
38  stepper->setModel(model);
39  stepper->initialize();
40  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
41 
42 
43  // Default values for construction.
46  auto solver = rcp(new Thyra::NOXNonlinearSolver());
47  solver->setParameterList(Tempus::defaultSolverParameters());
48 
49  bool useFSAL = stepper->getUseFSAL();
50  std::string ICConsistency = stepper->getICConsistency();
51  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
52  bool zeroInitialGuess = stepper->getZeroInitialGuess();
53 
54  // Test the set functions.
55  stepper->setAppAction(modifier); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
56  stepper->setAppAction(modifierX); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
57  stepper->setSolver(solver); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
58  stepper->setUseFSAL(useFSAL); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
59  stepper->setICConsistency(ICConsistency); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
60  stepper->setICConsistencyCheck(ICConsistencyCheck); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
61  stepper->setZeroInitialGuess(zeroInitialGuess); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
62 
63 // Full argument list construction.
65 model, solver, useFSAL,
66  ICConsistency, ICConsistencyCheck, zeroInitialGuess, modifier));
67 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
68 
69  // Test stepper properties.
70  TEUCHOS_ASSERT(stepper->getOrder() == 2);
71 }
72 
73 
74 // ************************************************************
75 // ************************************************************
76 TEUCHOS_UNIT_TEST(Trapezoidal, StepperFactory_Construction)
77 {
78  auto model = rcp(new Tempus_Test::SinCosModel<double>());
79  testFactoryConstruction("Trapezoidal Method", model);
80 }
81 
82 // ************************************************************
83 // ************************************************************
84 class StepperTrapezoidalModifierTest
85  : virtual public Tempus::StepperTrapezoidalModifierBase<double>
86 {
87 public:
89  StepperTrapezoidalModifierTest()
90  : testBEGIN_STEP(false), testBEFORE_SOLVE(false),
91  testAFTER_SOLVE(false), testEND_STEP(false),
92  testCurrentValue(-0.99), testWorkingValue(-0.99),
93  testDt(-1.5), testName("")
94  {}
95 
97  virtual ~StepperTrapezoidalModifierTest(){}
98 
100  virtual void modify(
104  {
105  switch(actLoc) {
107  {
108  testBEGIN_STEP = true;
109  auto x = sh->getCurrentState()->getX();
110  testCurrentValue = get_ele(*(x), 0);
111  break;
112  }
114  {
115  testBEFORE_SOLVE = true;
116  testDt = sh->getWorkingState()->getTimeStep()/10.0;
117  sh->getWorkingState()->setTimeStep(testDt);
118  break;
119  }
121  {
122  testAFTER_SOLVE = true;
123  testName = "Trapezoidal - Modifier";
124  stepper->setStepperName(testName);
125  break;
126  }
128  {
129  testEND_STEP = true;
130  auto x = sh->getWorkingState()->getX();
131  testWorkingValue = get_ele(*(x), 0);
132  break;
133  }
134  default:
135  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
136  "Error - unknown action location.\n");
137  }
138  }
139 
140  bool testBEGIN_STEP;
141  bool testBEFORE_SOLVE;
142  bool testAFTER_SOLVE;
143  bool testEND_STEP;
144  double testCurrentValue;
145  double testWorkingValue;
146  double testDt;
147  std::string testName;
148 };
149 
150 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Modifier)
151 {
153  model = rcp(new Tempus_Test::SinCosModel<double>());
154 
155  // Setup Stepper for field solve ----------------------------
156  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
157  stepper->setModel(model);
158  auto modifier = rcp(new StepperTrapezoidalModifierTest());
159  stepper->setAppAction(modifier);
160  stepper->initialize();
161 
162  // Create a SolutionHistory.
163  auto solutionHistory = Tempus::createSolutionHistoryME(model);
164 
165  // Take one time step.
166  stepper->setInitialConditions(solutionHistory);
167  solutionHistory->initWorkingState();
168  double dt = 0.1;
169  solutionHistory->getWorkingState()->setTimeStep(dt);
170  stepper->takeStep(solutionHistory);
171 
172  // Testing that each ACTION_LOCATION has been called.
173  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
174  TEST_COMPARE(modifier->testBEFORE_SOLVE, ==, true);
175  TEST_COMPARE(modifier->testAFTER_SOLVE, ==, true);
176  TEST_COMPARE(modifier->testEND_STEP, ==, true);
177 
178  // Testing that values can be set through the Modifier.
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  TEST_COMPARE(modifier->testName, ==, "Trapezoidal - Modifier");
186 }
187 
188 // ************************************************************
189 // ************************************************************
190 class StepperTrapezoidalObserverTest
191  : virtual public Tempus::StepperTrapezoidalObserverBase<double>
192 {
193 public:
194 
196  StepperTrapezoidalObserverTest()
197  : testBEGIN_STEP(false), testBEFORE_SOLVE(false),
198  testAFTER_SOLVE(false), testEND_STEP(false),
199  testCurrentValue(-0.99), testWorkingValue(-0.99),
200  testDt(-1.5), testName("")
201  {}
202 
204  virtual ~StepperTrapezoidalObserverTest(){}
205 
207  virtual void observe(
211  {
212  switch(actLoc) {
214  {
215  testBEGIN_STEP = true;
216  auto x = sh->getCurrentState()->getX();
217  testCurrentValue = get_ele(*(x), 0);
218  break;
219  }
221  {
222  testBEFORE_SOLVE = true;
223  testDt = sh->getWorkingState()->getTimeStep();
224  break;
225  }
227  {
228  testAFTER_SOLVE = true;
229  testName = stepper->getStepperType();
230  break;
231  }
233  {
234  testEND_STEP = true;
235  auto x = sh->getWorkingState()->getX();
236  testWorkingValue = get_ele(*(x), 0);
237  break;
238  }
239  default:
240  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
241  "Error - unknown action location.\n");
242  }
243  }
244  bool testBEGIN_STEP;
245  bool testBEFORE_SOLVE;
246  bool testAFTER_SOLVE;
247  bool testEND_STEP;
248  double testCurrentValue;
249  double testWorkingValue;
250  double testDt;
251  std::string testName;
252 };
253 
254 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Observer)
255 {
257  model = rcp(new Tempus_Test::SinCosModel<double>());
258 
259  // Setup Stepper for field solve ----------------------------
260  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
261  stepper->setModel(model);
262  auto observer = rcp(new StepperTrapezoidalObserverTest());
263  stepper->setAppAction(observer);
264  stepper->initialize();
265 
266  // Setup a SolutionHistory.
267  auto solutionHistory = Tempus::createSolutionHistoryME(model);
268 
269  // Take one time step.
270  stepper->setInitialConditions(solutionHistory);
271  solutionHistory->initWorkingState();
272  double dt = 0.1;
273  solutionHistory->getWorkingState()->setTimeStep(dt);
274  stepper->takeStep(solutionHistory);
275 
276  // Testing that each ACTION_LOCATION has been called.
277  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
278  TEST_COMPARE(observer->testBEFORE_SOLVE, ==, true);
279  TEST_COMPARE(observer->testAFTER_SOLVE, ==, true);
280  TEST_COMPARE(observer->testEND_STEP, ==, true);
281 
282  // Testing that values can be observed through the observer.
283  auto x = solutionHistory->getCurrentState()->getX();
284  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
285  x = solutionHistory->getWorkingState()->getX();
286  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
287  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-14);
288  TEST_COMPARE(observer->testName, ==, "Trapezoidal Method");
289 }
290 
291 // ************************************************************
292 // ************************************************************
293 class StepperTrapezoidalModifierXTest
294  : virtual public Tempus::StepperTrapezoidalModifierXBase<double>
295 {
296 public:
297 
299  StepperTrapezoidalModifierXTest()
300  : testX_BEGIN_STEP(false), testX_BEFORE_SOLVE(false),
301  testX_AFTER_SOLVE(false), testXDOT_END_STEP(false),
302  testX(-0.99), testXDot(-0.99),
303  testDt(-1.5), testTime(-1.5)
304  {}
305 
307  virtual ~StepperTrapezoidalModifierXTest(){}
309  virtual void modify(
311  const double time, const double dt,
313  {
314  switch(modType) {
316  {
317  testX_BEGIN_STEP = true;
318  testX = get_ele(*(x), 0);
319  break;
320  }
322  {
323  testX_BEFORE_SOLVE = true;
324  testDt = dt;
325  break;
326  }
328  {
329  testX_AFTER_SOLVE = true;
330  testTime = time;
331  break;
332  }
334  {
335  testXDOT_END_STEP = true;
336  testXDot = get_ele(*(x), 0);
337  break;
338  }
339  default:
340  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
341  "Error - unknown action location.\n");
342  }
343  }
344  bool testX_BEGIN_STEP;
345  bool testX_BEFORE_SOLVE;
346  bool testX_AFTER_SOLVE;
347  bool testXDOT_END_STEP;
348  double testX;
349  double testXDot;
350  double testDt;
351  double testTime;
352 };
353 
354 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_ModifierX)
355 {
357  model = rcp(new Tempus_Test::SinCosModel<double>());
358 
359  // Setup Stepper for field solve ----------------------------
360  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
361  stepper->setModel(model);
362  auto modifierX = rcp(new StepperTrapezoidalModifierXTest());
363  stepper->setAppAction(modifierX);
364  stepper->initialize();
365 
366  // Setup a SolutionHistory.
367  auto solutionHistory = Tempus::createSolutionHistoryME(model);
368 
369  // Take one time step.
370  stepper->setInitialConditions(solutionHistory);
371  solutionHistory->initWorkingState();
372  double dt = 0.1;
373  solutionHistory->getWorkingState()->setTimeStep(dt);
374  stepper->takeStep(solutionHistory);
375 
376  // Testing that each ACTION_LOCATION has been called.
377  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
378  TEST_COMPARE(modifierX->testX_BEFORE_SOLVE, ==, true);
379  TEST_COMPARE(modifierX->testX_AFTER_SOLVE, ==, true);
380  TEST_COMPARE(modifierX->testXDOT_END_STEP, ==, true);
381 
382  // Testing that values can be set through the Modifier.
383  auto x = solutionHistory->getCurrentState()->getX();
384  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
385  // Temporary memory for xDot is not guarranteed to exist outside the Stepper.
386  auto xDot = solutionHistory->getWorkingState()->getXDot();
387  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
388 
389  TEST_FLOATING_EQUALITY(modifierX->testXDot, get_ele(*(xDot), 0),1.0e-14);
390  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
391  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
392  auto time = solutionHistory->getWorkingState()->getTime();
393  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-14);
394  }
395 
396 } // namespace Tempus_Unit_Test
MODIFIER_TYPE
Indicates the location of application action (see algorithm).
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)
ACTION_LOCATION
Indicates the location of application action (see algorithm).
#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)
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)