Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
12 
13 #include "Tempus_StepperNewmarkExplicitAForm.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 class StepperNewmarkExplicitAFormModifierTest
33  : virtual public Tempus::StepperNewmarkExplicitAFormModifierBase<double> {
34  public:
36  StepperNewmarkExplicitAFormModifierTest()
37  : testBEGIN_STEP(false),
38  testBEFORE_EXPLICIT_EVAL(false),
39  testAFTER_EXPLICIT_EVAL(false),
40  testEND_STEP(false),
41  testCurrentValue(-0.99),
42  testDt(-1.5),
43  testName("")
44  {
45  }
46 
48  virtual ~StepperNewmarkExplicitAFormModifierTest() {}
49 
51  virtual void modify(
55  double>::ACTION_LOCATION actLoc)
56  {
57  switch (actLoc) {
59  testBEGIN_STEP = true;
60  break;
61  }
63  testBEFORE_EXPLICIT_EVAL = true;
64  testName = "Newmark Explicit A Form - Modifier";
65  stepper->setStepperName(testName);
66  break;
67  }
69  testAFTER_EXPLICIT_EVAL = true;
70  testDt = sh->getWorkingState()->getTimeStep();
71  // sh->getWorkingState()->setTimeStep(testDt);
72  break;
73  }
75  testEND_STEP = true;
76  auto x = sh->getWorkingState()->getX();
77  testCurrentValue = get_ele(*(x), 0);
78  break;
79  }
80  default:
81  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
82  "Error - unknown action location.\n");
83  }
84  }
85 
86  bool testBEGIN_STEP;
87  bool testBEFORE_EXPLICIT_EVAL;
88  bool testAFTER_EXPLICIT_EVAL;
89  bool testEND_STEP;
90  double testCurrentValue;
91  double testDt;
92  std::string testName;
93 };
94 
95 // ************************************************************
96 // ************************************************************
97 class StepperNewmarkExplicitAFormModifierXTest
98  : virtual public Tempus::StepperNewmarkExplicitAFormModifierXBase<double> {
99  public:
101  StepperNewmarkExplicitAFormModifierXTest()
102  : testX_BEGIN_STEP(false),
103  testX_BEFORE_EXPLICIT_EVAL(false),
104  testX_AFTER_EXPLICIT_EVAL(false),
105  testX_END_STEP(false),
106  testX(-0.99),
107  testXDot(-0.99),
108  testDt(-1.5),
109  testTime(-1.5)
110  {
111  }
112 
114  virtual ~StepperNewmarkExplicitAFormModifierXTest() {}
115 
117  virtual void modify(
118  Teuchos::RCP<Thyra::VectorBase<double>> x, const double time,
119  const double dt,
121  double>::MODIFIER_TYPE modType)
122  {
123  switch (modType) {
125  testX_BEGIN_STEP = true;
126  testDt = dt;
127  testX = get_ele(*(x), 0);
128  break;
129  }
130  case StepperNewmarkExplicitAFormModifierXBase<
131  double>::X_BEFORE_EXPLICIT_EVAL: {
132  testX_BEFORE_EXPLICIT_EVAL = true;
133  testTime = time;
134  testX = get_ele(*(x), 0);
135  break;
136  }
137  case StepperNewmarkExplicitAFormModifierXBase<
138  double>::X_AFTER_EXPLICIT_EVAL: {
139  testX_AFTER_EXPLICIT_EVAL = true;
140  testX = get_ele(*(x), 0);
141  break;
142  }
144  testX_END_STEP = true;
145  testTime = time;
146  testX = get_ele(*(x), 0);
147  break;
148  }
149  default:
150  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
151  "Error - unknown action location.\n");
152  }
153  }
154 
155  bool testX_BEGIN_STEP;
156  bool testX_BEFORE_EXPLICIT_EVAL;
157  bool testX_AFTER_EXPLICIT_EVAL;
158  bool testX_END_STEP;
159  double testX;
160  double testXDot;
161  double testDt;
162  double testTime;
163 };
164 
165 TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, AppAction_Modifier)
166 {
168  using Teuchos::RCP;
169  using Teuchos::sublist;
170 
172  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
173  std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
174 
175  // Read params from .xml file
176  RCP<ParameterList> pList = Teuchos::getParametersFromXmlFile(
177  "Tempus_NewmarkExplicitAForm_HarmonicOscillator_Damped.xml");
178 
179  // Setup the HarmonicOscillatorModel
180  RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
183 
184  // Setup the Integrator and reset initial time step
185  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
186  RCP<ParameterList> stepperPL = sublist(pl, "Default Stepper", true);
187  stepperPL->remove("Zero Initial Guess");
188 
189  double dt = pl->sublist("Default Integrator")
190  .sublist("Time Step Control")
191  .get<double>("Initial Time Step");
192  dt *= 2.0;
193 
194  pl->sublist("Default Integrator")
195  .sublist("Time Step Control")
196  .set("Initial Time Step", dt);
197  integrator = Tempus::createIntegratorBasic<double>(pl, model);
198 
200  Teuchos::rcp_dynamic_cast<Tempus::StepperNewmarkExplicitAForm<double>>(
201  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 TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, AppAction_ModifierX)
227 {
229  using Teuchos::RCP;
230  using Teuchos::sublist;
231 
233  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
234  std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
235 
236  // Read params from .xml file
237  RCP<ParameterList> pList = Teuchos::getParametersFromXmlFile(
238  "Tempus_NewmarkExplicitAForm_HarmonicOscillator_Damped.xml");
239 
240  // Setup the HarmonicOscillatorModel
241  RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
244 
245  // Setup the Integrator and reset initial time step
246  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
247  RCP<ParameterList> stepperPL = sublist(pl, "Default Stepper", true);
248  stepperPL->remove("Zero Initial Guess");
249 
250  double dt = pl->sublist("Default Integrator")
251  .sublist("Time Step Control")
252  .get<double>("Initial Time Step");
253  dt *= 2.0;
254 
255  pl->sublist("Default Integrator")
256  .sublist("Time Step Control")
257  .set("Initial Time Step", dt);
258  integrator = Tempus::createIntegratorBasic<double>(pl, model);
259 
261  Teuchos::rcp_dynamic_cast<Tempus::StepperNewmarkExplicitAForm<double>>(
262  integrator->getStepper(), true);
263 
264  auto modifierX = rcp(new StepperNewmarkExplicitAFormModifierXTest());
265  stepper->setAppAction(modifierX);
266  stepper->initialize();
267  integrator->initialize();
268 
269  // Integrate to timeMax
270  bool integratorStatus = integrator->advanceTime();
271  TEST_ASSERT(integratorStatus)
272 
273  // Testing that each ACTION_LOCATION has been called.
274  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
275  TEST_COMPARE(modifierX->testX_BEFORE_EXPLICIT_EVAL, ==, true);
276  TEST_COMPARE(modifierX->testX_AFTER_EXPLICIT_EVAL, ==, true);
277  TEST_COMPARE(modifierX->testX_END_STEP, ==, true);
278 
279  // Testing that values can be set through the Modifier.
280  auto Dt = integrator->getTime();
281  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
282 
283  const auto x = integrator->getX();
284  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
285 }
286 
287 } // namespace Tempus_Unit_Test
Application Action for StepperNewmarkExplicitAForm.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
#define TEST_ASSERT(v1)
Consider the ODE: where is a constant, is a constant damping parameter, is a constant forcing par...
bool remove(std::string const &name, bool throwIfNotExists=true)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void modify(Teuchos::RCP< SolutionHistory< double > >, Teuchos::RCP< StepperNewmarkExplicitAForm< double > >, const typename StepperNewmarkExplicitAFormAppAction< double >::ACTION_LOCATION actLoc)=0
Modify NewmarkExplicitAForm Stepper.
virtual void modify(Teuchos::RCP< Thyra::VectorBase< double > >, const double, const double, const MODIFIER_TYPE modType)=0
Modify solution based on the MODIFIER_TYPE.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")