10 #ifndef Tempus_StepperDIRK_impl_hpp
11 #define Tempus_StepperDIRK_impl_hpp
13 #include "Thyra_VectorStdOps.hpp"
15 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
19 template <
class Scalar>
22 this->setUseEmbedded(
false);
23 this->setZeroInitialGuess(
false);
24 this->setStageNumber(-1);
26 this->setAppAction(Teuchos::null);
27 this->setDefaultSolver();
30 template <
class Scalar>
34 bool useFSAL, std::string ICConsistency,
bool ICConsistencyCheck,
35 bool useEmbedded,
bool zeroInitialGuess,
38 this->setUseFSAL(useFSAL);
39 this->setICConsistency(ICConsistency);
40 this->setICConsistencyCheck(ICConsistencyCheck);
41 this->setUseEmbedded(useEmbedded);
42 this->setZeroInitialGuess(zeroInitialGuess);
44 this->setStageNumber(-1);
46 this->setAppAction(stepperRKAppAction);
47 this->setSolver(solver);
49 if (appModel != Teuchos::null) {
50 this->setModel(appModel);
55 template <
class Scalar>
59 auto pl = this->getValidParametersBasicImplicit();
60 pl->template set<bool>(
61 "Use Embedded", this->getUseEmbedded(),
62 "'Whether to use Embedded Stepper (if available) or not\n"
63 " 'true' - Stepper will compute embedded solution and is adaptive.\n"
64 " 'false' - Stepper is not embedded(adaptive).\n");
65 pl->template set<std::string>(
"Description", this->getDescription());
66 pl->template set<bool>(
"Reset Initial Guess", this->getResetInitialGuess());
71 template <
class Scalar>
75 "Error - Need to set the tableau, before calling "
76 "StepperDIRK::initialize()\n");
79 this->wrapperModel_ == Teuchos::null, std::logic_error,
80 "Error - Need to set the model, setModel(), before calling "
81 "StepperDIRK::initialize()\n");
86 template <
class Scalar>
93 const int numStages = this->tableau_->numStages();
94 stageXDot_.resize(numStages);
95 for (
int i = 0; i < numStages; ++i) {
96 stageXDot_[i] = Thyra::createMember(this->wrapperModel_->get_f_space());
99 xTilde_ = Thyra::createMember(this->wrapperModel_->get_x_space());
102 this->setEmbeddedMemory();
103 this->setErrorNorm();
105 this->isInitialized_ =
false;
108 template <
class Scalar>
111 if (this->getModel() == Teuchos::null)
114 if (this->tableau_->isEmbedded() && this->getUseEmbedded()) {
115 this->ee_ = Thyra::createMember(this->wrapperModel_->get_f_space());
116 this->abs_u0 = Thyra::createMember(this->wrapperModel_->get_f_space());
117 this->abs_u = Thyra::createMember(this->wrapperModel_->get_f_space());
118 this->sc = Thyra::createMember(this->wrapperModel_->get_f_space());
121 this->ee_ = Teuchos::null;
122 this->abs_u0 = Teuchos::null;
123 this->abs_u = Teuchos::null;
124 this->sc = Teuchos::null;
128 template <
class Scalar>
132 this->setStepperXDot(stageXDot_.back());
136 template <
class Scalar>
140 this->checkInitialized();
144 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperDIRK::takeStep()");
147 solutionHistory->getNumStates() < 2, std::logic_error,
148 "Error - StepperDIRK<Scalar>::takeStep(...)\n"
149 <<
"Need at least two SolutionStates for DIRK.\n"
150 <<
" Number of States = " << solutionHistory->getNumStates()
151 <<
"\nTry setting in \"Solution History\" "
152 <<
"\"Storage Type\" = \"Undo\"\n"
153 <<
" or \"Storage Type\" = \"Static\" and "
154 <<
"\"Storage Limit\" = \"2\"\n");
156 RCP<SolutionState<Scalar> > currentState =
157 solutionHistory->getCurrentState();
158 RCP<SolutionState<Scalar> > workingState =
159 solutionHistory->getWorkingState();
160 const Scalar dt = workingState->getTimeStep();
161 const Scalar time = currentState->getTime();
163 const int numStages = this->tableau_->numStages();
169 if (this->getResetInitialGuess() && (!this->getZeroInitialGuess()))
170 Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
172 RCP<StepperDIRK<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
173 this->stepperRKAppAction_->execute(
174 solutionHistory, thisStepper,
180 for (
int i = 0; i < numStages; ++i) {
181 this->setStageNumber(i);
183 Thyra::assign(xTilde_.ptr(), *(currentState->getX()));
184 for (
int j = 0; j < i; ++j) {
186 Thyra::Vp_StV(xTilde_.ptr(), dt * A(i, j), *(stageXDot_[j]));
189 this->setStepperXDot(stageXDot_[i]);
191 this->stepperRKAppAction_->execute(
192 solutionHistory, thisStepper,
195 Scalar ts = time + c(i) * dt;
198 bool isNeeded =
false;
199 for (
int k = i + 1; k < numStages; ++k)
200 if (A(k, i) != 0.0) isNeeded =
true;
201 if (b(i) != 0.0) isNeeded =
true;
202 if (this->tableau_->isEmbedded() && this->getUseEmbedded() &&
203 this->tableau_->bstar()(i) != 0.0)
205 if (isNeeded ==
false) {
210 if (i == 0 && this->getUseFSAL() &&
211 workingState->getNConsecutiveFailures() == 0) {
213 RCP<Thyra::VectorBase<Scalar> > tmp = stageXDot_[0];
214 stageXDot_[0] = stageXDot_.back();
215 stageXDot_.back() = tmp;
216 this->setStepperXDot(stageXDot_[0]);
221 MEB::InArgs<Scalar> inArgs = this->wrapperModel_->createInArgs();
222 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->createOutArgs();
223 inArgs.set_x(xTilde_);
224 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(ts);
225 if (inArgs.supports(MEB::IN_ARG_x_dot))
226 inArgs.set_x_dot(Teuchos::null);
227 outArgs.set_f(stageXDot_[i]);
229 this->wrapperModel_->getAppModel()->evalModel(inArgs, outArgs);
234 const Scalar alpha = 1.0 / (dt * A(i, i));
235 const Scalar beta = 1.0;
244 this->stepperRKAppAction_->execute(
245 solutionHistory, thisStepper,
249 this->solveImplicitODE(workingState->getX(), stageXDot_[i], ts, p);
253 this->stepperRKAppAction_->execute(
254 solutionHistory, thisStepper,
257 timeDer->compute(workingState->getX(), stageXDot_[i]);
259 this->stepperRKAppAction_->execute(
260 solutionHistory, thisStepper,
262 this->stepperRKAppAction_->execute(
263 solutionHistory, thisStepper,
267 this->setStageNumber(-1);
270 Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
271 for (
int i = 0; i < numStages; ++i) {
273 Thyra::Vp_StV((workingState->getX()).ptr(), dt * b(i),
278 if (this->tableau_->isEmbedded() && this->getUseEmbedded()) {
279 const Scalar tolRel = workingState->getTolRel();
280 const Scalar tolAbs = workingState->getTolAbs();
283 this->stepperErrorNormCalculator_->setRelativeTolerance(tolRel);
284 this->stepperErrorNormCalculator_->setAbsoluteTolerance(tolAbs);
289 errWght -= this->tableau_->bstar();
294 for (
int i = 0; i < numStages; ++i) {
296 Thyra::Vp_StV(this->ee_.ptr(), dt * errWght(i), *(stageXDot_[i]));
300 Scalar err = this->stepperErrorNormCalculator_->computeWRMSNorm(
301 currentState->getX(), workingState->getX(), this->ee_);
302 workingState->setErrorRel(err);
305 if (std::isinf(err) || std::isnan(err) || err > Teuchos::as<Scalar>(1.0))
314 workingState->setOrder(this->getOrder());
315 workingState->computeNorms(currentState);
316 this->stepperRKAppAction_->execute(
317 solutionHistory, thisStepper,
329 template <
class Scalar>
338 template <
class Scalar>
347 out <<
"--- StepperDIRK ---\n";
348 out <<
" tableau_ = " << this->tableau_ << std::endl;
349 if (this->tableau_ != Teuchos::null) this->tableau_->describe(out, verbLevel);
350 out <<
" stepperRKAppAction_ = " << this->stepperRKAppAction_ << std::endl;
351 out <<
" xTilde_ = " << xTilde_ << std::endl;
352 out <<
" stageXDot_.size() = " << stageXDot_.size() << std::endl;
353 const int numStages = stageXDot_.size();
354 for (
int i = 0; i < numStages; ++i)
355 out <<
" stageXDot_[" << i <<
"] = " << stageXDot_[i] << std::endl;
358 out <<
" ee_ = " << this->ee_ << std::endl;
359 out <<
" abs_u0 = " << this->abs_u0 << std::endl;
360 out <<
" abs_u = " << this->abs_u << std::endl;
361 out <<
" sc = " << this->sc << std::endl;
362 out <<
"-------------------" << std::endl;
365 template <
class Scalar>
369 bool isValidSetup =
true;
374 if (this->tableau_ == Teuchos::null) {
375 isValidSetup =
false;
376 out <<
"The tableau is not set!\n";
379 if (this->stepperRKAppAction_ == Teuchos::null) {
380 isValidSetup =
false;
381 out <<
"The AppAction is not set!\n";
387 template <
class Scalar>
391 return this->getValidParametersBasicDIRK();
395 #endif // Tempus_StepperDIRK_impl_hpp
Teuchos::RCP< Teuchos::ParameterList > getValidParametersBasicDIRK() const
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel) override
Set the model.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void initialize()
Initialize after construction and changing input parameters.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
Thyra Base interface for time steppers.
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel) override
Set the model.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Set the initial conditions and make them consistent.
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)
Thyra Base interface for implicit time steppers.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Application Action for StepperRKBase.
Time-derivative interface for DIRK.
virtual void setupDefault()
Default setup for constructor.
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual bool isValidSetup(Teuchos::FancyOStream &out) const override
virtual void initialize() override
Initialize after construction and changing input parameters.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Set the initial conditions and make them consistent.
virtual void setEmbeddedMemory() override
virtual void setup(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &wrapperModel, const Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > &solver, bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck, bool useEmbedded, bool zeroInitialGuess, const Teuchos::RCP< StepperRKAppAction< Scalar > > &stepperRKAppAction)
Setup for constructor.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState() override
Get a default (initial) StepperState.
Solve for x and determine xDot from x.
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) override
Take the specified timestep, dt, and return true if successful.
std::string toString(const T &t)