Tempus  Version of the Day
Time Integration
Tempus_IMEX_RKTest.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 
9 #include "Teuchos_UnitTestHarness.hpp"
10 #include "Teuchos_XMLParameterListHelpers.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
12 
13 #include "Thyra_VectorStdOps.hpp"
14 
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_WrapperModelEvaluatorPairIMEX_Basic.hpp"
17 #include "Tempus_StepperIMEX_RK.hpp"
18 
19 #include "../TestModels/VanDerPol_IMEX_ExplicitModel.hpp"
20 #include "../TestModels/VanDerPol_IMEX_ImplicitModel.hpp"
21 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
22 
23 #include <fstream>
24 #include <vector>
25 
26 namespace Tempus_Test {
27 
28 using Teuchos::RCP;
29 using Teuchos::ParameterList;
30 using Teuchos::sublist;
31 using Teuchos::getParametersFromXmlFile;
32 
36 
37 // Comment out any of the following tests to exclude from build/run.
38 #define TEST_CONSTRUCTING_FROM_DEFAULTS
39 #define TEST_VANDERPOL
40 
41 
42 #ifdef TEST_CONSTRUCTING_FROM_DEFAULTS
43 // ************************************************************
44 // ************************************************************
45 TEUCHOS_UNIT_TEST(IMEX_RK, ConstructingFromDefaults)
46 {
47  double dt = 0.025;
48 
49  // Read params from .xml file
50  RCP<ParameterList> pList =
51  getParametersFromXmlFile("Tempus_IMEX_RK_VanDerPol.xml");
52  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
53 
54  // Setup the explicit VanDerPol ModelEvaluator
55  RCP<ParameterList> vdpmPL = sublist(pList, "VanDerPolModel", true);
56  RCP<VanDerPol_IMEX_ExplicitModel<double> > explicitModel =
57  Teuchos::rcp(new VanDerPol_IMEX_ExplicitModel<double>(vdpmPL));
58 
59  // Setup the implicit VanDerPol ModelEvaluator (reuse vdpmPL)
60  RCP<VanDerPol_IMEX_ImplicitModel<double> > implicitModel =
61  Teuchos::rcp(new VanDerPol_IMEX_ImplicitModel<double>(vdpmPL));
62 
63  // Setup the IMEX Pair ModelEvaluator
64  RCP<Tempus::WrapperModelEvaluatorPairIMEX_Basic<double> > model =
66  explicitModel, implicitModel));
67 
68 
69  // Setup Stepper for field solve ----------------------------
70  RCP<Tempus::StepperIMEX_RK<double> > stepper =
71  Teuchos::rcp(new Tempus::StepperIMEX_RK<double>(model));
72 
73  // Setup TimeStepControl ------------------------------------
74  RCP<Tempus::TimeStepControl<double> > timeStepControl =
75  Teuchos::rcp(new Tempus::TimeStepControl<double>());
76  ParameterList tscPL = pl->sublist("Default Integrator")
77  .sublist("Time Step Control");
78  timeStepControl->setStepType (tscPL.get<std::string>("Integrator Step Type"));
79  timeStepControl->setInitIndex(tscPL.get<int> ("Initial Time Index"));
80  timeStepControl->setInitTime (tscPL.get<double>("Initial Time"));
81  timeStepControl->setFinalTime(tscPL.get<double>("Final Time"));
82  timeStepControl->setInitTimeStep(dt);
83  timeStepControl->initialize();
84 
85  // Setup initial condition SolutionState --------------------
86  Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
87  stepper->getModel()->getNominalValues();
88  RCP<Thyra::VectorBase<double> > icSolution =
89  Teuchos::rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
90  RCP<Tempus::SolutionState<double> > icState =
91  Teuchos::rcp(new Tempus::SolutionState<double>(icSolution));
92  icState->setTime (timeStepControl->getInitTime());
93  icState->setIndex (timeStepControl->getInitIndex());
94  icState->setTimeStep(0.0);
95  icState->setOrder (stepper->getOrder());
96  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
97 
98  // Setup SolutionHistory ------------------------------------
99  RCP<Tempus::SolutionHistory<double> > solutionHistory =
100  Teuchos::rcp(new Tempus::SolutionHistory<double>());
101  solutionHistory->setName("Forward States");
103  solutionHistory->setStorageLimit(2);
104  solutionHistory->addState(icState);
105 
106  // Setup Integrator -----------------------------------------
107  RCP<Tempus::IntegratorBasic<double> > integrator =
108  Tempus::integratorBasic<double>();
109  integrator->setStepperWStepper(stepper);
110  integrator->setTimeStepControl(timeStepControl);
111  integrator->setSolutionHistory(solutionHistory);
112  //integrator->setObserver(...);
113  integrator->initialize();
114 
115 
116  // Integrate to timeMax
117  bool integratorStatus = integrator->advanceTime();
118  TEST_ASSERT(integratorStatus)
119 
120 
121  // Test if at 'Final Time'
122  double time = integrator->getTime();
123  double timeFinal =pl->sublist("Default Integrator")
124  .sublist("Time Step Control").get<double>("Final Time");
125  TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
126 
127  // Time-integrated solution and the exact solution
128  RCP<Thyra::VectorBase<double> > x = integrator->getX();
129 
130  // Check the order and intercept
131  std::cout << " Stepper = " << stepper->description() << std::endl;
132  std::cout << " =========================" << std::endl;
133  std::cout << " Computed solution: " << get_ele(*(x ), 0) << " "
134  << get_ele(*(x ), 1) << std::endl;
135  std::cout << " =========================" << std::endl;
136  TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 1.810210, 1.0e-4 );
137  TEST_FLOATING_EQUALITY(get_ele(*(x), 1), -0.754602, 1.0e-4 );
138 }
139 #endif // TEST_CONSTRUCTING_FROM_DEFAULTS
140 
141 
142 #ifdef TEST_VANDERPOL
143 // ************************************************************
144 // ************************************************************
145 TEUCHOS_UNIT_TEST(IMEX_RK, VanDerPol)
146 {
147  std::vector<std::string> stepperTypes;
148  stepperTypes.push_back("IMEX RK 1st order");
149  stepperTypes.push_back("IMEX RK SSP2" );
150  stepperTypes.push_back("IMEX RK ARS 233" );
151  stepperTypes.push_back("General IMEX RK" );
152 
153  std::vector<double> stepperOrders;
154  stepperOrders.push_back(1.07964);
155  stepperOrders.push_back(2.00408);
156  stepperOrders.push_back(2.70655);
157  stepperOrders.push_back(2.00211);
158 
159  std::vector<double> stepperErrors;
160  stepperErrors.push_back(0.0046423);
161  stepperErrors.push_back(0.0154534);
162  stepperErrors.push_back(0.000298908);
163  stepperErrors.push_back(0.0071546);
164 
165  std::vector<double> stepperInitDt;
166  stepperInitDt.push_back(0.0125);
167  stepperInitDt.push_back(0.05);
168  stepperInitDt.push_back(0.05);
169  stepperInitDt.push_back(0.05);
170 
171  std::vector<std::string>::size_type m;
172  for(m = 0; m != stepperTypes.size(); m++) {
173 
174  std::string stepperType = stepperTypes[m];
175  std::string stepperName = stepperTypes[m];
176  std::replace(stepperName.begin(), stepperName.end(), ' ', '_');
177  std::replace(stepperName.begin(), stepperName.end(), '/', '.');
178 
179  RCP<Tempus::IntegratorBasic<double> > integrator;
180  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
181  std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
182  std::vector<double> StepSize;
183  std::vector<double> xErrorNorm;
184  std::vector<double> xDotErrorNorm;
185 
186  const int nTimeStepSizes = 3; // 6 for error plot
187  double dt = stepperInitDt[m];
188  double time = 0.0;
189  for (int n=0; n<nTimeStepSizes; n++) {
190 
191  // Read params from .xml file
192  RCP<ParameterList> pList =
193  getParametersFromXmlFile("Tempus_IMEX_RK_VanDerPol.xml");
194 
195  // Setup the explicit VanDerPol ModelEvaluator
196  RCP<ParameterList> vdpmPL = sublist(pList, "VanDerPolModel", true);
197  RCP<VanDerPol_IMEX_ExplicitModel<double> > explicitModel =
198  Teuchos::rcp(new VanDerPol_IMEX_ExplicitModel<double>(vdpmPL));
199 
200  // Setup the implicit VanDerPol ModelEvaluator (reuse vdpmPL)
201  RCP<VanDerPol_IMEX_ImplicitModel<double> > implicitModel =
202  Teuchos::rcp(new VanDerPol_IMEX_ImplicitModel<double>(vdpmPL));
203 
204  // Setup the IMEX Pair ModelEvaluator
205  RCP<Tempus::WrapperModelEvaluatorPairIMEX_Basic<double> > model =
207  explicitModel, implicitModel));
208 
209  // Set the Stepper
210  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
211  if (stepperType == "General IMEX RK"){
212  // use the appropriate stepper sublist
213  pl->sublist("Default Integrator").set("Stepper Name", "General IMEX RK");
214  } else {
215  pl->sublist("Default Stepper").set("Stepper Type", stepperType);
216  }
217 
218  // Set the step size
219  if (n == nTimeStepSizes-1) dt /= 10.0;
220  else dt /= 2;
221 
222  // Setup the Integrator and reset initial time step
223  pl->sublist("Default Integrator")
224  .sublist("Time Step Control").set("Initial Time Step", dt);
225  integrator = Tempus::integratorBasic<double>(pl, model);
226 
227  // Integrate to timeMax
228  bool integratorStatus = integrator->advanceTime();
229  TEST_ASSERT(integratorStatus)
230 
231  // Test if at 'Final Time'
232  time = integrator->getTime();
233  double timeFinal =pl->sublist("Default Integrator")
234  .sublist("Time Step Control").get<double>("Final Time");
235  double tol = 100.0 * std::numeric_limits<double>::epsilon();
236  TEST_FLOATING_EQUALITY(time, timeFinal, tol);
237 
238  // Store off the final solution and step size
239  StepSize.push_back(dt);
240  auto solution = Thyra::createMember(model->get_x_space());
241  Thyra::copy(*(integrator->getX()),solution.ptr());
242  solutions.push_back(solution);
243  auto solutionDot = Thyra::createMember(model->get_x_space());
244  Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
245  solutionsDot.push_back(solutionDot);
246 
247  // Output finest temporal solution for plotting
248  // This only works for ONE MPI process
249  if ((n == 0) or (n == nTimeStepSizes-1)) {
250  std::string fname = "Tempus_"+stepperName+"_VanDerPol-Ref.dat";
251  if (n == 0) fname = "Tempus_"+stepperName+"_VanDerPol.dat";
252  RCP<const SolutionHistory<double> > solutionHistory =
253  integrator->getSolutionHistory();
255  }
256  }
257 
258  // Check the order and intercept
259  double xSlope = 0.0;
260  double xDotSlope = 0.0;
261  RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
262  //double order = stepper->getOrder();
263  writeOrderError("Tempus_"+stepperName+"_VanDerPol-Error.dat",
264  stepper, StepSize,
265  solutions, xErrorNorm, xSlope,
266  solutionsDot, xDotErrorNorm, xDotSlope);
267 
268  TEST_FLOATING_EQUALITY( xSlope, stepperOrders[m], 0.02 );
269  TEST_FLOATING_EQUALITY( xErrorNorm[0], stepperErrors[m], 1.0e-4 );
270  // xDot not yet available for IMEX_RK.
271  //TEST_FLOATING_EQUALITY( xDotSlope, 1.74898, 0.10 );
272  //TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 1.0038, 1.0e-4 );
273 
274  }
275  //Teuchos::TimeMonitor::summarize();
276 }
277 #endif // TEST_VANDERPOL
278 
279 
280 } // namespace Tempus_Test
Tempus_Test::writeSolution
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar > > solutionHistory)
Definition: Tempus_ConvergenceTestUtils.hpp:302
Tempus::WrapperModelEvaluatorPairIMEX_Basic
ModelEvaluator pair for implicit and explicit (IMEX) evaulations.
Definition: Tempus_WrapperModelEvaluatorPairIMEX_Basic_decl.hpp:38
Tempus_Test::writeOrderError
void writeOrderError(const std::string filename, Teuchos::RCP< Tempus::Stepper< Scalar > > stepper, std::vector< Scalar > &StepSize, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutions, std::vector< Scalar > &xErrorNorm, Scalar &xSlope, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutionsDot, std::vector< Scalar > &xDotErrorNorm, Scalar &xDotSlope, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutionsDotDot, std::vector< Scalar > &xDotDotErrorNorm, Scalar &xDotDotSlope)
Definition: Tempus_ConvergenceTestUtils.hpp:184
Tempus_Test
Definition: Tempus_BackwardEuler_ASA.cpp:34
Tempus::solutionHistory
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
Definition: Tempus_SolutionHistory_impl.hpp:504
Tempus_Test::VanDerPol_IMEX_ExplicitModel
van der Pol model formulated for IMEX.
Definition: VanDerPol_IMEX_ExplicitModel_decl.hpp:107
Tempus::STORAGE_TYPE_STATIC
Keep a fix number of states.
Definition: Tempus_SolutionHistory_decl.hpp:30
Tempus::IntegratorBasic
Basic time integrator.
Definition: Tempus_IntegratorBasic_decl.hpp:35
Tempus_Test::VanDerPol_IMEX_ImplicitModel
van der Pol model formulated for IMEX-RK.
Definition: VanDerPol_IMEX_ImplicitModel_decl.hpp:107
Tempus::SolutionState
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...
Definition: Tempus_SolutionState_decl.hpp:56
Tempus_Test::TEUCHOS_UNIT_TEST
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
Definition: Tempus_BackwardEuler_ASA.cpp:47
Tempus::TimeStepControl
TimeStepControl manages the time step size. There several mechanicisms that effect the time step size...
Definition: Tempus_Integrator.hpp:26
Tempus::PASSED
Definition: Tempus_Types.hpp:17
Tempus::SolutionHistory
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Definition: Tempus_Integrator.hpp:25
Tempus::StepperIMEX_RK
Implicit-Explicit Runge-Kutta (IMEX-RK) time stepper.
Definition: Tempus_StepperIMEX_RK_decl.hpp:230