10 #ifndef Tempus_TimeEventListIndex_impl_hpp
11 #define Tempus_TimeEventListIndex_impl_hpp
15 template <
class Scalar>
18 this->setType(
"List Index");
19 this->setName(
"TimeEventListIndex");
22 template <
class Scalar>
26 this->setType(
"List Index");
27 if (name ==
"" && !indexList.empty()) {
28 std::ostringstream oss;
29 oss <<
"TimeEventListIndex (" << indexList_.front() <<
", ... ,"
30 << indexList_.back() <<
")";
31 this->setName(oss.str());
37 this->setIndexList(indexList);
40 template <
class Scalar>
44 indexList_ = indexList;
46 std::sort(indexList_.begin(), indexList_.end());
47 indexList_.erase(std::unique(indexList_.begin(), indexList_.end()),
52 template <
class Scalar>
55 if (indexList_.size() == 0) {
56 indexList_.push_back(index);
60 std::vector<int>::iterator it;
61 it = std::find(indexList_.begin(), indexList_.end(), index);
63 if (it != indexList_.end())
return;
65 it = std::upper_bound(indexList_.begin(), indexList_.end(), index);
66 indexList_.insert(it, index);
69 template <
class Scalar>
72 return (std::find(indexList_.begin(), indexList_.end(), index) !=
76 template <
class Scalar>
79 return indexOfNextEvent(index) - index;
82 template <
class Scalar>
85 if (indexList_.size() == 0)
return this->getDefaultIndex();
88 if (index < indexList_.front())
return indexList_.front();
91 if (index >= indexList_.back())
return this->getDefaultIndex();
93 std::vector<int>::const_iterator it =
94 std::upper_bound(indexList_.begin(), indexList_.end(), index);
99 template <
class Scalar>
102 if (index1 > index2) {
108 if (indexList_.size() == 0)
return false;
111 if (index2 < indexList_.front() || indexList_.back() < index1)
return false;
113 Scalar indexEvent1 = indexOfNextEvent(index1);
114 Scalar indexEvent2 = indexOfNextEvent(index2);
116 if (indexEvent1 != indexEvent2)
return true;
119 if (index1 < indexEvent1 && indexEvent1 <= index2)
return true;
124 template <
class Scalar>
128 auto l_out = Teuchos::fancyOStream(out.
getOStream());
130 l_out->setOutputToRootOnly(0);
132 *l_out <<
"TimeEventListIndex:"
134 <<
" name = " << this->getName() <<
"\n"
135 <<
" Type = " << this->getType() <<
"\n"
137 if (!indexList_.empty()) {
138 for (
auto it = indexList_.begin(); it != indexList_.end() - 1; ++it)
139 *l_out << *it <<
", ";
140 *l_out << *(indexList_.end() - 1) << std::endl;
143 *l_out <<
"<empty>" << std::endl;
147 template <
class Scalar>
152 Teuchos::parameterList(
"Time Event List Index");
155 pl->
set(
"Name", this->getName());
156 pl->
set(
"Type", this->getType());
158 std::ostringstream list;
159 if (!indexList_.empty()) {
160 for (std::size_t i = 0; i < indexList_.size() - 1; ++i)
161 list << indexList_[i] <<
", ";
162 list << indexList_[indexList_.size() - 1];
164 pl->
set<std::string>(
"Index List", list.str(),
165 "Comma deliminated list of indices");
173 template <
class Scalar>
178 if (pl == Teuchos::null)
return teli;
181 pl->
get<std::string>(
"Type",
"List Index") !=
"List Index",
183 "Error - Time Event Type != 'List Index'. (='" +
184 pl->
get<std::string>(
"Type") +
"')\n");
188 teli->setName(pl->
get(
"Name",
"From createTimeEventListIndex"));
190 std::vector<int> indexList;
192 std::string str = pl->
get<std::string>(
"Index List");
193 std::string delimiters(
",");
194 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
195 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
196 while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
197 std::string token = str.substr(lastPos, pos - lastPos);
198 indexList.push_back(
int(std::stoi(token)));
199 if (pos == std::string::npos)
break;
201 lastPos = str.find_first_not_of(delimiters, pos);
202 pos = str.find_first_of(delimiters, lastPos);
204 teli->setIndexList(indexList);
210 #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.
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.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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)