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 
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 
14 #include "Thyra_VectorStdOps.hpp"
15 
16 #include "Tempus_IntegratorBasic.hpp"
17 #include "Tempus_SolutionHistory.hpp"
19 
20 #include "Tempus_StepperNewmarkExplicitAForm.hpp"
25 
26 #include "../TestModels/SinCosModel.hpp"
27 #include "../TestModels/VanDerPolModel.hpp"
28 #include "../TestModels/HarmonicOscillatorModel.hpp"
29 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
30 
31 #include <fstream>
32 #include <vector>
33 
34 namespace Tempus_Unit_Test {
35 
36 using Teuchos::RCP;
37 using Teuchos::rcp;
38 using Teuchos::rcp_const_cast;
39 using Teuchos::rcp_dynamic_cast;
41 using Teuchos::sublist;
42 using Teuchos::getParametersFromXmlFile;
43 
44 
45 // ************************************************************
46 // ************************************************************
47 class StepperNewmarkExplicitAFormModifierTest
49 {
50 public:
51 
53  StepperNewmarkExplicitAFormModifierTest()
54  : testBEGIN_STEP(false), testBEFORE_EXPLICIT_EVAL(false),
55  testAFTER_EXPLICIT_EVAL(false), testEND_STEP(false),
56  testCurrentValue(-0.99),
57  testDt(-1.5), testName("")
58  {}
59 
61  virtual ~StepperNewmarkExplicitAFormModifierTest(){}
62 
64  virtual void modify(
68  {
69  switch(actLoc) {
71  {
72  testBEGIN_STEP = true;
73  break;
74  }
76  {
77  testBEFORE_EXPLICIT_EVAL = true;
78  testName = "Newmark Explicit A Form - Modifier";
79  stepper->setStepperName(testName);
80  break;
81  }
83  {
84  testAFTER_EXPLICIT_EVAL = true;
85  testDt = sh->getWorkingState()->getTimeStep();
86  //sh->getWorkingState()->setTimeStep(testDt);
87  break;
88  }
90  {
91  testEND_STEP = true;
92  auto x = sh->getWorkingState()->getX();
93  testCurrentValue = get_ele(*(x), 0);
94  break;
95  }
96  default:
97  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
98  "Error - unknown action location.\n");
99  }
100  }
101 
102  bool testBEGIN_STEP;
103  bool testBEFORE_EXPLICIT_EVAL;
104  bool testAFTER_EXPLICIT_EVAL;
105  bool testEND_STEP;
106  double testCurrentValue;
107  double testDt;
108  std::string testName;
109 };
110 
111 
112 // ************************************************************
113 // ************************************************************
114 class StepperNewmarkExplicitAFormModifierXTest
115  : virtual public Tempus::StepperNewmarkExplicitAFormModifierXBase<double>
116 {
117 public:
118 
120  StepperNewmarkExplicitAFormModifierXTest()
121  : testX_BEGIN_STEP(false), testX_BEFORE_EXPLICIT_EVAL(false),
122  testX_AFTER_EXPLICIT_EVAL(false), testX_END_STEP(false),
123  testX(-0.99), testXDot(-0.99),
124  testDt(-1.5), testTime(-1.5)
125  {}
126 
128  virtual ~StepperNewmarkExplicitAFormModifierXTest(){}
129 
131  virtual void modify(
133  const double time, const double dt,
135  {
136  switch(modType) {
138  {
139  testX_BEGIN_STEP = true;
140  testDt = dt;
141  testX = get_ele(*(x), 0);
142  break;
143  }
145  {
146  testX_BEFORE_EXPLICIT_EVAL = true;
147  testTime = time;
148  testX = get_ele(*(x), 0);
149  break;
150  }
152  {
153  testX_AFTER_EXPLICIT_EVAL = true;
154  testX = get_ele(*(x), 0);
155  break;
156  }
158  {
159  testX_END_STEP = true;
160  testTime = time;
161  testX = get_ele(*(x), 0);
162  break;
163  }
164  default:
165  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
166  "Error - unknown action location.\n");
167  }
168  }
169 
170  bool testX_BEGIN_STEP;
171  bool testX_BEFORE_EXPLICIT_EVAL;
172  bool testX_AFTER_EXPLICIT_EVAL;
173  bool testX_END_STEP;
174  double testX;
175  double testXDot;
176  double testDt;
177  double testTime;
178 };
179 
180 
181 TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, AppAction_Modifier)
182 {
183  using Teuchos::RCP;
184  using Teuchos::getParametersFromXmlFile;
185  using Teuchos::sublist;
187 
189  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
190  std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
191 
192  // Read params from .xml file
193  RCP<ParameterList> pList =
194  getParametersFromXmlFile("Tempus_NewmarkExplicitAForm_HarmonicOscillator_Damped.xml");
195 
196  // Setup the HarmonicOscillatorModel
197  RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
200 
201  // Setup the Integrator and reset initial time step
202  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
203  RCP<ParameterList> stepperPL = sublist(pl, "Default Stepper", true);
204  stepperPL->remove("Zero Initial Guess");
205 
206  double dt =pl->sublist("Default Integrator")
207  .sublist("Time Step Control").get<double>("Initial Time Step");
208  dt *= 2.0;
209 
210  pl->sublist("Default Integrator")
211  .sublist("Time Step Control").set("Initial Time Step", dt);
212  integrator = Tempus::createIntegratorBasic<double>(pl, model);
213 
215  Teuchos::rcp_dynamic_cast<Tempus::StepperNewmarkExplicitAForm<double> >(integrator->getStepper(), true);
216 
217  auto modifier = rcp(new StepperNewmarkExplicitAFormModifierTest());
218  stepper->setAppAction(modifier);
219  stepper->initialize();
220  integrator->initialize();
221 
222  // Integrate to timeMax
223  bool integratorStatus = integrator->advanceTime();
224  TEST_ASSERT(integratorStatus)
225 
226  // Testing that each ACTION_LOCATION has been called.
227  TEST_COMPARE(modifier->testBEGIN_STEP, ==, true);
228  TEST_COMPARE(modifier->testBEFORE_EXPLICIT_EVAL, ==, true);
229  TEST_COMPARE(modifier->testAFTER_EXPLICIT_EVAL, ==, true);
230  TEST_COMPARE(modifier->testEND_STEP, ==, true);
231 
232  // Testing that values can be set through the Modifier.
233  auto x = integrator->getX();
234  auto Dt = integrator->getTime();
235  TEST_FLOATING_EQUALITY(modifier->testDt, Dt, 1.0e-14);
236  TEST_FLOATING_EQUALITY(modifier->testCurrentValue, get_ele(*(x), 0), 1.0e-14);
237  TEST_COMPARE(modifier->testName, ==, stepper->getStepperName());
238 }
239 
240 
241 TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, AppAction_ModifierX)
242 {
243  using Teuchos::RCP;
244  using Teuchos::getParametersFromXmlFile;
245  using Teuchos::sublist;
247 
249  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
250  std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
251 
252  // Read params from .xml file
253  RCP<ParameterList> pList =
254  getParametersFromXmlFile("Tempus_NewmarkExplicitAForm_HarmonicOscillator_Damped.xml");
255 
256  // Setup the HarmonicOscillatorModel
257  RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
260 
261  // Setup the Integrator and reset initial time step
262  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
263  RCP<ParameterList> stepperPL = sublist(pl, "Default Stepper", true);
264  stepperPL->remove("Zero Initial Guess");
265 
266  double dt =pl->sublist("Default Integrator")
267  .sublist("Time Step Control").get<double>("Initial Time Step");
268  dt *= 2.0;
269 
270  pl->sublist("Default Integrator")
271  .sublist("Time Step Control").set("Initial Time Step", dt);
272  integrator = Tempus::createIntegratorBasic<double>(pl, model);
273 
275  Teuchos::rcp_dynamic_cast<Tempus::StepperNewmarkExplicitAForm<double> >(integrator->getStepper(), true);
276 
277  auto modifierX = rcp(new StepperNewmarkExplicitAFormModifierXTest());
278  stepper->setAppAction(modifierX);
279  stepper->initialize();
280  integrator->initialize();
281 
282  // Integrate to timeMax
283  bool integratorStatus = integrator->advanceTime();
284  TEST_ASSERT(integratorStatus)
285 
286  // Testing that each ACTION_LOCATION has been called.
287  TEST_COMPARE(modifierX->testX_BEGIN_STEP, ==, true);
288  TEST_COMPARE(modifierX->testX_BEFORE_EXPLICIT_EVAL, ==, true);
289  TEST_COMPARE(modifierX->testX_AFTER_EXPLICIT_EVAL, ==, true);
290  TEST_COMPARE(modifierX->testX_END_STEP, ==, true);
291 
292 
293  // Testing that values can be set through the Modifier.
294  auto Dt = integrator->getTime();
295  TEST_FLOATING_EQUALITY(modifierX->testDt, Dt, 1.0e-14);
296 
297  const auto x = integrator->getX();
298  TEST_FLOATING_EQUALITY(modifierX->testX, get_ele(*(x), 0), 1.0e-14);
299 }
300 
301 } // namespace Tempus_Test
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)
ACTION_LOCATION
Indicates the location of application action (see algorithm).
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
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="")
MODIFIER_TYPE
Indicates the location of application action (see algorithm).