9 #ifndef Tempus_StepperDIRK_impl_hpp
10 #define Tempus_StepperDIRK_impl_hpp
12 #include "Thyra_VectorStdOps.hpp"
14 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
20 template<
class Scalar>
23 this->setUseEmbedded(
false);
24 this->setZeroInitialGuess(
false);
25 this->setStageNumber(-1);
27 this->setAppAction(Teuchos::null);
28 this->setDefaultSolver();
32 template<
class Scalar>
37 std::string ICConsistency,
38 bool ICConsistencyCheck,
40 bool zeroInitialGuess,
43 this->setUseFSAL( useFSAL);
44 this->setICConsistency( ICConsistency);
45 this->setICConsistencyCheck( ICConsistencyCheck);
46 this->setUseEmbedded( useEmbedded);
47 this->setZeroInitialGuess( zeroInitialGuess);
49 this->setStageNumber(-1);
50 this->setAppAction(stepperRKAppAction);
51 this->setSolver(solver);
53 if (appModel != Teuchos::null) {
54 this->setModel(appModel);
60 template<
class Scalar>
64 auto pl = this->getValidParametersBasicImplicit();
65 pl->template set<bool>(
"Use Embedded", this->getUseEmbedded(),
66 "'Whether to use Embedded Stepper (if available) or not\n"
67 " 'true' - Stepper will compute embedded solution and is adaptive.\n"
68 " 'false' - Stepper is not embedded(adaptive).\n");
69 pl->template set<std::string>(
"Description", this->getDescription());
70 pl->template set<bool>(
"Reset Initial Guess", this->getResetInitialGuess());
76 template<
class Scalar>
80 const int numStages = this->tableau_->numStages();
81 stageXDot_.resize(numStages);
82 for (
int i=0; i<numStages; ++i) {
83 stageXDot_[i] = Thyra::createMember(this->wrapperModel_->get_f_space());
86 xTilde_ = Thyra::createMember(this->wrapperModel_->get_x_space());
89 if (this->tableau_->isEmbedded() && this->getUseEmbedded()) {
90 this->ee_ = Thyra::createMember(this->wrapperModel_->get_f_space());
91 this->abs_u0 = Thyra::createMember(this->wrapperModel_->get_f_space());
92 this->abs_u = Thyra::createMember(this->wrapperModel_->get_f_space());
93 this->sc = Thyra::createMember(this->wrapperModel_->get_f_space());
100 template<
class Scalar>
104 this->setStepperXDot(stageXDot_.back());
109 template<
class Scalar>
113 this->checkInitialized();
117 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperDIRK::takeStep()");
121 "Error - StepperDIRK<Scalar>::takeStep(...)\n"
122 "Need at least two SolutionStates for DIRK.\n"
123 " Number of States = " << solutionHistory->getNumStates() <<
"\n"
124 "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
125 " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
127 RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
128 RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
129 const Scalar dt = workingState->getTimeStep();
130 const Scalar time = currentState->getTime();
132 const int numStages = this->tableau_->numStages();
138 if ( this->getResetInitialGuess() && (!this->getZeroInitialGuess()) )
139 Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
141 RCP<StepperDIRK<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
142 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
148 for (
int i=0; i < numStages; ++i) {
149 this->setStageNumber(i);
151 Thyra::assign(xTilde_.ptr(), *(currentState->getX()));
152 for (
int j=0; j < i; ++j) {
154 Thyra::Vp_StV(xTilde_.ptr(), dt*A(i,j), *(stageXDot_[j]));
157 this->setStepperXDot(stageXDot_[i]);
159 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
162 Scalar ts = time + c(i)*dt;
165 bool isNeeded =
false;
166 for (
int k=i+1; k<numStages; ++k)
if (A(k,i) != 0.0) isNeeded =
true;
167 if (b(i) != 0.0) isNeeded =
true;
168 if (this->tableau_->isEmbedded() && this->getUseEmbedded() &&
169 this->tableau_->bstar()(i) != 0.0)
171 if (isNeeded ==
false) {
175 if (i == 0 && this->getUseFSAL() &&
176 workingState->getNConsecutiveFailures() == 0) {
178 RCP<Thyra::VectorBase<Scalar> > tmp = stageXDot_[0];
179 stageXDot_[0] = stageXDot_.back();
180 stageXDot_.back() = tmp;
181 this->setStepperXDot(stageXDot_[0]);
185 MEB::InArgs<Scalar> inArgs = this->wrapperModel_->getInArgs();
186 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
187 inArgs.set_x(xTilde_);
188 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(ts);
189 if (inArgs.supports(MEB::IN_ARG_x_dot))
190 inArgs.set_x_dot(Teuchos::null);
191 outArgs.set_f(stageXDot_[i]);
193 this->wrapperModel_->getAppModel()->evalModel(inArgs,outArgs);
197 const Scalar alpha = 1.0/(dt*A(i,i));
198 const Scalar beta = 1.0;
203 alpha,xTilde_.getConst()));
208 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
211 sStatus = this->solveImplicitODE(workingState->getX(), stageXDot_[i], ts, p);
215 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
218 timeDer->compute(workingState->getX(), stageXDot_[i]);
220 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
222 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
226 this->setStageNumber(-1);
229 Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
230 for (
int i=0; i < numStages; ++i) {
232 Thyra::Vp_StV((workingState->getX()).ptr(), dt*b(i), *(stageXDot_[i]));
236 if (this->tableau_->isEmbedded() && this->getUseEmbedded()) {
237 const Scalar tolRel = workingState->getTolRel();
238 const Scalar tolAbs = workingState->getTolAbs();
243 errWght -= this->tableau_->bstar();
248 for (
int i=0; i < numStages; ++i) {
250 Thyra::Vp_StV(this->ee_.ptr(), dt*errWght(i), *(stageXDot_[i]));
255 Thyra::abs( *(currentState->getX()), this->abs_u0.ptr());
256 Thyra::abs( *(workingState->getX()), this->abs_u.ptr());
257 Thyra::pair_wise_max_update(tolRel, *this->abs_u0, this->abs_u.ptr());
258 Thyra::add_scalar(tolAbs, this->abs_u.ptr());
262 Thyra::ele_wise_divide(Teuchos::as<Scalar>(1.0), *this->ee_, *this->abs_u, this->sc.ptr());
263 const auto space_dim = this->ee_->space()->dim();
264 Scalar err = std::abs(Thyra::norm(*this->sc)) / space_dim ;
265 workingState->setErrorRel(err);
268 if (std::isinf(err) || std::isnan(err) || err > Teuchos::as<Scalar>(1.0))
275 workingState->setOrder(this->getOrder());
276 workingState->computeNorms(currentState);
277 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
289 template<
class Scalar>
300 template<
class Scalar>
309 out <<
"--- StepperDIRK ---\n";
310 out <<
" tableau_ = " << this->tableau_ << std::endl;
311 if (this->tableau_ != Teuchos::null) this->tableau_->describe(out, verbLevel);
312 out <<
" stepperRKAppAction_ = " << this->stepperRKAppAction_ << std::endl;
313 out <<
" xTilde_ = " << xTilde_ << std::endl;
314 out <<
" stageXDot_.size() = " << stageXDot_.size() << std::endl;
315 const int numStages = stageXDot_.size();
316 for (
int i=0; i<numStages; ++i)
317 out <<
" stageXDot_["<<i<<
"] = " << stageXDot_[i] << std::endl;
318 out <<
" useEmbedded_ = "
320 out <<
" ee_ = " << this->ee_ << std::endl;
321 out <<
" abs_u0 = " << this->abs_u0 << std::endl;
322 out <<
" abs_u = " << this->abs_u << std::endl;
323 out <<
" sc = " << this->sc << std::endl;
324 out <<
"-------------------" << std::endl;
328 template<
class Scalar>
331 bool isValidSetup =
true;
336 if (this->tableau_ == Teuchos::null) {
337 isValidSetup =
false;
338 out <<
"The tableau is not set!\n";
341 if (this->stepperRKAppAction_ == Teuchos::null) {
342 isValidSetup =
false;
343 out <<
"The AppAction is not set!\n";
350 template<
class Scalar>
354 return this->getValidParametersBasicDIRK();
359 #endif // Tempus_StepperDIRK_impl_hpp
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
Teuchos::RCP< Teuchos::ParameterList > getValidParametersBasicDIRK() const
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void initialize()
Initialize after construction and changing input parameters.
Thyra Base interface for time steppers.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
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.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
virtual void initialize()
Initialize after construction and changing input parameters.
Time-derivative interface for DIRK.
virtual void setupDefault()
Default setup for constructor.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
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.
Solve for x and determine xDot from x.
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
std::string toString(const T &t)