9 #ifndef Tempus_StepperDIRK_impl_hpp
10 #define Tempus_StepperDIRK_impl_hpp
12 #include "Tempus_config.hpp"
14 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
15 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
16 #include "Thyra_VectorStdOps.hpp"
17 #include "NOX_Thyra.H"
23 template<
class Scalar>
class StepperFactory;
25 template<
class Scalar>
28 this->setUseFSAL( this->getUseFSALDefault());
29 this->setICConsistency( this->getICConsistencyDefault());
30 this->setICConsistencyCheck( this->getICConsistencyCheckDefault());
31 this->setUseEmbedded( this->getUseEmbeddedDefault());
32 this->setZeroInitialGuess(
false);
34 this->setStageNumber(-1);
36 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
39 this->setAppAction(Teuchos::null);
40 this->setDefaultSolver();
44 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
45 template<
class Scalar>
47 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel,
49 const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
51 std::string ICConsistency,
52 bool ICConsistencyCheck,
54 bool zeroInitialGuess)
56 this->setUseFSAL( useFSAL);
57 this->setICConsistency( ICConsistency);
58 this->setICConsistencyCheck( ICConsistencyCheck);
59 this->setUseEmbedded( useEmbedded);
60 this->setZeroInitialGuess( zeroInitialGuess);
62 this->setStageNumber(-1);
65 this->setObserver(obs);
66 this->setAppAction(Teuchos::null);
67 this->setSolver(solver);
69 if (appModel != Teuchos::null) {
70 this->setModel(appModel);
75 template<
class Scalar>
77 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel,
78 const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
80 std::string ICConsistency,
81 bool ICConsistencyCheck,
83 bool zeroInitialGuess,
86 this->setUseFSAL( useFSAL);
87 this->setICConsistency( ICConsistency);
88 this->setICConsistencyCheck( ICConsistencyCheck);
89 this->setUseEmbedded( useEmbedded);
90 this->setZeroInitialGuess( zeroInitialGuess);
92 this->setStageNumber(-1);
94 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
96 this->setObserver(Teuchos::null);
98 this->setAppAction(stepperRKAppAction);
99 this->setSolver(solver);
101 if (appModel != Teuchos::null) {
102 this->setModel(appModel);
108 template<
class Scalar>
110 Teuchos::RCP<Teuchos::ParameterList> pl)
const
113 pl->set<
bool>(
"Use Embedded",
false,
114 "'Whether to use Embedded Stepper (if available) or not\n"
115 " 'true' - Stepper will compute embedded solution and is adaptive.\n"
116 " 'false' - Stepper is not embedded(adaptive).\n");
117 pl->set<std::string>(
"Description", this->getDescription());
118 pl->set<std::string>(
"Solver Name",
"Default Solver",
119 "Name of ParameterList containing the solver specifications.");
120 pl->set<
bool>(
"Zero Initial Guess",
false);
121 pl->set<
bool>(
"Reset Initial Guess",
true);
123 pl->set(
"Default Solver", *solverPL);
127 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
128 template<
class Scalar>
132 if (this->stepperObserver_ == Teuchos::null)
133 this->stepperObserver_ =
136 if (( obs == Teuchos::null ) and (this->stepperObserver_->getSize() >0 ) )
139 if (( obs == Teuchos::null ) and (this->stepperObserver_->getSize() == 0) )
144 this->stepperObserver_->addObserver(
147 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
148 Teuchos::OSTab ostab(out,0,
"setObserver");
149 *out <<
"Tempus::StepperIMEX_RK::setObserver: Warning: An observer has been provided that";
150 *out <<
" does not support Tempus::StepperRKObserver. This observer WILL NOT be added.";
151 *out <<
" In the future, this will result in a runtime error!" << std::endl;
154 this->isInitialized_ =
false;
159 template<
class Scalar>
163 const int numStages = this->tableau_->numStages();
164 this->stageX_ = this->wrapperModel_->getNominalValues().get_x()->clone_v();
165 stageXDot_.resize(numStages);
166 for (
int i=0; i<numStages; ++i) {
167 stageXDot_[i] = Thyra::createMember(this->wrapperModel_->get_f_space());
168 assign(stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
170 xTilde_ = Thyra::createMember(this->wrapperModel_->get_x_space());
171 assign(xTilde_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
173 if (this->tableau_->isEmbedded() and this->getUseEmbedded()) {
174 ee_ = Thyra::createMember(this->wrapperModel_->get_f_space());
175 abs_u0 = Thyra::createMember(this->wrapperModel_->get_f_space());
176 abs_u = Thyra::createMember(this->wrapperModel_->get_f_space());
177 sc = Thyra::createMember(this->wrapperModel_->get_f_space());
184 template<
class Scalar>
190 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
193 if (initialState->getXDot() == Teuchos::null)
194 this->setStepperXDot(stageXDot_.back());
200 template<
class Scalar>
204 this->checkInitialized();
208 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperDIRK::takeStep()");
210 TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
212 "Error - StepperDIRK<Scalar>::takeStep(...)\n"
213 "Need at least two SolutionStates for DIRK.\n"
214 " Number of States = " << solutionHistory->getNumStates() <<
"\n"
215 "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
216 " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
218 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
219 this->stepperObserver_->observeBeginTakeStep(solutionHistory, *
this);
221 RCP<StepperDIRK<Scalar> > thisStepper = Teuchos::rcpFromRef(*
this);
222 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
225 RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
226 RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
227 const Scalar dt = workingState->getTimeStep();
228 const Scalar time = currentState->getTime();
230 const int numStages = this->tableau_->numStages();
231 Teuchos::SerialDenseMatrix<int,Scalar> A = this->tableau_->A();
232 Teuchos::SerialDenseVector<int,Scalar> b = this->tableau_->b();
233 Teuchos::SerialDenseVector<int,Scalar> c = this->tableau_->c();
236 if ( this->getResetInitialGuess() && (!this->getZeroInitialGuess()) )
237 Thyra::assign(this->stageX_.ptr(), *(currentState->getX()));
241 Thyra::SolveStatus<Scalar> sStatus;
242 for (
int i=0; i < numStages; ++i) {
243 this->setStageNumber(i);
244 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
245 this->stepperObserver_->observeBeginStage(solutionHistory, *
this);
248 this->stepperObserver_
249 ->observeBeforeImplicitExplicitly(solutionHistory, *
this);
253 bool isNeeded =
false;
254 for (
int k=i+1; k<numStages; ++k)
if (A(k,i) != 0.0) isNeeded =
true;
255 if (b(i) != 0.0) isNeeded =
true;
256 if (this->tableau_->isEmbedded() && this->getUseEmbedded() &&
257 this->tableau_->bstar()(i) != 0.0)
259 if (isNeeded ==
false) {
260 assign(stageXDot_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
264 Thyra::assign(xTilde_.ptr(), *(currentState->getX()));
265 for (
int j=0; j < i; ++j) {
266 if (A(i,j) != Teuchos::ScalarTraits<Scalar>::zero()) {
267 Thyra::Vp_StV(xTilde_.ptr(), dt*A(i,j), *(stageXDot_[j]));
271 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
274 Scalar ts = time + c(i)*dt;
275 if (A(i,i) == Teuchos::ScalarTraits<Scalar>::zero()) {
277 if (i == 0 && this->getUseFSAL() &&
278 workingState->getNConsecutiveFailures() == 0) {
280 RCP<Thyra::VectorBase<Scalar> > tmp = stageXDot_[0];
281 stageXDot_[0] = stageXDot_.back();
282 stageXDot_.back() = tmp;
285 typedef Thyra::ModelEvaluatorBase MEB;
286 MEB::InArgs<Scalar> inArgs = this->wrapperModel_->getInArgs();
287 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
288 inArgs.set_x(xTilde_);
289 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(ts);
290 if (inArgs.supports(MEB::IN_ARG_x_dot))
291 inArgs.set_x_dot(Teuchos::null);
292 outArgs.set_f(stageXDot_[i]);
294 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
295 this->stepperObserver_->observeBeforeExplicit(solutionHistory, *
this);
297 this->wrapperModel_->getAppModel()->evalModel(inArgs,outArgs);
301 const Scalar alpha = 1.0/(dt*A(i,i));
302 const Scalar beta = 1.0;
305 Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
307 alpha,xTilde_.getConst()));
312 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
313 this->stepperObserver_->observeBeforeSolve(solutionHistory, *
this);
315 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
318 sStatus = this->solveImplicitODE(this->stageX_, stageXDot_[i], ts, p);
320 if (sStatus.solveStatus != Thyra::SOLVE_STATUS_CONVERGED) pass=
false;
322 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
323 this->stepperObserver_->observeAfterSolve(solutionHistory, *
this);
325 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
328 timeDer->compute(this->stageX_, stageXDot_[i]);
330 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
331 this->stepperObserver_->observeEndStage(solutionHistory, *
this);
333 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
335 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
340 Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
341 for (
int i=0; i < numStages; ++i) {
342 if (b(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
343 Thyra::Vp_StV((workingState->getX()).ptr(), dt*b(i), *(stageXDot_[i]));
347 if (this->tableau_->isEmbedded() and this->getUseEmbedded()) {
348 const Scalar tolRel = workingState->getTolRel();
349 const Scalar tolAbs = workingState->getTolAbs();
353 Teuchos::SerialDenseVector<int,Scalar> errWght = b ;
354 errWght -= this->tableau_->bstar();
358 assign(ee_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
359 for (
int i=0; i < numStages; ++i) {
360 if (errWght(i) != Teuchos::ScalarTraits<Scalar>::zero()) {
361 Thyra::Vp_StV(ee_.ptr(), dt*errWght(i), *(stageXDot_[i]));
366 Thyra::abs( *(currentState->getX()), abs_u0.ptr());
367 Thyra::abs( *(workingState->getX()), abs_u.ptr());
368 Thyra::pair_wise_max_update(tolRel, *abs_u0, abs_u.ptr());
369 Thyra::add_scalar(tolAbs, abs_u.ptr());
372 assign(sc.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
373 Thyra::ele_wise_divide(Teuchos::as<Scalar>(1.0), *ee_, *abs_u, sc.ptr());
374 Scalar err = std::abs(Thyra::norm_inf(*sc));
375 workingState->setErrorRel(err);
378 if (std::isinf(err) || std::isnan(err) || err > Teuchos::as<Scalar>(1.0))
385 workingState->setOrder(this->getOrder());
386 workingState->computeNorms(currentState);
387 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
388 this->stepperObserver_->observeEndTakeStep(solutionHistory, *
this);
390 this->stepperRKAppAction_->execute(solutionHistory, thisStepper,
394 this->setStageNumber(-1);
404 template<
class Scalar>
405 Teuchos::RCP<Tempus::StepperState<Scalar> >
409 Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
415 template<
class Scalar>
417 Teuchos::FancyOStream &out,
418 const Teuchos::EVerbosityLevel verbLevel)
const
424 out <<
"--- StepperDIRK ---\n";
425 out <<
" tableau_ = " << this->tableau_ << std::endl;
426 if (this->tableau_ != Teuchos::null) this->tableau_->describe(out, verbLevel);
427 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
428 out <<
" stepperObserver_ = " << stepperObserver_ << std::endl;
430 out <<
" stepperRKAppAction_= " << this->stepperRKAppAction_ << std::endl;
431 out <<
" xTilde_ = " << xTilde_ << std::endl;
432 out <<
" stageX_ = " << this->stageX_ << std::endl;
433 out <<
" stageXDot_.size() = " << stageXDot_.size() << std::endl;
434 const int numStages = stageXDot_.size();
435 for (
int i=0; i<numStages; ++i)
436 out <<
" stageXDot_["<<i<<
"] = " << stageXDot_[i] << std::endl;
437 out <<
" useEmbedded_ = "
439 out <<
" ee_ = " << ee_ << std::endl;
440 out <<
" abs_u0 = " << abs_u0 << std::endl;
441 out <<
" abs_u = " << abs_u << std::endl;
442 out <<
" sc = " << sc << std::endl;
443 out <<
"-------------------" << std::endl;
447 template<
class Scalar>
450 bool isValidSetup =
true;
455 if (this->tableau_ == Teuchos::null) {
456 isValidSetup =
false;
457 out <<
"The tableau is not set!\n";
460 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
461 if (stepperObserver_ == Teuchos::null) {
462 isValidSetup =
false;
463 out <<
"The observer is not set!\n";
466 if (this->stepperRKAppAction_ == Teuchos::null) {
467 isValidSetup =
false;
468 out <<
"The AppAction is not set!\n";
475 template<
class Scalar>
476 Teuchos::RCP<const Teuchos::ParameterList>
479 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
480 this->getValidParametersBasicDIRK(pl);
487 #endif // Tempus_StepperDIRK_impl_hpp
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
const std::string toString(const Status status)
Convert Status to string.
virtual void setup(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &wrapperModel, const Teuchos::RCP< StepperRKObserver< Scalar > > &obs, const Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > &solver, bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck, bool useEmbedded, bool zeroInitialGuess)
Setup for constructor.
void getValidParametersBasicDIRK(Teuchos::RCP< Teuchos::ParameterList > pl) const
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
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.
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.
StepperObserver class for Stepper class.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
virtual void setObserver(Teuchos::RCP< StepperObserver< Scalar > > obs=Teuchos::null)
Set Observer.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
This observer is a composite observer,.
StepperRKObserver class for StepperRK.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Solve for x and determine xDot from x.
void getValidParametersBasic(Teuchos::RCP< Teuchos::ParameterList > pl, std::string stepperType)
Provide basic parameters to Steppers.
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.