9 #ifndef Tempus_TimeEventListIndex_impl_hpp
10 #define Tempus_TimeEventListIndex_impl_hpp
14 template <
class Scalar>
17 this->setType(
"List Index");
18 this->setName(
"TimeEventListIndex");
21 template <
class Scalar>
25 this->setType(
"List Index");
26 if (name ==
"" && !indexList.empty()) {
27 std::ostringstream oss;
28 oss <<
"TimeEventListIndex (" << indexList_.front() <<
", ... ,"
29 << indexList_.back() <<
")";
30 this->setName(oss.str());
36 this->setIndexList(indexList);
39 template <
class Scalar>
43 indexList_ = indexList;
45 std::sort(indexList_.begin(), indexList_.end());
46 indexList_.erase(std::unique(indexList_.begin(), indexList_.end()),
51 template <
class Scalar>
54 if (indexList_.size() == 0) {
55 indexList_.push_back(index);
59 std::vector<int>::iterator it;
60 it = std::find(indexList_.begin(), indexList_.end(), index);
62 if (it != indexList_.end())
return;
64 it = std::upper_bound(indexList_.begin(), indexList_.end(), index);
65 indexList_.insert(it, index);
68 template <
class Scalar>
71 return (std::find(indexList_.begin(), indexList_.end(), index) !=
75 template <
class Scalar>
78 return indexOfNextEvent(index) - index;
81 template <
class Scalar>
84 if (indexList_.size() == 0)
return this->getDefaultIndex();
87 if (index < indexList_.front())
return indexList_.front();
90 if (index >= indexList_.back())
return this->getDefaultIndex();
92 std::vector<int>::const_iterator it =
93 std::upper_bound(indexList_.begin(), indexList_.end(), index);
98 template <
class Scalar>
101 if (index1 > index2) {
107 if (indexList_.size() == 0)
return false;
110 if (index2 < indexList_.front() || indexList_.back() < index1)
return false;
112 Scalar indexEvent1 = indexOfNextEvent(index1);
113 Scalar indexEvent2 = indexOfNextEvent(index2);
115 if (indexEvent1 != indexEvent2)
return true;
118 if (index1 < indexEvent1 && indexEvent1 <= index2)
return true;
123 template <
class Scalar>
127 auto l_out = Teuchos::fancyOStream(out.
getOStream());
129 l_out->setOutputToRootOnly(0);
131 *l_out <<
"TimeEventListIndex:"
133 <<
" name = " << this->getName() <<
"\n"
134 <<
" Type = " << this->getType() <<
"\n"
136 if (!indexList_.empty()) {
137 for (
auto it = indexList_.begin(); it != indexList_.end() - 1; ++it)
138 *l_out << *it <<
", ";
139 *l_out << *(indexList_.end() - 1) << std::endl;
142 *l_out <<
"<empty>" << std::endl;
146 template <
class Scalar>
151 Teuchos::parameterList(
"Time Event List Index");
154 pl->
set(
"Name", this->getName());
155 pl->
set(
"Type", this->getType());
157 std::ostringstream list;
158 if (!indexList_.empty()) {
159 for (std::size_t i = 0; i < indexList_.size() - 1; ++i)
160 list << indexList_[i] <<
", ";
161 list << indexList_[indexList_.size() - 1];
163 pl->
set<std::string>(
"Index List", list.str(),
164 "Comma deliminated list of indices");
172 template <
class Scalar>
177 if (pl == Teuchos::null)
return teli;
180 pl->
get<std::string>(
"Type",
"List Index") !=
"List Index",
182 "Error - Time Event Type != 'List Index'. (='" +
183 pl->
get<std::string>(
"Type") +
"')\n");
187 teli->setName(pl->
get(
"Name",
"From createTimeEventListIndex"));
189 std::vector<int> indexList;
191 std::string str = pl->
get<std::string>(
"Index List");
192 std::string delimiters(
",");
193 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
194 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
195 while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
196 std::string token = str.substr(lastPos, pos - lastPos);
197 indexList.push_back(
int(std::stoi(token)));
198 if (pos == std::string::npos)
break;
200 lastPos = str.find_first_not_of(delimiters, pos);
201 pos = str.find_first_of(delimiters, lastPos);
203 teli->setIndexList(indexList);
209 #endif // Tempus_TimeEventListIndex_impl_hpp
TimeEventListIndex()
Default constructor.
virtual int indexToNextEvent(int index) const
How many indices until the next event.
T & get(const std::string &name, T def_value)
virtual bool isIndex(int index) const
Test if index is a time event.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
virtual int indexOfNextEvent(int index) const
Return the index of the next event following the input index.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
TimeEventListIndex specifies a list of index events.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a valid ParameterList with current settings.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void addIndex(int index)
Add the index to event vector.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Teuchos::RCP< TimeEventListIndex< Scalar > > createTimeEventListIndex(Teuchos::RCP< Teuchos::ParameterList > pList)
Nonmember Constructor via ParameterList.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Describe member data.
virtual bool eventInRangeIndex(int index1, int index2) const
Test if an event occurs within the index range.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
virtual void setIndexList(std::vector< int > indexList, bool sort=true)
Set the vector of event indices.
ParameterList & setName(const std::string &name)