9 #ifndef Tempus_SolutionHistory_impl_hpp
10 #define Tempus_SolutionHistory_impl_hpp
12 #include "Teuchos_StandardParameterEntryValidators.hpp"
15 #include "Thyra_VectorStdOps.hpp"
22 template <
class Scalar>
33 template <
class Scalar>
42 setInterpolator(interpolator);
43 setStorageType(storageType);
44 setStorageLimit(storageLimit);
46 isInitialized_ =
false;
47 if (getNumStates() > 0) isInitialized_ =
true;
50 template <
class Scalar>
55 if (Teuchos::as<int>(history_->size() + 1) > storageLimit_) {
56 switch (storageType_) {
59 true, std::logic_error,
60 "Error - Storage type is STORAGE_TYPE_INVALID.\n");
66 if (state->getTime() >= history_->front()->getTime()) {
69 history_->erase(history_->begin());
75 *out <<
"Warning, state is older than oldest state in history. "
76 <<
"State not added!" << std::endl;
84 "Error - unknown storage type.\n");
89 if (history_->size() == 0) {
90 history_->push_back(state);
93 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
94 state_it = history_->begin();
96 const Scalar newTime = state->getTime();
97 for (; state_it < history_->end(); state_it++) {
98 const Scalar shTime = (*state_it)->getTime();
100 const Scalar denom = std::max(std::fabs(shTime), std::fabs(newTime));
101 if (std::fabs(newTime - shTime) < 1.0e-14 * denom) {
105 *out <<
"Warning, state to be added matches state in history. "
106 <<
"State not added!" << std::endl;
108 *out <<
"===============" << std::endl;
109 *out <<
"Added SolutionState -- ";
111 *out <<
"===============" << std::endl;
118 if (newTime < shTime)
break;
120 if (!equal) history_->insert(state_it, state);
124 getNumStates() <= 0, std::logic_error,
125 "Error - SolutionHistory::addState() Invalid history size!\n");
130 template <
class Scalar>
136 auto cs = getCurrentState();
138 state->setIndex(cs->getIndex() + 1);
140 state->setTime(cs->getTime() + cs->getTimeStep());
141 state->setTimeStep(cs->getTimeStep());
145 workingState_ = (*history_)[getNumStates() - 1];
148 template <
class Scalar>
152 if (history_->size() != 0) {
153 auto state_it = history_->rbegin();
154 for (; state_it < history_->rend(); state_it++) {
155 if (state->getTime() == (*state_it)->getTime())
break;
159 "Error - removeState() Could not remove state = "
160 << (*state_it)->description());
163 history_->erase(std::next(state_it).base());
168 template <
class Scalar>
172 removeState(tmpState);
175 template <
class Scalar>
177 const Scalar time)
const
181 if (history_->size() > 0)
182 scale = (*history_)[history_->size() - 1]->getTime();
185 const Scalar absTol = scale * numericalTol<Scalar>();
187 !(minTime() - absTol <= time && time <= maxTime() + absTol),
189 "Error - SolutionHistory::findState() Requested time out of range!\n"
191 << minTime() <<
", " << maxTime()
196 auto state_it = history_->begin();
197 for (; state_it < history_->end(); ++state_it) {
201 TEUCHOS_TEST_FOR_EXCEPTION(state_it == history_->end(), std::logic_error,
202 "Error - SolutionHistory::findState()!\n"
203 " Did not find a SolutionState with time = "
204 << time << std::endl);
209 template <
class Scalar>
211 const Scalar time)
const
214 interpolate<Scalar>(*interpolator_, history_, time, state_out.
get());
218 template <
class Scalar>
222 interpolate<Scalar>(*interpolator_, history_, time, state_out);
226 template <
class Scalar>
229 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::SolutionHistory::initWorkingState()");
232 getCurrentState() == Teuchos::null, std::logic_error,
233 "Error - SolutionHistory::initWorkingState()\n"
234 "Can not initialize working state without a current state!\n");
238 if (getWorkingState(
false) != Teuchos::null)
return;
241 if (getNumStates() < storageLimit_) {
243 newState = getCurrentState()->clone();
247 newState = (*history_)[0];
248 history_->erase(history_->begin());
249 if (getNumStates() > 0) newState->copy(getCurrentState());
254 addWorkingState(newState);
259 template <
class Scalar>
262 auto ws = getWorkingState();
265 ws->setNFailures(std::max(0, ws->getNFailures() - 1));
266 ws->setNConsecutiveFailures(0);
269 ws->setIsInterpolated(
false);
270 workingState_ = Teuchos::null;
274 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::promoteWorkingState()");
275 *out <<
"Warning - WorkingState is not passing, so not promoted!\n"
280 template <
class Scalar>
284 this->setName(sh->getName());
287 auto sh_history = sh->getHistory();
288 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
289 state_it = sh_history->begin();
290 for (; state_it < sh_history->end(); state_it++) this->addState(*state_it);
294 this->setInterpolator(interpolator);
296 this->setStorageType(sh->getStorageType());
297 this->setStorageLimit(sh->getStorageLimit());
300 template <
class Scalar>
303 storageLimit_ = std::max(1, storage_limit);
307 if (storage_limit != 1) {
310 *out <<
"Warning - 'Storage Limit' for 'Keep Newest' is 1.\n"
311 <<
" (Storage Limit = " << storage_limit <<
"). Resetting to 1."
317 if (storage_limit != 2) {
320 *out <<
"Warning - 'Storage Limit' for 'Undo' is 2.\n"
321 <<
" (Storage Limit = " << storage_limit <<
"). Resetting to 2."
327 storageLimit_ = storage_limit;
330 storageLimit_ = std::numeric_limits<int>::max();
334 (Teuchos::as<int>(history_->size()) > storageLimit_), std::logic_error,
335 "Error - requested storage limit = "
337 <<
" is smaller than the current number of states stored = "
338 << history_->size() <<
"!\n");
340 isInitialized_ =
false;
343 template <
class Scalar>
352 setStorageLimit(std::numeric_limits<int>::max());
353 isInitialized_ =
false;
356 template <
class Scalar>
359 if (s ==
"Keep Newest") {
363 else if (s ==
"Undo") {
367 else if (s ==
"Static") {
370 else if (s ==
"Unlimited") {
375 true, std::logic_error,
376 "Error - Unknown 'Storage Type' = '" << s <<
"'\n");
378 isInitialized_ =
false;
381 template <
class Scalar>
384 std::string s =
"Invalid";
396 template <
class Scalar>
401 const int m = history_->size();
405 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexN");
406 *out <<
"Warning - getStateTimeIndexN() No states in SolutionHistory!"
411 state = (*history_)[m - 1];
416 template <
class Scalar>
421 const int m = history_->size();
425 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexNM1");
426 *out <<
"Warning - getStateTimeIndexNM1() Not enough states in "
427 <<
"SolutionHistory! Size of history = " << getNumStates()
432 const int n = (*history_)[m - 1]->getIndex();
433 const int nm1 = (*history_)[m - 2]->getIndex();
440 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexNM1");
441 *out <<
"Warning - getStateTimeIndexNM1() Timestep index n-1 is not in "
442 <<
"SolutionHistory!\n"
443 <<
" (n)th index = " << n <<
"\n"
444 <<
" (n-1)th index = " << nm1 << std::endl;
448 state = (*history_)[m - 2];
455 template <
class Scalar>
460 const int m = history_->size();
464 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexNM2");
465 *out <<
"Warning - getStateTimeIndexNM2() Not enough states in "
466 <<
"SolutionHistory! Size of history = " << getNumStates()
471 const int n = (*history_)[m - 1]->getIndex();
472 const int nm2 = (*history_)[m - 3]->getIndex();
477 const int nm1 = (*history_)[m - 2]->getIndex();
479 state = (*history_)[m - 2];
483 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexNM2");
484 *out <<
"Warning - getStateTimeIndexNM2() Timestep index n-2 is not in "
485 <<
"SolutionHistory!\n"
486 <<
" (n)th index = " << n <<
"\n"
487 <<
" (n-2)th index = " << nm2 << std::endl;
491 state = (*history_)[m - 3];
498 template <
class Scalar>
500 int index,
bool warn)
const
502 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
503 state_it = history_->begin();
504 for (; state_it < history_->end(); state_it++) {
505 if ((*state_it)->getIndex() == index)
break;
509 if (state_it == history_->end()) {
512 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndex");
513 *out <<
"Warning - getStateTimeIndex() Timestep index is not in "
514 <<
"SolutionHistory!\n"
515 <<
" index = " << index << std::endl;
524 template <
class Scalar>
527 return (
"Tempus::SolutionHistory - '" + name_ +
"'");
530 template <
class Scalar>
534 auto l_out = Teuchos::fancyOStream(out.
getOStream());
536 l_out->setOutputToRootOnly(0);
538 *l_out <<
"\n--- " << this->description() <<
" ---" << std::endl;
540 if ((Teuchos::as<int>(verbLevel) ==
545 *l_out <<
" storageLimit = " << storageLimit_ << std::endl;
546 *l_out <<
" storageType = " << getStorageTypeString() << std::endl;
547 *l_out <<
" number of states = " << history_->size() << std::endl;
548 if (history_->size() > 0) {
549 *l_out <<
" time range = (" << history_->front()->getTime() <<
", "
550 << history_->back()->getTime() <<
")" << std::endl;
555 for (
int i = 0; i < (int)history_->size(); ++i)
556 (*history_)[i]->describe(*l_out, verbLevel);
558 *l_out << std::string(this->description().length() + 8,
'-') << std::endl;
561 template <
class Scalar>
566 Teuchos::parameterList(
"Solution History");
571 "Storage Type", getStorageTypeString(),
572 "'Storage Type' sets the memory storage. "
573 "'Keep Newest' - will retain the single newest solution state. "
574 "'Undo' - will retain two solution states in order to do a single undo. "
575 "'Static' - will retain 'Storage Limit' number of solution states. "
576 "'Unlimited' - will not remove any solution states!");
579 "Storage Limit", getStorageLimit(),
580 "Limit on the number of SolutionStates that SolutionHistory can have.");
582 pl->
set(
"Interpolator", *interpolator_->getNonconstParameterList());
587 template <
class Scalar>
594 template <
class Scalar>
598 if (interpolator == Teuchos::null) {
602 interpolator_ = interpolator;
604 isInitialized_ =
false;
607 template <
class Scalar>
611 return interpolator_;
614 template <
class Scalar>
618 return interpolator_;
621 template <
class Scalar>
625 interpolator_ = lagrangeInterpolator<Scalar>();
626 return old_interpolator;
629 template <
class Scalar>
634 *out << name_ <<
" (size=" << history_->size() <<
")"
635 <<
" (w - working; c - current; i - interpolated)" << std::endl;
636 for (
int i = 0; i < (int)history_->size(); ++i) {
637 auto state = (*history_)[i];
639 if (state == getWorkingState())
641 else if (state == getCurrentState())
643 else if (state->getIsInterpolated() ==
true)
647 *out <<
"[" << i <<
"] = " << state << std::endl;
648 if (verb ==
"medium" || verb ==
"high") {
649 if (state != Teuchos::null) {
650 auto x = state->getX();
651 *out <<
" x = " << x << std::endl
652 <<
" norm(x) = " << Thyra::norm(*x) << std::endl;
655 if (verb ==
"high") {
656 (*history_)[i]->describe(*out, this->getVerbLevel());
661 template <
class Scalar>
665 getNumStates() <= 0, std::logic_error,
666 "Error - SolutionHistory::initialize() Invalid history size!\n");
669 interpolator_ == Teuchos::null, std::logic_error,
670 "Error - SolutionHistory::initialize() Interpolator is not set!\n");
673 "Error - SolutionHistory::initialize() Storage "
674 "Limit needs to a positive integer!\n"
675 <<
" Storage Limit = " << storageLimit_
680 "Error - SolutionHistory::initialize() Storage Type is invalid!\n");
685 "Error - SolutionHistory::initialize() \n"
686 <<
" For Storage Type = '" << getStorageTypeString()
687 <<
"', Storage Limit needs to be one.\n"
688 <<
" Storage Limit = " << storageLimit_ <<
"\n");
693 "Error - SolutionHistory::initialize() \n"
694 <<
" For Storage Type = '" << getStorageTypeString()
695 <<
"', Storage Limit needs to be two.\n"
696 <<
" Storage Limit = " << storageLimit_ <<
"\n");
698 isInitialized_ =
true;
704 template <
class Scalar>
708 sh->setName(
"From createSolutionHistory");
713 template <
class Scalar>
718 sh->setName(
"From createSolutionHistoryPL");
720 if (pl == Teuchos::null || pl->
numParams() == 0)
return sh;
724 sh->setName(pl->
name());
725 sh->setStorageTypeString(pl->
get(
"Storage Type",
"Undo"));
726 sh->setStorageLimit(pl->
get(
"Storage Limit", 2));
729 Teuchos::sublist(pl,
"Interpolator")));
734 template <
class Scalar>
739 sh->setName(
"From createSolutionHistoryState");
744 template <
class Scalar>
752 state->setTimeStep(0.0);
757 sh->setName(
"From createSolutionHistoryME");
764 #endif // Tempus_SolutionHistory_impl_hpp
Teuchos::RCP< Interpolator< Scalar > > unSetInterpolator()
Unset the interpolator for this history.
SolutionHistory()
Default Contructor.
const std::string & name() const
Keep the 2 newest states for undo.
Teuchos::RCP< Interpolator< Scalar > > interpolator_
void initWorkingState()
Initialize the working state.
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistory()
Nonmember constructor.
void printHistory(std::string verb="low") const
Print information on States in the SolutionHistory.
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)
Teuchos::RCP< SolutionState< Scalar > > interpolateState(const Scalar time) const
Generate and interpolate a new solution state at requested time.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Keep the single newest state.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Return a valid non-const ParameterList with current settings.
Ordinal numParams() const
Teuchos::RCP< Interpolator< Scalar > > getNonconstInterpolator()
void setStorageLimit(int storage_limit)
Set the maximum storage of this history.
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryPL(Teuchos::RCP< Teuchos::ParameterList > pList)
Nonmember constructor from a ParameterList.
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryME(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Nonmember contructor from a Thyra ModelEvaluator.
Teuchos::RCP< SolutionState< Scalar > > getStateTimeIndex(int index, bool warn=true) const
Get the state with timestep index equal to "index".
void initialize() const
Initialize SolutionHistory.
static Teuchos::RCP< Interpolator< Scalar > > createInterpolator(std::string interpolatorType="")
Create default interpolator from interpolator type (e.g., "Linear").
void promoteWorkingState()
Promote the working state to current state.
void removeState(const Teuchos::RCP< SolutionState< Scalar > > &state)
Remove solution state.
bool isInitialized_
Bool if SolutionHistory is initialized.
Teuchos::RCP< SolutionState< Scalar > > getStateTimeIndexNM1(bool warn=true) const
Get the state with timestep index equal to n-1.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Base strategy class for interpolation functionality.
virtual std::string description() const
Teuchos::RCP< SolutionState< Scalar > > getStateTimeIndexN(bool warn=true) const
Get the state with timestep index equal to n.
Teuchos::RCP< SolutionState< Scalar > > getStateTimeIndexNM2(bool warn=true) const
Get the state with timestep index equal to n-2.
void addState(const Teuchos::RCP< SolutionState< Scalar > > &state, bool doChecks=true)
Add solution state to history.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
std::string getStorageTypeString() const
Set the string storage type.
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateME(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, const Teuchos::RCP< StepperState< Scalar > > &stepperState=Teuchos::null, const Teuchos::RCP< PhysicsState< Scalar > > &physicsState=Teuchos::null)
Nonmember constructor from Thyra ModelEvaluator.
void setStorageType(StorageType st)
Set the storage type via enum.
void addWorkingState(const Teuchos::RCP< SolutionState< Scalar > > &state, const bool updateTime=true)
Add a working solution state to history.
Keep a fix number of states.
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryState(const Teuchos::RCP< SolutionState< Scalar > > &state)
Nonmember contructor from a SolutionState.
void setStorageTypeString(std::string st)
Set the storage type via string.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
bool approxEqualAbsTol(Scalar a, Scalar b, Scalar absTol)
Test if values are approximately equal within the absolute tolerance.
bool approxZero(Scalar value, Scalar tol=Teuchos::ScalarTraits< Scalar >::sfmin())
Test if value is approximately zero within tolerance.
Teuchos::RCP< const Interpolator< Scalar > > getInterpolator() const
void setInterpolator(const Teuchos::RCP< Interpolator< Scalar > > &interpolator)
Set the interpolator for this history.
ParameterList & setName(const std::string &name)
void copy(Teuchos::RCP< const SolutionHistory< Scalar > > sh)
Teuchos::RCP< SolutionState< Scalar > > findState(const Scalar time) const
Find solution state at requested time (no interpolation)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a valid ParameterList with current settings.
Grow the history as needed.
Solution state for integrators and steppers.
Teuchos::RCP< std::vector< Teuchos::RCP< SolutionState< Scalar > > > > history_