Rythmos - Transient Integration for Differential Equations  Version of the Day
 All Classes Functions Variables Typedefs Pages
Rythmos_TimeRange_def.hpp
1 //@HEADER
2 // ***********************************************************************
3 //
4 // Rythmos Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact Todd S. Coffey (tscoffe@sandia.gov)
25 //
26 // ***********************************************************************
27 //@HEADER
28 
29 #ifndef RYTHMOS_TIME_RANGE_DEF_H
30 #define RYTHMOS_TIME_RANGE_DEF_H
31 
32 #include "Rythmos_TimeRange_decl.hpp"
33 #include "Teuchos_Assert.hpp"
34 #include "Teuchos_ScalarTraits.hpp"
35 
36 
37 template<class TimeType>
38 int Rythmos::compareTimeValues( const TimeType &t1, const TimeType &t2 )
39 {
40  // Here we will do the comparison based on the magnitude of t1
41  const TimeType epsMore = 10.0*std::numeric_limits<TimeType>::epsilon();
42  const TimeType t1Mag = Teuchos::ScalarTraits<TimeType>::magnitude(t1);
43  const TimeType t1Tol = t1Mag*epsMore;
44  if ( t2 - t1Tol <= t1 && t1 <= t2 + t1Tol )
45  return 0;
46  else if ( t1 > t2 + t1Tol )
47  return +1;
48  // t1 < t2 - t1Tol
49  return -1;
50 }
51 
52 
53 template<class TimeType>
55 Rythmos::timeRange(const TimeType lower, const TimeType upper)
56 {
57  return TimeRange<TimeType>(lower,upper);
58 }
59 
60 
61 template<class TimeType>
63 Rythmos::invalidTimeRange()
64 {
65  return TimeRange<TimeType>();
66 }
67 
68 
69 template<class TimeType>
70 std::ostream&
71 Rythmos::operator<<( std::ostream& out, const TimeRange<TimeType>& range )
72 {
73  out << "[";
74  if (range.isValid()) {
75  out << range.lower() << "," << range.upper();
76  }
77  else {
78  out <<"INVALID";
79  }
80  out << "]";
81  return out;
82 }
83 
84 
85 template<class TimeType>
86 void Rythmos::asssertInTimeRange( const TimeRange<TimeType> &timeRange,
87  const TimeType &time )
88 {
89  TEUCHOS_TEST_FOR_EXCEPTION( !timeRange.isInRange(time), std::out_of_range,
90  "Error, the time = " << time
91  << " is out of the range = " << timeRange << "!"
92  );
93 }
94 
95 
96 template<class TimeType>
97 bool Rythmos::isInRange_cc(const TimeRange<TimeType> &tr, const TimeType &p)
98 {
99  return (
100  compareTimeValues(p,tr.lower()) >= 0
101  && compareTimeValues(p,tr.upper()) <= 0
102  );
103 }
104 
105 
106 template<class TimeType>
107 bool Rythmos::isInRange_oc(const TimeRange<TimeType> &tr, const TimeType &p)
108 {
109  return (
110  compareTimeValues(p,tr.lower()) > 0
111  && compareTimeValues(p,tr.upper()) <= 0
112  );
113 }
114 
115 
116 template<class TimeType>
117 bool Rythmos::isInRange_co(const TimeRange<TimeType> &tr, const TimeType &p)
118 {
119  return (
120  compareTimeValues(p,tr.lower()) >= 0
121  && compareTimeValues(p,tr.upper()) < 0
122  );
123 }
124 
125 
126 template<class TimeType>
127 bool Rythmos::isInRange_oo(const TimeRange<TimeType> &tr, const TimeType &p)
128 {
129  return (
130  compareTimeValues(p,tr.lower()) > 0
131  && compareTimeValues(p,tr.upper()) < 0
132  );
133 }
134 
135 
136 #define RYTHMOS_TIME_RANGE_INSTANT(SCALAR) \
137  \
138  template class TimeRange< SCALAR >; \
139  \
140  template int compareTimeValues( const SCALAR &t1, const SCALAR &t2 ); \
141  template TimeRange< SCALAR > timeRange(const SCALAR lower, const SCALAR upper); \
142  template TimeRange< SCALAR > invalidTimeRange(); \
143  template std::ostream& operator<<( std::ostream& out, const TimeRange< SCALAR >& range ); \
144  template void asssertInTimeRange( const TimeRange<SCALAR > &timeRange, const SCALAR &time ); \
145  template bool isInRange_cc(const TimeRange< SCALAR > &tr, const SCALAR &p); \
146  template bool isInRange_oc(const TimeRange< SCALAR > &tr, const SCALAR &p); \
147  template bool isInRange_co(const TimeRange< SCALAR > &tr, const SCALAR &p); \
148  template bool isInRange_oo(const TimeRange< SCALAR > &tr, const SCALAR &p); \
149  template class TimeRange_cc< SCALAR >; \
150  template class TimeRange_co< SCALAR >; \
151  template class TimeRange_oo< SCALAR >; \
152  template class TimeRange_oc< SCALAR >;
153 
154 
155 #endif //RYTHMOS_TIME_RANGE_DEF_H
TimeRange< TimeType > timeRange(const TimeType my_lower, const TimeType my_upper)
Nonmember constructor.
int compareTimeValues(const TimeType &t1, const TimeType &t2)
Compare two times taking into account floating point errors.