Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
00_Basic_Problem.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
17
18using namespace std;
19
87int main(int /*argc*/, char * /*argv*/[])
88{
89 bool verbose = true;
90 bool success = false;
91 try {
92 // Solution and its time-derivative.
93 double x_n[2]; // at time index n
94 double xDot_n[2]; // at time index n
95
96 // Initial Conditions
97 int n = 0;
98 double time = 0.0;
99 double epsilon = 1.0e-1;
100 bool passed = true; // ICs are considered passed.
101 x_n [0] = 2.0;
102 x_n [1] = 0.0;
103 xDot_n[0] = 0.0;
104 xDot_n[1] = -2.0/epsilon;
105
106 // Timestep size
107 double finalTime = 2.0;
108 int nTimeSteps = 2000;
109 const double constDT = finalTime/nTimeSteps;
110
111 // Output
112 cout << std::fixed;
113 cout << std::setw(8) << "index"
114 << std::setw(10) << "time"
115 << std::setw(12) << "x_0"
116 << std::setw(12) << "x_1" << endl;
117
118 cout << std::setw(8) << n
119 << std::setw(10) << std::setprecision(3) << time
120 << std::setw(12) << std::setprecision(4) << x_n[0]
121 << std::setw(12) << std::setprecision(4) << x_n[1] << endl;
122
123 // Advance the solution to the next timestep.
124 while (passed && time < finalTime && n < nTimeSteps) {
125
126 // Initialize next time step
127 double x_np1[2]; // at time index n+1
128 x_np1[0] = x_n[0];
129 x_np1[1] = x_n[1];
130
131 // Set the timestep and time.
132 double dt = constDT;
133 time = (n+1)*dt;
134
135 // Righthand side evaluation and time-derivative at n.
136 xDot_n[0] = x_n[1];
137 xDot_n[1] = ((1.0 - x_n[0]*x_n[0])*x_n[1] - x_n[0])/epsilon;
138
139 // Take the timestep - Forward Euler
140 x_np1[0] = x_n[0] + dt*xDot_n[0];
141 x_np1[1] = x_n[1] + dt*xDot_n[1];
142
143 // Test if solution has passed.
144 if ( std::isnan(x_n[0]) || std::isnan(x_n[1]) ) {
145 passed = false;
146 } else {
147 // Promote to next step (n <- n+1).
148 x_n[0] = x_np1[0];
149 x_n[1] = x_np1[1];
150 n++;
151 }
152
153 // Output
154 if (n % 100 == 0)
155 cout << std::setw(8) << n
156 << std::setw(10) << std::setprecision(3) << time
157 << std::setw(12) << std::setprecision(4) << x_n[0]
158 << std::setw(12) << std::setprecision(4) << x_n[1] << endl;
159 }
160
161 // Test for regression.
162 bool regressionPassed = tutorialRegressionTest(x_n);
163
164 if (passed && regressionPassed) success = true;
165 }
166 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
167
168 if(success)
169 cout << "\nEnd Result: Test Passed!" << std::endl;
170
171 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
172}
int main(int, char *[])
Regression utilities for the Tempus progression tutorials.
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.