Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 "Thyra_VectorStdOps.hpp"
13 
15 
16 namespace Tempus {
17 
18 template <class Scalar>
20 {
21  this->setStepperName("Forward Euler");
22  this->setStepperType("Forward Euler");
23  this->setUseFSAL(true);
24  this->setICConsistency("Consistent");
25  this->setICConsistencyCheck(false);
26  this->setAppAction(Teuchos::null);
27 }
28 
29 template <class Scalar>
31  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
32  bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck,
34  stepperFEAppAction)
35 {
36  this->setStepperName("Forward Euler");
37  this->setStepperType("Forward Euler");
38  this->setUseFSAL(useFSAL);
39  this->setICConsistency(ICConsistency);
40  this->setICConsistencyCheck(ICConsistencyCheck);
41 
42  this->setAppAction(stepperFEAppAction);
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  stepperFEAppAction_ =
57  }
58  else {
59  stepperFEAppAction_ = 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 xDot
72  if (initialState->getXDot() == Teuchos::null)
73  this->setStepperXDot(initialState->getX()->clone_v());
74  else
75  this->setStepperXDot(initialState->getXDot());
76 
78 }
79 
80 template <class Scalar>
82  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
83 {
84  this->checkInitialized();
85 
86  using Teuchos::RCP;
87 
88  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperForwardEuler::takeStep()");
89  {
91  solutionHistory->getNumStates() < 2, std::logic_error,
92  "Error - StepperForwardEuler<Scalar>::takeStep(...)\n"
93  << "Need at least two SolutionStates for Forward Euler.\n"
94  << " Number of States = " << solutionHistory->getNumStates()
95  << "\n Try setting in \"Solution History\" \"Storage Type\" = "
96  << "\"Undo\"\n or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
97  << "\"2\"\n");
98 
99  RCP<StepperForwardEuler<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
100  stepperFEAppAction_->execute(
101  solutionHistory, thisStepper,
103 
104  RCP<SolutionState<Scalar> > currentState =
105  solutionHistory->getCurrentState();
106  RCP<SolutionState<Scalar> > workingState =
107  solutionHistory->getWorkingState();
108  if (currentState->getXDot() != Teuchos::null)
109  this->setStepperXDot(currentState->getXDot());
110  RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot();
111  const Scalar dt = workingState->getTimeStep();
112 
113  if (!(this->getUseFSAL()) || workingState->getNConsecutiveFailures() != 0) {
114  // Need to compute XDotOld.
115  stepperFEAppAction_->execute(
116  solutionHistory, thisStepper,
118  Scalar>::ACTION_LOCATION::BEFORE_EXPLICIT_EVAL);
119 
121 
122  // Evaluate xDot = f(x,t).
123  this->evaluateExplicitODE(xDot, currentState->getX(),
124  currentState->getTime(), p);
125 
126  // For UseFSAL=false, x and xDot are now sync'ed or consistent
127  // at the same time level for the currentState.
128  currentState->setIsSynced(true);
129  }
130 
131  // Forward Euler update, x^n = x^{n-1} + dt^n * xDot^{n-1}
132  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getX())),
133  *(currentState->getX()), dt, *(xDot));
134 
135  if (workingState->getXDot() != Teuchos::null)
136  this->setStepperXDot(workingState->getXDot());
137  xDot = this->getStepperXDot();
138 
139  if (this->getUseFSAL()) {
140  // Get consistent xDot^n.
141  stepperFEAppAction_->execute(
142  solutionHistory, thisStepper,
144  Scalar>::ACTION_LOCATION::BEFORE_EXPLICIT_EVAL);
145 
147 
148  // Evaluate xDot = f(x,t).
149  this->evaluateExplicitODE(xDot, workingState->getX(),
150  workingState->getTime(), p);
151 
152  // For UseFSAL=true, x and xDot are now sync'ed or consistent
153  // for the workingState.
154  workingState->setIsSynced(true);
155  }
156  else {
157  assign(xDot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
158  workingState->setIsSynced(false);
159  }
160 
161  workingState->setSolutionStatus(Status::PASSED);
162  workingState->setOrder(this->getOrder());
163  workingState->computeNorms(currentState);
164  stepperFEAppAction_->execute(
165  solutionHistory, thisStepper,
167  }
168  return;
169 }
170 
177 template <class Scalar>
180 {
182  rcp(new StepperState<Scalar>(this->getStepperType()));
183  return stepperState;
184 }
185 
186 template <class Scalar>
188  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
189 {
190  auto l_out = Teuchos::fancyOStream(out.getOStream());
191  Teuchos::OSTab ostab(*l_out, 2, this->description());
192  l_out->setOutputToRootOnly(0);
193 
194  *l_out << std::endl;
195  Stepper<Scalar>::describe(*l_out, verbLevel);
196  StepperExplicit<Scalar>::describe(*l_out, verbLevel);
197  *l_out << " stepperFEAppAction_ = " << stepperFEAppAction_ << std::endl
198  << "----------------------------" << std::endl;
199 }
200 
201 template <class Scalar>
203 {
204  out.setOutputToRootOnly(0);
205 
206  bool isValidSetup = true;
207 
208  if (!Stepper<Scalar>::isValidSetup(out)) isValidSetup = false;
209  if (!StepperExplicit<Scalar>::isValidSetup(out)) isValidSetup = false;
210  if (stepperFEAppAction_ == Teuchos::null) {
211  isValidSetup = false;
212  out << "The Forward Euler AppAction is not set!\n";
213  }
214  return isValidSetup;
215 }
216 
217 // Nonmember constructor - ModelEvaluator and ParameterList
218 // ------------------------------------------------------------------------
219 template <class Scalar>
221  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
223 {
224  auto stepper = Teuchos::rcp(new StepperForwardEuler<Scalar>());
225  stepper->setStepperExplicitValues(pl);
226 
227  if (model != Teuchos::null) {
228  stepper->setModel(model);
229  stepper->initialize();
230  }
231 
232  return stepper;
233 }
234 
235 } // namespace Tempus
236 #endif // Tempus_StepperForwardEuler_impl_hpp
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Thyra Base interface for time steppers.
Teuchos::RCP< StepperForwardEuler< Scalar > > createStepperForwardEuler(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.
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)
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
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
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.
Thyra Base interface for implicit time steppers.