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