Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
16 using namespace std;
17 
217 int main(int argc, char *argv[])
218 {
219  bool verbose = true;
220  bool success = false;
221  try {
222  // Solution and its time-derivative.
223  double x_n[2]; // at time index n
224  double xDot_n[2]; // at time index n
225 
226  // Initial Conditions
227  int n = 0;
228  double time = 0.0;
229  double epsilon = 1.0e-1;
230  bool passed = true; // ICs are considered passed.
231  x_n [0] = 2.0;
232  x_n [1] = 0.0;
233  xDot_n[0] = 0.0;
234  xDot_n[1] = -2.0/epsilon;
235 
236  // Timestep size
237  double finalTime = 2.0;
238  int nTimeSteps = 2001;
239  const double constDT = finalTime/(nTimeSteps-1);
240 
241  // Advance the solution to the next timestep.
242  cout << n << " " << time << " " << x_n[0] << " " << x_n[1] << endl;
243  while (passed && time < finalTime && n < nTimeSteps) {
244 
245  // Initialize next time step
246  double x_np1[2]; // at time index n+1
247  x_np1[0] = x_n[0];
248  x_np1[1] = x_n[1];
249 
250  // Set the timestep and time.
251  double dt = constDT;
252  time = (n+1)*dt;
253 
254  // Righthand side evaluation and time-derivative at n.
255  xDot_n[0] = x_n[1];
256  xDot_n[1] = ((1.0 - x_n[0]*x_n[0])*x_n[1] - x_n[0])/epsilon;
257 
258  // Take the timestep - Forward Euler
259  x_np1[0] = x_n[0] + dt*xDot_n[0];
260  x_np1[1] = x_n[1] + dt*xDot_n[1];
261 
262  // Test if solution is passed.
263  if ( std::isnan(x_n[0]) || std::isnan(x_n[1]) ) {
264  passed = false;
265  } else {
266  // Promote to next step (n <- n+1).
267  x_n[0] = x_np1[0];
268  x_n[1] = x_np1[1];
269  n++;
270  }
271 
272  // Output
273  if ( n%100 == 0 )
274  cout << n << " " << time << " " << x_n[0] << " " << x_n[1] << endl;
275 
276  }
277 
278  // Test for regression.
279  double x_regress[2]; // Regression results for nTimeSteps = 2001
280  x_regress[0] = -1.59496108218721311;
281  x_regress[1] = 0.96359412806611255;
282  double x_L2norm_error = 0.0;
283  double x_L2norm_regress = 0.0;
284  for (int i=0; i < 2; i++) {
285  x_L2norm_error += (x_n[i]-x_regress[i])*(x_n[i]-x_regress[i]);
286  x_L2norm_regress += x_regress[1]*x_regress[1];
287  }
288  x_L2norm_error = sqrt(x_L2norm_error );
289  x_L2norm_regress = sqrt(x_L2norm_regress);
290  cout << "Relative L2 Norm of the error (regression) = "
291  << x_L2norm_error/x_L2norm_regress << endl;
292  if ( x_L2norm_error > 1.0e-08*x_L2norm_regress) {
293  passed = false;
294  cout << "FAILED regression constraint!" << endl;
295  }
296 
297  double x_best[2]; // Best resolution with nTimeSteps = 2000000001
298  x_best[0] = -1.58184083624543947;
299  x_best[1] = 0.97844890081968072;
300  x_L2norm_error = 0.0;
301  double x_L2norm_best = 0.0;
302  for (int i=0; i < 2; i++) {
303  x_L2norm_error += (x_n[i]-x_best[i])*(x_n[i]-x_best[i]);
304  x_L2norm_best += x_best[1]*x_best[1];
305  }
306  x_L2norm_error = sqrt(x_L2norm_error);
307  x_L2norm_best = sqrt(x_L2norm_best );
308  cout << "Relative L2 Norm of the error (best) = "
309  << x_L2norm_error/x_L2norm_best << endl;
310  if ( x_L2norm_error > 0.02*x_L2norm_best) {
311  passed = false;
312  cout << "FAILED best constraint!" << endl;
313  }
314  if (passed) success = true;
315  }
316  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
317 
318  if(success)
319  cout << "\nEnd Result: Test Passed!" << std::endl;
320 
321  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
322 }
int main(int argc, char *argv[])
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)