Tempus  Version of the Day
Time Integration
Tempus_StepperNewmarkExplicitAForm_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_StepperNewmarkExplicitAForm_impl_hpp
10 #define Tempus_StepperNewmarkExplicitAForm_impl_hpp
11 
12 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
13 #include "Thyra_VectorStdOps.hpp"
14 
15 //#define DEBUG_OUTPUT
16 
17 namespace Tempus {
18 
19 
20 template<class Scalar>
22 predictVelocity(Thyra::VectorBase<Scalar>& vPred,
23  const Thyra::VectorBase<Scalar>& v,
24  const Thyra::VectorBase<Scalar>& a,
25  const Scalar dt) const
26 {
27  //vPred = v + dt*(1.0-gamma_)*a
28  Thyra::V_StVpStV(Teuchos::ptrFromRef(vPred), 1.0, v, dt*(1.0-gamma_), a);
29 }
30 
31 template<class Scalar>
33 predictDisplacement(Thyra::VectorBase<Scalar>& dPred,
34  const Thyra::VectorBase<Scalar>& d,
35  const Thyra::VectorBase<Scalar>& v,
36  const Thyra::VectorBase<Scalar>& a,
37  const Scalar dt) const
38 {
39  Teuchos::RCP<const Thyra::VectorBase<Scalar> > tmp =
40  Thyra::createMember<Scalar>(dPred.space());
41  //dPred = dt*v + dt*dt/2.0*a
42  Scalar aConst = dt*dt/2.0;
43  Thyra::V_StVpStV(Teuchos::ptrFromRef(dPred), dt, v, aConst, a);
44  //dPred += d;
45  Thyra::Vp_V(Teuchos::ptrFromRef(dPred), d, 1.0);
46 }
47 
48 template<class Scalar>
50 correctVelocity(Thyra::VectorBase<Scalar>& v,
51  const Thyra::VectorBase<Scalar>& vPred,
52  const Thyra::VectorBase<Scalar>& a,
53  const Scalar dt) const
54 {
55  //v = vPred + dt*gamma_*a
56  Thyra::V_StVpStV(Teuchos::ptrFromRef(v), 1.0, vPred, dt*gamma_, a);
57 }
58 
59 
60 template<class Scalar>
62  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
63  Teuchos::RCP<Teuchos::ParameterList> pList) :
64  out_(Teuchos::VerboseObjectBase::getDefaultOStream())
65 {
66  // Set all the input parameters and call initialize
67  this->setParameterList(pList);
68  this->setModel(appModel);
69  this->initialize();
70 }
71 
72 template<class Scalar>
74  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel)
75 {
76  this->validExplicitODE(appModel);
77  appModel_ = appModel;
78 
79  inArgs_ = appModel_->createInArgs();
80  outArgs_ = appModel_->createOutArgs();
81  inArgs_ = appModel_->getNominalValues();
82 }
83 
84 template<class Scalar>
86  const Teuchos::RCP<Thyra::ModelEvaluator<Scalar> >& appModel)
87 {
88  this->setModel(appModel);
89 }
90 
91 template<class Scalar>
93 {
94  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
95  Teuchos::OSTab ostab(out,1,"StepperNewmarkExplicitAForm::setSolver()");
96  *out << "Warning -- No solver to set for StepperNewmarkExplicitAForm "
97  << "(i.e., explicit method).\n" << std::endl;
98  return;
99 }
100 
101 template<class Scalar>
103  Teuchos::RCP<Teuchos::ParameterList> solverPL)
104 {
105  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
106  Teuchos::OSTab ostab(out,1,"StepperNewmarkExplicitAForm::setSolver()");
107  *out << "Warning -- No solver to set for StepperNewmarkExplicitAForm "
108  << "(i.e., explicit method).\n" << std::endl;
109  return;
110 }
111 
112 template<class Scalar>
114  Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> > solver)
115 {
116  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
117  Teuchos::OSTab ostab(out,1,"StepperNewmarkExplicitAForm::setSolver()");
118  *out << "Warning -- No solver to set for StepperNewmarkExplicitAForm "
119  << "(i.e., explicit method).\n" << std::endl;
120  return;
121 }
122 
123 template<class Scalar>
125 {
126  TEUCHOS_TEST_FOR_EXCEPTION(
127  this->appModel_ == Teuchos::null, std::logic_error,
128  "Error - Need to set the model, setModel(), before calling "
129  "StepperNewmarkExplicitAForm::initialize()\n");
130 
131  this->setParameterList(this->stepperPL_);
132 }
133 
134 template<class Scalar>
136  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
137 {
138  using Teuchos::RCP;
139 
140  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperNewmarkExplicitAForm::takeStep()");
141  {
142  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
143  std::logic_error,
144  "Error - StepperNewmarkExplicitAForm<Scalar>::takeStep(...)\n"
145  "Need at least two SolutionStates for NewmarkExplicitAForm.\n"
146  " Number of States = " << solutionHistory->getNumStates() << "\n"
147  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
148  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
149 
150  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
151  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
152 
153  //Get values of d, v and a from previous step
154  RCP<const Thyra::VectorBase<Scalar> > d_old = currentState->getX();
155  RCP<const Thyra::VectorBase<Scalar> > v_old = currentState->getXDot();
156  RCP<Thyra::VectorBase<Scalar> > a_old = currentState->getXDotDot();
157 
158  //Get dt and time
159  const Scalar dt = workingState->getTimeStep();
160  const Scalar time = currentState->getTime();
161 
162  typedef Thyra::ModelEvaluatorBase MEB;
163 
164 #ifdef DEBUG_OUTPUT
165  *out_ << "IKT d_old = " << Thyra::max(*d_old) << "\n";
166  *out_ << "IKT v_old = " << Thyra::max(*v_old) << "\n";
167  *out_ << "IKT a_old prescribed = " << Thyra::max(*a_old) << "\n";
168 #endif
169 
170  //Compute initial acceleration, a_old, using initial displacement (d_old) and initial
171  //velocity (v_old) if in 1st time step
172  //allocate a_init
173  RCP<Thyra::VectorBase<Scalar> > a_init = Thyra::createMember(d_old->space());
174  Thyra::put_scalar(0.0, a_init.ptr());
175  if (time == solutionHistory->minTime()) {
176  //Set x and x_dot in inArgs_ to be initial d and v, respectively
177  inArgs_.set_x(d_old);
178  inArgs_.set_x_dot(v_old);
179  if (inArgs_.supports(MEB::IN_ARG_t)) inArgs_.set_t(time);
180  // For model evaluators whose state function f(x, x_dot, x_dotdot, t) describes
181  // an implicit ODE, and which accept an optional x_dotdot input argument,
182  // make sure the latter is set to null in order to request the evaluation
183  // of a state function corresponding to the explicit ODE formulation
184  // x_dotdot = f(x, x_dot, t)
185  if (inArgs_.supports(MEB::IN_ARG_x_dot_dot)) inArgs_.set_x_dot_dot(Teuchos::null);
186  outArgs_.set_f(a_init);
187  appModel_->evalModel(inArgs_,outArgs_);
188  Thyra::copy(*a_init, a_old.ptr());
189 #ifdef DEBUG_OUTPUT
190  *out_ << "IKT a_init computed = " << Thyra::max(*a_old) << "\n";
191 #endif
192  }
193 
194 
195  //New d, v and a to be computed here
196  RCP<Thyra::VectorBase<Scalar> > d_new = workingState->getX();
197  RCP<Thyra::VectorBase<Scalar> > v_new = workingState->getXDot();
198  RCP<Thyra::VectorBase<Scalar> > a_new = workingState->getXDotDot();
199 
200  //allocate d and v predictors
201  RCP<Thyra::VectorBase<Scalar> > d_pred =Thyra::createMember(d_old->space());
202  RCP<Thyra::VectorBase<Scalar> > v_pred =Thyra::createMember(v_old->space());
203 
204  //compute displacement and velocity predictors
205  predictDisplacement(*d_pred, *d_old, *v_old, *a_old, dt);
206  predictVelocity(*v_pred, *v_old, *a_old, dt);
207 
208 #ifdef DEBUG_OUTPUT
209  *out_ << "IKT d_pred = " << Thyra::max(*d_pred) << "\n";
210  *out_ << "IKT v_pred = " << Thyra::max(*v_pred) << "\n";
211 #endif
212 
213  //Set x and x_dot in inArgs_ to be d and v predictors, respectively
214  inArgs_.set_x(d_pred);
215  inArgs_.set_x_dot(v_pred);
216  if (inArgs_.supports(MEB::IN_ARG_t)) inArgs_.set_t(currentState->getTime());
217 
218  // For model evaluators whose state function f(x, x_dot, x_dotdot, t) describes
219  // an implicit ODE, and which accept an optional x_dotdot input argument,
220  // make sure the latter is set to null in order to request the evaluation
221  // of a state function corresponding to the explicit ODE formulation
222  // x_dotdot = f(x, x_dot, t)
223  if (inArgs_.supports(MEB::IN_ARG_x_dot_dot)) inArgs_.set_x_dot_dot(Teuchos::null);
224  outArgs_.set_f(a_old);
225 
226  appModel_->evalModel(inArgs_,outArgs_);
227 
228  Thyra::copy(*(outArgs_.get_f()), a_new.ptr());
229 #ifdef DEBUG_OUTPUT
230  *out_ << "IKT a_new = " << Thyra::max(*(workingState()->getXDotDot())) << "\n";
231 #endif
232 
233  //Set x in workingState to displacement predictor
234  Thyra::copy(*d_pred, d_new.ptr());
235 
236  //set xdot in workingState to velocity corrector
237  correctVelocity(*v_new, *v_pred, *a_new, dt);
238 #ifdef DEBUG_OUTPUT
239  *out_ << "IKT d_new = " << Thyra::max(*(workingState()->getX())) << "\n";
240  *out_ << "IKT v_new = " << Thyra::max(*(workingState()->getXDot())) << "\n";
241 #endif
242 
243  workingState->setSolutionStatus(Status::PASSED);
244  workingState->setOrder(this->getOrder());
245  }
246  return;
247 }
248 
249 
250 /** \brief Provide a StepperState to the SolutionState.
251  * This Stepper does not have any special state data,
252  * so just provide the base class StepperState with the
253  * Stepper description. This can be checked to ensure
254  * that the input StepperState can be used by this Stepper.
255  */
256 template<class Scalar>
257 Teuchos::RCP<Tempus::StepperState<Scalar> > StepperNewmarkExplicitAForm<Scalar>::
259 {
260  Teuchos::RCP<Tempus::StepperState<Scalar> > stepperState =
261  rcp(new StepperState<Scalar>(description()));
262  return stepperState;
263 }
264 
265 
266 template<class Scalar>
268 {
269  std::string name = "Newmark Explicit a-Form";
270  return(name);
271 }
272 
273 
274 template<class Scalar>
276  Teuchos::FancyOStream &out,
277  const Teuchos::EVerbosityLevel verbLevel) const
278 {
279  out << description() << "::describe:" << std::endl
280  << "appModel_ = " << appModel_->description() << std::endl;
281 }
282 
283 
284 template <class Scalar>
286  const Teuchos::RCP<Teuchos::ParameterList> & pList)
287 {
288  if (pList == Teuchos::null) {
289  // Create default parameters if null, otherwise keep current parameters.
290  if (stepperPL_ == Teuchos::null) stepperPL_ = this->getDefaultParameters();
291  } else {
292  stepperPL_ = pList;
293  }
294  stepperPL_->validateParametersAndSetDefaults(*this->getValidParameters());
295 
296  std::string stepperType = stepperPL_->get<std::string>("Stepper Type");
297  TEUCHOS_TEST_FOR_EXCEPTION( stepperType != "Newmark Explicit a-Form",
298  std::logic_error,
299  "Error - Stepper Type is not 'Newmark Explicit a-Form'!\n"
300  << " Stepper Type = "<< pList->get<std::string>("Stepper Type") << "\n");
301  gamma_ = 0.5; //default value
302  if (stepperPL_->isSublist("Newmark Explicit Parameters")) {
303  Teuchos::ParameterList &newmarkPL =
304  stepperPL_->sublist("Newmark Explicit Parameters", true);
305  gamma_ = newmarkPL.get("Gamma", 0.5);
306  *out_ << "\nSetting Gamma = " << gamma_ << " from input file.\n";
307  }
308  TEUCHOS_TEST_FOR_EXCEPTION( (gamma_ > 1.0) || (gamma_ < 0.0),
309  std::logic_error,
310  "\nError in 'Newmark Explicit a-Form' stepper: invalid value of Gamma = " <<gamma_ << ". Please select Gamma >= 0 and <= 1. \n");
311 
312 }
313 
314 
315 template<class Scalar>
316 Teuchos::RCP<const Teuchos::ParameterList>
318 {
319  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
320  pl->setName("Default Stepper - " + this->description());
321  pl->set("Stepper Type", "Newmark Explicit a-Form",
322  "'Stepper Type' must be 'Newmark Explicit a-Form'.");
323  pl->sublist("Newmark Explicit Parameters", false, "");
324  pl->sublist("Newmark Explicit Parameters", false, "").set("Gamma",
325  0.5, "Newmark Explicit parameter");
326 
327  return pl;
328 }
329 
330 
331 template<class Scalar>
332 Teuchos::RCP<Teuchos::ParameterList>
334 {
335  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
336  *pl = *(this->getValidParameters());
337  return pl;
338 }
339 
340 
341 template <class Scalar>
342 Teuchos::RCP<Teuchos::ParameterList>
344 {
345  return(stepperPL_);
346 }
347 
348 
349 template <class Scalar>
350 Teuchos::RCP<Teuchos::ParameterList>
352 {
353  Teuchos::RCP<Teuchos::ParameterList> temp_plist = stepperPL_;
354  stepperPL_ = Teuchos::null;
355  return(temp_plist);
356 }
357 
358 
359 } // namespace Tempus
360 #endif // Tempus_StepperNewmarkExplicitAForm_impl_hpp
Tempus::StepperNewmarkExplicitAForm::predictDisplacement
void predictDisplacement(Thyra::VectorBase< Scalar > &dPred, const Thyra::VectorBase< Scalar > &d, const Thyra::VectorBase< Scalar > &v, const Thyra::VectorBase< Scalar > &a, const Scalar dt) const
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:33
Tempus::StepperState
StepperState is a simple class to hold state information about the stepper.
Definition: Tempus_StepperState.hpp:36
Tempus::StepperNewmarkExplicitAForm::takeStep
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:135
Tempus::StepperNewmarkExplicitAForm::StepperNewmarkExplicitAForm
StepperNewmarkExplicitAForm()
Default Constructor – not allowed.
Tempus::StepperNewmarkExplicitAForm::setSolver
virtual void setSolver(std::string solverName)
Set solver via ParameterList solver name.
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:92
Tempus::StepperNewmarkExplicitAForm::getValidParameters
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:317
Tempus::solutionHistory
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
Definition: Tempus_SolutionHistory_impl.hpp:504
Tempus::StepperNewmarkExplicitAForm::getNonconstParameterList
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:343
Tempus
Definition: Tempus_AdjointAuxSensitivityModelEvaluator_decl.hpp:20
Tempus::StepperNewmarkExplicitAForm::setModel
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:73
Tempus::StepperNewmarkExplicitAForm::predictVelocity
void predictVelocity(Thyra::VectorBase< Scalar > &vPred, const Thyra::VectorBase< Scalar > &v, const Thyra::VectorBase< Scalar > &a, const Scalar dt) const
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:22
Tempus::StepperNewmarkExplicitAForm::description
virtual std::string description() const
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:267
Tempus::StepperNewmarkExplicitAForm::initialize
virtual void initialize()
Initialize during construction and after changing input parameters.
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:124
Tempus::StepperNewmarkExplicitAForm::unsetParameterList
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:351
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::StepperNewmarkExplicitAForm::describe
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:275
Tempus::StepperNewmarkExplicitAForm::getDefaultParameters
Teuchos::RCP< Teuchos::ParameterList > getDefaultParameters() const
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:333
Tempus::StepperNewmarkExplicitAForm::getDefaultStepperState
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:258
Teuchos
Definition: Tempus_Integrator.hpp:19
Tempus::StepperNewmarkExplicitAForm::setNonConstModel
virtual void setNonConstModel(const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &appModel)
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:85
Tempus::StepperNewmarkExplicitAForm::correctVelocity
void correctVelocity(Thyra::VectorBase< Scalar > &v, const Thyra::VectorBase< Scalar > &vPred, const Thyra::VectorBase< Scalar > &a, const Scalar dt) const
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:50
Tempus::StepperNewmarkExplicitAForm::setParameterList
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &pl)
Definition: Tempus_StepperNewmarkExplicitAForm_impl.hpp:285