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