Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
05_Intro_Stepper.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 <iomanip>
11#include <iostream>
12#include <stdlib.h>
13#include <math.h>
14#include "Teuchos_StandardCatchMacros.hpp"
15
16#include "../00_Basic_Problem/Tutorial_Regression_Tester.hpp"
17
18#include "Thyra_VectorStdOps.hpp"
19#include "Thyra_DefaultSpmdVectorSpace.hpp"
20#include "Thyra_DetachedVectorView.hpp"
21
22#include "../02_Use_ModelEvaluator/VanDerPol_ModelEvaluator_02.hpp"
23
24#include "Tempus_SolutionState.hpp"
25#include "Tempus_SolutionHistory.hpp"
26#include "Tempus_Stepper.hpp"
27#include "Tempus_StepperForwardEuler.hpp"
28
29
30using namespace std;
31using Teuchos::RCP;
32
86int main(int /*argc*/, char * /*argv*/[])
87{
88 bool verbose = true;
89 bool success = false;
90 try {
91 // Construct ModelEvaluator
92 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
93 model = Teuchos::rcp(new VanDerPol_ModelEvaluator_02<double>());
94
95 // Setup initial condition SolutionState
96 auto solState = Tempus::createSolutionStateX(
97 model->getNominalValues().get_x()->clone_v());
98 solState->setIndex (0);
99 solState->setTime (0.0);
100 solState->setTimeStep(0.0); // By convention, the IC has dt = 0.
101 solState->setSolutionStatus(Tempus::Status::PASSED); // ICs are considered passed.
102
103 // Create SolutionHistory
104 auto solHistory = Tempus::createSolutionHistoryState<double>(solState);
105
106 // Create and initialize StepperForwardEuler
107 auto stepper = Teuchos::rcp(new Tempus::StepperForwardEuler<double>());
108 stepper->setModel(model);
109 stepper->initialize();
110 stepper->setInitialConditions(solHistory);
111
112 // Timestep size
113 double finalTime = 2.0;
114 int nTimeSteps = 2000;
115 const double constDT = finalTime/nTimeSteps;
116
117 // Output
118 cout << std::fixed;
119 cout << std::setw(8) << "index"
120 << std::setw(10) << "time"
121 << std::setw(12) << "x_0"
122 << std::setw(12) << "x_1" << endl;
123
124 auto currentState = solHistory->getCurrentState();
125 cout << std::setw(8) << currentState->getIndex()
126 << std::setw(10) << std::setprecision(3) << currentState->getTime()
127 << std::setw(12) << std::setprecision(4) << Thyra::get_ele(*(currentState->getX()), 0)
128 << std::setw(12) << std::setprecision(4) << Thyra::get_ele(*(currentState->getX()), 1)
129 << endl;
130
131 // Advance the solution to the next timestep.
132 while (solHistory->getCurrentState()->getSolutionStatus() == Tempus::Status::PASSED &&
133 solHistory->getCurrentTime() < finalTime &&
134 solHistory->getCurrentIndex() < nTimeSteps ) {
135
136 // Initialize next time step using SolutionHistory
137 solHistory->initWorkingState();
138 auto workingState = solHistory->getWorkingState();
139
140 // Set the timestep and time for the working solution, i.e., n+1.
141 int index = workingState->getIndex(); // Already incremented by initWorkingState()
142 double dt = constDT;
143 double time = index*dt;
144 workingState->setTime(time);
145 workingState->setTimeStep(dt);
146
147 // Take one Forward Euler step through Tempus::StepperForwardEuler
148 stepper->takeStep(solHistory);
149
150 // Promote working state to current state
151 solHistory->promoteWorkingState();
152
153 // Output
154 if (solHistory->getCurrentState()->getIndex() % 100 == 0) {
155 currentState = solHistory->getCurrentState();
156 cout << std::setw(8) << currentState->getIndex()
157 << std::setw(10) << std::setprecision(3) << currentState->getTime()
158 << std::setw(12) << std::setprecision(4) << Thyra::get_ele(*(currentState->getX()), 0)
159 << std::setw(12) << std::setprecision(4) << Thyra::get_ele(*(currentState->getX()), 1)
160 << endl;
161 }
162 }
163
164 // Test for regression.
165 auto finalState = solHistory->getCurrentState();
166 bool passed = (finalState->getSolutionStatus() == Tempus::Status::PASSED);
167 bool regressionPassed = tutorialRegressionTest(finalState);
168
169 if (passed && regressionPassed) success = true;
170 }
171 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
172
173 if(success)
174 cout << "\nEnd Result: Test Passed!" << std::endl;
175
176 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
177}
int main(int, char *[])
bool tutorialRegressionTest(const Teuchos::RCP< Thyra::VectorBase< double > > &x_n, std::ostream &out=std::cout, const double relTol=Teuchos::as< double >(1.0e-8))
Regression check for the Tempus tutorial van der Pol examples.
ModelEvaluator implementation for the example van der Pol Problem.
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.