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: 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#include "Thyra_VectorStdOps.hpp"
17#include "Thyra_DefaultSpmdVectorSpace.hpp"
18#include "Thyra_DetachedVectorView.hpp"
19
20
21using namespace std;
22using Teuchos::RCP;
23
59int main(int argc, char *argv[])
60{
61 bool verbose = true;
62 bool success = false;
63 try {
64 // Solution and its time-derivative.
65 int vectorLength = 2; // number state unknowns
66 RCP<const Thyra::VectorSpaceBase<double> > xSpace =
67 Thyra::defaultSpmdVectorSpace<double>(vectorLength);
68
69 RCP<Thyra::VectorBase<double> > x_n = Thyra::createMember(xSpace);
70 RCP<Thyra::VectorBase<double> > xDot_n = Thyra::createMember(xSpace);
71
72 // Initial Conditions
73 int n = 0;
74 double time = 0.0;
75 double epsilon = 1.0e-1;
76 bool passed = true; // ICs are considered passed.
77 { // Scope to delete DetachedVectorViews
78 Thyra::DetachedVectorView<double> x_n_view(*x_n);
79 x_n_view[0] = 2.0;
80 x_n_view[1] = 0.0;
81 Thyra::DetachedVectorView<double> xDot_n_view(*xDot_n);
82 xDot_n_view[0] = 0.0;
83 xDot_n_view[1] = -2.0/epsilon;
84 }
85
86 // Timestep size
87 double finalTime = 2.0;
88 int nTimeSteps = 2000;
89 const double constDT = finalTime/nTimeSteps;
90
91 // Advance the solution to the next timestep.
92 cout << n << " " << time << " " << get_ele(*(x_n), 0)
93 << " " << get_ele(*(x_n), 1) << endl;
94 while (passed && time < finalTime && n < nTimeSteps) {
95
96 // Initialize next time step
97 RCP<Thyra::VectorBase<double> > x_np1 = x_n->clone_v(); // at time index n+1
98
99 // Set the timestep and time.
100 double dt = constDT;
101 time = (n+1)*dt;
102
103 // Righthand side evaluation and time-derivative at n.
104 {
105 Thyra::ConstDetachedVectorView<double> x_n_view(*x_n);
106 Thyra::DetachedVectorView<double> xDot_n_view(*xDot_n);
107 xDot_n_view[0] = x_n_view[1];
108 xDot_n_view[1] =
109 ((1.0-x_n_view[0]*x_n_view[0])*x_n_view[1]-x_n_view[0])/epsilon;
110 }
111
112 // Take the timestep - Forward Euler
113 Thyra::V_VpStV(x_np1.ptr(), *x_n, dt, *xDot_n);
114
115 // Test if solution has passed.
116 if ( std::isnan(Thyra::norm(*x_np1)) ) {
117 passed = false;
118 } else {
119 // Promote to next step (n <- n+1).
120 Thyra::V_V(x_n.ptr(), *x_np1);
121 n++;
122 }
123
124 // Output
125 if ( n%100 == 0 )
126 cout << n << " " << time << " " << get_ele(*(x_n), 0)
127 << " " << get_ele(*(x_n), 1) << endl;
128 }
129
130 // Test for regression.
131 RCP<Thyra::VectorBase<double> > x_regress = x_n->clone_v();
132 {
133 Thyra::DetachedVectorView<double> x_regress_view(*x_regress);
134 x_regress_view[0] = -1.59496108218721311;
135 x_regress_view[1] = 0.96359412806611255;
136 }
137
138 RCP<Thyra::VectorBase<double> > x_error = x_n->clone_v();
139 Thyra::V_VmV(x_error.ptr(), *x_n, *x_regress);
140 double x_L2norm_error = Thyra::norm_2(*x_error );
141 double x_L2norm_regress = Thyra::norm_2(*x_regress);
142
143 cout << "Relative L2 Norm of the error (regression) = "
144 << x_L2norm_error/x_L2norm_regress << endl;
145 if ( x_L2norm_error > 1.0e-08*x_L2norm_regress) {
146 passed = false;
147 cout << "FAILED regression constraint!" << endl;
148 }
149 if (passed) success = true;
150 }
151 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
152
153 if(success)
154 cout << "\nEnd Result: Test Passed!" << std::endl;
155
156 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
157}
int main(int argc, char *argv[])