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 
21 namespace Tempus {
22 
23 template<class Scalar>
25  : name_("Solution History"),
26  storageType_(STORAGE_TYPE_UNDO),
27  storageLimit_(2)
28 {
29  using Teuchos::RCP;
30  // Create history, a vector of solution states.
31  history_ = rcp(new std::vector<RCP<SolutionState<Scalar> > >);
33  isInitialized_ = false;
34 }
35 
36 
37 template<class Scalar>
39  std::string name,
40  Teuchos::RCP<std::vector<Teuchos::RCP<SolutionState<Scalar> > > > history,
41  Teuchos::RCP<Interpolator<Scalar> > interpolator,
42  StorageType storageType,
43  int storageLimit)
44 {
45  setName(name);
46  setHistory(history);
47  setInterpolator(interpolator);
48  setStorageType(storageType);
49  setStorageLimit(storageLimit);
50 
51  isInitialized_ = false;
52  if (getNumStates() > 0 ) isInitialized_ = true;
53 }
54 
55 
56 template<class Scalar>
58  const Teuchos::RCP<SolutionState<Scalar> >& state, bool doChecks)
59 {
60  // Check that we're not going to exceed our storage limit:
61  if (Teuchos::as<int>(history_->size()+1) > storageLimit_) {
62  switch (storageType_) {
63  case STORAGE_TYPE_INVALID: {
64  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
65  "Error - Storage type is STORAGE_TYPE_INVALID.\n");
66  break;
67  }
70  case STORAGE_TYPE_UNDO: {
71  if (state->getTime() >= history_->front()->getTime()) {
72  // Case: State is younger than the oldest state in history.
73  // Remove state from the beginning of history, then add new state.
74  history_->erase(history_->begin());
75  } else {
76  // Case: State is older than the oldest state in history.
77  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
78  Teuchos::OSTab ostab(out,1,"SolutionHistory::addState");
79  *out << "Warning, state is older than oldest state in history. "
80  << "State not added!" << std::endl;
81  return;
82  }
83  break;
84  }
86  break;
87  default:
88  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
89  "Error - unknown storage type.\n");
90  }
91  }
92 
93  // Add new state in chronological order.
94  if (history_->size() == 0) {
95  history_->push_back(state);
96  } else {
97  typename std::vector<Teuchos::RCP<SolutionState<Scalar> > >::iterator
98  state_it = history_->begin();
99  bool equal = false;
100  const Scalar newTime = state->getTime();
101  for (; state_it < history_->end(); state_it++) {
102  const Scalar shTime = (*state_it)->getTime();
103  if (doChecks) {
104  const Scalar denom = std::max(std::fabs(shTime), std::fabs(newTime));
105  if ( std::fabs(newTime - shTime) < 1.0e-14*denom ) {
106  equal = true;
107  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
108  Teuchos::OSTab ostab(out,1,"SolutionHistory::addState");
109  *out << "Warning, state to be added matches state in history. "
110  << "State not added!" << std::endl;
111 
112  *out << "===============" << std::endl;
113  *out << "Added SolutionState -- ";
114  (*state_it)->describe(*out, Teuchos::VERB_MEDIUM);
115  *out << "===============" << std::endl;
116  this->describe(*out, Teuchos::VERB_MEDIUM);
117 
118  std::exit(-1);
119  break;
120  }
121  }
122  if (newTime < shTime) break;
123  }
124  if (!equal) history_->insert(state_it, state);
125  }
126 
127  TEUCHOS_TEST_FOR_EXCEPTION(getNumStates() <= 0, std::logic_error,
128  "Error - SolutionHistory::addState() Invalid history size!\n");
129 
130  return;
131 }
132 
133 
134 template<class Scalar>
136  const Teuchos::RCP<SolutionState<Scalar> >& state, const bool updateTime)
137 {
138  using Teuchos::RCP;
139 
140  auto cs = getCurrentState();
141  state->setSolutionStatus(Status::WORKING);
142  state->setIndex(cs->getIndex()+1);
143  if (updateTime) {
144  state->setTime(cs->getTime() + cs->getTimeStep());
145  state->setTimeStep(cs->getTimeStep());
146  }
147 
148  addState(state);
149  workingState_ = (*history_)[getNumStates()-1];
150 }
151 
152 template<class Scalar>
154  const Teuchos::RCP<SolutionState<Scalar> >& state)
155 {
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;
160  }
161 
163  state_it == history_->rend(), std::logic_error,
164  "Error - removeState() Could not remove state = "
165  << (*state_it)->description());
166 
167  // Need to be careful when erasing a reverse iterator.
168  history_->erase(std::next(state_it).base());
169  }
170  return;
171 }
172 
173 
174 template<class Scalar>
176 {
177  Teuchos::RCP<SolutionState<Scalar> > tmpState = findState(time);
178  removeState(tmpState);
179 }
180 
181 
182 template<class Scalar>
184 SolutionHistory<Scalar>::findState(const Scalar time) const
185 {
186  // Use last step in solution history as the scale for comparing times
187  Scalar scale = 1.0;
188  if (history_->size() > 0) scale = (*history_)[history_->size()-1]->getTime();
189  if (approxZero(scale)) scale = Scalar(1.0);
190 
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");
197 
198  // Linear search
199  auto state_it = history_->begin();
200  for ( ; state_it < history_->end(); ++state_it) {
201  if ( approxEqualAbsTol(time, (*state_it)->getTime(), absTol))
202  break;
203  }
204 
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);
208 
209  return *state_it;
210 }
211 
212 
213 template<class Scalar>
216 {
217  Teuchos::RCP<SolutionState<Scalar> > state_out = getCurrentState()->clone();
218  interpolate<Scalar>(*interpolator_, history_, time, state_out.get());
219  return state_out;
220 }
221 
222 
223 template<class Scalar>
224 void
226  const Scalar time, SolutionState<Scalar>* state_out) const
227 {
228  interpolate<Scalar>(*interpolator_, history_, time, state_out);
229 }
230 
231 
233 template<class Scalar>
235 {
236  TEMPUS_FUNC_TIME_MONITOR("Tempus::SolutionHistory::initWorkingState()");
237  {
238  TEUCHOS_TEST_FOR_EXCEPTION(getCurrentState() == Teuchos::null,
239  std::logic_error,
240  "Error - SolutionHistory::initWorkingState()\n"
241  "Can not initialize working state without a current state!\n");
242 
243  // If workingState_ has a valid pointer, we are still working on it,
244  // i.e., step failed and trying again, so do not initialize it.
245  if (getWorkingState(false) != Teuchos::null) return;
246 
248  if (getNumStates() < storageLimit_) {
249  // Create newState which is duplicate of currentState
250  newState = getCurrentState()->clone();
251  } else {
252  // Recycle old state and copy currentState
253  newState = (*history_)[0];
254  history_->erase(history_->begin());
255  if (getNumStates() > 0) newState->copy(getCurrentState());
256  // When using the Griewank algorithm, we will want to select which
257  // older state to recycle.
258  }
259 
260  addWorkingState(newState);
261 
262  }
263  return;
264 }
265 
266 
267 template<class Scalar>
269 {
270  auto ws = getWorkingState();
271 
272  if ( ws->getSolutionStatus() == Status::PASSED ) {
273  ws->setNFailures(std::max(0,ws->getNFailures()-1));
274  ws->setNConsecutiveFailures(0);
275  ws->setSolutionStatus(Status::PASSED);
276  //ws->setIsSynced(true);
277  ws->setIsInterpolated(false);
278  workingState_ = Teuchos::null;
279  } else {
280  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
281  Teuchos::OSTab ostab(out,1,"SolutionHistory::promoteWorkingState()");
282  *out << "Warning - WorkingState is not passing, so not promoted!\n"
283  << std::endl;
284  }
285 }
286 
287 
288 template<class Scalar>
290 {
291  this->setName(sh->getName());
292 
293  this->clear();
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 );
299 
300  auto interpolator = Teuchos::rcp_const_cast<Interpolator<Scalar> >(sh->getInterpolator());
301  this->setInterpolator(interpolator);
302 
303  this->setStorageType(sh->getStorageType());
304  this->setStorageLimit(sh->getStorageLimit());
305 }
306 
307 
308 template<class Scalar>
310 {
311  storageLimit_ = std::max(1,storage_limit);
312 
313  if ( storageType_ == STORAGE_TYPE_INVALID ||
314  storageType_ == STORAGE_TYPE_KEEP_NEWEST ) {
315  if (storage_limit != 1) {
316  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
317  Teuchos::OSTab ostab(out,1,"SolutionHistory::setStorageLimit");
318  *out << "Warning - 'Storage Limit' for 'Keep Newest' is 1.\n"
319  << " (Storage Limit = "<<storage_limit<<"). Resetting to 1."
320  << std::endl;
321  storageLimit_ = 1;
322  }
323  } else if ( storageType_ == STORAGE_TYPE_UNDO ) {
324  if (storage_limit != 2) {
325  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
326  Teuchos::OSTab ostab(out,1,"SolutionHistory::setStorageLimit");
327  *out << "Warning - 'Storage Limit' for 'Undo' is 2.\n"
328  << " (Storage Limit = "<<storage_limit<<"). Resetting to 2."
329  << std::endl;
330  storageLimit_ = 2;
331  }
332  } else if ( storageType_ == STORAGE_TYPE_STATIC ) {
333  storageLimit_ = storage_limit;
334  } else if ( storageType_ == STORAGE_TYPE_UNLIMITED ) {
335  storageLimit_ = std::numeric_limits<int>::max();
336  }
337 
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");
343 
344  isInitialized_ = false;
345 }
346 
347 
348 template<class Scalar>
350 {
351  storageType_ = st;
352  if ( storageType_ == STORAGE_TYPE_KEEP_NEWEST ) setStorageLimit(1);
353  else if ( storageType_ == STORAGE_TYPE_UNDO ) setStorageLimit(2);
354  else if ( storageType_ == STORAGE_TYPE_UNLIMITED )
355  setStorageLimit(std::numeric_limits<int>::max());
356  isInitialized_ = false;
357 }
358 
359 
360 template<class Scalar>
362 {
363  if ( s == "Keep Newest" ) { // Keep the single newest state
364  storageType_ = STORAGE_TYPE_KEEP_NEWEST;
365  storageLimit_ = 1;
366  } else if ( s == "Undo" ) { // Keep the 2 newest states for undo
367  storageType_ = STORAGE_TYPE_UNDO;
368  storageLimit_ = 2;
369  } else if ( s == "Static" ) { // Keep a fix number of states
370  storageType_ = STORAGE_TYPE_STATIC;
371  } else if ( s == "Unlimited" ) { // Grow the history as needed
372  storageType_ = STORAGE_TYPE_UNLIMITED;
373  } else {
374  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
375  "Error - Unknown 'Storage Type' = '" << s << "'\n");
376  }
377  isInitialized_ = false;
378 }
379 
380 
381 template<class Scalar>
383 {
384  std::string s = "Invalid";
385  if ( storageType_ == STORAGE_TYPE_KEEP_NEWEST ) s = "Keep Newest";
386  else if ( storageType_ == STORAGE_TYPE_UNDO ) s = "Undo";
387  else if ( storageType_ == STORAGE_TYPE_STATIC ) s = "Static";
388  else if ( storageType_ == STORAGE_TYPE_UNLIMITED ) s = "Unlimited";
389  return s;
390 }
391 
392 
393 template<class Scalar>
396 {
397  Teuchos::RCP<SolutionState<Scalar> > state = Teuchos::null;
398  const int m = history_->size();
399  if ( m < 1 ) {
400  if ( warn ) {
401  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
402  Teuchos::OSTab ostab(out,1,"SolutionHistory::getStateTimeIndexN");
403  *out << "Warning - getStateTimeIndexN() No states in SolutionHistory!"
404  << std::endl;
405  }
406  } else {
407  state = (*history_)[m-1];
408  }
409  return state;
410 }
411 
412 
413 template<class Scalar>
416 {
417  Teuchos::RCP<SolutionState<Scalar> > state = Teuchos::null;
418  const int m = history_->size();
419  if ( m < 2 ) {
420  if ( warn ) {
421  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
422  Teuchos::OSTab ostab(out,1,"SolutionHistory::getStateTimeIndexNM1");
423  *out << "Warning - getStateTimeIndexNM1() Not enough states in "
424  << "SolutionHistory! Size of history = " << getNumStates()
425  << std::endl;
426  }
427  } else {
428  const int n = (*history_)[m-1]->getIndex();
429  const int nm1 = (*history_)[m-2]->getIndex();
430 
431  // No need to search SolutionHistory as states n and nm1 should be
432  // next to each other.
433  if ( nm1 != n-1 ) {
434  if ( warn ) {
435  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
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;
441  }
442  } else {
443  state = (*history_)[m-2];
444  }
445  }
446 
447  return state;
448 }
449 
450 
451 template<class Scalar>
454 {
455  Teuchos::RCP<SolutionState<Scalar> > state = Teuchos::null;
456  const int m = history_->size();
457  if ( m < 3 ) {
458  if ( warn ) {
459  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
460  Teuchos::OSTab ostab(out,1,"SolutionHistory::getStateTimeIndexNM2");
461  *out << "Warning - getStateTimeIndexNM2() Not enough states in "
462  << "SolutionHistory! Size of history = " << getNumStates()
463  << std::endl;
464  }
465  } else {
466  const int n = (*history_)[m-1]->getIndex();
467  const int nm2 = (*history_)[m-3]->getIndex();
468 
469  // Assume states n and nm2 are one away from each other.
470  if ( nm2 != n-2 ) {
471  // Check if it is at nm1
472  const int nm1 = (*history_)[m-2]->getIndex();
473  if ( nm1 == n-2 ) {
474  state = (*history_)[m-2];
475  } else if ( warn ) {
476  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
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;
482  }
483  } else {
484  state = (*history_)[m-3];
485  }
486  }
487 
488  return state;
489 }
490 
491 
492 template<class Scalar>
495 {
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;
500  }
501 
502  Teuchos::RCP<SolutionState<Scalar> > state = Teuchos::null;
503  if ( state_it == history_->end() ) {
504  if ( warn ) {
505  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
506  Teuchos::OSTab ostab(out,1,"SolutionHistory::getStateTimeIndex");
507  *out << "Warning - getStateTimeIndex() Timestep index is not in "
508  << "SolutionHistory!\n"
509  << " index = " << index << std::endl;
510  }
511  } else {
512  state = *state_it;
513  }
514  return state;
515 }
516 
517 
518 template<class Scalar>
520 {
521  return ("Tempus::SolutionHistory - '" + name_ + "'");
522 }
523 
524 
525 template<class Scalar>
528  const Teuchos::EVerbosityLevel verbLevel) const
529 {
530  auto l_out = Teuchos::fancyOStream( out.getOStream() );
531  Teuchos::OSTab ostab(*l_out, 2, this->description());
532  l_out->setOutputToRootOnly(0);
533 
534  *l_out << "\n--- " << this->description() << " ---" << std::endl;
535 
536  if ((Teuchos::as<int>(verbLevel)==Teuchos::as<int>(Teuchos::VERB_DEFAULT)) ||
537  (Teuchos::as<int>(verbLevel)>=Teuchos::as<int>(Teuchos::VERB_LOW) ) ){
538  //*l_out << " interpolator = " << interpolator->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() << ")"
545  << std::endl;
546  }
547  }
548 
549  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
550  for (int i=0; i<(int)history_->size() ; ++i)
551  (*history_)[i]->describe(*l_out, verbLevel);
552  }
553  *l_out << std::string(this->description().length()+8, '-') << std::endl;
554 }
555 
556 
557 template<class Scalar>
560 {
562  Teuchos::parameterList("Solution History");
563 
564  pl->setName(getName());
565 
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!");
572 
573  pl->set("Storage Limit", getStorageLimit(),
574  "Limit on the number of SolutionStates that SolutionHistory can have.");
575 
576  pl->set("Interpolator", *interpolator_->getNonconstParameterList());
577 
578  return pl;
579 }
580 
581 
582 template <class Scalar>
585 {
586  return Teuchos::rcp_const_cast<Teuchos::ParameterList>(getValidParameters());
587 }
588 
589 
590 template<class Scalar>
592  const Teuchos::RCP<Interpolator<Scalar> >& interpolator)
593 {
594  if (interpolator == Teuchos::null) {
596  } else {
597  interpolator_ = interpolator;
598  }
599  isInitialized_ = false;
600 }
601 
602 template<class Scalar>
605 {
606  return interpolator_;
607 }
608 
609 template<class Scalar>
612 {
613  return interpolator_;
614 }
615 
616 template<class Scalar>
619 {
620  Teuchos::RCP<Interpolator<Scalar> > old_interpolator = interpolator_;
621  interpolator_ = lagrangeInterpolator<Scalar>();
622  return old_interpolator;
623 }
624 
625 
626 template<class Scalar>
627 void SolutionHistory<Scalar>::printHistory(std::string verb) const
628 {
629  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
630  Teuchos::OSTab ostab(out,1,"SolutionHistory::printHistory");
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];
635  *out << " ";
636  if (state == getWorkingState()) *out << "w - ";
637  else if (state == getCurrentState()) *out << "c - ";
638  else if (state->getIsInterpolated() == true) *out<<"i - ";
639  else *out << " ";
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;
646  }
647  }
648  if (verb == "high") {
649  (*history_)[i]->describe(*out,this->getVerbLevel());
650  }
651  }
652 }
653 
654 
655 template<class Scalar>
657 {
658  TEUCHOS_TEST_FOR_EXCEPTION(getNumStates() <= 0, std::logic_error,
659  "Error - SolutionHistory::initialize() Invalid history size!\n");
660 
661  TEUCHOS_TEST_FOR_EXCEPTION(interpolator_ == Teuchos::null, std::logic_error,
662  "Error - SolutionHistory::initialize() Interpolator is not set!\n");
663 
664  TEUCHOS_TEST_FOR_EXCEPTION(storageLimit_ < 1, std::logic_error,
665  "Error - SolutionHistory::initialize() Storage Limit needs to a positive integer!\n"
666  << " Storage Limit = " << storageLimit_ << "\n");
667 
669  ( storageType_ == STORAGE_TYPE_INVALID ), std::logic_error,
670  "Error - SolutionHistory::initialize() Storage Type is invalid!\n");
671 
673  ( storageType_ == STORAGE_TYPE_KEEP_NEWEST && storageLimit_ != 1 ),
674  std::logic_error,
675  "Error - SolutionHistory::initialize() \n"
676  << " For Storage Type = '" << getStorageTypeString()
677  << "', Storage Limit needs to be one.\n"
678  << " Storage Limit = " << storageLimit_ << "\n");
679 
681  ( storageType_ == STORAGE_TYPE_UNDO && storageLimit_ != 2 ),
682  std::logic_error,
683  "Error - SolutionHistory::initialize() \n"
684  << " For Storage Type = '" << getStorageTypeString()
685  << "', Storage Limit needs to be two.\n"
686  << " Storage Limit = " << storageLimit_ << "\n");
687 
688  isInitialized_ = true; // Only place where this is set to true!
689 }
690 
691 
692 // Nonmember constructors.
693 // ------------------------------------------------------------------------
694 
695 template<class Scalar>
697 {
698  auto sh = rcp(new SolutionHistory<Scalar>());
699  sh->setName("From createSolutionHistory");
700 
701  return sh;
702 }
703 
704 
705 template<class Scalar>
708 {
709  auto sh = rcp(new SolutionHistory<Scalar>());
710  sh->setName("From createSolutionHistoryPL");
711 
712  if (pl == Teuchos::null || pl->numParams() == 0) return sh;
713 
714  pl->validateParametersAndSetDefaults(*sh->getValidParameters());
715 
716  sh->setName(pl->name());
717  sh->setStorageTypeString(pl->get("Storage Type", "Undo"));
718  sh->setStorageLimit(pl->get("Storage Limit", 2));
719 
721  Teuchos::sublist(pl, "Interpolator")));
722 
723  return sh;
724 }
725 
726 
727 template<class Scalar>
730 {
731  auto sh = rcp(new SolutionHistory<Scalar>());
732  sh->setName("From createSolutionHistoryState");
733  sh->addState(state);
734  return sh;
735 }
736 
737 
738 template<class Scalar>
741  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model)
742 {
743  // Setup initial condition SolutionState --------------------
744  auto state = createSolutionStateME(model);
745  state->setTime (0.0);
746  state->setIndex (0);
747  state->setTimeStep(0.0);
748  state->setOrder (1);
749 
750  // Setup SolutionHistory ------------------------------------
751  auto sh = rcp(new SolutionHistory<Scalar>());
752  sh->setName("From createSolutionHistoryME");
753  sh->addState(state);
754 
755  return sh;
756 }
757 
758 
759 } // namespace Tempus
760 #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)
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.
Solution state for integrators and steppers.
Teuchos::RCP< std::vector< Teuchos::RCP< SolutionState< Scalar > > > > history_