10 #ifndef Tempus_StepperImplicit_impl_hpp
11 #define Tempus_StepperImplicit_impl_hpp
14 #include "NOX_Thyra.H"
18 template <
class Scalar>
27 "Error - Solver is not set!\n");
28 solver_->setModel(wrapperModel_);
30 this->isInitialized_ =
false;
33 template <
class Scalar>
39 int numStates = solutionHistory->getNumStates();
42 numStates < 1, std::logic_error,
43 "Error - setInitialConditions() needs at least one SolutionState\n"
44 " to set the initial condition. Number of States = "
47 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
48 RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
49 RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
50 if (xDot() == Teuchos::null) xDot = this->getStepperXDot();
52 auto inArgs = this->wrapperModel_->getNominalValues();
54 RCP<Thyra::VectorBase<Scalar> > initialXDot = initialState->getXDot();
58 !((x != Teuchos::null && initialXDot != Teuchos::null) ||
59 (inArgs.get_x() != Teuchos::null &&
60 inArgs.get_x_dot() != Teuchos::null)),
62 "Error - We need to set the initial conditions for x and xDot from\n"
63 " either initialState or appModel_->getNominalValues::InArgs\n"
64 " (but not from a mixture of the two).\n");
69 if (x == Teuchos::null) {
71 (x == Teuchos::null) && (inArgs.get_x() == Teuchos::null),
73 "Error - setInitialConditions needs the ICs from the "
75 " or getNominalValues()!\n");
78 initialState->setX(x);
83 using Teuchos::rcp_const_cast;
84 if (x == Teuchos::null || xDot == Teuchos::null) {
86 (inArgs.get_x() == Teuchos::null) ||
87 (inArgs.get_x_dot() == Teuchos::null),
89 "Error - setInitialConditions() needs the ICs from the initialState\n"
90 " or getNominalValues()!\n");
92 initialState->setX(x);
93 RCP<Thyra::VectorBase<Scalar> > x_dot =
95 initialState->setXDot(x_dot);
100 std::string icConsistency = this->getICConsistency();
101 if (icConsistency ==
"None") {
103 if (initialState->getXDot() == Teuchos::null)
104 Thyra::assign(xDot.ptr(), Scalar(0.0));
107 if (initialState->getXDotDot() == Teuchos::null)
108 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
111 else if (icConsistency ==
"Zero") {
113 Thyra::assign(xDot.ptr(), Scalar(0.0));
115 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
117 else if (icConsistency ==
"App") {
122 x_dot == Teuchos::null, std::logic_error,
123 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
124 " but 'App' returned a null pointer for xDot!\n");
125 Thyra::assign(xDot.ptr(), *x_dot);
129 inArgs.get_x_dot_dot());
131 xDotDot == Teuchos::null, std::logic_error,
132 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
133 " but 'App' returned a null pointer for xDotDot!\n");
134 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), *xDotDot);
137 else if (icConsistency ==
"Consistent") {
140 const Scalar time = initialState->getTime();
141 const Scalar dt = initialState->getTimeStep();
142 RCP<TimeDerivative<Scalar> > timeDer = Teuchos::null;
143 const Scalar alpha = Scalar(1.0);
144 const Scalar beta = Scalar(0.0);
149 this->solveImplicitODE(x, xDot, time, p);
154 "Error - Solver failed while determining the initial conditions.\n"
160 true, std::logic_error,
161 "Error - setInitialConditions(): 'Consistent' for second-order ODE\n"
162 " has not been implemented.\n");
167 true, std::logic_error,
168 "Error - setInitialConditions() invalid IC consistency, "
169 << icConsistency <<
".\n");
174 initialState->setIsSynced(
true);
177 if (this->getICConsistencyCheck()) {
178 auto f = initialState->getX()->clone_v();
180 const Scalar time = initialState->getTime();
181 const Scalar dt = initialState->getTimeStep();
182 RCP<TimeDerivative<Scalar> > timeDer = Teuchos::null;
183 const Scalar alpha = Scalar(0.0);
184 const Scalar beta = Scalar(0.0);
188 this->evaluateImplicitODE(f, x, xDot, time, p);
190 Scalar normX = Thyra::norm(*x);
191 Scalar reldiff = Scalar(0.0);
192 if (normX == Scalar(0.0))
193 reldiff = Thyra::norm(*f);
195 reldiff = Thyra::norm(*f) / normX;
198 RCP<Teuchos::FancyOStream> out = this->getOStream();
199 Teuchos::OSTab ostab(out, 1,
"StepperImplicit::setInitialConditions()");
201 *out <<
"\n---------------------------------------------------\n"
202 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
203 <<
" Initial condition PASSED consistency check!\n"
204 <<
" (||f(x,xDot,t)||/||x|| = " << reldiff <<
") < "
205 <<
"(eps = " << eps <<
")" << std::endl
206 <<
"---------------------------------------------------\n"
210 *out <<
"\n---------------------------------------------------\n"
211 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
212 <<
" Initial condition FAILED consistency check but continuing!\n"
213 <<
" (||f(x,xDot,t)||/||x|| = " << reldiff <<
") > "
214 <<
"(eps = " << eps <<
")" << std::endl
215 <<
" ||f(x,xDot,t)|| = " << Thyra::norm(*f) << std::endl
216 <<
" ||x|| = " << Thyra::norm(*x) << std::endl
217 <<
"---------------------------------------------------\n"
223 template <
class Scalar>
228 this->setSolverName(
"Default Solver");
229 auto subPL = sublist(solverPL,
"NOX");
230 solver_->setParameterList(subPL);
232 if (wrapperModel_ != Teuchos::null) solver_->setModel(wrapperModel_);
234 this->isInitialized_ =
false;
237 template <
class Scalar>
242 solver == Teuchos::null, std::logic_error,
243 "Error - Can not set the solver to Teuchos::null.\n");
246 if (wrapperModel_ != Teuchos::null) solver_->setModel(wrapperModel_);
248 this->isInitialized_ =
false;
251 template <
class Scalar>
258 wrapperModel_->setForSolve(x, xDot, time, p, y, index);
262 switch (p->evaluationType_) {
264 if (getZeroInitialGuess()) Thyra::assign(x.ptr(), ST::zero());
265 sStatus = (*solver_).solve(&*x);
270 sStatus = (*solver_).solve(&*xDot);
281 template <
class Scalar>
289 MEB::InArgs<Scalar> inArgs = wrapperModel_->createInArgs();
291 if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(xDot);
292 if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time);
293 if (inArgs.supports(MEB::IN_ARG_step_size))
294 inArgs.set_step_size(p->timeStepSize_);
295 if (inArgs.supports(MEB::IN_ARG_alpha)) inArgs.set_alpha(Scalar(0.0));
296 if (inArgs.supports(MEB::IN_ARG_beta)) inArgs.set_beta(Scalar(0.0));
298 MEB::OutArgs<Scalar> outArgs = wrapperModel_->createOutArgs();
301 wrapperModel_->setForSolve(x, xDot, time, p);
303 wrapperModel_->evalModel(inArgs, outArgs);
306 template <
class Scalar>
311 out <<
"--- StepperImplicit ---\n";
312 out <<
" wrapperModel_ = " << wrapperModel_ << std::endl;
313 out <<
" solver_ = " << solver_ << std::endl;
314 out <<
" initialGuess_ = " << initialGuess_ << std::endl;
319 template <
class Scalar>
323 bool isValidSetup =
true;
325 if (wrapperModel_->getAppModel() == Teuchos::null) {
326 isValidSetup =
false;
327 out <<
"The application ModelEvaluator is not set!\n";
330 if (wrapperModel_ == Teuchos::null) {
331 isValidSetup =
false;
332 out <<
"The wrapper ModelEvaluator is not set!\n";
335 if (solver_ == Teuchos::null) {
336 isValidSetup =
false;
337 out <<
"The solver is not set!\n";
340 if (solver_ != Teuchos::null) {
341 if (solver_->getModel() == Teuchos::null) {
342 isValidSetup =
false;
343 out <<
"The solver's ModelEvaluator is not set!\n";
350 template <
class Scalar>
354 return this->getValidParametersBasicImplicit();
357 template <
class Scalar>
361 auto pl = this->getValidParametersBasic();
362 pl->template set<std::string>(
"Solver Name", this->getSolverName());
363 pl->template set<bool>(
"Zero Initial Guess", this->getZeroInitialGuess());
364 auto noxSolverPL = this->getSolver()->getParameterList();
365 auto solverPL = Teuchos::parameterList(this->getSolverName());
366 solverPL->set(
"NOX", *noxSolverPL);
367 pl->set(this->getSolverName(), *solverPL);
372 template <
class Scalar>
376 if (pl != Teuchos::null) {
379 this->setStepperValues(pl);
380 this->setZeroInitialGuess(pl->
get<
bool>(
"Zero Initial Guess",
false));
382 this->setStepperSolverValues(pl);
385 template <
class Scalar>
389 if (pl != Teuchos::null) {
391 std::string solverName = pl->
get<std::string>(
"Solver Name");
393 auto solverPL = Teuchos::parameterList();
394 solverPL = Teuchos::sublist(pl, solverName);
395 this->setSolverName(solverName);
397 Teuchos::sublist(solverPL,
"NOX",
true);
398 getSolver()->setParameterList(noxPL);
404 #endif // Tempus_StepperImplicit_impl_hpp
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
virtual void setSolver(Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > solver) override
Set solver.
Evaluate residual for the implicit ODE.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const override
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void setStepperImplicitValues(Teuchos::RCP< Teuchos::ParameterList > pl)
Set StepperImplicit member data from the ParameterList.
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
const Thyra::SolveStatus< Scalar > solveImplicitODE(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xDot, const Scalar time, const Teuchos::RCP< ImplicitODEParameters< Scalar > > &p, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &y=Teuchos::null, const int index=-1)
Solve implicit ODE, f(x, xDot, t, p) = 0.
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.
void evaluateImplicitODE(Teuchos::RCP< Thyra::VectorBase< Scalar > > &f, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xDot, const Scalar time, const Teuchos::RCP< ImplicitODEParameters< Scalar > > &p)
Evaluate implicit ODE residual, f(x, xDot, t, p).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Stepper integrates second-order ODEs.
bool isSublist(const std::string &name) const
virtual void setDefaultSolver()
TEUCHOSCORE_LIB_DLL_EXPORT std::string toString(const EVerbosityLevel verbLevel)
void setStepperSolverValues(Teuchos::RCP< Teuchos::ParameterList > pl)
Set solver from ParameterList.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
Solve for xDot keeping x constant (for ICs).
Stepper integrates first-order ODEs.
Teuchos::RCP< Teuchos::ParameterList > getValidParametersBasicImplicit() const
void validImplicitODE_DAE(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Validate ME supports implicit ODE/DAE evaluation, f(xdot,x,t) [= 0].
A ModelEvaluator for residual evaluations given a state. This ModelEvaluator takes a state...
Solve for x and determine xDot from x.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
std::string toString(const T &t)