9#ifndef Tempus_IntegratorBasic_impl_hpp
10#define Tempus_IntegratorBasic_impl_hpp
12#include "Thyra_VectorStdOps.hpp"
15#include "Tempus_StepperFactory.hpp"
16#include "Tempus_StepperForwardEuler.hpp"
50 std::vector<int> outputScreenIndices,
51 int outputScreenInterval)
91 TEUCHOS_TEST_FOR_EXCEPTION( i !=
"Integrator Basic", std::logic_error,
92 "Error - Integrator Type should be 'Integrator Basic'\n");
102 TEUCHOS_TEST_FOR_EXCEPTION(
stepper_ == Teuchos::null, std::logic_error,
103 "Error - setModel(), need to set stepper first!\n");
109template<
class Scalar>
113 if (stepper == Teuchos::null)
120template<
class Scalar>
132 TEUCHOS_TEST_FOR_EXCEPTION(
stepper_ == Teuchos::null, std::logic_error,
133 "Error - initializeSolutionHistory(), need to set stepper first!\n");
135 if (state == Teuchos::null) {
136 TEUCHOS_TEST_FOR_EXCEPTION(
stepper_->getModel() == Teuchos::null,
138 "Error - initializeSolutionHistory(), need to set stepper's model first!\n");
141 stepper_->getDefaultStepperState());
151 state->setOrder (
stepper_->getOrder());
161template<
class Scalar>
171 RCP<Thyra::VectorBase<Scalar> > xdot = x0->clone_v();
172 RCP<Thyra::VectorBase<Scalar> > xdotdot = x0->clone_v();
173 if (xdot0 == Teuchos::null)
174 Thyra::assign(xdot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
176 Thyra::assign(xdot.ptr(), *(xdot0));
177 if (xdotdot0 == Teuchos::null)
178 Thyra::assign(xdotdot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
180 Thyra::assign(xdotdot.ptr(), *(xdotdot0));
182 TEUCHOS_TEST_FOR_EXCEPTION(
stepper_ == Teuchos::null, std::logic_error,
183 "Error - initializeSolutionHistory(), need to set stepper first!\n");
186 state->setStepperState(
stepper_->getDefaultStepperState());
196 state->setOrder (
stepper_->getOrder());
203template<
class Scalar>
207 if (sh == Teuchos::null) {
217template<
class Scalar>
221 if (tsc == Teuchos::null) {
233template<
class Scalar>
237 if (obs == Teuchos::null)
244template<
class Scalar>
247 TEUCHOS_TEST_FOR_EXCEPTION(
stepper_ == Teuchos::null, std::logic_error,
248 "Error - Need to set the Stepper, setStepper(), before calling "
249 "IntegratorBasic::initialize()\n");
253 "Error - SolutionHistory requires at least one SolutionState.\n"
254 <<
" Supplied SolutionHistory has only "
265template<
class Scalar>
268 std::string name =
"Tempus::IntegratorBasic";
273template<
class Scalar>
275 Teuchos::FancyOStream &out,
276 const Teuchos::EVerbosityLevel verbLevel)
const
278 auto l_out = Teuchos::fancyOStream( out.getOStream() );
279 Teuchos::OSTab ostab(*l_out, 2, this->
description());
280 l_out->setOutputToRootOnly(0);
282 *l_out <<
"\n--- " << this->
description() <<
" ---" << std::endl;
299 *l_out <<
"stepper = " <<
stepper_ << std::endl;
301 *l_out << std::string(this->
description().length()+8,
'-') <<std::endl;
305template <
class Scalar>
315template <
class Scalar>
318 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
319 out->setOutputToRootOnly(0);
321 Teuchos::OSTab ostab(out,1,
"StartIntegrator");
322 *out <<
"Failure - IntegratorBasic is not initialized." << std::endl;
334 const Scalar initDt =
344template <
class Scalar>
347 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::IntegratorBasic::advanceTime()");
391template <
class Scalar>
401 std::vector<int>::const_iterator it =
406 ws->setOutputScreen(
false);
408 ws->setOutputScreen(
true);
412 ws->setOutputScreen(
true);
416template <
class Scalar>
424 RCP<Teuchos::FancyOStream> out = this->getOStream();
425 out->setOutputToRootOnly(0);
426 Teuchos::OSTab ostab(out, 2,
"checkTimeStep");
427 *out <<
"Failure - Stepper has failed more than the maximum allowed.\n"
428 <<
" (nFailures = "<<ws->getNFailures()<<
") >= (nFailuresMax = "
433 if (ws->getNConsecutiveFailures()
435 RCP<Teuchos::FancyOStream> out = this->getOStream();
436 out->setOutputToRootOnly(0);
437 Teuchos::OSTab ostab(out, 1,
"checkTimeStep");
438 *out <<
"Failure - Stepper has failed more than the maximum "
439 <<
"consecutive allowed.\n"
440 <<
" (nConsecutiveFailures = "<<ws->getNConsecutiveFailures()
441 <<
") >= (nConsecutiveFailuresMax = "
451 RCP<Teuchos::FancyOStream> out = this->getOStream();
452 out->setOutputToRootOnly(0);
453 Teuchos::OSTab ostab(out, 1,
"checkTimeStep");
454 *out <<
"Failure - Stepper has failed and the time step size is "
455 <<
"at the minimum.\n"
456 <<
" Solution Status = " <<
toString(ws->getSolutionStatus())
458 <<
" (TimeStep = " << ws->getTimeStep()
459 <<
") <= (Minimum TimeStep = "
473 RCP<Teuchos::FancyOStream> out = this->getOStream();
474 out->setOutputToRootOnly(0);
475 Teuchos::OSTab ostab(out, 0,
"checkTimeStep");
476 *out <<std::scientific
477 <<std::setw( 6)<<std::setprecision(3)<<ws->getIndex()
478 <<std::setw(11)<<std::setprecision(3)<<ws->getTime()
479 <<std::setw(11)<<std::setprecision(3)<<ws->getTimeStep()
480 <<
" STEP FAILURE!! - ";
482 *out <<
"Solution Status = " <<
toString(ws->getSolutionStatus())
490 ws->setNFailures(ws->getNFailures()+1);
491 ws->setNRunningFailures(ws->getNRunningFailures()+1);
492 ws->setNConsecutiveFailures(ws->getNConsecutiveFailures()+1);
503template <
class Scalar>
506 std::string exitStatus;
509 exitStatus =
"Time integration FAILURE!";
512 exitStatus =
"Time integration complete.";
519template <
class Scalar>
523 std::string delimiters(
",");
524 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
525 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
526 while ((pos != std::string::npos) || (lastPos != std::string::npos)) {
527 std::string token = str.substr(lastPos,pos-lastPos);
529 if(pos==std::string::npos)
532 lastPos = str.find_first_not_of(delimiters, pos);
533 pos = str.find_first_of(delimiters, lastPos);
545template <
class Scalar>
548 std::stringstream ss;
550 if(i != 0) ss <<
", ";
559template<
class Scalar>
560Teuchos::RCP<const Teuchos::ParameterList>
563 Teuchos::RCP<Teuchos::ParameterList> pl =
567 "'Integrator Type' must be 'Integrator Basic'.");
570 "Screen Output Index List. Required to be in TimeStepControl range "
571 "['Minimum Time Step Index', 'Maximum Time Step Index']");
574 "Screen Output Index Interval (e.g., every 100 time steps)");
576 pl->set(
"Stepper Name",
stepper_->getStepperName(),
577 "'Stepper Name' selects the Stepper block to construct (Required).");
583 Teuchos::RCP<Teuchos::ParameterList> tempusPL =
584 Teuchos::parameterList(
"Tempus");
586 tempusPL->set(
"Integrator Name", pl->name());
587 tempusPL->set(pl->name(), *pl);
596template<
class Scalar>
598 Teuchos::RCP<Teuchos::ParameterList> tempusPL,
bool runInitialize)
601 if (tempusPL == Teuchos::null || tempusPL->numParams() == 0)
604 auto integratorName = tempusPL->get<std::string>(
"Integrator Name");
605 auto integratorPL = Teuchos::sublist(tempusPL, integratorName,
true);
607 std::string integratorType = integratorPL->get<std::string>(
"Integrator Type");
608 TEUCHOS_TEST_FOR_EXCEPTION( integratorType !=
"Integrator Basic",
610 "Error - For IntegratorBasic, 'Integrator Type' should be "
611 <<
"'Integrator Basic'.\n"
612 <<
" Integrator Type = " << integratorType <<
"\n");
614 integrator->setIntegratorName(integratorName);
618 Teuchos::rcp_const_cast<Teuchos::ParameterList>(integrator->getValidParameters());
619 auto vIntegratorName = validPL->template get<std::string>(
"Integrator Name");
620 auto vIntegratorPL = Teuchos::sublist(validPL, vIntegratorName,
true);
621 integratorPL->validateParametersAndSetDefaults(*vIntegratorPL,1);
624 if (integratorPL->isParameter(
"Stepper Name")) {
626 auto stepperName = integratorPL->get<std::string>(
"Stepper Name");
627 auto stepperPL = Teuchos::sublist(tempusPL, stepperName,
true);
628 stepperPL->setName(stepperName);
630 integrator->setStepper(sf->createStepper(stepperPL));
634 integrator->setStepper(stepper);
638 if (integratorPL->isSublist(
"Time Step Control")) {
640 auto tscPL = Teuchos::sublist(integratorPL,
"Time Step Control",
true);
648 if (integratorPL->isSublist(
"Solution History")) {
650 auto shPL = Teuchos::sublist(integratorPL,
"Solution History",
true);
652 integrator->setSolutionHistory(sh);
659 integrator->setObserver(Teuchos::null);
662 integrator->setScreenOutputIndexInterval(
663 integratorPL->get<
int>(
"Screen Output Index Interval",
664 integrator->getScreenOutputIndexInterval()));
667 auto str = integratorPL->get<std::string>(
"Screen Output Index List",
"");
668 integrator->setScreenOutputIndexList(str);
676template<
class Scalar>
678 Teuchos::RCP<Teuchos::ParameterList> tempusPL,
683 if ( model == Teuchos::null )
return integrator;
685 Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> > constModel = model;
686 integrator->setModel(constModel);
690 integrator->getStepper()->getDefaultStepperState());
691 newState->setTime (integrator->getTimeStepControl()->getInitTime());
692 newState->setIndex (integrator->getTimeStepControl()->getInitIndex());
693 newState->setTimeStep(integrator->getTimeStepControl()->getInitTimeStep());
694 newState->setTolRel (integrator->getTimeStepControl()->getMaxRelError());
695 newState->setTolAbs (integrator->getTimeStepControl()->getMaxAbsError());
696 newState->setOrder (integrator->getStepper()->getOrder());
700 auto sh = integrator->getNonConstSolutionHistory();
701 sh->addState(newState);
702 integrator->getStepper()->setInitialConditions(sh);
704 if (runInitialize) integrator->initialize();
711template<
class Scalar>
714 std::string stepperType)
719 auto stepper = sf->createStepper(stepperType, model);
720 integrator->setStepper(stepper);
721 integrator->initializeSolutionHistory();
722 integrator->initialize();
729template<
class Scalar>
737template<
class Scalar>
739 Teuchos::RCP<Teuchos::ParameterList> tempusPL,
743 auto integratorName = tempusPL->get<std::string>(
"Integrator Name");
744 auto integratorPL = Teuchos::sublist(tempusPL, integratorName,
true);
746 std::string integratorType = integratorPL->get<std::string>(
"Integrator Type");
747 TEUCHOS_TEST_FOR_EXCEPTION( integratorType !=
"Integrator Basic",
749 "Error - For IntegratorBasic, 'Integrator Type' should be "
750 <<
"'Integrator Basic'.\n"
751 <<
" Integrator Type = " << integratorType <<
"\n");
754 integrator->setIntegratorName(integratorName);
756 TEUCHOS_TEST_FOR_EXCEPTION( !integratorPL->isParameter(
"Stepper Name"),
758 "Error - Need to set the 'Stepper Name' in 'Integrator Basic'.\n");
760 auto stepperName = integratorPL->get<std::string>(
"Stepper Name");
761 TEUCHOS_TEST_FOR_EXCEPTION( stepperName ==
"Operator Split",
763 "Error - 'Stepper Name' should be 'Operator Split'.\n");
766 auto stepperPL = Teuchos::sublist(tempusPL, stepperName,
true);
767 stepperPL->setName(stepperName);
769 integrator->setStepper(sf->createStepper(stepperPL, models));
772 if (integratorPL->isSublist(
"Time Step Control")) {
774 auto tscPL = Teuchos::sublist(integratorPL,
"Time Step Control",
true);
783 integrator->getStepper()->getDefaultStepperState());
784 newState->setTime (integrator->getTimeStepControl()->getInitTime());
785 newState->setIndex (integrator->getTimeStepControl()->getInitIndex());
786 newState->setTimeStep(integrator->getTimeStepControl()->getInitTimeStep());
787 newState->setTolRel (integrator->getTimeStepControl()->getMaxRelError());
788 newState->setTolAbs (integrator->getTimeStepControl()->getMaxAbsError());
789 newState->setOrder (integrator->getStepper()->getOrder());
793 auto shPL = Teuchos::sublist(integratorPL,
"Solution History",
true);
795 sh->addState(newState);
796 integrator->getStepper()->setInitialConditions(sh);
797 integrator->setSolutionHistory(sh);
800 integrator->setObserver(Teuchos::null);
803 integrator->setScreenOutputIndexInterval(
804 integratorPL->get<
int>(
"Screen Output Index Interval",
805 integrator->getScreenOutputIndexInterval()));
808 auto str = integratorPL->get<std::string>(
"Screen Output Index List",
"");
809 integrator->setScreenOutputIndexList(str);
811 auto validPL = Teuchos::rcp_const_cast<Teuchos::ParameterList>(integrator->getValidParameters());
814 auto vIntegratorName = validPL->template get<std::string>(
"Integrator Name");
815 auto vIntegratorPL = Teuchos::sublist(validPL, vIntegratorName,
true);
816 integratorPL->validateParametersAndSetDefaults(*vIntegratorPL);
819 auto vStepperName = vIntegratorPL->template get<std::string>(
"Stepper Name");
820 auto vStepperPL = Teuchos::sublist(validPL, vStepperName,
true);
821 stepperPL->validateParametersAndSetDefaults(*vStepperPL);
823 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.
IntegratorBasic()
Default constructor (requires calls to setModel and setSolutionHistory for initial conditions before ...
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.