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