Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_UnitTest_Leapfrog.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_StepperLeapfrog.hpp"
16
17#include "../TestModels/HarmonicOscillatorModel.hpp"
18
19
20namespace Tempus_Unit_Test {
21
22using Teuchos::RCP;
23using Teuchos::rcp;
24using Teuchos::rcp_const_cast;
25using Teuchos::rcp_dynamic_cast;
26using Teuchos::ParameterList;
27using Teuchos::sublist;
28
29
30// ************************************************************
31// ************************************************************
32TEUCHOS_UNIT_TEST(Leapfrog, Default_Construction)
33{
34 auto model = rcp(new Tempus_Test::HarmonicOscillatorModel<double>());
35
36 // Default construction.
37 auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
38 stepper->setModel(model);
39 stepper->initialize();
40 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
41
42
43 // Default values for construction.
44 auto modifier = rcp(new Tempus::StepperLeapfrogModifierDefault<double>());
45 stepper->setAppAction(modifier);
46 bool useFSAL = stepper->getUseFSAL();
47 std::string ICConsistency = stepper->getICConsistency();
48 bool ICConsistencyCheck = stepper->getICConsistencyCheck();
49
50
51 // Test the set functions.
52 stepper->setAppAction(modifier); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
53 stepper->setUseFSAL(useFSAL); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
54 stepper->setICConsistency(ICConsistency); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
55 stepper->setICConsistencyCheck(ICConsistencyCheck); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
56
57 // Full argument list construction.
58 stepper = rcp(new Tempus::StepperLeapfrog<double>(
59 model, useFSAL, ICConsistency, ICConsistencyCheck,modifier));
60 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
61
62 // Test stepper properties.
63 TEUCHOS_ASSERT(stepper->getOrder() == 2);
64}
65
66
67// ************************************************************
68// ************************************************************
69TEUCHOS_UNIT_TEST(Leapfrog, StepperFactory_Construction)
70{
71 auto model = rcp(new Tempus_Test::HarmonicOscillatorModel<double>());
72 testFactoryConstruction("Leapfrog", model);
73}
74
75// ************************************************************
76// ************************************************************
77class StepperLeapfrogModifierTest
78 : virtual public Tempus::StepperLeapfrogModifierBase<double>
79{
80public:
81
83 StepperLeapfrogModifierTest()
84 : testBEGIN_STEP(false), testBEFORE_X_UPDATE(false),
85 testBEFORE_EXPLICIT_EVAL(false), testBEFORE_XDOT_UPDATE(false),
86 testEND_STEP(false),
87 testCurrentValue(-0.99), testWorkingValue(-0.99),
88 testDt(-1.5), testName("")
89 {}
90
92 virtual ~StepperLeapfrogModifierTest(){}
93
95 virtual void modify(
96 Teuchos::RCP<Tempus::SolutionHistory<double> > sh,
97 Teuchos::RCP<Tempus::StepperLeapfrog<double> > stepper,
99 {
100 switch(actLoc) {
101 case StepperLeapfrogAppAction<double>::BEGIN_STEP:
102 {
103 testBEGIN_STEP = true;
104 auto x = sh->getCurrentState()->getX();
105 testCurrentValue = get_ele(*(x), 0);
106 break;
107 }
108 case StepperLeapfrogAppAction<double>::BEFORE_EXPLICIT_EVAL:
109 {
110 testBEFORE_EXPLICIT_EVAL = true;
111 testDt = sh->getWorkingState()->getTimeStep()/10.0;
112 sh->getWorkingState()->setTimeStep(testDt);
113 break;
114 }
115 case StepperLeapfrogAppAction<double>::BEFORE_X_UPDATE:
116 {
117 testBEFORE_X_UPDATE = true;
118 testName = "Leapfrog - Modifier";
119 stepper->setStepperName(testName);
120 break;
121 }
122 case StepperLeapfrogAppAction<double>::BEFORE_XDOT_UPDATE:
123 {
124 testBEFORE_XDOT_UPDATE = true;
125 auto x = sh->getWorkingState()->getX();
126 testWorkingValue = get_ele(*(x), 0);
127 break;
128 }
129 case StepperLeapfrogAppAction<double>::END_STEP:
130 {
131 testEND_STEP = true;
132 break;
133 }
134 default:
135 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
136 "Error - unknown action location.\n");
137 }
138 }
139 bool testBEGIN_STEP;
140 bool testBEFORE_X_UPDATE;
141 bool testBEFORE_EXPLICIT_EVAL;
142 bool testBEFORE_XDOT_UPDATE;
143 bool testEND_STEP;
144 double testCurrentValue;
145 double testWorkingValue;
146 double testDt;
147 std::string testName;
148};
149
150TEUCHOS_UNIT_TEST(Leapfrog, AppAction_Modifier)
151{
152 auto model = rcp(new Tempus_Test::HarmonicOscillatorModel<double>());
153
154 // Setup Stepper for field solve ----------------------------
155 auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
156 stepper->setModel(model);
157 auto modifier = rcp(new StepperLeapfrogModifierTest());
158 stepper->setAppAction(modifier);
159 stepper->initialize();
160
161 // Setup TimeStepControl ------------------------------------
162 auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
163 timeStepControl->setInitTimeStep(15.0);
164 timeStepControl->initialize();
165
166 // Setup initial condition SolutionState --------------------
167 auto inArgsIC = model->getNominalValues();
168 auto icX = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
169 auto icXDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
170 auto icXDotDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot_dot());
171 auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
172 icState->setTime (timeStepControl->getInitTime());
173 icState->setIndex (timeStepControl->getInitIndex());
174 icState->setTimeStep(15.0);
175 icState->setOrder (stepper->getOrder());
176 icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
177
178 // Setup SolutionHistory ------------------------------------
179 auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
180 solutionHistory->setName("Forward States");
181 solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
182 solutionHistory->setStorageLimit(2);
183 solutionHistory->addState(icState);
184
185 // Take one time step.
186 stepper->setInitialConditions(solutionHistory);
187 solutionHistory->initWorkingState();
188 solutionHistory->getWorkingState()->setTimeStep(15.0);
189 stepper->takeStep(solutionHistory);
190
191 TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
192 TEST_COMPARE(modifier->testBEFORE_EXPLICIT_EVAL, ==, true);
193 TEST_COMPARE(modifier->testBEFORE_X_UPDATE, ==, true);
194 TEST_COMPARE(modifier->testBEFORE_XDOT_UPDATE, ==, true);
195 TEST_COMPARE(modifier->testEND_STEP, ==, true);
196
197 auto x = solutionHistory->getCurrentState()->getX();
198 TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-15);
199 x = solutionHistory->getWorkingState()->getX();
200 TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-15);
201 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
202 TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-15);
203
204 TEST_COMPARE(modifier->testName, ==, "Leapfrog - Modifier");
205}
206// ************************************************************
207// ************************************************************
208class StepperLeapfrogModifierXTest
209 : virtual public Tempus::StepperLeapfrogModifierXBase<double>
210{
211public:
212
214 StepperLeapfrogModifierXTest()
215 : testX_BEGIN_STEP(false), testX_BEFORE_EXPLICIT_EVAL(false),
216 testX_BEFORE_X_UPDATE(false), testX_BEFORE_XDOT_UPDATE(false),
217 testX_END_STEP(false),
218 testX(0.0), testDt(-1.25), testTime(-1.25),testName("")
219 {}
220
222 virtual ~StepperLeapfrogModifierXTest(){}
223
225 virtual void modify(
226 Teuchos::RCP<Thyra::VectorBase<double> > x,
227 const double time, const double dt,
229 {
230 switch(modType) {
231 case StepperLeapfrogModifierXBase<double>::X_BEGIN_STEP:
232 {
233 testX_BEGIN_STEP = true;
234 testX = get_ele(*(x), 0);
235 break;
236 }
237 case StepperLeapfrogModifierXBase<double>::X_BEFORE_EXPLICIT_EVAL:
238 {
239 testX_BEFORE_EXPLICIT_EVAL = true;
240 testDt = dt;
241 break;
242 }
243 case StepperLeapfrogModifierXBase<double>::X_BEFORE_X_UPDATE:
244 {
245 testX_BEFORE_X_UPDATE = true;
246 testTime = time;
247 break;
248 }
249 case StepperLeapfrogModifierXBase<double>::X_BEFORE_XDOT_UPDATE:
250 {
251 testX_BEFORE_XDOT_UPDATE = true;
252 testName = "Leapfrog - ModifierX";
253 break;
254 }
255 case StepperLeapfrogModifierXBase<double>::X_END_STEP:
256 {
257 testX_END_STEP = true;
258 break;
259 }
260 default:
261 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
262 "Error - unknown action location.\n");
263 }
264 }
265 bool testX_BEGIN_STEP;
266 bool testX_BEFORE_EXPLICIT_EVAL;
267 bool testX_BEFORE_X_UPDATE;
268 bool testX_BEFORE_XDOT_UPDATE;
269 bool testX_END_STEP;
270 double testX;
271 double testDt;
272 double testTime;
273 std::string testName;
274};
275
276TEUCHOS_UNIT_TEST(LeapFrog, AppAction_ModifierX)
277{
278 auto model = rcp(new Tempus_Test::HarmonicOscillatorModel<double>());
279
280 // Setup Stepper for field solve ----------------------------
281 auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
282 stepper->setModel(model);
283 auto modifierX = rcp(new StepperLeapfrogModifierXTest());
284 stepper->setAppAction(modifierX);
285 stepper->initialize();
286
287 // Setup TimeStepControl ------------------------------------
288 auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
289 timeStepControl->setInitTimeStep(15.0);
290 timeStepControl->initialize();
291
292 // Setup initial condition SolutionState --------------------
293 auto inArgsIC = model->getNominalValues();
294 auto icX = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
295 auto icXDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
296 auto icXDotDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot_dot());
297 auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
298 icState->setTime (timeStepControl->getInitTime());
299 icState->setIndex (timeStepControl->getInitIndex());
300 icState->setTimeStep(15.0);
301 icState->setOrder (stepper->getOrder());
302 icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
303
304 // Setup SolutionHistory ------------------------------------
305 auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
306 solutionHistory->setName("Forward States");
307 solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
308 solutionHistory->setStorageLimit(2);
309 solutionHistory->addState(icState);
310
311 // Take one time step.
312 stepper->setInitialConditions(solutionHistory);
313 solutionHistory->initWorkingState();
314 solutionHistory->getWorkingState()->setTimeStep(15.0);
315 stepper->takeStep(solutionHistory);
316
317 TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
318 TEST_COMPARE(modifierX->testX_BEFORE_EXPLICIT_EVAL, ==, true);
319 TEST_COMPARE(modifierX->testX_BEFORE_XDOT_UPDATE, ==, true);
320 TEST_COMPARE(modifierX->testX_BEFORE_X_UPDATE, ==, true);
321 TEST_COMPARE(modifierX->testX_END_STEP, ==, true);
322
323 auto x = solutionHistory->getCurrentState()->getX();
324 TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-15);
325 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
326 TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-15);
327 auto time = solutionHistory->getWorkingState()->getTime();
328 TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-15);
329 TEST_COMPARE(modifierX->testName, ==, "Leapfrog - ModifierX");
330
331}
332
333// ************************************************************
334// ************************************************************
335class StepperLeapfrogObserverTest
336 : virtual public Tempus::StepperLeapfrogObserverBase<double>
337{
338public:
340 StepperLeapfrogObserverTest()
341 : testBEGIN_STEP(false), testBEFORE_EXPLICIT_EVAL(false),
342 testBEFORE_X_UPDATE(false), testBEFORE_XDOT_UPDATE(false),
343 testEND_STEP(false),
344 testCurrentValue(-0.99), testWorkingValue(-0.99),
345 testDt(-1.5), testName("")
346 {}
348 virtual ~StepperLeapfrogObserverTest(){}
349
351 virtual void observe(
352 Teuchos::RCP<const Tempus::SolutionHistory<double> > sh,
353 Teuchos::RCP<const Tempus::StepperLeapfrog<double> > stepper,
355 {
356 switch(actLoc) {
357 case StepperLeapfrogAppAction<double>::BEGIN_STEP:
358 {
359 testBEGIN_STEP = true;
360 auto x = sh->getCurrentState()->getX();
361 testCurrentValue = get_ele(*(x), 0);
362 break;
363 }
364 case StepperLeapfrogAppAction<double>::BEFORE_EXPLICIT_EVAL:
365 {
366 testBEFORE_EXPLICIT_EVAL = true;
367 testDt = sh->getWorkingState()->getTimeStep();
368 break;
369 }
370 case StepperLeapfrogAppAction<double>::BEFORE_X_UPDATE:
371 {
372 testBEFORE_X_UPDATE = true;
373 testName = stepper->getStepperType();
374 break;
375 }
376 case StepperLeapfrogAppAction<double>::BEFORE_XDOT_UPDATE:
377 {
378 testBEFORE_XDOT_UPDATE = true;
379 auto x = sh->getWorkingState()->getX();
380 testWorkingValue = get_ele(*(x), 0);
381 break;
382 }
383 case StepperLeapfrogAppAction<double>::END_STEP:
384 {
385 testEND_STEP = true;
386 break;
387 }
388 default:
389 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
390 "Error - unknown action location.\n");
391 }
392 }
393 bool testBEGIN_STEP;
394 bool testBEFORE_EXPLICIT_EVAL;
395 bool testBEFORE_X_UPDATE;
396 bool testBEFORE_XDOT_UPDATE;
397 bool testEND_STEP;
398 double testCurrentValue;
399 double testWorkingValue;
400 double testDt;
401 std::string testName;
402};
403
404TEUCHOS_UNIT_TEST(Leapfrog, AppAction_Observer)
405{
406 auto model = rcp(new Tempus_Test::HarmonicOscillatorModel<double>());
407
408 // Setup Stepper for field solve ----------------------------
409 auto stepper = rcp(new Tempus::StepperLeapfrog<double>());
410 stepper->setModel(model);
411 auto observer = rcp(new StepperLeapfrogObserverTest());
412 stepper->setAppAction(observer);
413 stepper->initialize();
414
415 // Setup TimeStepControl ------------------------------------
416 auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
417 double dt = 0.173;
418 timeStepControl->setInitTimeStep(dt);
419 timeStepControl->initialize();
420
421 // Setup initial condition SolutionState --------------------
422 auto inArgsIC = model->getNominalValues();
423 auto icX = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
424 auto icXDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot());
425 auto icXDotDot = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x_dot_dot());
426 auto icState = Tempus::createSolutionStateX<double>(icX, icXDot, icXDotDot);
427 icState->setTime (timeStepControl->getInitTime());
428 icState->setIndex (timeStepControl->getInitIndex());
429 icState->setTimeStep(dt);
430 icState->setOrder (stepper->getOrder());
431 icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
432
433 // Setup SolutionHistory ------------------------------------
434 auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
435 solutionHistory->setName("Forward States");
436 solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
437 solutionHistory->setStorageLimit(2);
438 solutionHistory->addState(icState);
439
440 // Take one time step.
441 stepper->setInitialConditions(solutionHistory);
442 solutionHistory->initWorkingState();
443 solutionHistory->getWorkingState()->setTimeStep(dt);
444 stepper->takeStep(solutionHistory);
445
446 // Testing that values can be observed through the observer.
447 TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
448 TEST_COMPARE(observer->testBEFORE_EXPLICIT_EVAL, ==, true);
449 TEST_COMPARE(observer->testBEFORE_X_UPDATE, ==, true);
450 TEST_COMPARE(observer->testBEFORE_XDOT_UPDATE, ==, true);
451 TEST_COMPARE(observer->testEND_STEP, ==, true);
452
453 auto x = solutionHistory->getCurrentState()->getX();
454 TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-15);
455 x = solutionHistory->getWorkingState()->getX();
456 TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-15);
457 TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-15);
458
459 TEST_COMPARE(observer->testName, ==, "Leapfrog");
460}
461
462
463} // namespace Tempus_Test
464
465
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
ACTION_LOCATION
Indicates the location of application action (see algorithm).
MODIFIER_TYPE
Indicates the location of application action (see algorithm).
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
@ STORAGE_TYPE_STATIC
Keep a fix number of states.