10 #ifndef Tempus_StepperExplicit_impl_hpp
11 #define Tempus_StepperExplicit_impl_hpp
13 #include "Thyra_VectorStdOps.hpp"
17 template <
class Scalar>
24 inArgs_ = appModel_->getNominalValues();
25 outArgs_ = appModel_->createOutArgs();
28 template <
class Scalar>
34 int numStates = solutionHistory->getNumStates();
37 numStates < 1, std::logic_error,
38 "Error - setInitialConditions() needs at least one SolutionState\n"
39 " to set the initial condition. Number of States = "
43 RCP<Teuchos::FancyOStream> out = this->getOStream();
44 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
45 *out <<
"Warning -- SolutionHistory has more than one state!\n"
46 <<
"Setting the initial conditions on the currentState.\n"
50 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
51 RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
52 RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
53 if (xDot == Teuchos::null) xDot = this->getStepperXDot();
55 inArgs_ = appModel_->getNominalValues();
57 RCP<Thyra::VectorBase<Scalar> > initialXDot = initialState->getXDot();
61 !((x != Teuchos::null && initialXDot != Teuchos::null) ||
62 (inArgs_.get_x() != Teuchos::null &&
63 inArgs_.get_x_dot() != Teuchos::null)),
65 "Error - We need to set the initial conditions for x and xDot from\n"
66 " either initialState or appModel_->getNominalValues::InArgs\n"
67 " (but not from a mixture of the two).\n");
71 if (x == Teuchos::null) {
74 (x == Teuchos::null) && (inArgs_.get_x() == Teuchos::null),
76 "Error - setInitialConditions needs the ICs from the "
78 " or getNominalValues()!\n");
81 initialState->setX(x);
85 using Teuchos::rcp_const_cast;
87 if (initialState->getX() == Teuchos::null ||
88 initialState->getXDot() == Teuchos::null) {
90 (inArgs_.get_x() == Teuchos::null) ||
91 (inArgs_.get_x_dot() == Teuchos::null),
93 "Error - setInitialConditions() needs the ICs from the initialState\n"
94 " or getNominalValues()!\n");
96 initialState->setX(x);
97 RCP<Thyra::VectorBase<Scalar> > x_dot =
99 initialState->setXDot(x_dot);
104 std::string icConsistency = this->getICConsistency();
105 if (icConsistency ==
"None") {
107 if (initialState->getXDot() == Teuchos::null) {
108 RCP<Teuchos::FancyOStream> out = this->getOStream();
109 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
110 *out <<
"Warning -- Requested IC consistency of 'None' but\n"
111 <<
" initialState does not have an xDot.\n"
112 <<
" Setting a 'Consistent' xDot!\n"
115 evaluateExplicitODE(xDot, x, initialState->getTime(), p);
116 initialState->setIsSynced(
true);
120 if (initialState->getXDotDot() == Teuchos::null) {
121 RCP<Teuchos::FancyOStream> out = this->getOStream();
122 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
123 *out <<
"Warning -- Requested IC consistency of 'None' but\n"
124 <<
" initialState does not have an xDotDot.\n"
125 <<
" Setting a 'Consistent' xDotDot!\n"
128 this->evaluateExplicitODE(this->getStepperXDotDot(initialState), x,
129 initialState->getXDot(),
130 initialState->getTime(), p);
131 initialState->setIsSynced(
true);
135 else if (icConsistency ==
"Zero") {
137 Thyra::assign(xDot.ptr(), Scalar(0.0));
139 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
141 else if (icConsistency ==
"App") {
144 inArgs_.get_x_dot());
146 xDot == Teuchos::null, std::logic_error,
147 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
148 " but 'App' returned a null pointer for xDot!\n");
149 Thyra::assign(xDot.ptr(), *x_dot);
153 inArgs_.get_x_dot_dot());
155 xDotDot == Teuchos::null, std::logic_error,
156 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
157 " but 'App' returned a null pointer for xDotDot!\n");
158 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), *xDotDot);
161 else if (icConsistency ==
"Consistent") {
165 evaluateExplicitODE(xDot, x, initialState->getTime(), p);
170 this->evaluateExplicitODE(initialState->getXDotDot(), x,
171 initialState->getXDot(),
172 initialState->getTime(), p);
177 true, std::logic_error,
178 "Error - setInitialConditions() invalid IC consistency, '"
179 << icConsistency <<
"'.\n");
184 initialState->setIsSynced(
true);
187 if (this->getICConsistencyCheck()) {
189 auto f = initialState->getX()->clone_v();
191 evaluateExplicitODE(f, x, initialState->getTime(), p);
192 Thyra::Vp_StV(f.ptr(), Scalar(-1.0), *(xDot));
193 Scalar normX = Thyra::norm(*x);
194 Scalar reldiff = Scalar(0.0);
195 if (normX == Scalar(0.0))
196 reldiff = Thyra::norm(*f);
198 reldiff = Thyra::norm(*f) / normX;
202 RCP<Teuchos::FancyOStream> out = this->getOStream();
203 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
205 *out <<
"\n---------------------------------------------------\n"
206 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
207 <<
" Initial condition PASSED consistency check!\n"
208 <<
" (||xDot-f(x,t)||/||x|| = " << reldiff <<
") < "
209 <<
"(eps = " << eps <<
")" << std::endl
210 <<
"---------------------------------------------------\n"
214 *out <<
"\n---------------------------------------------------\n"
215 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
216 <<
" Initial condition FAILED consistency check but continuing!\n"
217 <<
" (||xDot-f(x,t)||/||x|| = " << reldiff <<
") > "
218 <<
"(eps = " << eps <<
")" << std::endl
219 <<
" ||xDot-f(x,t)|| = " << Thyra::norm(*f) << std::endl
220 <<
" ||x|| = " << Thyra::norm(*x) << std::endl
221 <<
"---------------------------------------------------\n"
226 auto xDotDot = initialState->getXDotDot();
227 auto f = initialState->getX()->clone_v();
229 this->evaluateExplicitODE(f, x, initialState->getXDot(),
230 initialState->getTime(), p);
231 Thyra::Vp_StV(f.ptr(), Scalar(-1.0), *(xDotDot));
232 Scalar normX = Thyra::norm(*x);
233 Scalar reldiff = Scalar(0.0);
234 if (normX == Scalar(0.0))
235 reldiff = Thyra::norm(*f);
237 reldiff = Thyra::norm(*f) / normX;
241 RCP<Teuchos::FancyOStream> out = this->getOStream();
242 Teuchos::OSTab ostab(out, 1,
"StepperExplicit::setInitialConditions()");
244 *out <<
"\n---------------------------------------------------\n"
245 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
246 <<
" Initial condition PASSED consistency check!\n"
247 <<
" (||xDotDot-f(x,xDot,t)||/||x|| = " << reldiff <<
") > "
248 <<
"(eps = " << eps <<
")" << std::endl
249 <<
"---------------------------------------------------\n"
253 *out <<
"\n---------------------------------------------------\n"
254 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
255 <<
"Initial condition FAILED consistency check but continuing!\n"
256 <<
" (||xDotDot-f(x,xDot,t)||/||x|| = " << reldiff <<
") > "
257 <<
"(eps = " << eps <<
")" << std::endl
258 <<
" ||xDotDot-f(x,xDot,t)|| = " << Thyra::norm(*f) << std::endl
259 <<
" ||x|| = " << Thyra::norm(*x) << std::endl
260 <<
"---------------------------------------------------\n"
267 template <
class Scalar>
273 *out <<
"Warning -- No solver to set for StepperExplicit "
274 <<
"(i.e., explicit method).\n"
278 template <
class Scalar>
287 if (inArgs_.supports(MEB::IN_ARG_t)) inArgs_.set_t(time);
288 if (inArgs_.supports(MEB::IN_ARG_step_size))
289 inArgs_.set_step_size(p->timeStepSize_);
290 if (inArgs_.supports(MEB::IN_ARG_stage_number))
291 inArgs_.set_stage_number(p->stageNumber_);
298 if (inArgs_.supports(MEB::IN_ARG_x_dot)) inArgs_.set_x_dot(Teuchos::null);
300 outArgs_.set_f(xDot);
302 appModel_->evalModel(inArgs_, outArgs_);
305 template <
class Scalar>
315 if (inArgs_.supports(MEB::IN_ARG_x_dot)) inArgs_.set_x_dot(xDot);
316 if (inArgs_.supports(MEB::IN_ARG_t)) inArgs_.set_t(time);
317 if (inArgs_.supports(MEB::IN_ARG_step_size))
318 inArgs_.set_step_size(p->timeStepSize_);
319 if (inArgs_.supports(MEB::IN_ARG_stage_number))
320 inArgs_.set_stage_number(p->stageNumber_);
327 if (inArgs_.supports(MEB::IN_ARG_x_dot_dot))
328 inArgs_.set_x_dot_dot(Teuchos::null);
330 outArgs_.set_f(xDotDot);
332 appModel_->evalModel(inArgs_, outArgs_);
335 template <
class Scalar>
339 auto l_out = Teuchos::fancyOStream(out.
getOStream());
341 l_out->setOutputToRootOnly(0);
343 *l_out <<
"--- StepperExplicit ---\n"
344 <<
" appModel_ = " << appModel_ << std::endl
345 <<
" inArgs_ = " << inArgs_ << std::endl
346 <<
" outArgs_ = " << outArgs_ << std::endl;
349 template <
class Scalar>
353 bool isValidSetup =
true;
355 if (appModel_ == Teuchos::null) {
356 isValidSetup =
false;
357 out <<
"The application ModelEvaluator is not set!\n";
363 template <
class Scalar>
367 if (pl != Teuchos::null) {
369 this->setStepperValues(pl);
374 #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.