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