10#ifndef Tempus_TimeEventListIndex_impl_hpp
11#define Tempus_TimeEventListIndex_impl_hpp
15template <
class Scalar>
18 this->setType(
"List Index");
19 this->setName(
"TimeEventListIndex");
22template <
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);
40template <
class Scalar>
44 indexList_ = indexList;
46 std::sort(indexList_.begin(), indexList_.end());
47 indexList_.erase(std::unique(indexList_.begin(), indexList_.end()),
52template <
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);
69template <
class Scalar>
72 return (std::find(indexList_.begin(), indexList_.end(), index) !=
76template <
class Scalar>
79 return indexOfNextEvent(index) - index;
82template <
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);
99template <
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;
124template <
class Scalar>
126 Teuchos::FancyOStream &out,
const Teuchos::EVerbosityLevel verbLevel)
const
128 auto l_out = Teuchos::fancyOStream(out.getOStream());
129 Teuchos::OSTab ostab(*l_out, 2,
"TimeEventListIndex");
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;
147template <
class Scalar>
148Teuchos::RCP<const Teuchos::ParameterList>
151 Teuchos::RCP<Teuchos::ParameterList> pl =
152 Teuchos::parameterList(
"Time Event List Index");
154 pl->setName(this->getName());
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");
173template <
class Scalar>
175 Teuchos::RCP<Teuchos::ParameterList> pl)
178 if (pl == Teuchos::null)
return teli;
180 TEUCHOS_TEST_FOR_EXCEPTION(
181 pl->get<std::string>(
"Type",
"List Index") !=
"List Index",
183 "Error - Time Event Type != 'List Index'. (='" +
184 pl->get<std::string>(
"Type") +
"')\n");
186 pl->validateParametersAndSetDefaults(*teli->getValidParameters());
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);
TimeEventListIndex specifies a list of index events.
TimeEventListIndex()
Default constructor.
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)
Set the vector of event indices.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Describe member data.
virtual void addIndex(int index)
Add the index to event vector.
virtual bool isIndex(int index) const
Test if index is a time event.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a valid ParameterList with current settings.
virtual int indexToNextEvent(int index) const
How many indices until the next event.
virtual int indexOfNextEvent(int index) const
Return the index of the next event following the input index.
Teuchos::RCP< TimeEventListIndex< Scalar > > createTimeEventListIndex(Teuchos::RCP< Teuchos::ParameterList > pList)
Nonmember Constructor via ParameterList.