9 #ifndef Tempus_StepperBDF2_impl_hpp
10 #define Tempus_StepperBDF2_impl_hpp
12 #include "Tempus_config.hpp"
14 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
15 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
16 #include "NOX_Thyra.H"
22 template<
class Scalar>
class StepperFactory;
26 template<
class Scalar>
28 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel,
29 Teuchos::RCP<Teuchos::ParameterList> pList)
32 using Teuchos::ParameterList;
35 this->setParameterList(pList);
36 this->setModel(appModel);
47 template<
class Scalar>
51 using Teuchos::ParameterList;
53 RCP<ParameterList> startupStepperPL =
54 Teuchos::sublist(this->stepperPL_, startupStepperName,
true);
55 this->stepperPL_->set(
"Start Up Stepper Name", startupStepperName);
58 sf->createStepper(this->wrapperModel_->getAppModel(), startupStepperPL);
68 template<
class Scalar>
70 Teuchos::RCP<Teuchos::ParameterList> startupStepperPL)
73 using Teuchos::ParameterList;
75 Teuchos::RCP<Teuchos::ParameterList> stepperPL = this->stepperPL_;
76 std::string startupStepperName =
77 stepperPL->get<std::string>(
"Start Up Stepper Name",
"None");
78 if (is_null(startupStepperPL)) {
80 if (startUpStepper_ == Teuchos::null) {
81 if (startupStepperName !=
"None") {
83 RCP<ParameterList> startupStepperPL =
84 Teuchos::sublist(this->stepperPL_, startupStepperName,
true);
85 RCP<StepperFactory<Scalar> > sf =
88 sf->createStepper(this->wrapperModel_->getAppModel(), startupStepperPL);
91 RCP<StepperFactory<Scalar> > sf =
94 sf->createStepper(this->wrapperModel_->getAppModel(),
95 "IRK 1 Stage Theta Method");
97 startupStepperName = startUpStepper_->description();
98 startupStepperPL = startUpStepper_->getNonconstParameterList();
99 this->stepperPL_->set(
"Start Up Stepper Name", startupStepperName);
100 this->stepperPL_->set(startupStepperName, *startupStepperPL);
104 TEUCHOS_TEST_FOR_EXCEPTION( startupStepperName == startupStepperPL->name(),
106 "Error - Trying to add a startup stepper that is already in "
107 <<
"ParameterList!\n"
108 <<
" Stepper Type = "<< stepperPL->get<std::string>(
"Stepper Type")
109 <<
"\n" <<
" Start Up Stepper Name = "<<startupStepperName<<
"\n");
110 startupStepperName = startupStepperPL->name();
111 this->stepperPL_->set(
"Start Up Stepper Name", startupStepperName);
112 this->stepperPL_->set(startupStepperName, *startupStepperPL);
113 RCP<StepperFactory<Scalar> > sf =
116 sf->createStepper(this->wrapperModel_->getAppModel(), startupStepperPL);
121 template<
class Scalar>
125 if (obs == Teuchos::null) {
127 if (stepperObserver_ == Teuchos::null) {
128 stepperBDF2Observer_ =
132 (stepperBDF2Observer_);
135 stepperObserver_ = obs;
136 stepperBDF2Observer_ =
142 template<
class Scalar>
145 TEUCHOS_TEST_FOR_EXCEPTION( this->wrapperModel_ == Teuchos::null,
147 "Error - Need to set the model, setModel(), before calling "
148 "StepperBDF2::initialize()\n");
150 this->setParameterList(this->stepperPL_);
152 this->setStartUpStepper();
158 template<
class Scalar>
164 TEMPUS_FUNC_TIME_MONITOR(
"Tempus::StepperBDF2::takeStep()");
168 RCP<Thyra::VectorBase<Scalar> > xOld;
169 RCP<Thyra::VectorBase<Scalar> > xOldOld;
177 TEUCHOS_TEST_FOR_EXCEPTION( (numStates < 3), std::logic_error,
178 "Error in Tempus::StepperBDF2::takeStep(): numStates after \n"
179 <<
"startup stepper must be at least 3, whereas numStates = "
180 << numStates <<
"!\n" <<
"If running with Storage Type = Static, "
181 <<
"make sure Storage Limit > 2.\n");
188 RCP<SolutionState<Scalar> > workingState=
solutionHistory->getWorkingState();
189 RCP<SolutionState<Scalar> > currentState=
solutionHistory->getCurrentState();
191 RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
192 RCP<Thyra::VectorBase<Scalar> > xDot = workingState->getXDot();
193 if (xDot == Teuchos::null) xDot = getXDotTemp(x);
196 const Scalar time = workingState->getTime();
197 const Scalar dt = workingState->getTimeStep();
198 const Scalar dtOld = currentState->getTimeStep();
204 const Scalar alpha = (1.0/(dt + dtOld))*(1.0/dt)*(2.0*dt + dtOld);
205 const Scalar beta = 1.0;
208 Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
212 typedef Thyra::ModelEvaluatorBase MEB;
213 MEB::InArgs<Scalar> inArgs = this->wrapperModel_->getInArgs();
214 MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
216 if (inArgs.supports(MEB::IN_ARG_x_dot )) inArgs.set_x_dot (xDot);
217 if (inArgs.supports(MEB::IN_ARG_t )) inArgs.set_t (time);
218 if (inArgs.supports(MEB::IN_ARG_step_size)) inArgs.set_step_size(dt);
219 if (inArgs.supports(MEB::IN_ARG_alpha )) inArgs.set_alpha (alpha);
220 if (inArgs.supports(MEB::IN_ARG_beta )) inArgs.set_beta (beta);
222 this->wrapperModel_->setForSolve(timeDer, inArgs, outArgs);
224 if (!Teuchos::is_null(stepperBDF2Observer_))
227 const Thyra::SolveStatus<Scalar> sStatus = this->solveImplicitODE(x);
229 if (!Teuchos::is_null(stepperBDF2Observer_))
232 if (workingState->getXDot() != Teuchos::null)
233 timeDer->compute(x, xDot);
235 if (sStatus.solveStatus == Thyra::SOLVE_STATUS_CONVERGED)
239 workingState->setOrder(this->getOrder());
245 template<
class Scalar>
246 Teuchos::RCP<Thyra::VectorBase<Scalar> >
250 if (xDotTemp_ == Teuchos::null) {
251 xDotTemp_ = x->clone_v();
252 Thyra::assign(xDotTemp_.ptr(), Scalar(0.0));
257 template<
class Scalar>
261 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
262 Teuchos::OSTab ostab(out,1,
"StepperBDF2::computeStartUp()");
263 *out <<
"Warning -- Taking a startup step for BDF2 using '"
264 << startUpStepper_->description()<<
"'!" << std::endl;
269 order_ = startUpStepper_->getOrder();
278 template<
class Scalar>
279 Teuchos::RCP<Tempus::StepperState<Scalar> >
283 Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
289 template<
class Scalar>
292 std::string name =
"BDF2";
297 template<
class Scalar>
299 Teuchos::FancyOStream &out,
300 const Teuchos::EVerbosityLevel verbLevel)
const
302 out << description() <<
"::describe:" << std::endl
303 <<
"wrapperModel_ = " << this->wrapperModel_->description() << std::endl;
307 template <
class Scalar>
309 Teuchos::RCP<Teuchos::ParameterList>
const& pList)
311 Teuchos::RCP<Teuchos::ParameterList> stepperPL = this->stepperPL_;
312 if (pList == Teuchos::null) {
314 if (stepperPL == Teuchos::null) stepperPL = this->getDefaultParameters();
318 if (!(stepperPL->isParameter(
"Solver Name"))) {
319 stepperPL->set<std::string>(
"Solver Name",
"Default Solver");
320 Teuchos::RCP<Teuchos::ParameterList> solverPL =
321 this->defaultSolverParameters();
322 stepperPL->set(
"Default Solver", *solverPL);
327 std::string stepperType = stepperPL->get<std::string>(
"Stepper Type");
328 TEUCHOS_TEST_FOR_EXCEPTION( stepperType !=
"BDF2", std::logic_error,
329 "Error - Stepper Type is not 'BDF2'!\n"
330 <<
" Stepper Type = "<<stepperPL->get<std::string>(
"Stepper Type")<<
"\n");
332 this->stepperPL_ = stepperPL;
336 template<
class Scalar>
337 Teuchos::RCP<const Teuchos::ParameterList>
340 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
341 pl->setName(
"Default Stepper - " + this->description());
342 pl->set(
"Stepper Type", this->description());
343 pl->set(
"Zero Initial Guess",
false);
344 pl->set(
"Solver Name",
"",
345 "Name of ParameterList containing the solver specifications.");
351 template<
class Scalar>
352 Teuchos::RCP<Teuchos::ParameterList>
356 using Teuchos::ParameterList;
358 RCP<ParameterList> pl = Teuchos::parameterList();
359 pl->setName(
"Default Stepper - " + this->description());
360 pl->set<std::string>(
"Stepper Type", this->description());
361 pl->set<
bool> (
"Zero Initial Guess",
false);
362 pl->set<std::string>(
"Solver Name",
"Default Solver");
364 RCP<ParameterList> solverPL = this->defaultSolverParameters();
365 pl->set(
"Default Solver", *solverPL);
371 template <
class Scalar>
372 Teuchos::RCP<Teuchos::ParameterList>
375 return(this->stepperPL_);
379 template <
class Scalar>
380 Teuchos::RCP<Teuchos::ParameterList>
383 Teuchos::RCP<Teuchos::ParameterList> temp_plist = this->stepperPL_;
384 this->stepperPL_ = Teuchos::null;
390 #endif // Tempus_StepperBDF2_impl_hpp