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