9 #ifndef TEMPUS_CONVERGENCE_TEST_UTILS_HPP
10 #define TEMPUS_CONVERGENCE_TEST_UTILS_HPP
17 #include "Thyra_VectorStdOps.hpp"
20 #include "Tempus_Stepper.hpp"
23 namespace Tempus_Test {
28 template<
class Scalar>
33 void setData(std::vector<Scalar>& x, std::vector<Scalar>& y);
42 std::vector<Scalar>
x_;
43 std::vector<Scalar>
y_;
51 template<
class Scalar>
54 isInitialized_ =
false;
57 template<
class Scalar>
59 std::vector<Scalar>& x, std::vector<Scalar>& y)
64 isInitialized_ =
true;
68 template<
class Scalar>
70 std::vector<Scalar>& x, std::vector<Scalar>& y)
73 "x and y data are note the same size for linear regression\n");
75 "Not enough data points for linear regression!\n");
76 int N = Teuchos::as<int>(x.size());
80 for (
int i=1; i<N ; ++i) {
88 template<
class Scalar>
95 template<
class Scalar>
102 template<
class Scalar>
108 int N = Teuchos::as<int>(x_.size());
110 Scalar sum1 = ST::zero();
111 Scalar sum2 = ST::zero();
112 for (
int i=0 ; i<N ; ++i) {
116 sum1 *= Scalar(-2*ST::one());
117 sum2 *= Scalar(-2*ST::one());
119 Scalar sum3 = ST::zero();
120 Scalar sum4 = ST::zero();
121 for (
int i=0 ; i<N ; ++i) {
122 for (
int j=0 ; j<N ; ++j) {
127 sum3 *= Scalar(2*ST::one()/Scalar(N));
128 sum4 *= Scalar(2*ST::one()/Scalar(N));
130 slope_ = ( sum3 + sum1 ) / ( sum4 + sum2 );
132 yIntercept_ = ST::zero();
133 for (
int i=0 ; i<N ; ++i ) {
134 yIntercept_ += y_[i]-slope_*x_[i];
136 yIntercept_ *= Scalar(ST::one()/Scalar(N));
140 template<
class Scalar>
142 std::vector<Scalar>& x, std::vector<Scalar>& y)
149 template<
class Scalar>
151 std::vector<Scalar>& x, std::vector<Scalar>& y,
152 Scalar & slope, Scalar & yIntercept)
161 template<
class Scalar>
163 std::vector<Scalar>& x, std::vector<Scalar>& y)
166 int N = Teuchos::as<int>(x.size());
167 std::vector<Scalar> xlog;
168 std::vector<Scalar> ylog;
170 for (
int i=0 ; i<N ; ++i) {
171 xlog.push_back(log(x[i]));
172 ylog.push_back(log(y[i]));
180 template<
class Scalar>
188 template<
class Scalar>
190 const std::string filename,
192 std::vector<Scalar> & StepSize,
194 std::vector<Scalar> & xErrorNorm,
197 std::vector<Scalar> & xDotErrorNorm,
200 std::vector<Scalar> & xDotDotErrorNorm,
201 Scalar & xDotDotSlope)
203 if ( solutions.empty() ) {
204 std::cout <<
"No solutions to write out!\n" << std::endl;
207 std::size_t lastElem = solutions.size()-1;
208 std::vector<Scalar> dt;
210 auto ref_solution = solutions[lastElem];
211 for (std::size_t i=0; i < lastElem; ++i) {
212 dt.push_back(StepSize[i]);
213 auto tmp = solutions[i];
214 Thyra::Vp_StV(tmp.ptr(), -1.0, *ref_solution);
215 Scalar L2norm = Thyra::norm_2(*tmp);
216 xErrorNorm.push_back(L2norm);
218 xSlope = computeLinearRegressionLogLog<Scalar>(dt, xErrorNorm);
220 if (!solutionsDot.empty()) {
221 auto ref_solutionDot = solutionsDot[lastElem];
222 for (std::size_t i=0; i < lastElem; ++i) {
223 auto tmp = solutionsDot[i];
224 Thyra::Vp_StV(tmp.ptr(), -1.0, *ref_solutionDot);
225 Scalar L2norm = Thyra::norm_2(*tmp);
226 xDotErrorNorm.push_back(L2norm);
228 xDotSlope = computeLinearRegressionLogLog<Scalar>(dt, xDotErrorNorm);
231 if (!solutionsDotDot.empty()) {
232 auto ref_solutionDotDot = solutionsDotDot[solutions.size()-1];
233 for (std::size_t i=0; i < lastElem; ++i) {
234 auto tmp = solutionsDotDot[i];
235 Thyra::Vp_StV(tmp.ptr(), -1.0, *ref_solutionDotDot);
236 Scalar L2norm = Thyra::norm_2(*tmp);
237 xDotDotErrorNorm.push_back(L2norm);
239 xDotDotSlope = computeLinearRegressionLogLog<Scalar>(dt, xDotDotErrorNorm);
242 Scalar order = stepper->getOrder();
243 std::cout <<
" Stepper = " << stepper->description() << std::endl;
244 std::cout <<
" =================================" << std::endl;
245 std::cout <<
" Expected Order = " << order << std::endl;
246 std::cout <<
" x Order = " << xSlope << std::endl;
247 if (!solutionsDot.empty())
248 std::cout<<
" xDot Order = " << xDotSlope << std::endl;
249 if (!solutionsDotDot.empty())
250 std::cout<<
" xDotDot Order = " << xDotDotSlope << std::endl;
251 std::cout <<
" =================================" << std::endl;
253 std::ofstream ftmp(filename);
255 for (std::size_t n=0; n<lastElem; n++) {
256 factor = 0.8*(pow(dt[n]/dt[0], order));
258 <<
" " << xErrorNorm[n] <<
" " << factor*xErrorNorm[0];
259 if (xDotErrorNorm.size() == lastElem)
260 ftmp <<
" "<< xDotErrorNorm[n] <<
" " << factor*xDotErrorNorm[0];
261 if (xDotDotErrorNorm.size() == lastElem)
262 ftmp <<
" "<< xDotDotErrorNorm[n] <<
" " << factor*xDotDotErrorNorm[0];
268 template<
class Scalar>
270 const std::string filename,
272 std::vector<Scalar> & StepSize,
274 std::vector<Scalar> & xErrorNorm,
277 std::vector<Scalar> & xDotErrorNorm,
280 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> solutionsDotDot;
281 std::vector<Scalar> xDotDotErrorNorm;
282 Scalar xDotDotSlope = Scalar(0.0);
284 solutions, xErrorNorm, xSlope,
285 solutionsDot, xDotErrorNorm, xDotSlope,
286 solutionsDotDot, xDotDotErrorNorm, xDotDotSlope);
289 template<
class Scalar>
291 const std::string filename,
293 std::vector<Scalar> & StepSize,
295 std::vector<Scalar> & xErrorNorm,
298 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> solutionsDot;
299 std::vector<Scalar> xDotErrorNorm;
300 Scalar xDotSlope = Scalar(0.0);
301 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> solutionsDotDot;
302 std::vector<Scalar> xDotDotErrorNorm;
303 Scalar xDotDotSlope = Scalar(0.0);
305 solutions, xErrorNorm, xSlope,
306 solutionsDot, xDotErrorNorm, xDotSlope,
307 solutionsDotDot, xDotDotErrorNorm, xDotDotSlope);
310 template<
class Scalar>
312 const std::string filename,
315 std::ofstream ftmp(filename);
319 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
321 (*solutionHistory)[i];
322 Scalar time = solutionState->getTime();
323 x = solutionState->getX();
324 xDot = solutionState->getXDot();
325 xDotDot = solutionState->getXDotDot();
326 int J = x->space()->dim();
329 for (
int j=0; j<J; j++) ftmp <<
" " << get_ele(*x, j);
330 if (xDot != Teuchos::null)
331 for (
int j=0; j<J; j++) ftmp <<
" " << get_ele(*xDot, j);
332 if (xDotDot != Teuchos::null)
333 for (
int j=0; j<J; j++) ftmp <<
" " << get_ele(*xDotDot, j);
339 template<
class Scalar>
341 const std::string filename,
350 #endif // TEMPUS_CONVERGENCE_TEST_UTILS_HPP
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Scalar getYIntercept() const
void writeSolution(const std::string filename, Teuchos::RCP< const Tempus::SolutionHistory< Scalar > > solutionHistory)
Thyra Base interface for time steppers.
void writeOrderError(const std::string filename, Teuchos::RCP< Tempus::Stepper< Scalar > > stepper, std::vector< Scalar > &StepSize, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutions, std::vector< Scalar > &xErrorNorm, Scalar &xSlope, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutionsDot, std::vector< Scalar > &xDotErrorNorm, Scalar &xDotSlope, std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar >>> &solutionsDotDot, std::vector< Scalar > &xDotDotErrorNorm, Scalar &xDotDotSlope)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Scalar computeLinearRegressionLogLog(std::vector< Scalar > &x, std::vector< Scalar > &y)
Teuchos::RCP< LinearRegression< Scalar > > linearRegression()
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
void validateXYData_(std::vector< Scalar > &x, std::vector< Scalar > &y)
Scalar computeLinearRegression(std::vector< Scalar > &x, std::vector< Scalar > &y)
void setData(std::vector< Scalar > &x, std::vector< Scalar > &y)
Linear regression class. Copied and modified from Rythmos.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)