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