Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
01_Utilize_Thyra.cpp
Go to the documentation of this file.
1// @HEADER
2// ****************************************************************************
3// Tempus: Copyright (2017) Sandia Corporation
4//
5// Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6// ****************************************************************************
7// @HEADER
8
9#include <iomanip>
10#include <iostream>
11#include <stdlib.h>
12#include <math.h>
13#include "Teuchos_StandardCatchMacros.hpp"
14
15#include "Thyra_VectorStdOps.hpp"
16#include "Thyra_DefaultSpmdVectorSpace.hpp"
17#include "Thyra_DetachedVectorView.hpp"
18
19
20using namespace std;
21using Teuchos::RCP;
22
24
213int main(int argc, char *argv[])
214{
215 bool verbose = true;
216 bool success = false;
217 try {
218 // Solution and its time-derivative.
219 int vectorLength = 2; // number state unknowns
220 RCP<const Thyra::VectorSpaceBase<double> > xSpace =
221 Thyra::defaultSpmdVectorSpace<double>(vectorLength);
222
223 RCP<Thyra::VectorBase<double> > x_n = Thyra::createMember(xSpace);
224 RCP<Thyra::VectorBase<double> > xDot_n = Thyra::createMember(xSpace);
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 { // Scope to delete DetachedVectorViews
232 Thyra::DetachedVectorView<double> x_n_view(*x_n);
233 x_n_view[0] = 2.0;
234 x_n_view[1] = 0.0;
235 Thyra::DetachedVectorView<double> xDot_n_view(*xDot_n);
236 xDot_n_view[0] = 0.0;
237 xDot_n_view[1] = -2.0/epsilon;
238 }
239
240 // Timestep size
241 double finalTime = 2.0;
242 int nTimeSteps = 2001;
243 const double constDT = finalTime/(nTimeSteps-1);
244
245 // Advance the solution to the next timestep.
246 cout << n << " " << time << " " << get_ele(*(x_n), 0)
247 << " " << get_ele(*(x_n), 1) << endl;
248 while (passed && time < finalTime && n < nTimeSteps) {
249
250 // Initialize next time step
251 RCP<Thyra::VectorBase<double> > x_np1 = x_n->clone_v(); // at time index n+1
252
253 // Set the timestep and time.
254 double dt = constDT;
255 time = (n+1)*dt;
256
257 // Righthand side evaluation and time-derivative at n.
258 {
259 Thyra::ConstDetachedVectorView<double> x_n_view(*x_n);
260 Thyra::DetachedVectorView<double> xDot_n_view(*xDot_n);
261 xDot_n_view[0] = x_n_view[1];
262 xDot_n_view[1] =
263 ((1.0-x_n_view[0]*x_n_view[0])*x_n_view[1]-x_n_view[0])/epsilon;
264 }
265
266 // Take the timestep - Forward Euler
267 Thyra::V_VpStV(x_np1.ptr(), *x_n, dt, *xDot_n);
268
269 // Test if solution has passed.
270 if ( std::isnan(Thyra::norm(*x_np1)) ) {
271 passed = false;
272 } else {
273 // Promote to next step (n <- n+1).
274 Thyra::V_V(x_n.ptr(), *x_np1);
275 n++;
276 }
277
278 // Output
279 if ( n%100 == 0 )
280 cout << n << " " << time << " " << get_ele(*(x_n), 0)
281 << " " << get_ele(*(x_n), 1) << endl;
282 }
283
284 // Test for regression.
285 RCP<Thyra::VectorBase<double> > x_regress = x_n->clone_v();
286 {
287 Thyra::DetachedVectorView<double> x_regress_view(*x_regress);
288 x_regress_view[0] = -1.59496108218721311;
289 x_regress_view[1] = 0.96359412806611255;
290 }
291
292 RCP<Thyra::VectorBase<double> > x_error = x_n->clone_v();
293 Thyra::V_VmV(x_error.ptr(), *x_n, *x_regress);
294 double x_L2norm_error = Thyra::norm_2(*x_error );
295 double x_L2norm_regress = Thyra::norm_2(*x_regress);
296
297 cout << "Relative L2 Norm of the error (regression) = "
298 << x_L2norm_error/x_L2norm_regress << endl;
299 if ( x_L2norm_error > 1.0e-08*x_L2norm_regress) {
300 passed = false;
301 cout << "FAILED regression constraint!" << endl;
302 }
303
304 RCP<Thyra::VectorBase<double> > x_best = x_n->clone_v();
305 {
306 Thyra::DetachedVectorView<double> x_best_view(*x_best);
307 x_best_view[0] = -1.59496108218721311;
308 x_best_view[1] = 0.96359412806611255;
309 }
310
311 Thyra::V_VmV(x_error.ptr(), *x_n, *x_best);
312 x_L2norm_error = Thyra::norm_2(*x_error);
313 double x_L2norm_best = Thyra::norm_2(*x_best );
314
315 cout << "Relative L2 Norm of the error (best) = "
316 << x_L2norm_error/x_L2norm_best << endl;
317 if ( x_L2norm_error > 0.02*x_L2norm_best) {
318 passed = false;
319 cout << "FAILED best constraint!" << endl;
320 }
321 if (passed) success = true;
322 }
323 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
324
325 if(success)
326 cout << "\nEnd Result: Test Passed!" << std::endl;
327
328 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
329}
int main(int argc, char *argv[])
Description: