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: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
9 #ifndef Tempus_StepperLeapfrog_impl_hpp
10 #define Tempus_StepperLeapfrog_impl_hpp
11 
12 #include "Thyra_VectorStdOps.hpp"
13 
15 
16 namespace Tempus {
17 
18 template <class Scalar>
20 {
21  this->setStepperName("Leapfrog");
22  this->setStepperType("Leapfrog");
23  this->setUseFSAL(false);
24  this->setICConsistency("Consistent");
25  this->setICConsistencyCheck(false);
26 
27  this->setAppAction(Teuchos::null);
28 }
29 
30 template <class Scalar>
32  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
33  bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck,
34  const Teuchos::RCP<StepperLeapfrogAppAction<Scalar> >& stepperLFAppAction)
35 {
36  this->setStepperName("Leapfrog");
37  this->setStepperType("Leapfrog");
38  this->setUseFSAL(useFSAL);
39  this->setICConsistency(ICConsistency);
40  this->setICConsistencyCheck(ICConsistencyCheck);
41  this->setAppAction(stepperLFAppAction);
42  if (appModel != Teuchos::null) {
43  this->setModel(appModel);
44  this->initialize();
45  }
46 }
47 
48 template <class Scalar>
51 {
52  if (appAction == Teuchos::null) {
53  // Create default appAction
54  stepperLFAppAction_ =
56  }
57  else {
58  stepperLFAppAction_ = appAction;
59  }
60 }
61 
62 template <class Scalar>
64  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
65 {
66  using Teuchos::RCP;
67 
68  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
69 
70  // Check if we need Stepper storage for xDotDot
71  if (initialState->getXDotDot() == Teuchos::null)
72  this->setStepperXDotDot(initialState->getX()->clone_v());
73  else
74  this->setStepperXDotDot(initialState->getXDotDot());
75 
77 
78  if (this->getUseFSAL()) {
79  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
80  Teuchos::OSTab ostab(out, 1, "StepperLeapfrog::setInitialConditions()");
81  *out << "Warning -- The First-Same-As-Last (FSAL) principle is not "
82  << "used with Leapfrog because of the algorithm's prescribed "
83  << "order of solution update. The default is to set useFSAL=false, "
84  << "however useFSAL=true will also work but have no affect "
85  << "(i.e., no-op).\n"
86  << std::endl;
87  }
88 }
89 
90 template <class Scalar>
92  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
93 {
94  this->checkInitialized();
95 
96  using Teuchos::RCP;
97 
98  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperLeapfrog::takeStep()");
99  {
101  solutionHistory->getNumStates() < 2, std::logic_error,
102  "Error - StepperLeapfrog<Scalar>::takeStep(...)\n"
103  << "Need at least two SolutionStates for Leapfrog.\n"
104  << " Number of States = " << solutionHistory->getNumStates()
105  << "\nTry setting in \"Solution History\" \"Storage Type\" = "
106  << "\"Undo\"\n"
107  << " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
108  << "\"2\"\n");
109 
110  RCP<SolutionState<Scalar> > currentState =
111  solutionHistory->getCurrentState();
112  RCP<SolutionState<Scalar> > workingState =
113  solutionHistory->getWorkingState();
114  const Scalar time = currentState->getTime();
115  const Scalar dt = workingState->getTimeStep();
116 
117  RCP<StepperLeapfrog<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
118 
119  stepperLFAppAction_->execute(
120  solutionHistory, thisStepper,
122 
123  // Perform half-step startup if working state is synced
124  // (i.e., xDot and x are at the same time level).
125  if (workingState->getIsSynced() == true) {
126  // Half-step startup: xDot_{n+1/2} = xDot_n + 0.5*dt*xDotDot_n
127  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getXDot())),
128  *(currentState->getXDot()), 0.5 * dt,
129  *(currentState->getXDotDot()));
130  }
131  stepperLFAppAction_->execute(
132  solutionHistory, thisStepper,
134  // x_{n+1} = x_n + dt*xDot_{n+1/2}
135  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getX())),
136  *(currentState->getX()), dt, *(workingState->getXDot()));
137 
138  stepperLFAppAction_->execute(
139  solutionHistory, thisStepper,
141  Scalar>::ACTION_LOCATION::BEFORE_EXPLICIT_EVAL);
143 
144  // Evaluate xDotDot = f(x,t).
145  this->evaluateExplicitODE(workingState->getXDotDot(), workingState->getX(),
146  Teuchos::null, time + dt, p);
147  stepperLFAppAction_->execute(
148  solutionHistory, thisStepper,
150  if (workingState->getOutput() == true) {
151  // Half-step sync: xDot_{n+1} = xDot_{n+1/2} + 0.5*dt*xDotDot_{n+1}
152  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getXDot())),
153  *(workingState->getXDot()), 0.5 * dt,
154  *(workingState->getXDotDot()));
155  workingState->setIsSynced(true);
156  }
157  else {
158  // Full leapfrog step: xDot_{n+3/2} = xDot_{n+1/2} + dt*xDotDot_{n+1}
159  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getXDot())),
160  *(workingState->getXDot()), dt,
161  *(workingState->getXDotDot()));
162  workingState->setIsSynced(false);
163  }
164 
165  workingState->setSolutionStatus(Status::PASSED);
166  workingState->setOrder(this->getOrder());
167  workingState->computeNorms(currentState);
168 
169  stepperLFAppAction_->execute(
170  solutionHistory, thisStepper,
172  }
173  return;
174 }
175 
182 template <class Scalar>
185 {
187  rcp(new StepperState<Scalar>(this->getStepperType()));
188  return stepperState;
189 }
190 
191 template <class Scalar>
193  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
194 {
195  out.setOutputToRootOnly(0);
196  out << std::endl;
197  Stepper<Scalar>::describe(out, verbLevel);
198  StepperExplicit<Scalar>::describe(out, verbLevel);
199 
200  out << "--- StepperLeapfrog ---\n";
201  out << " stepperLFAppAction_ = " << stepperLFAppAction_
202  << std::endl;
203  out << "-----------------------" << std::endl;
204 }
205 
206 template <class Scalar>
208 {
209  out.setOutputToRootOnly(0);
210  bool isValidSetup = true;
211 
212  if (!Stepper<Scalar>::isValidSetup(out)) isValidSetup = false;
213  if (!StepperExplicit<Scalar>::isValidSetup(out)) isValidSetup = false;
214  if (stepperLFAppAction_ == Teuchos::null) {
215  isValidSetup = false;
216  out << "The Leapfrog AppAction is not set!\n";
217  }
218 
219  return isValidSetup;
220 }
221 
222 // Nonmember constructor - ModelEvaluator and ParameterList
223 // ------------------------------------------------------------------------
224 template <class Scalar>
226  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
228 {
229  auto stepper = Teuchos::rcp(new StepperLeapfrog<Scalar>());
230  stepper->setStepperExplicitValues(pl);
231 
232  if (model != Teuchos::null) {
233  stepper->setModel(model);
234  stepper->initialize();
235  }
236 
237  return stepper;
238 }
239 
240 } // namespace Tempus
241 #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.