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: 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_StepperHHTAlpha.hpp"
19 
20 #include "../TestModels/HarmonicOscillatorModel.hpp"
21 
22 namespace Tempus_Unit_Test {
23 
25 using Teuchos::RCP;
26 using Teuchos::rcp;
27 using Teuchos::rcp_const_cast;
28 using Teuchos::rcp_dynamic_cast;
29 using Teuchos::sublist;
30 
31 // ************************************************************
32 // ************************************************************
33 TEUCHOS_UNIT_TEST(HHTAlpha, Default_Construction)
34 {
36 
37  // Default construction.
38  auto stepper = rcp(new Tempus::StepperHHTAlpha<double>());
39  stepper->setModel(model);
40  stepper->initialize();
41  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
42 
43  // Default values for construction.
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  std::string schemeName = "Newmark Beta User Defined";
52  double beta = 0.25;
53  double gamma = 0.5;
54  double alpha_f = 0.0;
55  double alpha_m = 0.0;
56 
60 
61  // Test the set functions.
62  stepper->setSolver(solver);
63  stepper->initialize();
64  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
65  stepper->setUseFSAL(useFSAL);
66  stepper->initialize();
67  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
68  stepper->setICConsistency(ICConsistency);
69  stepper->initialize();
70  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
71  stepper->setICConsistencyCheck(ICConsistencyCheck);
72  stepper->initialize();
73  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
74  stepper->setZeroInitialGuess(zeroInitialGuess);
75  stepper->initialize();
76  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
77 
78  stepper->setAppAction(modifier);
79  stepper->initialize();
80  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
81  stepper->setAppAction(modifierX);
82  stepper->initialize();
83  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
84  stepper->setAppAction(observer);
85  stepper->initialize();
86  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
87 
88  stepper->setSchemeName(schemeName);
89  stepper->initialize();
90  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
91  stepper->setBeta(beta);
92  stepper->initialize();
93  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
94  stepper->setGamma(gamma);
95  stepper->initialize();
96  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
97  stepper->setAlphaF(alpha_f);
98  stepper->initialize();
99  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
100  stepper->setAlphaM(alpha_m);
101  stepper->initialize();
102  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
103 
104  // Full argument list construction.
105  stepper = rcp(new Tempus::StepperHHTAlpha<double>(
106  model, solver, useFSAL, ICConsistency, ICConsistencyCheck,
107  zeroInitialGuess, schemeName, beta, gamma, alpha_f, alpha_m, modifier));
108  TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
109 
110  // Test stepper properties.
111  TEUCHOS_ASSERT(stepper->getOrder() == 2);
112 }
113 
114 // ************************************************************
115 // ************************************************************
116 TEUCHOS_UNIT_TEST(HHTAlpha, StepperFactory_Construction)
117 {
119  testFactoryConstruction("HHT-Alpha", model);
120 }
121 
122 // ************************************************************
123 // ************************************************************
124 class StepperHHTAlphaModifierTest
125  : virtual public Tempus::StepperHHTAlphaModifierBase<double> {
126  public:
128  StepperHHTAlphaModifierTest()
129  : testBEGIN_STEP(false),
130  testBEFORE_SOLVE(false),
131  testAFTER_SOLVE(false),
132  testEND_STEP(false),
133  testCurrentValue(-0.99),
134  testWorkingValue(-0.99),
135  testDt(-1.5),
136  testName("")
137  {
138  }
139 
141  virtual ~StepperHHTAlphaModifierTest() {}
142 
144  virtual void modify(
148  actLoc)
149  {
150  switch (actLoc) {
152  testBEGIN_STEP = true;
153  auto x = sh->getCurrentState()->getX();
154  testCurrentValue = get_ele(*(x), 0);
155  break;
156  }
158  testBEFORE_SOLVE = true;
159  testDt = sh->getWorkingState()->getTimeStep() / 10.0;
160  sh->getWorkingState()->setTimeStep(testDt);
161  break;
162  }
164  testAFTER_SOLVE = true;
165  testName = "HHT Alpha - Modifier";
166  stepper->setStepperName(testName);
167  break;
168  }
170  testEND_STEP = true;
171  auto x = sh->getWorkingState()->getX();
172  testWorkingValue = get_ele(*(x), 0);
173  break;
174  }
175  default:
176  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
177  "Error - unknown action location.\n");
178  }
179  }
180  bool testBEGIN_STEP;
181  bool testBEFORE_SOLVE;
182  bool testAFTER_SOLVE;
183  bool testEND_STEP;
184  double testCurrentValue;
185  double testWorkingValue;
186  double testDt;
187  std::string testName;
188 };
189 
190 TEUCHOS_UNIT_TEST(HHTAlpha, AppAction_Modifier)
191 {
194 
195  // Setup Stepper for field solve ----------------------------
196  auto stepper = rcp(new Tempus::StepperHHTAlpha<double>());
197  stepper->setModel(model);
198  auto modifier = rcp(new StepperHHTAlphaModifierTest());
199  stepper->setAppAction(modifier);
200  stepper->initialize();
201 
202  // Create a SolutionHistory.
203  auto solutionHistory = Tempus::createSolutionHistoryME(model);
204 
205  // Take one time step.
206  stepper->setInitialConditions(solutionHistory);
207  solutionHistory->initWorkingState();
208  double dt = 0.1;
209  solutionHistory->getWorkingState()->setTimeStep(dt);
210  stepper->takeStep(solutionHistory);
211 
212  // Testing that each ACTION_LOCATION has been called.
213  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
214  TEST_COMPARE(modifier->testBEFORE_SOLVE, ==, true);
215  TEST_COMPARE(modifier->testAFTER_SOLVE, ==, true);
216  TEST_COMPARE(modifier->testEND_STEP, ==, true);
217 
218  // Testing that values can be set through the Modifier.
219  auto x = solutionHistory->getCurrentState()->getX();
220  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
221  x = solutionHistory->getWorkingState()->getX();
222  TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
223  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
224  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
225  TEST_COMPARE(modifier->testName, ==, "HHT Alpha - Modifier");
226 }
227 
228 // ************************************************************
229 // ************************************************************
230 class StepperHHTAlphaObserverTest
231  : virtual public Tempus::StepperHHTAlphaObserverBase<double> {
232  public:
234  StepperHHTAlphaObserverTest()
235  : testBEGIN_STEP(false),
236  testBEFORE_SOLVE(false),
237  testAFTER_SOLVE(false),
238  testEND_STEP(false),
239  testCurrentValue(-0.99),
240  testWorkingValue(-0.99),
241  testDt(-1.5),
242  testName("")
243  {
244  }
245 
247  virtual ~StepperHHTAlphaObserverTest() {}
248 
250  virtual void observe(
254  actLoc)
255  {
256  switch (actLoc) {
258  testBEGIN_STEP = true;
259  auto x = sh->getCurrentState()->getX();
260  testCurrentValue = get_ele(*(x), 0);
261  break;
262  }
264  testBEFORE_SOLVE = true;
265  testDt = sh->getWorkingState()->getTimeStep();
266  break;
267  }
269  testAFTER_SOLVE = true;
270  testName = stepper->getStepperType();
271  break;
272  }
274  testEND_STEP = true;
275  auto x = sh->getWorkingState()->getX();
276  testWorkingValue = get_ele(*(x), 0);
277  break;
278  }
279  default:
280  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
281  "Error - unknown action location.\n");
282  }
283  }
284 
285  bool testBEGIN_STEP;
286  bool testBEFORE_SOLVE;
287  bool testAFTER_SOLVE;
288  bool testEND_STEP;
289  double testCurrentValue;
290  double testWorkingValue;
291  double testDt;
292  std::string testName;
293 };
294 
295 TEUCHOS_UNIT_TEST(HHTAlpha, AppAction_Observer)
296 {
299 
300  // Setup Stepper for field solve ----------------------------
301  auto stepper = rcp(new Tempus::StepperHHTAlpha<double>());
302  stepper->setModel(model);
303  auto observer = rcp(new StepperHHTAlphaObserverTest());
304  stepper->setAppAction(observer);
305  stepper->initialize();
306 
307  // Setup a SolutionHistory.
308  auto solutionHistory = Tempus::createSolutionHistoryME(model);
309 
310  // Take one time step.
311  stepper->setInitialConditions(solutionHistory);
312  solutionHistory->initWorkingState();
313  double dt = 0.1;
314  solutionHistory->getWorkingState()->setTimeStep(dt);
315  stepper->takeStep(solutionHistory);
316 
317  // Testing that each ACTION_LOCATION has been called.
318  TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
319  TEST_COMPARE(observer->testBEFORE_SOLVE, ==, true);
320  TEST_COMPARE(observer->testAFTER_SOLVE, ==, true);
321  TEST_COMPARE(observer->testEND_STEP, ==, true);
322  // Testing that values can be observed through the observer.
323  auto x = solutionHistory->getCurrentState()->getX();
324  TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
325  x = solutionHistory->getWorkingState()->getX();
326  TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
327  TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-14);
328 
329  TEST_COMPARE(observer->testName, ==, "HHT-Alpha");
330 }
331 
332 // ************************************************************
333 // ************************************************************
334 class StepperHHTAlphaModifierXTest
335  : virtual public Tempus::StepperHHTAlphaModifierXBase<double> {
336  public:
338  StepperHHTAlphaModifierXTest()
339  : testX_BEGIN_STEP(false),
340  testX_BEFORE_SOLVE(false),
341  testX_AFTER_SOLVE(false),
342  testX_END_STEP(false),
343  testXbegin(-0.99),
344  testXend(-0.99),
345  testDt(-1.5),
346  testTime(-1.5)
347  {
348  }
349 
351  virtual ~StepperHHTAlphaModifierXTest() {}
352 
354  virtual void modify(
355  Teuchos::RCP<Thyra::VectorBase<double> > x, const double time,
356  const double dt,
358  modType)
359  {
360  switch (modType) {
362  testX_BEGIN_STEP = true;
363  testXbegin = get_ele(*(x), 0);
364  break;
365  }
367  testX_BEFORE_SOLVE = true;
368  testDt = dt;
369  break;
370  }
372  testX_AFTER_SOLVE = true;
373  testTime = time;
374  break;
375  }
377  testX_END_STEP = true;
378  testXend = get_ele(*(x), 0);
379  break;
380  }
381  default:
382  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
383  "Error - unknown action location.\n");
384  }
385  }
386 
387  bool testX_BEGIN_STEP;
388  bool testX_BEFORE_SOLVE;
389  bool testX_AFTER_SOLVE;
390  bool testX_END_STEP;
391  double testXbegin;
392  double testXend;
393  double testDt;
394  double testTime;
395 };
396 
397 TEUCHOS_UNIT_TEST(HHTAlpha, AppAction_ModifierX)
398 {
401 
402  // Setup Stepper for field solve ----------------------------
403  auto stepper = rcp(new Tempus::StepperHHTAlpha<double>());
404  stepper->setModel(model);
405  auto modifierX = rcp(new StepperHHTAlphaModifierXTest());
406  stepper->setAppAction(modifierX);
407  stepper->initialize();
408 
409  // Setup a SolutionHistory.
410  auto solutionHistory = Tempus::createSolutionHistoryME(model);
411  // Take one time step.
412  stepper->setInitialConditions(solutionHistory);
413  solutionHistory->initWorkingState();
414  double dt = 0.1;
415  solutionHistory->getWorkingState()->setTimeStep(dt);
416  stepper->takeStep(solutionHistory);
417 
418  // Testing that each ACTION_LOCATION has been called.
419  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
420  TEST_COMPARE(modifierX->testX_BEFORE_SOLVE, ==, true);
421  TEST_COMPARE(modifierX->testX_AFTER_SOLVE, ==, true);
422  TEST_COMPARE(modifierX->testX_END_STEP, ==, true);
423 
424  // Testing that values can be set through the Modifier.
425  auto xbegin = solutionHistory->getCurrentState()->getX();
426  TEST_FLOATING_EQUALITY(modifierX->testXbegin, get_ele(*(xbegin), 0), 1.0e-14);
427  auto xend = solutionHistory->getWorkingState()->getX();
428  TEST_FLOATING_EQUALITY(modifierX->testXend, get_ele(*(xend), 0), 1.0e-14);
429  auto Dt = solutionHistory->getWorkingState()->getTimeStep();
430  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
431  auto time = solutionHistory->getWorkingState()->getTime();
432  TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-14);
433 }
434 
435 } // namespace Tempus_Unit_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)