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