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
66int main(int argc, char *argv[])
67{
68 bool verbose = true;
69 bool success = false;
70 try {
71 // Construct ModelEvaluator
72 Teuchos::RCP<const Thyra::ModelEvaluator<double> >
73 model = Teuchos::rcp(new VanDerPol_ModelEvaluator_02<double>());
74
75 // Get initial conditions from ModelEvaluator::getNominalValues.
76 int n = 0;
77 double time = 0.0;
78 bool passed = true; // ICs are considered passed.
79 RCP<Thyra::VectorBase<double> > x_n =
80 model->getNominalValues().get_x()->clone_v();
81 RCP<Thyra::VectorBase<double> > xDot_n =
82 model->getNominalValues().get_x_dot()->clone_v();
83
84 // Timestep size
85 double finalTime = 2.0;
86 int nTimeSteps = 2000;
87 const double constDT = finalTime/nTimeSteps;
88
89 // Advance the solution to the next timestep.
90 cout << n << " " << time << " " << get_ele(*(x_n), 0)
91 << " " << get_ele(*(x_n), 1) << endl;
92 while (passed && time < finalTime && n < nTimeSteps) {
93
94 // Initialize next time step
95 RCP<Thyra::VectorBase<double> > x_np1 = x_n->clone_v(); // at time index n+1
96
97 // Set the timestep and time.
98 double dt = constDT;
99 time = (n+1)*dt;
100
101 // For explicit ODE formulation, xDot = f(x, t),
102 // xDot is part of the outArgs.
103 auto inArgs = model->createInArgs();
104 auto outArgs = model->createOutArgs();
105 inArgs.set_t(time);
106 inArgs.set_x(x_n);
107 inArgs.set_x_dot(Teuchos::null);
108 outArgs.set_f(xDot_n);
109
110 // Righthand side evaluation and time-derivative at n.
111 model->evalModel(inArgs, outArgs);
112
113 // Take the timestep - Forward Euler
114 Thyra::V_VpStV(x_np1.ptr(), *x_n, dt, *xDot_n);
115
116 // Test if solution has passed.
117 if ( std::isnan(Thyra::norm(*x_np1)) ) {
118 passed = false;
119 } else {
120 // Promote to next step (n <- n+1).
121 Thyra::V_V(x_n.ptr(), *x_np1);
122 n++;
123 }
124
125 // Output
126 if ( n%100 == 0 )
127 cout << n << " " << time << " " << get_ele(*(x_n), 0)
128 << " " << get_ele(*(x_n), 1) << endl;
129 }
130
131 // Test for regression.
132 RCP<Thyra::VectorBase<double> > x_regress = x_n->clone_v();
133 {
134 Thyra::DetachedVectorView<double> x_regress_view(*x_regress);
135 x_regress_view[0] = -1.59496108218721311;
136 x_regress_view[1] = 0.96359412806611255;
137 }
138
139 RCP<Thyra::VectorBase<double> > x_error = x_n->clone_v();
140 Thyra::V_VmV(x_error.ptr(), *x_n, *x_regress);
141 double x_L2norm_error = Thyra::norm_2(*x_error );
142 double x_L2norm_regress = Thyra::norm_2(*x_regress);
143
144 cout << "Relative L2 Norm of the error (regression) = "
145 << x_L2norm_error/x_L2norm_regress << endl;
146 if ( x_L2norm_error > 1.0e-08*x_L2norm_regress) {
147 passed = false;
148 cout << "FAILED regression constraint!" << endl;
149 }
150 if (passed) success = true;
151 }
152 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
153
154 if(success)
155 cout << "\nEnd Result: Test Passed!" << std::endl;
156
157 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
158}
int main(int argc, char *argv[])
ModelEvaluator implementation for the example van der Pol Problem.