Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_HHTAlpha.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_StepperHHTAlpha.hpp"
24 
25 #include "../TestModels/SinCosModel.hpp"
26 #include "../TestModels/VanDerPolModel.hpp"
27 #include "../TestModels/HarmonicOscillatorModel.hpp"
28 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
29 
30 #include <fstream>
31 #include <vector>
32 
33 namespace Tempus_Unit_Test {
34 
35 using Teuchos::RCP;
36 using Teuchos::rcp;
37 using Teuchos::rcp_const_cast;
38 using Teuchos::rcp_dynamic_cast;
40 using Teuchos::sublist;
41 using Teuchos::getParametersFromXmlFile;
42 
43 
44 // ************************************************************
45 // ************************************************************
46 TEUCHOS_UNIT_TEST(HHTAlpha, Default_Construction)
47 {
49 
50  // Default construction.
51  auto stepper = rcp(new Tempus::StepperHHTAlpha<double>());
52  stepper->setModel(model);
53  stepper->initialize();
54  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
55 
56 
57  // Default values for construction.
58  auto solver = rcp(new Thyra::NOXNonlinearSolver());
59  solver->setParameterList(Tempus::defaultSolverParameters());
60 
61  bool useFSAL = stepper->getUseFSAL();
62  std::string ICConsistency = stepper->getICConsistency();
63  bool ICConsistencyCheck = stepper->getICConsistencyCheck();
64  bool zeroInitialGuess = stepper->getZeroInitialGuess();
65  std::string schemeName = "Newmark Beta User Defined";
66  double beta = 0.25;
67  double gamma = 0.5;
68  double alpha_f = 0.0;
69  double alpha_m = 0.0;
70 
74 
75  // Test the set functions.
76  stepper->setSolver(solver); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
77  stepper->setUseFSAL(useFSAL); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
78  stepper->setICConsistency(ICConsistency); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
79  stepper->setICConsistencyCheck(ICConsistencyCheck); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
80  stepper->setZeroInitialGuess(zeroInitialGuess); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
81 
82  stepper->setAppAction(modifier); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
83  stepper->setAppAction(modifierX); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
84  stepper->setAppAction(observer); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
85 
86  stepper->setSchemeName(schemeName); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
87  stepper->setBeta(beta); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
88  stepper->setGamma(gamma); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
89  stepper->setAlphaF(alpha_f); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
90  stepper->setAlphaM(alpha_m); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
91 
92  // Full argument list construction.
94  model, solver, useFSAL,
95  ICConsistency, ICConsistencyCheck, zeroInitialGuess,
96  schemeName, beta, gamma, alpha_f, alpha_m,modifier));
97  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
98 
99  // Test stepper properties.
100  TEUCHOS_ASSERT(stepper->getOrder() == 2);
101 }
102 
103 
104 // ************************************************************
105 // ************************************************************
106 TEUCHOS_UNIT_TEST(HHTAlpha, StepperFactory_Construction)
107 {
109  testFactoryConstruction("HHT-Alpha", model);
110 }
111 
112 // ************************************************************
113 // ************************************************************
114 class StepperHHTAlphaModifierTest
115  : virtual public Tempus::StepperHHTAlphaModifierBase<double>
116 {
117 public:
118 
120  StepperHHTAlphaModifierTest()
121  : testBEGIN_STEP(false), testBEFORE_SOLVE(false),
122  testAFTER_SOLVE(false), testEND_STEP(false),
123  testCurrentValue(-0.99), testWorkingValue(-0.99),
124  testDt(-1.5), testName("")
125  {}
126 
128  virtual ~StepperHHTAlphaModifierTest(){}
129 
131  virtual void modify(
135  {
136  switch(actLoc) {
138  {
139  testBEGIN_STEP = true;
140  auto x = sh->getCurrentState()->getX();
141  testCurrentValue = get_ele(*(x), 0);
142  break;
143  }
145  {
146  testBEFORE_SOLVE = true;
147  testDt = sh->getWorkingState()->getTimeStep()/10.0;
148  sh->getWorkingState()->setTimeStep(testDt);
149  break;
150  }
152  {
153  testAFTER_SOLVE = true;
154  testName = "HHT Alpha - Modifier";
155  stepper->setStepperName(testName);
156  break;
157  }
159  {
160  testEND_STEP = true;
161  auto x = sh->getWorkingState()->getX();
162  testWorkingValue = get_ele(*(x), 0);
163  break;
164  }
165  default:
166  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
167  "Error - unknown action location.\n");
168  }
169  }
170  bool testBEGIN_STEP;
171  bool testBEFORE_SOLVE;
172  bool testAFTER_SOLVE;
173  bool testEND_STEP;
174  double testCurrentValue;
175  double testWorkingValue;
176  double testDt;
177  std::string testName;
178 };
179 
180 TEUCHOS_UNIT_TEST(HHTAlpha, AppAction_Modifier)
181 {
184 
185  // Setup Stepper for field solve ----------------------------
186  auto stepper = rcp(new Tempus::StepperHHTAlpha<double>());
187  stepper->setModel(model);
188  auto modifier = rcp(new StepperHHTAlphaModifierTest());
189  stepper->setAppAction(modifier);
190  stepper->initialize();
191 
192  // Create a SolutionHistory.
193  auto solutionHistory = Tempus::createSolutionHistoryME(model);
194 
195  // Take one time step.
196  stepper->setInitialConditions(solutionHistory);
197  solutionHistory->initWorkingState();
198  double dt = 0.1;
199  solutionHistory->getWorkingState()->setTimeStep(dt);
200  stepper->takeStep(solutionHistory);
201 
202  // Testing that each ACTION_LOCATION has been called.
203  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
204  TEST_COMPARE(modifier->testBEFORE_SOLVE, ==, true);
205  TEST_COMPARE(modifier->testAFTER_SOLVE, ==, true);
206  TEST_COMPARE(modifier->testEND_STEP, ==, true);
207 
208  // Testing that values can be set through the Modifier.
209  auto x = solutionHistory->getCurrentState()->getX();
210  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
211  x = solutionHistory->getWorkingState()->getX();
212  TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
213  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
214  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
215  TEST_COMPARE(modifier->testName, ==, "HHT Alpha - Modifier");
216 }
217 
218  // ************************************************************
219  // ************************************************************
220 class StepperHHTAlphaObserverTest
221  : virtual public Tempus::StepperHHTAlphaObserverBase<double>
222 {
223 public:
225  StepperHHTAlphaObserverTest()
226  : testBEGIN_STEP(false), testBEFORE_SOLVE(false),
227  testAFTER_SOLVE(false), testEND_STEP(false),
228  testCurrentValue(-0.99), testWorkingValue(-0.99),
229  testDt(-1.5), testName("")
230  {}
231 
233  virtual ~StepperHHTAlphaObserverTest(){}
234 
236  virtual void observe(
240  {
241  switch(actLoc) {
243  {
244  testBEGIN_STEP = true;
245  auto x = sh->getCurrentState()->getX();
246  testCurrentValue = get_ele(*(x), 0);
247  break;
248  }
250  {
251  testBEFORE_SOLVE = true;
252  testDt = sh->getWorkingState()->getTimeStep();
253  break;
254  }
256  {
257  testAFTER_SOLVE = true;
258  testName = stepper->getStepperType();
259  break;
260  }
262  {
263  testEND_STEP = true;
264  auto x = sh->getWorkingState()->getX();
265  testWorkingValue = get_ele(*(x), 0);
266  break;
267  }
268  default:
269  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
270  "Error - unknown action location.\n");
271  }
272  }
273 
274  bool testBEGIN_STEP;
275  bool testBEFORE_SOLVE;
276  bool testAFTER_SOLVE;
277  bool testEND_STEP;
278  double testCurrentValue;
279  double testWorkingValue;
280  double testDt;
281  std::string testName;
282 };
283 
284 TEUCHOS_UNIT_TEST(HHTAlpha, AppAction_Observer)
285 {
288 
289  // Setup Stepper for field solve ----------------------------
290  auto stepper = rcp(new Tempus::StepperHHTAlpha<double>());
291  stepper->setModel(model);
292  auto observer = rcp(new StepperHHTAlphaObserverTest());
293  stepper->setAppAction(observer);
294  stepper->initialize();
295 
296  // Setup a SolutionHistory.
297  auto solutionHistory = Tempus::createSolutionHistoryME(model);
298 
299  // Take one time step.
300  stepper->setInitialConditions(solutionHistory);
301  solutionHistory->initWorkingState();
302  double dt = 0.1;
303  solutionHistory->getWorkingState()->setTimeStep(dt);
304  stepper->takeStep(solutionHistory);
305 
306  // Testing that each ACTION_LOCATION has been called.
307  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
308  TEST_COMPARE(observer->testBEFORE_SOLVE, ==, true);
309  TEST_COMPARE(observer->testAFTER_SOLVE, ==, true);
310  TEST_COMPARE(observer->testEND_STEP, ==, true);
311  // Testing that values can be observed through the observer.
312  auto x = solutionHistory->getCurrentState()->getX();
313  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
314  x = solutionHistory->getWorkingState()->getX();
315  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
316  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-14);
317 
318  TEST_COMPARE(observer->testName, ==, "HHT-Alpha");
319 }
320 
321 // ************************************************************
322 // ************************************************************
323 class StepperHHTAlphaModifierXTest
324  : virtual public Tempus::StepperHHTAlphaModifierXBase<double>
325 {
326 public:
327 
329  StepperHHTAlphaModifierXTest()
330  : testX_BEGIN_STEP(false), testX_BEFORE_SOLVE(false),
331  testX_AFTER_SOLVE(false), testX_END_STEP(false),
332  testXbegin(-0.99), testXend(-0.99),
333  testDt(-1.5), testTime(-1.5)
334  {}
335 
337  virtual ~StepperHHTAlphaModifierXTest(){}
338 
340  virtual void modify(
342  const double time, const double dt,
344  {
345  switch(modType) {
347  {
348  testX_BEGIN_STEP = true;
349  testXbegin = get_ele(*(x), 0);
350  break;
351  }
353  {
354  testX_BEFORE_SOLVE = true;
355  testDt = dt;
356  break;
357  }
359  {
360  testX_AFTER_SOLVE = true;
361  testTime = time;
362  break;
363  }
365  {
366  testX_END_STEP = true;
367  testXend = get_ele(*(x), 0);
368  break;
369  }
370  default:
371  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
372  "Error - unknown action location.\n");
373  }
374  }
375 
376  bool testX_BEGIN_STEP;
377  bool testX_BEFORE_SOLVE;
378  bool testX_AFTER_SOLVE;
379  bool testX_END_STEP;
380  double testXbegin;
381  double testXend;
382  double testDt;
383  double testTime;
384 };
385 
386 TEUCHOS_UNIT_TEST(HHTAlpha, AppAction_ModifierX)
387 {
390 
391  // Setup Stepper for field solve ----------------------------
392  auto stepper = rcp(new Tempus::StepperHHTAlpha<double>());
393  stepper->setModel(model);
394  auto modifierX = rcp(new StepperHHTAlphaModifierXTest());
395  stepper->setAppAction(modifierX);
396  stepper->initialize();
397 
398  // Setup a SolutionHistory.
399  auto solutionHistory = Tempus::createSolutionHistoryME(model);
400  // Take one time step.
401  stepper->setInitialConditions(solutionHistory);
402  solutionHistory->initWorkingState();
403  double dt = 0.1;
404  solutionHistory->getWorkingState()->setTimeStep(dt);
405  stepper->takeStep(solutionHistory);
406 
407  // Testing that each ACTION_LOCATION has been called.
408  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
409  TEST_COMPARE(modifierX->testX_BEFORE_SOLVE, ==, true);
410  TEST_COMPARE(modifierX->testX_AFTER_SOLVE, ==, true);
411  TEST_COMPARE(modifierX->testX_END_STEP, ==, true);
412 
413  // Testing that values can be set through the Modifier.
414  auto xbegin = solutionHistory->getCurrentState()->getX();
415  TEST_FLOATING_EQUALITY(modifierX->testXbegin, get_ele(*(xbegin), 0), 1.0e-14);
416  auto xend = solutionHistory->getWorkingState()->getX();
417  TEST_FLOATING_EQUALITY(modifierX->testXend, get_ele(*(xend), 0),1.0e-14);
418  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
419  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
420  auto time = solutionHistory->getWorkingState()->getTime();
421  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-14);
422 }
423 
424 } // namespace Tempus_Test
MODIFIER_TYPE
Indicates the location of application action (see algorithm).
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)
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.
Consider the ODE: where is a constant, is a constant damping parameter, is a constant forcing par...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
ACTION_LOCATION
Indicates the location of application action (see algorithm).
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)