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
16using namespace std;
17
81int main(int argc, char *argv[])
82{
83 bool verbose = true;
84 bool success = false;
85 try {
86 // Solution and its time-derivative.
87 double x_n[2]; // at time index n
88 double xDot_n[2]; // at time index n
89
90 // Initial Conditions
91 int n = 0;
92 double time = 0.0;
93 double epsilon = 1.0e-1;
94 bool passed = true; // ICs are considered passed.
95 x_n [0] = 2.0;
96 x_n [1] = 0.0;
97 xDot_n[0] = 0.0;
98 xDot_n[1] = -2.0/epsilon;
99
100 // Timestep size
101 double finalTime = 2.0;
102 int nTimeSteps = 2001;
103 const double constDT = finalTime/(nTimeSteps-1);
104
105 // Advance the solution to the next timestep.
106 cout << n << " " << time << " " << x_n[0] << " " << x_n[1] << endl;
107 while (passed && time < finalTime && n < nTimeSteps) {
108
109 // Initialize next time step
110 double x_np1[2]; // at time index n+1
111 x_np1[0] = x_n[0];
112 x_np1[1] = x_n[1];
113
114 // Set the timestep and time.
115 double dt = constDT;
116 time = (n+1)*dt;
117
118 // Righthand side evaluation and time-derivative at n.
119 xDot_n[0] = x_n[1];
120 xDot_n[1] = ((1.0 - x_n[0]*x_n[0])*x_n[1] - x_n[0])/epsilon;
121
122 // Take the timestep - Forward Euler
123 x_np1[0] = x_n[0] + dt*xDot_n[0];
124 x_np1[1] = x_n[1] + dt*xDot_n[1];
125
126 // Test if solution has passed.
127 if ( std::isnan(x_n[0]) || std::isnan(x_n[1]) ) {
128 passed = false;
129 } else {
130 // Promote to next step (n <- n+1).
131 x_n[0] = x_np1[0];
132 x_n[1] = x_np1[1];
133 n++;
134 }
135
136 // Output
137 if ( n%100 == 0 )
138 cout << n << " " << time << " " << x_n[0] << " " << x_n[1] << endl;
139
140 }
141
142 // Test for regression.
143 double x_regress[2]; // Regression results for nTimeSteps = 2001
144 x_regress[0] = -1.59496108218721311;
145 x_regress[1] = 0.96359412806611255;
146 double x_L2norm_error = 0.0;
147 double x_L2norm_regress = 0.0;
148 for (int i=0; i < 2; i++) {
149 x_L2norm_error += (x_n[i]-x_regress[i])*(x_n[i]-x_regress[i]);
150 x_L2norm_regress += x_regress[1]*x_regress[1];
151 }
152 x_L2norm_error = sqrt(x_L2norm_error );
153 x_L2norm_regress = sqrt(x_L2norm_regress);
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 passed = false;
158 cout << "FAILED regression constraint!" << endl;
159 }
160
161 double x_best[2]; // Best resolution with nTimeSteps = 2000000001
162 x_best[0] = -1.58184083624543947;
163 x_best[1] = 0.97844890081968072;
164 x_L2norm_error = 0.0;
165 double x_L2norm_best = 0.0;
166 for (int i=0; i < 2; i++) {
167 x_L2norm_error += (x_n[i]-x_best[i])*(x_n[i]-x_best[i]);
168 x_L2norm_best += x_best[1]*x_best[1];
169 }
170 x_L2norm_error = sqrt(x_L2norm_error);
171 x_L2norm_best = sqrt(x_L2norm_best );
172 cout << "Relative L2 Norm of the error (best) = "
173 << x_L2norm_error/x_L2norm_best << endl;
174 if ( x_L2norm_error > 0.02*x_L2norm_best) {
175 passed = false;
176 cout << "FAILED best constraint!" << endl;
177 }
178 if (passed) success = true;
179 }
180 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
181
182 if(success)
183 cout << "\nEnd Result: Test Passed!" << std::endl;
184
185 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
186}
int main(int argc, char *argv[])