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