10 #ifndef Tempus_SolutionHistory_impl_hpp
11 #define Tempus_SolutionHistory_impl_hpp
13 #include "Teuchos_StandardParameterEntryValidators.hpp"
16 #include "Thyra_VectorStdOps.hpp"
23 template <
class Scalar>
34 template <
class Scalar>
43 setInterpolator(interpolator);
44 setStorageType(storageType);
45 setStorageLimit(storageLimit);
47 isInitialized_ =
false;
48 if (getNumStates() > 0) isInitialized_ =
true;
51 template <
class Scalar>
56 if (Teuchos::as<int>(history_->size() + 1) > storageLimit_) {
57 switch (storageType_) {
60 true, std::logic_error,
61 "Error - Storage type is STORAGE_TYPE_INVALID.\n");
67 if (state->getTime() >= history_->front()->getTime()) {
70 history_->erase(history_->begin());
76 *out <<
"Warning, state is older than oldest state in history. "
77 <<
"State not added!" << std::endl;
85 "Error - unknown storage type.\n");
90 if (history_->size() == 0) {
91 history_->push_back(state);
94 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
95 state_it = history_->begin();
97 const Scalar newTime = state->getTime();
98 for (; state_it < history_->end(); state_it++) {
99 const Scalar shTime = (*state_it)->getTime();
101 const Scalar denom = std::max(std::fabs(shTime), std::fabs(newTime));
102 if (std::fabs(newTime - shTime) < 1.0e-14 * denom) {
106 *out <<
"Warning, state to be added matches state in history. "
107 <<
"State not added!" << std::endl;
109 *out <<
"===============" << std::endl;
110 *out <<
"Added SolutionState -- ";
112 *out <<
"===============" << std::endl;
119 if (newTime < shTime)
break;
121 if (!equal) history_->insert(state_it, state);
125 getNumStates() <= 0, std::logic_error,
126 "Error - SolutionHistory::addState() Invalid history size!\n");
131 template <
class Scalar>
137 auto cs = getCurrentState();
139 state->setIndex(cs->getIndex() + 1);
141 state->setTime(cs->getTime() + cs->getTimeStep());
142 state->setTimeStep(cs->getTimeStep());
146 workingState_ = (*history_)[getNumStates() - 1];
149 template <
class Scalar>
153 if (history_->size() != 0) {
154 auto state_it = history_->rbegin();
155 for (; state_it < history_->rend(); state_it++) {
156 if (state->getTime() == (*state_it)->getTime())
break;
160 "Error - removeState() Could not remove state = "
161 << (*state_it)->description());
164 history_->erase(std::next(state_it).base());
169 template <
class Scalar>
173 removeState(tmpState);
176 template <
class Scalar>
178 const Scalar time)
const
182 if (history_->size() > 0)
183 scale = (*history_)[history_->size() - 1]->getTime();
186 const Scalar absTol = scale * numericalTol<Scalar>();
188 !(minTime() - absTol <= time && time <= maxTime() + absTol),
190 "Error - SolutionHistory::findState() Requested time out of range!\n"
192 << minTime() <<
", " << maxTime()
197 auto state_it = history_->begin();
198 for (; state_it < history_->end(); ++state_it) {
202 TEUCHOS_TEST_FOR_EXCEPTION(state_it == history_->end(), std::logic_error,
203 "Error - SolutionHistory::findState()!\n"
204 " Did not find a SolutionState with time = "
205 << time << std::endl);
210 template <
class Scalar>
212 const Scalar time)
const
215 interpolate<Scalar>(*interpolator_, history_, time, state_out.
get());
219 template <
class Scalar>
223 interpolate<Scalar>(*interpolator_, history_, time, state_out);
227 template <
class Scalar>
230 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::SolutionHistory::initWorkingState()");
233 getCurrentState() == Teuchos::null, std::logic_error,
234 "Error - SolutionHistory::initWorkingState()\n"
235 "Can not initialize working state without a current state!\n");
239 if (getWorkingState(
false) != Teuchos::null)
return;
242 if (getNumStates() < storageLimit_) {
244 newState = getCurrentState()->clone();
248 newState = (*history_)[0];
249 history_->erase(history_->begin());
250 if (getNumStates() > 0) newState->copy(getCurrentState());
255 addWorkingState(newState);
260 template <
class Scalar>
263 auto ws = getWorkingState();
266 ws->setNFailures(std::max(0, ws->getNFailures() - 1));
267 ws->setNConsecutiveFailures(0);
270 ws->setIsInterpolated(
false);
271 workingState_ = Teuchos::null;
275 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::promoteWorkingState()");
276 *out <<
"Warning - WorkingState is not passing, so not promoted!\n"
281 template <
class Scalar>
285 this->setName(sh->getName());
288 auto sh_history = sh->getHistory();
289 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
290 state_it = sh_history->begin();
291 for (; state_it < sh_history->end(); state_it++) this->addState(*state_it);
295 this->setInterpolator(interpolator);
297 this->setStorageType(sh->getStorageType());
298 this->setStorageLimit(sh->getStorageLimit());
301 template <
class Scalar>
304 storageLimit_ = std::max(1, storage_limit);
308 if (storage_limit != 1) {
311 *out <<
"Warning - 'Storage Limit' for 'Keep Newest' is 1.\n"
312 <<
" (Storage Limit = " << storage_limit <<
"). Resetting to 1."
318 if (storage_limit != 2) {
321 *out <<
"Warning - 'Storage Limit' for 'Undo' is 2.\n"
322 <<
" (Storage Limit = " << storage_limit <<
"). Resetting to 2."
328 storageLimit_ = storage_limit;
331 storageLimit_ = std::numeric_limits<int>::max();
335 (Teuchos::as<int>(history_->size()) > storageLimit_), std::logic_error,
336 "Error - requested storage limit = "
338 <<
" is smaller than the current number of states stored = "
339 << history_->size() <<
"!\n");
341 isInitialized_ =
false;
344 template <
class Scalar>
353 setStorageLimit(std::numeric_limits<int>::max());
354 isInitialized_ =
false;
357 template <
class Scalar>
360 if (s ==
"Keep Newest") {
364 else if (s ==
"Undo") {
368 else if (s ==
"Static") {
371 else if (s ==
"Unlimited") {
376 true, std::logic_error,
377 "Error - Unknown 'Storage Type' = '" << s <<
"'\n");
379 isInitialized_ =
false;
382 template <
class Scalar>
385 std::string s =
"Invalid";
397 template <
class Scalar>
402 const int m = history_->size();
406 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexN");
407 *out <<
"Warning - getStateTimeIndexN() No states in SolutionHistory!"
412 state = (*history_)[m - 1];
417 template <
class Scalar>
422 const int m = history_->size();
426 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexNM1");
427 *out <<
"Warning - getStateTimeIndexNM1() Not enough states in "
428 <<
"SolutionHistory! Size of history = " << getNumStates()
433 const int n = (*history_)[m - 1]->getIndex();
434 const int nm1 = (*history_)[m - 2]->getIndex();
441 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexNM1");
442 *out <<
"Warning - getStateTimeIndexNM1() Timestep index n-1 is not in "
443 <<
"SolutionHistory!\n"
444 <<
" (n)th index = " << n <<
"\n"
445 <<
" (n-1)th index = " << nm1 << std::endl;
449 state = (*history_)[m - 2];
456 template <
class Scalar>
461 const int m = history_->size();
465 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexNM2");
466 *out <<
"Warning - getStateTimeIndexNM2() Not enough states in "
467 <<
"SolutionHistory! Size of history = " << getNumStates()
472 const int n = (*history_)[m - 1]->getIndex();
473 const int nm2 = (*history_)[m - 3]->getIndex();
478 const int nm1 = (*history_)[m - 2]->getIndex();
480 state = (*history_)[m - 2];
484 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndexNM2");
485 *out <<
"Warning - getStateTimeIndexNM2() Timestep index n-2 is not in "
486 <<
"SolutionHistory!\n"
487 <<
" (n)th index = " << n <<
"\n"
488 <<
" (n-2)th index = " << nm2 << std::endl;
492 state = (*history_)[m - 3];
499 template <
class Scalar>
501 int index,
bool warn)
const
503 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
504 state_it = history_->begin();
505 for (; state_it < history_->end(); state_it++) {
506 if ((*state_it)->getIndex() == index)
break;
510 if (state_it == history_->end()) {
513 Teuchos::OSTab ostab(out, 1,
"SolutionHistory::getStateTimeIndex");
514 *out <<
"Warning - getStateTimeIndex() Timestep index is not in "
515 <<
"SolutionHistory!\n"
516 <<
" index = " << index << std::endl;
525 template <
class Scalar>
528 return (
"Tempus::SolutionHistory - '" + name_ +
"'");
531 template <
class Scalar>
535 auto l_out = Teuchos::fancyOStream(out.
getOStream());
537 l_out->setOutputToRootOnly(0);
539 *l_out <<
"\n--- " << this->description() <<
" ---" << std::endl;
541 if ((Teuchos::as<int>(verbLevel) ==
546 *l_out <<
" storageLimit = " << storageLimit_ << std::endl;
547 *l_out <<
" storageType = " << getStorageTypeString() << std::endl;
548 *l_out <<
" number of states = " << history_->size() << std::endl;
549 if (history_->size() > 0) {
550 *l_out <<
" time range = (" << history_->front()->getTime() <<
", "
551 << history_->back()->getTime() <<
")" << std::endl;
556 for (
int i = 0; i < (int)history_->size(); ++i)
557 (*history_)[i]->describe(*l_out, verbLevel);
559 *l_out << std::string(this->description().length() + 8,
'-') << std::endl;
562 template <
class Scalar>
567 Teuchos::parameterList(
"Solution History");
572 "Storage Type", getStorageTypeString(),
573 "'Storage Type' sets the memory storage. "
574 "'Keep Newest' - will retain the single newest solution state. "
575 "'Undo' - will retain two solution states in order to do a single undo. "
576 "'Static' - will retain 'Storage Limit' number of solution states. "
577 "'Unlimited' - will not remove any solution states!");
580 "Storage Limit", getStorageLimit(),
581 "Limit on the number of SolutionStates that SolutionHistory can have.");
583 pl->
set(
"Interpolator", *interpolator_->getNonconstParameterList());
588 template <
class Scalar>
595 template <
class Scalar>
599 if (interpolator == Teuchos::null) {
603 interpolator_ = interpolator;
605 isInitialized_ =
false;
608 template <
class Scalar>
612 return interpolator_;
615 template <
class Scalar>
619 return interpolator_;
622 template <
class Scalar>
626 interpolator_ = lagrangeInterpolator<Scalar>();
627 return old_interpolator;
630 template <
class Scalar>
635 *out << name_ <<
" (size=" << history_->size() <<
")"
636 <<
" (w - working; c - current; i - interpolated)" << std::endl;
637 for (
int i = 0; i < (int)history_->size(); ++i) {
638 auto state = (*history_)[i];
640 if (state == getWorkingState())
642 else if (state == getCurrentState())
644 else if (state->getIsInterpolated() ==
true)
648 *out <<
"[" << i <<
"] = " << state << std::endl;
649 if (verb ==
"medium" || verb ==
"high") {
650 if (state != Teuchos::null) {
651 auto x = state->getX();
652 *out <<
" x = " << x << std::endl
653 <<
" norm(x) = " << Thyra::norm(*x) << std::endl;
656 if (verb ==
"high") {
657 (*history_)[i]->describe(*out, this->getVerbLevel());
662 template <
class Scalar>
666 getNumStates() <= 0, std::logic_error,
667 "Error - SolutionHistory::initialize() Invalid history size!\n");
670 interpolator_ == Teuchos::null, std::logic_error,
671 "Error - SolutionHistory::initialize() Interpolator is not set!\n");
674 "Error - SolutionHistory::initialize() Storage "
675 "Limit needs to a positive integer!\n"
676 <<
" Storage Limit = " << storageLimit_
681 "Error - SolutionHistory::initialize() Storage Type is invalid!\n");
686 "Error - SolutionHistory::initialize() \n"
687 <<
" For Storage Type = '" << getStorageTypeString()
688 <<
"', Storage Limit needs to be one.\n"
689 <<
" Storage Limit = " << storageLimit_ <<
"\n");
694 "Error - SolutionHistory::initialize() \n"
695 <<
" For Storage Type = '" << getStorageTypeString()
696 <<
"', Storage Limit needs to be two.\n"
697 <<
" Storage Limit = " << storageLimit_ <<
"\n");
699 isInitialized_ =
true;
705 template <
class Scalar>
709 sh->setName(
"From createSolutionHistory");
714 template <
class Scalar>
719 sh->setName(
"From createSolutionHistoryPL");
721 if (pl == Teuchos::null || pl->
numParams() == 0)
return sh;
725 sh->setName(pl->
name());
726 sh->setStorageTypeString(pl->
get(
"Storage Type",
"Undo"));
727 sh->setStorageLimit(pl->
get(
"Storage Limit", 2));
730 Teuchos::sublist(pl,
"Interpolator")));
735 template <
class Scalar>
740 sh->setName(
"From createSolutionHistoryState");
745 template <
class Scalar>
753 state->setTimeStep(0.0);
758 sh->setName(
"From createSolutionHistoryME");
765 #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_