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
214int main(int argc, char *argv[])
215{
216 bool verbose = true;
217 bool success = false;
218 try {
219 // Solution and its time-derivative.
220 int vectorLength = 2; // number state unknowns
221 RCP<const Thyra::VectorSpaceBase<double> > xSpace =
222 Thyra::defaultSpmdVectorSpace<double>(vectorLength);
223
224 RCP<Thyra::VectorBase<double> > x_n = Thyra::createMember(xSpace);
225 RCP<Thyra::VectorBase<double> > xDot_n = Thyra::createMember(xSpace);
226
227 // Initial Conditions
228 int n = 0;
229 double time = 0.0;
230 double epsilon = 1.0e-1;
231 bool passed = true; // ICs are considered passed.
232 { // Scope to delete DetachedVectorViews
233 Thyra::DetachedVectorView<double> x_n_view(*x_n);
234 x_n_view[0] = 2.0;
235 x_n_view[1] = 0.0;
236 Thyra::DetachedVectorView<double> xDot_n_view(*xDot_n);
237 xDot_n_view[0] = 0.0;
238 xDot_n_view[1] = -2.0/epsilon;
239 }
240
241 // Timestep size
242 double finalTime = 2.0;
243 int nTimeSteps = 2001;
244 const double constDT = finalTime/(nTimeSteps-1);
245
246 // Advance the solution to the next timestep.
247 cout << n << " " << time << " " << get_ele(*(x_n), 0)
248 << " " << get_ele(*(x_n), 1) << endl;
249 while (passed && time < finalTime && n < nTimeSteps) {
250
251 // Initialize next time step
252 RCP<Thyra::VectorBase<double> > x_np1 = x_n->clone_v(); // at time index n+1
253
254 // Set the timestep and time.
255 double dt = constDT;
256 time = (n+1)*dt;
257
258 // Righthand side evaluation and time-derivative at n.
259 {
260 Thyra::ConstDetachedVectorView<double> x_n_view(*x_n);
261 Thyra::DetachedVectorView<double> xDot_n_view(*xDot_n);
262 xDot_n_view[0] = x_n_view[1];
263 xDot_n_view[1] =
264 ((1.0-x_n_view[0]*x_n_view[0])*x_n_view[1]-x_n_view[0])/epsilon;
265 }
266
267 // Take the timestep - Forward Euler
268 Thyra::V_VpStV(x_np1.ptr(), *x_n, dt, *xDot_n);
269
270 // Test if solution has passed.
271 if ( std::isnan(Thyra::norm(*x_np1)) ) {
272 passed = false;
273 } else {
274 // Promote to next step (n <- n+1).
275 Thyra::V_V(x_n.ptr(), *x_np1);
276 n++;
277 }
278
279 // Output
280 if ( n%100 == 0 )
281 cout << n << " " << time << " " << get_ele(*(x_n), 0)
282 << " " << get_ele(*(x_n), 1) << endl;
283 }
284
285 // Test for regression.
286 RCP<Thyra::VectorBase<double> > x_regress = x_n->clone_v();
287 {
288 Thyra::DetachedVectorView<double> x_regress_view(*x_regress);
289 x_regress_view[0] = -1.59496108218721311;
290 x_regress_view[1] = 0.96359412806611255;
291 }
292
293 RCP<Thyra::VectorBase<double> > x_error = x_n->clone_v();
294 Thyra::V_VmV(x_error.ptr(), *x_n, *x_regress);
295 double x_L2norm_error = Thyra::norm_2(*x_error );
296 double x_L2norm_regress = Thyra::norm_2(*x_regress);
297
298 cout << "Relative L2 Norm of the error (regression) = "
299 << x_L2norm_error/x_L2norm_regress << endl;
300 if ( x_L2norm_error > 1.0e-08*x_L2norm_regress) {
301 passed = false;
302 cout << "FAILED regression constraint!" << endl;
303 }
304
305 RCP<Thyra::VectorBase<double> > x_best = x_n->clone_v();
306 {
307 Thyra::DetachedVectorView<double> x_best_view(*x_best);
308 x_best_view[0] = -1.59496108218721311;
309 x_best_view[1] = 0.96359412806611255;
310 }
311
312 Thyra::V_VmV(x_error.ptr(), *x_n, *x_best);
313 x_L2norm_error = Thyra::norm_2(*x_error);
314 double x_L2norm_best = Thyra::norm_2(*x_best );
315
316 cout << "Relative L2 Norm of the error (best) = "
317 << x_L2norm_error/x_L2norm_best << endl;
318 if ( x_L2norm_error > 0.02*x_L2norm_best) {
319 passed = false;
320 cout << "FAILED best constraint!" << endl;
321 }
322 if (passed) success = true;
323 }
324 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
325
326 if(success)
327 cout << "\nEnd Result: Test Passed!" << std::endl;
328
329 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
330}
int main(int argc, char *argv[])
Description: