9 #ifndef Tempus_StepperExplicit_impl_hpp
10 #define Tempus_StepperExplicit_impl_hpp
12 #include "Thyra_VectorStdOps.hpp"
16 template <
class Scalar>
23 inArgs_ = appModel_->getNominalValues();
24 outArgs_ = appModel_->createOutArgs();
27 template <
class Scalar>
33 int numStates = solutionHistory->getNumStates();
36 numStates < 1, std::logic_error,
37 "Error - setInitialConditions() needs at least one SolutionState\n"
38 " to set the initial condition. Number of States = "
42 RCP<Teuchos::FancyOStream> out = this->getOStream();
43 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
44 *out <<
"Warning -- SolutionHistory has more than one state!\n"
45 <<
"Setting the initial conditions on the currentState.\n"
49 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
50 RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
51 RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
52 if (xDot == Teuchos::null) xDot = this->getStepperXDot();
54 inArgs_ = appModel_->getNominalValues();
56 RCP<Thyra::VectorBase<Scalar> > initialXDot = initialState->getXDot();
60 !((x != Teuchos::null && initialXDot != Teuchos::null) ||
61 (inArgs_.get_x() != Teuchos::null &&
62 inArgs_.get_x_dot() != Teuchos::null)),
64 "Error - We need to set the initial conditions for x and xDot from\n"
65 " either initialState or appModel_->getNominalValues::InArgs\n"
66 " (but not from a mixture of the two).\n");
70 if (x == Teuchos::null) {
73 (x == Teuchos::null) && (inArgs_.get_x() == Teuchos::null),
75 "Error - setInitialConditions needs the ICs from the "
77 " or getNominalValues()!\n");
80 initialState->setX(x);
84 using Teuchos::rcp_const_cast;
86 if (initialState->getX() == Teuchos::null ||
87 initialState->getXDot() == Teuchos::null) {
89 (inArgs_.get_x() == Teuchos::null) ||
90 (inArgs_.get_x_dot() == Teuchos::null),
92 "Error - setInitialConditions() needs the ICs from the initialState\n"
93 " or getNominalValues()!\n");
95 initialState->setX(x);
96 RCP<Thyra::VectorBase<Scalar> > x_dot =
98 initialState->setXDot(x_dot);
103 std::string icConsistency = this->getICConsistency();
104 if (icConsistency ==
"None") {
106 if (initialState->getXDot() == Teuchos::null) {
107 RCP<Teuchos::FancyOStream> out = this->getOStream();
108 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
109 *out <<
"Warning -- Requested IC consistency of 'None' but\n"
110 <<
" initialState does not have an xDot.\n"
111 <<
" Setting a 'Consistent' xDot!\n"
114 evaluateExplicitODE(xDot, x, initialState->getTime(), p);
115 initialState->setIsSynced(
true);
119 if (initialState->getXDotDot() == Teuchos::null) {
120 RCP<Teuchos::FancyOStream> out = this->getOStream();
121 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
122 *out <<
"Warning -- Requested IC consistency of 'None' but\n"
123 <<
" initialState does not have an xDotDot.\n"
124 <<
" Setting a 'Consistent' xDotDot!\n"
127 this->evaluateExplicitODE(this->getStepperXDotDot(initialState), x,
128 initialState->getXDot(),
129 initialState->getTime(), p);
130 initialState->setIsSynced(
true);
134 else if (icConsistency ==
"Zero") {
136 Thyra::assign(xDot.ptr(), Scalar(0.0));
138 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
140 else if (icConsistency ==
"App") {
143 inArgs_.get_x_dot());
145 xDot == Teuchos::null, std::logic_error,
146 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
147 " but 'App' returned a null pointer for xDot!\n");
148 Thyra::assign(xDot.ptr(), *x_dot);
152 inArgs_.get_x_dot_dot());
154 xDotDot == Teuchos::null, std::logic_error,
155 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
156 " but 'App' returned a null pointer for xDotDot!\n");
157 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), *xDotDot);
160 else if (icConsistency ==
"Consistent") {
164 evaluateExplicitODE(xDot, x, initialState->getTime(), p);
169 this->evaluateExplicitODE(initialState->getXDotDot(), x,
170 initialState->getXDot(),
171 initialState->getTime(), p);
176 true, std::logic_error,
177 "Error - setInitialConditions() invalid IC consistency, '"
178 << icConsistency <<
"'.\n");
183 initialState->setIsSynced(
true);
186 if (this->getICConsistencyCheck()) {
188 auto f = initialState->getX()->clone_v();
190 evaluateExplicitODE(f, x, initialState->getTime(), p);
191 Thyra::Vp_StV(f.ptr(), Scalar(-1.0), *(xDot));
192 Scalar normX = Thyra::norm(*x);
193 Scalar reldiff = Scalar(0.0);
194 if (normX == Scalar(0.0))
195 reldiff = Thyra::norm(*f);
197 reldiff = Thyra::norm(*f) / normX;
201 RCP<Teuchos::FancyOStream> out = this->getOStream();
202 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
204 *out <<
"\n---------------------------------------------------\n"
205 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
206 <<
" Initial condition PASSED consistency check!\n"
207 <<
" (||xDot-f(x,t)||/||x|| = " << reldiff <<
") < "
208 <<
"(eps = " << eps <<
")" << std::endl
209 <<
"---------------------------------------------------\n"
213 *out <<
"\n---------------------------------------------------\n"
214 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
215 <<
" Initial condition FAILED consistency check but continuing!\n"
216 <<
" (||xDot-f(x,t)||/||x|| = " << reldiff <<
") > "
217 <<
"(eps = " << eps <<
")" << std::endl
218 <<
" ||xDot-f(x,t)|| = " << Thyra::norm(*f) << std::endl
219 <<
" ||x|| = " << Thyra::norm(*x) << std::endl
220 <<
"---------------------------------------------------\n"
225 auto xDotDot = initialState->getXDotDot();
226 auto f = initialState->getX()->clone_v();
228 this->evaluateExplicitODE(f, x, initialState->getXDot(),
229 initialState->getTime(), p);
230 Thyra::Vp_StV(f.ptr(), Scalar(-1.0), *(xDotDot));
231 Scalar normX = Thyra::norm(*x);
232 Scalar reldiff = Scalar(0.0);
233 if (normX == Scalar(0.0))
234 reldiff = Thyra::norm(*f);
236 reldiff = Thyra::norm(*f) / normX;
240 RCP<Teuchos::FancyOStream> out = this->getOStream();
241 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
243 *out <<
"\n---------------------------------------------------\n"
244 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
245 <<
" Initial condition PASSED consistency check!\n"
246 <<
" (||xDotDot-f(x,xDot,t)||/||x|| = " << reldiff <<
") > "
247 <<
"(eps = " << eps <<
")" << std::endl
248 <<
"---------------------------------------------------\n"
252 *out <<
"\n---------------------------------------------------\n"
253 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
254 <<
"Initial condition FAILED consistency check but continuing!\n"
255 <<
" (||xDotDot-f(x,xDot,t)||/||x|| = " << reldiff <<
") > "
256 <<
"(eps = " << eps <<
")" << std::endl
257 <<
" ||xDotDot-f(x,xDot,t)|| = " << Thyra::norm(*f) << std::endl
258 <<
" ||x|| = " << Thyra::norm(*x) << std::endl
259 <<
"---------------------------------------------------\n"
266 template <
class Scalar>
272 *out <<
"Warning -- No solver to set for StepperExplicit "
273 <<
"(i.e., explicit method).\n"
277 template <
class Scalar>
286 if (inArgs_.supports(MEB::IN_ARG_t)) inArgs_.set_t(time);
287 if (inArgs_.supports(MEB::IN_ARG_step_size))
288 inArgs_.set_step_size(p->timeStepSize_);
289 if (inArgs_.supports(MEB::IN_ARG_stage_number))
290 inArgs_.set_stage_number(p->stageNumber_);
297 if (inArgs_.supports(MEB::IN_ARG_x_dot)) inArgs_.set_x_dot(Teuchos::null);
299 outArgs_.set_f(xDot);
301 appModel_->evalModel(inArgs_, outArgs_);
304 template <
class Scalar>
314 if (inArgs_.supports(MEB::IN_ARG_x_dot)) inArgs_.set_x_dot(xDot);
315 if (inArgs_.supports(MEB::IN_ARG_t)) inArgs_.set_t(time);
316 if (inArgs_.supports(MEB::IN_ARG_step_size))
317 inArgs_.set_step_size(p->timeStepSize_);
318 if (inArgs_.supports(MEB::IN_ARG_stage_number))
319 inArgs_.set_stage_number(p->stageNumber_);
326 if (inArgs_.supports(MEB::IN_ARG_x_dot_dot))
327 inArgs_.set_x_dot_dot(Teuchos::null);
329 outArgs_.set_f(xDotDot);
331 appModel_->evalModel(inArgs_, outArgs_);
334 template <
class Scalar>
338 auto l_out = Teuchos::fancyOStream(out.
getOStream());
340 l_out->setOutputToRootOnly(0);
342 *l_out <<
"--- StepperExplicit ---\n"
343 <<
" appModel_ = " << appModel_ << std::endl
344 <<
" inArgs_ = " << inArgs_ << std::endl
345 <<
" outArgs_ = " << outArgs_ << std::endl;
348 template <
class Scalar>
352 bool isValidSetup =
true;
354 if (appModel_ == Teuchos::null) {
355 isValidSetup =
false;
356 out <<
"The application ModelEvaluator is not set!\n";
362 template <
class Scalar>
366 if (pl != Teuchos::null) {
368 this->setStepperValues(pl);
373 #endif // Tempus_StepperExplicit_impl_hpp
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void validExplicitODE(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Validate that the model supports explicit ODE evaluation, f(x,t) [=xdot].
virtual void setSolver(Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > solver)
Set solver.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Stepper integrates second-order ODEs.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions, make them consistent, and set needed memory.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Set model.
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
Stepper integrates first-order ODEs.
virtual void evaluateExplicitODE(Teuchos::RCP< Thyra::VectorBase< Scalar > > xDot, Teuchos::RCP< const Thyra::VectorBase< Scalar > > x, const Scalar time, const Teuchos::RCP< ExplicitODEParameters< Scalar > > &p)
Evaluate xDot = f(x,t).
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
void setStepperExplicitValues(Teuchos::RCP< Teuchos::ParameterList > pl)
Set StepperExplicit member data from the ParameterList.