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