Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_StepperNewmarkImplicitDForm_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_StepperNewmarkImplicitDForm_impl_hpp
10 #define Tempus_StepperNewmarkImplicitDForm_impl_hpp
11 
12 #include "NOX_Thyra.H"
14 #include "Tempus_config.hpp"
15 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
16 
17 //#define VERBOSE_DEBUG_OUTPUT
18 //#define DEBUG_OUTPUT
19 
20 namespace Tempus {
21 
22 // Forward Declaration for recursive includes (this Stepper <--> StepperFactory)
23 template <class Scalar>
24 class StepperFactory;
25 
26 template <class Scalar>
27 void
29  Thyra::VectorBase<Scalar>& vPred, const Thyra::VectorBase<Scalar>& v,
30  const Thyra::VectorBase<Scalar>& a, const Scalar dt) const {
31 #ifdef VERBOSE_DEBUG_OUTPUT
32  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
33 #endif
34  // vPred = v + dt*(1.0-gamma_)*a
35  Thyra::V_StVpStV(Teuchos::ptrFromRef(vPred), 1.0, v, dt * (1.0 - gamma_), a);
36 }
37 
38 template <class Scalar>
39 void
41  Thyra::VectorBase<Scalar>& dPred, const Thyra::VectorBase<Scalar>& d,
42  const Thyra::VectorBase<Scalar>& v, const Thyra::VectorBase<Scalar>& a,
43  const Scalar dt) const {
44 #ifdef VERBOSE_DEBUG_OUTPUT
45  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
46 #endif
47  Teuchos::RCP<const Thyra::VectorBase<Scalar>> tmp =
48  Thyra::createMember<Scalar>(dPred.space());
49  // dPred = dt*v + dt*dt/2.0*(1.0-2.0*beta_)*a
50  Scalar aConst = dt * dt / 2.0 * (1.0 - 2.0 * beta_);
51  Thyra::V_StVpStV(Teuchos::ptrFromRef(dPred), dt, v, aConst, a);
52  // dPred += d;
53  Thyra::Vp_V(Teuchos::ptrFromRef(dPred), d, 1.0);
54 }
55 
56 template <class Scalar>
57 void
59  Thyra::VectorBase<Scalar>& v, const Thyra::VectorBase<Scalar>& vPred,
60  const Thyra::VectorBase<Scalar>& a, const Scalar dt) const {
61 #ifdef VERBOSE_DEBUG_OUTPUT
62  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
63 #endif
64  // v = vPred + dt*gamma_*a
65  Thyra::V_StVpStV(Teuchos::ptrFromRef(v), 1.0, vPred, dt * gamma_, a);
66 }
67 
68 template <class Scalar>
69 void
71  Thyra::VectorBase<Scalar>& d, const Thyra::VectorBase<Scalar>& dPred,
72  const Thyra::VectorBase<Scalar>& a, const Scalar dt) const {
73 #ifdef VERBOSE_DEBUG_OUTPUT
74  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
75 #endif
76  // d = dPred + beta_*dt*dt*a
77  Thyra::V_StVpStV(Teuchos::ptrFromRef(d), 1.0, dPred, beta_ * dt * dt, a);
78 }
79 
80 template <class Scalar>
81 void
83  Thyra::VectorBase<Scalar>& a, const Thyra::VectorBase<Scalar>& dPred,
84  const Thyra::VectorBase<Scalar>& d, const Scalar dt) const {
85 #ifdef VERBOSE_DEBUG_OUTPUT
86  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
87 #endif
88  // a = (d - dPred) / (beta_*dt*dt)
89  Scalar const c = 1.0 / beta_ / dt / dt;
90  Thyra::V_StVpStV(Teuchos::ptrFromRef(a), c, d, -c, dPred);
91 }
92 
93 template<class Scalar>
95 {
96  if (schemeName_ != "User Defined") {
97  *out_ << "\nWARNING: schemeName != 'User Defined' (=" <<schemeName_<< ").\n"
98  << " Not setting beta, and leaving as beta = " << beta_ << "!\n";
99  return;
100  }
101 
102  beta_ = beta;
103 
104  if (beta_ == 0.0) {
105  *out_ << "\nWARNING: Running (implicit implementation of) Newmark "
106  << "Implicit a-Form Stepper with Beta = 0.0, which \n"
107  << "specifies an explicit scheme. Mass lumping is not possible, "
108  << "so this will be slow! To run explicit \n"
109  << "implementation of Newmark Implicit a-Form Stepper, please "
110  << "re-run with 'Stepper Type' = 'Newmark Explicit a-Form'.\n"
111  << "This stepper allows for mass lumping when called through "
112  << "Piro::TempusSolver.\n";
113  }
114 
115  TEUCHOS_TEST_FOR_EXCEPTION( (beta_ > 1.0) || (beta_ < 0.0),
116  std::logic_error,
117  "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Beta = "
118  << beta_ << ". Please select Beta >= 0 and <= 1. \n");
119 }
120 
121 
122 template<class Scalar>
124 {
125  if (schemeName_ != "User Defined") {
126  *out_ << "\nWARNING: schemeName != 'User Defined' (=" <<schemeName_<< ").\n"
127  << " Not setting gamma, and leaving as gamma = " << gamma_ << "!\n";
128  return;
129  }
130 
131  gamma_ = gamma;
132 
133  TEUCHOS_TEST_FOR_EXCEPTION( (gamma_ > 1.0) || (gamma_ < 0.0),
134  std::logic_error,
135  "\nError in 'Newmark Implicit a-Form' stepper: invalid value of Gamma ="
136  <<gamma_ << ". Please select Gamma >= 0 and <= 1. \n");
137 }
138 
139 
140 template<class Scalar>
142  std::string schemeName)
143 {
144  schemeName_ = schemeName;
145 
146  if (schemeName_ == "Average Acceleration") {
147  beta_= 0.25; gamma_ = 0.5;
148  }
149  else if (schemeName_ == "Linear Acceleration") {
150  beta_= 0.25; gamma_ = 1.0/6.0;
151  }
152  else if (schemeName_ == "Central Difference") {
153  beta_= 0.0; gamma_ = 0.5;
154  }
155  else if (schemeName_ == "User Defined") {
156  beta_= 0.25; gamma_ = 0.5; // Use defaults until setBeta and setGamma calls.
157  }
158  else {
159  TEUCHOS_TEST_FOR_EXCEPTION(true,
160  std::logic_error,
161  "\nError in Tempus::StepperNewmarkImplicitDForm! "
162  <<"Invalid Scheme Name = " << schemeName_ <<". \n"
163  <<"Valid Scheme Names are: 'Average Acceleration', "
164  <<"'Linear Acceleration', \n"
165  <<"'Central Difference' and 'User Defined'.\n");
166  }
167 }
168 
169 
170 template <class Scalar>
172  : out_(Teuchos::VerboseObjectBase::getDefaultOStream()) {
173 #ifdef VERBOSE_DEBUG_OUTPUT
174  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
175 #endif
176 
177  this->setStepperType( "Newmark Implicit d-Form");
178  this->setUseFSAL( this->getUseFSALDefault());
181  this->setZeroInitialGuess( false);
182  this->setSchemeName( "Average Acceleration");
183 
184  this->setObserver();
185 }
186 
187 template <class Scalar>
189  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar>>& appModel,
190  const Teuchos::RCP<StepperObserver<Scalar> >& obs,
191  const Teuchos::RCP<Thyra::NonlinearSolverBase<Scalar> >& solver,
192  bool useFSAL,
193  std::string ICConsistency,
194  bool ICConsistencyCheck,
195  bool zeroInitialGuess,
196  std::string schemeName,
197  Scalar beta,
198  Scalar gamma)
199  : out_(Teuchos::VerboseObjectBase::getDefaultOStream())
200 {
201  this->setStepperType( "Newmark Implicit d-Form");
202  this->setUseFSAL( useFSAL);
203  this->setICConsistency( ICConsistency);
204  this->setICConsistencyCheck( ICConsistencyCheck);
205  this->setZeroInitialGuess( zeroInitialGuess);
206  this->setSchemeName( schemeName);
207  this->setBeta( beta);
208  this->setGamma( gamma);
209 
210  this->setObserver(obs);
211 
212  if (appModel != Teuchos::null) {
213 
214  this->setModel(appModel);
215  this->setSolver(solver);
216  this->initialize();
217  }
218 }
219 
220 
221 template <class Scalar>
222 void
224  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar>>& appModel) {
225 #ifdef VERBOSE_DEBUG_OUTPUT
226  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
227 #endif
228  validSecondOrderODE_DAE(appModel);
229  Teuchos::RCP<WrapperModelEvaluatorSecondOrder<Scalar> > wrapperModel =
231  appModel, "Newmark Implicit d-Form"));
232  this->wrapperModel_ = wrapperModel;
233  inArgs_ = this->wrapperModel_->getNominalValues();
234  outArgs_ = this->wrapperModel_->createOutArgs();
235 }
236 
237 template <class Scalar>
238 void
240 {
241  TEUCHOS_TEST_FOR_EXCEPTION( this->wrapperModel_ == Teuchos::null,
242  std::logic_error,
243  "Error - Need to set the model, setModel(), before calling "
244  "StepperNewmarkImplicitDForm::initialize()\n");
245 
246 #ifdef VERBOSE_DEBUG_OUTPUT
247  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
248 #endif
249 }
250 
251 template <class Scalar>
252 void
254  const Teuchos::RCP<SolutionHistory<Scalar>>& solutionHistory) {
255 #ifdef VERBOSE_DEBUG_OUTPUT
256  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
257 #endif
258  using Teuchos::RCP;
259 
260  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperNewmarkImplicitDForm::takeStep()");
261  {
262  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
263  std::logic_error,
264  "Error - StepperNewmarkImplicitDForm<Scalar>::takeStep(...)\n"
265  "Need at least two SolutionStates for NewmarkImplicitDForm.\n"
266  " Number of States = " << solutionHistory->getNumStates() << "\n"
267  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
268  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
269 
270  RCP<SolutionState<Scalar>> workingState =solutionHistory->getWorkingState();
271  RCP<SolutionState<Scalar>> currentState =solutionHistory->getCurrentState();
272 
273  Teuchos::RCP<WrapperModelEvaluatorSecondOrder<Scalar> > wrapperModel =
274  Teuchos::rcp_dynamic_cast<WrapperModelEvaluatorSecondOrder<Scalar> >(
275  this->wrapperModel_);
276 
277  // Get values of d, v and a from previous step
278  RCP<const Thyra::VectorBase<Scalar>> d_old = currentState->getX();
279  RCP<Thyra::VectorBase<Scalar>> v_old = currentState->getXDot();
280  RCP<Thyra::VectorBase<Scalar>> a_old = currentState->getXDotDot();
281 
282  // Get new values of d, v and a from current workingState
283  //(to be updated here)
284  RCP<Thyra::VectorBase<Scalar>> d_new = workingState->getX();
285  RCP<Thyra::VectorBase<Scalar>> v_new = workingState->getXDot();
286  RCP<Thyra::VectorBase<Scalar>> a_new = workingState->getXDotDot();
287 
288  // Get time and dt
289  const Scalar time = currentState->getTime();
290  const Scalar dt = workingState->getTimeStep();
291  // Update time
292  Scalar t = time + dt;
293 
294 
295 #ifdef DEBUG_OUTPUT
296  Teuchos::Range1D range;
297 
298  *out_ << "\n*** d_old ***\n";
299  RTOpPack::ConstSubVectorView<Scalar> dov;
300  d_old->acquireDetachedView(range, &dov);
301  auto doa = dov.values();
302  for (auto i = 0; i < doa.size(); ++i) *out_ << doa[i] << " ";
303  *out_ << "\n*** d_old ***\n";
304 
305  *out_ << "\n*** v_old ***\n";
306  RTOpPack::ConstSubVectorView<Scalar> vov;
307  v_old->acquireDetachedView(range, &vov);
308  auto voa = vov.values();
309  for (auto i = 0; i < voa.size(); ++i) *out_ << voa[i] << " ";
310  *out_ << "\n*** v_old ***\n";
311 
312  *out_ << "\n*** a_old ***\n";
313  RTOpPack::ConstSubVectorView<Scalar> aov;
314  a_old->acquireDetachedView(range, &aov);
315  auto aoa = aov.values();
316  for (auto i = 0; i < aoa.size(); ++i) *out_ << aoa[i] << " ";
317  *out_ << "\n*** a_old ***\n";
318 #endif
319 
320  // allocate d and v predictors
321  RCP<Thyra::VectorBase<Scalar>> d_pred = Thyra::createMember(d_old->space());
322  RCP<Thyra::VectorBase<Scalar>> v_pred = Thyra::createMember(v_old->space());
323 
324  // compute displacement and velocity predictors
325  predictDisplacement(*d_pred, *d_old, *v_old, *a_old, dt);
326  predictVelocity(*v_pred, *v_old, *a_old, dt);
327 
328 #ifdef DEBUG_OUTPUT
329  *out_ << "\n*** d_pred ***\n";
330  RTOpPack::ConstSubVectorView<Scalar> dpv;
331  d_pred->acquireDetachedView(range, &dpv);
332  auto dpa = dpv.values();
333  for (auto i = 0; i < dpa.size(); ++i) *out_ << dpa[i] << " ";
334  *out_ << "\n*** d_pred ***\n";
335 
336  *out_ << "\n*** v_pred ***\n";
337  RTOpPack::ConstSubVectorView<Scalar> vpv;
338  v_pred->acquireDetachedView(range, &vpv);
339  auto vpa = vpv.values();
340  for (auto i = 0; i < vpa.size(); ++i) *out_ << vpa[i] << " ";
341  *out_ << "\n*** v_pred ***\n";
342 
343 #endif
344  // inject d_pred, v_pred, a and other relevant data into wrapperModel
345  wrapperModel->initializeNewmark(v_pred, d_pred, dt, t, beta_, gamma_);
346 
347  // create initial guess in NOX solver
348  RCP<Thyra::VectorBase<Scalar>> initial_guess = Thyra::createMember(d_pred->space());
349  if ((time == solutionHistory->minTime()) && (this->initial_guess_ != Teuchos::null)) {
350  //if first time step and initial_guess_ is provided, set initial_guess = initial_guess_
351  //Throw an exception if initial_guess is not compatible with solution
352  bool is_compatible = (initial_guess->space())->isCompatible(*this->initial_guess_->space());
353  TEUCHOS_TEST_FOR_EXCEPTION(
354  is_compatible != true, std::logic_error,
355  "Error in Tempus::NemwarkImplicitDForm takeStep(): user-provided initial guess'!\n"
356  << "for Newton is not compatible with solution vector!\n");
357  Thyra::copy(*this->initial_guess_, initial_guess.ptr());
358  }
359  else {
360  //Otherwise, set initial guess = diplacement predictor
361  Thyra::copy(*d_pred, initial_guess.ptr());
362  }
363 
364  //Set d_pred as initial guess for NOX solver, and solve nonlinear system.
365  const Thyra::SolveStatus<Scalar> sStatus =
366  this->solveImplicitODE(initial_guess);
367 
368  workingState->setSolutionStatus(sStatus); // Converged --> pass.
369 
370  //solveImplicitODE will return converged solution in initial_guess
371  //vector. Copy it here to d_new, to define the new displacement.
372  Thyra::copy(*initial_guess, d_new.ptr());
373 
374  //correct acceleration, velocity
375  correctAcceleration(*a_new, *d_pred, *d_new, dt);
376  correctVelocity(*v_new, *v_pred, *a_new, dt);
377 
378 #ifdef DEBUG_OUTPUT
379  *out_ << "\n*** d_new ***\n";
380  RTOpPack::ConstSubVectorView<Scalar> dnv;
381  d_new->acquireDetachedView(range, &dnv);
382  auto dna = dnv.values();
383  for (auto i = 0; i < dna.size(); ++i) *out_ << dna[i] << " ";
384  *out_ << "\n*** d_new ***\n";
385 
386  *out_ << "\n*** v_new ***\n";
387  RTOpPack::ConstSubVectorView<Scalar> vnv;
388  v_new->acquireDetachedView(range, &vnv);
389  auto vna = vnv.values();
390  for (auto i = 0; i < vna.size(); ++i) *out_ << vna[i] << " ";
391  *out_ << "\n*** v_new ***\n";
392 
393  *out_ << "\n*** a_new ***\n";
394  RTOpPack::ConstSubVectorView<Scalar> anv;
395  a_new->acquireDetachedView(range, &anv);
396  auto ana = anv.values();
397  for (auto i = 0; i < ana.size(); ++i) *out_ << ana[i] << " ";
398  *out_ << "\n*** a_new ***\n";
399 #endif
400 
401  workingState->setOrder(this->getOrder());
402  }
403  return;
404 }
405 
406 /** \brief Provide a StepperState to the SolutionState.
407  * This Stepper does not have any special state data,
408  * so just provide the base class StepperState with the
409  * Stepper description. This can be checked to ensure
410  * that the input StepperState can be used by this Stepper.
411  */
412 template <class Scalar>
413 Teuchos::RCP<Tempus::StepperState<Scalar>>
415 #ifdef VERBOSE_DEBUG_OUTPUT
416  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
417 #endif
418  Teuchos::RCP<Tempus::StepperState<Scalar>> stepperState =
419  rcp(new StepperState<Scalar>(this->getStepperType()));
420  return stepperState;
421 }
422 
423 template <class Scalar>
424 void
426  Teuchos::FancyOStream& out,
427  const Teuchos::EVerbosityLevel /* verbLevel */) const {
428 #ifdef VERBOSE_DEBUG_OUTPUT
429  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
430 #endif
431  out << this->getStepperType() << "::describe:" << std::endl
432  << "wrapperModel_ = " << this->wrapperModel_->description() << std::endl;
433 }
434 
435 template <class Scalar>
436 Teuchos::RCP<const Teuchos::ParameterList>
438 #ifdef VERBOSE_DEBUG_OUTPUT
439  *out_ << "DEBUG: " << __PRETTY_FUNCTION__ << "\n";
440 #endif
441  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
442  getValidParametersBasic(pl, this->getStepperType());
443  pl->set<std::string>("Scheme Name", "Average Acceleration");
444  pl->set<double> ("Beta" , 0.25);
445  pl->set<double> ("Gamma", 0.5 );
446  pl->set<std::string>("Solver Name", "Default Solver");
447  pl->set<bool> ("Zero Initial Guess", false);
448  Teuchos::RCP<Teuchos::ParameterList> solverPL = defaultSolverParameters();
449  pl->set("Default Solver", *solverPL);
450 
451  return pl;
452 }
453 
454 } // namespace Tempus
455 #endif // Tempus_StepperNewmarkImplicitDForm_impl_hpp
void correctVelocity(Thyra::VectorBase< Scalar > &v, const Thyra::VectorBase< Scalar > &vPred, const Thyra::VectorBase< Scalar > &a, const Scalar dt) const
void correctDisplacement(Thyra::VectorBase< Scalar > &d, const Thyra::VectorBase< Scalar > &dPred, const Thyra::VectorBase< Scalar > &a, const Scalar dt) const
virtual void initialize()
Initialize during construction and after changing input parameters.
virtual void setSolver(Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > solver=Teuchos::null)
Set solver.
virtual bool getICConsistencyCheckDefault() const
Teuchos::RCP< Teuchos::ParameterList > defaultSolverParameters()
Returns the default solver ParameterList for implicit Steppers.
virtual std::string getICConsistencyDefault() const
A ModelEvaluator for residual evaluations given a state. This ModelEvaluator takes a state...
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
StepperState is a simple class to hold state information about the stepper.
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar >> &solutionHistory)
Take the specified timestep, dt, and return true if successful.
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
void predictVelocity(Thyra::VectorBase< Scalar > &vPred, const Thyra::VectorBase< Scalar > &v, const Thyra::VectorBase< Scalar > &a, const Scalar dt) const
virtual void setObserver(Teuchos::RCP< StepperObserver< Scalar > >=Teuchos::null)
Set Observer.
void setICConsistencyCheck(bool c)
StepperObserver class for Stepper class.
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
virtual void setZeroInitialGuess(bool zIG)
Set parameter so that the initial guess is set to zero (=True) or use last timestep (=False)...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
void validSecondOrderODE_DAE(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Validate ME supports 2nd order implicit ODE/DAE evaluation, f(xdotdot,xdot,x,t) [= 0]...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual bool getUseFSALDefault() const
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar >> &appModel)
void getValidParametersBasic(Teuchos::RCP< Teuchos::ParameterList > pl, std::string stepperType)
Provide basic parameters to Steppers.
void correctAcceleration(Thyra::VectorBase< Scalar > &a, const Thyra::VectorBase< Scalar > &dPred, const Thyra::VectorBase< Scalar > &d, const Scalar dt) const
void setStepperType(std::string s)
void setICConsistency(std::string s)