Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_UnitTest_NewmarkExplicitAForm.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 "Teuchos_XMLParameterListHelpers.hpp"
12
13#include "Tempus_StepperNewmarkExplicitAForm.hpp"
18
19#include "../TestModels/HarmonicOscillatorModel.hpp"
20
21
22namespace Tempus_Unit_Test {
23
24using Teuchos::RCP;
25using Teuchos::rcp;
26using Teuchos::rcp_const_cast;
27using Teuchos::rcp_dynamic_cast;
28using Teuchos::ParameterList;
29using Teuchos::sublist;
30
31
32// ************************************************************
33// ************************************************************
34class StepperNewmarkExplicitAFormModifierTest
36{
37public:
38
40 StepperNewmarkExplicitAFormModifierTest()
41 : testBEGIN_STEP(false), testBEFORE_EXPLICIT_EVAL(false),
42 testAFTER_EXPLICIT_EVAL(false), testEND_STEP(false),
43 testCurrentValue(-0.99),
44 testDt(-1.5), testName("")
45 {}
46
48 virtual ~StepperNewmarkExplicitAFormModifierTest(){}
49
51 virtual void modify(
52 Teuchos::RCP<Tempus::SolutionHistory<double> > sh,
55 {
56 switch(actLoc) {
57 case StepperNewmarkExplicitAFormAppAction<double>::BEGIN_STEP:
58 {
59 testBEGIN_STEP = true;
60 break;
61 }
62 case StepperNewmarkExplicitAFormAppAction<double>::BEFORE_EXPLICIT_EVAL:
63 {
64 testBEFORE_EXPLICIT_EVAL = true;
65 testName = "Newmark Explicit A Form - Modifier";
66 stepper->setStepperName(testName);
67 break;
68 }
69 case StepperNewmarkExplicitAFormAppAction<double>::AFTER_EXPLICIT_EVAL:
70 {
71 testAFTER_EXPLICIT_EVAL = true;
72 testDt = sh->getWorkingState()->getTimeStep();
73 //sh->getWorkingState()->setTimeStep(testDt);
74 break;
75 }
76 case StepperNewmarkExplicitAFormAppAction<double>::END_STEP:
77 {
78 testEND_STEP = true;
79 auto x = sh->getWorkingState()->getX();
80 testCurrentValue = get_ele(*(x), 0);
81 break;
82 }
83 default:
84 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
85 "Error - unknown action location.\n");
86 }
87 }
88
89 bool testBEGIN_STEP;
90 bool testBEFORE_EXPLICIT_EVAL;
91 bool testAFTER_EXPLICIT_EVAL;
92 bool testEND_STEP;
93 double testCurrentValue;
94 double testDt;
95 std::string testName;
96};
97
98
99// ************************************************************
100// ************************************************************
101class StepperNewmarkExplicitAFormModifierXTest
103{
104public:
105
107 StepperNewmarkExplicitAFormModifierXTest()
108 : testX_BEGIN_STEP(false), testX_BEFORE_EXPLICIT_EVAL(false),
109 testX_AFTER_EXPLICIT_EVAL(false), testX_END_STEP(false),
110 testX(-0.99), testXDot(-0.99),
111 testDt(-1.5), testTime(-1.5)
112 {}
113
115 virtual ~StepperNewmarkExplicitAFormModifierXTest(){}
116
118 virtual void modify(
119 Teuchos::RCP<Thyra::VectorBase<double> > x,
120 const double time, const double dt,
122 {
123 switch(modType) {
124 case StepperNewmarkExplicitAFormModifierXBase<double>::X_BEGIN_STEP:
125 {
126 testX_BEGIN_STEP = true;
127 testDt = dt;
128 testX = get_ele(*(x), 0);
129 break;
130 }
131 case StepperNewmarkExplicitAFormModifierXBase<double>::X_BEFORE_EXPLICIT_EVAL:
132 {
133 testX_BEFORE_EXPLICIT_EVAL = true;
134 testTime = time;
135 testX = get_ele(*(x), 0);
136 break;
137 }
138 case StepperNewmarkExplicitAFormModifierXBase<double>::X_AFTER_EXPLICIT_EVAL:
139 {
140 testX_AFTER_EXPLICIT_EVAL = true;
141 testX = get_ele(*(x), 0);
142 break;
143 }
144 case StepperNewmarkExplicitAFormModifierXBase<double>::X_END_STEP:
145 {
146 testX_END_STEP = true;
147 testTime = time;
148 testX = get_ele(*(x), 0);
149 break;
150 }
151 default:
152 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
153 "Error - unknown action location.\n");
154 }
155 }
156
157 bool testX_BEGIN_STEP;
158 bool testX_BEFORE_EXPLICIT_EVAL;
159 bool testX_AFTER_EXPLICIT_EVAL;
160 bool testX_END_STEP;
161 double testX;
162 double testXDot;
163 double testDt;
164 double testTime;
165};
166
167
168TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, AppAction_Modifier)
169{
170 using Teuchos::RCP;
171 using Teuchos::sublist;
172 using Teuchos::ParameterList;
173
174 RCP<Tempus::IntegratorBasic<double> > integrator;
175 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
176 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
177
178 // Read params from .xml file
179 RCP<ParameterList> pList =
180 Teuchos::getParametersFromXmlFile("Tempus_NewmarkExplicitAForm_HarmonicOscillator_Damped.xml");
181
182 // Setup the HarmonicOscillatorModel
183 RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
184 RCP<Tempus_Test::HarmonicOscillatorModel<double> > model =
185 Teuchos::rcp(new Tempus_Test::HarmonicOscillatorModel<double>(hom_pl));
186
187 // Setup the Integrator and reset initial time step
188 RCP<ParameterList> pl = sublist(pList, "Tempus", true);
189 RCP<ParameterList> stepperPL = sublist(pl, "Default Stepper", true);
190 stepperPL->remove("Zero Initial Guess");
191
192 double dt =pl->sublist("Default Integrator")
193 .sublist("Time Step Control").get<double>("Initial Time Step");
194 dt *= 2.0;
195
196 pl->sublist("Default Integrator")
197 .sublist("Time Step Control").set("Initial Time Step", dt);
198 integrator = Tempus::createIntegratorBasic<double>(pl, model);
199
200 RCP<Tempus::StepperNewmarkExplicitAForm<double> > stepper =
201 Teuchos::rcp_dynamic_cast<Tempus::StepperNewmarkExplicitAForm<double> >(integrator->getStepper(), true);
202
203 auto modifier = rcp(new StepperNewmarkExplicitAFormModifierTest());
204 stepper->setAppAction(modifier);
205 stepper->initialize();
206 integrator->initialize();
207
208 // Integrate to timeMax
209 bool integratorStatus = integrator->advanceTime();
210 TEST_ASSERT(integratorStatus)
211
212 // Testing that each ACTION_LOCATION has been called.
213 TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
214 TEST_COMPARE(modifier->testBEFORE_EXPLICIT_EVAL, ==, true);
215 TEST_COMPARE(modifier->testAFTER_EXPLICIT_EVAL, ==, true);
216 TEST_COMPARE(modifier->testEND_STEP, ==, true);
217
218 // Testing that values can be set through the Modifier.
219 auto x = integrator->getX();
220 auto Dt = integrator->getTime();
221 TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
222 TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
223 TEST_COMPARE(modifier->testName, ==, stepper->getStepperName());
224}
225
226
227TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, AppAction_ModifierX)
228{
229 using Teuchos::RCP;
230 using Teuchos::sublist;
231 using Teuchos::ParameterList;
232
233 RCP<Tempus::IntegratorBasic<double> > integrator;
234 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
235 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
236
237 // Read params from .xml file
238 RCP<ParameterList> pList =
239 Teuchos::getParametersFromXmlFile("Tempus_NewmarkExplicitAForm_HarmonicOscillator_Damped.xml");
240
241 // Setup the HarmonicOscillatorModel
242 RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
243 RCP<Tempus_Test::HarmonicOscillatorModel<double> > model =
244 Teuchos::rcp(new Tempus_Test::HarmonicOscillatorModel<double>(hom_pl));
245
246 // Setup the Integrator and reset initial time step
247 RCP<ParameterList> pl = sublist(pList, "Tempus", true);
248 RCP<ParameterList> stepperPL = sublist(pl, "Default Stepper", true);
249 stepperPL->remove("Zero Initial Guess");
250
251 double dt =pl->sublist("Default Integrator")
252 .sublist("Time Step Control").get<double>("Initial Time Step");
253 dt *= 2.0;
254
255 pl->sublist("Default Integrator")
256 .sublist("Time Step Control").set("Initial Time Step", dt);
257 integrator = Tempus::createIntegratorBasic<double>(pl, model);
258
259 RCP<Tempus::StepperNewmarkExplicitAForm<double> > stepper =
260 Teuchos::rcp_dynamic_cast<Tempus::StepperNewmarkExplicitAForm<double> >(integrator->getStepper(), true);
261
262 auto modifierX = rcp(new StepperNewmarkExplicitAFormModifierXTest());
263 stepper->setAppAction(modifierX);
264 stepper->initialize();
265 integrator->initialize();
266
267 // Integrate to timeMax
268 bool integratorStatus = integrator->advanceTime();
269 TEST_ASSERT(integratorStatus)
270
271 // Testing that each ACTION_LOCATION has been called.
272 TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
273 TEST_COMPARE(modifierX->testX_BEFORE_EXPLICIT_EVAL, ==, true);
274 TEST_COMPARE(modifierX->testX_AFTER_EXPLICIT_EVAL, ==, true);
275 TEST_COMPARE(modifierX->testX_END_STEP, ==, true);
276
277
278 // Testing that values can be set through the Modifier.
279 auto Dt = integrator->getTime();
280 TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
281
282 const auto x = integrator->getX();
283 TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
284}
285
286} // 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).
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
MODIFIER_TYPE
Indicates the location of application action (see algorithm).
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)