Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_Test_BDF2_SinCos.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/SinCosModel.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, SinCos)
42 {
44  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
45  std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
46  std::vector<double> StepSize;
47 
48  // Read params from .xml file
49  RCP<ParameterList> pList = getParametersFromXmlFile("Tempus_BDF2_SinCos.xml");
50  // Set initial time step = 2*dt specified in input file (for convergence
51  // study)
52  double dt = pList->sublist("Tempus")
53  .sublist("Default Integrator")
54  .sublist("Time Step Control")
55  .get<double>("Initial Time Step");
56  dt *= 2.0;
57 
58  // Setup the SinCosModel
59  RCP<ParameterList> scm_pl = sublist(pList, "SinCosModel", true);
60  const int nTimeStepSizes = scm_pl->get<int>("Number of Time Step Sizes", 7);
61  std::string output_file_string =
62  scm_pl->get<std::string>("Output File Name", "Tempus_BDF2_SinCos");
63  std::string output_file_name = output_file_string + ".dat";
64  std::string ref_out_file_name = output_file_string + "-Ref.dat";
65  std::string err_out_file_name = output_file_string + "-Error.dat";
66  double time = 0.0;
67  for (int n = 0; n < nTimeStepSizes; n++) {
68  auto model = rcp(new SinCosModel<double>(scm_pl));
69 
70  dt /= 2;
71 
72  // Setup the Integrator and reset initial time step
73  RCP<ParameterList> tempusPL =
74  getParametersFromXmlFile("Tempus_BDF2_SinCos.xml");
75  RCP<ParameterList> pl = sublist(tempusPL, "Tempus", true);
76  pl->sublist("Default Integrator")
77  .sublist("Time Step Control")
78  .set("Initial Time Step", dt);
79  integrator = Tempus::createIntegratorBasic<double>(pl, model);
80 
81  // Initial Conditions
82  // During the Integrator construction, the initial SolutionState
83  // is set by default to model->getNominalVales().get_x(). However,
84  // the application can set it also by integrator->initializeSolutionHistory.
86  model->getNominalValues().get_x()->clone_v();
87  integrator->initializeSolutionHistory(0.0, x0);
88 
89  // Integrate to timeMax
90  bool integratorStatus = integrator->advanceTime();
91  TEST_ASSERT(integratorStatus)
92 
93  // Test if at 'Final Time'
94  time = integrator->getTime();
95  double timeFinal = pl->sublist("Default Integrator")
96  .sublist("Time Step Control")
97  .get<double>("Final Time");
98  TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
99 
100  // Plot sample solution and exact solution
101  if (n == 0) {
102  RCP<const SolutionHistory<double>> solutionHistory =
103  integrator->getSolutionHistory();
104  writeSolution(output_file_name, solutionHistory);
105 
106  auto solnHistExact = rcp(new Tempus::SolutionHistory<double>());
107  for (int i = 0; i < solutionHistory->getNumStates(); i++) {
108  double time_i = (*solutionHistory)[i]->getTime();
109  auto state = Tempus::createSolutionStateX(
110  rcp_const_cast<Thyra::VectorBase<double>>(
111  model->getExactSolution(time_i).get_x()),
112  rcp_const_cast<Thyra::VectorBase<double>>(
113  model->getExactSolution(time_i).get_x_dot()));
114  state->setTime((*solutionHistory)[i]->getTime());
115  solnHistExact->addState(state);
116  }
117  writeSolution(ref_out_file_name, solnHistExact);
118  }
119 
120  // Store off the final solution and step size
121  StepSize.push_back(dt);
122  auto solution = Thyra::createMember(model->get_x_space());
123  Thyra::copy(*(integrator->getX()), solution.ptr());
124  solutions.push_back(solution);
125  auto solutionDot = Thyra::createMember(model->get_x_space());
126  Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
127  solutionsDot.push_back(solutionDot);
128  if (n == nTimeStepSizes - 1) { // Add exact solution last in vector.
129  StepSize.push_back(0.0);
130  auto solutionExact = Thyra::createMember(model->get_x_space());
131  Thyra::copy(*(model->getExactSolution(time).get_x()),
132  solutionExact.ptr());
133  solutions.push_back(solutionExact);
134  auto solutionDotExact = Thyra::createMember(model->get_x_space());
135  Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
136  solutionDotExact.ptr());
137  solutionsDot.push_back(solutionDotExact);
138  }
139  }
140 
141  // Check the order and intercept
142  if (nTimeStepSizes > 1) {
143  double xSlope = 0.0;
144  double xDotSlope = 0.0;
145  std::vector<double> xErrorNorm;
146  std::vector<double> xDotErrorNorm;
147  RCP<Tempus::Stepper<double>> stepper = integrator->getStepper();
148  double order = stepper->getOrder();
149  writeOrderError(err_out_file_name, stepper, StepSize, solutions, xErrorNorm,
150  xSlope, solutionsDot, xDotErrorNorm, xDotSlope, out);
151 
152  TEST_FLOATING_EQUALITY(xSlope, order, 0.01);
153  TEST_FLOATING_EQUALITY(xDotSlope, order, 0.01);
154  TEST_FLOATING_EQUALITY(xErrorNorm[0], 5.13425e-05, 1.0e-4);
155  TEST_FLOATING_EQUALITY(xDotErrorNorm[0], 5.13425e-05, 1.0e-4);
156  }
157 
159 }
160 
161 } // 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.
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)
T * get() const
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar >> solutionHistory)
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...
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Solution state for integrators and steppers.