9 #ifndef TEMPUS_CONVERGENCE_TEST_UTILS_HPP
10 #define TEMPUS_CONVERGENCE_TEST_UTILS_HPP
13 #include "Teuchos_as.hpp"
14 #include "Tempus_Stepper.hpp"
18 namespace Tempus_Test {
23 template<
class Scalar>
28 void setData(std::vector<Scalar>& x, std::vector<Scalar>& y);
37 std::vector<Scalar>
x_;
38 std::vector<Scalar>
y_;
46 template<
class Scalar>
49 isInitialized_ =
false;
52 template<
class Scalar>
54 std::vector<Scalar>& x, std::vector<Scalar>& y)
59 isInitialized_ =
true;
63 template<
class Scalar>
65 std::vector<Scalar>& x, std::vector<Scalar>& y)
67 TEUCHOS_TEST_FOR_EXCEPTION(x.size() != y.size(), std::logic_error,
68 "x and y data are note the same size for linear regression\n");
69 TEUCHOS_TEST_FOR_EXCEPTION(x.size() < 2, std::logic_error,
70 "Not enough data points for linear regression!\n");
71 int N = Teuchos::as<int>(x.size());
75 for (
int i=1; i<N ; ++i) {
80 TEUCHOS_TEST_FOR_EXCEPT(numUnique==1);
83 template<
class Scalar>
86 TEUCHOS_TEST_FOR_EXCEPT(!isInitialized_);
90 template<
class Scalar>
93 TEUCHOS_TEST_FOR_EXCEPT(!isInitialized_);
97 template<
class Scalar>
100 TEUCHOS_TEST_FOR_EXCEPT(!isInitialized_);
101 typedef Teuchos::ScalarTraits<Scalar> ST;
103 int N = Teuchos::as<int>(x_.size());
105 Scalar sum1 = ST::zero();
106 Scalar sum2 = ST::zero();
107 for (
int i=0 ; i<N ; ++i) {
111 sum1 *= Scalar(-2*ST::one());
112 sum2 *= Scalar(-2*ST::one());
114 Scalar sum3 = ST::zero();
115 Scalar sum4 = ST::zero();
116 for (
int i=0 ; i<N ; ++i) {
117 for (
int j=0 ; j<N ; ++j) {
122 sum3 *= Scalar(2*ST::one()/Scalar(N));
123 sum4 *= Scalar(2*ST::one()/Scalar(N));
125 slope_ = ( sum3 + sum1 ) / ( sum4 + sum2 );
127 yIntercept_ = ST::zero();
128 for (
int i=0 ; i<N ; ++i ) {
129 yIntercept_ += y_[i]-slope_*x_[i];
131 yIntercept_ *= Scalar(ST::one()/Scalar(N));
135 template<
class Scalar>
137 std::vector<Scalar>& x, std::vector<Scalar>& y)
144 template<
class Scalar>
146 std::vector<Scalar>& x, std::vector<Scalar>& y,
147 Scalar & slope, Scalar & yIntercept)
156 template<
class Scalar>
158 std::vector<Scalar>& x, std::vector<Scalar>& y)
160 TEUCHOS_TEST_FOR_EXCEPT(x.size() != y.size());
161 int N = Teuchos::as<int>(x.size());
162 std::vector<Scalar> xlog;
163 std::vector<Scalar> ylog;
165 for (
int i=0 ; i<N ; ++i) {
166 xlog.push_back(log(x[i]));
167 ylog.push_back(log(y[i]));
175 template<
class Scalar>
178 Teuchos::RCP<LinearRegression<Scalar> > lr =
183 template<
class Scalar>
185 const std::string filename,
187 std::vector<Scalar> & StepSize,
188 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> & solutions,
189 std::vector<Scalar> & xErrorNorm,
191 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> & solutionsDot,
192 std::vector<Scalar> & xDotErrorNorm,
194 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> & solutionsDotDot,
195 std::vector<Scalar> & xDotDotErrorNorm,
196 Scalar & xDotDotSlope)
198 if ( solutions.empty() ) {
199 std::cout <<
"No solutions to write out!\n" << std::endl;
202 std::size_t lastElem = solutions.size()-1;
203 std::vector<Scalar> dt;
205 auto ref_solution = solutions[lastElem];
206 for (std::size_t i=0; i < lastElem; ++i) {
207 dt.push_back(StepSize[i]);
208 auto tmp = solutions[i];
209 Thyra::Vp_StV(tmp.ptr(), -1.0, *ref_solution);
210 Scalar L2norm = Thyra::norm_2(*tmp);
211 xErrorNorm.push_back(L2norm);
213 xSlope = computeLinearRegressionLogLog<Scalar>(dt, xErrorNorm);
215 if (!solutionsDot.empty()) {
216 auto ref_solutionDot = solutionsDot[lastElem];
217 for (std::size_t i=0; i < lastElem; ++i) {
218 auto tmp = solutionsDot[i];
219 Thyra::Vp_StV(tmp.ptr(), -1.0, *ref_solutionDot);
220 Scalar L2norm = Thyra::norm_2(*tmp);
221 xDotErrorNorm.push_back(L2norm);
223 xDotSlope = computeLinearRegressionLogLog<Scalar>(dt, xDotErrorNorm);
226 if (!solutionsDotDot.empty()) {
227 auto ref_solutionDotDot = solutionsDotDot[solutions.size()-1];
228 for (std::size_t i=0; i < lastElem; ++i) {
229 auto tmp = solutionsDotDot[i];
230 Thyra::Vp_StV(tmp.ptr(), -1.0, *ref_solutionDotDot);
231 Scalar L2norm = Thyra::norm_2(*tmp);
232 xDotDotErrorNorm.push_back(L2norm);
234 xDotDotSlope = computeLinearRegressionLogLog<Scalar>(dt, xDotDotErrorNorm);
237 Scalar order = stepper->getOrder();
238 std::cout <<
" Stepper = " << stepper->description() << std::endl;
239 std::cout <<
" =================================" << std::endl;
240 std::cout <<
" Expected Order = " << order << std::endl;
241 std::cout <<
" x Order = " << xSlope << std::endl;
242 if (!solutionsDot.empty())
243 std::cout<<
" xDot Order = " << xDotSlope << std::endl;
244 if (!solutionsDotDot.empty())
245 std::cout<<
" xDotDot Order = " << xDotDotSlope << std::endl;
246 std::cout <<
" =================================" << std::endl;
248 std::ofstream ftmp(filename);
250 for (std::size_t n=0; n<lastElem; n++) {
251 factor = 0.8*(pow(dt[n]/dt[0], order));
253 <<
" " << xErrorNorm[n] <<
" " << factor*xErrorNorm[0];
254 if (xDotErrorNorm.size() == lastElem)
255 ftmp <<
" "<< xDotErrorNorm[n] <<
" " << factor*xDotErrorNorm[0];
256 if (xDotDotErrorNorm.size() == lastElem)
257 ftmp <<
" "<< xDotDotErrorNorm[n] <<
" " << factor*xDotDotErrorNorm[0];
263 template<
class Scalar>
265 const std::string filename,
267 std::vector<Scalar> & StepSize,
268 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> & solutions,
269 std::vector<Scalar> & xErrorNorm,
271 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> & solutionsDot,
272 std::vector<Scalar> & xDotErrorNorm,
275 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> solutionsDotDot;
276 std::vector<Scalar> xDotDotErrorNorm;
277 Scalar xDotDotSlope = Scalar(0.0);
279 solutions, xErrorNorm, xSlope,
280 solutionsDot, xDotErrorNorm, xDotSlope,
281 solutionsDotDot, xDotDotErrorNorm, xDotDotSlope);
284 template<
class Scalar>
286 const std::string filename,
288 std::vector<Scalar> & StepSize,
289 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> & solutions,
290 std::vector<Scalar> & xErrorNorm,
293 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> solutionsDot;
294 std::vector<Scalar> xDotErrorNorm;
295 Scalar xDotSlope = Scalar(0.0);
296 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar>>> solutionsDotDot;
297 std::vector<Scalar> xDotDotErrorNorm;
298 Scalar xDotDotSlope = Scalar(0.0);
300 solutions, xErrorNorm, xSlope,
301 solutionsDot, xDotErrorNorm, xDotSlope,
302 solutionsDotDot, xDotDotErrorNorm, xDotDotSlope);
305 template<
class Scalar>
307 const std::string filename,
310 std::ofstream ftmp(filename);
311 Teuchos::RCP<const Thyra::VectorBase<Scalar> > x;
312 Teuchos::RCP<const Thyra::VectorBase<Scalar> > xDot;
313 Teuchos::RCP<const Thyra::VectorBase<Scalar> > xDotDot;
314 for (
int i=0; i<solutionHistory->getNumStates(); i++) {
315 Teuchos::RCP<const Tempus::SolutionState<Scalar> > solutionState =
316 (*solutionHistory)[i];
317 Scalar time = solutionState->getTime();
318 x = solutionState->getX();
319 xDot = solutionState->getXDot();
320 xDotDot = solutionState->getXDotDot();
321 int J = x->space()->dim();
324 for (
int j=0; j<J; j++) ftmp <<
" " << get_ele(*x, j);
325 if (xDot != Teuchos::null)
326 for (
int j=0; j<J; j++) ftmp <<
" " << get_ele(*xDot, j);
327 if (xDotDot != Teuchos::null)
328 for (
int j=0; j<J; j++) ftmp <<
" " << get_ele(*xDotDot, j);
334 template<
class Scalar>
336 const std::string filename,
339 Teuchos::RCP<const Tempus::SolutionHistory<Scalar> > sh(solutionHistory);
345 #endif // TEMPUS_CONVERGENCE_TEST_UTILS_HPP
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)
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.