Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
02_Use_ModelEvaluator.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
23
24
25using namespace std;
26using Teuchos::RCP;
27
76int main(int /*argc*/, char * /*argv*/[])
77{
78 bool verbose = true;
79 bool success = false;
80 try {
81 // Construct ModelEvaluator
82 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
83 model = Teuchos::rcp(new VanDerPol_ModelEvaluator_02<double>());
84
85 // Get initial conditions from ModelEvaluator::getNominalValues.
86 int n = 0;
87 double time = 0.0;
88 bool passed = true; // ICs are considered passed.
89 RCP<Thyra::VectorBase<double> > x_n =
90 model->getNominalValues().get_x()->clone_v();
91 RCP<Thyra::VectorBase<double> > xDot_n =
92 model->getNominalValues().get_x_dot()->clone_v();
93
94 // Timestep size
95 double finalTime = 2.0;
96 int nTimeSteps = 2000;
97 const double constDT = finalTime/nTimeSteps;
98
99 // Output
100 cout << std::fixed;
101 cout << std::setw(8) << "index"
102 << std::setw(10) << "time"
103 << std::setw(12) << "x_0"
104 << std::setw(12) << "x_1" << endl;
105
106 cout << std::setw(8) << n
107 << std::setw(10) << std::setprecision(3) << time
108 << std::setw(12) << std::setprecision(4) << get_ele(*x_n, 0)
109 << std::setw(12) << std::setprecision(4) << get_ele(*x_n, 1) << endl;
110
111 // Advance the solution to the next timestep.
112 while (passed && time < finalTime && n < nTimeSteps) {
113
114 // Initialize next time step
115 RCP<Thyra::VectorBase<double> > x_np1 = x_n->clone_v(); // at time index n+1
116
117 // Set the timestep and time.
118 double dt = constDT;
119 time = (n+1)*dt;
120
121 // For explicit ODE formulation, xDot = f(x, t),
122 // xDot is part of the outArgs.
123 auto inArgs = model->createInArgs();
124 auto outArgs = model->createOutArgs();
125 inArgs.set_t(time);
126 inArgs.set_x(x_n);
127 inArgs.set_x_dot(Teuchos::null);
128 outArgs.set_f(xDot_n);
129
130 // Righthand side evaluation and time-derivative at n.
131 model->evalModel(inArgs, outArgs);
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.
ModelEvaluator implementation for the example van der Pol Problem.