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 
40 // ************************************************************
41 // ************************************************************
42 TEUCHOS_UNIT_TEST(IMEX_RK, ConstructingFromDefaults)
43 {
44  double dt = 0.025;
45 
46  // Read params from .xml file
47  RCP<ParameterList> pList =
48  getParametersFromXmlFile("Tempus_IMEX_RK_VanDerPol.xml");
49  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
50 
51  // Setup the explicit VanDerPol ModelEvaluator
52  RCP<ParameterList> vdpmPL = sublist(pList, "VanDerPolModel", true);
53  auto explicitModel = rcp(new VanDerPol_IMEX_ExplicitModel<double>(vdpmPL));
54 
55  // Setup the implicit VanDerPol ModelEvaluator (reuse vdpmPL)
56  auto implicitModel = rcp(new VanDerPol_IMEX_ImplicitModel<double>(vdpmPL));
57 
58  // Setup the IMEX Pair ModelEvaluator
60  explicitModel, implicitModel));
61 
62 
63  // Setup Stepper for field solve ----------------------------
64  auto stepper = rcp(new Tempus::StepperIMEX_RK<double>());
65  stepper->setModel(model);
66  stepper->initialize();
67 
68  // Setup TimeStepControl ------------------------------------
69  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
70  ParameterList tscPL = pl->sublist("Default Integrator")
71  .sublist("Time Step Control");
72  timeStepControl->setStepType (tscPL.get<std::string>("Integrator Step Type"));
73  timeStepControl->setInitIndex(tscPL.get<int> ("Initial Time Index"));
74  timeStepControl->setInitTime (tscPL.get<double>("Initial Time"));
75  timeStepControl->setFinalTime(tscPL.get<double>("Final Time"));
76  timeStepControl->setInitTimeStep(dt);
77  timeStepControl->initialize();
78 
79  // Setup initial condition SolutionState --------------------
80  Thyra::ModelEvaluatorBase::InArgs<double> inArgsIC =
81  stepper->getModel()->getNominalValues();
82  auto icSolution = rcp_const_cast<Thyra::VectorBase<double> > (inArgsIC.get_x());
83  auto icState = Tempus::createSolutionStateX(icSolution);
84  icState->setTime (timeStepControl->getInitTime());
85  icState->setIndex (timeStepControl->getInitIndex());
86  icState->setTimeStep(0.0);
87  icState->setOrder (stepper->getOrder());
88  icState->setSolutionStatus(Tempus::Status::PASSED); // ICs are passing.
89 
90  // Setup SolutionHistory ------------------------------------
91  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
92  solutionHistory->setName("Forward States");
93  solutionHistory->setStorageType(Tempus::STORAGE_TYPE_STATIC);
94  solutionHistory->setStorageLimit(2);
95  solutionHistory->addState(icState);
96 
97  // Setup Integrator -----------------------------------------
98  RCP<Tempus::IntegratorBasic<double> > integrator =
99  Tempus::integratorBasic<double>();
100  integrator->setStepperWStepper(stepper);
101  integrator->setTimeStepControl(timeStepControl);
102  integrator->setSolutionHistory(solutionHistory);
103  integrator->initialize();
104 
105 
106  // Integrate to timeMax
107  bool integratorStatus = integrator->advanceTime();
108  TEST_ASSERT(integratorStatus)
109 
110 
111  // Test if at 'Final Time'
112  double time = integrator->getTime();
113  double timeFinal =pl->sublist("Default Integrator")
114  .sublist("Time Step Control").get<double>("Final Time");
115  TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
116 
117  // Time-integrated solution and the exact solution
118  RCP<Thyra::VectorBase<double> > x = integrator->getX();
119 
120  // Check the order and intercept
121  std::cout << " Stepper = " << stepper->description() << std::endl;
122  std::cout << " =========================" << std::endl;
123  std::cout << " Computed solution: " << get_ele(*(x ), 0) << " "
124  << get_ele(*(x ), 1) << std::endl;
125  std::cout << " =========================" << std::endl;
126  TEST_FLOATING_EQUALITY(get_ele(*(x), 0), 1.810210, 1.0e-4 );
127  TEST_FLOATING_EQUALITY(get_ele(*(x), 1), -0.754602, 1.0e-4 );
128 }
129 
130 
131 // ************************************************************
132 // ************************************************************
133 TEUCHOS_UNIT_TEST(IMEX_RK, VanDerPol)
134 {
135  std::vector<std::string> stepperTypes;
136  stepperTypes.push_back("IMEX RK 1st order");
137  stepperTypes.push_back("SSP1_111" );
138  stepperTypes.push_back("IMEX RK SSP2" );
139  stepperTypes.push_back("SSP2_222" );
140  stepperTypes.push_back("IMEX RK ARS 233" );
141  stepperTypes.push_back("General IMEX RK" );
142  stepperTypes.push_back("IMEX RK SSP3" );
143 
144  std::vector<double> stepperOrders;
145  stepperOrders.push_back(1.07964);
146  stepperOrders.push_back(1.07964); // SSP1_111
147  stepperOrders.push_back(2.00408);
148  stepperOrders.push_back(2.76941); //SSP2_222
149  stepperOrders.push_back(2.70655);
150  stepperOrders.push_back(2.00211);
151  stepperOrders.push_back(2.00211);
152 
153  std::vector<double> stepperErrors;
154  stepperErrors.push_back(0.0046423);
155  stepperErrors.push_back(0.103569); // SSP1_111
156  stepperErrors.push_back(0.0154534);
157  stepperErrors.push_back(0.000533759); // SSP2_222
158  stepperErrors.push_back(0.000298908);
159  stepperErrors.push_back(0.0071546);
160  stepperErrors.push_back(0.0151202);
161 
162  std::vector<double> stepperInitDt;
163  stepperInitDt.push_back(0.0125);
164  stepperInitDt.push_back(0.0125);
165  stepperInitDt.push_back(0.05);
166  stepperInitDt.push_back(0.05);
167  stepperInitDt.push_back(0.05);
168  stepperInitDt.push_back(0.05);
169  stepperInitDt.push_back(0.05);
170 
171  TEUCHOS_ASSERT( stepperTypes.size() == stepperOrders.size() );
172  TEUCHOS_ASSERT( stepperTypes.size() == stepperErrors.size() );
173  TEUCHOS_ASSERT( stepperTypes.size() == stepperInitDt.size() );
174 
175  std::vector<std::string>::size_type m;
176  for(m = 0; m != stepperTypes.size(); m++) {
177 
178  std::string stepperType = stepperTypes[m];
179  std::string stepperName = stepperTypes[m];
180  std::replace(stepperName.begin(), stepperName.end(), ' ', '_');
181  std::replace(stepperName.begin(), stepperName.end(), '/', '.');
182 
183  RCP<Tempus::IntegratorBasic<double> > integrator;
184  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
185  std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
186  std::vector<double> StepSize;
187  std::vector<double> xErrorNorm;
188  std::vector<double> xDotErrorNorm;
189 
190  const int nTimeStepSizes = 3; // 6 for error plot
191  double dt = stepperInitDt[m];
192  double time = 0.0;
193  for (int n=0; n<nTimeStepSizes; n++) {
194 
195  // Read params from .xml file
196  RCP<ParameterList> pList =
197  getParametersFromXmlFile("Tempus_IMEX_RK_VanDerPol.xml");
198 
199  // Setup the explicit VanDerPol ModelEvaluator
200  RCP<ParameterList> vdpmPL = sublist(pList, "VanDerPolModel", true);
201  auto explicitModel = rcp(new VanDerPol_IMEX_ExplicitModel<double>(vdpmPL));
202 
203  // Setup the implicit VanDerPol ModelEvaluator (reuse vdpmPL)
204  auto implicitModel = rcp(new VanDerPol_IMEX_ImplicitModel<double>(vdpmPL));
205 
206  // Setup the IMEX Pair ModelEvaluator
208  explicitModel, implicitModel));
209 
210  // Set the Stepper
211  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
212  if (stepperType == "General IMEX RK"){
213  // use the appropriate stepper sublist
214  pl->sublist("Default Integrator").set("Stepper Name", "General IMEX RK");
215  } else {
216  pl->sublist("Default Stepper").set("Stepper Type", stepperType);
217  }
218 
219  // Set the step size
220  if (n == nTimeStepSizes-1) dt /= 10.0;
221  else dt /= 2;
222 
223  // Setup the Integrator and reset initial time step
224  pl->sublist("Default Integrator")
225  .sublist("Time Step Control").set("Initial Time Step", dt);
226  integrator = Tempus::integratorBasic<double>(pl, model);
227 
228  // Integrate to timeMax
229  bool integratorStatus = integrator->advanceTime();
230  TEST_ASSERT(integratorStatus)
231 
232  // Test if at 'Final Time'
233  time = integrator->getTime();
234  double timeFinal =pl->sublist("Default Integrator")
235  .sublist("Time Step Control").get<double>("Final Time");
236  double tol = 100.0 * std::numeric_limits<double>::epsilon();
237  TEST_FLOATING_EQUALITY(time, timeFinal, tol);
238 
239  // Store off the final solution and step size
240  StepSize.push_back(dt);
241  auto solution = Thyra::createMember(model->get_x_space());
242  Thyra::copy(*(integrator->getX()),solution.ptr());
243  solutions.push_back(solution);
244  auto solutionDot = Thyra::createMember(model->get_x_space());
245  Thyra::copy(*(integrator->getXdot()),solutionDot.ptr());
246  solutionsDot.push_back(solutionDot);
247 
248  // Output finest temporal solution for plotting
249  // This only works for ONE MPI process
250  if ((n == 0) or (n == nTimeStepSizes-1)) {
251  std::string fname = "Tempus_"+stepperName+"_VanDerPol-Ref.dat";
252  if (n == 0) fname = "Tempus_"+stepperName+"_VanDerPol.dat";
253  RCP<const SolutionHistory<double> > solutionHistory =
254  integrator->getSolutionHistory();
255  writeSolution(fname, solutionHistory);
256  }
257  }
258 
259  // Check the order and intercept
260  double xSlope = 0.0;
261  double xDotSlope = 0.0;
262  RCP<Tempus::Stepper<double> > stepper = integrator->getStepper();
263  //double order = stepper->getOrder();
264  writeOrderError("Tempus_"+stepperName+"_VanDerPol-Error.dat",
265  stepper, StepSize,
266  solutions, xErrorNorm, xSlope,
267  solutionsDot, xDotErrorNorm, xDotSlope);
268 
269  TEST_FLOATING_EQUALITY( xSlope, stepperOrders[m], 0.02 );
270  TEST_FLOATING_EQUALITY( xErrorNorm[0], stepperErrors[m], 1.0e-4 );
271  // xDot not yet available for IMEX_RK.
272  //TEST_FLOATING_EQUALITY( xDotSlope, 1.74898, 0.10 );
273  //TEST_FLOATING_EQUALITY( xDotErrorNorm[0], 1.0038, 1.0e-4 );
274 
275  }
276  //Teuchos::TimeMonitor::summarize();
277 }
278 
279 
280 } // namespace Tempus_Test
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateX(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdot=Teuchos::null, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdotdot=Teuchos::null)
Nonmember constructor from non-const solution vectors, x.
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...
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...