9 #ifndef Tempus_TimeEventComposite_decl_hpp
10 #define Tempus_TimeEventComposite_decl_hpp
15 #include "Tempus_config.hpp"
18 #include "Tempus_TimeEventRange.hpp"
19 #include "Tempus_TimeEventRangeIndex.hpp"
20 #include "Tempus_TimeEventList.hpp"
21 #include "Tempus_TimeEventListIndex.hpp"
30 template <
class Scalar>
37 this->
setName(
"TimeEventComposite");
42 std::string name =
"TimeEventComposite")
54 virtual std::vector<Teuchos::RCP<TimeEventBase<Scalar> > >
getTimeEvents()
58 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > te =
timeEvents_;
83 virtual bool isTime(Scalar time)
const
85 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > timeEvents;
86 return isTime(time, timeEvents);
106 if (e->isTime(time)) timeEvents.push_back(e);
108 return (!timeEvents.empty());
151 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > timeEvents;
179 typedef std::pair<Scalar, Teuchos::RCP<TimeEventBase<Scalar> > > TEPAIR;
180 std::vector<TEPAIR> timeEventPair;
182 timeEventPair.push_back(std::make_pair(e->timeOfNextEvent(time), e));
186 auto compare = [](TEPAIR a, TEPAIR b) {
return a.first < b.first; };
187 std::stable_sort(timeEventPair.begin(), timeEventPair.end(), compare);
190 Scalar tone = timeEventPair.front().first;
193 for (
auto it = timeEventPair.begin(); it != timeEventPair.end(); ++it) {
194 if ((*it).second->isTime(tone)) timeEvents.push_back((*it).second);
215 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > timeEvents;
239 Scalar time1, Scalar time2,
242 typedef std::pair<Scalar, Teuchos::RCP<TimeEventBase<Scalar> > > TEPAIR;
243 std::vector<TEPAIR> timeEventPair;
245 if (e->eventInRange(time1, time2))
246 timeEventPair.push_back(std::make_pair(e->timeOfNextEvent(time1), e));
249 auto compare = [](TEPAIR a, TEPAIR b) {
return a.first < b.first; };
250 std::stable_sort(timeEventPair.begin(), timeEventPair.end(), compare);
253 for (
auto& e : timeEventPair) timeEvents.push_back(e.second);
255 return (!timeEvents.empty());
268 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > timeEvents;
269 return isIndex(index, timeEvents);
288 if (e->isIndex(index)) timeEvents.push_back(e);
290 return (!timeEvents.empty());
329 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > timeEvents;
351 typedef std::pair<int, Teuchos::RCP<TimeEventBase<Scalar> > > TEPAIR;
352 std::vector<TEPAIR> timeEventPair;
354 timeEventPair.push_back(std::make_pair(e->indexOfNextEvent(index), e));
358 auto compare = [](TEPAIR a, TEPAIR b) {
return a.first < b.first; };
359 std::stable_sort(timeEventPair.begin(), timeEventPair.end(), compare);
362 int ione = timeEventPair.front().first;
365 for (
auto it = timeEventPair.begin(); it != timeEventPair.end(); ++it) {
366 if ((*it).second->isIndex(ione)) timeEvents.push_back((*it).second);
385 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > timeEvents;
407 int index1,
int index2,
410 typedef std::pair<int, Teuchos::RCP<TimeEventBase<Scalar> > > TEPAIR;
411 std::vector<TEPAIR> timeEventPair;
413 if (e->eventInRangeIndex(index1, index2))
414 timeEventPair.push_back(std::make_pair(e->indexOfNextEvent(index1), e));
417 auto compare = [](TEPAIR a, TEPAIR b) {
return a.first < b.first; };
418 std::stable_sort(timeEventPair.begin(), timeEventPair.end(), compare);
421 for (
auto& e : timeEventPair) timeEvents.push_back(e.second);
423 return (!timeEvents.empty());
432 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > timeEvents;
449 Scalar largestAbsTol =
timeEvents_.front()->getAbsTol();
452 if (e->getAbsTol() > largestAbsTol) largestAbsTol = e->getAbsTol();
454 for (
auto& e : timeEvents_)
455 if (e->getAbsTol() - largestAbsTol < largestAbsTol * 1.0e-14)
456 timeEvents.push_back(e);
458 return largestAbsTol;
470 std::vector<Teuchos::RCP<TimeEventBase<Scalar> > > timeEvents;
489 if (e->getLandOnExactly()) timeEvents.push_back(e);
491 return (!timeEvents.empty());
506 std::string name = timeEvent->getName();
527 void remove(std::string name)
529 for (std::size_t i = 0; i <
timeEvents_.size(); ++i) {
547 for (std::size_t i = 0; i <
timeEvents_.size(); ++i)
550 return Teuchos::null;
562 std::stringstream tecList;
563 for (std::size_t i = 0; i <
timeEvents_.size(); ++i) {
567 return tecList.str();
574 auto l_out = Teuchos::fancyOStream(out.
getOStream());
576 l_out->setOutputToRootOnly(0);
578 *l_out <<
"TimeEventComposite:"
580 <<
" name = " << this->
getName() <<
"\n"
581 <<
" Type = " << this->
getType() <<
"\n"
582 <<
" Number of TimeEvents = " << this->
getSize() <<
"\n"
585 *l_out <<
"--------------------------------------------" << std::endl;
587 (*e).describe(*l_out, verbLevel);
588 *l_out <<
"--------------------------------------------" << std::endl;
605 Teuchos::parameterList(
"Time Event Composite");
612 for (
auto& s :
timeEvents_) pl->
set(s->getName(), *s->getValidParameters());
638 template <
class Scalar>
646 if (pList == Teuchos::null || pList->
numParams() == 0)
return tec;
649 pList->
get<std::string>(
"Type",
"Composite") !=
"Composite",
651 "Error - Time Event Type != 'Composite'. (='" +
652 pList->
get<std::string>(
"Type") +
"')\n");
654 tec->setName(pList->
get(
"Name",
"From createTimeEventComposite"));
657 std::vector<std::string> teList;
659 std::string str = pList->
get<std::string>(
"Time Events");
660 std::string delimiters(
",");
661 const char* WhiteSpace =
" \t\v\r\n";
663 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
665 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
666 while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
668 std::string token = str.substr(lastPos, pos - lastPos);
670 std::size_t start = token.find_first_not_of(WhiteSpace);
671 std::size_t end = token.find_last_not_of(WhiteSpace);
673 (start == end ? std::string() : token.substr(start, end - start + 1));
675 teList.push_back(token);
676 if (pos == std::string::npos)
break;
678 lastPos = str.find_first_not_of(delimiters, pos);
679 pos = str.find_first_of(delimiters, lastPos);
683 for (
auto teName : teList) {
684 RCP<ParameterList> pl =
687 auto timeEventType = pl->get<std::string>(
"Type",
"Unknown");
688 if (timeEventType ==
"Range") {
689 tec->add(createTimeEventRange<Scalar>(pl));
691 else if (timeEventType ==
"Range Index") {
692 tec->add(createTimeEventRangeIndex<Scalar>(pl));
694 else if (timeEventType ==
"List") {
695 tec->add(createTimeEventList<Scalar>(pl));
697 else if (timeEventType ==
"List Index") {
698 tec->add(createTimeEventListIndex<Scalar>(pl));
701 RCP<Teuchos::FancyOStream> out =
702 Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
703 out->setOutputToRootOnly(0);
705 *out <<
"Warning -- createTimeEventComposite() - Unknown Time Event "
707 <<
"'Type' = '" << timeEventType <<
"'\n"
708 <<
"Should call add() with this "
709 <<
"(app-specific?) Time Event.\n"
714 if (tec->getSize() == 0) {
715 RCP<Teuchos::FancyOStream> out =
716 Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
717 out->setOutputToRootOnly(0);
719 *out <<
"Warning -- createTimeEventComposite() - Did not\n"
720 <<
" find/recognize any TimeEvents to create!\n"
721 <<
" If there is a app-specific TimeEvent,\n"
722 <<
" explicitly add it to this TimeEventComposite.\n"
731 #endif // Tempus_TimeEventComposite_decl_hpp
virtual int indexOfNextEvent(int index, std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
Return the index of the next event following the input index plus the constraining TimeEvent(s)...
virtual std::string getType() const
Return the type of TimeEvent.
virtual Scalar getDefaultTime() const
Return the default time used for TimeEvents.
TimeEventComposite(std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > te, std::string name="TimeEventComposite")
Construct with full argument list of data members.
virtual bool isIndex(int index) const
Test if index is a time event.
virtual void setType(std::string s)
TimeEventComposite()
Default Constructor.
std::string getTimeEventNames() const
Return a string of the names of Time Events (comma separated).
virtual void setName(std::string name)
Set the name of the TimeEvent.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual bool isIndex(int index, std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
Test if index is a time event plus the constraining TimeEvent(s).
virtual bool getLandOnExactly(std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
Return if the time events need to be landed on exactly plus the constraining TimeEvent(s).
virtual Scalar timeOfNextEvent(Scalar time, std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
Return the time of the next time event and constraining TimeEvent(s).
Ordinal numParams() const
virtual bool isTime(Scalar time) const
Test if time is near a TimeEvent (within tolerance).
virtual void setTimeEvents(std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > te)
Set the TimeEvents.
virtual Scalar timeToNextEvent(Scalar time, std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
How much time until the next event plus the constraining TimeEvent(s).
virtual int getDefaultIndex() const
Return the default index used by TimeEvents.
virtual bool isTime(Scalar time, std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
Test if time is near a TimeEvent (within tolerance) plus the constraining TimeEvent(s).
void add(Teuchos::RCP< TimeEventBase< Scalar > > timeEvent)
Add TimeEvent to the TimeEvent vector.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual bool eventInRange(Scalar time1, Scalar time2) const
Test if an event occurs within the time range.
virtual Scalar getAbsTol() const
Return the largest absolute tolerance from all the TimeEvents.
void clear()
Clear the TimeEvent vector.
virtual Scalar timeToNextEvent(Scalar time) const
How much time until the next event.
This composite TimeEvent loops over added TimeEvents.
std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > timeEvents_
std::size_t getSize() const
Return the size of the TimeEvent vector.
virtual bool eventInRangeIndex(int index1, int index2) const
Test if an event occurs within the index range.
virtual bool eventInRangeIndex(int index1, int index2, std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
Test if an event occurs within the index range plus the constraining TimeEvent(s).
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.
This class defines time events which can be used to "trigger" an action.
virtual std::string getName() const
Return the name of the TimeEvent.
Teuchos::RCP< TimeEventBase< Scalar > > find(std::string name)
Find TimeEvent based on name.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Describe member data.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
virtual std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > getTimeEvents() const
Get a copy of the current set of TimeEvents.
virtual Scalar timeOfNextEvent(Scalar time) const
Return the time of the next event following the input time.
virtual ~TimeEventComposite()
Destructor.
virtual int indexToNextEvent(int index, std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
How many indices until the next event.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ParameterList & setName(const std::string &name)
virtual bool eventInRange(Scalar time1, Scalar time2, std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
Test if an event occurs within the time range plus the constraining TimeEvent(s). ...
Teuchos::RCP< TimeEventComposite< Scalar > > createTimeEventComposite(Teuchos::RCP< Teuchos::ParameterList > const &pList)
TimeEventComposite nonmember constructor via ParameterList.
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a valid ParameterList with current settings.
virtual bool getLandOnExactly() const
Return if the time events need to be landed on exactly.
virtual Scalar getAbsTol(std::vector< Teuchos::RCP< TimeEventBase< Scalar > > > &timeEvents) const
Return the largest absolute tolerance from all the TimeEvents plus the constraining TimeEvent(s)...