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 "Thyra_VectorStdOps.hpp"
17#include "Thyra_DefaultSpmdVectorSpace.hpp"
18#include "Thyra_DetachedVectorView.hpp"
19
20#include "../02_Use_ModelEvaluator/VanDerPol_ModelEvaluator_02.hpp"
21
22#include "Tempus_SolutionState.hpp"
23#include "Tempus_SolutionHistory.hpp"
24#include "Tempus_Stepper.hpp"
25#include "Tempus_StepperForwardEuler.hpp"
26
27
28using namespace std;
29using Teuchos::RCP;
30
77int main(int argc, char *argv[])
78{
79 bool verbose = true;
80 bool success = false;
81 try {
82 // Construct ModelEvaluator
83 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
84 model = Teuchos::rcp(new VanDerPol_ModelEvaluator_02<double>());
85
86 // Setup initial condition SolutionState
87 auto solState = Tempus::createSolutionStateX(
88 model->getNominalValues().get_x()->clone_v());
89 solState->setIndex (0);
90 solState->setTime (0.0);
91 solState->setTimeStep(0.0); // By convention, the IC has dt = 0.
92 solState->setSolutionStatus(Tempus::Status::PASSED); // ICs are considered passed.
93
94 // Create SolutionHistory
95 auto solHistory = Tempus::createSolutionHistoryState<double>(solState);
96
97 // Create and initialize StepperForwardEuler
98 auto stepper = Teuchos::rcp(new Tempus::StepperForwardEuler<double>());
99 stepper->setModel(model);
100 stepper->initialize();
101 stepper->setInitialConditions(solHistory);
102
103 // Timestep size
104 double finalTime = 2.0;
105 int nTimeSteps = 2000;
106 const double constDT = finalTime/nTimeSteps;
107
108 // Advance the solution to the next timestep.
109 while (solHistory->getCurrentState()->getSolutionStatus() == Tempus::Status::PASSED &&
110 solHistory->getCurrentState()->getTime() < finalTime &&
111 solHistory->getCurrentState()->getIndex() < nTimeSteps) {
112
113 // Initialize next time step using SolutionHistory
114 solHistory->initWorkingState();
115 auto workingState = solHistory->getWorkingState();
116
117 // Set the timestep and time for the working solution, i.e., n+1.
118 int index = workingState->getIndex(); // Already incremented by initWorkingState()
119 double dt = constDT;
120 double time = index*dt;
121 workingState->setTime(time);
122 workingState->setTimeStep(dt);
123
124 // Take one Forward Euler step through Tempus::StepperForwardEuler
125 stepper->takeStep(solHistory);
126
127 // Promote working state to current state
128 solHistory->promoteWorkingState();
129
130 // Output
131 if (solHistory->getCurrentState()->getIndex() % 100 == 0) {
132 auto currentState = solHistory->getCurrentState();
133 cout << currentState->getIndex() << " " << currentState->getTime()
134 << " " << Thyra::get_ele(*(currentState->getX()), 0)
135 << " " << Thyra::get_ele(*(currentState->getX()), 1) << endl;
136 }
137 }
138
139 // Test for regression.
140 auto finalState = solHistory->getCurrentState();
141 RCP<Thyra::VectorBase<double> > x_n = finalState->getX();
142 RCP<Thyra::VectorBase<double> > x_regress = x_n->clone_v();
143 {
144 Thyra::DetachedVectorView<double> x_regress_view(*x_regress);
145 x_regress_view[0] = -1.59496108218721311;
146 x_regress_view[1] = 0.96359412806611255;
147 }
148
149 RCP<Thyra::VectorBase<double> > x_error = x_n->clone_v();
150 Thyra::V_VmV(x_error.ptr(), *x_n, *x_regress);
151 double x_L2norm_error = Thyra::norm_2(*x_error );
152 double x_L2norm_regress = Thyra::norm_2(*x_regress);
153
154 cout << "Relative L2 Norm of the error (regression) = "
155 << x_L2norm_error/x_L2norm_regress << endl;
156 if ( x_L2norm_error > 1.0e-08*x_L2norm_regress) {
157 finalState->setSolutionStatus(Tempus::Status::FAILED);
158 cout << "FAILED regression constraint!" << endl;
159 }
160 if (finalState->getSolutionStatus() == Tempus::Status::PASSED) success = true;
161 }
162 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
163
164 if(success)
165 cout << "\nEnd Result: Test Passed!" << std::endl;
166
167 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
168}
int main(int argc, char *argv[])
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.