Rythmos_TimeRange.hpp

00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //                     Rythmos Package
00005 //                 Copyright (2006) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Todd S. Coffey (tscoffe@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 //@HEADER
00028 
00029 #ifndef RYTHMOS_TIME_RANGE_H
00030 #define RYTHMOS_TIME_RANGE_H
00031 
00032 #include "Rythmos_ConfigDefs.h"
00033 #include "Teuchos_Assert.hpp"
00034 
00035 namespace Rythmos {
00036 
00037 
00059 template<class TimeType>
00060 int compareTimeValues( const TimeType &t1, const TimeType &t2 )
00061 {
00062   // Here we will do the comparison based on the larger mag number in order to
00063   // make the comparison return the same independent of the order the
00064   // arguments are passed in!
00065   typedef Teuchos::ScalarTraits<TimeType> ST;
00066   const TimeType epsMore = 10.0*std::numeric_limits<TimeType>::epsilon();
00067   const TimeType t1Mag = ST::magnitude(t1);
00068   const TimeType t1Tol = t1Mag*epsMore;
00069   if ( t2 - t1Tol <= t1 && t1 <= t2 + t1Tol )
00070     return 0;
00071   else if ( t1 > t2 + t1Tol )
00072     return +1;
00073   // t1 < t2 - t1Tol
00074   return -1;
00075 }
00076 
00077 
00085 template<class TimeType>
00086 class TimeRange {
00087 public:
00089   TimeRange()
00090     : lower_(0.0), upper_(-1.0)
00091     {}
00093   TimeRange( const TimeType &lower, const TimeType &upper )
00094     : lower_(lower), upper_(upper)
00095     {
00096       TEUCHOS_ASSERT( lower_ <= upper_ ); 
00097     }
00099   bool isValid() const { return (lower_ <= upper_); }
00101   TimeType lower() const { return lower_; }
00103   TimeType upper() const { return upper_; }
00105   TimeType length() const { return (upper_ - lower_); }
00107   bool isInRange ( const TimeType &t ) const
00108     {
00109       return (
00110         compareTimeValues(lower_,t) <= 0
00111         && compareTimeValues(upper_,t) >= 0
00112         );
00113     }
00115   TimeRange<TimeType> copyAndScale( const TimeType &scale ) const
00116     {
00117       TimeRange<TimeType> newRange = *this;
00118       if (!newRange.isValid())
00119         return newRange;
00120       newRange.lower_ *= scale;
00121       newRange.upper_ *= scale;
00122       return newRange;
00123     }
00124 
00125 private:
00126   TimeType lower_;
00127   TimeType upper_;
00128 };
00129 
00130 
00135 template<class TimeType>
00136 inline
00137 TimeRange<TimeType> timeRange(const TimeType lower, const TimeType upper)
00138 {
00139   return TimeRange<TimeType>(lower,upper);
00140 }
00141 
00142 
00147 template<class TimeType>
00148 inline
00149 TimeRange<TimeType> invalidTimeRange()
00150 {
00151   return TimeRange<TimeType>();
00152 }
00153 
00154 
00159 template<class TimeType>
00160 std::ostream& operator<<( std::ostream& out, const TimeRange<TimeType>& range )
00161 {
00162   out << "[";
00163   if (range.isValid()) {
00164     out << range.lower() << "," << range.upper();
00165   }
00166   else {
00167     out <<"INVALID";
00168   }
00169   out << "]";
00170   return out;
00171 }
00172 
00173 
00180 template<class TimeType>
00181 bool isInRange_cc(const TimeRange<TimeType> &tr, const TimeType &p)
00182 {
00183   return (
00184     compareTimeValues(tr.lower(),p) <= 0
00185     && compareTimeValues(tr.upper(),p) >= 0
00186     );
00187 }
00188 
00189 
00196 template<class TimeType>
00197 bool isInRange_oc(const TimeRange<TimeType> &tr, const TimeType &p)
00198 {
00199   return (
00200     compareTimeValues(tr.lower(),p) < 0
00201     && compareTimeValues(tr.upper(),p) >= 0
00202     );
00203 }
00204 
00205 
00212 template<class TimeType>
00213 bool isInRange_co(const TimeRange<TimeType> &tr, const TimeType &p)
00214 {
00215   return (
00216     compareTimeValues(tr.lower(),p) <= 0
00217     && compareTimeValues(tr.upper(),p) > 0
00218     );
00219 }
00220 
00221 
00228 template<class TimeType>
00229 bool isInRange_oo(const TimeRange<TimeType> &tr, const TimeType &p)
00230 {
00231   return (
00232     compareTimeValues(tr.lower(),p) < 0
00233     && compareTimeValues(tr.upper(),p) > 0
00234     );
00235 }
00236 
00237 
00238 } // namespace Rythmos
00239 
00240 
00241 #endif //RYTHMOS_TIME_RANGE_H

Generated on Tue Oct 20 12:46:08 2009 for Rythmos - Transient Integration for Differential Equations by doxygen 1.4.7