9 #ifndef Tempus_StepperExplicit_impl_hpp
10 #define Tempus_StepperExplicit_impl_hpp
16 template<
class Scalar>
18 const Teuchos::RCP<
const Thyra::ModelEvaluator<Scalar> >& appModel)
20 this->validExplicitODE(appModel);
23 inArgs_ = appModel_->getNominalValues();
24 outArgs_ = appModel_->createOutArgs();
28 template<
class Scalar>
30 const Teuchos::RCP<Thyra::ModelEvaluator<Scalar> >& appModel)
35 template<
class Scalar>
43 TEUCHOS_TEST_FOR_EXCEPTION(numStates < 1, std::logic_error,
44 "Error - setInitialConditions() needs at least one SolutionState\n"
45 " to set the initial condition. Number of States = " << numStates);
48 RCP<Teuchos::FancyOStream> out = this->getOStream();
49 Teuchos::OSTab ostab(out,1,
"StepperExplicit::setInitialConditions()");
50 *out <<
"Warning -- SolutionHistory has more than one state!\n"
51 <<
"Setting the initial conditions on the currentState.\n"<<std::endl;
54 RCP<SolutionState<Scalar> > initialState =
solutionHistory->getCurrentState();
55 RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
57 inArgs_ = appModel_->getNominalValues();
59 RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
63 TEUCHOS_TEST_FOR_EXCEPTION(
64 !((x != Teuchos::null && xDot != Teuchos::null) ||
65 (inArgs_.get_x() != Teuchos::null &&
66 inArgs_.get_x_dot() != Teuchos::null)), std::logic_error,
67 "Error - We need to set the initial conditions for x and xDot from\n"
68 " either initialState or appModel_->getNominalValues::InArgs\n"
69 " (but not from a mixture of the two).\n");
73 if (x == Teuchos::null) {
75 TEUCHOS_TEST_FOR_EXCEPTION( (x == Teuchos::null) &&
76 (inArgs_.get_x() == Teuchos::null), std::logic_error,
77 "Error - setInitialConditions needs the ICs from the SolutionHistory\n"
78 " or getNominalValues()!\n");
80 x = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs_.get_x());
81 initialState->setX(x);
85 using Teuchos::rcp_const_cast;
87 if ( initialState->getX() == Teuchos::null ||
88 initialState->getXDot() == Teuchos::null ) {
89 TEUCHOS_TEST_FOR_EXCEPTION( (inArgs_.get_x() == Teuchos::null) ||
90 (inArgs_.get_x_dot() == Teuchos::null), std::logic_error,
91 "Error - setInitialConditions() needs the ICs from the initialState\n"
92 " or getNominalValues()!\n");
93 x = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs_.get_x());
94 initialState->setX(x);
95 RCP<Thyra::VectorBase<Scalar> > xDot
96 = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs_.get_x_dot());
97 initialState->setXDot(xDot);
102 std::string icConsistency = getICConsistency();
103 if (icConsistency ==
"None") {
105 if (initialState->getXDot() == Teuchos::null) {
106 RCP<Teuchos::FancyOStream> out = this->getOStream();
107 Teuchos::OSTab ostab(out,1,
"StepperExplicit::setInitialConditions()");
108 *out <<
"Warning -- Requested IC consistency of 'None' but\n"
109 <<
" initialState does not have an xDot.\n"
110 <<
" Setting a 'Consistent' xDot!\n" << std::endl;
111 evaluateExplicitODE(getStepperXDot(initialState), x,
112 initialState->getTime());
113 initialState->setIsSynced(
true);
117 if (initialState->getXDotDot() == Teuchos::null) {
118 RCP<Teuchos::FancyOStream> out = this->getOStream();
119 Teuchos::OSTab ostab(out,1,
"StepperExplicit::setInitialConditions()");
120 *out <<
"Warning -- Requested IC consistency of 'None' but\n"
121 <<
" initialState does not have an xDotDot.\n"
122 <<
" Setting a 'Consistent' xDotDot!\n" << std::endl;
123 this->evaluateExplicitODE(getStepperXDotDot(initialState), x,
124 initialState->getXDot(),
125 initialState->getTime());
126 initialState->setIsSynced(
true);
130 else if (icConsistency ==
"Zero") {
132 Thyra::assign(getStepperXDot(initialState).ptr(), Scalar(0.0));
134 Thyra::assign(getStepperXDotDot(initialState).ptr(), Scalar(0.0));
136 else if (icConsistency ==
"App") {
138 auto xDot = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(
139 inArgs_.get_x_dot());
140 TEUCHOS_TEST_FOR_EXCEPTION(xDot == Teuchos::null, std::logic_error,
141 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
142 " but 'App' returned a null pointer for xDot!\n");
143 Thyra::assign(getStepperXDot(initialState).ptr(), *xDot);
146 auto xDotDot = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(
147 inArgs_.get_x_dot_dot());
148 TEUCHOS_TEST_FOR_EXCEPTION(xDotDot == Teuchos::null, std::logic_error,
149 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
150 " but 'App' returned a null pointer for xDotDot!\n");
151 Thyra::assign(getStepperXDotDot(initialState).ptr(), *xDotDot);
154 else if (icConsistency ==
"Consistent") {
157 evaluateExplicitODE(getStepperXDot(initialState), x,
158 initialState->getTime());
162 this->evaluateExplicitODE(initialState->getXDotDot(), x,
163 initialState->getXDot(),
164 initialState->getTime());
168 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
169 "Error - setInitialConditions() invalid IC consistency, "
170 << icConsistency <<
".\n");
175 initialState->setIsSynced(
true);
178 if (getICConsistencyCheck()) {
180 auto xDot = getStepperXDot(initialState);
181 auto f = initialState->getX()->clone_v();
182 evaluateExplicitODE(f, x, initialState->getTime());
183 Thyra::Vp_StV(f.ptr(), Scalar(-1.0), *(xDot));
184 Scalar normX = Thyra::norm(*x);
185 Scalar reldiff = Scalar(0.0);
186 if (normX == Scalar(0.0)) reldiff = Thyra::norm(*f);
187 else reldiff = Thyra::norm(*f)/normX;
189 Scalar eps = Scalar(100.0)*std::abs(Teuchos::ScalarTraits<Scalar>::eps());
191 RCP<Teuchos::FancyOStream> out = this->getOStream();
192 Teuchos::OSTab ostab(out,1,
"StepperExplicit::setInitialConditions()");
193 *out <<
"Warning -- Failed consistency check but continuing!\n"
194 <<
" ||xDot-f(x,t)||/||x|| > eps" << std::endl
195 <<
" ||xDot-f(x,t)|| = " << Thyra::norm(*f) << std::endl
196 <<
" ||x|| = " << Thyra::norm(*x) << std::endl
197 <<
" ||xDot-f(x,t)||/||x|| = " << reldiff << std::endl
198 <<
" eps = " << eps << std::endl;
202 auto xDotDot = initialState->getXDotDot();
203 auto f = initialState->getX()->clone_v();
204 this->evaluateExplicitODE(f, x, initialState->getXDot(),
205 initialState->getTime());
206 Thyra::Vp_StV(f.ptr(), Scalar(-1.0), *(xDotDot));
207 Scalar normX = Thyra::norm(*x);
208 Scalar reldiff = Scalar(0.0);
209 if (normX == Scalar(0.0)) reldiff = Thyra::norm(*f);
210 else reldiff = Thyra::norm(*f)/normX;
212 Scalar eps = Scalar(100.0)*std::abs(Teuchos::ScalarTraits<Scalar>::eps());
214 RCP<Teuchos::FancyOStream> out = this->getOStream();
215 Teuchos::OSTab ostab(out,1,
"StepperExplicit::setInitialConditions()");
216 *out <<
"Warning -- Failed consistency check but continuing!\n"
217 <<
" ||xDotDot-f(x,xDot,t)||/||x|| > eps" << std::endl
218 <<
" ||xDotDot-f(x,xDot,t)|| = " << Thyra::norm(*f)
220 <<
" ||x|| = " << Thyra::norm(*x)
222 <<
" ||xDotDot-f(x,xDot,t)||/||x|| = " << reldiff << std::endl
223 <<
" eps = " << eps << std::endl;
229 template<
class Scalar>
232 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
233 Teuchos::OSTab ostab(out,1,
"StepperExplicit::setSolver()");
234 *out <<
"Warning -- No solver to set for StepperExplicit "
235 <<
"(i.e., explicit method).\n" << std::endl;
239 template<
class Scalar>
241 Teuchos::RCP<Teuchos::ParameterList> )
243 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
244 Teuchos::OSTab ostab(out,1,
"StepperExplicit::setSolver()");
245 *out <<
"Warning -- No solver to set for StepperExplicit "
246 <<
"(i.e., explicit method).\n" << std::endl;
250 template<
class Scalar>
252 Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> > )
254 Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
255 Teuchos::OSTab ostab(out,1,
"StepperExplicit::setSolver()");
256 *out <<
"Warning -- No solver to set for StepperExplicit "
257 <<
"(i.e., explicit method).\n" << std::endl;
261 template<
class Scalar>
262 Teuchos::RCP<Thyra::VectorBase<Scalar> >
266 if (state->getX() != Teuchos::null) stepperX_ = state->getX();
270 TEUCHOS_TEST_FOR_EXCEPTION( stepperX_ == Teuchos::null, std::logic_error,
271 "Error - stepperX_ has not been set in setInitialConditions() or\n"
272 " can not be set from the state!\n");
277 template<
class Scalar>
278 Teuchos::RCP<Thyra::VectorBase<Scalar> >
282 if (state->getXDot() != Teuchos::null) stepperXDot_ = state->getXDot();
286 TEUCHOS_TEST_FOR_EXCEPTION( stepperXDot_ == Teuchos::null, std::logic_error,
287 "Error - stepperXDot_ has not set in setInitialConditions() or\n"
288 " can not be set from the state!\n");
293 template<
class Scalar>
294 Teuchos::RCP<Thyra::VectorBase<Scalar> >
298 if (state->getXDotDot() != Teuchos::null) stepperXDotDot_=state->getXDotDot();
302 TEUCHOS_TEST_FOR_EXCEPTION( stepperXDotDot_==Teuchos::null, std::logic_error,
303 "Error - stepperXDotDot_ has not set in setInitialConditions() or\n"
304 " can not be set from the state!\n");
306 return stepperXDotDot_;
309 template<
class Scalar>
313 Teuchos::RCP<
const Thyra::VectorBase<Scalar> > x,
316 typedef Thyra::ModelEvaluatorBase MEB;
319 if (inArgs_.supports(MEB::IN_ARG_t)) inArgs_.set_t(time);
326 if (inArgs_.supports(MEB::IN_ARG_x_dot)) inArgs_.set_x_dot(Teuchos::null);
328 outArgs_.set_f(xDot);
330 appModel_->evalModel(inArgs_, outArgs_);
333 template<
class Scalar>
337 Teuchos::RCP<
const Thyra::VectorBase<Scalar> > x,
338 Teuchos::RCP<
const Thyra::VectorBase<Scalar> > xDot,
341 typedef Thyra::ModelEvaluatorBase MEB;
344 if (inArgs_.supports(MEB::IN_ARG_x_dot)) inArgs_.set_x_dot(xDot);
345 if (inArgs_.supports(MEB::IN_ARG_t)) inArgs_.set_t(time);
352 if (inArgs_.supports(MEB::IN_ARG_x_dot_dot))
353 inArgs_.set_x_dot_dot(Teuchos::null);
355 outArgs_.set_f(xDotDot);
357 appModel_->evalModel(inArgs_, outArgs_);
362 #endif // Tempus_StepperExplicit_impl_hpp
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getStepperXDot(Teuchos::RCP< SolutionState< Scalar > > state)
Get xDot from SolutionState or Stepper storage.
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getStepperXDotDot(Teuchos::RCP< SolutionState< Scalar > > state)
Get xDotDot from SolutionState or Stepper storage.
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.
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getStepperX(Teuchos::RCP< SolutionState< Scalar > > state)
Get x from SolutionState or Stepper storage.
Stepper integrates first-order ODEs.
virtual void setSolver(std::string solverName)
Set solver via ParameterList solver name.
virtual void setNonConstModel(const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &appModel)
virtual void evaluateExplicitODE(Teuchos::RCP< Thyra::VectorBase< Scalar > > xDot, Teuchos::RCP< const Thyra::VectorBase< Scalar > > x, const Scalar time)
Evaluate xDot = f(x,t).
Solution state for integrators and steppers. SolutionState contains the metadata for solutions and th...