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