9 #ifndef Tempus_SolutionHistory_impl_hpp
10 #define Tempus_SolutionHistory_impl_hpp
13 #include "Teuchos_StandardParameterEntryValidators.hpp"
14 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
15 #include "Teuchos_TimeMonitor.hpp"
25 static std::string Invalid_name =
"Invalid";
26 static std::string KeepNewest_name =
"Keep Newest";
27 static std::string Undo_name =
"Undo";
28 static std::string Static_name =
"Static";
29 static std::string Unlimited_name =
"Unlimited";
30 static std::string Storage_name =
"Storage Type";
31 static std::string Storage_default = Undo_name;
33 static std::string StorageLimit_name =
"Storage Limit";
34 static int StorageLimit_default = 2;
36 std::vector<std::string> HistoryPolicies =
37 {Invalid_name, KeepNewest_name, Undo_name, Static_name, Unlimited_name};
39 const Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<Tempus::StorageType> >
40 StorageTypeValidator = Teuchos::rcp(
41 new Teuchos::StringToIntegralParameterEntryValidator<Tempus::StorageType>(
43 Teuchos::tuple<Tempus::StorageType>(
56 template<
class Scalar>
63 this->setParameterList(Teuchos::null);
65 if (Teuchos::as<int>(this->getVerbLevel()) >=
66 Teuchos::as<int>(Teuchos::VERB_HIGH)) {
67 RCP<Teuchos::FancyOStream> out = this->getOStream();
68 Teuchos::OSTab ostab(out,1,
"SolutionHistory::SolutionHistory");
69 *out << this->description() << std::endl;
74 template<
class Scalar>
82 this->setParameterList(Teuchos::null);
86 this->setStorageType(storageType);
87 this->setStorageLimit(storageLimit);
89 if (Teuchos::as<int>(this->getVerbLevel()) >=
90 Teuchos::as<int>(Teuchos::VERB_HIGH)) {
91 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
92 Teuchos::OSTab ostab(out,1,
"SolutionHistory::SolutionHistory");
93 *out << this->description() << std::endl;
98 template<
class Scalar>
100 Teuchos::RCP<Teuchos::ParameterList> pList)
106 this->setParameterList(pList);
108 if (Teuchos::as<int>(this->getVerbLevel()) >=
109 Teuchos::as<int>(Teuchos::VERB_HIGH)) {
110 RCP<Teuchos::FancyOStream> out = this->getOStream();
111 Teuchos::OSTab ostab(out,1,
"SolutionHistory::SolutionHistory");
112 *out << this->description() << std::endl;
117 template<
class Scalar>
122 if (Teuchos::as<int>(history_->size()+1) > storageLimit_) {
123 switch (storageType_) {
125 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
126 "Error - Storage type is STORAGE_TYPE_INVALID.\n");
132 if (state->getTime() >= history_->front()->getTime()) {
135 history_->erase(history_->begin());
138 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
139 Teuchos::OSTab ostab(out,1,
"SolutionHistory::addState");
140 *out <<
"Warning, state is younger than youngest state in history. "
141 <<
"State not added!" << std::endl;
149 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
150 "Error - unknown storage type.\n");
155 if (history_->size() == 0) {
156 history_->push_back(state);
158 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
159 state_it = history_->begin();
160 for (; state_it < history_->end(); state_it++) {
161 if (state->getTime() < (*state_it)->getTime())
break;
163 history_->insert(state_it, state);
166 TEUCHOS_TEST_FOR_EXCEPTION(getNumStates() <= 0, std::logic_error,
167 "Error - SolutionHistory::addState() Invalid history size!\n");
172 template<
class Scalar>
179 workingState_ = (*history_)[getNumStates()-1];
180 auto cs = getCurrentState();
181 auto ws = getWorkingState();
183 ws->setIndex(cs->getIndex()+1);
185 ws->setTime(cs->getTime() + cs->getTimeStep());
186 ws->setTimeStep(cs->getTimeStep());
190 template<
class Scalar>
194 if (history_->size() != 0) {
195 auto state_it = history_->rbegin();
196 for ( ; state_it < history_->rend(); state_it++) {
197 if (state->getTime() == (*state_it)->getTime())
break;
200 TEUCHOS_TEST_FOR_EXCEPTION(state_it == history_->rend(), std::logic_error,
201 "Error - removeState() Could not remove state = "
206 history_->erase(std::next(state_it).base());
212 template<
class Scalar>
215 Teuchos::RCP<SolutionState<Scalar> > tmpState = findState(time);
216 removeState(tmpState);
220 template<
class Scalar>
221 Teuchos::RCP<SolutionState<Scalar> >
224 TEUCHOS_TEST_FOR_EXCEPTION(
225 !(minTime() <= time and time <= maxTime()), std::logic_error,
226 "Error - SolutionHistory::findState() Requested time out of range!\n"
227 " [Min, Max] = [" << minTime() <<
", " << maxTime() <<
"]\n"
228 " time = "<< time <<
"\n");
232 history_->size() > 0 ? (*history_)[history_->size()-1]->getTime() : Scalar(1.0);
234 auto state_it = history_->begin();
235 for ( ; state_it < history_->end(); ++state_it) {
240 TEUCHOS_TEST_FOR_EXCEPTION(state_it == history_->end(), std::logic_error,
241 "Error - SolutionHistory::findState()!\n"
242 " Did not find a SolutionState with time = " <<time<< std::endl);
248 template<
class Scalar>
249 Teuchos::RCP<SolutionState<Scalar> >
252 Teuchos::RCP<SolutionState<Scalar> > state_out = getCurrentState()->clone();
253 interpolate<Scalar>(*interpolator_, history_, time, state_out.get());
258 template<
class Scalar>
263 interpolate<Scalar>(*interpolator_, history_, time, state_out);
268 template<
class Scalar>
271 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::SolutionHistory::initWorkingState()");
273 TEUCHOS_TEST_FOR_EXCEPTION(getCurrentState() == Teuchos::null,
275 "Error - SolutionHistory::initWorkingState()\n"
276 "Can not initialize working state without a current state!\n");
280 if (getWorkingState(
false) != Teuchos::null)
return;
282 Teuchos::RCP<SolutionState<Scalar> > newState;
283 if (getNumStates() < storageLimit_) {
285 newState = getCurrentState()->clone();
288 newState = (*history_)[0];
289 history_->erase(history_->begin());
290 if (getNumStates() > 0) newState->copy(getCurrentState());
295 addWorkingState(newState);
302 template<
class Scalar>
305 auto ws = getWorkingState();
308 ws->setNFailures(std::max(0,ws->getNFailures()-1));
309 ws->setNConsecutiveFailures(0);
312 ws->setIsInterpolated(
false);
313 workingState_ = Teuchos::null;
315 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
316 Teuchos::OSTab ostab(out,1,
"SolutionHistory::promoteWorkingState()");
317 *out <<
"Warning - WorkingState is not passing, so not promoted!\n"
323 template<
class Scalar>
326 storageLimit_ = std::max(1,storage_limit);
328 TEUCHOS_TEST_FOR_EXCEPTION(
329 (Teuchos::as<int>(history_->size()) > storageLimit_), std::logic_error,
330 "Error - requested storage limit = " << storageLimit_
331 <<
" is smaller than the current number of states stored = "
332 << history_->size() <<
"!\n");
336 template<
class Scalar>
337 Teuchos::RCP<SolutionState<Scalar> >
340 const int m = history_->size();
341 TEUCHOS_TEST_FOR_EXCEPTION( (m < 1), std::out_of_range,
342 "Error - getStateTimeIndexN() No states in SolutionHistory!\n");
343 return (*history_)[m-1];
347 template<
class Scalar>
348 Teuchos::RCP<SolutionState<Scalar> >
351 const int m = history_->size();
352 TEUCHOS_TEST_FOR_EXCEPTION( (m < 2), std::out_of_range,
353 "Error - getStateTimeIndexNM1() Not enough states in "
354 <<
"SolutionHistory!\n");
355 const int n = (*history_)[m-1]->getIndex();
356 const int nm1 = (*history_)[m-2]->getIndex();
360 TEUCHOS_TEST_FOR_EXCEPTION( (nm1 != n-1), std::out_of_range,
361 "Error - getStateTimeIndexNM1() Timestep index n-1 is not in "
362 <<
"SolutionHistory!\n"
363 <<
" (n)th index = " << n <<
"\n"
364 <<
" (n-1)th index = " << nm1 <<
"\n");
366 return (*history_)[m-2];
370 template<
class Scalar>
371 Teuchos::RCP<SolutionState<Scalar> >
374 const int m = history_->size();
375 TEUCHOS_TEST_FOR_EXCEPTION( (m < 3), std::out_of_range,
376 "Error - getStateTimeIndexNM2() Not enough states in "
377 <<
"SolutionHistory!\n");
378 const int n = (*history_)[m-1]->getIndex();
379 const int nm2 = (*history_)[m-3]->getIndex();
383 TEUCHOS_TEST_FOR_EXCEPTION( (nm2 != n-2), std::out_of_range,
384 "Error - getStateTimeIndexNM2() Timestep index n-2 is not in "
385 <<
"SolutionHistory!\n"
386 <<
" (n)th index = " << n <<
"\n"
387 <<
" (n-2)th index = " << nm2 <<
"\n");
389 return (*history_)[m-3];
393 template<
class Scalar>
394 Teuchos::RCP<SolutionState<Scalar> >
397 typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
398 state_it = history_->begin();
399 for (; state_it < history_->end(); state_it++) {
400 if ((*state_it)->getIndex() == index)
break;
402 TEUCHOS_TEST_FOR_EXCEPTION( state_it==history_->end(), std::out_of_range,
403 "Error - getStateTimeIndex() Timestep index is not in "
404 <<
"SolutionHistory!\n"
405 <<
" index = " << index <<
"\n");
410 template<
class Scalar>
413 return (
"Tempus::SolutionHistory - name = '" + name_ +
"'");
417 template<
class Scalar>
419 Teuchos::FancyOStream &out,
420 const Teuchos::EVerbosityLevel verbLevel)
const
422 if ((Teuchos::as<int>(verbLevel)==Teuchos::as<int>(Teuchos::VERB_DEFAULT)) ||
423 (Teuchos::as<int>(verbLevel)>=Teuchos::as<int>(Teuchos::VERB_LOW) ) ){
424 out << description() <<
"::describe" << std::endl;
426 out <<
"storageLimit = " << storageLimit_ << std::endl;
427 out <<
"storageType = " << storageType_ << std::endl;
428 out <<
"number of states = " << history_->size() << std::endl;
429 out <<
"time range = (" << history_->front()->getTime() <<
", "
430 << history_->back()->getTime() <<
")"
432 }
else if (Teuchos::as<int>(verbLevel) >=
433 Teuchos::as<int>(Teuchos::VERB_HIGH)) {
434 out <<
"SolutionStates: " << std::endl;
435 for (
int i=0; i<(int)history_->size() ; ++i) {
436 out <<
"SolutionState[" << i <<
"] = " << std::endl;
437 (*history_)[i]->describe(out,this->getVerbLevel());
443 template <
class Scalar>
445 Teuchos::RCP<Teuchos::ParameterList>
const& pList)
447 if (pList == Teuchos::null) {
449 if (shPL_ == Teuchos::null) {
450 shPL_ = Teuchos::parameterList(
"Solution History");
451 *shPL_ = *(this->getValidParameters());
456 shPL_->validateParametersAndSetDefaults(*this->getValidParameters());
458 name_ = shPL_->name();
460 storageType_ = StorageTypeValidator->getIntegralValue(
461 *shPL_, Storage_name, Storage_default);
463 int storage_limit = shPL_->get(StorageLimit_name, StorageLimit_default);
465 switch (storageType_) {
469 if (storage_limit != 1) {
470 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
471 Teuchos::OSTab ostab(out,1,
"SolutionHistory::setParameterList");
472 *out <<
"Warning - 'Storage Limit' for 'Keep Newest' is 1.\n"
473 <<
" (Storage Limit = "<<storage_limit<<
"). Resetting to 1."
477 setStorageLimit(storage_limit);
481 if (storage_limit != 2) {
482 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
483 Teuchos::OSTab ostab(out,1,
"SolutionHistory::setParameterList");
484 *out <<
"Warning - 'Storage Limit' for 'Undo' is 2.\n"
485 <<
" (Storage Limit = "<<storage_limit<<
"). Resetting to 2."
489 setStorageLimit(storage_limit);
496 storage_limit = 1000000000;
500 setStorageLimit(storage_limit);
503 Teuchos::sublist(shPL_,
"Interpolator"));
507 template<
class Scalar>
508 Teuchos::RCP<const Teuchos::ParameterList>
511 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
513 pl->setName(
"Valid ParameterList");
515 pl->set(Storage_name, Storage_default,
516 "'Storage Type' sets the memory storage. "
517 "'Keep Newest' - will retain the single newest solution state. "
518 "'Undo' - will retain two solution states in order to do a single undo. "
519 "'Static' - will retain 'Storage Limit' number of solution states. "
520 "'Unlimited' - will not remove any solution states!",
521 StorageTypeValidator);
523 pl->set(StorageLimit_name, StorageLimit_default,
524 "Storage limit for the solution history.");
527 pl->sublist(
"Interpolator",
false,
"").disableRecursiveValidation();
533 template <
class Scalar>
534 Teuchos::RCP<Teuchos::ParameterList>
541 template <
class Scalar>
542 Teuchos::RCP<Teuchos::ParameterList>
545 Teuchos::RCP<Teuchos::ParameterList> temp_plist = shPL_;
546 shPL_ = Teuchos::null;
550 template<
class Scalar>
554 if (interpolator == Teuchos::null) {
557 interpolator_ = interpolator;
559 if (Teuchos::as<int>(this->getVerbLevel()) >=
560 Teuchos::as<int>(Teuchos::VERB_HIGH)) {
561 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
562 Teuchos::OSTab ostab(out,1,
"SolutionHistory::setInterpolator");
563 *out <<
"interpolator = " << interpolator_->description() << std::endl;
567 template<
class Scalar>
568 Teuchos::RCP<Interpolator<Scalar> >
571 return interpolator_;
574 template<
class Scalar>
575 Teuchos::RCP<const Interpolator<Scalar> >
578 return interpolator_;
581 template<
class Scalar>
582 Teuchos::RCP<Interpolator<Scalar> >
585 Teuchos::RCP<Interpolator<Scalar> > old_interpolator = interpolator_;
586 interpolator_ = lagrangeInterpolator<Scalar>();
587 return old_interpolator;
593 template<
class Scalar>
595 Teuchos::RCP<Teuchos::ParameterList> pList)
602 template<
class Scalar>
603 Teuchos::RCP<SolutionHistory<Scalar> >
607 sh->setName(
"From createSolutionHistoryState");
613 template<
class Scalar>
614 Teuchos::RCP<SolutionHistory<Scalar> >
616 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& model)
620 state->setTime (0.0);
622 state->setTimeStep(0.0);
627 sh->setName(
"From createSolutionHistoryME");
635 #endif // Tempus_SolutionHistory_impl_hpp
Teuchos::RCP< SolutionState< Scalar > > getStateTimeIndexNM2() const
Get the state with timestep index equal to n-2.
Teuchos::RCP< Interpolator< Scalar > > unSetInterpolator()
Unset the interpolator for this history.
SolutionHistory()
Default Contructor.
Keep the 2 newest states for undo.
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
void initWorkingState()
Initialize the working state.
Teuchos::RCP< SolutionState< Scalar > > interpolateState(const Scalar time) const
Generate and interpolate a new solution state at requested time.
Keep the single newest state.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
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.
static Teuchos::RCP< Interpolator< Scalar > > createInterpolator(std::string interpolatorType="")
Create default interpolator from interpolator type (e.g., "Linear").
Teuchos::RCP< SolutionState< Scalar > > getStateTimeIndexNM1() const
Get the state with timestep index equal to n-1.
void promoteWorkingState()
Promote the working state to current state.
void removeState(const Teuchos::RCP< SolutionState< Scalar > > &state)
Remove solution state.
Base strategy class for interpolation functionality.
virtual std::string description() const
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
void addState(const Teuchos::RCP< SolutionState< Scalar > > &state)
Add solution state to history.
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 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.
Teuchos::RCP< SolutionState< Scalar > > getStateTimeIndex(int index) const
Get the state with timestep index equal to "index".
Teuchos::RCP< SolutionState< Scalar > > getStateTimeIndexN() const
Get the state with timestep index equal to n.
Teuchos::RCP< const Interpolator< Scalar > > getInterpolator() const
bool floating_compare_equals(const Scalar &a, const Scalar &b, const Scalar &scale)
Helper function for comparing times.
void setInterpolator(const Teuchos::RCP< Interpolator< Scalar > > &interpolator)
Set the interpolator for this history.
Teuchos::RCP< SolutionState< Scalar > > findState(const Scalar time) const
Find solution state at requested time (no interpolation)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Grow the history as needed.
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...