29 #ifndef Rythmos_INTERPOLATION_BUFFER_HELPERS_HPP
30 #define Rythmos_INTERPOLATION_BUFFER_HELPERS_HPP
33 #include "Rythmos_InterpolationBufferBase.hpp"
34 #include "Teuchos_Assert.hpp"
35 #include "Teuchos_as.hpp"
45 template<
class Scalar>
46 void assertTimePointsAreSorted(
const Array<Scalar>& time_vec);
66 template<
class Scalar>
67 void assertNoTimePointsBeforeCurrentTimeRange(
68 const InterpolationBufferBase<Scalar> &interpBuffer,
69 const Array<Scalar>& time_vec,
70 const int &startingTimePointIndex = 0
88 template<
class Scalar>
89 void assertNoTimePointsInsideCurrentTimeRange(
90 const InterpolationBufferBase<Scalar> &interpBuffer,
91 const Array<Scalar>& time_vec
99 template<
class TimeType>
100 void selectPointsInTimeRange(
101 const Array<TimeType>& points_in,
102 const TimeRange<TimeType>& range,
103 const Ptr<Array<TimeType> >& points_out
111 template<
class TimeType>
112 void removePointsInTimeRange(
113 Array<TimeType>* points_in,
114 const TimeRange<TimeType>& range
164 template<
class Scalar>
165 bool getCurrentPoints(
166 const InterpolationBufferBase<Scalar> &interpBuffer,
167 const Array<Scalar>& time_vec,
168 Array<RCP<
const Thyra::VectorBase<Scalar> > >* x_vec,
169 Array<RCP<
const Thyra::VectorBase<Scalar> > >* xdot_vec,
170 int *nextTimePointIndex
182 template<
class Scalar>
183 void Rythmos::assertTimePointsAreSorted(
const Array<Scalar>& time_vec)
185 const int numTimePoints = time_vec.size();
186 for (
int i = 0; i < numTimePoints-1; ++ i ) {
187 TEUCHOS_TEST_FOR_EXCEPTION(
188 time_vec[i] >= time_vec[i+1], std::logic_error,
189 "Error, the time vector points time_vec["<<i<<
"] = " << time_vec[i]
190 <<
" >= time_vec["<<i+1<<
"] = " << time_vec[i+1] <<
" are not [unique|sorted]!"
196 template<
class Scalar>
197 void Rythmos::assertNoTimePointsBeforeCurrentTimeRange(
198 const InterpolationBufferBase<Scalar> &interpBuffer,
199 const Array<Scalar>& time_vec,
203 typedef ScalarTraits<Scalar> ST;
204 const int numTimePoints = time_vec.size();
205 const TimeRange<Scalar> currentTimeRange = interpBuffer.getTimeRange();
206 if (currentTimeRange.length() >= ST::zero()) {
207 for (
int i = 0; i < numTimePoints; ++i ) {
208 TEUCHOS_TEST_FOR_EXCEPTION(
209 time_vec[i] < currentTimeRange.lower(), std::out_of_range,
210 "Error, time_vec["<<i<<
"] = " << time_vec[i] <<
" < currentTimeRange.lower() = "
211 << currentTimeRange.lower() <<
" for " << interpBuffer.description() <<
"!"
218 template<
class Scalar>
219 void Rythmos::assertNoTimePointsInsideCurrentTimeRange(
220 const InterpolationBufferBase<Scalar>& interpBuffer,
221 const Array<Scalar>& time_vec
224 typedef ScalarTraits<Scalar> ST;
225 const int numTimePoints = time_vec.size();
226 const TimeRange<Scalar> currentTimeRange = interpBuffer.getTimeRange();
227 if (currentTimeRange.length() >= ST::zero()) {
228 for (
int i = 0; i < numTimePoints; ++i ) {
229 TEUCHOS_TEST_FOR_EXCEPTION(
230 currentTimeRange.isInRange(time_vec[i]), std::out_of_range,
231 "Error, time_vec["<<i<<
"] = " << time_vec[i] <<
" is in TimeRange of "
232 << interpBuffer.description() <<
" = ["
233 << currentTimeRange.lower() <<
"," << currentTimeRange.upper() <<
"]!"
240 template<
class TimeType>
241 void Rythmos::selectPointsInTimeRange(
242 const Array<TimeType>& points_in,
243 const TimeRange<TimeType>& range,
244 const Ptr<Array<TimeType> >& points_out
248 int Nt = Teuchos::as<int>(points_in.size());
249 for (
int i=0; i < Nt ; ++i) {
250 if (range.isInRange(points_in[i])) {
251 points_out->push_back(points_in[i]);
257 template<
class TimeType>
258 void Rythmos::removePointsInTimeRange(
259 Array<TimeType>* points_in,
260 const TimeRange<TimeType>& range
263 Array<TimeType> values_to_remove;
264 for (
int i=0 ; i<Teuchos::as<int>(points_in->size()) ; ++i) {
265 if (range.isInRange((*points_in)[i])) {
266 values_to_remove.push_back((*points_in)[i]);
269 typename Array<TimeType>::iterator point_it;
270 for (
int i=0 ; i< Teuchos::as<int>(values_to_remove.size()) ; ++i) {
271 point_it = std::find(points_in->begin(),points_in->end(),values_to_remove[i]);
272 TEUCHOS_TEST_FOR_EXCEPTION(
273 point_it == points_in->end(), std::logic_error,
274 "Error, point to remove = " << values_to_remove[i] <<
" not found with std:find!\n"
276 points_in->erase(point_it);
281 template<
class Scalar>
282 bool Rythmos::getCurrentPoints(
283 const InterpolationBufferBase<Scalar> &interpBuffer,
284 const Array<Scalar>& time_vec,
285 Array<RCP<
const Thyra::VectorBase<Scalar> > >* x_vec,
286 Array<RCP<
const Thyra::VectorBase<Scalar> > >* xdot_vec,
287 int *nextTimePointIndex_inout
291 typedef ScalarTraits<Scalar> ST;
294 const int numTotalTimePoints = time_vec.size();
297 #ifdef HAVE_RYTHMOS_DEBUG
298 TEUCHOS_TEST_FOR_EXCEPT(nextTimePointIndex_inout==0);
299 TEUCHOS_ASSERT( 0 <= *nextTimePointIndex_inout && *nextTimePointIndex_inout < numTotalTimePoints );
300 TEUCHOS_ASSERT( x_vec == 0 || as<int>(x_vec->size()) == numTotalTimePoints );
301 TEUCHOS_ASSERT( xdot_vec == 0 || as<int>(xdot_vec->size()) == numTotalTimePoints );
302 #endif // HAVE_RYTHMOS_DEBUG
304 int &nextTimePointIndex = *nextTimePointIndex_inout;
305 const int initNextTimePointIndex = nextTimePointIndex;
307 const TimeRange<Scalar> currentTimeRange = interpBuffer.getTimeRange();
309 if (currentTimeRange.length() >= ST::zero()) {
313 Array<Scalar> current_time_vec;
316 for ( i = 0; i < numTotalTimePoints-nextTimePointIndex; ++i ) {
317 const Scalar t = time_vec[nextTimePointIndex];
318 #ifdef HAVE_RYTHMOS_DEBUG
319 TEUCHOS_ASSERT( t >= currentTimeRange.lower() );
320 #endif // HAVE_RYTHMOS_DEBUG
321 if ( currentTimeRange.isInRange(t) ) {
322 ++nextTimePointIndex;
323 current_time_vec.push_back(t);
329 #ifdef HAVE_RYTHMOS_DEBUG
332 TEUCHOS_ASSERT( nextTimePointIndex-initNextTimePointIndex == i );
338 const int numCurrentTimePoints = current_time_vec.size();
340 if ( numCurrentTimePoints > 0 ) {
344 Array<RCP<const Thyra::VectorBase<Scalar> > > current_x_vec;
345 Array<RCP<const Thyra::VectorBase<Scalar> > > current_xdot_vec;
346 if (x_vec || xdot_vec) {
347 interpBuffer.getPoints(
349 x_vec ? ¤t_x_vec : 0,
350 xdot_vec ? ¤t_xdot_vec : 0,
357 for (
int i = initNextTimePointIndex; i < nextTimePointIndex; ++i ) {
359 (*x_vec)[i] = current_x_vec[i-initNextTimePointIndex];
361 (*xdot_vec)[i] = current_xdot_vec[i-initNextTimePointIndex];
368 return ( nextTimePointIndex == initNextTimePointIndex ?
false :
true );
373 #endif //Rythmos_INTERPOLATION_BUFFER_HELPERS_HPP