29 #ifndef Rythmos_INTERPOLATION_BUFFER_DEF_H
30 #define Rythmos_INTERPOLATION_BUFFER_DEF_H
32 #include "Rythmos_InterpolationBuffer_decl.hpp"
33 #include "Rythmos_InterpolatorBaseHelpers.hpp"
34 #include "Rythmos_LinearInterpolator.hpp"
35 #include "Thyra_VectorStdOps.hpp"
36 #include "Teuchos_StandardParameterEntryValidators.hpp"
37 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
41 static std::string IBPolicyTypeInvalid_name =
"Invalid Policy";
42 static std::string IBPolicyTypeStatic_name =
"Static Policy";
43 static std::string IBPolicyTypeKeepNewest_name =
"Keep Newest Policy";
44 static std::string interpolationBufferPolicySelection_name =
"InterpolationBufferPolicy";
45 static std::string interpolationBufferPolicySelection_default = IBPolicyTypeKeepNewest_name;
47 static std::string interpolationBufferStorageLimit_name =
"StorageLimit";
48 static int interpolationBufferStorageLimit_default = 0;
50 Teuchos::Array<std::string>
51 S_InterpolationBufferPolicyTypes = Teuchos::tuple<std::string>(
52 IBPolicyTypeInvalid_name,
53 IBPolicyTypeStatic_name,
54 IBPolicyTypeKeepNewest_name
57 const Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<Rythmos::IBPolicy> >
58 interpolationBufferPolicyValidator = Teuchos::rcp(
59 new Teuchos::StringToIntegralParameterEntryValidator<Rythmos::IBPolicy>(
60 S_InterpolationBufferPolicyTypes,
61 Teuchos::tuple<Rythmos::IBPolicy>(
62 Rythmos::BUFFER_POLICY_INVALID,
63 Rythmos::BUFFER_POLICY_STATIC,
64 Rythmos::BUFFER_POLICY_KEEP_NEWEST
66 interpolationBufferPolicySelection_name
79 template<
class Scalar>
82 this->defaultInitializeAll_();
83 initialize(Teuchos::null,0);
86 template<
class Scalar>
89 interpolator_ = Teuchos::null;
91 data_vec_ = Teuchos::null;
92 paramList_ = Teuchos::null;
93 policy_ = BUFFER_POLICY_INVALID;
96 template<
class Scalar>
97 RCP<const Thyra::VectorSpaceBase<Scalar> >
100 if (data_vec_->size() == 0) {
101 RCP<const Thyra::VectorSpaceBase<Scalar> > space;
104 return((*data_vec_)[0].x->space());
109 template<
class Scalar>
115 RCP<Teuchos::FancyOStream> out = this->getOStream();
116 Teuchos::OSTab ostab(out,1,
"IB::initialize");
117 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
118 *out <<
"Initializing InterpolationBuffer" << std::endl;
119 *out <<
"Calling setInterpolator..." << std::endl;
121 data_vec_ = rcp(
new typename DataStore<Scalar>::DataStoreVector_t);
122 setInterpolator(interpolator);
123 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
124 *out <<
"Calling setStorage..." << std::endl;
127 policy_ = BUFFER_POLICY_KEEP_NEWEST;
130 template<
class Scalar>
133 int storage_limit = std::max(2,storage);
134 TEUCHOS_TEST_FOR_EXCEPTION(
135 Teuchos::as<int>(data_vec_->size()) > storage_limit,
137 "Error, specified storage = " << storage_limit
138 <<
" is below current number of vectors stored = " << data_vec_->size() <<
"!\n"
140 storage_limit_ = storage_limit;
141 RCP<Teuchos::FancyOStream> out = this->getOStream();
142 Teuchos::OSTab ostab(out,1,
"IB::setStorage");
143 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
144 *out <<
"storage_limit = " << storage_limit_ << std::endl;
149 template<
class Scalar>
152 return(storage_limit_);
156 template<
class Scalar>
161 if (interpolator == Teuchos::null) {
162 interpolator_ = linearInterpolator<Scalar>();
164 interpolator_ = interpolator;
166 RCP<Teuchos::FancyOStream> out = this->getOStream();
167 Teuchos::OSTab ostab(out,1,
"IB::setInterpolator");
168 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
169 *out <<
"interpolator = " << interpolator_->description() << std::endl;
173 template<
class Scalar>
174 RCP<InterpolatorBase<Scalar> >
177 return interpolator_;
180 template<
class Scalar>
181 RCP<const InterpolatorBase<Scalar> >
184 return interpolator_;
187 template<
class Scalar>
190 RCP<InterpolatorBase<Scalar> > old_interpolator = interpolator_;
191 interpolator_ = linearInterpolator<Scalar>();
192 return old_interpolator;
196 template<
class Scalar>
198 const Array<Scalar>& time_vec
199 ,
const Array<RCP<
const Thyra::VectorBase<Scalar> > >& x_vec
200 ,
const Array<RCP<
const Thyra::VectorBase<Scalar> > >& xdot_vec
203 #ifdef HAVE_RYTHMOS_DEBUG
205 assertTimePointsAreSorted(time_vec);
206 int tsize = Teuchos::as<int>(time_vec.size());
207 TEUCHOS_TEST_FOR_EXCEPTION(
208 tsize == 0, std::logic_error,
209 "Error, time_vec is empty!"
211 TEUCHOS_TEST_FOR_EXCEPTION(
212 Teuchos::as<int>(x_vec.size()) != tsize, std::logic_error,
213 "Error, size of x_vec = " << x_vec.size() <<
" != " << tsize <<
" = size of time_vec!\n"
215 TEUCHOS_TEST_FOR_EXCEPTION(
216 Teuchos::as<int>(xdot_vec.size()) != tsize, std::logic_error,
217 "Error, size of xdot_vec = " << x_vec.size() <<
" != " << tsize <<
" = size of time_vec!\n"
219 for (
int i=0; i<tsize ; ++i) {
220 TEUCHOS_TEST_FOR_EXCEPTION(
221 x_vec[i] == Teuchos::null, std::logic_error,
222 "Error, x_vec[" << i <<
"] == null!\n"
229 assertNoTimePointsInsideCurrentTimeRange(*
this,time_vec);
230 #endif // HAVE_RYTHMOS_DEBUG
231 RCP<Teuchos::FancyOStream> out = this->getOStream();
232 Teuchos::OSTab ostab(out,1,
"IB::addPoints");
233 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
234 *out <<
"time_vec = " << std::endl;
235 for (Teuchos::Ordinal i=0 ; i<time_vec.size() ; ++i) {
236 *out <<
"time_vec[" << i <<
"] = " << time_vec[i] << std::endl;
238 *out <<
"x_vec = " << std::endl;
239 for (Teuchos::Ordinal i=0 ; i<x_vec.size() ; ++i) {
240 *out <<
"x_vec[" << i <<
"] = " << std::endl;
241 x_vec[i]->describe(*out,Teuchos::VERB_EXTREME);
243 *out <<
"xdot_vec = " << std::endl;
244 for (Teuchos::Ordinal i=0 ; i<xdot_vec.size() ; ++i) {
245 if (!is_null(xdot_vec[i])) {
246 *out <<
"xdot_vec[" << i <<
"] = " << std::endl;
247 xdot_vec[i]->describe(*out,Teuchos::VERB_EXTREME);
251 typename DataStore<Scalar>::DataStoreList_t input_data_list;
252 vectorToDataStoreList<Scalar>(time_vec,x_vec,xdot_vec,&input_data_list);
254 if (Teuchos::as<int>(data_vec_->size()+input_data_list.size()) > storage_limit_) {
255 if (policy_ == BUFFER_POLICY_STATIC) {
256 TEUCHOS_TEST_FOR_EXCEPTION(
257 true, std::logic_error,
258 "Error, buffer would be over-full and buffer policy is BUFFER_POLICY_STATIC, these points can not be added\n"
260 }
else if (policy_ == BUFFER_POLICY_KEEP_NEWEST) {
261 if (input_data_list.front() > data_vec_->back()) {
264 int num_extra_points = input_data_list.size()-(storage_limit_-data_vec_->size());
265 #ifdef HAVE_RYTHMOS_DEBUG
266 TEUCHOS_TEST_FOR_EXCEPTION( num_extra_points <= 0, std::logic_error,
267 "Error! Buffer policy is keep newest and input data size = " << input_data_list.size() <<
", storage limit = " << storage_limit_ <<
", and data_vec size = " << data_vec_->size() <<
". Somehow number of points to delete = " << num_extra_points <<
" <= 0!"
269 #endif // HAVE_RYTHMOS_DEBUG
270 typename DataStore<Scalar>::DataStoreVector_t::iterator
271 data_it = data_vec_->begin();
272 for (
int i=0 ; i < num_extra_points ; ++i) {
275 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
276 *out <<
"Removing " << num_extra_points
277 <<
" from beginning of data_vec to make room for new points." << std::endl;
279 data_vec_->erase(data_vec_->begin(),data_it);
280 }
else if (input_data_list.back() < data_vec_->front()) {
283 int num_extra_points = input_data_list.size()-(storage_limit_-data_vec_->size());
284 #ifdef HAVE_RYTHMOS_DEBUG
285 TEUCHOS_TEST_FOR_EXCEPTION( num_extra_points <= 0, std::logic_error,
286 "Error! Buffer policy is keep newest and input data size = " << input_data_list.size() <<
", storage limit = " << storage_limit_ <<
", and data_vec size = " << data_vec_->size() <<
". Somehow number of points to delete = " << num_extra_points <<
" <= 0!"
288 #endif // HAVE_RYTHMOS_DEBUG
289 typename DataStore<Scalar>::DataStoreVector_t::iterator
290 data_it = data_vec_->end();
291 for (
int i=0 ; i < num_extra_points ; ++i) {
294 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
295 *out <<
"Removing " << num_extra_points
296 <<
" from end of data_vec to make room for new points." << std::endl;
298 data_vec_->erase(data_it,data_vec_->end());
301 TEUCHOS_TEST_FOR_EXCEPTION(
302 true, std::logic_error,
303 "Error, incoming points are on both sides of TimeRange, I don't know which points are newest in this case.\n"
308 TEUCHOS_TEST_FOR_EXCEPTION(
309 true, std::logic_error,
310 "Error, unknown buffer policy.\n"
315 std::list<DataStore<Scalar> > internal_input_data_list;
316 typename DataStore<Scalar>::DataStoreList_t::iterator it_list;
317 for (it_list = input_data_list.begin() ; it_list != input_data_list.end() ; it_list++) {
318 RCP<DataStore<Scalar> > ds_clone = it_list->clone();
319 internal_input_data_list.push_back(*ds_clone);
322 data_vec_->insert(data_vec_->end(),internal_input_data_list.begin(),internal_input_data_list.end());
324 std::sort(data_vec_->begin(),data_vec_->end());
325 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
326 *out <<
"data_vec at end of addPoints:" << std::endl;
327 for (Teuchos::Ordinal i=0 ; i<data_vec_->size() ; ++i) {
328 *out <<
"data_vec[" << i <<
"] = " << std::endl;
329 (*data_vec_)[i].describe(*out,Teuchos::VERB_EXTREME);
335 template<
class Scalar>
337 const Array<Scalar>& time_vec
338 ,Array<RCP<
const Thyra::VectorBase<Scalar> > >* x_vec
339 ,Array<RCP<
const Thyra::VectorBase<Scalar> > >* xdot_vec
340 ,Array<ScalarMag>* accuracy_vec
343 RCP<Teuchos::FancyOStream> out = this->getOStream();
344 Teuchos::OSTab ostab(out,1,
"IB::getPoints");
345 typename DataStore<Scalar>::DataStoreVector_t data_out;
346 interpolate<Scalar>(*interpolator_, data_vec_, time_vec, &data_out);
347 Array<Scalar> time_out_vec;
348 dataStoreVectorToVector<Scalar>(data_out, &time_out_vec, x_vec, xdot_vec, accuracy_vec);
349 TEUCHOS_TEST_FOR_EXCEPTION(
350 (time_vec.size() != time_out_vec.size()), std::logic_error,
351 "Error, number of time points returned from interpolator = " <<
352 time_out_vec.size() <<
" != " << time_vec.size() <<
353 " = number of time points requested\n"
355 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
356 *out <<
"Conversion of DataStoreVector to Vector successful" << std::endl;
361 template<
class Scalar>
365 if (data_vec_->size() > 0) {
372 template<
class Scalar>
375 int N = data_vec_->size();
377 time_vec->reserve(N);
378 for (
int i=0 ; i<N ; ++i) {
379 time_vec->push_back((*data_vec_)[i].time);
381 RCP<Teuchos::FancyOStream> out = this->getOStream();
382 Teuchos::OSTab ostab(out,1,
"IB::getNodes");
383 if ( Teuchos::as<int>(this->getVerbLevel()) >= Teuchos::as<int>(Teuchos::VERB_HIGH) ) {
384 *out << this->description() << std::endl;
385 for (Teuchos::Ordinal i=0 ; i<time_vec->size() ; ++i) {
386 *out <<
"time_vec[" << i <<
"] = " << (*time_vec)[i] << std::endl;
392 template<
class Scalar>
395 typedef Teuchos::ScalarTraits<Scalar> ST;
396 int N = time_vec.size();
397 #ifdef HAVE_RYTHMOS_DEBUG
400 for (
int i=0; i<N ; ++i) {
401 TEUCHOS_TEST_FOR_EXCEPTION(
402 (range.
lower() <= time_vec[i]) && (time_vec[i] <= range.
upper()),
404 "Error, time_vec[" << i <<
"] = " << time_vec[i] <<
405 "is not in range of this interpolation buffer = [" <<
406 range.
lower() <<
"," << range.
upper() <<
"]!\n"
409 #endif // HAVE_RYTHMOS_DEBUG
410 RCP<Thyra::VectorBase<Scalar> > vec_temp;
411 ScalarMag z = ST::zero();
412 for (
int i=0; i<N ; ++i) {
413 DataStore<Scalar> ds_temp(time_vec[i],vec_temp,vec_temp,z);
414 typename DataStore<Scalar>::DataStoreVector_t::iterator
415 data_it = std::find(data_vec_->begin(),data_vec_->end(),ds_temp);
416 TEUCHOS_TEST_FOR_EXCEPTION(
417 data_it == data_vec_->end(), std::logic_error,
418 "Error, time_vec[" << i <<
"] = " << time_vec[i] <<
"is not a node in the interpolation buffer!\n"
420 data_vec_->erase(data_it);
425 template<
class Scalar>
428 return(interpolator_->order());
432 template<
class Scalar>
435 std::string name =
"Rythmos::InterpolationBuffer";
440 template<
class Scalar>
442 Teuchos::FancyOStream &out
443 ,
const Teuchos::EVerbosityLevel verbLevel
446 if ( (Teuchos::as<int>(verbLevel) == Teuchos::as<int>(Teuchos::VERB_DEFAULT) ) ||
447 (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_LOW) )
449 out << description() <<
"::describe" << std::endl;
450 out <<
"interpolator = " << interpolator_->description() << std::endl;
451 out <<
"storage_limit = " << storage_limit_ << std::endl;
452 }
else if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_LOW)) {
453 }
else if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
454 }
else if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_HIGH)) {
455 out <<
"data_vec = " << std::endl;
456 for (Teuchos::Ordinal i=0; i<data_vec_->size() ; ++i) {
457 out <<
"data_vec[" << i <<
"] = " << std::endl;
458 (*data_vec_)[i].describe(out,this->getVerbLevel());
464 template <
class Scalar>
467 TEUCHOS_TEST_FOR_EXCEPT( is_null(paramList) );
468 paramList->validateParameters(*this->getValidParameters());
469 paramList_ = paramList;
471 Teuchos::readVerboseObjectSublist(&*paramList_,
this);
476 IBPolicy policyLevel = interpolationBufferPolicyValidator->getIntegralValue(
477 *paramList_, interpolationBufferPolicySelection_name, interpolationBufferPolicySelection_default
479 if (policyLevel != BUFFER_POLICY_INVALID) {
480 policy_ = policyLevel;
482 int storage_limit = paramList_->get( interpolationBufferStorageLimit_name, interpolationBufferStorageLimit_default);
483 setStorage(storage_limit);
486 template<
class Scalar>
489 static RCP<Teuchos::ParameterList> validPL;
491 if (is_null(validPL)) {
493 RCP<Teuchos::ParameterList>
494 pl = Teuchos::parameterList();
496 Teuchos::setupVerboseObjectSublist(&*pl);
499 interpolationBufferPolicySelection_name,
500 interpolationBufferPolicySelection_default,
501 "Interpolation Buffer Policy for when the maximum storage size is exceeded. Static will throw an exception when the storage limit is exceeded. Keep Newest will over-write the oldest data in the buffer when the storage limit is exceeded.",
502 interpolationBufferPolicyValidator
506 interpolationBufferStorageLimit_name,
507 interpolationBufferStorageLimit_default,
508 "Storage limit for the interpolation buffer."
518 template <
class Scalar>
519 RCP<Teuchos::ParameterList>
526 template <
class Scalar>
529 RCP<Teuchos::ParameterList> temp_param_list = paramList_;
530 paramList_ = Teuchos::null;
531 return(temp_param_list);
534 template <
class Scalar>
546 #define RYTHMOS_INTERPOLATION_BUFFER_INSTANT(SCALAR) \
548 template class InterpolationBuffer< SCALAR >; \
550 template RCP<InterpolationBuffer< SCALAR > > interpolationBuffer( \
551 const RCP<InterpolatorBase< SCALAR > >& interpolator, \
558 #endif // Rythmos_INTERPOLATION_BUFFER_DEF_H
void addPoints(const Array< Scalar > &time_vec, const Array< RCP< const Thyra::VectorBase< Scalar > > > &x_vec, const Array< RCP< const Thyra::VectorBase< Scalar > > > &xdot_vec)
Add point to buffer.
Base strategy class for interpolation functionality.
RCP< Teuchos::ParameterList > getNonconstParameterList()
void setStorage(int storage)
Set the maximum storage of this buffer.
RCP< InterpolatorBase< Scalar > > unSetInterpolator()
Unset the interpolator for this buffer.
void removeNodes(Array< Scalar > &time_vec)
Remove interpolation nodes.
void getNodes(Array< Scalar > *time_vec) const
Get interpolation nodes.
RCP< const InterpolatorBase< Scalar > > getInterpolator() const
void initialize(const RCP< InterpolatorBase< Scalar > > &interpolator, int storage)
Initialize the buffer:
RCP< InterpolatorBase< Scalar > > getNonconstInterpolator()
int getOrder() const
Get order of interpolation.
void getPoints(const Array< Scalar > &time_vec, Array< RCP< const Thyra::VectorBase< Scalar > > > *x_vec, Array< RCP< const Thyra::VectorBase< Scalar > > > *xdot_vec, Array< ScalarMag > *accuracy_vec) const
Get value from buffer.
TimeRange< Scalar > getTimeRange() const
concrete class for interpolation buffer functionality.
RCP< const Thyra::VectorSpaceBase< Scalar > > get_x_space() const
Redefined from Rythmos::InterpolationBufferBase.
std::string description() const
Redefined from Teuchos::Describable.
int getStorage() const
Get the maximum storage of this buffer.
void setInterpolator(const RCP< InterpolatorBase< Scalar > > &interpolator)
Redefined from Rythmos::InterpolatorAcceptingObjectBase.
RCP< Teuchos::ParameterList > unsetParameterList()
void setParameterList(RCP< Teuchos::ParameterList > const ¶mList)
Redefined from Teuchos::ParameterListAcceptor.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const