Tempus  Version of the Day
Time Integration
Tempus_IntegratorBasic_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_IntegratorBasic_impl_hpp
10 #define Tempus_IntegratorBasic_impl_hpp
11 
12 #include "Thyra_VectorStdOps.hpp"
13 
14 #include "Tempus_StepperFactory.hpp"
15 #include "Tempus_StepperForwardEuler.hpp"
16 
17 
18 namespace Tempus {
19 
20 template<class Scalar>
22  : outputScreenIndices_(std::vector<int>()),
23  outputScreenInterval_(1000000),
24  integratorStatus_(WORKING),
25  isInitialized_(false)
26 {
27  setIntegratorName("Integrator Basic");
28  setIntegratorType("Integrator Basic");
29  setStepper(Teuchos::null);
30  setSolutionHistory(Teuchos::null);
31  setTimeStepControl(Teuchos::null);
32  setObserver(Teuchos::null);
33 
34  integratorTimer_ = rcp(new Teuchos::Time("Integrator Timer"));
35  stepperTimer_ = rcp(new Teuchos::Time("Stepper Timer"));
36 
37 }
38 
39 
40 template<class Scalar>
42  Teuchos::RCP<Stepper<Scalar> > stepper,
43  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
44  Teuchos::RCP<TimeStepControl<Scalar> > timeStepControl,
45  Teuchos::RCP<IntegratorObserver<Scalar> > integratorObserver,
46  std::vector<int> outputScreenIndices,
47  int outputScreenInterval)
48  : outputScreenIndices_(outputScreenIndices),
49  outputScreenInterval_(outputScreenInterval),
50  integratorStatus_(WORKING),
51  isInitialized_(false)
52 {
53  setIntegratorName("Integrator Basic");
54  setIntegratorType("Integrator Basic");
55  setStepper(stepper);
56  setSolutionHistory(solutionHistory);
57  setTimeStepControl(timeStepControl);
58  setObserver(integratorObserver);
59 
60  integratorTimer_ = rcp(new Teuchos::Time("Integrator Timer"));
61  stepperTimer_ = rcp(new Teuchos::Time("Stepper Timer"));
62 
63 }
64 
65 
66 template<class Scalar>
68 {
69  TEUCHOS_TEST_FOR_EXCEPTION( i != "Integrator Basic", std::logic_error,
70  "Error - Integrator Type should be 'Integrator Basic'\n");
71 
72  this->integratorType_ = i;
73 }
74 
75 
76 template<class Scalar>
78  Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> > model)
79 {
80  TEUCHOS_TEST_FOR_EXCEPTION( stepper_ == Teuchos::null, std::logic_error,
81  "Error - setModel(), need to set stepper first!\n");
82 
83  stepper_->setModel(model);
84 }
85 
86 
87 template<class Scalar>
89  Teuchos::RCP<Stepper<Scalar> > stepper)
90 {
91  if (stepper == Teuchos::null)
92  stepper_ = Teuchos::rcp(new StepperForwardEuler<Scalar>());
93  else
94  stepper_ = stepper;
95 }
96 
98 template<class Scalar>
101 {
102  using Teuchos::RCP;
103 
104  if (solutionHistory_ == Teuchos::null) {
105  solutionHistory_ = rcp(new SolutionHistory<Scalar>());
106  } else {
107  solutionHistory_->clear();
108  }
109 
110  TEUCHOS_TEST_FOR_EXCEPTION( stepper_ == Teuchos::null, std::logic_error,
111  "Error - initializeSolutionHistory(), need to set stepper first!\n");
112 
113  if (state == Teuchos::null) {
114  TEUCHOS_TEST_FOR_EXCEPTION( stepper_->getModel() == Teuchos::null,
115  std::logic_error,
116  "Error - initializeSolutionHistory(), need to set stepper's model first!\n");
117  // Construct default IC from the application model
118  state = createSolutionStateME( stepper_->getModel(),
119  stepper_->getDefaultStepperState());
120 
121  if (timeStepControl_ != Teuchos::null) {
122  // Set SolutionState from TimeStepControl
123  state->setTime (timeStepControl_->getInitTime());
124  state->setIndex (timeStepControl_->getInitIndex());
125  state->setTimeStep(timeStepControl_->getInitTimeStep());
126  state->setTolRel (timeStepControl_->getMaxRelError());
127  state->setTolAbs (timeStepControl_->getMaxAbsError());
128  }
129  state->setOrder (stepper_->getOrder());
130  state->setSolutionStatus(Status::PASSED); // ICs are considered passing.
131  }
132 
133  solutionHistory_->addState(state);
134 
135  stepper_->setInitialConditions(solutionHistory_);
136 }
137 
138 
139 template<class Scalar>
142  Teuchos::RCP<const Thyra::VectorBase<Scalar> > x0,
143  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xdot0,
144  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xdotdot0)
145 {
146  using Teuchos::RCP;
147 
148  // Create and set xdot and xdotdot.
149  RCP<Thyra::VectorBase<Scalar> > xdot = x0->clone_v();
150  RCP<Thyra::VectorBase<Scalar> > xdotdot = x0->clone_v();
151  if (xdot0 == Teuchos::null)
152  Thyra::assign(xdot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
153  else
154  Thyra::assign(xdot.ptr(), *(xdot0));
155  if (xdotdot0 == Teuchos::null)
156  Thyra::assign(xdotdot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
157  else
158  Thyra::assign(xdotdot.ptr(), *(xdotdot0));
159 
160  TEUCHOS_TEST_FOR_EXCEPTION( stepper_ == Teuchos::null, std::logic_error,
161  "Error - initializeSolutionHistory(), need to set stepper first!\n");
162 
163  auto state = createSolutionStateX(x0->clone_v(), xdot, xdotdot);
164  state->setStepperState(stepper_->getDefaultStepperState());
165 
166  state->setTime (t0);
167  if (timeStepControl_ != Teuchos::null) {
168  // Set SolutionState from TimeStepControl
169  state->setIndex (timeStepControl_->getInitIndex());
170  state->setTimeStep(timeStepControl_->getInitTimeStep());
171  state->setTolRel (timeStepControl_->getMaxRelError());
172  state->setTolAbs (timeStepControl_->getMaxAbsError());
173  }
174  state->setOrder (stepper_->getOrder());
175  state->setSolutionStatus(Status::PASSED); // ICs are considered passing.
176 
177  initializeSolutionHistory(state);
178 }
179 
180 
181 template<class Scalar>
183  Teuchos::RCP<SolutionHistory<Scalar> > sh)
184 {
185  if (sh == Teuchos::null) {
186  // Create default SolutionHistory, otherwise keep current history.
187  if (solutionHistory_ == Teuchos::null)
188  solutionHistory_ = rcp(new SolutionHistory<Scalar>());
189  } else {
190  solutionHistory_ = sh;
191  }
192 }
193 
194 
195 template<class Scalar>
197  Teuchos::RCP<TimeStepControl<Scalar> > tsc)
198 {
199  if (tsc == Teuchos::null) {
200  // Create timeStepControl_ if null, otherwise keep current parameters.
201  if (timeStepControl_ == Teuchos::null) {
202  // Construct default TimeStepControl
203  timeStepControl_ = rcp(new TimeStepControl<Scalar>());
204  }
205  } else {
206  timeStepControl_ = tsc;
207  }
208 }
209 
210 
211 template<class Scalar>
213  Teuchos::RCP<IntegratorObserver<Scalar> > obs)
214 {
215  if (obs == Teuchos::null)
216  integratorObserver_ = Teuchos::rcp(new IntegratorObserverBasic<Scalar>());
217  else
218  integratorObserver_ = obs;
219 }
220 
221 
222 template<class Scalar>
224 {
225  TEUCHOS_TEST_FOR_EXCEPTION( stepper_ == Teuchos::null, std::logic_error,
226  "Error - Need to set the Stepper, setStepper(), before calling "
227  "IntegratorBasic::initialize()\n");
228 
229  TEUCHOS_TEST_FOR_EXCEPTION( solutionHistory_->getNumStates() < 1,
230  std::out_of_range,
231  "Error - SolutionHistory requires at least one SolutionState.\n"
232  << " Supplied SolutionHistory has only "
233  << solutionHistory_->getNumStates() << " SolutionStates.\n");
234 
235  stepper_->initialize();
236  solutionHistory_->initialize();
237  timeStepControl_->initialize();
238 
239  isInitialized_ = true;
240 }
241 
242 
243 template<class Scalar>
245 {
246  std::string name = "Tempus::IntegratorBasic";
247  return(name);
248 }
249 
250 
251 template<class Scalar>
253  Teuchos::FancyOStream &out,
254  const Teuchos::EVerbosityLevel verbLevel) const
255 {
256  auto l_out = Teuchos::fancyOStream( out.getOStream() );
257  Teuchos::OSTab ostab(*l_out, 2, this->description());
258  l_out->setOutputToRootOnly(0);
259 
260  *l_out << "\n--- " << this->description() << " ---" << std::endl;
261 
262  if ( solutionHistory_ != Teuchos::null ) {
263  solutionHistory_->describe(*l_out,verbLevel);
264  } else {
265  *l_out << "solutionHistory = " << solutionHistory_ << std::endl;
266  }
267 
268  if ( timeStepControl_ != Teuchos::null ) {
269  timeStepControl_->describe(out,verbLevel);
270  } else {
271  *l_out << "timeStepControl = " << timeStepControl_ << std::endl;
272  }
273 
274  if ( stepper_ != Teuchos::null ) {
275  stepper_->describe(out,verbLevel);
276  } else {
277  *l_out << "stepper = " << stepper_ << std::endl;
278  }
279  *l_out << std::string(this->description().length()+8, '-') <<std::endl;
280 }
281 
282 
283 template <class Scalar>
284 bool IntegratorBasic<Scalar>::advanceTime(const Scalar timeFinal)
285 {
286  if (timeStepControl_->timeInRange(timeFinal))
287  timeStepControl_->setFinalTime(timeFinal);
288  bool itgrStatus = advanceTime();
289  return itgrStatus;
290 }
291 
292 
293 template <class Scalar>
295 {
296  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
297  out->setOutputToRootOnly(0);
298  if (isInitialized_ == false) {
299  Teuchos::OSTab ostab(out,1,"StartIntegrator");
300  *out << "Failure - IntegratorBasic is not initialized." << std::endl;
301  setStatus(Status::FAILED);
302  return;
303  }
304 
305  //set the Abs/Rel tolerance
306  auto cs = solutionHistory_->getCurrentState();
307  cs->setTolRel(timeStepControl_->getMaxRelError());
308  cs->setTolAbs(timeStepControl_->getMaxAbsError());
309 
310  integratorTimer_->start();
311  // get optimal initial time step
312  const Scalar initDt =
313  std::min(timeStepControl_->getInitTimeStep(),
314  stepper_->getInitTimeStep(solutionHistory_));
315  // update initial time step
316  timeStepControl_->setInitTimeStep(initDt);
317  timeStepControl_->initialize();
318  setStatus(WORKING);
319 }
320 
321 
322 template <class Scalar>
324 {
325  TEMPUS_FUNC_TIME_MONITOR("Tempus::IntegratorBasic::advanceTime()");
326  {
327  startIntegrator();
328  integratorObserver_->observeStartIntegrator(*this);
329 
330  while (integratorStatus_ == WORKING &&
331  timeStepControl_->timeInRange (solutionHistory_->getCurrentTime()) &&
332  timeStepControl_->indexInRange(solutionHistory_->getCurrentIndex())){
333 
334  stepperTimer_->reset();
335  stepperTimer_->start();
336  solutionHistory_->initWorkingState();
337 
338  startTimeStep();
339  integratorObserver_->observeStartTimeStep(*this);
340 
341  timeStepControl_->setNextTimeStep(solutionHistory_, integratorStatus_);
342  integratorObserver_->observeNextTimeStep(*this);
343 
344  if (integratorStatus_ == Status::FAILED) break;
345  solutionHistory_->getWorkingState()->setSolutionStatus(WORKING);
346 
347  integratorObserver_->observeBeforeTakeStep(*this);
348 
349  stepper_->takeStep(solutionHistory_);
350 
351  integratorObserver_->observeAfterTakeStep(*this);
352 
353  stepperTimer_->stop();
354  checkTimeStep();
355  integratorObserver_->observeAfterCheckTimeStep(*this);
356 
357  solutionHistory_->promoteWorkingState();
358  integratorObserver_->observeEndTimeStep(*this);
359  }
360 
361  endIntegrator();
362  integratorObserver_->observeEndIntegrator(*this);
363  }
364 
365  return (integratorStatus_ == Status::PASSED);
366 }
367 
368 
369 template <class Scalar>
371 {
372  auto ws = solutionHistory_->getWorkingState();
373 
374  //set the Abs/Rel tolerance
375  ws->setTolRel(timeStepControl_->getMaxRelError());
376  ws->setTolAbs(timeStepControl_->getMaxAbsError());
377 
378  // Check if we need to dump screen output this step
379  std::vector<int>::const_iterator it =
380  std::find(outputScreenIndices_.begin(),
381  outputScreenIndices_.end(),
382  ws->getIndex());
383  if (it == outputScreenIndices_.end())
384  ws->setOutputScreen(false);
385  else
386  ws->setOutputScreen(true);
387 
388  const int initial = timeStepControl_->getInitIndex();
389  if ( (ws->getIndex() - initial) % outputScreenInterval_ == 0)
390  ws->setOutputScreen(true);
391 }
392 
393 
394 template <class Scalar>
396 {
397  using Teuchos::RCP;
398  auto ws = solutionHistory_->getWorkingState();
399 
400  // Too many TimeStep failures, Integrator fails.
401  if (ws->getNFailures() >= timeStepControl_->getMaxFailures()) {
402  RCP<Teuchos::FancyOStream> out = this->getOStream();
403  out->setOutputToRootOnly(0);
404  Teuchos::OSTab ostab(out, 2, "checkTimeStep");
405  *out << "Failure - Stepper has failed more than the maximum allowed.\n"
406  << " (nFailures = "<<ws->getNFailures()<< ") >= (nFailuresMax = "
407  << timeStepControl_->getMaxFailures()<<")" << std::endl;
408  setStatus(Status::FAILED);
409  return;
410  }
411  if (ws->getNConsecutiveFailures()
412  >= timeStepControl_->getMaxConsecFailures()){
413  RCP<Teuchos::FancyOStream> out = this->getOStream();
414  out->setOutputToRootOnly(0);
415  Teuchos::OSTab ostab(out, 1, "checkTimeStep");
416  *out << "Failure - Stepper has failed more than the maximum "
417  << "consecutive allowed.\n"
418  << " (nConsecutiveFailures = "<<ws->getNConsecutiveFailures()
419  << ") >= (nConsecutiveFailuresMax = "
420  << timeStepControl_->getMaxConsecFailures()
421  << ")" << std::endl;
422  setStatus(Status::FAILED);
423  return;
424  }
425 
426  // Check Stepper failure.
427  if (ws->getSolutionStatus() == Status::FAILED ||
428  // Constant time step failure
429  ((timeStepControl_->getStepType() == "Constant") &&
430  (ws->getTimeStep() != timeStepControl_->getInitTimeStep()) &&
431  (ws->getOutput() != true) &&
432  (ws->getTime() != timeStepControl_->getFinalTime())
433  )
434  )
435  {
436  RCP<Teuchos::FancyOStream> out = this->getOStream();
437  out->setOutputToRootOnly(0);
438  Teuchos::OSTab ostab(out, 0, "checkTimeStep");
439  *out <<std::scientific
440  <<std::setw( 6)<<std::setprecision(3)<<ws->getIndex()
441  <<std::setw(11)<<std::setprecision(3)<<ws->getTime()
442  <<std::setw(11)<<std::setprecision(3)<<ws->getTimeStep()
443  << " STEP FAILURE!! - ";
444  if (ws->getSolutionStatus() == Status::FAILED) {
445  *out << "Solution Status = " << toString(ws->getSolutionStatus())
446  << std::endl;
447  } else if ((timeStepControl_->getStepType() == "Constant") &&
448  (ws->getTimeStep() != timeStepControl_->getInitTimeStep())) {
449  *out << "dt != Constant dt (="<<timeStepControl_->getInitTimeStep()<<")"
450  << std::endl;
451  }
452 
453  ws->setNFailures(ws->getNFailures()+1);
454  ws->setNRunningFailures(ws->getNRunningFailures()+1);
455  ws->setNConsecutiveFailures(ws->getNConsecutiveFailures()+1);
456  ws->setSolutionStatus(Status::FAILED);
457  return;
458  }
459 
460  // TIME STEP PASSED basic tests! Ensure it is set as such.
461  ws->setSolutionStatus(Status::PASSED);
462 
463 }
464 
465 
466 template <class Scalar>
468 {
469  std::string exitStatus;
470  if (solutionHistory_->getCurrentState()->getSolutionStatus() ==
471  Status::FAILED || integratorStatus_ == Status::FAILED) {
472  exitStatus = "Time integration FAILURE!";
473  } else {
474  setStatus(Status::PASSED);
475  exitStatus = "Time integration complete.";
476  }
477 
478  integratorTimer_->stop();
479 }
480 
481 
482 template <class Scalar>
484 {
485  // Parse output indices
486  std::string delimiters(",");
487  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
488  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
489  while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
490  std::string token = str.substr(lastPos,pos-lastPos);
491  outputScreenIndices_.push_back(int(std::stoi(token)));
492  if(pos==std::string::npos)
493  break;
494 
495  lastPos = str.find_first_not_of(delimiters, pos);
496  pos = str.find_first_of(delimiters, lastPos);
497  }
498 
499  // order output indices and remove duplicates.
500  std::sort(outputScreenIndices_.begin(),outputScreenIndices_.end());
501  outputScreenIndices_.erase(std::unique(outputScreenIndices_.begin(),
502  outputScreenIndices_.end() ),
503  outputScreenIndices_.end() );
504  return;
505 }
506 
507 
508 template <class Scalar>
510 {
511  std::stringstream ss;
512  for(size_t i = 0; i < outputScreenIndices_.size(); ++i) {
513  if(i != 0) ss << ", ";
514  ss << outputScreenIndices_[i];
515  }
516  return ss.str();
517 }
518 
519 
522 template<class Scalar>
523 Teuchos::RCP<const Teuchos::ParameterList>
525 {
526  Teuchos::RCP<Teuchos::ParameterList> pl =
527  Teuchos::parameterList(getIntegratorName());
528 
529  pl->set("Integrator Type", getIntegratorType(),
530  "'Integrator Type' must be 'Integrator Basic'.");
531 
532  pl->set("Screen Output Index List", getScreenOutputIndexListString(),
533  "Screen Output Index List. Required to be in TimeStepControl range "
534  "['Minimum Time Step Index', 'Maximum Time Step Index']");
535 
536  pl->set("Screen Output Index Interval", getScreenOutputIndexInterval(),
537  "Screen Output Index Interval (e.g., every 100 time steps)");
538 
539  pl->set("Stepper Name", stepper_->getStepperName(),
540  "'Stepper Name' selects the Stepper block to construct (Required).");
541 
542  pl->set("Solution History", *solutionHistory_->getValidParameters());
543  pl->set("Time Step Control", *timeStepControl_->getValidParameters());
544 
545 
546  Teuchos::RCP<Teuchos::ParameterList> tempusPL =
547  Teuchos::parameterList("Tempus");
548 
549  tempusPL->set("Integrator Name", pl->name());
550  tempusPL->set(pl->name(), *pl);
551  tempusPL->set(stepper_->getStepperName(), *stepper_->getValidParameters());
552 
553  return tempusPL;
554 }
555 
556 
557 // Nonmember constructor
558 // ------------------------------------------------------------------------
559 template<class Scalar>
560 Teuchos::RCP<IntegratorBasic<Scalar> > createIntegratorBasic(
561  Teuchos::RCP<Teuchos::ParameterList> tempusPL)
562 {
563  auto integratorName = tempusPL->get<std::string>("Integrator Name");
564  auto integratorPL = Teuchos::sublist(tempusPL, integratorName, true);
565 
566  std::string integratorType = integratorPL->get<std::string>("Integrator Type");
567  TEUCHOS_TEST_FOR_EXCEPTION( integratorType != "Integrator Basic",
568  std::logic_error,
569  "Error - For IntegratorBasic, 'Integrator Type' should be "
570  << "'Integrator Basic'.\n"
571  << " Integrator Type = " << integratorType << "\n");
572 
573  auto integrator = Teuchos::rcp(new IntegratorBasic<Scalar>());
574  integrator->setIntegratorName(integratorName);
575 
576  // Set Stepper
577  if (integratorPL->isParameter("Stepper Name")) {
578  // Construct from Integrator ParameterList
579  auto stepperName = integratorPL->get<std::string>("Stepper Name");
580  auto stepperPL = Teuchos::sublist(tempusPL, stepperName, true);
581  stepperPL->setName(stepperName);
582  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
583  integrator->setStepper(sf->createStepper(stepperPL));
584  } else {
585  // Construct default Stepper
586  auto stepper = Teuchos::rcp(new StepperForwardEuler<Scalar>());
587  integrator->setStepper(stepper);
588  }
589 
590  // Set TimeStepControl
591  if (integratorPL->isSublist("Time Step Control")) {
592  // Construct from Integrator ParameterList
593  auto tscPL = Teuchos::sublist(integratorPL, "Time Step Control", true);
594  integrator->setTimeStepControl(createTimeStepControl<Scalar>(tscPL));
595  } else {
596  // Construct default TimeStepControl
597  integrator->setTimeStepControl(rcp(new TimeStepControl<Scalar>()));
598  }
599 
600  // Set SolutionHistory
601  if (integratorPL->isSublist("Solution History")) {
602  // Construct from Integrator ParameterList
603  auto shPL = Teuchos::sublist(integratorPL, "Solution History", true);
604  auto sh = createSolutionHistoryPL<Scalar>(shPL);
605  integrator->setSolutionHistory(sh);
606  } else {
607  // Construct default SolutionHistory
608  integrator->setSolutionHistory(createSolutionHistory<Scalar>());
609  }
610 
611  // Set Observer to default.
612  integrator->setObserver(Teuchos::null);
613 
614  // Set screen output interval.
615  integrator->setScreenOutputIndexInterval(
616  integratorPL->get<int>("Screen Output Index Interval",
617  integrator->getScreenOutputIndexInterval()));
618 
619  // Parse screen output indices
620  auto str = integratorPL->get<std::string>("Screen Output Index List", "");
621  integrator->setScreenOutputIndexList(str);
622 
623  auto validPL = Teuchos::rcp_const_cast<Teuchos::ParameterList>(integrator->getValidParameters());
624 
625  // Validate the Integrator ParameterList
626  auto vIntegratorName = validPL->template get<std::string>("Integrator Name");
627  auto vIntegratorPL = Teuchos::sublist(validPL, vIntegratorName, true);
628  integratorPL->validateParametersAndSetDefaults(*vIntegratorPL,1);
629 
630  // Validate the Stepper ParameterList
631  auto stepperName = integratorPL->get<std::string>("Stepper Name");
632  auto stepperPL = Teuchos::sublist(tempusPL, stepperName, true);
633  auto vStepperName = vIntegratorPL->template get<std::string>("Stepper Name");
634  auto vStepperPL = Teuchos::sublist(validPL, vStepperName, true);
635  stepperPL->validateParametersAndSetDefaults(*vStepperPL);
636 
637  return integrator; // integrator is not initialized (missing model and IC).
638 }
639 
640 
641 // Nonmember constructor
642 // ------------------------------------------------------------------------
643 template<class Scalar>
644 Teuchos::RCP<IntegratorBasic<Scalar> > createIntegratorBasic(
645  Teuchos::RCP<Teuchos::ParameterList> tempusPL,
646  const Teuchos::RCP<Thyra::ModelEvaluator<Scalar> >& model)
647 {
648  auto integrator = createIntegratorBasic<Scalar>(tempusPL);
649  if ( model == Teuchos::null ) return integrator;
650 
651  Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> > constModel = model;
652  integrator->setModel(constModel);
653 
654  // Construct default IC state from the application model and TimeStepControl
655  auto newState = createSolutionStateME(integrator->getStepper()->getModel(),
656  integrator->getStepper()->getDefaultStepperState());
657  newState->setTime (integrator->getTimeStepControl()->getInitTime());
658  newState->setIndex (integrator->getTimeStepControl()->getInitIndex());
659  newState->setTimeStep(integrator->getTimeStepControl()->getInitTimeStep());
660  newState->setTolRel (integrator->getTimeStepControl()->getMaxRelError());
661  newState->setTolAbs (integrator->getTimeStepControl()->getMaxAbsError());
662  newState->setOrder (integrator->getStepper()->getOrder());
663  newState->setSolutionStatus(Status::PASSED); // ICs are considered passing.
664 
665  // Set SolutionHistory IC
666  auto sh = integrator->getNonConstSolutionHistory();
667  sh->addState(newState);
668  integrator->getStepper()->setInitialConditions(sh);
669 
670  integrator->initialize();
671 
672  return integrator;
673 }
674 
675 
677 template<class Scalar>
678 Teuchos::RCP<IntegratorBasic<Scalar> > createIntegratorBasic(
679  const Teuchos::RCP<Thyra::ModelEvaluator<Scalar> >& model,
680  std::string stepperType)
681 {
682  using Teuchos::rcp;
683  auto integrator = rcp(new IntegratorBasic<Scalar>());
684 
685  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
686  auto stepper = sf->createStepper(stepperType, model);
687  integrator->setStepper(stepper);
688  integrator->initializeSolutionHistory();
689  integrator->initialize();
690 
691  return integrator;
692 }
693 
694 
696 template<class Scalar>
697 Teuchos::RCP<IntegratorBasic<Scalar> > createIntegratorBasic()
698 {
699  return Teuchos::rcp(new IntegratorBasic<Scalar>());
700 }
701 
702 
704 template<class Scalar>
705 Teuchos::RCP<IntegratorBasic<Scalar> > createIntegratorBasic(
706  Teuchos::RCP<Teuchos::ParameterList> tempusPL,
707  std::vector<Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> > > models)
708 {
709  auto integratorName = tempusPL->get<std::string>("Integrator Name");
710  auto integratorPL = Teuchos::sublist(tempusPL, integratorName, true);
711 
712  std::string integratorType = integratorPL->get<std::string>("Integrator Type");
713  TEUCHOS_TEST_FOR_EXCEPTION( integratorType != "Integrator Basic",
714  std::logic_error,
715  "Error - For IntegratorBasic, 'Integrator Type' should be "
716  << "'Integrator Basic'.\n"
717  << " Integrator Type = " << integratorType << "\n");
718 
719  auto integrator = Teuchos::rcp(new IntegratorBasic<Scalar>());
720  integrator->setIntegratorName(integratorName);
721 
722  TEUCHOS_TEST_FOR_EXCEPTION( !integratorPL->isParameter("Stepper Name"),
723  std::logic_error,
724  "Error - Need to set the 'Stepper Name' in 'Integrator Basic'.\n");
725 
726  auto stepperName = integratorPL->get<std::string>("Stepper Name");
727  TEUCHOS_TEST_FOR_EXCEPTION( stepperName == "Operator Split",
728  std::logic_error,
729  "Error - 'Stepper Name' should be 'Operator Split'.\n");
730 
731  // Construct Steppers from Integrator ParameterList
732  auto stepperPL = Teuchos::sublist(tempusPL, stepperName, true);
733  stepperPL->setName(stepperName);
734  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
735  integrator->setStepper(sf->createStepper(stepperPL, models));
736 
737  // Set TimeStepControl
738  if (integratorPL->isSublist("Time Step Control")) {
739  // Construct from Integrator ParameterList
740  auto tscPL = Teuchos::sublist(integratorPL, "Time Step Control", true);
741  integrator->setTimeStepControl(createTimeStepControl<Scalar>(tscPL));
742  } else {
743  // Construct default TimeStepControl
744  integrator->setTimeStepControl(rcp(new TimeStepControl<Scalar>()));
745  }
746 
747  // Construct default IC state from the application model and TimeStepControl
748  auto newState = createSolutionStateME(integrator->getStepper()->getModel(),
749  integrator->getStepper()->getDefaultStepperState());
750  newState->setTime (integrator->getTimeStepControl()->getInitTime());
751  newState->setIndex (integrator->getTimeStepControl()->getInitIndex());
752  newState->setTimeStep(integrator->getTimeStepControl()->getInitTimeStep());
753  newState->setTolRel (integrator->getTimeStepControl()->getMaxRelError());
754  newState->setTolAbs (integrator->getTimeStepControl()->getMaxAbsError());
755  newState->setOrder (integrator->getStepper()->getOrder());
756  newState->setSolutionStatus(Status::PASSED); // ICs are considered passing.
757 
758  // Set SolutionHistory
759  auto shPL = Teuchos::sublist(integratorPL, "Solution History", true);
760  auto sh = createSolutionHistoryPL<Scalar>(shPL);
761  sh->addState(newState);
762  integrator->getStepper()->setInitialConditions(sh);
763  integrator->setSolutionHistory(sh);
764 
765  // Set Observer to default.
766  integrator->setObserver(Teuchos::null);
767 
768  // Set screen output interval.
769  integrator->setScreenOutputIndexInterval(
770  integratorPL->get<int>("Screen Output Index Interval",
771  integrator->getScreenOutputIndexInterval()));
772 
773  // Parse screen output indices
774  auto str = integratorPL->get<std::string>("Screen Output Index List", "");
775  integrator->setScreenOutputIndexList(str);
776 
777  auto validPL = Teuchos::rcp_const_cast<Teuchos::ParameterList>(integrator->getValidParameters());
778 
779  // Validate the Integrator ParameterList
780  auto vIntegratorName = validPL->template get<std::string>("Integrator Name");
781  auto vIntegratorPL = Teuchos::sublist(validPL, vIntegratorName, true);
782  integratorPL->validateParametersAndSetDefaults(*vIntegratorPL);
783 
784  // Validate the Stepper ParameterList
785  auto vStepperName = vIntegratorPL->template get<std::string>("Stepper Name");
786  auto vStepperPL = Teuchos::sublist(validPL, vStepperName, true);
787  stepperPL->validateParametersAndSetDefaults(*vStepperPL);
788 
789  integrator->initialize();
790 
791  return integrator;
792 }
793 
794 
795 } // namespace Tempus
796 #endif // Tempus_IntegratorBasic_impl_hpp
virtual bool advanceTime()
Advance the solution to timeMax, and return true if successful.
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateX(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdot=Teuchos::null, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdotdot=Teuchos::null)
Nonmember constructor from non-const solution vectors, x.
virtual void setModel(Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > model)
Set the model on the stepper.
const std::string toString(const Status status)
Convert Status to string.
Teuchos::RCP< Teuchos::Time > integratorTimer_
std::string description() const override
virtual void setTimeStepControl(Teuchos::RCP< TimeStepControl< Scalar > > tsc=Teuchos::null)
Set the TimeStepControl.
virtual void setObserver(Teuchos::RCP< IntegratorObserver< Scalar > > obs=Teuchos::null)
Set the Observer.
Teuchos::RCP< Teuchos::Time > stepperTimer_
IntegratorObserverBasic class for time integrators. This basic class has simple no-op functions...
IntegratorBasic()
Default constructor that requires a subsequent, ??? , setStepper, and initialize calls.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Create valid IntegratorBasic ParameterList.
virtual void setStepper(Teuchos::RCP< Stepper< Scalar > > stepper)
Set the Stepper.
Thyra Base interface for time steppers.
virtual void checkTimeStep()
Check if time step has passed or failed.
virtual std::string getScreenOutputIndexListString() const
virtual void setSolutionHistory(Teuchos::RCP< SolutionHistory< Scalar > > sh=Teuchos::null)
Set the SolutionHistory.
IntegratorObserver class for time integrators.
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
virtual void initializeSolutionHistory(Teuchos::RCP< SolutionState< Scalar > > state=Teuchos::null)
Set the initial state which has the initial conditions.
virtual void setScreenOutputIndexList(std::vector< int > indices)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
void setIntegratorName(std::string i)
Set the Integrator Name.
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.
Teuchos::RCP< IntegratorBasic< Scalar > > createIntegratorBasic(Teuchos::RCP< Teuchos::ParameterList > pList)
Nonmember constructor.
virtual void initialize()
Initializes the Integrator after set* function calls.
virtual void endIntegrator()
Perform tasks after end of integrator.
virtual void startIntegrator()
Perform tasks before start of integrator.
void setIntegratorType(std::string i)
Set the Integrator Type.
virtual void startTimeStep()
Start time step.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...