Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_Test_NewmarkImplicitAForm_HarmonicOscillator_Damped_FirstOrder.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 "Tempus_config.hpp"
14 #include "Tempus_IntegratorBasic.hpp"
15 
16 #include "Tempus_StepperFactory.hpp"
17 #include "Tempus_StepperNewmarkImplicitAForm.hpp"
18 #include "Tempus_StepperNewmarkImplicitDForm.hpp"
19 
20 #include "../TestModels/HarmonicOscillatorModel.hpp"
21 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
22 
23 #include <fstream>
24 #include <limits>
25 #include <sstream>
26 #include <vector>
27 
28 namespace Tempus_Test {
29 
30 using Teuchos::getParametersFromXmlFile;
32 using Teuchos::RCP;
33 using Teuchos::rcp_const_cast;
34 using Teuchos::rcp_dynamic_cast;
35 using Teuchos::sublist;
36 
40 
41 // ************************************************************
42 TEUCHOS_UNIT_TEST(NewmarkImplicitAForm, HarmonicOscillatorDamped_FirstOrder)
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 = 10;
51  double time = 0.0;
52 
53  // Read params from .xml file
54  RCP<ParameterList> pList = getParametersFromXmlFile(
55  "Tempus_Test_NewmarkImplicitAForm_HarmonicOscillator_Damped_FirstOrder."
56  "xml");
57 
58  // Setup the HarmonicOscillatorModel
59  RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
62 
63  // Setup the Integrator and reset initial time step
64  RCP<ParameterList> pl = sublist(pList, "Tempus", true);
65 
66  // Set initial time step = 2*dt specified in input file (for convergence
67  // study)
68  //
69  double dt = pl->sublist("Default Integrator")
70  .sublist("Time Step Control")
71  .get<double>("Initial Time Step");
72  dt *= 2.0;
73 
74  for (int n = 0; n < nTimeStepSizes; n++) {
75  // Perform time-step refinement
76  dt /= 2;
77  out << "\n \n time step #" << n << " (out of " << nTimeStepSizes - 1
78  << "), dt = " << dt << "\n";
79  pl->sublist("Default Integrator")
80  .sublist("Time Step Control")
81  .set("Initial Time Step", dt);
82  integrator = Tempus::createIntegratorBasic<double>(pl, model);
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();
100  "Tempus_Test_NewmarkImplicitAForm_HarmonicOscillator_Damped_"
101  "FirstOrder.dat",
102  solutionHistory);
103 
104  RCP<Tempus::SolutionHistory<double>> solnHistExact =
106  for (int i = 0; i < solutionHistory->getNumStates(); i++) {
107  double time_i = (*solutionHistory)[i]->getTime();
109  rcp_const_cast<Thyra::VectorBase<double>>(
110  model->getExactSolution(time_i).get_x()),
111  rcp_const_cast<Thyra::VectorBase<double>>(
112  model->getExactSolution(time_i).get_x_dot()));
113  state->setTime((*solutionHistory)[i]->getTime());
114  solnHistExact->addState(state);
115  }
117  "Tempus_Test_NewmarkImplicitAForm_HarmonicOscillator_Damped_"
118  "FirstOrder-Ref.dat",
119  solnHistExact);
120  }
121 
122  // Store off the final solution and step size
123  StepSize.push_back(dt);
124  auto solution = Thyra::createMember(model->get_x_space());
125  Thyra::copy(*(integrator->getX()), solution.ptr());
126  solutions.push_back(solution);
127  auto solutionDot = Thyra::createMember(model->get_x_space());
128  Thyra::copy(*(integrator->getXDot()), solutionDot.ptr());
129  solutionsDot.push_back(solutionDot);
130  if (n == nTimeStepSizes - 1) { // Add exact solution last in vector.
131  StepSize.push_back(0.0);
132  auto solutionExact = Thyra::createMember(model->get_x_space());
133  Thyra::copy(*(model->getExactSolution(time).get_x()),
134  solutionExact.ptr());
135  solutions.push_back(solutionExact);
136  auto solutionDotExact = Thyra::createMember(model->get_x_space());
137  Thyra::copy(*(model->getExactSolution(time).get_x_dot()),
138  solutionDotExact.ptr());
139  solutionsDot.push_back(solutionDotExact);
140  }
141  }
142 
143  // Check the order and intercept
144  double xSlope = 0.0;
145  double xDotSlope = 0.0;
146  RCP<Tempus::Stepper<double>> stepper = integrator->getStepper();
147  double order = stepper->getOrder();
149  "Tempus_Test_NewmarkImplicitAForm_HarmonicOscillator_Damped_FirstOrder-"
150  "Error.dat",
151  stepper, StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
152  xDotErrorNorm, xDotSlope, out);
153 
154  TEST_FLOATING_EQUALITY(xSlope, order, 0.02);
155  TEST_FLOATING_EQUALITY(xErrorNorm[0], 0.0224726, 1.0e-4);
156  TEST_FLOATING_EQUALITY(xDotSlope, order, 0.02);
157  TEST_FLOATING_EQUALITY(xDotErrorNorm[0], 0.0122223, 1.0e-4);
158 
160 }
161 
162 } // 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
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar >> solutionHistory)
Consider the ODE: where is a constant, is a constant damping parameter, is a constant forcing par...
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.