Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
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>
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>
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>
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>
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 &in_out,
254  const Teuchos::EVerbosityLevel verbLevel) const
255 {
256  auto out = Teuchos::fancyOStream( in_out.getOStream() );
257  out->setOutputToRootOnly(0);
258  *out << description() << "::describe" << std::endl;
259  *out << "solutionHistory= " << solutionHistory_->description()<<std::endl;
260  *out << "timeStepControl= " << timeStepControl_->description()<<std::endl;
261  *out << "stepper = " << stepper_ ->description()<<std::endl;
262 
263  if (Teuchos::as<int>(verbLevel) >=
264  Teuchos::as<int>(Teuchos::VERB_HIGH)) {
265  *out << "solutionHistory= " << std::endl;
266  solutionHistory_->describe(in_out,verbLevel);
267  *out << "timeStepControl= " << std::endl;
268  timeStepControl_->describe(in_out,verbLevel);
269  *out << "stepper = " << std::endl;
270  stepper_ ->describe(in_out,verbLevel);
271  }
272 }
273 
274 
275 template <class Scalar>
276 bool IntegratorBasic<Scalar>::advanceTime(const Scalar timeFinal)
277 {
278  if (timeStepControl_->timeInRange(timeFinal))
279  timeStepControl_->setFinalTime(timeFinal);
280  bool itgrStatus = advanceTime();
281  return itgrStatus;
282 }
283 
284 
285 template <class Scalar>
287 {
288  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
289  out->setOutputToRootOnly(0);
290  if (isInitialized_ == false) {
291  Teuchos::OSTab ostab(out,1,"StartIntegrator");
292  *out << "Failure - IntegratorBasic is not initialized." << std::endl;
293  integratorStatus_ = Status::FAILED;
294  return;
295  }
296 
297  //set the Abs/Rel tolerance
298  auto cs = solutionHistory_->getCurrentState();
299  cs->setTolRel(timeStepControl_->getMaxRelError());
300  cs->setTolAbs(timeStepControl_->getMaxAbsError());
301 
302  integratorTimer_->start();
303  // get optimal initial time step
304  const Scalar initDt =
305  std::min(timeStepControl_->getInitTimeStep(),
306  stepper_->getInitTimeStep(solutionHistory_));
307  // update initial time step
308  timeStepControl_->setInitTimeStep(initDt);
309  timeStepControl_->initialize();
310  integratorStatus_ = WORKING;
311 }
312 
313 
314 template <class Scalar>
316 {
317  TEMPUS_FUNC_TIME_MONITOR("Tempus::IntegratorBasic::advanceTime()");
318  {
319  startIntegrator();
320  integratorObserver_->observeStartIntegrator(*this);
321 
322  while (integratorStatus_ == WORKING &&
323  timeStepControl_->timeInRange (solutionHistory_->getCurrentTime()) &&
324  timeStepControl_->indexInRange(solutionHistory_->getCurrentIndex())){
325 
326  stepperTimer_->reset();
327  stepperTimer_->start();
328  solutionHistory_->initWorkingState();
329 
330  startTimeStep();
331  integratorObserver_->observeStartTimeStep(*this);
332 
333  timeStepControl_->setNextTimeStep(solutionHistory_, integratorStatus_);
334  integratorObserver_->observeNextTimeStep(*this);
335 
336  if (integratorStatus_ == Status::FAILED) break;
337  solutionHistory_->getWorkingState()->setSolutionStatus(WORKING);
338 
339  integratorObserver_->observeBeforeTakeStep(*this);
340 
341  stepper_->takeStep(solutionHistory_);
342 
343  integratorObserver_->observeAfterTakeStep(*this);
344 
345  stepperTimer_->stop();
346  checkTimeStep();
347  integratorObserver_->observeAfterCheckTimeStep(*this);
348 
349  solutionHistory_->promoteWorkingState();
350  integratorObserver_->observeEndTimeStep(*this);
351  }
352 
353  endIntegrator();
354  integratorObserver_->observeEndIntegrator(*this);
355  }
356 
357  return (integratorStatus_ == Status::PASSED);
358 }
359 
360 
361 template <class Scalar>
363 {
364  auto ws = solutionHistory_->getWorkingState();
365 
366  //set the Abs/Rel tolerance
367  ws->setTolRel(timeStepControl_->getMaxRelError());
368  ws->setTolAbs(timeStepControl_->getMaxAbsError());
369 
370  // Check if we need to dump screen output this step
371  std::vector<int>::const_iterator it =
372  std::find(outputScreenIndices_.begin(),
373  outputScreenIndices_.end(),
374  ws->getIndex());
375  if (it == outputScreenIndices_.end())
376  ws->setOutputScreen(false);
377  else
378  ws->setOutputScreen(true);
379 
380  const int initial = timeStepControl_->getInitIndex();
381  if ( (ws->getIndex() - initial) % outputScreenInterval_ == 0)
382  ws->setOutputScreen(true);
383 }
384 
385 
386 template <class Scalar>
388 {
389  using Teuchos::RCP;
390  auto ws = solutionHistory_->getWorkingState();
391 
392  // Too many TimeStep failures, Integrator fails.
393  if (ws->getNFailures() >= timeStepControl_->getMaxFailures()) {
394  RCP<Teuchos::FancyOStream> out = this->getOStream();
395  out->setOutputToRootOnly(0);
396  Teuchos::OSTab ostab(out,2,"checkTimeStep");
397  *out << "Failure - Stepper has failed more than the maximum allowed.\n"
398  << " (nFailures = "<<ws->getNFailures()<< ") >= (nFailuresMax = "
399  << timeStepControl_->getMaxFailures()<<")" << std::endl;
400  integratorStatus_ = Status::FAILED;
401  return;
402  }
403  if (ws->getNConsecutiveFailures()
404  >= timeStepControl_->getMaxConsecFailures()){
405  RCP<Teuchos::FancyOStream> out = this->getOStream();
406  out->setOutputToRootOnly(0);
407  Teuchos::OSTab ostab(out,1,"checkTimeStep");
408  *out << "Failure - Stepper has failed more than the maximum "
409  << "consecutive allowed.\n"
410  << " (nConsecutiveFailures = "<<ws->getNConsecutiveFailures()
411  << ") >= (nConsecutiveFailuresMax = "
412  << timeStepControl_->getMaxConsecFailures()
413  << ")" << std::endl;
414  integratorStatus_ = Status::FAILED;
415  return;
416  }
417 
418  // Check Stepper failure.
419  if (ws->getSolutionStatus() == Status::FAILED ||
420  // Constant time step failure
421  ((timeStepControl_->getStepType() == "Constant") &&
422  (ws->getTimeStep() != timeStepControl_->getInitTimeStep()) &&
423  (ws->getOutput() != true) &&
424  (ws->getTime() != timeStepControl_->getFinalTime())
425  )
426  )
427  {
428  RCP<Teuchos::FancyOStream> out = this->getOStream();
429  out->setOutputToRootOnly(0);
430  Teuchos::OSTab ostab(out,0,"checkTimeStep");
431  *out <<std::scientific
432  <<std::setw( 6)<<std::setprecision(3)<<ws->getIndex()
433  <<std::setw(11)<<std::setprecision(3)<<ws->getTime()
434  <<std::setw(11)<<std::setprecision(3)<<ws->getTimeStep()
435  << " STEP FAILURE!! - ";
436  if (ws->getSolutionStatus() == Status::FAILED) {
437  *out << "Solution Status = " << toString(ws->getSolutionStatus())
438  << std::endl;
439  } else if ((timeStepControl_->getStepType() == "Constant") &&
440  (ws->getTimeStep() != timeStepControl_->getInitTimeStep())) {
441  *out << "dt != Constant dt (="<<timeStepControl_->getInitTimeStep()<<")"
442  << std::endl;
443  }
444 
445  ws->setNFailures(ws->getNFailures()+1);
446  ws->setNRunningFailures(ws->getNRunningFailures()+1);
447  ws->setNConsecutiveFailures(ws->getNConsecutiveFailures()+1);
448  ws->setSolutionStatus(Status::FAILED);
449  return;
450  }
451 
452  // TIME STEP PASSED basic tests! Ensure it is set as such.
453  ws->setSolutionStatus(Status::PASSED);
454 
455 }
456 
457 
458 template <class Scalar>
460 {
461  std::string exitStatus;
462  if (solutionHistory_->getCurrentState()->getSolutionStatus() ==
463  Status::FAILED || integratorStatus_ == Status::FAILED) {
464  exitStatus = "Time integration FAILURE!";
465  } else {
466  integratorStatus_ = Status::PASSED;
467  exitStatus = "Time integration complete.";
468  }
469 
470  integratorTimer_->stop();
471 }
472 
473 
474 template <class Scalar>
476 {
477  // Parse output indices
478  std::string delimiters(",");
479  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
480  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
481  while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
482  std::string token = str.substr(lastPos,pos-lastPos);
483  outputScreenIndices_.push_back(int(std::stoi(token)));
484  if(pos==std::string::npos)
485  break;
486 
487  lastPos = str.find_first_not_of(delimiters, pos);
488  pos = str.find_first_of(delimiters, lastPos);
489  }
490 
491  // order output indices and remove duplicates.
492  std::sort(outputScreenIndices_.begin(),outputScreenIndices_.end());
493  outputScreenIndices_.erase(std::unique(outputScreenIndices_.begin(),
494  outputScreenIndices_.end() ),
495  outputScreenIndices_.end() );
496  return;
497 }
498 
499 
500 template <class Scalar>
502 {
503  std::stringstream ss;
504  for(size_t i = 0; i < outputScreenIndices_.size(); ++i) {
505  if(i != 0) ss << ", ";
506  ss << outputScreenIndices_[i];
507  }
508  return ss.str();
509 }
510 
511 
514 template<class Scalar>
517 {
519  Teuchos::parameterList(getIntegratorName());
520 
521  pl->set("Integrator Type", getIntegratorType(),
522  "'Integrator Type' must be 'Integrator Basic'.");
523 
524  pl->set("Screen Output Index List", getScreenOutputIndexListString(),
525  "Screen Output Index List. Required to be in TimeStepControl range "
526  "['Minimum Time Step Index', 'Maximum Time Step Index']");
527 
528  pl->set("Screen Output Index Interval", getScreenOutputIndexInterval(),
529  "Screen Output Index Interval (e.g., every 100 time steps)");
530 
531  pl->set("Stepper Name", stepper_->getStepperName(),
532  "'Stepper Name' selects the Stepper block to construct (Required).");
533 
534  pl->set("Solution History", *solutionHistory_->getValidParameters());
535  pl->set("Time Step Control", *timeStepControl_->getValidParameters());
536 
537 
539  Teuchos::parameterList("Tempus");
540 
541  tempusPL->set("Integrator Name", pl->name());
542  tempusPL->set(pl->name(), *pl);
543  tempusPL->set(stepper_->getStepperName(), *stepper_->getValidParameters());
544 
545  return tempusPL;
546 }
547 
548 
549 // Nonmember constructor
550 // ------------------------------------------------------------------------
551 template<class Scalar>
555 {
556  auto integratorName = tempusPL->get<std::string>("Integrator Name");
557  auto integratorPL = Teuchos::sublist(tempusPL, integratorName, true);
558 
559  std::string integratorType = integratorPL->get<std::string>("Integrator Type");
560  TEUCHOS_TEST_FOR_EXCEPTION( integratorType != "Integrator Basic",
561  std::logic_error,
562  "Error - For IntegratorBasic, 'Integrator Type' should be "
563  << "'Integrator Basic'.\n"
564  << " Integrator Type = " << integratorType << "\n");
565 
566  auto integrator = Teuchos::rcp(new IntegratorBasic<Scalar>());
567  integrator->setIntegratorName(integratorName);
568 
569  // Set Stepper
570  if (integratorPL->isParameter("Stepper Name")) {
571  // Construct from Integrator ParameterList
572  auto stepperName = integratorPL->get<std::string>("Stepper Name");
573  auto stepperPL = Teuchos::sublist(tempusPL, stepperName, true);
574  stepperPL->setName(stepperName);
575  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
576  integrator->setStepper(sf->createStepper(stepperPL, model));
577  } else {
578  // Construct default Stepper
580  integrator->setStepper(
581  createStepperForwardEuler(constModel, Teuchos::null));
582  }
583 
584  // Set TimeStepControl
585  if (integratorPL->isSublist("Time Step Control")) {
586  // Construct from Integrator ParameterList
587  auto tscPL = Teuchos::sublist(integratorPL, "Time Step Control", true);
588  integrator->setTimeStepControl(createTimeStepControl<Scalar>(tscPL));
589  } else {
590  // Construct default TimeStepControl
591  integrator->setTimeStepControl(rcp(new TimeStepControl<Scalar>()));
592  }
593 
594  // Construct default IC state from the application model and TimeStepControl
595  auto newState = createSolutionStateME(integrator->getStepper()->getModel(),
596  integrator->getStepper()->getDefaultStepperState());
597  newState->setTime (integrator->getTimeStepControl()->getInitTime());
598  newState->setIndex (integrator->getTimeStepControl()->getInitIndex());
599  newState->setTimeStep(integrator->getTimeStepControl()->getInitTimeStep());
600  newState->setTolRel (integrator->getTimeStepControl()->getMaxRelError());
601  newState->setTolAbs (integrator->getTimeStepControl()->getMaxAbsError());
602  newState->setOrder (integrator->getStepper()->getOrder());
603  newState->setSolutionStatus(Status::PASSED); // ICs are considered passing.
604 
605  // Set SolutionHistory
606  auto shPL = Teuchos::sublist(integratorPL, "Solution History", true);
607  auto sh = createSolutionHistoryPL<Scalar>(shPL);
608  sh->addState(newState);
609  integrator->getStepper()->setInitialConditions(sh);
610  integrator->setSolutionHistory(sh);
611 
612  // Set Observer to default.
613  integrator->setObserver(Teuchos::null);
614 
615  // Set screen output interval.
616  integrator->setScreenOutputIndexInterval(
617  integratorPL->get<int>("Screen Output Index Interval",
618  integrator->getScreenOutputIndexInterval()));
619 
620  // Parse screen output indices
621  auto str = integratorPL->get<std::string>("Screen Output Index List", "");
622  integrator->setScreenOutputIndexList(str);
623 
624  auto validPL = Teuchos::rcp_const_cast<Teuchos::ParameterList>(integrator->getValidParameters());
625 
626  // Validate the Integrator ParameterList
627  auto vIntegratorName = validPL->template get<std::string>("Integrator Name");
628  auto vIntegratorPL = Teuchos::sublist(validPL, vIntegratorName, true);
629  integratorPL->validateParametersAndSetDefaults(*vIntegratorPL);
630 
631  // Validate the Stepper ParameterList
632  auto stepperName = integratorPL->get<std::string>("Stepper Name");
633  auto stepperPL = Teuchos::sublist(tempusPL, stepperName, true);
634  auto vStepperName = vIntegratorPL->template get<std::string>("Stepper Name");
635  auto vStepperPL = Teuchos::sublist(validPL, vStepperName, true);
636  stepperPL->validateParametersAndSetDefaults(*vStepperPL);
637 
638  integrator->initialize();
639 
640  return integrator;
641 }
642 
643 
645 template<class Scalar>
648  std::string stepperType)
649 {
650  using Teuchos::rcp;
651  auto integrator = rcp(new IntegratorBasic<Scalar>());
652 
653  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
654  auto stepper = sf->createStepper(stepperType, model);
655  integrator->setStepper(stepper);
656  integrator->initializeSolutionHistory();
657  integrator->initialize();
658 
659  return integrator;
660 }
661 
662 
664 template<class Scalar>
666 {
668 }
669 
670 
672 template<class Scalar>
675  std::vector<Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> > > models)
676 {
677  auto integratorName = tempusPL->get<std::string>("Integrator Name");
678  auto integratorPL = Teuchos::sublist(tempusPL, integratorName, true);
679 
680  std::string integratorType = integratorPL->get<std::string>("Integrator Type");
681  TEUCHOS_TEST_FOR_EXCEPTION( integratorType != "Integrator Basic",
682  std::logic_error,
683  "Error - For IntegratorBasic, 'Integrator Type' should be "
684  << "'Integrator Basic'.\n"
685  << " Integrator Type = " << integratorType << "\n");
686 
687  auto integrator = Teuchos::rcp(new IntegratorBasic<Scalar>());
688  integrator->setIntegratorName(integratorName);
689 
690  TEUCHOS_TEST_FOR_EXCEPTION( !integratorPL->isParameter("Stepper Name"),
691  std::logic_error,
692  "Error - Need to set the 'Stepper Name' in 'Integrator Basic'.\n");
693 
694  auto stepperName = integratorPL->get<std::string>("Stepper Name");
695  TEUCHOS_TEST_FOR_EXCEPTION( stepperName == "Operator Split",
696  std::logic_error,
697  "Error - 'Stepper Name' should be 'Operator Split'.\n");
698 
699  // Construct Steppers from Integrator ParameterList
700  auto stepperPL = Teuchos::sublist(tempusPL, stepperName, true);
701  stepperPL->setName(stepperName);
702  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
703  integrator->setStepper(sf->createStepper(stepperPL, models));
704 
705  // Set TimeStepControl
706  if (integratorPL->isSublist("Time Step Control")) {
707  // Construct from Integrator ParameterList
708  auto tscPL = Teuchos::sublist(integratorPL, "Time Step Control", true);
709  integrator->setTimeStepControl(createTimeStepControl<Scalar>(tscPL));
710  } else {
711  // Construct default TimeStepControl
712  integrator->setTimeStepControl(rcp(new TimeStepControl<Scalar>()));
713  }
714 
715  // Construct default IC state from the application model and TimeStepControl
716  auto newState = createSolutionStateME(integrator->getStepper()->getModel(),
717  integrator->getStepper()->getDefaultStepperState());
718  newState->setTime (integrator->getTimeStepControl()->getInitTime());
719  newState->setIndex (integrator->getTimeStepControl()->getInitIndex());
720  newState->setTimeStep(integrator->getTimeStepControl()->getInitTimeStep());
721  newState->setTolRel (integrator->getTimeStepControl()->getMaxRelError());
722  newState->setTolAbs (integrator->getTimeStepControl()->getMaxAbsError());
723  newState->setOrder (integrator->getStepper()->getOrder());
724  newState->setSolutionStatus(Status::PASSED); // ICs are considered passing.
725 
726  // Set SolutionHistory
727  auto shPL = Teuchos::sublist(integratorPL, "Solution History", true);
728  auto sh = createSolutionHistoryPL<Scalar>(shPL);
729  sh->addState(newState);
730  integrator->getStepper()->setInitialConditions(sh);
731  integrator->setSolutionHistory(sh);
732 
733  // Set Observer to default.
734  integrator->setObserver(Teuchos::null);
735 
736  // Set screen output interval.
737  integrator->setScreenOutputIndexInterval(
738  integratorPL->get<int>("Screen Output Index Interval",
739  integrator->getScreenOutputIndexInterval()));
740 
741  // Parse screen output indices
742  auto str = integratorPL->get<std::string>("Screen Output Index List", "");
743  integrator->setScreenOutputIndexList(str);
744 
745  auto validPL = Teuchos::rcp_const_cast<Teuchos::ParameterList>(integrator->getValidParameters());
746 
747  // Validate the Integrator ParameterList
748  auto vIntegratorName = validPL->template get<std::string>("Integrator Name");
749  auto vIntegratorPL = Teuchos::sublist(validPL, vIntegratorName, true);
750  integratorPL->validateParametersAndSetDefaults(*vIntegratorPL);
751 
752  // Validate the Stepper ParameterList
753  auto vStepperName = vIntegratorPL->template get<std::string>("Stepper Name");
754  auto vStepperPL = Teuchos::sublist(validPL, vStepperName, true);
755  stepperPL->validateParametersAndSetDefaults(*vStepperPL);
756 
757  integrator->initialize();
758 
759  return integrator;
760 }
761 
762 
763 } // namespace Tempus
764 #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.
const std::string & name() const
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_
T & get(const std::string &name, T def_value)
std::string description() const override
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< Teuchos::Time > stepperTimer_
virtual std::string getScreenOutputIndexListString() const
IntegratorObserverBasic class for time integrators. This basic class has simple no-op functions...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Create valid IntegratorBasic ParameterList.
IntegratorBasic()
Default constructor that requires a subsequent, ??? , setStepper, and initialize calls.
virtual void setStepper(Teuchos::RCP< Stepper< Scalar > > stepper)
Set the Stepper.
Thyra Base interface for time steppers.
Teuchos::RCP< StepperForwardEuler< Scalar > > createStepperForwardEuler(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.
virtual void checkTimeStep()
Check if time step has passed or failed.
virtual void setSolutionHistory(Teuchos::RCP< SolutionHistory< Scalar > > sh=Teuchos::null)
Set the SolutionHistory.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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.
Teuchos::RCP< IntegratorBasic< Scalar > > createIntegratorBasic(Teuchos::RCP< Teuchos::ParameterList > pList, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &model)
Nonmember constructor.
virtual void setScreenOutputIndexList(std::vector< int > indices)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
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.
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.
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
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...