Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_StepperLeapfrog_impl.hpp
Go to the documentation of this file.
1 //@HEADER
2 // *****************************************************************************
3 // Tempus: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
10 #ifndef Tempus_StepperLeapfrog_impl_hpp
11 #define Tempus_StepperLeapfrog_impl_hpp
12 
13 #include "Thyra_VectorStdOps.hpp"
14 
16 
17 namespace Tempus {
18 
19 template <class Scalar>
21 {
22  this->setStepperName("Leapfrog");
23  this->setStepperType("Leapfrog");
24  this->setUseFSAL(false);
25  this->setICConsistency("Consistent");
26  this->setICConsistencyCheck(false);
27 
28  this->setAppAction(Teuchos::null);
29 }
30 
31 template <class Scalar>
33  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
34  bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck,
35  const Teuchos::RCP<StepperLeapfrogAppAction<Scalar> >& stepperLFAppAction)
36 {
37  this->setStepperName("Leapfrog");
38  this->setStepperType("Leapfrog");
39  this->setUseFSAL(useFSAL);
40  this->setICConsistency(ICConsistency);
41  this->setICConsistencyCheck(ICConsistencyCheck);
42  this->setAppAction(stepperLFAppAction);
43  if (appModel != Teuchos::null) {
44  this->setModel(appModel);
45  this->initialize();
46  }
47 }
48 
49 template <class Scalar>
52 {
53  if (appAction == Teuchos::null) {
54  // Create default appAction
55  stepperLFAppAction_ =
57  }
58  else {
59  stepperLFAppAction_ = appAction;
60  }
61 }
62 
63 template <class Scalar>
65  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
66 {
67  using Teuchos::RCP;
68 
69  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
70 
71  // Check if we need Stepper storage for xDotDot
72  if (initialState->getXDotDot() == Teuchos::null)
73  this->setStepperXDotDot(initialState->getX()->clone_v());
74  else
75  this->setStepperXDotDot(initialState->getXDotDot());
76 
78 
79  if (this->getUseFSAL()) {
80  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
81  Teuchos::OSTab ostab(out, 1, "StepperLeapfrog::setInitialConditions()");
82  *out << "Warning -- The First-Same-As-Last (FSAL) principle is not "
83  << "used with Leapfrog because of the algorithm's prescribed "
84  << "order of solution update. The default is to set useFSAL=false, "
85  << "however useFSAL=true will also work but have no affect "
86  << "(i.e., no-op).\n"
87  << std::endl;
88  }
89 }
90 
91 template <class Scalar>
93  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
94 {
95  this->checkInitialized();
96 
97  using Teuchos::RCP;
98 
99  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperLeapfrog::takeStep()");
100  {
102  solutionHistory->getNumStates() < 2, std::logic_error,
103  "Error - StepperLeapfrog<Scalar>::takeStep(...)\n"
104  << "Need at least two SolutionStates for Leapfrog.\n"
105  << " Number of States = " << solutionHistory->getNumStates()
106  << "\nTry setting in \"Solution History\" \"Storage Type\" = "
107  << "\"Undo\"\n"
108  << " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
109  << "\"2\"\n");
110 
111  RCP<SolutionState<Scalar> > currentState =
112  solutionHistory->getCurrentState();
113  RCP<SolutionState<Scalar> > workingState =
114  solutionHistory->getWorkingState();
115  const Scalar time = currentState->getTime();
116  const Scalar dt = workingState->getTimeStep();
117 
118  RCP<StepperLeapfrog<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
119 
120  stepperLFAppAction_->execute(
121  solutionHistory, thisStepper,
123 
124  // Perform half-step startup if working state is synced
125  // (i.e., xDot and x are at the same time level).
126  if (workingState->getIsSynced() == true) {
127  // Half-step startup: xDot_{n+1/2} = xDot_n + 0.5*dt*xDotDot_n
128  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getXDot())),
129  *(currentState->getXDot()), 0.5 * dt,
130  *(currentState->getXDotDot()));
131  }
132  stepperLFAppAction_->execute(
133  solutionHistory, thisStepper,
135  // x_{n+1} = x_n + dt*xDot_{n+1/2}
136  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getX())),
137  *(currentState->getX()), dt, *(workingState->getXDot()));
138 
139  stepperLFAppAction_->execute(
140  solutionHistory, thisStepper,
142  Scalar>::ACTION_LOCATION::BEFORE_EXPLICIT_EVAL);
144 
145  // Evaluate xDotDot = f(x,t).
146  this->evaluateExplicitODE(workingState->getXDotDot(), workingState->getX(),
147  Teuchos::null, time + dt, p);
148  stepperLFAppAction_->execute(
149  solutionHistory, thisStepper,
151  if (workingState->getOutput() == true) {
152  // Half-step sync: xDot_{n+1} = xDot_{n+1/2} + 0.5*dt*xDotDot_{n+1}
153  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getXDot())),
154  *(workingState->getXDot()), 0.5 * dt,
155  *(workingState->getXDotDot()));
156  workingState->setIsSynced(true);
157  }
158  else {
159  // Full leapfrog step: xDot_{n+3/2} = xDot_{n+1/2} + dt*xDotDot_{n+1}
160  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getXDot())),
161  *(workingState->getXDot()), dt,
162  *(workingState->getXDotDot()));
163  workingState->setIsSynced(false);
164  }
165 
166  workingState->setSolutionStatus(Status::PASSED);
167  workingState->setOrder(this->getOrder());
168  workingState->computeNorms(currentState);
169 
170  stepperLFAppAction_->execute(
171  solutionHistory, thisStepper,
173  }
174  return;
175 }
176 
183 template <class Scalar>
186 {
188  rcp(new StepperState<Scalar>(this->getStepperType()));
189  return stepperState;
190 }
191 
192 template <class Scalar>
194  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
195 {
196  out.setOutputToRootOnly(0);
197  out << std::endl;
198  Stepper<Scalar>::describe(out, verbLevel);
199  StepperExplicit<Scalar>::describe(out, verbLevel);
200 
201  out << "--- StepperLeapfrog ---\n";
202  out << " stepperLFAppAction_ = " << stepperLFAppAction_
203  << std::endl;
204  out << "-----------------------" << std::endl;
205 }
206 
207 template <class Scalar>
209 {
210  out.setOutputToRootOnly(0);
211  bool isValidSetup = true;
212 
213  if (!Stepper<Scalar>::isValidSetup(out)) isValidSetup = false;
214  if (!StepperExplicit<Scalar>::isValidSetup(out)) isValidSetup = false;
215  if (stepperLFAppAction_ == Teuchos::null) {
216  isValidSetup = false;
217  out << "The Leapfrog AppAction is not set!\n";
218  }
219 
220  return isValidSetup;
221 }
222 
223 // Nonmember constructor - ModelEvaluator and ParameterList
224 // ------------------------------------------------------------------------
225 template <class Scalar>
227  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
229 {
230  auto stepper = Teuchos::rcp(new StepperLeapfrog<Scalar>());
231  stepper->setStepperExplicitValues(pl);
232 
233  if (model != Teuchos::null) {
234  stepper->setModel(model);
235  stepper->initialize();
236  }
237 
238  return stepper;
239 }
240 
241 } // namespace Tempus
242 #endif // Tempus_StepperLeapfrog_impl_hpp
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
Teuchos::RCP< StepperLeapfrog< Scalar > > createStepperLeapfrog(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
Thyra Base interface for time steppers.
StepperState is a simple class to hold state information about the stepper.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Application Action for StepperLeapfrog.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
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
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
StepperLeapfrog()
Default constructor.
virtual void setAppAction(Teuchos::RCP< StepperLeapfrogAppAction< Scalar > > appAction)
Thyra Base interface for implicit time steppers.