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"
14 
15 
16 namespace Tempus {
17 
18 
19 template<class Scalar>
21 {
22  this->setStepperType( "Forward Euler");
23  this->setUseFSAL( this->getUseFSALDefault());
24  this->setICConsistency( this->getICConsistencyDefault());
25  this->setICConsistencyCheck( this->getICConsistencyCheckDefault());
26 
27  this->setObserver();
28 }
29 
30 
31 template<class Scalar>
33  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
34  const Teuchos::RCP<StepperObserver<Scalar> >& obs,
35  bool useFSAL,
36  std::string ICConsistency,
37  bool ICConsistencyCheck)
38 {
39  this->setStepperType( "Forward Euler");
40  this->setUseFSAL( useFSAL);
41  this->setICConsistency( ICConsistency);
42  this->setICConsistencyCheck( ICConsistencyCheck);
43 
44  this->setObserver(obs);
45 
46  if (appModel != Teuchos::null) {
47  this->setModel(appModel);
48  this->initialize();
49  }
50 }
51 
52 
53 template<class Scalar>
55  Teuchos::RCP<StepperObserver<Scalar> > obs)
56 {
57  if (obs == Teuchos::null) {
58  // Create default observer, otherwise keep current observer.
59  if (this->stepperObserver_ == Teuchos::null) {
60  stepperFEObserver_ =
61  Teuchos::rcp(new StepperForwardEulerObserver<Scalar>());
62  this->stepperObserver_ =
63  Teuchos::rcp_dynamic_cast<StepperObserver<Scalar> >(stepperFEObserver_,true);
64  }
65  } else {
66  this->stepperObserver_ = obs;
67  stepperFEObserver_ =
68  Teuchos::rcp_dynamic_cast<StepperForwardEulerObserver<Scalar> >
69  (this->stepperObserver_,true);
70  }
71 }
72 
73 template<class Scalar>
75 {
76  TEUCHOS_TEST_FOR_EXCEPTION(
77  this->appModel_ == Teuchos::null, std::logic_error,
78  "Error - Need to set the model, setModel(), before calling "
79  "StepperForwardEuler::initialize()\n");
80 }
81 
82 template<class Scalar>
84  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
85 {
86  using Teuchos::RCP;
87 
88  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
89 
90  // Check if we need Stepper storage for xDot
91  if (initialState->getXDot() == Teuchos::null)
92  this->setStepperXDot(initialState->getX()->clone_v());
93 
95 }
96 
97 template<class Scalar>
99  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
100 {
101  using Teuchos::RCP;
102 
103  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperForwardEuler::takeStep()");
104  {
105  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
106  std::logic_error,
107  "Error - StepperForwardEuler<Scalar>::takeStep(...)\n"
108  "Need at least two SolutionStates for Forward Euler.\n"
109  " Number of States = " << solutionHistory->getNumStates() << "\n"
110  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
111  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
112 
113  this->stepperObserver_->observeBeginTakeStep(solutionHistory, *this);
114  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
115  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
116 
117  RCP<Thyra::VectorBase<Scalar> > xDot = this->getStepperXDot(currentState);
118  const Scalar dt = workingState->getTimeStep();
119 
120  if ( !(this->getUseFSAL()) ) {
121  // Need to compute XDotOld.
122  if (!Teuchos::is_null(stepperFEObserver_))
123  stepperFEObserver_->observeBeforeExplicit(solutionHistory, *this);
124 
125  auto p = Teuchos::rcp(new ExplicitODEParameters<Scalar>(dt));
126 
127  // Evaluate xDot = f(x,t).
128  this->evaluateExplicitODE(xDot, currentState->getX(),
129  currentState->getTime(), p);
130 
131  // For UseFSAL=false, x and xDot are now sync'ed or consistent
132  // at the same time level for the currentState.
133  currentState->setIsSynced(true);
134  }
135 
136 
137  // Forward Euler update, x^n = x^{n-1} + dt^n * xDot^{n-1}
138  Thyra::V_VpStV(Teuchos::outArg(*(workingState->getX())),
139  *(currentState->getX()),dt,*(xDot));
140 
141 
142  xDot = this->getStepperXDot(workingState);
143 
144  if (this->getUseFSAL()) {
145  // Get consistent xDot^n.
146  if (!Teuchos::is_null(stepperFEObserver_))
147  stepperFEObserver_->observeBeforeExplicit(solutionHistory, *this);
148 
149  auto p = Teuchos::rcp(new ExplicitODEParameters<Scalar>(dt));
150 
151  // Evaluate xDot = f(x,t).
152  this->evaluateExplicitODE(xDot, workingState->getX(),
153  workingState->getTime(), p);
154 
155  // For UseFSAL=true, x and xDot are now sync'ed or consistent
156  // for the workingState.
157  workingState->setIsSynced(true);
158  } else {
159  assign(xDot.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
160  workingState->setIsSynced(false);
161  }
162 
163  workingState->setSolutionStatus(Status::PASSED);
164  workingState->setOrder(this->getOrder());
165  this->stepperObserver_->observeEndTakeStep(solutionHistory, *this);
166  }
167  return;
168 }
169 
170 
171 /** \brief Provide a StepperState to the SolutionState.
172  * This Stepper does not have any special state data,
173  * so just provide the base class StepperState with the
174  * Stepper description. This can be checked to ensure
175  * that the input StepperState can be used by this Stepper.
176  */
177 template<class Scalar>
178 Teuchos::RCP<Tempus::StepperState<Scalar> > StepperForwardEuler<Scalar>::
180 {
181  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
182  rcp(new StepperState<Scalar>(this->getStepperType()));
183  return stepperState;
184 }
185 
186 
187 template<class Scalar>
189  Teuchos::FancyOStream &out,
190  const Teuchos::EVerbosityLevel /* verbLevel */) const
191 {
192  out << this->getStepperType() << "::describe:" << std::endl
193  << "appModel_ = " << this->appModel_->description() << std::endl;
194 }
195 
196 
197 template<class Scalar>
198 Teuchos::RCP<const Teuchos::ParameterList>
200 {
201  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
202  getValidParametersBasic(pl, this->getStepperType());
203  pl->set<bool>("Use FSAL", true);
204  pl->set<std::string>("Initial Condition Consistency", "Consistent");
205  return pl;
206 }
207 
208 
209 } // namespace Tempus
210 #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.
virtual void initialize()
Initialize during construction and after changing input parameters.
StepperForwardEulerObserver class for StepperForwardEuler.
StepperState is a simple class to hold state information about the stepper.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions, make them consistent, and set needed memory.
StepperObserver class for Stepper class.
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 Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
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
void getValidParametersBasic(Teuchos::RCP< Teuchos::ParameterList > pl, std::string stepperType)
Provide basic parameters to Steppers.