Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros 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 "Teuchos_VerboseObjectParameterListHelpers.hpp"
13 #include "Thyra_VectorStdOps.hpp"
14 
15 
16 namespace Tempus {
17 
18 template<class Scalar>
20 {
21  this->setParameterList(Teuchos::null);
22  this->modelWarning();
23 }
24 
25 template<class Scalar>
27  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
28  Teuchos::RCP<Teuchos::ParameterList> pList)
29 {
30  this->setParameterList(pList);
31 
32  if (appModel == Teuchos::null) {
33  this->modelWarning();
34  }
35  else {
36  this->setModel(appModel);
37  this->initialize();
38  }
39 }
40 
41 template<class Scalar>
43  Teuchos::RCP<StepperObserver<Scalar> > obs)
44 {
45  if (obs == Teuchos::null) {
46  // Create default observer, otherwise keep current observer.
47  if (this->stepperObserver_ == Teuchos::null) {
48  stepperLFObserver_ =
49  Teuchos::rcp(new StepperLeapfrogObserver<Scalar>());
50  this->stepperObserver_ =
51  Teuchos::rcp_dynamic_cast<StepperObserver<Scalar> >(stepperLFObserver_);
52  }
53  } else {
54  this->stepperObserver_ = obs;
55  stepperLFObserver_ =
56  Teuchos::rcp_dynamic_cast<StepperLeapfrogObserver<Scalar> >
57  (this->stepperObserver_);
58  }
59 }
60 
61 template<class Scalar>
63 {
64  TEUCHOS_TEST_FOR_EXCEPTION(
65  this->appModel_ == Teuchos::null, std::logic_error,
66  "Error - Need to set the model, setModel(), before calling "
67  "StepperLeapfrog::initialize()\n");
68 
69  this->setParameterList(this->stepperPL_);
70  this->setObserver();
71 }
72 
73 template<class Scalar>
75  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
76 {
77  using Teuchos::RCP;
78 
79  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
80 
81  // Check if we need Stepper storage for xDotDot
82  if (initialState->getXDotDot() == Teuchos::null)
83  this->setStepperXDotDot(initialState->getX()->clone_v());
84 
86 
87  if (this->getUseFSAL()) {
88  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
89  Teuchos::OSTab ostab(out,1,"StepperLeapfrog::setInitialConditions()");
90  *out << "Warning -- The First-Step-As-Last (FSAL) principle is not "
91  << "used with Leapfrog because of the algorithm's prescribed "
92  << "order of solution update. The default is to set useFSAL=false, "
93  << "however useFSAL=true will also work but have no affect "
94  << "(i.e., no-op).\n" << std::endl;
95  }
96 }
97 
98 template<class Scalar>
100  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
101 {
102  using Teuchos::RCP;
103 
104  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperLeapfrog::takeStep()");
105  {
106  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
107  std::logic_error,
108  "Error - StepperLeapfrog<Scalar>::takeStep(...)\n"
109  "Need at least two SolutionStates for Leapfrog.\n"
110  " Number of States = " << solutionHistory->getNumStates() << "\n"
111  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
112  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
113 
114  this->stepperObserver_->observeBeginTakeStep(solutionHistory, *this);
115  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
116  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
117  const Scalar time = currentState->getTime();
118  const Scalar dt = workingState->getTimeStep();
119 
120  // Perform half-step startup if working state is synced
121  // (i.e., xDot and x are at the same time level).
122  if (workingState->getIsSynced() == true) {
123  if (!Teuchos::is_null(stepperLFObserver_))
124  stepperLFObserver_->observeBeforeXDotUpdateInitialize(
125  solutionHistory, *this);
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,*(currentState->getXDotDot()));
129  }
130 
131  if (!Teuchos::is_null(stepperLFObserver_))
132  stepperLFObserver_->observeBeforeXUpdate(solutionHistory, *this);
133  // x_{n+1} = x_n + dt*xDot_{n+1/2}
134  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getX())),
135  *(currentState->getX()),dt,*(workingState->getXDot()));
136 
137  if (!Teuchos::is_null(stepperLFObserver_))
138  stepperLFObserver_->observeBeforeExplicit(solutionHistory, *this);
139 
140  // Evaluate xDotDot = f(x,t).
141  this->evaluateExplicitODE(workingState->getXDotDot(),
142  workingState->getX(),
143  Teuchos::null, time+dt);
144 
145  if (!Teuchos::is_null(stepperLFObserver_))
146  stepperLFObserver_->observeBeforeXDotUpdate(solutionHistory, *this);
147  if (workingState->getOutput() == true) {
148  // Half-step sync: xDot_{n+1} = xDot_{n+1/2} + 0.5*dt*xDotDot_{n+1}
149  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getXDot())),
150  *(workingState->getXDot()),0.5*dt,*(workingState->getXDotDot()));
151  workingState->setIsSynced(true);
152  } else {
153  // Full leapfrog step: xDot_{n+3/2} = xDot_{n+1/2} + dt*xDotDot_{n+1}
154  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getXDot())),
155  *(workingState->getXDot()),dt,*(workingState->getXDotDot()));
156  workingState->setIsSynced(false);
157  }
158 
159  workingState->setSolutionStatus(Status::PASSED);
160  workingState->setOrder(this->getOrder());
161  this->stepperObserver_->observeEndTakeStep(solutionHistory, *this);
162  }
163  return;
164 }
165 
166 
167 /** \brief Provide a StepperState to the SolutionState.
168  * This Stepper does not have any special state data,
169  * so just provide the base class StepperState with the
170  * Stepper description. This can be checked to ensure
171  * that the input StepperState can be used by this Stepper.
172  */
173 template<class Scalar>
174 Teuchos::RCP<Tempus::StepperState<Scalar> > StepperLeapfrog<Scalar>::
176 {
177  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
178  rcp(new StepperState<Scalar>(description()));
179  return stepperState;
180 }
181 
182 
183 template<class Scalar>
185 {
186  std::string name = "Leapfrog";
187  return(name);
188 }
189 
190 
191 template<class Scalar>
193  Teuchos::FancyOStream &out,
194  const Teuchos::EVerbosityLevel /* verbLevel */) const
195 {
196  out << description() << "::describe:" << std::endl
197  << "appModel_ = " << this->appModel_->description() << std::endl;
198 }
199 
200 
201 template <class Scalar>
203  const Teuchos::RCP<Teuchos::ParameterList> & pList)
204 {
205  if (pList == Teuchos::null) {
206  // Create default parameters if null, otherwise keep current parameters.
207  if (this->stepperPL_ == Teuchos::null)
208  this->stepperPL_ = this->getDefaultParameters();
209  } else {
210  this->stepperPL_ = pList;
211  }
212  this->stepperPL_->validateParametersAndSetDefaults(*this->getValidParameters());
213 
214  std::string stepperType =
215  this->stepperPL_->template get<std::string>("Stepper Type");
216  TEUCHOS_TEST_FOR_EXCEPTION( stepperType != "Leapfrog",
217  std::logic_error,
218  "Error - Stepper Type is not 'Leapfrog'!\n"
219  << " Stepper Type = "<< pList->get<std::string>("Stepper Type") << "\n");
220 }
221 
222 
223 template<class Scalar>
224 Teuchos::RCP<const Teuchos::ParameterList>
226 {
227  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
228  pl->setName("Default Stepper - " + this->description());
229  pl->set<std::string>("Stepper Type", "Leapfrog",
230  "'Stepper Type' must be 'Leapfrog'.");
231  this->getValidParametersBasic(pl);
232  pl->set<std::string>("Initial Condition Consistency", "Consistent");
233  return pl;
234 }
235 
236 
237 template<class Scalar>
238 Teuchos::RCP<Teuchos::ParameterList>
240 {
241  using Teuchos::RCP;
242  using Teuchos::ParameterList;
243  using Teuchos::rcp_const_cast;
244 
245  RCP<ParameterList> pl =
246  rcp_const_cast<ParameterList>(this->getValidParameters());
247 
248  return pl;
249 }
250 
251 
252 template <class Scalar>
253 Teuchos::RCP<Teuchos::ParameterList>
255 {
256  return(this->stepperPL_);
257 }
258 
259 
260 template <class Scalar>
261 Teuchos::RCP<Teuchos::ParameterList>
263 {
264  Teuchos::RCP<Teuchos::ParameterList> temp_plist = this->stepperPL_;
265  this->stepperPL_ = Teuchos::null;
266  return(temp_plist);
267 }
268 
269 
270 } // namespace Tempus
271 #endif // Tempus_StepperLeapfrog_impl_hpp
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
Teuchos::RCP< Teuchos::ParameterList > getDefaultParameters() const
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl)
virtual void setObserver(Teuchos::RCP< StepperObserver< Scalar > > obs=Teuchos::null)
Set Observer.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
StepperState is a simple class to hold state information about the stepper.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
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.
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
StepperObserver class for Stepper class.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
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 std::string description() const
StepperLeapfrog()
Default constructor.
StepperLeapfrogObserver class for StepperLeapfrog.
virtual void initialize()
Initialize during construction and after changing input parameters.