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 
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 
14 #include "Thyra_VectorStdOps.hpp"
15 
16 #include "Tempus_StepperTrapezoidal.hpp"
24 
25 #include "../TestModels/SinCosModel.hpp"
26 #include "../TestModels/VanDerPolModel.hpp"
27 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
28 
29 #include <fstream>
30 #include <vector>
31 
32 namespace Tempus_Unit_Test {
33 
34 using Teuchos::RCP;
35 using Teuchos::rcp;
36 using Teuchos::rcp_const_cast;
37 using Teuchos::rcp_dynamic_cast;
39 using Teuchos::sublist;
40 using Teuchos::getParametersFromXmlFile;
41 
42 
43 // ************************************************************
44 // ************************************************************
45 TEUCHOS_UNIT_TEST(Trapezoidal, Default_Construction)
46 {
47  auto model = rcp(new Tempus_Test::SinCosModel<double>());
48 
49  // Default construction.
50  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
51  stepper->setModel(model);
52  stepper->initialize();
53  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
54 
55 
56  // Default values for construction.
59  auto solver = rcp(new Thyra::NOXNonlinearSolver());
60  solver->setParameterList(Tempus::defaultSolverParameters());
61 
62  bool useFSAL = stepper->getUseFSAL();
63  std::string ICConsistency = stepper->getICConsistency();
64  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
65  bool zeroInitialGuess = stepper->getZeroInitialGuess();
66 
67  // Test the set functions.
68  stepper->setAppAction(modifier); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
69  stepper->setAppAction(modifierX); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
70  stepper->setSolver(solver); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
71  stepper->setUseFSAL(useFSAL); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
72  stepper->setICConsistency(ICConsistency); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
73  stepper->setICConsistencyCheck(ICConsistencyCheck); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
74  stepper->setZeroInitialGuess(zeroInitialGuess); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
75 
76 // Full argument list construction.
78 model, solver, useFSAL,
79  ICConsistency, ICConsistencyCheck, 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 // ************************************************************
89 TEUCHOS_UNIT_TEST(Trapezoidal, StepperFactory_Construction)
90 {
91  auto model = rcp(new Tempus_Test::SinCosModel<double>());
92  testFactoryConstruction("Trapezoidal Method", model);
93 }
94 
95 // ************************************************************
96 // ************************************************************
97 class StepperTrapezoidalModifierTest
98  : virtual public Tempus::StepperTrapezoidalModifierBase<double>
99 {
100 public:
102  StepperTrapezoidalModifierTest()
103  : testBEGIN_STEP(false), testBEFORE_SOLVE(false),
104  testAFTER_SOLVE(false), testEND_STEP(false),
105  testCurrentValue(-0.99), testWorkingValue(-0.99),
106  testDt(-1.5), testName("")
107  {}
108 
110  virtual ~StepperTrapezoidalModifierTest(){}
111 
113  virtual void modify(
117  {
118  switch(actLoc) {
120  {
121  testBEGIN_STEP = true;
122  auto x = sh->getCurrentState()->getX();
123  testCurrentValue = get_ele(*(x), 0);
124  break;
125  }
127  {
128  testBEFORE_SOLVE = true;
129  testDt = sh->getWorkingState()->getTimeStep()/10.0;
130  sh->getWorkingState()->setTimeStep(testDt);
131  break;
132  }
134  {
135  testAFTER_SOLVE = true;
136  testName = "Trapezoidal - Modifier";
137  stepper->setStepperName(testName);
138  break;
139  }
141  {
142  testEND_STEP = true;
143  auto x = sh->getWorkingState()->getX();
144  testWorkingValue = get_ele(*(x), 0);
145  break;
146  }
147  default:
148  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
149  "Error - unknown action location.\n");
150  }
151  }
152 
153  bool testBEGIN_STEP;
154  bool testBEFORE_SOLVE;
155  bool testAFTER_SOLVE;
156  bool testEND_STEP;
157  double testCurrentValue;
158  double testWorkingValue;
159  double testDt;
160  std::string testName;
161 };
162 
163 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Modifier)
164 {
166  model = rcp(new Tempus_Test::SinCosModel<double>());
167 
168  // Setup Stepper for field solve ----------------------------
169  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
170  stepper->setModel(model);
171  auto modifier = rcp(new StepperTrapezoidalModifierTest());
172  stepper->setAppAction(modifier);
173  stepper->initialize();
174 
175  // Create a SolutionHistory.
176  auto solutionHistory = Tempus::createSolutionHistoryME(model);
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  // Testing that each ACTION_LOCATION has been called.
186  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
187  TEST_COMPARE(modifier->testBEFORE_SOLVE, ==, true);
188  TEST_COMPARE(modifier->testAFTER_SOLVE, ==, true);
189  TEST_COMPARE(modifier->testEND_STEP, ==, true);
190 
191  // Testing that values can be set through the Modifier.
192  auto x = solutionHistory->getCurrentState()->getX();
193  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
194  x = solutionHistory->getWorkingState()->getX();
195  TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
196  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
197  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
198  TEST_COMPARE(modifier->testName, ==, "Trapezoidal - Modifier");
199 }
200 
201 // ************************************************************
202 // ************************************************************
203 class StepperTrapezoidalObserverTest
204  : virtual public Tempus::StepperTrapezoidalObserverBase<double>
205 {
206 public:
207 
209  StepperTrapezoidalObserverTest()
210  : testBEGIN_STEP(false), testBEFORE_SOLVE(false),
211  testAFTER_SOLVE(false), testEND_STEP(false),
212  testCurrentValue(-0.99), testWorkingValue(-0.99),
213  testDt(-1.5), testName("")
214  {}
215 
217  virtual ~StepperTrapezoidalObserverTest(){}
218 
220  virtual void observe(
224  {
225  switch(actLoc) {
227  {
228  testBEGIN_STEP = true;
229  auto x = sh->getCurrentState()->getX();
230  testCurrentValue = get_ele(*(x), 0);
231  break;
232  }
234  {
235  testBEFORE_SOLVE = true;
236  testDt = sh->getWorkingState()->getTimeStep();
237  break;
238  }
240  {
241  testAFTER_SOLVE = true;
242  testName = stepper->getStepperType();
243  break;
244  }
246  {
247  testEND_STEP = true;
248  auto x = sh->getWorkingState()->getX();
249  testWorkingValue = get_ele(*(x), 0);
250  break;
251  }
252  default:
253  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
254  "Error - unknown action location.\n");
255  }
256  }
257  bool testBEGIN_STEP;
258  bool testBEFORE_SOLVE;
259  bool testAFTER_SOLVE;
260  bool testEND_STEP;
261  double testCurrentValue;
262  double testWorkingValue;
263  double testDt;
264  std::string testName;
265 };
266 
267 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Observer)
268 {
270  model = rcp(new Tempus_Test::SinCosModel<double>());
271 
272  // Setup Stepper for field solve ----------------------------
273  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
274  stepper->setModel(model);
275  auto observer = rcp(new StepperTrapezoidalObserverTest());
276  stepper->setAppAction(observer);
277  stepper->initialize();
278 
279  // Setup a SolutionHistory.
280  auto solutionHistory = Tempus::createSolutionHistoryME(model);
281 
282  // Take one time step.
283  stepper->setInitialConditions(solutionHistory);
284  solutionHistory->initWorkingState();
285  double dt = 0.1;
286  solutionHistory->getWorkingState()->setTimeStep(dt);
287  stepper->takeStep(solutionHistory);
288 
289  // Testing that each ACTION_LOCATION has been called.
290  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
291  TEST_COMPARE(observer->testBEFORE_SOLVE, ==, true);
292  TEST_COMPARE(observer->testAFTER_SOLVE, ==, true);
293  TEST_COMPARE(observer->testEND_STEP, ==, true);
294 
295  // Testing that values can be observed through the observer.
296  auto x = solutionHistory->getCurrentState()->getX();
297  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
298  x = solutionHistory->getWorkingState()->getX();
299  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
300  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-14);
301  TEST_COMPARE(observer->testName, ==, "Trapezoidal Method");
302 }
303 
304 // ************************************************************
305 // ************************************************************
306 class StepperTrapezoidalModifierXTest
307  : virtual public Tempus::StepperTrapezoidalModifierXBase<double>
308 {
309 public:
310 
312  StepperTrapezoidalModifierXTest()
313  : testX_BEGIN_STEP(false), testX_BEFORE_SOLVE(false),
314  testX_AFTER_SOLVE(false), testXDOT_END_STEP(false),
315  testX(-0.99), testXDot(-0.99),
316  testDt(-1.5), testTime(-1.5)
317  {}
318 
320  virtual ~StepperTrapezoidalModifierXTest(){}
322  virtual void modify(
324  const double time, const double dt,
326  {
327  switch(modType) {
329  {
330  testX_BEGIN_STEP = true;
331  testX = get_ele(*(x), 0);
332  break;
333  }
335  {
336  testX_BEFORE_SOLVE = true;
337  testDt = dt;
338  break;
339  }
341  {
342  testX_AFTER_SOLVE = true;
343  testTime = time;
344  break;
345  }
347  {
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_SOLVE;
359  bool testX_AFTER_SOLVE;
360  bool testXDOT_END_STEP;
361  double testX;
362  double testXDot;
363  double testDt;
364  double testTime;
365 };
366 
367 TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_ModifierX)
368 {
370  model = rcp(new Tempus_Test::SinCosModel<double>());
371 
372  // Setup Stepper for field solve ----------------------------
373  auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
374  stepper->setModel(model);
375  auto modifierX = rcp(new StepperTrapezoidalModifierXTest());
376  stepper->setAppAction(modifierX);
377  stepper->initialize();
378 
379  // Setup a SolutionHistory.
380  auto solutionHistory = Tempus::createSolutionHistoryME(model);
381 
382  // Take one time step.
383  stepper->setInitialConditions(solutionHistory);
384  solutionHistory->initWorkingState();
385  double dt = 0.1;
386  solutionHistory->getWorkingState()->setTimeStep(dt);
387  stepper->takeStep(solutionHistory);
388 
389  // Testing that each ACTION_LOCATION has been called.
390  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
391  TEST_COMPARE(modifierX->testX_BEFORE_SOLVE, ==, true);
392  TEST_COMPARE(modifierX->testX_AFTER_SOLVE, ==, true);
393  TEST_COMPARE(modifierX->testXDOT_END_STEP, ==, true);
394 
395  // Testing that values can be set through the Modifier.
396  auto x = solutionHistory->getCurrentState()->getX();
397  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
398  // Temporary memory for xDot is not guarranteed to exist outside the Stepper.
399  auto xDot = solutionHistory->getWorkingState()->getXDot();
400  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
401 
402  TEST_FLOATING_EQUALITY(modifierX->testXDot, get_ele(*(xDot), 0),1.0e-14);
403  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
404  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
405  auto time = solutionHistory->getWorkingState()->getTime();
406  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-14);
407  }
408 
409 } // namespace Tempus_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)