Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_Test_BackwardEuler_SinCos.cpp
Go to the documentation of this file.
1 //@HEADER
2 // *****************************************************************************
3 // Tempus: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
12 #include "Teuchos_TimeMonitor.hpp"
13 #include "Teuchos_DefaultComm.hpp"
14 
15 #include "Tempus_config.hpp"
16 #include "Tempus_IntegratorBasic.hpp"
17 #include "Tempus_StepperBackwardEuler.hpp"
18 
19 #include "../TestModels/SinCosModel.hpp"
20 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
21 
22 #include <vector>
23 #include <fstream>
24 #include <sstream>
25 #include <limits>
26 
27 namespace Tempus_Test {
28 
29 using Teuchos::getParametersFromXmlFile;
31 using Teuchos::RCP;
32 using Teuchos::rcp;
33 using Teuchos::rcp_const_cast;
34 using Teuchos::sublist;
35 
39 
40 // ************************************************************
41 // ************************************************************
42 TEUCHOS_UNIT_TEST(BackwardEuler, SinCos)
43 {
45  std::vector<RCP<Thyra::VectorBase<double>>> solutions;
46  std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
47  std::vector<double> StepSize;
48  std::vector<double> xErrorNorm;
49  std::vector<double> xDotErrorNorm;
50  const int nTimeStepSizes = 7;
51  double dt = 0.2;
52  double time = 0.0;
53  for (int n = 0; n < nTimeStepSizes; n++) {
54  // Read params from .xml file
55  RCP<ParameterList> pList =
56  getParametersFromXmlFile("Tempus_BackwardEuler_SinCos.xml");
57 
58  // std::ofstream ftmp("PL.txt");
59  // pList->print(ftmp);
60  // ftmp.close();
61 
62  // Setup the SinCosModel
63  RCP<ParameterList> scm_pl = sublist(pList, "SinCosModel", true);
64  // RCP<SinCosModel<double> > model = sineCosineModel(scm_pl);
65  auto model = rcp(new SinCosModel<double>(scm_pl));
66 
67  dt /= 2;
68 
69  // Setup the Integrator and reset initial time step
70  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
71  pl->sublist("Default Integrator")
72  .sublist("Time Step Control")
73  .set("Initial Time Step", dt);
74  integrator = Tempus::createIntegratorBasic<double>(pl, model);
75 
76  // Initial Conditions
77  // During the Integrator construction, the initial SolutionState
78  // is set by default to model->getNominalVales().get_x(). However,
79  // the application can set it also by integrator->initializeSolutionHistory.
81  model->getNominalValues().get_x()->clone_v();
82  integrator->initializeSolutionHistory(0.0, x0);
83 
84  // Integrate to timeMax
85  bool integratorStatus = integrator->advanceTime();
86  TEST_ASSERT(integratorStatus)
87 
88  // Test if at 'Final Time'
89  time = integrator->getTime();
90  double timeFinal = pl->sublist("Default Integrator")
91  .sublist("Time Step Control")
92  .get<double>("Final Time");
93  TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
94 
95  // Plot sample solution and exact solution
96  if (n == 0) {
97  RCP<const SolutionHistory<double>> solutionHistory =
98  integrator->getSolutionHistory();
99  writeSolution("Tempus_BackwardEuler_SinCos.dat", solutionHistory);
100 
101  auto solnHistExact = rcp(new Tempus::SolutionHistory<double>());
102  for (int i = 0; i < solutionHistory->getNumStates(); i++) {
103  double time_i = (*solutionHistory)[i]->getTime();
104  auto state = Tempus::createSolutionStateX(
105  rcp_const_cast<Thyra::VectorBase<double>>(
106  model->getExactSolution(time_i).get_x()),
107  rcp_const_cast<Thyra::VectorBase<double>>(
108  model->getExactSolution(time_i).get_x_dot()));
109  state->setTime((*solutionHistory)[i]->getTime());
110  solnHistExact->addState(state);
111  }
112  writeSolution("Tempus_BackwardEuler_SinCos-Ref.dat", solnHistExact);
113  }
114 
115  // Store off the final solution and step size
116  StepSize.push_back(dt);
117  auto solution = Thyra::createMember(model->get_x_space());
118  Thyra::copy(*(integrator->getX()), solution.ptr());
119  solutions.push_back(solution);
120  auto solutionDot = Thyra::createMember(model->get_x_space());
121  Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
122  solutionsDot.push_back(solutionDot);
123  if (n == nTimeStepSizes - 1) { // Add exact solution last in vector.
124  StepSize.push_back(0.0);
125  auto solutionExact = Thyra::createMember(model->get_x_space());
126  Thyra::copy(*(model->getExactSolution(time).get_x()),
127  solutionExact.ptr());
128  solutions.push_back(solutionExact);
129  auto solutionDotExact = Thyra::createMember(model->get_x_space());
130  Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
131  solutionDotExact.ptr());
132  solutionsDot.push_back(solutionDotExact);
133  }
134  }
135 
136  // Check the order and intercept
137  double xSlope = 0.0;
138  double xDotSlope = 0.0;
139  RCP<Tempus::Stepper<double>> stepper = integrator->getStepper();
140  double order = stepper->getOrder();
141  writeOrderError("Tempus_BackwardEuler_SinCos-Error.dat", stepper, StepSize,
142  solutions, xErrorNorm, xSlope, solutionsDot, xDotErrorNorm,
143  xDotSlope, out);
144 
145  TEST_FLOATING_EQUALITY(xSlope, order, 0.01);
146  TEST_FLOATING_EQUALITY(xErrorNorm[0], 0.0486418, 1.0e-4);
147  TEST_FLOATING_EQUALITY(xDotSlope, order, 0.01);
148  TEST_FLOATING_EQUALITY(xDotErrorNorm[0], 0.0486418, 1.0e-4);
149 
151 }
152 
153 } // 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.
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)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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.