10#ifndef Tempus_StepperExplicitRK_impl_hpp
11#define Tempus_StepperExplicitRK_impl_hpp
13#include "Thyra_VectorStdOps.hpp"
19template <
class Scalar>
27template <
class Scalar>
30 bool useFSAL, std::string ICConsistency,
bool ICConsistencyCheck,
43 if (appModel != Teuchos::null) {
49template <
class Scalar>
53 Scalar dt = Scalar(1.0e+99);
56 Teuchos::RCP<SolutionState<Scalar> > currentState = sh->getCurrentState();
57 const int order = currentState->getOrder();
58 const Scalar time = currentState->getTime();
59 const Scalar errorRel = currentState->getTolRel();
60 const Scalar errorAbs = currentState->getTolAbs();
62 Teuchos::RCP<Thyra::VectorBase<Scalar> > stageX, scratchX;
63 stageX = Thyra::createMember(this->
appModel_->get_f_space());
64 scratchX = Thyra::createMember(this->
appModel_->get_f_space());
65 Thyra::assign(stageX.ptr(), *(currentState->getX()));
67 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar> > > stageXDot(2);
68 for (
int i = 0; i < 2; ++i) {
69 stageXDot[i] = Thyra::createMember(this->
appModel_->get_f_space());
70 assign(stageXDot[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
74 typedef Thyra::ModelEvaluatorBase MEB;
75 MEB::InArgs<Scalar> inArgs = this->
appModel_->getNominalValues();
76 MEB::OutArgs<Scalar> outArgs = this->
appModel_->createOutArgs();
78 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time);
79 if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(Teuchos::null);
80 outArgs.set_f(stageXDot[0]);
81 this->
appModel_->evalModel(inArgs, outArgs);
90 dt = Teuchos::as<Scalar>(0.01) * (d0 / d1);
93 Thyra::Vp_StV(stageX.ptr(), dt, *(stageXDot[0]));
97 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time + dt);
98 if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(Teuchos::null);
99 outArgs.set_f(stageXDot[1]);
100 this->
appModel_->evalModel(inArgs, outArgs);
104 Teuchos::RCP<Thyra::VectorBase<Scalar> > errX;
105 errX = Thyra::createMember(this->
appModel_->get_f_space());
106 assign(errX.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
107 Thyra::V_VmV(errX.ptr(), *(stageXDot[1]), *(stageXDot[0]));
111 Scalar max_d1_d2 = std::max(d1, d2);
112 Scalar h1 = std::pow((0.01 / max_d1_d2), (1.0 / (order + 1)));
115 dt = std::min(100 * dt, h1);
119template <
class Scalar>
120Teuchos::RCP<const Teuchos::ParameterList>
126template <
class Scalar>
127Teuchos::RCP<Teuchos::ParameterList>
131 pl->template set<bool>(
133 "'Whether to use Embedded Stepper (if available) or not\n"
134 " 'true' - Stepper will compute embedded solution and is adaptive.\n"
135 " 'false' - Stepper is not embedded(adaptive).\n");
136 pl->template set<std::string>(
"Description", this->
getDescription());
141template <
class Scalar>
144 TEUCHOS_TEST_FOR_EXCEPTION(this->
tableau_ == Teuchos::null, std::logic_error,
145 "Error - Need to set the tableau, before calling "
146 "StepperExplicitRK::initialize()\n");
148 TEUCHOS_TEST_FOR_EXCEPTION(
149 this->
appModel_ == Teuchos::null, std::logic_error,
150 "Error - Need to set the model, setModel(), before calling "
151 "StepperExplicitRK::initialize()\n");
156template <
class Scalar>
163 int numStages = this->
tableau_->numStages();
165 for (
int i = 0; i < numStages; ++i) {
167 assign(
stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
176template <
class Scalar>
179 if (this->
getModel() == Teuchos::null)
182 if (this->
tableau_->isEmbedded() && this->getUseEmbedded()) {
183 this->
ee_ = Thyra::createMember(this->
appModel_->get_f_space());
185 this->
abs_u = Thyra::createMember(this->
appModel_->get_f_space());
186 this->
sc = Thyra::createMember(this->
appModel_->get_f_space());
189 this->
ee_ = Teuchos::null;
190 this->
abs_u0 = Teuchos::null;
191 this->
abs_u = Teuchos::null;
192 this->
sc = Teuchos::null;
196template <
class Scalar>
207 auto xDot = solutionHistory->getCurrentState()->getXDot();
208 if (xDot != Teuchos::null && this->
getUseFSAL())
212template <
class Scalar>
220 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperExplicitRK::takeStep()");
222 TEUCHOS_TEST_FOR_EXCEPTION(
223 solutionHistory->getNumStates() < 2, std::logic_error,
224 "Error - StepperExplicitRK<Scalar>::takeStep(...)\n"
225 "Need at least two SolutionStates for ExplicitRK.\n"
226 " Number of States = "
227 << solutionHistory->getNumStates()
229 "Try setting in \"Solution History\" \"Storage Type\" = "
231 " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
234 RCP<SolutionState<Scalar> > currentState =
235 solutionHistory->getCurrentState();
236 RCP<SolutionState<Scalar> > workingState =
237 solutionHistory->getWorkingState();
238 const Scalar dt = workingState->getTimeStep();
239 const Scalar time = currentState->getTime();
241 const int numStages = this->
tableau_->numStages();
242 Teuchos::SerialDenseMatrix<int, Scalar> A = this->
tableau_->A();
243 Teuchos::SerialDenseVector<int, Scalar> b = this->
tableau_->b();
244 Teuchos::SerialDenseVector<int, Scalar> c = this->
tableau_->c();
246 Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
248 RCP<StepperExplicitRK<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
250 solutionHistory, thisStepper,
254 for (
int i = 0; i < numStages; ++i) {
256 Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
257 for (
int j = 0; j < i; ++j) {
258 if (A(i, j) != Teuchos::ScalarTraits<Scalar>::zero()) {
259 Thyra::Vp_StV(workingState->getX().ptr(), dt * A(i, j),
266 solutionHistory, thisStepper,
269 solutionHistory, thisStepper,
272 solutionHistory, thisStepper,
275 solutionHistory, thisStepper,
279 workingState->getNConsecutiveFailures() == 0) {
280 RCP<Thyra::VectorBase<Scalar> > tmp =
stageXDot_[0];
286 const Scalar ts = time + c(i) * dt;
294 solutionHistory, thisStepper,
301 Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
302 for (
int i = 0; i < numStages; ++i) {
303 if (b(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
304 Thyra::Vp_StV((workingState->getX()).ptr(), dt * b(i),
310 if (numStages == 1) {
311 const Scalar ts = time + dt;
316 if (workingState->getXDot() != Teuchos::null)
317 Thyra::assign((workingState->getXDot()).ptr(), *(
stageXDot_.back()));
325 if (this->
tableau_->isEmbedded() && this->getUseEmbedded()) {
326 const Scalar tolRel = workingState->getTolRel();
327 const Scalar tolAbs = workingState->getTolAbs();
335 Teuchos::SerialDenseVector<int, Scalar> errWght = b;
340 assign(this->
ee_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
341 for (
int i = 0; i < numStages; ++i) {
342 if (errWght(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
343 Thyra::Vp_StV(this->
ee_.ptr(), dt * errWght(i), *(
stageXDot_[i]));
348 currentState->getX(), workingState->getX(), this->ee_);
349 workingState->setErrorRel(err);
352 if (std::isinf(err) || std::isnan(err) || err > Teuchos::as<Scalar>(1.0))
356 workingState->setOrder(this->
getOrder());
357 workingState->computeNorms(currentState);
359 solutionHistory, thisStepper,
371template <
class Scalar>
372Teuchos::RCP<Tempus::StepperState<Scalar> >
375 Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
380template <
class Scalar>
382 Teuchos::FancyOStream& out,
const Teuchos::EVerbosityLevel verbLevel)
const
384 out.setOutputToRootOnly(0);
389 out <<
"--- StepperExplicitRK ---\n";
390 if (this->
tableau_ != Teuchos::null) this->
tableau_->describe(out, verbLevel);
391 out <<
" tableau_ = " << this->
tableau_ << std::endl;
393 out <<
" stageXDot_.size() = " <<
stageXDot_.size() << std::endl;
395 for (
int i = 0; i < numStages; ++i)
396 out <<
" stageXDot_[" << i <<
"] = " <<
stageXDot_[i] << std::endl;
397 out <<
" useEmbedded_ = " << Teuchos::toString(this->
useEmbedded_)
399 out <<
" ee_ = " << this->
ee_ << std::endl;
400 out <<
" abs_u0 = " << this->
abs_u0 << std::endl;
401 out <<
" abs_u = " << this->
abs_u << std::endl;
402 out <<
" sc = " << this->
sc << std::endl;
403 out <<
"-------------------------" << std::endl;
406template <
class Scalar>
409 out.setOutputToRootOnly(0);
415 if (this->
tableau_ == Teuchos::null) {
417 out <<
"The tableau is not set!\n";
422 out <<
"The AppAction is not set!\n";
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
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
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.
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
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.