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