Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_Test_BDF2_VanDerPol.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 #include "Teuchos_DefaultComm.hpp"
13 
14 #include "Tempus_config.hpp"
15 #include "Tempus_IntegratorBasic.hpp"
16 #include "Tempus_StepperBDF2.hpp"
17 
18 #include "../TestModels/VanDerPolModel.hpp"
19 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
20 
21 #include <fstream>
22 #include <limits>
23 #include <sstream>
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(BDF2, VanDerPol)
42 {
43  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
44  std::vector<double> StepSize;
45  std::vector<double> ErrorNorm;
46 
47  // Read params from .xml file
48  RCP<ParameterList> pList =
49  getParametersFromXmlFile("Tempus_BDF2_VanDerPol.xml");
50  // Set initial time step = 2*dt specified in input file (for convergence
51  // study)
52  //
53  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
54  double dt = pl->sublist("Demo Integrator")
55  .sublist("Time Step Control")
56  .get<double>("Initial Time Step");
57  dt *= 2.0;
58 
59  RCP<ParameterList> vdpm_pl = sublist(pList, "VanDerPolModel", true);
60  const int nTimeStepSizes = vdpm_pl->get<int>("Number of Time Step Sizes", 3);
61  // const int nTimeStepSizes = 5;
62  double order = 0.0;
63 
64  for (int n = 0; n < nTimeStepSizes; n++) {
65  // Setup the VanDerPolModel
66  auto model = rcp(new VanDerPolModel<double>(vdpm_pl));
67 
68  // Set the step size
69  dt /= 2;
70  if (n == nTimeStepSizes - 1) dt /= 10.0;
71 
72  // Setup the Integrator and reset initial time step
73  pl->sublist("Demo Integrator")
74  .sublist("Time Step Control")
75  .set("Initial Time Step", dt);
77  Tempus::createIntegratorBasic<double>(pl, model);
78  order = integrator->getStepper()->getOrder();
79 
80  // Integrate to timeMax
81  bool integratorStatus = integrator->advanceTime();
82  TEST_ASSERT(integratorStatus)
83 
84  // Test if at 'Final Time'
85  double time = integrator->getTime();
86  double timeFinal = pl->sublist("Demo Integrator")
87  .sublist("Time Step Control")
88  .get<double>("Final Time");
89  double tol = 100.0 * std::numeric_limits<double>::epsilon();
90  TEST_FLOATING_EQUALITY(time, timeFinal, tol);
91 
92  // Store off the final solution and step size
93  auto solution = Thyra::createMember(model->get_x_space());
94  Thyra::copy(*(integrator->getX()), solution.ptr());
95  solutions.push_back(solution);
96  StepSize.push_back(dt);
97 
98  // Output finest temporal solution for plotting
99  // This only works for ONE MPI process
100  if ((n == 0) || (n == nTimeStepSizes - 1)) {
101  std::string fname = "Tempus_BDF2_VanDerPol-Ref.dat";
102  if (n == 0) fname = "Tempus_BDF2_VanDerPol.dat";
103  std::ofstream ftmp(fname);
104  RCP<const SolutionHistory<double>> solutionHistory =
105  integrator->getSolutionHistory();
106  int nStates = solutionHistory->getNumStates();
107  for (int i = 0; i < nStates; i++) {
108  RCP<const SolutionState<double>> solutionState = (*solutionHistory)[i];
109  RCP<const Thyra::VectorBase<double>> x = solutionState->getX();
110  double ttime = solutionState->getTime();
111  ftmp << ttime << " " << get_ele(*x, 0) << " " << get_ele(*x, 1)
112  << std::endl;
113  }
114  ftmp.close();
115  }
116  }
117 
118  // Calculate the error - use the most temporally refined mesh for
119  // the reference solution.
120  auto ref_solution = solutions[solutions.size() - 1];
121  std::vector<double> StepSizeCheck;
122  for (std::size_t i = 0; i < (solutions.size() - 1); ++i) {
123  auto tmp = solutions[i];
124  Thyra::Vp_StV(tmp.ptr(), -1.0, *ref_solution);
125  const double L2norm = Thyra::norm_2(*tmp);
126  StepSizeCheck.push_back(StepSize[i]);
127  ErrorNorm.push_back(L2norm);
128  }
129 
130  if (nTimeStepSizes > 2) {
131  // Check the order and intercept
132  double slope =
133  computeLinearRegressionLogLog<double>(StepSizeCheck, ErrorNorm);
134  out << " Stepper = BDF2" << std::endl;
135  out << " =========================" << std::endl;
136  out << " Expected order: " << order << std::endl;
137  out << " Observed order: " << slope << std::endl;
138  out << " =========================" << std::endl;
139  TEST_FLOATING_EQUALITY(slope, order, 0.10);
140  out << "\n\n ** Slope on BDF2 Method = " << slope << "\n"
141  << std::endl;
142  }
143 
144  // Write error data
145  {
146  std::ofstream ftmp("Tempus_BDF2_VanDerPol-Error.dat");
147  double error0 = 0.8 * ErrorNorm[0];
148  for (std::size_t n = 0; n < StepSizeCheck.size(); n++) {
149  ftmp << StepSizeCheck[n] << " " << ErrorNorm[n] << " "
150  << error0 * (pow(StepSize[n] / StepSize[0], order)) << std::endl;
151  }
152  ftmp.close();
153  }
154 
156 }
157 
158 } // namespace Tempus_Test
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)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
#define TEST_ASSERT(v1)
T * get() const
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
van der Pol model problem for nonlinear electrical circuit.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Solution state for integrators and steppers.