Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_UnitTest_Trapezoidal.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_StepperTrapezoidal.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(Trapezoidal, Default_Construction)
33{
34 auto model = rcp(new Tempus_Test::SinCosModel<double>());
35
36 // Default construction.
37 auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
38 stepper->setModel(model);
39 stepper->initialize();
40 TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
41
42
43 // Default values for construction.
46 auto solver = rcp(new Thyra::NOXNonlinearSolver());
47 solver->setParameterList(Tempus::defaultSolverParameters());
48
49 bool useFSAL = stepper->getUseFSAL();
50 std::string ICConsistency = stepper->getICConsistency();
51 bool ICConsistencyCheck = stepper->getICConsistencyCheck();
52 bool zeroInitialGuess = stepper->getZeroInitialGuess();
53
54 // Test the set functions.
55 stepper->setAppAction(modifier); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
56 stepper->setAppAction(modifierX); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
57 stepper->setSolver(solver); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
58 stepper->setUseFSAL(useFSAL); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
59 stepper->setICConsistency(ICConsistency); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
60 stepper->setICConsistencyCheck(ICConsistencyCheck); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
61 stepper->setZeroInitialGuess(zeroInitialGuess); stepper->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
62
63// Full argument list construction.
65model, solver, useFSAL,
66 ICConsistency, ICConsistencyCheck, zeroInitialGuess, modifier));
67TEUCHOS_TEST_FOR_EXCEPT(!stepper->isInitialized());
68
69 // Test stepper properties.
70 TEUCHOS_ASSERT(stepper->getOrder() == 2);
71}
72
73
74// ************************************************************
75// ************************************************************
76TEUCHOS_UNIT_TEST(Trapezoidal, StepperFactory_Construction)
77{
78 auto model = rcp(new Tempus_Test::SinCosModel<double>());
79 testFactoryConstruction("Trapezoidal Method", model);
80}
81
82// ************************************************************
83// ************************************************************
84class StepperTrapezoidalModifierTest
85 : virtual public Tempus::StepperTrapezoidalModifierBase<double>
86{
87public:
89 StepperTrapezoidalModifierTest()
90 : testBEGIN_STEP(false), testBEFORE_SOLVE(false),
91 testAFTER_SOLVE(false), testEND_STEP(false),
92 testCurrentValue(-0.99), testWorkingValue(-0.99),
93 testDt(-1.5), testName("")
94 {}
95
97 virtual ~StepperTrapezoidalModifierTest(){}
98
100 virtual void modify(
101 Teuchos::RCP<Tempus::SolutionHistory<double> > sh,
102 Teuchos::RCP<Tempus::StepperTrapezoidal<double> > stepper,
104 {
105 switch(actLoc) {
106 case StepperTrapezoidalAppAction<double>::BEGIN_STEP:
107 {
108 testBEGIN_STEP = true;
109 auto x = sh->getCurrentState()->getX();
110 testCurrentValue = get_ele(*(x), 0);
111 break;
112 }
113 case StepperTrapezoidalAppAction<double>::BEFORE_SOLVE:
114 {
115 testBEFORE_SOLVE = true;
116 testDt = sh->getWorkingState()->getTimeStep()/10.0;
117 sh->getWorkingState()->setTimeStep(testDt);
118 break;
119 }
120 case StepperTrapezoidalAppAction<double>::AFTER_SOLVE:
121 {
122 testAFTER_SOLVE = true;
123 testName = "Trapezoidal - Modifier";
124 stepper->setStepperName(testName);
125 break;
126 }
127 case StepperTrapezoidalAppAction<double>::END_STEP:
128 {
129 testEND_STEP = true;
130 auto x = sh->getWorkingState()->getX();
131 testWorkingValue = get_ele(*(x), 0);
132 break;
133 }
134 default:
135 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
136 "Error - unknown action location.\n");
137 }
138 }
139
140 bool testBEGIN_STEP;
141 bool testBEFORE_SOLVE;
142 bool testAFTER_SOLVE;
143 bool testEND_STEP;
144 double testCurrentValue;
145 double testWorkingValue;
146 double testDt;
147 std::string testName;
148};
149
150TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Modifier)
151{
152 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
153 model = rcp(new Tempus_Test::SinCosModel<double>());
154
155 // Setup Stepper for field solve ----------------------------
156 auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
157 stepper->setModel(model);
158 auto modifier = rcp(new StepperTrapezoidalModifierTest());
159 stepper->setAppAction(modifier);
160 stepper->initialize();
161
162 // Create a SolutionHistory.
163 auto solutionHistory = Tempus::createSolutionHistoryME(model);
164
165 // Take one time step.
166 stepper->setInitialConditions(solutionHistory);
167 solutionHistory->initWorkingState();
168 double dt = 0.1;
169 solutionHistory->getWorkingState()->setTimeStep(dt);
170 stepper->takeStep(solutionHistory);
171
172 // Testing that each ACTION_LOCATION has been called.
173 TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
174 TEST_COMPARE(modifier->testBEFORE_SOLVE, ==, true);
175 TEST_COMPARE(modifier->testAFTER_SOLVE, ==, true);
176 TEST_COMPARE(modifier->testEND_STEP, ==, true);
177
178 // Testing that values can be set through the Modifier.
179 auto x = solutionHistory->getCurrentState()->getX();
180 TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
181 x = solutionHistory->getWorkingState()->getX();
182 TEST_FLOATING_EQUALITY(modifier->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
183 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
184 TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
185 TEST_COMPARE(modifier->testName, ==, "Trapezoidal - Modifier");
186}
187
188// ************************************************************
189// ************************************************************
190class StepperTrapezoidalObserverTest
191 : virtual public Tempus::StepperTrapezoidalObserverBase<double>
192{
193public:
194
196 StepperTrapezoidalObserverTest()
197 : testBEGIN_STEP(false), testBEFORE_SOLVE(false),
198 testAFTER_SOLVE(false), testEND_STEP(false),
199 testCurrentValue(-0.99), testWorkingValue(-0.99),
200 testDt(-1.5), testName("")
201 {}
202
204 virtual ~StepperTrapezoidalObserverTest(){}
205
207 virtual void observe(
208 Teuchos::RCP<const Tempus::SolutionHistory<double> > sh,
209 Teuchos::RCP<const Tempus::StepperTrapezoidal<double> > stepper,
211 {
212 switch(actLoc) {
213 case StepperTrapezoidalAppAction<double>::BEGIN_STEP:
214 {
215 testBEGIN_STEP = true;
216 auto x = sh->getCurrentState()->getX();
217 testCurrentValue = get_ele(*(x), 0);
218 break;
219 }
220 case StepperTrapezoidalAppAction<double>::BEFORE_SOLVE:
221 {
222 testBEFORE_SOLVE = true;
223 testDt = sh->getWorkingState()->getTimeStep();
224 break;
225 }
226 case StepperTrapezoidalAppAction<double>::AFTER_SOLVE:
227 {
228 testAFTER_SOLVE = true;
229 testName = stepper->getStepperType();
230 break;
231 }
232 case StepperTrapezoidalAppAction<double>::END_STEP:
233 {
234 testEND_STEP = true;
235 auto x = sh->getWorkingState()->getX();
236 testWorkingValue = get_ele(*(x), 0);
237 break;
238 }
239 default:
240 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
241 "Error - unknown action location.\n");
242 }
243 }
244 bool testBEGIN_STEP;
245 bool testBEFORE_SOLVE;
246 bool testAFTER_SOLVE;
247 bool testEND_STEP;
248 double testCurrentValue;
249 double testWorkingValue;
250 double testDt;
251 std::string testName;
252};
253
254TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_Observer)
255{
256 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
257 model = rcp(new Tempus_Test::SinCosModel<double>());
258
259 // Setup Stepper for field solve ----------------------------
260 auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
261 stepper->setModel(model);
262 auto observer = rcp(new StepperTrapezoidalObserverTest());
263 stepper->setAppAction(observer);
264 stepper->initialize();
265
266 // Setup a SolutionHistory.
267 auto solutionHistory = Tempus::createSolutionHistoryME(model);
268
269 // Take one time step.
270 stepper->setInitialConditions(solutionHistory);
271 solutionHistory->initWorkingState();
272 double dt = 0.1;
273 solutionHistory->getWorkingState()->setTimeStep(dt);
274 stepper->takeStep(solutionHistory);
275
276 // Testing that each ACTION_LOCATION has been called.
277 TEST_COMPARE(observer->testBEGIN_STEP, ==, true);
278 TEST_COMPARE(observer->testBEFORE_SOLVE, ==, true);
279 TEST_COMPARE(observer->testAFTER_SOLVE, ==, true);
280 TEST_COMPARE(observer->testEND_STEP, ==, true);
281
282 // Testing that values can be observed through the observer.
283 auto x = solutionHistory->getCurrentState()->getX();
284 TEST_FLOATING_EQUALITY(observer->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
285 x = solutionHistory->getWorkingState()->getX();
286 TEST_FLOATING_EQUALITY(observer->testWorkingValue, get_ele(*(x), 0), 1.0e-14);
287 TEST_FLOATING_EQUALITY(observer->testDt, dt, 1.0e-14);
288 TEST_COMPARE(observer->testName, ==, "Trapezoidal Method");
289}
290
291// ************************************************************
292// ************************************************************
293class StepperTrapezoidalModifierXTest
294 : virtual public Tempus::StepperTrapezoidalModifierXBase<double>
295{
296public:
297
299 StepperTrapezoidalModifierXTest()
300 : testX_BEGIN_STEP(false), testX_BEFORE_SOLVE(false),
301 testX_AFTER_SOLVE(false), testXDOT_END_STEP(false),
302 testX(-0.99), testXDot(-0.99),
303 testDt(-1.5), testTime(-1.5)
304 {}
305
307 virtual ~StepperTrapezoidalModifierXTest(){}
309 virtual void modify(
310 Teuchos::RCP<Thyra::VectorBase<double> > x,
311 const double time, const double dt,
313 {
314 switch(modType) {
315 case StepperTrapezoidalModifierXBase<double>::X_BEGIN_STEP:
316 {
317 testX_BEGIN_STEP = true;
318 testX = get_ele(*(x), 0);
319 break;
320 }
321 case StepperTrapezoidalModifierXBase<double>::X_BEFORE_SOLVE:
322 {
323 testX_BEFORE_SOLVE = true;
324 testDt = dt;
325 break;
326 }
327 case StepperTrapezoidalModifierXBase<double>::X_AFTER_SOLVE:
328 {
329 testX_AFTER_SOLVE = true;
330 testTime = time;
331 break;
332 }
333 case StepperTrapezoidalModifierXBase<double>::XDOT_END_STEP:
334 {
335 testXDOT_END_STEP = true;
336 testXDot = get_ele(*(x), 0);
337 break;
338 }
339 default:
340 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
341 "Error - unknown action location.\n");
342 }
343 }
344 bool testX_BEGIN_STEP;
345 bool testX_BEFORE_SOLVE;
346 bool testX_AFTER_SOLVE;
347 bool testXDOT_END_STEP;
348 double testX;
349 double testXDot;
350 double testDt;
351 double testTime;
352};
353
354TEUCHOS_UNIT_TEST(Trapezoidal, AppAction_ModifierX)
355{
356 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
357 model = rcp(new Tempus_Test::SinCosModel<double>());
358
359 // Setup Stepper for field solve ----------------------------
360 auto stepper = rcp(new Tempus::StepperTrapezoidal<double>());
361 stepper->setModel(model);
362 auto modifierX = rcp(new StepperTrapezoidalModifierXTest());
363 stepper->setAppAction(modifierX);
364 stepper->initialize();
365
366 // Setup a SolutionHistory.
367 auto solutionHistory = Tempus::createSolutionHistoryME(model);
368
369 // Take one time step.
370 stepper->setInitialConditions(solutionHistory);
371 solutionHistory->initWorkingState();
372 double dt = 0.1;
373 solutionHistory->getWorkingState()->setTimeStep(dt);
374 stepper->takeStep(solutionHistory);
375
376 // Testing that each ACTION_LOCATION has been called.
377 TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
378 TEST_COMPARE(modifierX->testX_BEFORE_SOLVE, ==, true);
379 TEST_COMPARE(modifierX->testX_AFTER_SOLVE, ==, true);
380 TEST_COMPARE(modifierX->testXDOT_END_STEP, ==, true);
381
382 // Testing that values can be set through the Modifier.
383 auto x = solutionHistory->getCurrentState()->getX();
384 TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
385 // Temporary memory for xDot is not guarranteed to exist outside the Stepper.
386 auto xDot = solutionHistory->getWorkingState()->getXDot();
387 if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
388
389 TEST_FLOATING_EQUALITY(modifierX->testXDot, get_ele(*(xDot), 0),1.0e-14);
390 auto Dt = solutionHistory->getWorkingState()->getTimeStep();
391 TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
392 auto time = solutionHistory->getWorkingState()->getTime();
393 TEST_FLOATING_EQUALITY(modifierX->testTime, time, 1.0e-14);
394 }
395
396} // namespace Tempus_Test
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).
Trapezoidal method time stepper.
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.
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryME(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Nonmember contructor from a Thyra ModelEvaluator.
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.