Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_StepperForwardEuler_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_StepperForwardEuler_impl_hpp
10 #define Tempus_StepperForwardEuler_impl_hpp
11 
12 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
13 #include "Thyra_VectorStdOps.hpp"
15 
16 namespace Tempus {
17 
18 template<class Scalar>
20 {
21  this->setStepperType( "Forward Euler");
22  this->setUseFSAL( this->getUseFSALDefault());
23  this->setICConsistency( this->getICConsistencyDefault());
24  this->setICConsistencyCheck( this->getICConsistencyCheckDefault());
25 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
26  this->setObserver();
27 #endif
28  this->setAppAction(Teuchos::null);
29 }
30 
31 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
32 template<class Scalar>
34  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
35  const Teuchos::RCP<StepperObserver<Scalar> >& obs,
36  bool useFSAL,
37  std::string ICConsistency,
38  bool ICConsistencyCheck)
39 {
40  this->setStepperType( "Forward Euler");
41  this->setUseFSAL( useFSAL);
42  this->setICConsistency( ICConsistency);
43  this->setICConsistencyCheck( ICConsistencyCheck);
44  this->setObserver(obs);
45  this->setAppAction(Teuchos::null);
46 
47  if (appModel != Teuchos::null) {
48  this->setModel(appModel);
49  this->initialize();
50  }
51 }
52 #endif
53 
54 template<class Scalar>
56  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
57  bool useFSAL,
58  std::string ICConsistency,
59  bool ICConsistencyCheck,
60  const Teuchos::RCP<StepperForwardEulerAppAction<Scalar> >& stepperFEAppAction)
61 {
62  this->setStepperType( "Forward Euler");
63  this->setUseFSAL( useFSAL);
64  this->setICConsistency( ICConsistency);
65  this->setICConsistencyCheck( ICConsistencyCheck);
66  this->setObserver();
67 
68  this->setAppAction(stepperFEAppAction);
69  if (appModel != Teuchos::null) {
70  this->setModel(appModel);
71  this->initialize();
72  }
73 }
74 
75 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
76 template<class Scalar>
78  Teuchos::RCP<StepperObserver<Scalar> > obs)
79 {
80  if (obs == Teuchos::null) {
81  // Create default observer, otherwise keep current observer.
82  if (this->stepperObserver_ == Teuchos::null) {
83  stepperFEObserver_ =
84  Teuchos::rcp(new StepperForwardEulerObserver<Scalar>());
85  this->stepperObserver_ =
86  Teuchos::rcp_dynamic_cast<StepperObserver<Scalar> >(stepperFEObserver_,true);
87  }
88  } else {
89  this->stepperObserver_ = obs;
90  stepperFEObserver_ =
91  Teuchos::rcp_dynamic_cast<StepperForwardEulerObserver<Scalar> >
92  (this->stepperObserver_,true);
93  }
94 
95  this->isInitialized_ = false;
96 }
97 #endif
98 
99 template<class Scalar>
101  Teuchos::RCP<StepperForwardEulerAppAction<Scalar> > appAction)
102 {
103  if (appAction == Teuchos::null) {
104  // Create default appAction
105  stepperFEAppAction_ =
106  Teuchos::rcp(new StepperForwardEulerModifierDefault<Scalar>());
107  }
108  else {
109  stepperFEAppAction_ = appAction;
110  }
111 }
112 
113 
114 template<class Scalar>
116  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
117 {
118  using Teuchos::RCP;
119 
120  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
121 
122  // Check if we need Stepper storage for xDot
123  if (initialState->getXDot() == Teuchos::null)
124  this->setStepperXDot(initialState->getX()->clone_v());
125 
127 }
128 
129 template<class Scalar>
131  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
132 {
133  this->checkInitialized();
134 
135  using Teuchos::RCP;
136 
137  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperForwardEuler::takeStep()");
138  {
139  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
140  std::logic_error,
141  "Error - StepperForwardEuler<Scalar>::takeStep(...)\n"
142  "Need at least two SolutionStates for Forward Euler.\n"
143  " Number of States = " << solutionHistory->getNumStates() << "\n"
144  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
145  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
146 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
147  this->stepperObserver_->observeBeginTakeStep(solutionHistory, *this);
148 #endif
149  RCP<StepperForwardEuler<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
150  stepperFEAppAction_->execute(solutionHistory, thisStepper,
152 
153  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
154  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
155  RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot(currentState);
156  const Scalar dt = workingState->getTimeStep();
157 
158  if ( !(this->getUseFSAL()) ) {
159  // Need to compute XDotOld.
160 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
161  if (!Teuchos::is_null(stepperFEObserver_))
162  stepperFEObserver_->observeBeforeExplicit(solutionHistory, *this);
163 #endif
164  stepperFEAppAction_->execute(solutionHistory, thisStepper,
166 
167  auto p = Teuchos::rcp(new ExplicitODEParameters<Scalar>(dt));
168 
169  // Evaluate xDot = f(x,t).
170  this->evaluateExplicitODE(xDot, currentState->getX(),
171  currentState->getTime(), p);
172 
173  // For UseFSAL=false, x and xDot are now sync'ed or consistent
174  // at the same time level for the currentState.
175  currentState->setIsSynced(true);
176  }
177 
178 
179  // Forward Euler update, x^n = x^{n-1} + dt^n * xDot^{n-1}
180  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getX())),
181  *(currentState->getX()),dt,*(xDot));
182 
183 
184  xDot = this->getStepperXDot(workingState);
185 
186  if (this->getUseFSAL()) {
187  // Get consistent xDot^n.
188 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
189  if (!Teuchos::is_null(stepperFEObserver_))
190  stepperFEObserver_->observeBeforeExplicit(solutionHistory, *this);
191 #endif
192  stepperFEAppAction_->execute(solutionHistory, thisStepper,
194 
195  auto p = Teuchos::rcp(new ExplicitODEParameters<Scalar>(dt));
196 
197  // Evaluate xDot = f(x,t).
198  this->evaluateExplicitODE(xDot, workingState->getX(),
199  workingState->getTime(), p);
200 
201  // For UseFSAL=true, x and xDot are now sync'ed or consistent
202  // for the workingState.
203  workingState->setIsSynced(true);
204  } else {
205  assign(xDot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
206  workingState->setIsSynced(false);
207  }
208 
209  workingState->setSolutionStatus(Status::PASSED);
210  workingState->setOrder(this->getOrder());
211  workingState->computeNorms(currentState);
212 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
213  this->stepperObserver_->observeEndTakeStep(solutionHistory, *this);
214 #endif
215  stepperFEAppAction_->execute(solutionHistory, thisStepper,
217  }
218  return;
219 }
220 
221 
222 /** \brief Provide a StepperState to the SolutionState.
223  * This Stepper does not have any special state data,
224  * so just provide the base class StepperState with the
225  * Stepper description. This can be checked to ensure
226  * that the input StepperState can be used by this Stepper.
227  */
228 template<class Scalar>
229 Teuchos::RCP<Tempus::StepperState<Scalar> > StepperForwardEuler<Scalar>::
231 {
232  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
233  rcp(new StepperState<Scalar>(this->getStepperType()));
234  return stepperState;
235 }
236 
237 
238 template<class Scalar>
240  Teuchos::FancyOStream &out,
241  const Teuchos::EVerbosityLevel verbLevel) const
242 {
243  out << std::endl;
244  Stepper<Scalar>::describe(out, verbLevel);
245  StepperExplicit<Scalar>::describe(out, verbLevel);
246 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
247  out << "--- StepperForwardEuler ---\n";
248  out << stepperFEObserver_ << std::endl;
249  out << "---------------------------" << std::endl;
250 #endif
251  out << " stepperFEAppAction_ = "
252  << stepperFEAppAction_ << std::endl;
253  out << "----------------------------" << std::endl;
254 }
255 
256 
257 template<class Scalar>
258 bool StepperForwardEuler<Scalar>::isValidSetup(Teuchos::FancyOStream & out) const
259 {
260  bool isValidSetup = true;
261 
262  if ( !Stepper<Scalar>::isValidSetup(out) ) isValidSetup = false;
263  if ( !StepperExplicit<Scalar>::isValidSetup(out) ) isValidSetup = false;
264 #ifndef TEMPUS_HIDE_DEPRECATED_CODE
265  if (stepperFEObserver_ == Teuchos::null) {
266  isValidSetup = false;
267  out << "The Forward Euler observer is not set!\n";
268  }
269 #endif
270  if (stepperFEAppAction_ == Teuchos::null) {
271  isValidSetup = false;
272  out << "The Forward Euler AppAction is not set!\n";
273  }
274  return isValidSetup;
275 }
276 
277 
278 template<class Scalar>
279 Teuchos::RCP<const Teuchos::ParameterList>
281 {
282  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
283  getValidParametersBasic(pl, this->getStepperType());
284  pl->set<bool>("Use FSAL", true);
285  pl->set<std::string>("Initial Condition Consistency", "Consistent");
286  return pl;
287 }
288 
289 
290 } // namespace Tempus
291 #endif // Tempus_StepperForwardEuler_impl_hpp
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual void setObserver(Teuchos::RCP< StepperObserver< Scalar > > obs=Teuchos::null)
Set Observer.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions, make them consistent, and set needed memory.
StepperForwardEulerObserver class for StepperForwardEuler.
Thyra Base interface for time steppers.
StepperState is a simple class to hold state information about the stepper.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Application Action for StepperForwardEuler.
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
StepperObserver class for Stepper class.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
virtual void setAppAction(Teuchos::RCP< StepperForwardEulerAppAction< Scalar > > appAction)
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Thyra Base interface for implicit time steppers.
void getValidParametersBasic(Teuchos::RCP< Teuchos::ParameterList > pl, std::string stepperType)
Provide basic parameters to Steppers.