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  this->setDefaultSolver();
36 }
37 
38 
39 template<class Scalar>
41  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
42  const Teuchos::RCP<StepperObserver<Scalar> >& obs,
43  const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
44  bool useFSAL,
45  std::string ICConsistency,
46  bool ICConsistencyCheck,
47  bool zeroInitialGuess)
48 
49 {
50  this->setStepperType( "Trapezoidal Method");
51  this->setUseFSAL( useFSAL);
52  this->setICConsistency( ICConsistency);
53  this->setICConsistencyCheck( ICConsistencyCheck);
54  this->setZeroInitialGuess( zeroInitialGuess);
55 
56  this->setObserver(obs);
57  this->setSolver(solver);
58 
59  if (appModel != Teuchos::null) {
60  this->setModel(appModel);
61  this->initialize();
62  }
63 }
64 
65 
66 template<class Scalar>
68  Teuchos::RCP<StepperObserver<Scalar> > obs)
69 {
70  if (obs == Teuchos::null) {
71  // Create default observer, otherwise keep current observer.
72  if (this->stepperObserver_ == Teuchos::null) {
73  stepperTrapObserver_ =
74  Teuchos::rcp(new StepperTrapezoidalObserver<Scalar>());
75  this->stepperObserver_ =
76  Teuchos::rcp_dynamic_cast<StepperObserver<Scalar> >
77  (stepperTrapObserver_, true);
78  }
79  } else {
80  this->stepperObserver_ = obs;
81  stepperTrapObserver_ =
82  Teuchos::rcp_dynamic_cast<StepperTrapezoidalObserver<Scalar> >
83  (this->stepperObserver_, true);
84  }
85 
86  this->isInitialized_ = false;
87 }
88 
89 
90 template<class Scalar>
92  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
93 {
94  using Teuchos::RCP;
95 
96  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
97 
98  // Check if we need Stepper storage for xDot
99  if (initialState->getXDot() == Teuchos::null)
100  this->setStepperXDot(initialState->getX()->clone_v());
101 
103 
104  TEUCHOS_TEST_FOR_EXCEPTION( !(this->getUseFSAL()), std::logic_error,
105  "Error - The First-Step-As-Last (FSAL) principle is required\n"
106  " for the Trapezoidal Stepper (i.e., useFSAL=true)!\n");
107 // There are at least two ways around this, but are not implemented.
108 // - Do a solve for xDotOld, xDot_{n-1}, at each time step as for the
109 // initial conditions. This is expensive since you would be doing
110 // two solves every time step.
111 // - Use evaluateExplicitODE to get xDot_{n-1} if the application
112 // provides it. Explicit evaluations are cheaper but requires the
113 // application to implement xDot = f(x,t).
114 }
115 
116 
117 template<class Scalar>
119  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
120 {
121  this->checkInitialized();
122 
123  using Teuchos::RCP;
124 
125  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperTrapezoidal::takeStep()");
126  {
127  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
128  std::logic_error,
129  "Error - StepperTrapezoidal<Scalar>::takeStep(...)\n"
130  "Need at least two SolutionStates for Trapezoidal.\n"
131  " Number of States = " << solutionHistory->getNumStates() << "\n"
132  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
133  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
134 
135  this->stepperObserver_->observeBeginTakeStep(solutionHistory, *this);
136  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
137  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
138 
139  RCP<const Thyra::VectorBase<Scalar> > xOld = currentState->getX();
140  RCP<const Thyra::VectorBase<Scalar> > xDotOld = currentState->getXDot();
141  RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
142  RCP<Thyra::VectorBase<Scalar> > xDot = workingState->getXDot();
143 
144  const Scalar time = workingState->getTime();
145  const Scalar dt = workingState->getTimeStep();
146  const Scalar alpha = getAlpha(dt);
147  const Scalar beta = getBeta (dt);
148 
149  // Setup TimeDerivative
150  Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
152  alpha, xOld, xDotOld));
153 
154  auto p = Teuchos::rcp(new ImplicitODEParameters<Scalar>(
155  timeDer, dt, alpha, beta));
156 
157  stepperTrapObserver_->observeBeforeSolve(solutionHistory, *this);
158 
159  const Thyra::SolveStatus<Scalar> sStatus =
160  this->solveImplicitODE(x, xDot, time, p);
161 
162  stepperTrapObserver_->observeAfterSolve(solutionHistory, *this);
163 
164  if (workingState->getXDot() != Teuchos::null)
165  timeDer->compute(x, xDot);
166 
167  workingState->setSolutionStatus(sStatus); // Converged --> pass.
168  workingState->setOrder(this->getOrder());
169  workingState->computeNorms(currentState);
170  this->stepperObserver_->observeEndTakeStep(solutionHistory, *this);
171  }
172  return;
173 }
174 
175 
176 /** \brief Provide a StepperState to the SolutionState.
177  * This Stepper does not have any special state data,
178  * so just provide the base class StepperState with the
179  * Stepper description. This can be checked to ensure
180  * that the input StepperState can be used by this Stepper.
181  */
182 template<class Scalar>
183 Teuchos::RCP<Tempus::StepperState<Scalar> >
186 {
187  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
188  rcp(new StepperState<Scalar>(this->getStepperType()));
189  return stepperState;
190 }
191 
192 
193 template<class Scalar>
195  Teuchos::FancyOStream &out,
196  const Teuchos::EVerbosityLevel verbLevel) const
197 {
198  out << std::endl;
199  Stepper<Scalar>::describe(out, verbLevel);
200  StepperImplicit<Scalar>::describe(out, verbLevel);
201 
202  out << "--- StepperTrapezoidal ---\n";
203  out << " stepperTrapObserver_ = " << stepperTrapObserver_ << std::endl;
204  out << "--------------------------" << std::endl;
205 }
206 
207 
208 template<class Scalar>
209 bool StepperTrapezoidal<Scalar>::isValidSetup(Teuchos::FancyOStream & out) const
210 {
211  bool isValidSetup = true;
212 
213  if ( !Stepper<Scalar>::isValidSetup(out) ) isValidSetup = false;
214  if ( !StepperImplicit<Scalar>::isValidSetup(out) ) isValidSetup = false;
215 
216  if (stepperTrapObserver_ == Teuchos::null) {
217  isValidSetup = false;
218  out << "The Trapezoidal observer is not set!\n";
219  }
220 
221  return isValidSetup;
222 }
223 
224 
225 template<class Scalar>
226 Teuchos::RCP<const Teuchos::ParameterList>
228 {
229  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
230  getValidParametersBasic(pl, this->getStepperType());
231  pl->set<bool> ("Use FSAL", this->getUseFSALDefault());
232  pl->set<std::string>("Initial Condition Consistency",
233  this->getICConsistencyDefault());
234  pl->set<std::string>("Solver Name", "Default Solver");
235  pl->set<bool> ("Zero Initial Guess", false);
236  Teuchos::RCP<Teuchos::ParameterList> solverPL = defaultSolverParameters();
237  pl->set("Default Solver", *solverPL);
238 
239  return pl;
240 }
241 
242 
243 } // namespace Tempus
244 #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.
Thyra Base interface for time 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.
Thyra Base interface for implicit time steppers.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
StepperTrapezoidalObserver class for StepperTrapezoidal.
StepperObserver class for Stepper class.
Time-derivative interface for Trapezoidal method.
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
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.