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 
22 namespace Tempus_Unit_Test {
23 
24 using Teuchos::RCP;
25 using Teuchos::rcp;
26 using Teuchos::rcp_const_cast;
27 using Teuchos::rcp_dynamic_cast;
29 using Teuchos::sublist;
30 
31 
32 // ************************************************************
33 // ************************************************************
34 class StepperNewmarkExplicitAFormModifierTest
36 {
37 public:
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(
55  {
56  switch(actLoc) {
58  {
59  testBEGIN_STEP = true;
60  break;
61  }
63  {
64  testBEFORE_EXPLICIT_EVAL = true;
65  testName = "Newmark Explicit A Form - Modifier";
66  stepper->setStepperName(testName);
67  break;
68  }
70  {
71  testAFTER_EXPLICIT_EVAL = true;
72  testDt = sh->getWorkingState()->getTimeStep();
73  //sh->getWorkingState()->setTimeStep(testDt);
74  break;
75  }
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 // ************************************************************
101 class StepperNewmarkExplicitAFormModifierXTest
102  : virtual public Tempus::StepperNewmarkExplicitAFormModifierXBase<double>
103 {
104 public:
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(
120  const double time, const double dt,
122  {
123  switch(modType) {
125  {
126  testX_BEGIN_STEP = true;
127  testDt = dt;
128  testX = get_ele(*(x), 0);
129  break;
130  }
132  {
133  testX_BEFORE_EXPLICIT_EVAL = true;
134  testTime = time;
135  testX = get_ele(*(x), 0);
136  break;
137  }
139  {
140  testX_AFTER_EXPLICIT_EVAL = true;
141  testX = get_ele(*(x), 0);
142  break;
143  }
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 
168 TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, AppAction_Modifier)
169 {
170  using Teuchos::RCP;
171  using Teuchos::sublist;
173 
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);
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 
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 
227 TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, AppAction_ModifierX)
228 {
229  using Teuchos::RCP;
230  using Teuchos::sublist;
232 
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);
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 
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_Unit_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).