Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_StepperTrapezoidal_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_StepperTrapezoidal_impl_hpp
10 #define Tempus_StepperTrapezoidal_impl_hpp
11 
12 #include "Tempus_config.hpp"
14 #include "Tempus_WrapperModelEvaluatorBasic.hpp"
15 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
16 #include "NOX_Thyra.H"
17 
18 
19 namespace Tempus {
20 
21 // Forward Declaration for recursive includes (this Stepper <--> StepperFactory)
22 template<class Scalar> class StepperFactory;
23 
24 
25 template<class Scalar>
27 {
28  this->setStepperType( "Trapezoidal Method");
29  this->setUseFSAL( this->getUseFSALDefault());
30  this->setICConsistency( this->getICConsistencyDefault());
31  this->setICConsistencyCheck( this->getICConsistencyCheckDefault());
32  this->setZeroInitialGuess( false);
33 
34  this->setObserver();
35 }
36 
37 
38 template<class Scalar>
40  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
41  const Teuchos::RCP<StepperObserver<Scalar> >& obs,
42  const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
43  bool useFSAL,
44  std::string ICConsistency,
45  bool ICConsistencyCheck,
46  bool zeroInitialGuess)
47 
48 {
49  this->setStepperType( "Trapezoidal Method");
50  this->setUseFSAL( useFSAL);
51  this->setICConsistency( ICConsistency);
52  this->setICConsistencyCheck( ICConsistencyCheck);
53  this->setZeroInitialGuess( zeroInitialGuess);
54 
55  this->setObserver(obs);
56 
57  if (appModel != Teuchos::null) {
58  this->setModel(appModel);
59  this->setSolver(solver);
60  this->initialize();
61  }
62 }
63 
64 
65 template<class Scalar>
67  Teuchos::RCP<StepperObserver<Scalar> > obs)
68 {
69  if (obs == Teuchos::null) {
70  // Create default observer, otherwise keep current observer.
71  if (this->stepperObserver_ == Teuchos::null) {
72  stepperTrapObserver_ =
73  Teuchos::rcp(new StepperTrapezoidalObserver<Scalar>());
74  this->stepperObserver_ =
75  Teuchos::rcp_dynamic_cast<StepperObserver<Scalar> >
76  (stepperTrapObserver_, true);
77  }
78  } else {
79  this->stepperObserver_ = obs;
80  stepperTrapObserver_ =
81  Teuchos::rcp_dynamic_cast<StepperTrapezoidalObserver<Scalar> >
82  (this->stepperObserver_, true);
83  }
84 }
85 
86 
87 template<class Scalar>
89 {
90  TEUCHOS_TEST_FOR_EXCEPTION(
91  this->wrapperModel_ == Teuchos::null, std::logic_error,
92  "Error - Need to set the model, setModel(), before calling "
93  "StepperTrapezoidal::initialize()\n");
94 }
95 
96 
97 template<class Scalar>
99  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
100 {
101  using Teuchos::RCP;
102 
103  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
104 
105  // Check if we need Stepper storage for xDot
106  if (initialState->getXDot() == Teuchos::null)
107  this->setStepperXDot(initialState->getX()->clone_v());
108 
110 
111  TEUCHOS_TEST_FOR_EXCEPTION( !(this->getUseFSAL()), std::logic_error,
112  "Error - The First-Step-As-Last (FSAL) principle is required\n"
113  " for the Trapezoidal Stepper (i.e., useFSAL=true)!\n");
114 // There are at least two ways around this, but are not implemented.
115 // - Do a solve for xDotOld, xDot_{n-1}, at each time step as for the
116 // initial conditions. This is expensive since you would be doing
117 // two solves every time step.
118 // - Use evaluateExplicitODE to get xDot_{n-1} if the application
119 // provides it. Explicit evaluations are cheaper but requires the
120 // application to implement xDot = f(x,t).
121 }
122 
123 
124 template<class Scalar>
126  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
127 {
128  using Teuchos::RCP;
129 
130  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperTrapezoidal::takeStep()");
131  {
132  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
133  std::logic_error,
134  "Error - StepperTrapezoidal<Scalar>::takeStep(...)\n"
135  "Need at least two SolutionStates for Trapezoidal.\n"
136  " Number of States = " << solutionHistory->getNumStates() << "\n"
137  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
138  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
139 
140  this->stepperObserver_->observeBeginTakeStep(solutionHistory, *this);
141  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
142  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
143 
144  RCP<const Thyra::VectorBase<Scalar> > xOld = currentState->getX();
145  RCP<const Thyra::VectorBase<Scalar> > xDotOld = currentState->getXDot();
146  RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
147  RCP<Thyra::VectorBase<Scalar> > xDot = workingState->getXDot();
148 
149  const Scalar time = workingState->getTime();
150  const Scalar dt = workingState->getTimeStep();
151  const Scalar alpha = getAlpha(dt);
152  const Scalar beta = getBeta (dt);
153 
154  // Setup TimeDerivative
155  Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
157  alpha, xOld, xDotOld));
158 
159  auto p = Teuchos::rcp(new ImplicitODEParameters<Scalar>(
160  timeDer, dt, alpha, beta));
161 
162  stepperTrapObserver_->observeBeforeSolve(solutionHistory, *this);
163 
164  const Thyra::SolveStatus<Scalar> sStatus =
165  this->solveImplicitODE(x, xDot, time, p);
166 
167  stepperTrapObserver_->observeAfterSolve(solutionHistory, *this);
168 
169  if (workingState->getXDot() != Teuchos::null)
170  timeDer->compute(x, xDot);
171 
172  workingState->setSolutionStatus(sStatus); // Converged --> pass.
173  workingState->setOrder(this->getOrder());
174  this->stepperObserver_->observeEndTakeStep(solutionHistory, *this);
175  }
176  return;
177 }
178 
179 
180 /** \brief Provide a StepperState to the SolutionState.
181  * This Stepper does not have any special state data,
182  * so just provide the base class StepperState with the
183  * Stepper description. This can be checked to ensure
184  * that the input StepperState can be used by this Stepper.
185  */
186 template<class Scalar>
187 Teuchos::RCP<Tempus::StepperState<Scalar> >
190 {
191  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
192  rcp(new StepperState<Scalar>(this->getStepperType()));
193  return stepperState;
194 }
195 
196 
197 template<class Scalar>
199  Teuchos::FancyOStream &out,
200  const Teuchos::EVerbosityLevel /* verbLevel */) const
201 {
202  out << this->getStepperType() << "::describe:" << std::endl
203  << "wrapperModel_ = " << this->wrapperModel_->description() << std::endl;
204 }
205 
206 
207 template<class Scalar>
208 Teuchos::RCP<const Teuchos::ParameterList>
210 {
211  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
212  getValidParametersBasic(pl, this->getStepperType());
213  pl->set<bool> ("Use FSAL", this->getUseFSALDefault());
214  pl->set<std::string>("Initial Condition Consistency",
215  this->getICConsistencyDefault());
216  pl->set<std::string>("Solver Name", "Default Solver");
217  pl->set<bool> ("Zero Initial Guess", false);
218  Teuchos::RCP<Teuchos::ParameterList> solverPL = defaultSolverParameters();
219  pl->set("Default Solver", *solverPL);
220 
221  return pl;
222 }
223 
224 
225 } // namespace Tempus
226 #endif // Tempus_StepperTrapezoidal_impl_hpp
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
StepperState is a simple class to hold state information about the stepper.
StepperTrapezoidalObserver class for StepperTrapezoidal.
virtual void initialize()
Initialize during construction and after changing input parameters.
StepperObserver class for Stepper class.
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
Time-derivative interface for Trapezoidal method.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
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 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.