9 #ifndef Tempus_TimeEventListIndex_impl_hpp
10 #define Tempus_TimeEventListIndex_impl_hpp
15 template<
class Scalar>
18 this->setType(
"List Index");
19 this->setName(
"TimeEventListIndex");
23 template<
class Scalar>
25 std::vector<int> indexList, std::string name)
27 this->setType(
"List Index");
28 if (name ==
"" && !indexList.empty()) {
29 std::ostringstream oss;
30 oss <<
"TimeEventListIndex (" << indexList_.front() <<
", ... ,"
31 << indexList_.back() <<
")";
32 this->setName(oss.str());
37 this->setIndexList(indexList);
41 template<
class Scalar>
44 indexList_ = indexList;
46 std::sort(indexList_.begin(), indexList_.end());
47 indexList_.erase(std::unique(
48 indexList_.begin(), indexList_.end()), indexList_.end());
53 template<
class Scalar>
56 if (indexList_.size() == 0) {
57 indexList_.push_back(index);
61 std::vector<int>::iterator it;
62 it = std::find(indexList_.begin(), indexList_.end(), index);
64 if (it != indexList_.end())
return;
66 it = std::upper_bound(indexList_.begin(), indexList_.end(), index);
67 indexList_.insert(it, index);
71 template<
class Scalar>
74 return (std::find(indexList_.begin(), indexList_.end(), index) != indexList_.end() );
78 template<
class Scalar>
81 return indexOfNextEvent(index) - index;
85 template<
class Scalar>
88 if (indexList_.size() == 0)
return this->getDefaultIndex();
91 if (index < indexList_.front())
return indexList_.front();
94 if (index >= indexList_.back())
return this->getDefaultIndex();
96 std::vector<int>::const_iterator it =
97 std::upper_bound(indexList_.begin(), indexList_.end(), index);
103 template<
class Scalar>
106 if (index1 > index2) {
112 if (indexList_.size() == 0)
return false;
115 if (index2 < indexList_.front() || indexList_.back() < index1)
return false;
117 Scalar indexEvent1 = indexOfNextEvent(index1);
118 Scalar indexEvent2 = indexOfNextEvent(index2);
120 if (indexEvent1 != indexEvent2)
return true;
123 if (index1 < indexEvent1 && indexEvent1 <= index2)
return true;
129 template<
class Scalar>
133 auto l_out = Teuchos::fancyOStream( out.
getOStream() );
135 l_out->setOutputToRootOnly(0);
137 *l_out <<
"TimeEventListIndex:" <<
"\n"
138 <<
" name = " << this->getName() <<
"\n"
139 <<
" Type = " << this->getType() <<
"\n"
141 if (!indexList_.empty()) {
142 for (
auto it = indexList_.begin(); it != indexList_.end()-1; ++it)
143 *l_out << *it <<
", ";
144 *l_out << *(indexList_.end()-1) << std::endl;
146 *l_out <<
"<empty>" << std::endl;
151 template<
class Scalar>
156 Teuchos::parameterList(
"Time Event List Index");
159 pl->
set(
"Name", this->getName());
160 pl->
set(
"Type", this->getType());
162 std::ostringstream list;
163 if (!indexList_.empty()) {
164 for (std::size_t i = 0; i < indexList_.size()-1; ++i)
165 list << indexList_[i] <<
", ";
166 list << indexList_[indexList_.size()-1];
168 pl->
set<std::string>(
"Index List", list.str(),
169 "Comma deliminated list of indices");
178 template<
class Scalar>
183 if (pl == Teuchos::null)
return teli;
186 pl->
get<std::string>(
"Type",
"List Index") !=
"List Index",
188 "Error - Time Event Type != 'List Index'. (='"
189 + pl->
get<std::string>(
"Type")+
"')\n");
193 teli->setName (pl->
get(
"Name",
"From createTimeEventListIndex"));
195 std::vector<int> indexList;
197 std::string str = pl->
get<std::string>(
"Index List");
198 std::string delimiters(
",");
199 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
200 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
201 while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
202 std::string token = str.substr(lastPos,pos-lastPos);
203 indexList.push_back(
int(std::stoi(token)));
204 if(pos==std::string::npos)
break;
206 lastPos = str.find_first_not_of(delimiters, pos);
207 pos = str.find_first_of(delimiters, lastPos);
209 teli->setIndexList(indexList);
216 #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)