10#ifndef Tempus_IntegratorBasic_impl_hpp
11#define Tempus_IntegratorBasic_impl_hpp
13#include "Thyra_VectorStdOps.hpp"
16#include "Tempus_StepperFactory.hpp"
17#include "Tempus_StepperForwardEuler.hpp"
21template <
class Scalar>
43template <
class Scalar>
49 std::vector<int> outputScreenIndices,
int outputScreenInterval)
68template <
class Scalar>
84template <
class Scalar>
87 TEUCHOS_TEST_FOR_EXCEPTION(
88 i !=
"Integrator Basic", std::logic_error,
89 "Error - Integrator Type should be 'Integrator Basic'\n");
94template <
class Scalar>
98 TEUCHOS_TEST_FOR_EXCEPTION(
99 stepper_ == Teuchos::null, std::logic_error,
100 "Error - setModel(), need to set stepper first!\n");
105template <
class Scalar>
108 if (stepper == Teuchos::null)
115template <
class Scalar>
128 TEUCHOS_TEST_FOR_EXCEPTION(
129 stepper_ == Teuchos::null, std::logic_error,
130 "Error - initializeSolutionHistory(), need to set stepper first!\n");
132 if (state == Teuchos::null) {
133 TEUCHOS_TEST_FOR_EXCEPTION(
stepper_->getModel() == Teuchos::null,
135 "Error - initializeSolutionHistory(), need to "
136 "set stepper's model first!\n");
139 stepper_->getDefaultStepperState());
149 state->setOrder(
stepper_->getOrder());
158template <
class Scalar>
167 RCP<Thyra::VectorBase<Scalar> > xdot = x0->clone_v();
168 RCP<Thyra::VectorBase<Scalar> > xdotdot = x0->clone_v();
169 if (xdot0 == Teuchos::null)
170 Thyra::assign(xdot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
172 Thyra::assign(xdot.ptr(), *(xdot0));
173 if (xdotdot0 == Teuchos::null)
174 Thyra::assign(xdotdot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
176 Thyra::assign(xdotdot.ptr(), *(xdotdot0));
178 TEUCHOS_TEST_FOR_EXCEPTION(
179 stepper_ == Teuchos::null, std::logic_error,
180 "Error - initializeSolutionHistory(), need to set stepper first!\n");
183 state->setStepperState(
stepper_->getDefaultStepperState());
193 state->setOrder(
stepper_->getOrder());
199template <
class Scalar>
203 if (sh == Teuchos::null) {
213template <
class Scalar>
217 if (tsc == Teuchos::null) {
229template <
class Scalar>
233 if (obs == Teuchos::null)
239template <
class Scalar>
242 TEUCHOS_TEST_FOR_EXCEPTION(
243 stepper_ == Teuchos::null, std::logic_error,
244 "Error - Need to set the Stepper, setStepper(), before calling "
245 "IntegratorBasic::initialize()\n");
247 TEUCHOS_TEST_FOR_EXCEPTION(
249 "Error - SolutionHistory requires at least one SolutionState.\n"
250 <<
" Supplied SolutionHistory has only "
260template <
class Scalar>
263 std::string name =
"Tempus::IntegratorBasic";
267template <
class Scalar>
269 Teuchos::FancyOStream& out,
const Teuchos::EVerbosityLevel verbLevel)
const
271 auto l_out = Teuchos::fancyOStream(out.getOStream());
272 Teuchos::OSTab ostab(*l_out, 2, this->
description());
273 l_out->setOutputToRootOnly(0);
275 *l_out <<
"\n--- " << this->
description() <<
" ---" << std::endl;
295 *l_out <<
"stepper = " <<
stepper_ << std::endl;
297 *l_out << std::string(this->
description().length() + 8,
'-') << std::endl;
300template <
class Scalar>
309template <
class Scalar>
312 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
313 out->setOutputToRootOnly(0);
315 Teuchos::OSTab ostab(out, 1,
"StartIntegrator");
316 *out <<
"Failure - IntegratorBasic is not initialized." << std::endl;
336template <
class Scalar>
339 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::IntegratorBasic::advanceTime()");
382template <
class Scalar>
392 std::vector<int>::const_iterator it = std::find(
395 ws->setOutputScreen(
false);
397 ws->setOutputScreen(
true);
401 ws->setOutputScreen(
true);
404template <
class Scalar>
412 RCP<Teuchos::FancyOStream> out = this->getOStream();
413 out->setOutputToRootOnly(0);
414 Teuchos::OSTab ostab(out, 2,
"checkTimeStep");
415 *out <<
"Failure - Stepper has failed more than the maximum allowed.\n"
416 <<
" (nFailures = " << ws->getNFailures()
422 if (ws->getNConsecutiveFailures() >=
424 RCP<Teuchos::FancyOStream> out = this->getOStream();
425 out->setOutputToRootOnly(0);
426 Teuchos::OSTab ostab(out, 1,
"checkTimeStep");
427 *out <<
"Failure - Stepper has failed more than the maximum "
428 <<
"consecutive allowed.\n"
429 <<
" (nConsecutiveFailures = " << ws->getNConsecutiveFailures()
430 <<
") >= (nConsecutiveFailuresMax = "
439 RCP<Teuchos::FancyOStream> out = this->getOStream();
440 out->setOutputToRootOnly(0);
441 Teuchos::OSTab ostab(out, 1,
"checkTimeStep");
442 *out <<
"Failure - Stepper has failed and the time step size is "
443 <<
"at the minimum.\n"
444 <<
" Solution Status = " <<
toString(ws->getSolutionStatus())
446 <<
" (TimeStep = " << ws->getTimeStep()
458 RCP<Teuchos::FancyOStream> out = this->getOStream();
459 out->setOutputToRootOnly(0);
460 Teuchos::OSTab ostab(out, 0,
"checkTimeStep");
461 *out << std::scientific << std::setw(6) << std::setprecision(3)
462 << ws->getIndex() << std::setw(11) << std::setprecision(3)
463 << ws->getTime() << std::setw(11) << std::setprecision(3)
464 << ws->getTimeStep() <<
" STEP FAILURE!! - ";
466 *out <<
"Solution Status = " <<
toString(ws->getSolutionStatus())
475 ws->setNFailures(ws->getNFailures() + 1);
476 ws->setNRunningFailures(ws->getNRunningFailures() + 1);
477 ws->setNConsecutiveFailures(ws->getNConsecutiveFailures() + 1);
486template <
class Scalar>
489 std::string exitStatus;
493 exitStatus =
"Time integration FAILURE!";
497 exitStatus =
"Time integration complete.";
503template <
class Scalar>
507 std::string delimiters(
",");
508 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
509 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
510 while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
511 std::string token = str.substr(lastPos, pos - lastPos);
513 if (pos == std::string::npos)
break;
515 lastPos = str.find_first_not_of(delimiters, pos);
516 pos = str.find_first_of(delimiters, lastPos);
527template <
class Scalar>
530 std::stringstream ss;
532 if (i != 0) ss <<
", ";
540template <
class Scalar>
541Teuchos::RCP<const Teuchos::ParameterList>
544 Teuchos::RCP<Teuchos::ParameterList> pl =
548 "'Integrator Type' must be 'Integrator Basic'.");
551 "Screen Output Index List. Required to be in TimeStepControl range "
552 "['Minimum Time Step Index', 'Maximum Time Step Index']");
555 "Screen Output Index Interval (e.g., every 100 time steps)");
557 pl->set(
"Stepper Name",
stepper_->getStepperName(),
558 "'Stepper Name' selects the Stepper block to construct (Required).");
563 Teuchos::RCP<Teuchos::ParameterList> tempusPL =
564 Teuchos::parameterList(
"Tempus");
566 tempusPL->set(
"Integrator Name", pl->name());
567 tempusPL->set(pl->name(), *pl);
575template <
class Scalar>
577 Teuchos::RCP<Teuchos::ParameterList> tempusPL,
bool runInitialize)
580 if (tempusPL == Teuchos::null || tempusPL->numParams() == 0)
583 auto integratorName = tempusPL->get<std::string>(
"Integrator Name");
584 auto integratorPL = Teuchos::sublist(tempusPL, integratorName,
true);
586 std::string integratorType =
587 integratorPL->get<std::string>(
"Integrator Type");
588 TEUCHOS_TEST_FOR_EXCEPTION(
589 integratorType !=
"Integrator Basic", std::logic_error,
590 "Error - For IntegratorBasic, 'Integrator Type' should be "
591 <<
"'Integrator Basic'.\n"
592 <<
" Integrator Type = " << integratorType <<
"\n");
594 integrator->setIntegratorName(integratorName);
597 auto validPL = Teuchos::rcp_const_cast<Teuchos::ParameterList>(
598 integrator->getValidParameters());
599 auto vIntegratorName = validPL->template get<std::string>(
"Integrator Name");
600 auto vIntegratorPL = Teuchos::sublist(validPL, vIntegratorName,
true);
601 integratorPL->validateParametersAndSetDefaults(*vIntegratorPL, 1);
604 if (integratorPL->isParameter(
"Stepper Name")) {
606 auto stepperName = integratorPL->get<std::string>(
"Stepper Name");
607 auto stepperPL = Teuchos::sublist(tempusPL, stepperName,
true);
608 stepperPL->setName(stepperName);
610 integrator->setStepper(sf->createStepper(stepperPL));
615 integrator->setStepper(stepper);
619 if (integratorPL->isSublist(
"Time Step Control")) {
621 auto tscPL = Teuchos::sublist(integratorPL,
"Time Step Control",
true);
622 integrator->setTimeStepControl(
631 if (integratorPL->isSublist(
"Solution History")) {
633 auto shPL = Teuchos::sublist(integratorPL,
"Solution History",
true);
635 integrator->setSolutionHistory(sh);
643 integrator->setObserver(Teuchos::null);
646 integrator->setScreenOutputIndexInterval(
647 integratorPL->get<
int>(
"Screen Output Index Interval",
648 integrator->getScreenOutputIndexInterval()));
651 auto str = integratorPL->get<std::string>(
"Screen Output Index List",
"");
652 integrator->setScreenOutputIndexList(str);
659template <
class Scalar>
661 Teuchos::RCP<Teuchos::ParameterList> tempusPL,
666 if (model == Teuchos::null)
return integrator;
668 Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> > constModel = model;
669 integrator->setModel(constModel);
674 integrator->getStepper()->getDefaultStepperState());
675 newState->setTime(integrator->getTimeStepControl()->getInitTime());
676 newState->setIndex(integrator->getTimeStepControl()->getInitIndex());
677 newState->setTimeStep(integrator->getTimeStepControl()->getInitTimeStep());
678 newState->setTolRel(integrator->getTimeStepControl()->getMaxRelError());
679 newState->setTolAbs(integrator->getTimeStepControl()->getMaxAbsError());
680 newState->setOrder(integrator->getStepper()->getOrder());
684 auto sh = integrator->getNonConstSolutionHistory();
685 sh->addState(newState);
686 integrator->getStepper()->setInitialConditions(sh);
688 if (runInitialize) integrator->initialize();
694template <
class Scalar>
697 std::string stepperType)
702 auto stepper = sf->createStepper(stepperType, model);
703 integrator->setStepper(stepper);
704 integrator->initializeSolutionHistory();
705 integrator->initialize();
711template <
class Scalar>
718template <
class Scalar>
720 Teuchos::RCP<Teuchos::ParameterList> tempusPL,
724 auto integratorName = tempusPL->get<std::string>(
"Integrator Name");
725 auto integratorPL = Teuchos::sublist(tempusPL, integratorName,
true);
727 std::string integratorType =
728 integratorPL->get<std::string>(
"Integrator Type");
729 TEUCHOS_TEST_FOR_EXCEPTION(
730 integratorType !=
"Integrator Basic", std::logic_error,
731 "Error - For IntegratorBasic, 'Integrator Type' should be "
732 <<
"'Integrator Basic'.\n"
733 <<
" Integrator Type = " << integratorType <<
"\n");
736 integrator->setIntegratorName(integratorName);
738 TEUCHOS_TEST_FOR_EXCEPTION(
739 !integratorPL->isParameter(
"Stepper Name"), std::logic_error,
740 "Error - Need to set the 'Stepper Name' in 'Integrator Basic'.\n");
742 auto stepperName = integratorPL->get<std::string>(
"Stepper Name");
743 TEUCHOS_TEST_FOR_EXCEPTION(
744 stepperName ==
"Operator Split", std::logic_error,
745 "Error - 'Stepper Name' should be 'Operator Split'.\n");
748 auto stepperPL = Teuchos::sublist(tempusPL, stepperName,
true);
749 stepperPL->setName(stepperName);
751 integrator->setStepper(sf->createStepper(stepperPL, models));
754 if (integratorPL->isSublist(
"Time Step Control")) {
756 auto tscPL = Teuchos::sublist(integratorPL,
"Time Step Control",
true);
757 integrator->setTimeStepControl(
768 integrator->getStepper()->getDefaultStepperState());
769 newState->setTime(integrator->getTimeStepControl()->getInitTime());
770 newState->setIndex(integrator->getTimeStepControl()->getInitIndex());
771 newState->setTimeStep(integrator->getTimeStepControl()->getInitTimeStep());
772 newState->setTolRel(integrator->getTimeStepControl()->getMaxRelError());
773 newState->setTolAbs(integrator->getTimeStepControl()->getMaxAbsError());
774 newState->setOrder(integrator->getStepper()->getOrder());
778 auto shPL = Teuchos::sublist(integratorPL,
"Solution History",
true);
780 sh->addState(newState);
781 integrator->getStepper()->setInitialConditions(sh);
782 integrator->setSolutionHistory(sh);
785 integrator->setObserver(Teuchos::null);
788 integrator->setScreenOutputIndexInterval(
789 integratorPL->get<
int>(
"Screen Output Index Interval",
790 integrator->getScreenOutputIndexInterval()));
793 auto str = integratorPL->get<std::string>(
"Screen Output Index List",
"");
794 integrator->setScreenOutputIndexList(str);
796 auto validPL = Teuchos::rcp_const_cast<Teuchos::ParameterList>(
797 integrator->getValidParameters());
800 auto vIntegratorName = validPL->template get<std::string>(
"Integrator Name");
801 auto vIntegratorPL = Teuchos::sublist(validPL, vIntegratorName,
true);
802 integratorPL->validateParametersAndSetDefaults(*vIntegratorPL);
805 auto vStepperName = vIntegratorPL->template get<std::string>(
"Stepper Name");
806 auto vStepperPL = Teuchos::sublist(validPL, vStepperName,
true);
807 stepperPL->validateParametersAndSetDefaults(*vStepperPL);
809 integrator->initialize();
virtual void checkTimeStep()
Check if time step has passed or failed.
std::vector< int > outputScreenIndices_
Vector of screen output indices.
virtual void initializeSolutionHistory(Teuchos::RCP< SolutionState< Scalar > > state=Teuchos::null)
Set the initial state which has the initial conditions.
virtual void setSolutionHistory(Teuchos::RCP< SolutionHistory< Scalar > > sh=Teuchos::null)
Set the SolutionHistory.
int outputScreenInterval_
screen output interval.
virtual void startIntegrator()
Perform tasks before start of integrator.
std::string description() const override
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Create valid IntegratorBasic ParameterList.
void setIntegratorName(std::string i)
Set the Integrator Name.
Teuchos::RCP< IntegratorObserver< Scalar > > integratorObserver_
Teuchos::RCP< TimeStepControl< Scalar > > timeStepControl_
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory_
virtual void setScreenOutputIndexInterval(int i)
virtual void setStepper(Teuchos::RCP< Stepper< Scalar > > stepper)
Set the Stepper.
virtual void setTimeStepControl(Teuchos::RCP< TimeStepControl< Scalar > > tsc=Teuchos::null)
Set the TimeStepControl.
virtual void initialize()
Initializes the Integrator after set* function calls.
std::string getIntegratorType() const
Get the Integrator Type.
void setIntegratorType(std::string i)
Set the Integrator Type.
virtual void endIntegrator()
Perform tasks after end of integrator.
virtual void startTimeStep()
Start time step.
virtual void copy(Teuchos::RCP< IntegratorBasic< Scalar > > iB)
Copy (a shallow copy).
Teuchos::RCP< Teuchos::Time > integratorTimer_
std::string integratorType_
the integrator type.
virtual void setScreenOutputIndexList(std::vector< int > indices)
virtual int getScreenOutputIndexInterval() const
Teuchos::RCP< Stepper< Scalar > > stepper_
virtual void setStatus(const Status st) override
Set Status.
std::string getIntegratorName() const
Get the Integrator Name.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void setModel(Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > model)
Set the model on the stepper.
Teuchos::RCP< Teuchos::Time > stepperTimer_
virtual bool advanceTime()
Advance the solution to timeMax, and return true if successful.
virtual void setObserver(Teuchos::RCP< IntegratorObserver< Scalar > > obs=Teuchos::null)
Set the Observer.
virtual std::string getScreenOutputIndexListString() const
IntegratorObserverBasic class for time integrators. This basic class has simple no-op functions,...
IntegratorObserver class for time integrators.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Solution state for integrators and steppers.
Forward Euler time stepper.
Thyra Base interface for time steppers.
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
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.
bool approxEqual(Scalar a, Scalar b, Scalar relTol=numericalTol< Scalar >())
Test if values are approximately equal within the relative tolerance.
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.
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryPL(Teuchos::RCP< Teuchos::ParameterList > pList)
Nonmember constructor from a ParameterList.
Teuchos::RCP< TimeStepControl< Scalar > > createTimeStepControl(Teuchos::RCP< Teuchos::ParameterList > const &pList, bool runInitialize=true)
Nonmember constructor from ParameterList.
const std::string toString(const Status status)
Convert Status to string.
Teuchos::RCP< IntegratorBasic< Scalar > > createIntegratorBasic()
Nonmember constructor.
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistory()
Nonmember constructor.