9 #ifndef Tempus_IntegratorBasicOld_impl_hpp 10 #define Tempus_IntegratorBasicOld_impl_hpp 12 #include "Thyra_VectorStdOps.hpp" 14 #include "Tempus_StepperFactory.hpp" 19 template<
class Scalar>
21 Teuchos::RCP<Teuchos::ParameterList> inputPL,
23 : integratorObserver_(
Teuchos::null),
24 integratorStatus_(
WORKING), isInitialized_(false)
32 template<
class Scalar>
35 std::string stepperType)
36 : integratorObserver_(
Teuchos::null),
37 integratorStatus_(
WORKING), isInitialized_(false)
41 RCP<Stepper<Scalar> > stepper = sf->createStepper(stepperType, model);
49 template<
class Scalar>
51 : integratorObserver_(
Teuchos::null),
52 integratorStatus_(
WORKING), isInitialized_(false)
58 template<
class Scalar>
60 Teuchos::RCP<Teuchos::ParameterList> inputPL,
62 : integratorObserver_(
Teuchos::null),
63 integratorStatus_(
WORKING), isInitialized_(false)
71 template<
class Scalar>
76 using Teuchos::ParameterList;
78 if (stepper_ == Teuchos::null) {
81 std::string stepperName = integratorPL_->get<std::string>(
"Stepper Name");
83 RCP<ParameterList> stepperPL = Teuchos::sublist(tempusPL_,stepperName,
true);
84 stepper_ = sf->createStepper(stepperPL, model);
86 stepper_->setModel(model);
91 template<
class Scalar>
96 using Teuchos::ParameterList;
100 std::string stepperName = integratorPL_->get<std::string>(
"Stepper Name");
102 RCP<ParameterList> stepperPL = Teuchos::sublist(tempusPL_,stepperName,
true);
103 stepper_ = sf->createStepper(stepperPL, models);
107 template<
class Scalar>
111 stepper_ = newStepper;
115 template<
class Scalar>
120 using Teuchos::ParameterList;
123 RCP<ParameterList> shPL =
124 Teuchos::sublist(integratorPL_,
"Solution History",
true);
125 solutionHistory_ = createSolutionHistoryPL<Scalar>(shPL);
127 if (state == Teuchos::null) {
130 stepper_->getModel(), stepper_->getDefaultStepperState());
133 newState->setTime (timeStepControl_->getInitTime());
134 newState->setIndex (timeStepControl_->getInitIndex());
135 newState->setTimeStep(timeStepControl_->getInitTimeStep());
136 newState->setTolRel (timeStepControl_->getMaxRelError());
137 newState->setTolAbs (timeStepControl_->getMaxAbsError());
138 newState->setOrder (stepper_->getOrder());
141 solutionHistory_->addState(newState);
145 solutionHistory_->addState(state);
149 stepper_->setInitialConditions(solutionHistory_);
153 template<
class Scalar>
161 using Teuchos::ParameterList;
164 RCP<ParameterList> shPL =
165 Teuchos::sublist(integratorPL_,
"Solution History",
true);
166 solutionHistory_ = createSolutionHistoryPL<Scalar>(shPL);
169 RCP<Thyra::VectorBase<Scalar> > xdot = x0->clone_v();
170 RCP<Thyra::VectorBase<Scalar> > xdotdot = x0->clone_v();
171 if (xdot0 == Teuchos::null)
172 Thyra::assign(xdot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
174 Thyra::assign(xdot.ptr(), *(xdot0));
175 if (xdotdot0 == Teuchos::null)
176 Thyra::assign(xdotdot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
178 Thyra::assign(xdotdot.ptr(), *(xdotdot0));
181 x0->clone_v(), xdot, xdotdot);
182 newState->setStepperState(stepper_->getDefaultStepperState());
185 newState->setTime (t0);
186 newState->setIndex (timeStepControl_->getInitIndex());
187 newState->setTimeStep(timeStepControl_->getInitTimeStep());
188 newState->setOrder(stepper_->getOrder());
192 solutionHistory_->addState(newState);
195 stepper_->setInitialConditions(solutionHistory_);
199 template<
class Scalar>
204 using Teuchos::ParameterList;
206 if (sh == Teuchos::null) {
208 if (solutionHistory_ == Teuchos::null) initializeSolutionHistory();
211 TEUCHOS_TEST_FOR_EXCEPTION( sh->getNumStates() < 1,
213 "Error - setSolutionHistory requires at least one SolutionState.\n" 214 <<
" Supplied SolutionHistory has only " << sh->getNumStates()
215 <<
" SolutionStates.\n");
217 solutionHistory_ = sh;
222 template<
class Scalar>
227 using Teuchos::ParameterList;
229 if (tsc == Teuchos::null) {
231 if (timeStepControl_ == Teuchos::null) {
232 if (integratorPL_->isSublist(
"Time Step Control")) {
234 RCP<ParameterList> tscPL =
235 Teuchos::sublist(integratorPL_,
"Time Step Control",
true);
236 timeStepControl_ = createTimeStepControl<Scalar>(tscPL);
240 RCP<const ParameterList> tscPL = timeStepControl_->getValidParameters();
241 integratorPL_->set(
"Time Step Control", tscPL->name());
242 integratorPL_->set(tscPL->name(), *tscPL);
248 timeStepControl_ = tsc;
249 RCP<const ParameterList> tscPL = timeStepControl_->getValidParameters();
250 integratorPL_->set(tscPL->name(), *tscPL);
253 timeStepControl_->initialize();
257 template<
class Scalar>
262 if (obs == Teuchos::null) {
264 if (integratorObserver_ == Teuchos::null) {
265 integratorObserver_ =
268 Teuchos::RCP<IntegratorObserverBasic<Scalar> > basicObs =
270 integratorObserver_->addObserver(basicObs);
273 if (integratorObserver_ == Teuchos::null) {
274 integratorObserver_ =
277 integratorObserver_->addObserver(obs);
283 template<
class Scalar>
286 TEUCHOS_TEST_FOR_EXCEPTION( stepper_ == Teuchos::null, std::logic_error,
287 "Error - Need to set the Stepper, setStepper(), before calling " 288 "IntegratorBasicOld::initialize()\n");
290 this->setTimeStepControl();
291 this->parseScreenOutput();
292 this->setSolutionHistory();
296 stepper_->setInitialConditions(solutionHistory_);
298 if (integratorTimer_ == Teuchos::null)
299 integratorTimer_ = rcp(
new Teuchos::Time(
"Integrator Timer"));
300 if (stepperTimer_ == Teuchos::null)
301 stepperTimer_ = rcp(
new Teuchos::Time(
"Stepper Timer"));
303 if (Teuchos::as<int>(this->getVerbLevel()) >=
304 Teuchos::as<int>(Teuchos::VERB_HIGH)) {
305 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
306 Teuchos::OSTab ostab(out,1,
"IntegratorBasicOld::IntegratorBasicOld");
307 *out << this->description() << std::endl;
310 isInitialized_ =
true;
314 template<
class Scalar>
317 std::string name =
"Tempus::IntegratorBasicOld";
322 template<
class Scalar>
324 Teuchos::FancyOStream &out,
325 const Teuchos::EVerbosityLevel verbLevel)
const 327 out << description() <<
"::describe" << std::endl;
328 out <<
"solutionHistory= " << solutionHistory_->description()<<std::endl;
329 out <<
"timeStepControl= " << timeStepControl_->description()<<std::endl;
330 out <<
"stepper = " << stepper_ ->description()<<std::endl;
332 if (Teuchos::as<int>(verbLevel) >=
333 Teuchos::as<int>(Teuchos::VERB_HIGH)) {
334 out <<
"solutionHistory= " << std::endl;
335 solutionHistory_->describe(out,verbLevel);
336 out <<
"timeStepControl= " << std::endl;
337 timeStepControl_->describe(out,verbLevel);
338 out <<
"stepper = " << std::endl;
339 stepper_ ->describe(out,verbLevel);
344 template <
class Scalar>
347 if (timeStepControl_->timeInRange(timeFinal))
348 timeStepControl_->setFinalTime(timeFinal);
349 bool itgrStatus = advanceTime();
354 template <
class Scalar>
357 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
358 if (isInitialized_ ==
false) {
359 Teuchos::OSTab ostab(out,1,
"StartIntegrator");
360 *out <<
"Failure - IntegratorBasicOld is not initialized." << std::endl;
366 auto cs = solutionHistory_->getCurrentState();
367 cs->setTolRel(timeStepControl_->getMaxRelError());
368 cs->setTolAbs(timeStepControl_->getMaxAbsError());
370 integratorTimer_->start();
372 const Scalar initDt =
373 std::min(timeStepControl_->getInitTimeStep(),
374 stepper_->getInitTimeStep(solutionHistory_));
376 timeStepControl_->setInitTimeStep(initDt);
377 timeStepControl_->initialize();
382 template <
class Scalar>
385 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::IntegratorBasicOld::advanceTime()");
388 integratorObserver_->observeStartIntegrator(*
this);
390 while (integratorStatus_ ==
WORKING &&
391 timeStepControl_->timeInRange (solutionHistory_->getCurrentTime()) &&
392 timeStepControl_->indexInRange(solutionHistory_->getCurrentIndex())){
394 stepperTimer_->reset();
395 stepperTimer_->start();
396 solutionHistory_->initWorkingState();
399 integratorObserver_->observeStartTimeStep(*
this);
401 timeStepControl_->setNextTimeStep(solutionHistory_, integratorStatus_);
402 integratorObserver_->observeNextTimeStep(*
this);
405 solutionHistory_->getWorkingState()->setSolutionStatus(
WORKING);
407 integratorObserver_->observeBeforeTakeStep(*
this);
409 stepper_->takeStep(solutionHistory_);
411 integratorObserver_->observeAfterTakeStep(*
this);
413 stepperTimer_->stop();
415 integratorObserver_->observeAfterCheckTimeStep(*
this);
417 solutionHistory_->promoteWorkingState();
418 integratorObserver_->observeEndTimeStep(*
this);
422 integratorObserver_->observeEndIntegrator(*
this);
429 template <
class Scalar>
432 auto ws = solutionHistory_->getWorkingState();
435 ws->setTolRel(timeStepControl_->getMaxRelError());
436 ws->setTolAbs(timeStepControl_->getMaxAbsError());
439 std::vector<int>::const_iterator it =
440 std::find(outputScreenIndices_.begin(),
441 outputScreenIndices_.end(),
443 if (it == outputScreenIndices_.end())
444 ws->setOutputScreen(
false);
446 ws->setOutputScreen(
true);
448 const int initial = timeStepControl_->getInitIndex();
449 const int interval = integratorPL_->get<
int>(
"Screen Output Index Interval");
450 if ( (ws->getIndex() - initial) % interval == 0)
451 ws->setOutputScreen(
true);
455 template <
class Scalar>
459 auto ws = solutionHistory_->getWorkingState();
462 if (ws->getNFailures() >= timeStepControl_->getMaxFailures()) {
463 RCP<Teuchos::FancyOStream> out = this->getOStream();
464 Teuchos::OSTab ostab(out,2,
"checkTimeStep");
465 *out <<
"Failure - Stepper has failed more than the maximum allowed.\n" 466 <<
" (nFailures = "<<ws->getNFailures()<<
") >= (nFailuresMax = " 467 << timeStepControl_->getMaxFailures()<<
")" << std::endl;
471 if (ws->getNConsecutiveFailures()
472 >= timeStepControl_->getMaxConsecFailures()){
473 RCP<Teuchos::FancyOStream> out = this->getOStream();
474 Teuchos::OSTab ostab(out,1,
"checkTimeStep");
475 *out <<
"Failure - Stepper has failed more than the maximum " 476 <<
"consecutive allowed.\n" 477 <<
" (nConsecutiveFailures = "<<ws->getNConsecutiveFailures()
478 <<
") >= (nConsecutiveFailuresMax = " 479 << timeStepControl_->getMaxConsecFailures()
488 ((timeStepControl_->getStepType() ==
"Constant") &&
489 (ws->getTimeStep() != timeStepControl_->getInitTimeStep()) &&
490 (ws->getOutput() !=
true) &&
491 (ws->getTime() != timeStepControl_->getFinalTime())
495 RCP<Teuchos::FancyOStream> out = this->getOStream();
496 Teuchos::OSTab ostab(out,0,
"checkTimeStep");
497 *out <<std::scientific
498 <<std::setw( 6)<<std::setprecision(3)<<ws->getIndex()
499 <<std::setw(11)<<std::setprecision(3)<<ws->getTime()
500 <<std::setw(11)<<std::setprecision(3)<<ws->getTimeStep()
501 <<
" STEP FAILURE!! - ";
503 *out <<
"Solution Status = " <<
toString(ws->getSolutionStatus())
505 }
else if ((timeStepControl_->getStepType() ==
"Constant") &&
506 (ws->getTimeStep() != timeStepControl_->getInitTimeStep())) {
507 *out <<
"dt != Constant dt (="<<timeStepControl_->getInitTimeStep()<<
")" 511 ws->setNFailures(ws->getNFailures()+1);
512 ws->setNRunningFailures(ws->getNRunningFailures()+1);
513 ws->setNConsecutiveFailures(ws->getNConsecutiveFailures()+1);
524 template <
class Scalar>
527 std::string exitStatus;
528 if (solutionHistory_->getCurrentState()->getSolutionStatus() ==
530 exitStatus =
"Time integration FAILURE!";
533 exitStatus =
"Time integration complete.";
536 integratorTimer_->stop();
537 runtime_ = integratorTimer_->totalElapsedTime();
541 template <
class Scalar>
547 outputScreenIndices_.clear();
549 integratorPL_->get<std::string>(
"Screen Output Index List",
"");
550 std::string delimiters(
",");
551 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
552 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
553 while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
554 std::string token = str.substr(lastPos,pos-lastPos);
555 outputScreenIndices_.push_back(
int(std::stoi(token)));
556 if(pos==std::string::npos)
559 lastPos = str.find_first_not_of(delimiters, pos);
560 pos = str.find_first_of(delimiters, lastPos);
564 std::sort(outputScreenIndices_.begin(),outputScreenIndices_.end());
565 outputScreenIndices_.erase(std::unique(outputScreenIndices_.begin(),
566 outputScreenIndices_.end() ),
567 outputScreenIndices_.end() );
572 template <
class Scalar>
574 const Teuchos::RCP<Teuchos::ParameterList> & inputPL)
576 if (inputPL == Teuchos::null) {
577 if (tempusPL_->isParameter(
"Integrator Name")) {
579 std::string integratorName_ =
580 tempusPL_->get<std::string>(
"Integrator Name");
581 integratorPL_ = Teuchos::sublist(tempusPL_, integratorName_,
true);
584 integratorPL_ = Teuchos::parameterList();
585 integratorPL_->setName(
"Default Integrator");
586 *integratorPL_ = *(this->getValidParameters());
587 tempusPL_->set(
"Integrator Name",
"Default Integrator");
588 tempusPL_->set(
"Default Integrator", *integratorPL_);
591 *integratorPL_ = *inputPL;
592 tempusPL_->set(
"Integrator Name", integratorPL_->name());
593 tempusPL_->set(integratorPL_->name(), *integratorPL_);
596 integratorPL_->validateParametersAndSetDefaults(*this->getValidParameters());
598 std::string integratorType =
599 integratorPL_->get<std::string>(
"Integrator Type");
600 TEUCHOS_TEST_FOR_EXCEPTION( integratorType !=
"Integrator Basic",
602 "Error - Inconsistent Integrator Type for IntegratorBasicOld\n" 603 <<
" Integrator Type = " << integratorType <<
"\n");
611 template<
class Scalar>
612 Teuchos::RCP<const Teuchos::ParameterList>
615 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
617 std::ostringstream tmp;
618 tmp <<
"'Integrator Type' must be 'Integrator Basic'.";
619 pl->set(
"Integrator Type",
"Integrator Basic", tmp.str());
622 tmp <<
"Screen Output Index List. Required to be in TimeStepControl range " 623 <<
"['Minimum Time Step Index', 'Maximum Time Step Index']";
624 pl->set(
"Screen Output Index List",
"", tmp.str());
625 pl->set(
"Screen Output Index Interval", 1000000,
626 "Screen Output Index Interval (e.g., every 100 time steps)");
628 pl->set(
"Stepper Name",
"",
629 "'Stepper Name' selects the Stepper block to construct (Required).");
632 pl->sublist(
"Solution History",
false,
"solutionHistory_docs")
633 .disableRecursiveValidation();
636 pl->sublist(
"Time Step Control",
false,
"solutionHistory_docs")
637 .disableRecursiveValidation();
643 template <
class Scalar>
644 Teuchos::RCP<Teuchos::ParameterList>
647 return(integratorPL_);
651 template <
class Scalar>
652 Teuchos::RCP<Teuchos::ParameterList>
655 Teuchos::RCP<Teuchos::ParameterList> temp_param_list = integratorPL_;
656 integratorPL_ = Teuchos::null;
657 return(temp_param_list);
661 template<
class Scalar>
663 Teuchos::RCP<Teuchos::ParameterList> pList,
666 Teuchos::RCP<IntegratorBasicOld<Scalar> > integrator =
672 template<
class Scalar>
675 std::string stepperType)
677 Teuchos::RCP<IntegratorBasicOld<Scalar> > integrator =
683 template<
class Scalar>
686 Teuchos::RCP<IntegratorBasicOld<Scalar> > integrator =
692 template<
class Scalar>
694 Teuchos::RCP<Teuchos::ParameterList> pList,
697 Teuchos::RCP<IntegratorBasicOld<Scalar> > integrator =
703 #endif // Tempus_IntegratorBasicOld_impl_hpp 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.
IntegratorBasicOld()
Constructor that requires a subsequent setParameterList, setStepper, and initialize calls...
void parseScreenOutput()
Parse when screen output should be executed.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList() override
const std::string toString(const Status status)
Convert Status to string.
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList() override
IntegratorObserverBasic class for time integrators. This basic class has simple no-op functions...
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void initializeSolutionHistory(Teuchos::RCP< SolutionState< Scalar > > state=Teuchos::null)
Set the initial state which has the initial conditions.
Thyra Base interface for time steppers.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Create valid IntegratorBasicOld ParameterList.
virtual void setObserver(Teuchos::RCP< IntegratorObserver< Scalar > > obs=Teuchos::null)
Set the Observer.
virtual void checkTimeStep()
Check if time step has passed or failed.
virtual void setStepperWStepper(Teuchos::RCP< Stepper< Scalar > > stepper)
Set the Stepper.
IntegratorObserver class for time integrators.
std::string description() const override
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void setTimeStepControl(Teuchos::RCP< TimeStepControl< Scalar > > tsc=Teuchos::null)
Set the TimeStepControl.
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 setTempusParameterList(Teuchos::RCP< Teuchos::ParameterList > pl) override
virtual void initialize()
Initializes the Integrator after set* function calls.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl) override
virtual void startTimeStep()
Start time step.
virtual void setSolutionHistory(Teuchos::RCP< SolutionHistory< Scalar > > sh=Teuchos::null)
Set the SolutionHistory.
This observer is a composite observer,.
virtual void startIntegrator()
Perform tasks before start of integrator.
virtual void setStepper(Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > model)
Set the Stepper.
virtual void endIntegrator()
Perform tasks after end of integrator.
Teuchos::RCP< IntegratorBasicOld< Scalar > > integratorBasic(Teuchos::RCP< Teuchos::ParameterList > pList, const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &model)
Nonmember constructor.
virtual bool advanceTime()
Advance the solution to timeMax, and return true if successful.
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...