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: 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_StepperForwardEuler_impl_hpp
11 #define Tempus_StepperForwardEuler_impl_hpp
12 
13 #include "Thyra_VectorStdOps.hpp"
14 
16 
17 namespace Tempus {
18 
19 template <class Scalar>
21 {
22  this->setStepperName("Forward Euler");
23  this->setStepperType("Forward Euler");
24  this->setUseFSAL(true);
25  this->setICConsistency("Consistent");
26  this->setICConsistencyCheck(false);
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,
35  stepperFEAppAction)
36 {
37  this->setStepperName("Forward Euler");
38  this->setStepperType("Forward Euler");
39  this->setUseFSAL(useFSAL);
40  this->setICConsistency(ICConsistency);
41  this->setICConsistencyCheck(ICConsistencyCheck);
42 
43  this->setAppAction(stepperFEAppAction);
44  if (appModel != Teuchos::null) {
45  this->setModel(appModel);
46  this->initialize();
47  }
48 }
49 
50 template <class Scalar>
53 {
54  if (appAction == Teuchos::null) {
55  // Create default appAction
56  stepperFEAppAction_ =
58  }
59  else {
60  stepperFEAppAction_ = appAction;
61  }
62 }
63 
64 template <class Scalar>
66  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
67 {
68  using Teuchos::RCP;
69 
70  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
71 
72  // Check if we need Stepper storage for xDot
73  if (initialState->getXDot() == Teuchos::null)
74  this->setStepperXDot(initialState->getX()->clone_v());
75  else
76  this->setStepperXDot(initialState->getXDot());
77 
79 }
80 
81 template <class Scalar>
83  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
84 {
85  this->checkInitialized();
86 
87  using Teuchos::RCP;
88 
89  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperForwardEuler::takeStep()");
90  {
92  solutionHistory->getNumStates() < 2, std::logic_error,
93  "Error - StepperForwardEuler<Scalar>::takeStep(...)\n"
94  << "Need at least two SolutionStates for Forward Euler.\n"
95  << " Number of States = " << solutionHistory->getNumStates()
96  << "\n Try setting in \"Solution History\" \"Storage Type\" = "
97  << "\"Undo\"\n or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
98  << "\"2\"\n");
99 
100  RCP<StepperForwardEuler<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
101  stepperFEAppAction_->execute(
102  solutionHistory, thisStepper,
104 
105  RCP<SolutionState<Scalar> > currentState =
106  solutionHistory->getCurrentState();
107  RCP<SolutionState<Scalar> > workingState =
108  solutionHistory->getWorkingState();
109  if (currentState->getXDot() != Teuchos::null)
110  this->setStepperXDot(currentState->getXDot());
111  RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot();
112  const Scalar dt = workingState->getTimeStep();
113 
114  if (!(this->getUseFSAL()) || workingState->getNConsecutiveFailures() != 0) {
115  // Need to compute XDotOld.
116  stepperFEAppAction_->execute(
117  solutionHistory, thisStepper,
119  Scalar>::ACTION_LOCATION::BEFORE_EXPLICIT_EVAL);
120 
122 
123  // Evaluate xDot = f(x,t).
124  this->evaluateExplicitODE(xDot, currentState->getX(),
125  currentState->getTime(), p);
126 
127  // For UseFSAL=false, x and xDot are now sync'ed or consistent
128  // at the same time level for the currentState.
129  currentState->setIsSynced(true);
130  }
131 
132  // Forward Euler update, x^n = x^{n-1} + dt^n * xDot^{n-1}
133  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getX())),
134  *(currentState->getX()), dt, *(xDot));
135 
136  if (workingState->getXDot() != Teuchos::null)
137  this->setStepperXDot(workingState->getXDot());
138  xDot = this->getStepperXDot();
139 
140  if (this->getUseFSAL()) {
141  // Get consistent xDot^n.
142  stepperFEAppAction_->execute(
143  solutionHistory, thisStepper,
145  Scalar>::ACTION_LOCATION::BEFORE_EXPLICIT_EVAL);
146 
148 
149  // Evaluate xDot = f(x,t).
150  this->evaluateExplicitODE(xDot, workingState->getX(),
151  workingState->getTime(), p);
152 
153  // For UseFSAL=true, x and xDot are now sync'ed or consistent
154  // for the workingState.
155  workingState->setIsSynced(true);
156  }
157  else {
158  assign(xDot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
159  workingState->setIsSynced(false);
160  }
161 
162  workingState->setSolutionStatus(Status::PASSED);
163  workingState->setOrder(this->getOrder());
164  workingState->computeNorms(currentState);
165  stepperFEAppAction_->execute(
166  solutionHistory, thisStepper,
168  }
169  return;
170 }
171 
178 template <class Scalar>
181 {
183  rcp(new StepperState<Scalar>(this->getStepperType()));
184  return stepperState;
185 }
186 
187 template <class Scalar>
189  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
190 {
191  auto l_out = Teuchos::fancyOStream(out.getOStream());
192  Teuchos::OSTab ostab(*l_out, 2, this->description());
193  l_out->setOutputToRootOnly(0);
194 
195  *l_out << std::endl;
196  Stepper<Scalar>::describe(*l_out, verbLevel);
197  StepperExplicit<Scalar>::describe(*l_out, verbLevel);
198  *l_out << " stepperFEAppAction_ = " << stepperFEAppAction_ << std::endl
199  << "----------------------------" << std::endl;
200 }
201 
202 template <class Scalar>
204 {
205  out.setOutputToRootOnly(0);
206 
207  bool isValidSetup = true;
208 
209  if (!Stepper<Scalar>::isValidSetup(out)) isValidSetup = false;
210  if (!StepperExplicit<Scalar>::isValidSetup(out)) isValidSetup = false;
211  if (stepperFEAppAction_ == Teuchos::null) {
212  isValidSetup = false;
213  out << "The Forward Euler AppAction is not set!\n";
214  }
215  return isValidSetup;
216 }
217 
218 // Nonmember constructor - ModelEvaluator and ParameterList
219 // ------------------------------------------------------------------------
220 template <class Scalar>
222  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
224 {
225  auto stepper = Teuchos::rcp(new StepperForwardEuler<Scalar>());
226  stepper->setStepperExplicitValues(pl);
227 
228  if (model != Teuchos::null) {
229  stepper->setModel(model);
230  stepper->initialize();
231  }
232 
233  return stepper;
234 }
235 
236 } // namespace Tempus
237 #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.