9#ifndef Tempus_StepperExplicitRK_impl_hpp
10#define Tempus_StepperExplicitRK_impl_hpp
12#include "Thyra_VectorStdOps.hpp"
34 std::string ICConsistency,
35 bool ICConsistencyCheck,
48 if (appModel != Teuchos::null) {
60 Scalar dt = Scalar(1.0e+99);
63 Teuchos::RCP<SolutionState<Scalar> > currentState=sh->getCurrentState();
64 const int order = currentState->getOrder();
65 const Scalar time = currentState->getTime();
66 const Scalar errorRel = currentState->getTolRel();
67 const Scalar errorAbs = currentState->getTolAbs();
69 Teuchos::RCP<Thyra::VectorBase<Scalar> > stageX, scratchX;
70 stageX = Thyra::createMember(this->
appModel_->get_f_space());
71 scratchX = Thyra::createMember(this->
appModel_->get_f_space());
72 Thyra::assign(stageX.ptr(), *(currentState->getX()));
74 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar> > > stageXDot(2);
75 for (
int i=0; i<2; ++i) {
76 stageXDot[i] = Thyra::createMember(this->
appModel_->get_f_space());
77 assign(stageXDot[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
81 typedef Thyra::ModelEvaluatorBase MEB;
82 MEB::InArgs<Scalar> inArgs = this->
appModel_->getNominalValues();
83 MEB::OutArgs<Scalar> outArgs = this->
appModel_->createOutArgs();
85 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time);
86 if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(Teuchos::null);
87 outArgs.set_f(stageXDot[0]);
88 this->
appModel_->evalModel(inArgs, outArgs);
97 dt = Teuchos::as<Scalar>(0.01)*(d0/d1);
100 Thyra::Vp_StV(stageX.ptr(), dt, *(stageXDot[0]));
103 inArgs.set_x(stageX);
104 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time + dt);
105 if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(Teuchos::null);
106 outArgs.set_f(stageXDot[1]);
107 this->
appModel_->evalModel(inArgs, outArgs);
111 Teuchos::RCP<Thyra::VectorBase<Scalar> > errX;
112 errX = Thyra::createMember(this->
appModel_->get_f_space());
113 assign(errX.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
114 Thyra::V_VmV(errX.ptr(), *(stageXDot[1]), *(stageXDot[0]));
118 Scalar max_d1_d2 = std::max(d1, d2);
119 Scalar h1 = std::pow((0.01/max_d1_d2),(1.0/(order+1)));
122 dt = std::min(100*dt, h1);
127template<
class Scalar>
128Teuchos::RCP<const Teuchos::ParameterList>
135template<
class Scalar>
136Teuchos::RCP<Teuchos::ParameterList>
141 "'Whether to use Embedded Stepper (if available) or not\n"
142 " 'true' - Stepper will compute embedded solution and is adaptive.\n"
143 " 'false' - Stepper is not embedded(adaptive).\n");
144 pl->template set<std::string>(
"Description", this->
getDescription());
150template<
class Scalar>
153 TEUCHOS_TEST_FOR_EXCEPTION( this->
tableau_ == Teuchos::null, std::logic_error,
154 "Error - Need to set the tableau, before calling "
155 "StepperExplicitRK::initialize()\n");
157 TEUCHOS_TEST_FOR_EXCEPTION( this->
appModel_==Teuchos::null, std::logic_error,
158 "Error - Need to set the model, setModel(), before calling "
159 "StepperExplicitRK::initialize()\n");
165template<
class Scalar>
172 int numStages = this->
tableau_->numStages();
174 for (
int i=0; i<numStages; ++i) {
176 assign(
stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
186template<
class Scalar>
189 if (this->
getModel() == Teuchos::null)
192 if ( this->
tableau_->isEmbedded() && this->getUseEmbedded() ){
193 this->
ee_ = Thyra::createMember(this->
appModel_->get_f_space());
195 this->
abs_u = Thyra::createMember(this->
appModel_->get_f_space());
196 this->
sc = Thyra::createMember(this->
appModel_->get_f_space());
198 this->
ee_ = Teuchos::null;
199 this->
abs_u0 = Teuchos::null;
200 this->
abs_u = Teuchos::null;
201 this->
sc = Teuchos::null;
206template<
class Scalar>
217 auto xDot = solutionHistory->getCurrentState()->getXDot();
218 if (xDot != Teuchos::null && this->
getUseFSAL())
223template<
class Scalar>
231 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperExplicitRK::takeStep()");
233 TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
235 "Error - StepperExplicitRK<Scalar>::takeStep(...)\n"
236 "Need at least two SolutionStates for ExplicitRK.\n"
237 " Number of States = " << solutionHistory->getNumStates() <<
"\n"
238 "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
239 " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
241 RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
242 RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
243 const Scalar dt = workingState->getTimeStep();
244 const Scalar time = currentState->getTime();
246 const int numStages = this->
tableau_->numStages();
247 Teuchos::SerialDenseMatrix<int,Scalar> A = this->
tableau_->A();
248 Teuchos::SerialDenseVector<int,Scalar> b = this->
tableau_->b();
249 Teuchos::SerialDenseVector<int,Scalar> c = this->
tableau_->c();
251 Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
253 RCP<StepperExplicitRK<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
258 for (
int i=0; i < numStages; ++i) {
260 Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
261 for (
int j=0; j < i; ++j) {
262 if (A(i,j) != Teuchos::ScalarTraits<Scalar>::zero()) {
263 Thyra::Vp_StV(workingState->getX().ptr(), dt*A(i,j), *
stageXDot_[j]);
278 workingState->getNConsecutiveFailures() == 0 ) {
279 RCP<Thyra::VectorBase<Scalar> > tmp =
stageXDot_[0];
285 const Scalar ts = time + c(i)*dt;
299 Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
300 for (
int i=0; i < numStages; ++i) {
301 if (b(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
302 Thyra::Vp_StV((workingState->getX()).ptr(), dt*b(i), *(
stageXDot_[i]));
307 if (numStages == 1) {
308 const Scalar ts = time + dt;
313 if (workingState->getXDot() != Teuchos::null)
314 Thyra::assign((workingState->getXDot()).ptr(), *(
stageXDot_.back()));
323 if (this->
tableau_->isEmbedded() && this->getUseEmbedded()) {
325 const Scalar tolRel = workingState->getTolRel();
326 const Scalar tolAbs = workingState->getTolAbs();
334 Teuchos::SerialDenseVector<int,Scalar> errWght = b ;
339 assign(this->
ee_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
340 for (
int i=0; i < numStages; ++i) {
341 if (errWght(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
342 Thyra::Vp_StV(this->
ee_.ptr(), dt*errWght(i), *(
stageXDot_[i]));
348 workingState->setErrorRel(err);
351 if (std::isinf(err) || std::isnan(err) || err > Teuchos::as<Scalar>(1.0))
355 workingState->setOrder(this->
getOrder());
356 workingState->computeNorms(currentState);
370template<
class Scalar>
374 Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
380template<
class Scalar>
382 Teuchos::FancyOStream &out,
383 const Teuchos::EVerbosityLevel verbLevel)
const
385 out.setOutputToRootOnly(0);
390 out <<
"--- StepperExplicitRK ---\n";
391 if (this->
tableau_ != Teuchos::null) this->
tableau_->describe(out, verbLevel);
392 out <<
" tableau_ = " << this->
tableau_ << std::endl;
394 out <<
" stageXDot_.size() = " <<
stageXDot_.size() << std::endl;
396 for (
int i=0; i<numStages; ++i)
397 out <<
" stageXDot_["<<i<<
"] = " <<
stageXDot_[i] << std::endl;
398 out <<
" useEmbedded_ = "
400 out <<
" ee_ = " << this->
ee_ << std::endl;
401 out <<
" abs_u0 = " << this->
abs_u0 << std::endl;
402 out <<
" abs_u = " << this->
abs_u << std::endl;
403 out <<
" sc = " << this->
sc << std::endl;
404 out <<
"-------------------------" << std::endl;
408template<
class Scalar>
411 out.setOutputToRootOnly(0);
417 if (this->
tableau_ == Teuchos::null) {
419 out <<
"The tableau is not set!\n";
424 out <<
"The AppAction is not set!\n";
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void setup(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel, bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck, bool useEmbedded, const Teuchos::RCP< StepperRKAppAction< Scalar > > &stepperRKAppAction)
Setup for constructor.
virtual std::string getDescription() const =0
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual void setEmbeddedMemory()
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
virtual Scalar getInitTimeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) const
virtual void setupDefault()
Default setup for constructor.
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Set model.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Teuchos::RCP< Teuchos::ParameterList > getValidParametersBasicERK() const
std::vector< Teuchos::RCP< Thyra::VectorBase< Scalar > > > stageXDot_
virtual void initialize()
Initialize during construction and after changing input parameters.
virtual void evaluateExplicitODE(Teuchos::RCP< Thyra::VectorBase< Scalar > > xDot, Teuchos::RCP< const Thyra::VectorBase< Scalar > > x, const Scalar time, const Teuchos::RCP< ExplicitODEParameters< Scalar > > &p)
Evaluate xDot = f(x,t).
virtual Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > getModel() const
Return the application ModelEvaluator.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions, make them consistent, and set needed memory.
Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > appModel_
Explicit ODE ModelEvaluator.
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Set model.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Application Action for StepperRKBase.
@ BEGIN_STEP
At the beginning of the step.
@ BEFORE_SOLVE
Before the implicit solve.
@ BEFORE_EXPLICIT_EVAL
Before the explicit evaluation.
@ AFTER_SOLVE
After the implicit solve.
@ BEGIN_STAGE
At the beginning of the stage.
@ END_STAGE
At the end of the stage.
@ END_STEP
At the end of the step.
Teuchos::RCP< Thyra::VectorBase< Scalar > > ee_
virtual void setStageNumber(int s)
Teuchos::RCP< Thyra::VectorBase< Scalar > > abs_u0
Teuchos::RCP< StepperRKAppAction< Scalar > > stepperRKAppAction_
virtual void setUseEmbedded(bool a)
Teuchos::RCP< Thyra::VectorBase< Scalar > > sc
virtual void setErrorNorm(const Teuchos::RCP< Stepper_ErrorNorm< Scalar > > &errCalculator=Teuchos::null)
virtual Scalar getOrder() const
virtual void setAppAction(Teuchos::RCP< StepperRKAppAction< Scalar > > appAction)
virtual bool getUseEmbedded() const
Teuchos::RCP< Thyra::VectorBase< Scalar > > abs_u
Teuchos::RCP< Stepper_ErrorNorm< Scalar > > stepperErrorNormCalculator_
Teuchos::RCP< RKButcherTableau< Scalar > > tableau_
StepperState is a simple class to hold state information about the stepper.
bool isInitialized_
True if stepper's member data is initialized.
void setICConsistencyCheck(bool c)
virtual void setStepperXDot(Teuchos::RCP< Thyra::VectorBase< Scalar > > xDot)
Set xDot for Stepper storage.
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getStepperXDot()
Get Stepper xDot.
virtual void initialize()
Initialize after construction and changing input parameters.
std::string getStepperType() const
Get the stepper type. The stepper type is used as an identifier for the stepper, and can only be set ...
Teuchos::RCP< Teuchos::ParameterList > getValidParametersBasic() const
Add basic parameters to Steppers ParameterList.
virtual void setUseFSAL(bool a)
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
virtual void checkInitialized()
Check initialization, and error out on failure.
void setICConsistency(std::string s)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const