Tempus  Version of the Day
Time Integration
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 // StepperTrapezoidal definitions:
26 template<class Scalar>
28  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
29  Teuchos::RCP<Teuchos::ParameterList> pList)
30 {
31  // Set all the input parameters and call initialize
32  this->setParameterList(pList);
33  this->setModel(appModel);
34  this->initialize();
35 }
36 
37 
38 template<class Scalar>
40  Teuchos::RCP<StepperObserver<Scalar> > obs)
41 {
42  if (obs == Teuchos::null) {
43  // Create default observer, otherwise keep current observer.
44  if (stepperObserver_ == Teuchos::null) {
45  stepperTrapObserver_ =
46  Teuchos::rcp(new StepperTrapezoidalObserver<Scalar>());
47  stepperObserver_ =
48  Teuchos::rcp_dynamic_cast<StepperObserver<Scalar> >
49  (stepperTrapObserver_);
50  }
51  } else {
52  stepperObserver_ = obs;
53  stepperTrapObserver_ =
54  Teuchos::rcp_dynamic_cast<StepperTrapezoidalObserver<Scalar> >
55  (stepperObserver_);
56  }
57 }
58 
59 
60 template<class Scalar>
62 {
63  TEUCHOS_TEST_FOR_EXCEPTION(
64  this->wrapperModel_ == Teuchos::null, std::logic_error,
65  "Error - Need to set the model, setModel(), before calling "
66  "StepperTrapezoidal::initialize()\n");
67 
68  this->setParameterList(this->stepperPL_);
69  this->setSolver();
70  this->setObserver();
71 }
72 
73 
74 template<class Scalar>
76  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
77 {
78  using Teuchos::RCP;
79 
80  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperTrapezoidal::takeStep()");
81  {
82  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
83  std::logic_error,
84  "Error - StepperTrapezoidal<Scalar>::takeStep(...)\n"
85  "Need at least two SolutionStates for Trapezoidal.\n"
86  " Number of States = " << solutionHistory->getNumStates() << "\n"
87  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
88  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
89 
90  stepperObserver_->observeBeginTakeStep(solutionHistory, *this);
91  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
92  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
93 
94  RCP<const Thyra::VectorBase<Scalar> > xOld = currentState->getX();
95  RCP<const Thyra::VectorBase<Scalar> > xDotOld = currentState->getXDot();
96  RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
97  RCP<Thyra::VectorBase<Scalar> > xDot = workingState->getXDot();
98  if (xDot == Teuchos::null) xDot = getXDotTemp(x);
99 
100  const Scalar time = workingState->getTime();
101  const Scalar dt = workingState->getTimeStep();
102  const Scalar alpha = 2.0/dt;
103 
104  // Setup TimeDerivative
105  Teuchos::RCP<TimeDerivative<Scalar> > timeDer =
107  alpha, xOld, xDotOld));
108 
109  // Setup InArgs and OutArgs
110  typedef Thyra::ModelEvaluatorBase MEB;
111  MEB::InArgs<Scalar> inArgs = this->wrapperModel_->getInArgs();
112  MEB::OutArgs<Scalar> outArgs = this->wrapperModel_->getOutArgs();
113  inArgs.set_x(x);
114  if (inArgs.supports(MEB::IN_ARG_x_dot )) inArgs.set_x_dot (xDot);
115  if (inArgs.supports(MEB::IN_ARG_t )) inArgs.set_t (time);
116  if (inArgs.supports(MEB::IN_ARG_step_size)) inArgs.set_step_size(dt);
117  if (inArgs.supports(MEB::IN_ARG_alpha )) inArgs.set_alpha (alpha);
118  if (inArgs.supports(MEB::IN_ARG_beta )) inArgs.set_beta (1.0);
119 
120  this->wrapperModel_->setForSolve(timeDer, inArgs, outArgs);
121 
122  stepperTrapObserver_->observeBeforeSolve(solutionHistory, *this);
123 
124  const Thyra::SolveStatus<Scalar> sStatus = this->solveImplicitODE(x);
125 
126  stepperTrapObserver_->observeAfterSolve(solutionHistory, *this);
127 
128  if (workingState->getXDot() != Teuchos::null)
129  timeDer->compute(x, xDot);
130 
131  if (sStatus.solveStatus == Thyra::SOLVE_STATUS_CONVERGED)
132  workingState->setSolutionStatus(Status::PASSED);
133  else
134  workingState->setSolutionStatus(Status::FAILED);
135  workingState->setOrder(this->getOrder());
136  stepperObserver_->observeEndTakeStep(solutionHistory, *this);
137  }
138  return;
139 }
140 
141 template<class Scalar>
142 Teuchos::RCP<Thyra::VectorBase<Scalar> >
144 getXDotTemp(Teuchos::RCP<Thyra::VectorBase<Scalar> > x)
145 {
146  if (xDotTemp_ == Teuchos::null) {
147  xDotTemp_ = x->clone_v();
148  Thyra::assign(xDotTemp_.ptr(), Scalar(0.0));
149  }
150  return xDotTemp_;
151 }
152 
153 /** \brief Provide a StepperState to the SolutionState.
154  * This Stepper does not have any special state data,
155  * so just provide the base class StepperState with the
156  * Stepper description. This can be checked to ensure
157  * that the input StepperState can be used by this Stepper.
158  */
159 template<class Scalar>
160 Teuchos::RCP<Tempus::StepperState<Scalar> >
163 {
164  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
165  rcp(new StepperState<Scalar>(description()));
166  return stepperState;
167 }
168 
169 
170 template<class Scalar>
172 {
173  std::string name = "Trapezoidal Method";
174  return(name);
175 }
176 
177 
178 template<class Scalar>
180  Teuchos::FancyOStream &out,
181  const Teuchos::EVerbosityLevel verbLevel) const
182 {
183  out << description() << "::describe:" << std::endl
184  << "wrapperModel_ = " << this->wrapperModel_->description() << std::endl;
185 }
186 
187 
188 template <class Scalar>
190  Teuchos::RCP<Teuchos::ParameterList> const& pList)
191 {
192  Teuchos::RCP<Teuchos::ParameterList> stepperPL = this->stepperPL_;
193  if (pList == Teuchos::null) {
194  // Create default parameters if null, otherwise keep current parameters.
195  if (stepperPL == Teuchos::null) stepperPL = this->getDefaultParameters();
196  } else {
197  stepperPL = pList;
198  }
199  if (!(stepperPL->isParameter("Solver Name"))) {
200  stepperPL->set<std::string>("Solver Name", "Default Solver");
201  Teuchos::RCP<Teuchos::ParameterList> solverPL =
202  this->defaultSolverParameters();
203  stepperPL->set("Default Solver", *solverPL);
204  }
205  // Can not validate because of optional Parameters (e.g., Solver Name).
206  // stepperPL->validateParametersAndSetDefaults(*this->getValidParameters());
207 
208  std::string stepperType = stepperPL->get<std::string>("Stepper Type");
209  TEUCHOS_TEST_FOR_EXCEPTION( stepperType != "Trapezoidal Method",
210  std::logic_error,
211  "Error - Stepper Type is not 'Trapezoidal Method'!\n"
212  << " Stepper Type = "<<stepperPL->get<std::string>("Stepper Type")<<"\n");
213 
214  this->stepperPL_ = stepperPL;
215 }
216 
217 
218 template<class Scalar>
219 Teuchos::RCP<const Teuchos::ParameterList>
221 {
222  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
223  pl->setName("Default Stepper - " + this->description());
224  pl->set("Stepper Type", this->description());
225  pl->set("Zero Initial Guess", false);
226  pl->set("Solver Name", "",
227  "Name of ParameterList containing the solver specifications.");
228 
229  return pl;
230 }
231 
232 
233 template<class Scalar>
234 Teuchos::RCP<Teuchos::ParameterList>
236 {
237  using Teuchos::RCP;
238  using Teuchos::ParameterList;
239 
240  RCP<ParameterList> pl = Teuchos::parameterList();
241  pl->setName("Default Stepper - " + this->description());
242  pl->set<std::string>("Stepper Type", this->description());
243  pl->set<bool> ("Zero Initial Guess", false);
244  pl->set<std::string>("Solver Name", "Default Solver");
245 
246  RCP<ParameterList> solverPL = this->defaultSolverParameters();
247  pl->set("Default Solver", *solverPL);
248 
249  return pl;
250 }
251 
252 
253 template <class Scalar>
254 Teuchos::RCP<Teuchos::ParameterList>
256 {
257  return(this->stepperPL_);
258 }
259 
260 
261 template <class Scalar>
262 Teuchos::RCP<Teuchos::ParameterList>
264 {
265  Teuchos::RCP<Teuchos::ParameterList> temp_plist = this->stepperPL_;
266  this->stepperPL_ = Teuchos::null;
267  return(temp_plist);
268 }
269 
270 
271 } // namespace Tempus
272 #endif // Tempus_StepperTrapezoidal_impl_hpp
Tempus::StepperState
StepperState is a simple class to hold state information about the stepper.
Definition: Tempus_StepperState.hpp:36
Tempus::StepperTrapezoidal::getValidParameters
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Definition: Tempus_StepperTrapezoidal_impl.hpp:220
Tempus::solutionHistory
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
Definition: Tempus_SolutionHistory_impl.hpp:504
Tempus::StepperTrapezoidal::getDefaultStepperState
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
Definition: Tempus_StepperTrapezoidal_impl.hpp:162
Tempus::StepperTrapezoidal::getDefaultParameters
Teuchos::RCP< Teuchos::ParameterList > getDefaultParameters() const
Definition: Tempus_StepperTrapezoidal_impl.hpp:235
Tempus::StepperTrapezoidal::StepperTrapezoidal
StepperTrapezoidal()
Default Constructor – not allowed.
Tempus
Definition: Tempus_AdjointAuxSensitivityModelEvaluator_decl.hpp:20
Tempus::StepperTrapezoidal::initialize
virtual void initialize()
Initialize during construction and after changing input parameters.
Definition: Tempus_StepperTrapezoidal_impl.hpp:61
Tempus::StepperTrapezoidal::describe
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Definition: Tempus_StepperTrapezoidal_impl.hpp:179
Tempus::StepperTrapezoidal::unsetParameterList
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Definition: Tempus_StepperTrapezoidal_impl.hpp:263
Tempus::StepperObserver
StepperObserver class for Stepper class.
Definition: Tempus_StepperObserver.hpp:38
Tempus::StepperTrapezoidal::description
virtual std::string description() const
Definition: Tempus_StepperTrapezoidal_impl.hpp:171
Tempus::StepperTrapezoidal::setObserver
virtual void setObserver(Teuchos::RCP< StepperObserver< Scalar > > obs=Teuchos::null)
Set Observer.
Definition: Tempus_StepperTrapezoidal_impl.hpp:39
Tempus::StepperTrapezoidalObserver
StepperTrapezoidalObserver class for StepperTrapezoidal.
Definition: Tempus_StepperTrapezoidalObserver.hpp:35
Tempus::StepperTrapezoidal::setParameterList
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl)
Definition: Tempus_StepperTrapezoidal_impl.hpp:189
Tempus::PASSED
Definition: Tempus_Types.hpp:17
Tempus::SolutionHistory
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Definition: Tempus_Integrator.hpp:25
Tempus::StepperTrapezoidal::takeStep
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
Definition: Tempus_StepperTrapezoidal_impl.hpp:75
Tempus::FAILED
Definition: Tempus_Types.hpp:18
Tempus::StepperTrapezoidalTimeDerivative
Time-derivative interface for Trapezoidal method.
Definition: Tempus_StepperTrapezoidal_decl.hpp:115
Tempus::StepperTrapezoidal::getXDotTemp
virtual Teuchos::RCP< Thyra::VectorBase< Scalar > > getXDotTemp(Teuchos::RCP< Thyra::VectorBase< Scalar > > x)
Provide temporary xDot memory for Stepper if SolutionState doesn't.
Definition: Tempus_StepperTrapezoidal_impl.hpp:144
Tempus::StepperTrapezoidal::getNonconstParameterList
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Definition: Tempus_StepperTrapezoidal_impl.hpp:255
Tempus_StepperFactory.hpp