Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_SolutionHistory_impl.hpp
Go to the documentation of this file.
1 //@HEADER
2 // *****************************************************************************
3 // Tempus: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
10 #ifndef Tempus_SolutionHistory_impl_hpp
11 #define Tempus_SolutionHistory_impl_hpp
12 
13 #include "Teuchos_StandardParameterEntryValidators.hpp"
14 #include "Teuchos_TimeMonitor.hpp"
15 
16 #include "Thyra_VectorStdOps.hpp"
17 
20 
21 namespace Tempus {
22 
23 template <class Scalar>
25  : name_("Solution History"), storageType_(STORAGE_TYPE_UNDO), storageLimit_(2)
26 {
27  using Teuchos::RCP;
28  // Create history, a vector of solution states.
29  history_ = rcp(new std::vector<RCP<SolutionState<Scalar> > >);
31  isInitialized_ = false;
32 }
33 
34 template <class Scalar>
36  std::string name,
37  Teuchos::RCP<std::vector<Teuchos::RCP<SolutionState<Scalar> > > > history,
38  Teuchos::RCP<Interpolator<Scalar> > interpolator, StorageType storageType,
39  int storageLimit)
40 {
41  setName(name);
42  setHistory(history);
43  setInterpolator(interpolator);
44  setStorageType(storageType);
45  setStorageLimit(storageLimit);
46 
47  isInitialized_ = false;
48  if (getNumStates() > 0) isInitialized_ = true;
49 }
50 
51 template <class Scalar>
53  const Teuchos::RCP<SolutionState<Scalar> >& state, bool doChecks)
54 {
55  // Check that we're not going to exceed our storage limit:
56  if (Teuchos::as<int>(history_->size() + 1) > storageLimit_) {
57  switch (storageType_) {
58  case STORAGE_TYPE_INVALID: {
60  true, std::logic_error,
61  "Error - Storage type is STORAGE_TYPE_INVALID.\n");
62  break;
63  }
66  case STORAGE_TYPE_UNDO: {
67  if (state->getTime() >= history_->front()->getTime()) {
68  // Case: State is younger than the oldest state in history.
69  // Remove state from the beginning of history, then add new state.
70  history_->erase(history_->begin());
71  }
72  else {
73  // Case: State is older than the oldest state in history.
74  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
75  Teuchos::OSTab ostab(out, 1, "SolutionHistory::addState");
76  *out << "Warning, state is older than oldest state in history. "
77  << "State not added!" << std::endl;
78  return;
79  }
80  break;
81  }
82  case STORAGE_TYPE_UNLIMITED: break;
83  default:
84  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
85  "Error - unknown storage type.\n");
86  }
87  }
88 
89  // Add new state in chronological order.
90  if (history_->size() == 0) {
91  history_->push_back(state);
92  }
93  else {
94  typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
95  state_it = history_->begin();
96  bool equal = false;
97  const Scalar newTime = state->getTime();
98  for (; state_it < history_->end(); state_it++) {
99  const Scalar shTime = (*state_it)->getTime();
100  if (doChecks) {
101  const Scalar denom = std::max(std::fabs(shTime), std::fabs(newTime));
102  if (std::fabs(newTime - shTime) < 1.0e-14 * denom) {
103  equal = true;
104  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
105  Teuchos::OSTab ostab(out, 1, "SolutionHistory::addState");
106  *out << "Warning, state to be added matches state in history. "
107  << "State not added!" << std::endl;
108 
109  *out << "===============" << std::endl;
110  *out << "Added SolutionState -- ";
111  (*state_it)->describe(*out, Teuchos::VERB_MEDIUM);
112  *out << "===============" << std::endl;
113  this->describe(*out, Teuchos::VERB_MEDIUM);
114 
115  std::exit(-1);
116  break;
117  }
118  }
119  if (newTime < shTime) break;
120  }
121  if (!equal) history_->insert(state_it, state);
122  }
123 
125  getNumStates() <= 0, std::logic_error,
126  "Error - SolutionHistory::addState() Invalid history size!\n");
127 
128  return;
129 }
130 
131 template <class Scalar>
133  const Teuchos::RCP<SolutionState<Scalar> >& state, const bool updateTime)
134 {
135  using Teuchos::RCP;
136 
137  auto cs = getCurrentState();
138  state->setSolutionStatus(Status::WORKING);
139  state->setIndex(cs->getIndex() + 1);
140  if (updateTime) {
141  state->setTime(cs->getTime() + cs->getTimeStep());
142  state->setTimeStep(cs->getTimeStep());
143  }
144 
145  addState(state);
146  workingState_ = (*history_)[getNumStates() - 1];
147 }
148 
149 template <class Scalar>
151  const Teuchos::RCP<SolutionState<Scalar> >& state)
152 {
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;
157  }
158 
159  TEUCHOS_TEST_FOR_EXCEPTION(state_it == history_->rend(), std::logic_error,
160  "Error - removeState() Could not remove state = "
161  << (*state_it)->description());
162 
163  // Need to be careful when erasing a reverse iterator.
164  history_->erase(std::next(state_it).base());
165  }
166  return;
167 }
168 
169 template <class Scalar>
171 {
172  Teuchos::RCP<SolutionState<Scalar> > tmpState = findState(time);
173  removeState(tmpState);
174 }
175 
176 template <class Scalar>
178  const Scalar time) const
179 {
180  // Use last step in solution history as the scale for comparing times
181  Scalar scale = 1.0;
182  if (history_->size() > 0)
183  scale = (*history_)[history_->size() - 1]->getTime();
184  if (approxZero(scale)) scale = Scalar(1.0);
185 
186  const Scalar absTol = scale * numericalTol<Scalar>();
188  !(minTime() - absTol <= time && time <= maxTime() + absTol),
189  std::logic_error,
190  "Error - SolutionHistory::findState() Requested time out of range!\n"
191  << " [Min, Max] = ["
192  << minTime() << ", " << maxTime()
193  << "]\n time = "
194  << time << "\n");
195 
196  // Linear search
197  auto state_it = history_->begin();
198  for (; state_it < history_->end(); ++state_it) {
199  if (approxEqualAbsTol(time, (*state_it)->getTime(), absTol)) break;
200  }
201 
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);
206 
207  return *state_it;
208 }
209 
210 template <class Scalar>
212  const Scalar time) const
213 {
214  Teuchos::RCP<SolutionState<Scalar> > state_out = getCurrentState()->clone();
215  interpolate<Scalar>(*interpolator_, history_, time, state_out.get());
216  return state_out;
217 }
218 
219 template <class Scalar>
221  const Scalar time, SolutionState<Scalar>* state_out) const
222 {
223  interpolate<Scalar>(*interpolator_, history_, time, state_out);
224 }
225 
227 template <class Scalar>
229 {
230  TEMPUS_FUNC_TIME_MONITOR("Tempus::SolutionHistory::initWorkingState()");
231  {
233  getCurrentState() == Teuchos::null, std::logic_error,
234  "Error - SolutionHistory::initWorkingState()\n"
235  "Can not initialize working state without a current state!\n");
236 
237  // If workingState_ has a valid pointer, we are still working on it,
238  // i.e., step failed and trying again, so do not initialize it.
239  if (getWorkingState(false) != Teuchos::null) return;
240 
242  if (getNumStates() < storageLimit_) {
243  // Create newState which is duplicate of currentState
244  newState = getCurrentState()->clone();
245  }
246  else {
247  // Recycle old state and copy currentState
248  newState = (*history_)[0];
249  history_->erase(history_->begin());
250  if (getNumStates() > 0) newState->copy(getCurrentState());
251  // When using the Griewank algorithm, we will want to select which
252  // older state to recycle.
253  }
254 
255  addWorkingState(newState);
256  }
257  return;
258 }
259 
260 template <class Scalar>
262 {
263  auto ws = getWorkingState();
264 
265  if (ws->getSolutionStatus() == Status::PASSED) {
266  ws->setNFailures(std::max(0, ws->getNFailures() - 1));
267  ws->setNConsecutiveFailures(0);
268  ws->setSolutionStatus(Status::PASSED);
269  // ws->setIsSynced(true);
270  ws->setIsInterpolated(false);
271  workingState_ = Teuchos::null;
272  }
273  else {
274  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
275  Teuchos::OSTab ostab(out, 1, "SolutionHistory::promoteWorkingState()");
276  *out << "Warning - WorkingState is not passing, so not promoted!\n"
277  << std::endl;
278  }
279 }
280 
281 template <class Scalar>
284 {
285  this->setName(sh->getName());
286 
287  this->clear();
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);
292 
293  auto interpolator =
294  Teuchos::rcp_const_cast<Interpolator<Scalar> >(sh->getInterpolator());
295  this->setInterpolator(interpolator);
296 
297  this->setStorageType(sh->getStorageType());
298  this->setStorageLimit(sh->getStorageLimit());
299 }
300 
301 template <class Scalar>
303 {
304  storageLimit_ = std::max(1, storage_limit);
305 
306  if (storageType_ == STORAGE_TYPE_INVALID ||
307  storageType_ == STORAGE_TYPE_KEEP_NEWEST) {
308  if (storage_limit != 1) {
309  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
310  Teuchos::OSTab ostab(out, 1, "SolutionHistory::setStorageLimit");
311  *out << "Warning - 'Storage Limit' for 'Keep Newest' is 1.\n"
312  << " (Storage Limit = " << storage_limit << "). Resetting to 1."
313  << std::endl;
314  storageLimit_ = 1;
315  }
316  }
317  else if (storageType_ == STORAGE_TYPE_UNDO) {
318  if (storage_limit != 2) {
319  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
320  Teuchos::OSTab ostab(out, 1, "SolutionHistory::setStorageLimit");
321  *out << "Warning - 'Storage Limit' for 'Undo' is 2.\n"
322  << " (Storage Limit = " << storage_limit << "). Resetting to 2."
323  << std::endl;
324  storageLimit_ = 2;
325  }
326  }
327  else if (storageType_ == STORAGE_TYPE_STATIC) {
328  storageLimit_ = storage_limit;
329  }
330  else if (storageType_ == STORAGE_TYPE_UNLIMITED) {
331  storageLimit_ = std::numeric_limits<int>::max();
332  }
333 
335  (Teuchos::as<int>(history_->size()) > storageLimit_), std::logic_error,
336  "Error - requested storage limit = "
337  << storageLimit_
338  << " is smaller than the current number of states stored = "
339  << history_->size() << "!\n");
340 
341  isInitialized_ = false;
342 }
343 
344 template <class Scalar>
346 {
347  storageType_ = st;
348  if (storageType_ == STORAGE_TYPE_KEEP_NEWEST)
349  setStorageLimit(1);
350  else if (storageType_ == STORAGE_TYPE_UNDO)
351  setStorageLimit(2);
352  else if (storageType_ == STORAGE_TYPE_UNLIMITED)
353  setStorageLimit(std::numeric_limits<int>::max());
354  isInitialized_ = false;
355 }
356 
357 template <class Scalar>
359 {
360  if (s == "Keep Newest") { // Keep the single newest state
361  storageType_ = STORAGE_TYPE_KEEP_NEWEST;
362  storageLimit_ = 1;
363  }
364  else if (s == "Undo") { // Keep the 2 newest states for undo
365  storageType_ = STORAGE_TYPE_UNDO;
366  storageLimit_ = 2;
367  }
368  else if (s == "Static") { // Keep a fix number of states
369  storageType_ = STORAGE_TYPE_STATIC;
370  }
371  else if (s == "Unlimited") { // Grow the history as needed
372  storageType_ = STORAGE_TYPE_UNLIMITED;
373  }
374  else {
376  true, std::logic_error,
377  "Error - Unknown 'Storage Type' = '" << s << "'\n");
378  }
379  isInitialized_ = false;
380 }
381 
382 template <class Scalar>
384 {
385  std::string s = "Invalid";
386  if (storageType_ == STORAGE_TYPE_KEEP_NEWEST)
387  s = "Keep Newest";
388  else if (storageType_ == STORAGE_TYPE_UNDO)
389  s = "Undo";
390  else if (storageType_ == STORAGE_TYPE_STATIC)
391  s = "Static";
392  else if (storageType_ == STORAGE_TYPE_UNLIMITED)
393  s = "Unlimited";
394  return s;
395 }
396 
397 template <class Scalar>
400 {
401  Teuchos::RCP<SolutionState<Scalar> > state = Teuchos::null;
402  const int m = history_->size();
403  if (m < 1) {
404  if (warn) {
405  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
406  Teuchos::OSTab ostab(out, 1, "SolutionHistory::getStateTimeIndexN");
407  *out << "Warning - getStateTimeIndexN() No states in SolutionHistory!"
408  << std::endl;
409  }
410  }
411  else {
412  state = (*history_)[m - 1];
413  }
414  return state;
415 }
416 
417 template <class Scalar>
420 {
421  Teuchos::RCP<SolutionState<Scalar> > state = Teuchos::null;
422  const int m = history_->size();
423  if (m < 2) {
424  if (warn) {
425  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
426  Teuchos::OSTab ostab(out, 1, "SolutionHistory::getStateTimeIndexNM1");
427  *out << "Warning - getStateTimeIndexNM1() Not enough states in "
428  << "SolutionHistory! Size of history = " << getNumStates()
429  << std::endl;
430  }
431  }
432  else {
433  const int n = (*history_)[m - 1]->getIndex();
434  const int nm1 = (*history_)[m - 2]->getIndex();
435 
436  // No need to search SolutionHistory as states n and nm1 should be
437  // next to each other.
438  if (nm1 != n - 1) {
439  if (warn) {
440  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
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;
446  }
447  }
448  else {
449  state = (*history_)[m - 2];
450  }
451  }
452 
453  return state;
454 }
455 
456 template <class Scalar>
459 {
460  Teuchos::RCP<SolutionState<Scalar> > state = Teuchos::null;
461  const int m = history_->size();
462  if (m < 3) {
463  if (warn) {
464  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
465  Teuchos::OSTab ostab(out, 1, "SolutionHistory::getStateTimeIndexNM2");
466  *out << "Warning - getStateTimeIndexNM2() Not enough states in "
467  << "SolutionHistory! Size of history = " << getNumStates()
468  << std::endl;
469  }
470  }
471  else {
472  const int n = (*history_)[m - 1]->getIndex();
473  const int nm2 = (*history_)[m - 3]->getIndex();
474 
475  // Assume states n and nm2 are one away from each other.
476  if (nm2 != n - 2) {
477  // Check if it is at nm1
478  const int nm1 = (*history_)[m - 2]->getIndex();
479  if (nm1 == n - 2) {
480  state = (*history_)[m - 2];
481  }
482  else if (warn) {
483  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
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;
489  }
490  }
491  else {
492  state = (*history_)[m - 3];
493  }
494  }
495 
496  return state;
497 }
498 
499 template <class Scalar>
501  int index, bool warn) const
502 {
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;
507  }
508 
509  Teuchos::RCP<SolutionState<Scalar> > state = Teuchos::null;
510  if (state_it == history_->end()) {
511  if (warn) {
512  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
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;
517  }
518  }
519  else {
520  state = *state_it;
521  }
522  return state;
523 }
524 
525 template <class Scalar>
527 {
528  return ("Tempus::SolutionHistory - '" + name_ + "'");
529 }
530 
531 template <class Scalar>
533  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
534 {
535  auto l_out = Teuchos::fancyOStream(out.getOStream());
536  Teuchos::OSTab ostab(*l_out, 2, this->description());
537  l_out->setOutputToRootOnly(0);
538 
539  *l_out << "\n--- " << this->description() << " ---" << std::endl;
540 
541  if ((Teuchos::as<int>(verbLevel) ==
542  Teuchos::as<int>(Teuchos::VERB_DEFAULT)) ||
543  (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_LOW))) {
544  //*l_out << " interpolator = " << interpolator->description() <<
545  // std::endl;
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;
552  }
553  }
554 
555  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
556  for (int i = 0; i < (int)history_->size(); ++i)
557  (*history_)[i]->describe(*l_out, verbLevel);
558  }
559  *l_out << std::string(this->description().length() + 8, '-') << std::endl;
560 }
561 
562 template <class Scalar>
565 {
567  Teuchos::parameterList("Solution History");
568 
569  pl->setName(getName());
570 
571  pl->set(
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!");
578 
579  pl->set(
580  "Storage Limit", getStorageLimit(),
581  "Limit on the number of SolutionStates that SolutionHistory can have.");
582 
583  pl->set("Interpolator", *interpolator_->getNonconstParameterList());
584 
585  return pl;
586 }
587 
588 template <class Scalar>
591 {
592  return Teuchos::rcp_const_cast<Teuchos::ParameterList>(getValidParameters());
593 }
594 
595 template <class Scalar>
597  const Teuchos::RCP<Interpolator<Scalar> >& interpolator)
598 {
599  if (interpolator == Teuchos::null) {
601  }
602  else {
603  interpolator_ = interpolator;
604  }
605  isInitialized_ = false;
606 }
607 
608 template <class Scalar>
611 {
612  return interpolator_;
613 }
614 
615 template <class Scalar>
618 {
619  return interpolator_;
620 }
621 
622 template <class Scalar>
624 {
625  Teuchos::RCP<Interpolator<Scalar> > old_interpolator = interpolator_;
626  interpolator_ = lagrangeInterpolator<Scalar>();
627  return old_interpolator;
628 }
629 
630 template <class Scalar>
631 void SolutionHistory<Scalar>::printHistory(std::string verb) const
632 {
633  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
634  Teuchos::OSTab ostab(out, 1, "SolutionHistory::printHistory");
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];
639  *out << " ";
640  if (state == getWorkingState())
641  *out << "w - ";
642  else if (state == getCurrentState())
643  *out << "c - ";
644  else if (state->getIsInterpolated() == true)
645  *out << "i - ";
646  else
647  *out << " ";
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;
654  }
655  }
656  if (verb == "high") {
657  (*history_)[i]->describe(*out, this->getVerbLevel());
658  }
659  }
660 }
661 
662 template <class Scalar>
664 {
666  getNumStates() <= 0, std::logic_error,
667  "Error - SolutionHistory::initialize() Invalid history size!\n");
668 
670  interpolator_ == Teuchos::null, std::logic_error,
671  "Error - SolutionHistory::initialize() Interpolator is not set!\n");
672 
673  TEUCHOS_TEST_FOR_EXCEPTION(storageLimit_ < 1, std::logic_error,
674  "Error - SolutionHistory::initialize() Storage "
675  "Limit needs to a positive integer!\n"
676  << " Storage Limit = " << storageLimit_
677  << "\n");
678 
680  (storageType_ == STORAGE_TYPE_INVALID), std::logic_error,
681  "Error - SolutionHistory::initialize() Storage Type is invalid!\n");
682 
684  (storageType_ == STORAGE_TYPE_KEEP_NEWEST && storageLimit_ != 1),
685  std::logic_error,
686  "Error - SolutionHistory::initialize() \n"
687  << " For Storage Type = '" << getStorageTypeString()
688  << "', Storage Limit needs to be one.\n"
689  << " Storage Limit = " << storageLimit_ << "\n");
690 
692  (storageType_ == STORAGE_TYPE_UNDO && storageLimit_ != 2),
693  std::logic_error,
694  "Error - SolutionHistory::initialize() \n"
695  << " For Storage Type = '" << getStorageTypeString()
696  << "', Storage Limit needs to be two.\n"
697  << " Storage Limit = " << storageLimit_ << "\n");
698 
699  isInitialized_ = true; // Only place where this is set to true!
700 }
701 
702 // Nonmember constructors.
703 // ------------------------------------------------------------------------
704 
705 template <class Scalar>
707 {
708  auto sh = rcp(new SolutionHistory<Scalar>());
709  sh->setName("From createSolutionHistory");
710 
711  return sh;
712 }
713 
714 template <class Scalar>
717 {
718  auto sh = rcp(new SolutionHistory<Scalar>());
719  sh->setName("From createSolutionHistoryPL");
720 
721  if (pl == Teuchos::null || pl->numParams() == 0) return sh;
722 
723  pl->validateParametersAndSetDefaults(*sh->getValidParameters());
724 
725  sh->setName(pl->name());
726  sh->setStorageTypeString(pl->get("Storage Type", "Undo"));
727  sh->setStorageLimit(pl->get("Storage Limit", 2));
728 
730  Teuchos::sublist(pl, "Interpolator")));
731 
732  return sh;
733 }
734 
735 template <class Scalar>
737  const Teuchos::RCP<SolutionState<Scalar> >& state)
738 {
739  auto sh = rcp(new SolutionHistory<Scalar>());
740  sh->setName("From createSolutionHistoryState");
741  sh->addState(state);
742  return sh;
743 }
744 
745 template <class Scalar>
747  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model)
748 {
749  // Setup initial condition SolutionState --------------------
750  auto state = createSolutionStateME(model);
751  state->setTime(0.0);
752  state->setIndex(0);
753  state->setTimeStep(0.0);
754  state->setOrder(1);
755 
756  // Setup SolutionHistory ------------------------------------
757  auto sh = rcp(new SolutionHistory<Scalar>());
758  sh->setName("From createSolutionHistoryME");
759  sh->addState(state);
760 
761  return sh;
762 }
763 
764 } // namespace Tempus
765 #endif // Tempus_SolutionHistory_impl_hpp
Teuchos::RCP< Interpolator< Scalar > > unSetInterpolator()
Unset the interpolator for this history.
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)
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Return a valid non-const ParameterList with current settings.
T * get() const
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 &quot;index&quot;.
void initialize() const
Initialize SolutionHistory.
static Teuchos::RCP< Interpolator< Scalar > > createInterpolator(std::string interpolatorType="")
Create default interpolator from interpolator type (e.g., &quot;Linear&quot;).
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.
Solution state for integrators and steppers.
Teuchos::RCP< std::vector< Teuchos::RCP< SolutionState< Scalar > > > > history_