9 #ifndef Tempus_StepperExplicitRK_impl_hpp
10 #define Tempus_StepperExplicitRK_impl_hpp
12 #include "Thyra_VectorStdOps.hpp"
18 template <
class Scalar>
21 this->setUseEmbedded(
false);
22 this->setStageNumber(-1);
23 this->setAppAction(Teuchos::null);
26 template <
class Scalar>
29 bool useFSAL, std::string ICConsistency,
bool ICConsistencyCheck,
33 this->setUseFSAL(useFSAL);
34 this->setICConsistency(ICConsistency);
35 this->setICConsistencyCheck(ICConsistencyCheck);
36 this->setUseEmbedded(useEmbedded);
37 this->setStageNumber(-1);
40 this->setAppAction(stepperRKAppAction);
42 if (appModel != Teuchos::null) {
43 this->setModel(appModel);
48 template <
class Scalar>
52 Scalar dt = Scalar(1.0e+99);
53 if (!this->getUseEmbedded())
return dt;
56 const int order = currentState->getOrder();
57 const Scalar time = currentState->getTime();
58 const Scalar errorRel = currentState->getTolRel();
59 const Scalar errorAbs = currentState->getTolAbs();
62 stageX = Thyra::createMember(this->appModel_->get_f_space());
63 scratchX = Thyra::createMember(this->appModel_->get_f_space());
64 Thyra::assign(stageX.
ptr(), *(currentState->getX()));
66 std::vector<Teuchos::RCP<Thyra::VectorBase<Scalar> > > stageXDot(2);
67 for (
int i = 0; i < 2; ++i) {
68 stageXDot[i] = Thyra::createMember(this->appModel_->get_f_space());
74 MEB::InArgs<Scalar> inArgs = this->appModel_->getNominalValues();
75 MEB::OutArgs<Scalar> outArgs = this->appModel_->createOutArgs();
77 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time);
78 if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(Teuchos::null);
79 outArgs.set_f(stageXDot[0]);
80 this->appModel_->evalModel(inArgs, outArgs);
82 this->stepperErrorNormCalculator_->setRelativeTolerance(errorRel);
83 this->stepperErrorNormCalculator_->setAbsoluteTolerance(errorAbs);
85 Scalar d0 = this->stepperErrorNormCalculator_->errorNorm(stageX);
86 Scalar d1 = this->stepperErrorNormCalculator_->errorNorm(stageXDot[0]);
89 dt = Teuchos::as<Scalar>(0.01) * (d0 / d1);
92 Thyra::Vp_StV(stageX.
ptr(), dt, *(stageXDot[0]));
96 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time + dt);
97 if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(Teuchos::null);
98 outArgs.set_f(stageXDot[1]);
99 this->appModel_->evalModel(inArgs, outArgs);
104 errX = Thyra::createMember(this->appModel_->get_f_space());
106 Thyra::V_VmV(errX.
ptr(), *(stageXDot[1]), *(stageXDot[0]));
107 Scalar d2 = this->stepperErrorNormCalculator_->errorNorm(errX) / dt;
110 Scalar max_d1_d2 = std::max(d1, d2);
111 Scalar h1 = std::pow((0.01 / max_d1_d2), (1.0 / (order + 1)));
114 dt = std::min(100 * dt, h1);
118 template <
class Scalar>
122 return this->getValidParametersBasicERK();
125 template <
class Scalar>
129 auto pl = this->getValidParametersBasic();
130 pl->template set<bool>(
131 "Use Embedded", this->getUseEmbedded(),
132 "'Whether to use Embedded Stepper (if available) or not\n"
133 " 'true' - Stepper will compute embedded solution and is adaptive.\n"
134 " 'false' - Stepper is not embedded(adaptive).\n");
135 pl->template set<std::string>(
"Description", this->getDescription());
140 template <
class Scalar>
144 "Error - Need to set the tableau, before calling "
145 "StepperExplicitRK::initialize()\n");
148 this->appModel_ == Teuchos::null, std::logic_error,
149 "Error - Need to set the model, setModel(), before calling "
150 "StepperExplicitRK::initialize()\n");
155 template <
class Scalar>
162 int numStages = this->tableau_->numStages();
163 stageXDot_.resize(numStages);
164 for (
int i = 0; i < numStages; ++i) {
165 stageXDot_[i] = Thyra::createMember(this->appModel_->get_f_space());
169 this->setEmbeddedMemory();
170 this->setErrorNorm();
172 this->isInitialized_ =
false;
175 template <
class Scalar>
178 if (this->getModel() == Teuchos::null)
181 if (this->tableau_->isEmbedded() && this->getUseEmbedded()) {
182 this->ee_ = Thyra::createMember(this->appModel_->get_f_space());
183 this->abs_u0 = Thyra::createMember(this->appModel_->get_f_space());
184 this->abs_u = Thyra::createMember(this->appModel_->get_f_space());
185 this->sc = Thyra::createMember(this->appModel_->get_f_space());
188 this->ee_ = Teuchos::null;
189 this->abs_u0 = Teuchos::null;
190 this->abs_u = Teuchos::null;
191 this->sc = Teuchos::null;
195 template <
class Scalar>
199 if (this->getUseFSAL())
200 this->setStepperXDot(stageXDot_.back());
202 this->setStepperXDot(stageXDot_[0]);
206 auto xDot = solutionHistory->getCurrentState()->getXDot();
207 if (xDot != Teuchos::null && this->getUseFSAL())
208 Thyra::assign(this->getStepperXDot().ptr(), *(xDot));
211 template <
class Scalar>
215 this->checkInitialized();
219 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperExplicitRK::takeStep()");
222 solutionHistory->getNumStates() < 2, std::logic_error,
223 "Error - StepperExplicitRK<Scalar>::takeStep(...)\n"
224 "Need at least two SolutionStates for ExplicitRK.\n"
225 " Number of States = "
226 << solutionHistory->getNumStates()
228 "Try setting in \"Solution History\" \"Storage Type\" = "
230 " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
233 RCP<SolutionState<Scalar> > currentState =
234 solutionHistory->getCurrentState();
235 RCP<SolutionState<Scalar> > workingState =
236 solutionHistory->getWorkingState();
237 const Scalar dt = workingState->getTimeStep();
238 const Scalar time = currentState->getTime();
240 const int numStages = this->tableau_->numStages();
245 Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
247 RCP<StepperExplicitRK<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
248 this->stepperRKAppAction_->execute(
249 solutionHistory, thisStepper,
253 for (
int i = 0; i < numStages; ++i) {
254 this->setStageNumber(i);
255 Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
256 for (
int j = 0; j < i; ++j) {
258 Thyra::Vp_StV(workingState->getX().ptr(), dt * A(i, j),
262 this->setStepperXDot(stageXDot_[i]);
264 this->stepperRKAppAction_->execute(
265 solutionHistory, thisStepper,
267 this->stepperRKAppAction_->execute(
268 solutionHistory, thisStepper,
270 this->stepperRKAppAction_->execute(
271 solutionHistory, thisStepper,
273 this->stepperRKAppAction_->execute(
274 solutionHistory, thisStepper,
277 if (i == 0 && this->getUseFSAL() &&
278 workingState->getNConsecutiveFailures() == 0) {
279 RCP<Thyra::VectorBase<Scalar> > tmp = stageXDot_[0];
280 stageXDot_[0] = stageXDot_.back();
281 stageXDot_.back() = tmp;
282 this->setStepperXDot(stageXDot_[0]);
285 const Scalar ts = time + c(i) * dt;
289 this->evaluateExplicitODE(stageXDot_[i], workingState->getX(), ts, p);
292 this->stepperRKAppAction_->execute(
293 solutionHistory, thisStepper,
297 this->setStageNumber(-1);
300 Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
301 for (
int i = 0; i < numStages; ++i) {
303 Thyra::Vp_StV((workingState->getX()).ptr(), dt * b(i),
308 if (this->getUseFSAL()) {
309 if (numStages == 1) {
310 const Scalar ts = time + dt;
313 this->evaluateExplicitODE(stageXDot_[0], workingState->getX(), ts, p);
315 if (workingState->getXDot() != Teuchos::null)
316 Thyra::assign((workingState->getXDot()).ptr(), *(stageXDot_.back()));
324 if (this->tableau_->isEmbedded() && this->getUseEmbedded()) {
325 const Scalar tolRel = workingState->getTolRel();
326 const Scalar tolAbs = workingState->getTolAbs();
329 this->stepperErrorNormCalculator_->setRelativeTolerance(tolRel);
330 this->stepperErrorNormCalculator_->setAbsoluteTolerance(tolAbs);
335 errWght -= this->tableau_->bstar();
340 for (
int i = 0; i < numStages; ++i) {
342 Thyra::Vp_StV(this->ee_.ptr(), dt * errWght(i), *(stageXDot_[i]));
346 Scalar err = this->stepperErrorNormCalculator_->computeWRMSNorm(
347 currentState->getX(), workingState->getX(), this->ee_);
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);
357 this->stepperRKAppAction_->execute(
358 solutionHistory, thisStepper,
370 template <
class Scalar>
379 template <
class Scalar>
388 out <<
"--- StepperExplicitRK ---\n";
389 if (this->tableau_ != Teuchos::null) this->tableau_->describe(out, verbLevel);
390 out <<
" tableau_ = " << this->tableau_ << std::endl;
391 out <<
" stepperRKAppAction_= " << this->stepperRKAppAction_ << std::endl;
392 out <<
" stageXDot_.size() = " << stageXDot_.size() << std::endl;
393 const int numStages = stageXDot_.size();
394 for (
int i = 0; i < numStages; ++i)
395 out <<
" stageXDot_[" << i <<
"] = " << stageXDot_[i] << std::endl;
398 out <<
" ee_ = " << this->ee_ << std::endl;
399 out <<
" abs_u0 = " << this->abs_u0 << std::endl;
400 out <<
" abs_u = " << this->abs_u << std::endl;
401 out <<
" sc = " << this->sc << std::endl;
402 out <<
"-------------------------" << std::endl;
405 template <
class Scalar>
409 bool isValidSetup =
true;
414 if (this->tableau_ == Teuchos::null) {
415 isValidSetup =
false;
416 out <<
"The tableau is not set!\n";
419 if (this->stepperRKAppAction_ == Teuchos::null) {
420 isValidSetup =
false;
421 out <<
"The AppAction is not set!\n";
428 #endif // Tempus_StepperExplicitRK_impl_hpp
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Set model.
virtual void setupDefault()
Default setup for constructor.
virtual Scalar getInitTimeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual void initialize()
Initialize after construction and changing input parameters.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
Thyra Base interface for time steppers.
StepperState is a simple class to hold state information about the stepper.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Application Action for StepperRKBase.
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions, make them consistent, and set needed memory.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Set model.
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
Teuchos::RCP< Teuchos::ParameterList > getValidParametersBasicERK() const
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
virtual void initialize()
Initialize during construction and after changing input parameters.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
virtual void setEmbeddedMemory()
Thyra Base interface for implicit time steppers.
std::string toString(const T &t)
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.