Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
06_Intro_TimeStepControl.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#include "Tempus_TimeStepControl.hpp"
29
30using namespace std;
31using Teuchos::RCP;
32
91int main(int argc, char *argv[])
92{
93 bool verbose = true;
94 bool success = false;
95 try {
96 // Construct ModelEvaluator
97 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
98 model = Teuchos::rcp(new VanDerPol_ModelEvaluator_02<double>());
99
100 // Setup initial condition SolutionState
101 auto solState = Tempus::createSolutionStateX(
102 model->getNominalValues().get_x()->clone_v());
103 solState->setIndex (0);
104 solState->setTime (0.0);
105 solState->setTimeStep(0.0); // By convention, the IC has dt = 0.
106 solState->setSolutionStatus(Tempus::Status::PASSED); // ICs are considered passed.
107
108 // Create SolutionHistory
109 auto solHistory = Tempus::createSolutionHistoryState<double>(solState);
110
111 // Create and initialize StepperForwardEuler
112 auto stepper = Teuchos::rcp(new Tempus::StepperForwardEuler<double>());
113 stepper->setModel(model);
114 stepper->initialize();
115 stepper->setInitialConditions(solHistory);
116
117 // Create and initialize TimeStepControl
118 auto timeStepControl = Teuchos::rcp(new Tempus::TimeStepControl<double>());
119 timeStepControl->setFinalTime(2.0);
120 timeStepControl->setNumTimeSteps(2000);
121 timeStepControl->initialize();
122
123 Tempus::Status integratorStatus = Tempus::Status::WORKING;
124
125 // Output
126 cout << std::fixed;
127 cout << std::setw(8) << "index"
128 << std::setw(10) << "time"
129 << std::setw(12) << "x_0"
130 << std::setw(12) << "x_1" << endl;
131
132 auto currentState = solHistory->getCurrentState();
133 cout << std::setw(8) << currentState->getIndex()
134 << std::setw(10) << std::setprecision(3) << currentState->getTime()
135 << std::setw(12) << std::setprecision(4) << Thyra::get_ele(*(currentState->getX()), 0)
136 << std::setw(12) << std::setprecision(4) << Thyra::get_ele(*(currentState->getX()), 1)
137 << endl;
138
139 // Advance the solution to the next timestep.
140 while (solHistory->getCurrentState()->getSolutionStatus() == Tempus::Status::PASSED &&
141 timeStepControl->timeInRange(solHistory->getCurrentTime()) &&
142 timeStepControl->indexInRange(solHistory->getCurrentIndex())) {
143
144 // Initialize next time step using SolutionHistory
145 solHistory->initWorkingState();
146
147 // Let TimeStepControl determine the next time-step metadata.
148 timeStepControl->setNextTimeStep(solHistory, integratorStatus);
149
150 // Take one Forward Euler step through Tempus::StepperForwardEuler
151 stepper->takeStep(solHistory);
152
153 // Promote working state to current state
154 solHistory->promoteWorkingState();
155
156 // Output
157 if (solHistory->getCurrentState()->getIndex() % 100 == 0) {
158 currentState = solHistory->getCurrentState();
159 cout << std::setw(8) << currentState->getIndex()
160 << std::setw(10) << std::setprecision(3) << currentState->getTime()
161 << std::setw(12) << std::setprecision(4) << Thyra::get_ele(*(currentState->getX()), 0)
162 << std::setw(12) << std::setprecision(4) << Thyra::get_ele(*(currentState->getX()), 1)
163 << endl;
164 }
165 }
166
167 // Test for regression.
168 auto finalState = solHistory->getCurrentState();
169 bool passed = (finalState->getSolutionStatus() == Tempus::Status::PASSED);
170 bool regressionPassed = tutorialRegressionTest(finalState);
171
172 if (passed && regressionPassed) success = true;
173 }
174 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
175
176 if(success)
177 cout << "\nEnd Result: Test Passed!" << std::endl;
178
179 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
180}
int main(int argc, char *argv[])
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.
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
ModelEvaluator implementation for the example van der Pol Problem.
Status
Status for the Integrator, the Stepper and the SolutionState.
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.