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 "../00_Basic_Problem/Tutorial_Regression_Tester.hpp"
17
18#include "Thyra_VectorStdOps.hpp"
19#include "Thyra_DefaultSpmdVectorSpace.hpp"
20#include "Thyra_DetachedVectorView.hpp"
21
22
23using namespace std;
24using Teuchos::RCP;
25
70int main(int /*argc*/, char * /*argv*/[])
71{
72 bool verbose = true;
73 bool success = false;
74 try {
75 // Solution and its time-derivative.
76 int vectorLength = 2; // number state unknowns
77 RCP<const Thyra::VectorSpaceBase<double> > xSpace =
78 Thyra::defaultSpmdVectorSpace<double>(vectorLength);
79
80 RCP<Thyra::VectorBase<double> > x_n = Thyra::createMember(xSpace);
81 RCP<Thyra::VectorBase<double> > xDot_n = Thyra::createMember(xSpace);
82
83 // Initial Conditions
84 int n = 0;
85 double time = 0.0;
86 double epsilon = 1.0e-1;
87 bool passed = true; // ICs are considered passed.
88 { // Scope to delete DetachedVectorViews
89 Thyra::DetachedVectorView<double> x_n_view(*x_n);
90 x_n_view[0] = 2.0;
91 x_n_view[1] = 0.0;
92 Thyra::DetachedVectorView<double> xDot_n_view(*xDot_n);
93 xDot_n_view[0] = 0.0;
94 xDot_n_view[1] = -2.0/epsilon;
95 }
96
97 // Timestep size
98 double finalTime = 2.0;
99 int nTimeSteps = 2000;
100 const double constDT = finalTime/nTimeSteps;
101
102 // Output
103 cout << std::fixed;
104 cout << std::setw(8) << "index"
105 << std::setw(10) << "time"
106 << std::setw(12) << "x_0"
107 << std::setw(12) << "x_1" << endl;
108
109 cout << std::setw(8) << n
110 << std::setw(10) << std::setprecision(3) << time
111 << std::setw(12) << std::setprecision(4) << get_ele(*x_n, 0)
112 << std::setw(12) << std::setprecision(4) << get_ele(*x_n, 1) << endl;
113
114 // Advance the solution to the next timestep.
115 while (passed && time < finalTime && n < nTimeSteps) {
116
117 // Initialize next time step
118 RCP<Thyra::VectorBase<double> > x_np1 = x_n->clone_v(); // at time index n+1
119
120 // Set the timestep and time.
121 double dt = constDT;
122 time = (n+1)*dt;
123
124 // Righthand side evaluation and time-derivative at n.
125 {
126 Thyra::ConstDetachedVectorView<double> x_n_view(*x_n);
127 Thyra::DetachedVectorView<double> xDot_n_view(*xDot_n);
128 xDot_n_view[0] = x_n_view[1];
129 xDot_n_view[1] =
130 ((1.0-x_n_view[0]*x_n_view[0])*x_n_view[1]-x_n_view[0])/epsilon;
131 }
132
133 // Take the timestep - Forward Euler
134 Thyra::V_VpStV(x_np1.ptr(), *x_n, dt, *xDot_n);
135
136 // Test if solution has passed.
137 if ( std::isnan(Thyra::norm(*x_np1)) ) {
138 passed = false;
139 } else {
140 // Promote to next step (n <- n+1).
141 Thyra::V_V(x_n.ptr(), *x_np1);
142 n++;
143 }
144
145 // Output
146 if (n % 100 == 0)
147 cout << std::setw(8) << n
148 << std::setw(10) << std::setprecision(3) << time
149 << std::setw(12) << std::setprecision(4) << get_ele(*x_n, 0)
150 << std::setw(12) << std::setprecision(4) << get_ele(*x_n, 1) << endl;
151 }
152
153 // Test for regression.
154 bool regressionPassed = tutorialRegressionTest(x_n);
155
156 if (passed && regressionPassed) success = true;
157 }
158 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
159
160 if(success)
161 cout << "\nEnd Result: Test Passed!" << std::endl;
162
163 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
164}
int main(int, char *[])
bool tutorialRegressionTest(const Teuchos::RCP< Thyra::VectorBase< double > > &x_n, std::ostream &out=std::cout, const double relTol=Teuchos::as< double >(1.0e-8))
Regression check for the Tempus tutorial van der Pol examples.