9 #ifndef Tempus_SolutionHistory_impl_hpp
10 #define Tempus_SolutionHistory_impl_hpp
12 #include "Teuchos_StandardParameterEntryValidators.hpp"
15 #include "Thyra_VectorStdOps.hpp"
23 template<
class Scalar>
25 : name_(
"Solution History"),
37 template<
class Scalar>
47 setInterpolator(interpolator);
48 setStorageType(storageType);
49 setStorageLimit(storageLimit);
51 isInitialized_ =
false;
52 if (getNumStates() > 0 ) isInitialized_ =
true;
56 template<
class Scalar>
61 if (Teuchos::as<int>(history_->size()+1) > storageLimit_) {
62 switch (storageType_) {
65 "Error - Storage type is STORAGE_TYPE_INVALID.\n");
71 if (state->getTime() >= history_->front()->getTime()) {
74 history_->erase(history_->begin());
79 *out <<
"Warning, state is older than oldest state in history. "
80 <<
"State not added!" << std::endl;
89 "Error - unknown storage type.\n");
94 if (history_->size() == 0) {
95 history_->push_back(state);
97 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
98 state_it = history_->begin();
100 const Scalar newTime = state->getTime();
101 for (; state_it < history_->end(); state_it++) {
102 const Scalar shTime = (*state_it)->getTime();
104 const Scalar denom = std::max(std::fabs(shTime), std::fabs(newTime));
105 if ( std::fabs(newTime - shTime) < 1.0e-14*denom ) {
109 *out <<
"Warning, state to be added matches state in history. "
110 <<
"State not added!" << std::endl;
112 *out <<
"===============" << std::endl;
113 *out <<
"Added SolutionState -- ";
115 *out <<
"===============" << std::endl;
122 if (newTime < shTime)
break;
124 if (!equal) history_->insert(state_it, state);
128 "Error - SolutionHistory::addState() Invalid history size!\n");
134 template<
class Scalar>
140 auto cs = getCurrentState();
142 state->setIndex(cs->getIndex()+1);
144 state->setTime(cs->getTime() + cs->getTimeStep());
145 state->setTimeStep(cs->getTimeStep());
149 workingState_ = (*history_)[getNumStates()-1];
152 template<
class Scalar>
156 if (history_->size() != 0) {
157 auto state_it = history_->rbegin();
158 for ( ; state_it < history_->rend(); state_it++) {
159 if (state->getTime() == (*state_it)->getTime())
break;
163 state_it == history_->rend(), std::logic_error,
164 "Error - removeState() Could not remove state = "
165 << (*state_it)->description());
168 history_->erase(std::next(state_it).base());
174 template<
class Scalar>
178 removeState(tmpState);
182 template<
class Scalar>
188 if (history_->size() > 0) scale = (*history_)[history_->size()-1]->getTime();
191 const Scalar absTol = scale*numericalTol<Scalar>();
193 !(minTime()-absTol <= time && time <= maxTime()+absTol), std::logic_error,
194 "Error - SolutionHistory::findState() Requested time out of range!\n"
195 " [Min, Max] = [" << minTime() <<
", " << maxTime() <<
"]\n"
196 " time = "<< time <<
"\n");
199 auto state_it = history_->begin();
200 for ( ; state_it < history_->end(); ++state_it) {
205 TEUCHOS_TEST_FOR_EXCEPTION(state_it == history_->end(), std::logic_error,
206 "Error - SolutionHistory::findState()!\n"
207 " Did not find a SolutionState with time = " <<time<< std::endl);
213 template<
class Scalar>
218 interpolate<Scalar>(*interpolator_, history_, time, state_out.
get());
223 template<
class Scalar>
228 interpolate<Scalar>(*interpolator_, history_, time, state_out);
233 template<
class Scalar>
236 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::SolutionHistory::initWorkingState()");
240 "Error - SolutionHistory::initWorkingState()\n"
241 "Can not initialize working state without a current state!\n");
245 if (getWorkingState(
false) != Teuchos::null)
return;
248 if (getNumStates() < storageLimit_) {
250 newState = getCurrentState()->clone();
253 newState = (*history_)[0];
254 history_->erase(history_->begin());
255 if (getNumStates() > 0) newState->copy(getCurrentState());
260 addWorkingState(newState);
267 template<
class Scalar>
270 auto ws = getWorkingState();
273 ws->setNFailures(std::max(0,ws->getNFailures()-1));
274 ws->setNConsecutiveFailures(0);
277 ws->setIsInterpolated(
false);
278 workingState_ = Teuchos::null;
281 Teuchos::OSTab ostab(out,1,
"SolutionHistory::promoteWorkingState()");
282 *out <<
"Warning - WorkingState is not passing, so not promoted!\n"
288 template<
class Scalar>
291 this->setName(sh->getName());
294 auto sh_history = sh->getHistory();
295 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
296 state_it = sh_history->begin();
297 for (; state_it < sh_history->end(); state_it++)
298 this->addState( *state_it );
301 this->setInterpolator(interpolator);
303 this->setStorageType(sh->getStorageType());
304 this->setStorageLimit(sh->getStorageLimit());
308 template<
class Scalar>
311 storageLimit_ = std::max(1,storage_limit);
315 if (storage_limit != 1) {
318 *out <<
"Warning - 'Storage Limit' for 'Keep Newest' is 1.\n"
319 <<
" (Storage Limit = "<<storage_limit<<
"). Resetting to 1."
324 if (storage_limit != 2) {
327 *out <<
"Warning - 'Storage Limit' for 'Undo' is 2.\n"
328 <<
" (Storage Limit = "<<storage_limit<<
"). Resetting to 2."
333 storageLimit_ = storage_limit;
335 storageLimit_ = std::numeric_limits<int>::max();
339 (Teuchos::as<int>(history_->size()) > storageLimit_), std::logic_error,
340 "Error - requested storage limit = " << storageLimit_
341 <<
" is smaller than the current number of states stored = "
342 << history_->size() <<
"!\n");
344 isInitialized_ =
false;
348 template<
class Scalar>
355 setStorageLimit(std::numeric_limits<int>::max());
356 isInitialized_ =
false;
360 template<
class Scalar>
363 if ( s ==
"Keep Newest" ) {
366 }
else if ( s ==
"Undo" ) {
369 }
else if ( s ==
"Static" ) {
371 }
else if ( s ==
"Unlimited" ) {
375 "Error - Unknown 'Storage Type' = '" << s <<
"'\n");
377 isInitialized_ =
false;
381 template<
class Scalar>
384 std::string s =
"Invalid";
393 template<
class Scalar>
398 const int m = history_->size();
402 Teuchos::OSTab ostab(out,1,
"SolutionHistory::getStateTimeIndexN");
403 *out <<
"Warning - getStateTimeIndexN() No states in SolutionHistory!"
407 state = (*history_)[m-1];
413 template<
class Scalar>
418 const int m = history_->size();
422 Teuchos::OSTab ostab(out,1,
"SolutionHistory::getStateTimeIndexNM1");
423 *out <<
"Warning - getStateTimeIndexNM1() Not enough states in "
424 <<
"SolutionHistory! Size of history = " << getNumStates()
428 const int n = (*history_)[m-1]->getIndex();
429 const int nm1 = (*history_)[m-2]->getIndex();
436 Teuchos::OSTab ostab(out,1,
"SolutionHistory::getStateTimeIndexNM1");
437 *out <<
"Warning - getStateTimeIndexNM1() Timestep index n-1 is not in "
438 <<
"SolutionHistory!\n"
439 <<
" (n)th index = " << n <<
"\n"
440 <<
" (n-1)th index = " << nm1 << std::endl;
443 state = (*history_)[m-2];
451 template<
class Scalar>
456 const int m = history_->size();
460 Teuchos::OSTab ostab(out,1,
"SolutionHistory::getStateTimeIndexNM2");
461 *out <<
"Warning - getStateTimeIndexNM2() Not enough states in "
462 <<
"SolutionHistory! Size of history = " << getNumStates()
466 const int n = (*history_)[m-1]->getIndex();
467 const int nm2 = (*history_)[m-3]->getIndex();
472 const int nm1 = (*history_)[m-2]->getIndex();
474 state = (*history_)[m-2];
477 Teuchos::OSTab ostab(out,1,
"SolutionHistory::getStateTimeIndexNM2");
478 *out <<
"Warning - getStateTimeIndexNM2() Timestep index n-2 is not in "
479 <<
"SolutionHistory!\n"
480 <<
" (n)th index = " << n <<
"\n"
481 <<
" (n-2)th index = " << nm2 << std::endl;
484 state = (*history_)[m-3];
492 template<
class Scalar>
496 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
497 state_it = history_->begin();
498 for (; state_it < history_->end(); state_it++) {
499 if ((*state_it)->getIndex() == index)
break;
503 if ( state_it == history_->end() ) {
507 *out <<
"Warning - getStateTimeIndex() Timestep index is not in "
508 <<
"SolutionHistory!\n"
509 <<
" index = " << index << std::endl;
518 template<
class Scalar>
521 return (
"Tempus::SolutionHistory - '" + name_ +
"'");
525 template<
class Scalar>
530 auto l_out = Teuchos::fancyOStream( out.
getOStream() );
532 l_out->setOutputToRootOnly(0);
534 *l_out <<
"\n--- " << this->description() <<
" ---" << std::endl;
539 *l_out <<
" storageLimit = " << storageLimit_ << std::endl;
540 *l_out <<
" storageType = " << getStorageTypeString() << std::endl;
541 *l_out <<
" number of states = " << history_->size() << std::endl;
542 if ( history_->size() > 0 ) {
543 *l_out<<
" time range = (" << history_->front()->getTime() <<
", "
544 << history_->back()->getTime() <<
")"
550 for (
int i=0; i<(int)history_->size() ; ++i)
551 (*history_)[i]->describe(*l_out, verbLevel);
553 *l_out << std::string(this->description().length()+8,
'-') << std::endl;
557 template<
class Scalar>
562 Teuchos::parameterList(
"Solution History");
566 pl->
set(
"Storage Type", getStorageTypeString(),
567 "'Storage Type' sets the memory storage. "
568 "'Keep Newest' - will retain the single newest solution state. "
569 "'Undo' - will retain two solution states in order to do a single undo. "
570 "'Static' - will retain 'Storage Limit' number of solution states. "
571 "'Unlimited' - will not remove any solution states!");
573 pl->
set(
"Storage Limit", getStorageLimit(),
574 "Limit on the number of SolutionStates that SolutionHistory can have.");
576 pl->
set(
"Interpolator", *interpolator_->getNonconstParameterList());
582 template <
class Scalar>
590 template<
class Scalar>
594 if (interpolator == Teuchos::null) {
597 interpolator_ = interpolator;
599 isInitialized_ =
false;
602 template<
class Scalar>
606 return interpolator_;
609 template<
class Scalar>
613 return interpolator_;
616 template<
class Scalar>
621 interpolator_ = lagrangeInterpolator<Scalar>();
622 return old_interpolator;
626 template<
class Scalar>
631 *out << name_ <<
" (size=" << history_->size() <<
")"
632 <<
" (w - working; c - current; i - interpolated)" << std::endl;
633 for (
int i=0; i<(int)history_->size() ; ++i) {
634 auto state = (*history_)[i];
636 if (state == getWorkingState()) *out <<
"w - ";
637 else if (state == getCurrentState()) *out <<
"c - ";
638 else if (state->getIsInterpolated() ==
true) *out<<
"i - ";
640 *out <<
"[" << i <<
"] = " << state << std::endl;
641 if (verb ==
"medium" || verb ==
"high") {
642 if (state != Teuchos::null) {
643 auto x = state->getX();
644 *out <<
" x = " << x << std::endl
645 <<
" norm(x) = " << Thyra::norm(*x) << std::endl;
648 if (verb ==
"high") {
649 (*history_)[i]->describe(*out,this->getVerbLevel());
655 template<
class Scalar>
659 "Error - SolutionHistory::initialize() Invalid history size!\n");
662 "Error - SolutionHistory::initialize() Interpolator is not set!\n");
665 "Error - SolutionHistory::initialize() Storage Limit needs to a positive integer!\n"
666 <<
" Storage Limit = " << storageLimit_ <<
"\n");
670 "Error - SolutionHistory::initialize() Storage Type is invalid!\n");
675 "Error - SolutionHistory::initialize() \n"
676 <<
" For Storage Type = '" << getStorageTypeString()
677 <<
"', Storage Limit needs to be one.\n"
678 <<
" Storage Limit = " << storageLimit_ <<
"\n");
683 "Error - SolutionHistory::initialize() \n"
684 <<
" For Storage Type = '" << getStorageTypeString()
685 <<
"', Storage Limit needs to be two.\n"
686 <<
" Storage Limit = " << storageLimit_ <<
"\n");
688 isInitialized_ =
true;
695 template<
class Scalar>
699 sh->setName(
"From createSolutionHistory");
705 template<
class Scalar>
710 sh->setName(
"From createSolutionHistoryPL");
712 if (pl == Teuchos::null || pl->
numParams() == 0)
return sh;
716 sh->setName(pl->
name());
717 sh->setStorageTypeString(pl->
get(
"Storage Type",
"Undo"));
718 sh->setStorageLimit(pl->
get(
"Storage Limit", 2));
721 Teuchos::sublist(pl,
"Interpolator")));
727 template<
class Scalar>
732 sh->setName(
"From createSolutionHistoryState");
738 template<
class Scalar>
745 state->setTime (0.0);
747 state->setTimeStep(0.0);
752 sh->setName(
"From createSolutionHistoryME");
760 #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)
Make a shallow copy of SolutionHistory (i.e., only RCPs to states and interpolator).
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_