Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_TimeEventRangeIndex_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
9 #ifndef Tempus_TimeEventRangeIndex_impl_hpp
10 #define Tempus_TimeEventRangeIndex_impl_hpp
11 
12 
13 namespace Tempus {
14 
15 template<class Scalar>
17  : start_(0), stop_(0), stride_(1)
18 {
19  this->setName("TimeEventRangeIndex");
20  setNumEvents();
21 }
22 
23 
24 template<class Scalar>
26  std::string name, int start, int stop, int stride)
27 {
28  this->setName(name);
29  this->setIndexRange(start, stop, stride);
30 }
31 
32 
33 template<class Scalar>
35 {
36  start_ = start;
37  if (stop_ < start_) {
38  stop_ = start_;
39  }
40  setNumEvents();
41 }
42 
43 
44 template<class Scalar>
46 {
47  stop_ = stop;
48  if (start_ > stop_) {
49  start_ = stop_;
50  setIndexStride(1);
51  }
52  setNumEvents();
53 }
54 
55 
56 template<class Scalar>
58 {
59  stride_ = stride;
60  if (stride_ < 1) {
61  stride_ = 1;
62  } else if (stride_ > (stop_ - start_)) {
63  stride_ = stop_ - start_;
64  }
65  setNumEvents();
66 }
67 
68 
69 template<class Scalar>
71 {
72  if (stride_ == 0 || start_ == stop_)
73  numEvents_ = 1;
74  else
75  numEvents_ = int((stop_ - start_) / stride_) + 1;
76 }
77 
78 
79 template<class Scalar>
81 {
82  return (indexToNextEvent(index) == 0);
83 }
84 
85 
86 template<class Scalar>
88 {
89  return indexOfNextEvent(index) - index;
90 }
91 
92 
93 template<class Scalar>
95 {
96  // Check if before or equal to the first index.
97  if (index <= start_) return start_;
98 
99  const int indexOfLast = start_ + (numEvents_-1) * stride_;
100  // Check if after or equal to last index.
101  if (indexOfLast <= index) return indexOfLast;
102 
103  // check if index is an event.
104  if ((index - start_) % stride_ == 0) return index;
105 
106  const int numStrides = (index - start_) / stride_ + 1;
107  const Scalar indexOfNext = start_ + numStrides * stride_;
108  return indexOfNext;
109 }
110 
111 
112 template<class Scalar>
113 bool TimeEventRangeIndex<Scalar>::eventInRangeIndex(int index1, int index2) const
114 {
115  if (index1 > index2) {
116  int tmp = index1;
117  index1 = index2;
118  index2 = tmp;
119  }
120 
121  const Scalar indexOfLast = start_ + (numEvents_-1) * stride_;
122  if (index2 < start_ || indexOfLast < index1) return false;
123 
124  Scalar indexEvent1 = indexOfNextEvent(index1);
125  Scalar indexEvent2 = indexOfNextEvent(index2);
126  // Check if the next index event is different for the two indices.
127  if (indexEvent1 != indexEvent2) return true;
128 
129  // Check if indices bracket index event.
130  if (index1 <= indexEvent1 && indexEvent1 <= index2) return true;
131 
132  return false;
133 }
134 
135 
136 template<class Scalar>
138 {
139  Teuchos::RCP<Teuchos::FancyOStream> out =
140  Teuchos::VerboseObjectBase::getDefaultOStream();
141  *out << "TimeEventRange:" << "\n"
142  << "name = " << this->getName() << "\n"
143  << "start_ = " << start_ << "\n"
144  << "stop_ = " << stop_ << "\n"
145  << "stride_ = " << stride_ << "\n"
146  << "numEvents_ = " << numEvents_ << std::endl;
147 }
148 
149 
150 
151 } // namespace Tempus
152 #endif // Tempus_TimeEventRangeIndex_impl_hpp
virtual void setName(std::string name)
virtual int indexToNextEvent(int index) const
How many indices until the next event. Negative indicating the last event is in the past...
virtual void describe() const
Describe member data.
virtual bool eventInRangeIndex(int index1, int index2) const
Test if an event occurs within the index range.
virtual int indexOfNextEvent(int index) const
Index of the next event. Negative indicating the last event is in the past.
virtual bool isIndex(int index) const
Test if index is a time event.