Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_TimeEventListIndex_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_TimeEventListIndex_impl_hpp
10 #define Tempus_TimeEventListIndex_impl_hpp
11 
12 
13 namespace Tempus {
14 
15 template<class Scalar>
17 {
18  this->setName("TimeEventListIndex");
19 }
20 
21 
22 template<class Scalar>
24  std::string name, std::vector<int> indexList)
25 {
26  this->setName(name);
27  this->setIndexList(indexList);
28 }
29 
30 
31 template<class Scalar>
32 void TimeEventListIndex<Scalar>::setIndexList(std::vector<int> indexList, bool sort)
33 {
34  indexList_ = indexList;
35  if (sort) {
36  std::sort(indexList_.begin(), indexList_.end());
37  indexList_.erase(std::unique(
38  indexList_.begin(), indexList_.end()), indexList_.end());
39  }
40 }
41 
42 
43 template<class Scalar>
45 {
46  if (indexList_.size() == 0) {
47  indexList_.push_back(index);
48  return;
49  }
50 
51  std::vector<int>::iterator it;
52  it = std::find(indexList_.begin(), indexList_.end(), index);
53  // Check if index is already in list.
54  if (it != indexList_.end()) return;
55 
56  it = std::upper_bound(indexList_.begin(), indexList_.end(), index);
57  indexList_.insert(it, index);
58 }
59 
60 
61 template<class Scalar>
63 {
64  return (indexToNextEvent(index) == 0);
65 }
66 
67 
68 template<class Scalar>
70 {
71  return indexOfNextEvent(index) - index; // Neg. indicating in the past.
72 }
73 
74 
75 template<class Scalar>
77 {
78  if (indexList_.size() == 0) return this->getDefaultIndex();
79 
80  // Check if before first event.
81  if (indexList_.front() >= index) return indexList_.front();
82 
83  // Check if after last event.
84  if (indexList_.back() <= index) return indexList_.back();
85 
86  std::vector<int>::const_iterator it =
87  std::upper_bound(indexList_.begin(), indexList_.end(), index);
88 
89  // Check if left-side index event
90  const Scalar indexOfLeftEvent = *(it-1);
91  if (indexOfLeftEvent == index) return indexOfLeftEvent;
92 
93  // Otherwise it is the next event.
94  return *it;
95 }
96 
97 
98 template<class Scalar>
99 bool TimeEventListIndex<Scalar>::eventInRangeIndex(int index1, int index2) const
100 {
101  if (index1 > index2) {
102  int tmp = index1;
103  index1 = index2;
104  index2 = tmp;
105  }
106 
107  if (indexList_.size() == 0) return false;
108 
109  // Check if range is completely outside index events.
110  if (index2 < indexList_.front() || indexList_.back() < index1) return false;
111 
112  Scalar indexEvent1 = indexOfNextEvent(index1);
113  Scalar indexEvent2 = indexOfNextEvent(index2);
114  // Check if the next index event is different for the two indices.
115  if (indexEvent1 != indexEvent2) return true;
116 
117  // Check if indices bracket index event.
118  if (index1 <= indexEvent1 && indexEvent1 <= index2) return true;
119 
120  return false;
121 }
122 
123 
124 template<class Scalar>
126 {
127  Teuchos::RCP<Teuchos::FancyOStream> out =
128  Teuchos::VerboseObjectBase::getDefaultOStream();
129  *out << "TimeEventListIndex:" << "\n"
130  << "name = " << this->getName() << "\n"
131  << "IndexList_ = " << std::endl;
132  for (auto it = indexList_.begin(); it != indexList_.end()-1; ++it)
133  *out << *it << ", ";
134  *out << *(indexList_.end()-1) << "\n";
135 }
136 
137 
138 } // namespace Tempus
139 #endif // Tempus_TimeEventListIndex_impl_hpp
virtual int indexToNextEvent(int index) const
How many indices until the next event. Negative indicating the last event is in the past...
virtual bool isIndex(int index) const
Test if index is near a index event (within tolerance).
virtual int indexOfNextEvent(int index) const
Index of 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 void setIndexList(std::vector< int > indexList, bool sort=true)