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 "Thyra_VectorStdOps.hpp"
17#include "Thyra_DefaultSpmdVectorSpace.hpp"
18#include "Thyra_DetachedVectorView.hpp"
19
21
22
23using namespace std;
24using Teuchos::RCP;
25
203int main(int argc, char *argv[])
204{
205 bool verbose = true;
206 bool success = false;
207 try {
208 // Construct ModelEvaluator
209 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
210 model = Teuchos::rcp(new VanDerPol_ModelEvaluator_02<double>());
211
212 // Get initial conditions from ModelEvaluator::getNominalValues.
213 int n = 0;
214 double time = 0.0;
215 bool passed = true; // ICs are considered passed.
216 RCP<Thyra::VectorBase<double> > x_n =
217 model->getNominalValues().get_x()->clone_v();
218 RCP<Thyra::VectorBase<double> > xDot_n =
219 model->getNominalValues().get_x_dot()->clone_v();
220
221 // Timestep size
222 double finalTime = 2.0;
223 int nTimeSteps = 2001;
224 const double constDT = finalTime/(nTimeSteps-1);
225
226 // Advance the solution to the next timestep.
227 cout << n << " " << time << " " << get_ele(*(x_n), 0)
228 << " " << get_ele(*(x_n), 1) << endl;
229 while (passed && time < finalTime && n < nTimeSteps) {
230
231 // Initialize next time step
232 RCP<Thyra::VectorBase<double> > x_np1 = x_n->clone_v(); // at time index n+1
233
234 // Set the timestep and time.
235 double dt = constDT;
236 time = (n+1)*dt;
237
238 // For explicit ODE formulation, xDot = f(x, t),
239 // xDot is part of the outArgs.
240 auto inArgs = model->createInArgs();
241 auto outArgs = model->createOutArgs();
242 inArgs.set_t(time);
243 inArgs.set_x(x_n);
244 inArgs.set_x_dot(Teuchos::null);
245 outArgs.set_f(xDot_n);
246
247 // Righthand side evaluation and time-derivative at n.
248 model->evalModel(inArgs, outArgs);
249
250 // Take the timestep - Forward Euler
251 Thyra::V_VpStV(x_np1.ptr(), *x_n, dt, *xDot_n);
252
253 // Test if solution has passed.
254 if ( std::isnan(Thyra::norm(*x_np1)) ) {
255 passed = false;
256 } else {
257 // Promote to next step (n <- n+1).
258 Thyra::V_V(x_n.ptr(), *x_np1);
259 n++;
260 }
261
262 // Output
263 if ( n%100 == 0 )
264 cout << n << " " << time << " " << get_ele(*(x_n), 0)
265 << " " << get_ele(*(x_n), 1) << endl;
266 }
267
268 // Test for regression.
269 RCP<Thyra::VectorBase<double> > x_regress = x_n->clone_v();
270 {
271 Thyra::DetachedVectorView<double> x_regress_view(*x_regress);
272 x_regress_view[0] = -1.59496108218721311;
273 x_regress_view[1] = 0.96359412806611255;
274 }
275
276 RCP<Thyra::VectorBase<double> > x_error = x_n->clone_v();
277 Thyra::V_VmV(x_error.ptr(), *x_n, *x_regress);
278 double x_L2norm_error = Thyra::norm_2(*x_error );
279 double x_L2norm_regress = Thyra::norm_2(*x_regress);
280
281 cout << "Relative L2 Norm of the error (regression) = "
282 << x_L2norm_error/x_L2norm_regress << endl;
283 if ( x_L2norm_error > 1.0e-08*x_L2norm_regress) {
284 passed = false;
285 cout << "FAILED regression constraint!" << endl;
286 }
287
288 RCP<Thyra::VectorBase<double> > x_best = x_n->clone_v();
289 {
290 Thyra::DetachedVectorView<double> x_best_view(*x_best);
291 x_best_view[0] = -1.59496108218721311;
292 x_best_view[1] = 0.96359412806611255;
293 }
294
295 Thyra::V_VmV(x_error.ptr(), *x_n, *x_best);
296 x_L2norm_error = Thyra::norm_2(*x_error);
297 double x_L2norm_best = Thyra::norm_2(*x_best );
298
299 cout << "Relative L2 Norm of the error (best) = "
300 << x_L2norm_error/x_L2norm_best << endl;
301 if ( x_L2norm_error > 0.02*x_L2norm_best) {
302 passed = false;
303 cout << "FAILED best constraint!" << endl;
304 }
305 if (passed) success = true;
306 }
307 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
308
309 if(success)
310 cout << "\nEnd Result: Test Passed!" << std::endl;
311
312 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
313}
int main(int argc, char *argv[])
Description:
ModelEvaluator implementation for the example van der Pol Problem.