Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_Test_NewmarkExplicitAForm_HarmonicOscillator_Damped.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
10#include "Teuchos_UnitTestHarness.hpp"
11#include "Teuchos_XMLParameterListHelpers.hpp"
12#include "Teuchos_TimeMonitor.hpp"
13
14#include "Tempus_config.hpp"
15#include "Tempus_IntegratorBasic.hpp"
16
17#include "Tempus_StepperFactory.hpp"
18#include "Tempus_StepperNewmarkImplicitAForm.hpp"
19#include "Tempus_StepperNewmarkImplicitDForm.hpp"
20
21#include "../TestModels/HarmonicOscillatorModel.hpp"
22#include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
23
24#include <fstream>
25#include <limits>
26#include <sstream>
27#include <vector>
28
29namespace Tempus_Test {
30
31using Teuchos::getParametersFromXmlFile;
32using Teuchos::ParameterList;
33using Teuchos::RCP;
34using Teuchos::rcp_const_cast;
35using Teuchos::rcp_dynamic_cast;
36using Teuchos::sublist;
37
41
42// ************************************************************
43TEUCHOS_UNIT_TEST(NewmarkExplicitAForm, HarmonicOscillatorDamped)
44{
45 RCP<Tempus::IntegratorBasic<double>> integrator;
46 std::vector<RCP<Thyra::VectorBase<double>>> solutions;
47 std::vector<RCP<Thyra::VectorBase<double>>> solutionsDot;
48 std::vector<double> StepSize;
49 std::vector<double> xErrorNorm;
50 std::vector<double> xDotErrorNorm;
51 const int nTimeStepSizes = 9;
52 double time = 0.0;
53
54 // Read params from .xml file
55 RCP<ParameterList> pList = getParametersFromXmlFile(
56 "Tempus_Test_NewmarkExplicitAForm_HarmonicOscillator_Damped.xml");
57
58 // Setup the HarmonicOscillatorModel
59 RCP<ParameterList> hom_pl = sublist(pList, "HarmonicOscillatorModel", true);
60 RCP<HarmonicOscillatorModel<double>> model =
61 Teuchos::rcp(new HarmonicOscillatorModel<double>(hom_pl));
62
63 // Setup the Integrator and reset initial time step
64 RCP<ParameterList> pl = sublist(pList, "Tempus", true);
65 RCP<ParameterList> stepperPL = sublist(pl, "Default Stepper", true);
66 stepperPL->remove("Zero Initial Guess");
67
68 // Set initial time step = 2*dt specified in input file (for convergence
69 // study)
70 //
71 double dt = pl->sublist("Default Integrator")
72 .sublist("Time Step Control")
73 .get<double>("Initial Time Step");
74 dt *= 2.0;
75
76 for (int n = 0; n < nTimeStepSizes; n++) {
77 // Perform time-step refinement
78 dt /= 2;
79 out << "\n \n time step #" << n << " (out of " << nTimeStepSizes - 1
80 << "), dt = " << dt << "\n";
81 pl->sublist("Default Integrator")
82 .sublist("Time Step Control")
83 .set("Initial Time Step", dt);
84 integrator = Tempus::createIntegratorBasic<double>(pl, model);
85
86 // Integrate to timeMax
87 bool integratorStatus = integrator->advanceTime();
88 TEST_ASSERT(integratorStatus)
89
90 // Test if at 'Final Time'
91 time = integrator->getTime();
92 double timeFinal = pl->sublist("Default Integrator")
93 .sublist("Time Step Control")
94 .get<double>("Final Time");
95 TEST_FLOATING_EQUALITY(time, timeFinal, 1.0e-14);
96
97 // Plot sample solution and exact solution
98 if (n == 0) {
99 RCP<const SolutionHistory<double>> solutionHistory =
100 integrator->getSolutionHistory();
102 "Tempus_Test_NewmarkExplicitAForm_HarmonicOscillator_Damped.dat",
103 solutionHistory);
104
105 RCP<Tempus::SolutionHistory<double>> solnHistExact =
106 Teuchos::rcp(new Tempus::SolutionHistory<double>());
107 for (int i = 0; i < solutionHistory->getNumStates(); i++) {
108 double time_i = (*solutionHistory)[i]->getTime();
109 RCP<Tempus::SolutionState<double>> 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 }
118 "Tempus_Test_NewmarkExplicitAForm_HarmonicOscillator_Damped-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_NewmarkExplicitAForm_HarmonicOscillator_Damped-Error.dat",
150 stepper, StepSize, solutions, xErrorNorm, xSlope, solutionsDot,
151 xDotErrorNorm, xDotSlope, out);
152
153 TEST_FLOATING_EQUALITY(xSlope, 1.060930, 0.01);
154 TEST_FLOATING_EQUALITY(xErrorNorm[0], 0.508229, 1.0e-4);
155 TEST_FLOATING_EQUALITY(xDotSlope, 1.019300, 0.01);
156 TEST_FLOATING_EQUALITY(xDotErrorNorm[0], 0.172900, 1.0e-4);
157
158 Teuchos::TimeMonitor::summarize();
159}
160
161} // namespace Tempus_Test
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Solution state for integrators and steppers.
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar > > solutionHistory)
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)
TEUCHOS_UNIT_TEST(BackwardEuler, SinCos_ASA)
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.