Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_StepperSubcycling_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_StepperSubcycling_impl_hpp
10 #define Tempus_StepperSubcycling_impl_hpp
11 
12 #include "Thyra_VectorStdOps.hpp"
13 
14 #include "Tempus_StepperForwardEuler.hpp"
18 #include "Tempus_IntegratorObserverSubcycling.hpp"
19 #include "Tempus_IntegratorObserverNoOp.hpp"
20 
21 
22 namespace Tempus {
23 
24 
25 template<class Scalar>
27 {
28  using Teuchos::RCP;
30 
31  this->setStepperName( "Subcycling");
32  this->setStepperType( "Subcycling");
33  this->setUseFSAL( false);
34  this->setICConsistency( "None");
35  this->setICConsistencyCheck( false);
36 
37  this->setAppAction(Teuchos::null);
38  scIntegrator_ = Teuchos::rcp(new IntegratorBasic<Scalar>());
39 
40  scIntegrator_->setObserver(
42 
43  RCP<ParameterList> tempusPL =
44  Teuchos::rcp_const_cast<Teuchos::ParameterList> (
45  scIntegrator_->getValidParameters());
46 
47  { // Set default subcycling Stepper to Forward Euler.
48  tempusPL->sublist("Default Integrator")
49  .set("Stepper Name", "Default Subcycling Stepper");
50  RCP<ParameterList> stepperPL = Teuchos::parameterList();
51  stepperPL->set("Stepper Type", "Forward Euler");
52  tempusPL->set("Default Subcycling Stepper", *stepperPL);
53 
54  auto stepperFE = Teuchos::rcp(new StepperForwardEuler<Scalar>());
55  setSubcyclingStepper(stepperFE);
56  }
57 
58  // Keep the default SolutionHistory settings:
59  // * 'Storage Type' = "Undo"
60  // * 'Storage Limit' = 2
61  // Also
62  // * No checkpointing within the subcycling, but can restart from
63  // failed subcycle step.
64 
65  // Keep the default TimeStepControl settings for subcycling:
66  // * Finish exactly on the full timestep.
67  // * No solution output during the subcycling.
68  // * Variable time step size.
69  // Set the default initial time step size.
70  {
71  tempusPL->sublist("Default Integrator")
72  .sublist("Time Step Control")
73  .set("Initial Time Step", std::numeric_limits<Scalar>::max());
74  }
75 
76  scIntegrator_->setTimeStepControl();
77  this->setSubcyclingPrintDtChanges(false);
78 
79 }
80 
81 
82 template<class Scalar>
84  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
85  const Teuchos::RCP<IntegratorBasic<Scalar> >& scIntegrator,
86  bool useFSAL,
87  std::string ICConsistency,
88  bool ICConsistencyCheck,
89  const Teuchos::RCP<StepperSubcyclingAppAction<Scalar> >& stepperSCAppAction)
90 {
91  this->setStepperName( "Subcycling");
92  this->setStepperType( "Subcycling");
93  this->setUseFSAL( useFSAL);
94  this->setICConsistency( ICConsistency);
95  this->setICConsistencyCheck( ICConsistencyCheck);
96  this->setAppAction(stepperSCAppAction);
97  scIntegrator_ = scIntegrator;
98  this->setSubcyclingPrintDtChanges(false);
99 
100  if (appModel != Teuchos::null) {
101  this->setModel(appModel);
102  this->initialize();
103  }
104 }
105 
106 template<class Scalar>
108  Teuchos::RCP<Stepper<Scalar> > stepper)
109 {
110  scIntegrator_->setStepper(stepper);
111  this->isInitialized_ = false;
112 }
113 
114 
115 template<class Scalar>
117 {
118  scIntegrator_->getNonConstTimeStepControl()->setMinTimeStep(MinTimeStep);
119  this->isInitialized_ = false;
120 }
121 
122 
123 template<class Scalar>
125 {
126  scIntegrator_->getNonConstTimeStepControl()->setInitTimeStep(InitTimeStep);
127  this->isInitialized_ = false;
128 }
129 
130 
131 template<class Scalar>
133 {
134  scIntegrator_->getNonConstTimeStepControl()->setMaxTimeStep(MaxTimeStep);
135  this->isInitialized_ = false;
136 }
137 
138 
139 template<class Scalar>
141 {
142  scIntegrator_->getNonConstTimeStepControl()->setMaxFailures(MaxFailures);
143  this->isInitialized_ = false;
144 }
145 
146 
147 template<class Scalar>
149 setSubcyclingMaxConsecFailures(int MaxConsecFailures)
150 {
151  scIntegrator_->getNonConstTimeStepControl()->setMaxConsecFailures(MaxConsecFailures);
152  this->isInitialized_ = false;
153 }
154 
155 
156 template<class Scalar>
159 {
160  scIntegrator_->setScreenOutputIndexInterval(i);
161  this->isInitialized_ = false;
162 }
163 
164 
165 template<class Scalar>
168 {
169  scIntegrator_->setScreenOutputIndexList(s);
170  this->isInitialized_ = false;
171 }
172 
173 
174 template<class Scalar>
178 {
179  scIntegrator_->getNonConstTimeStepControl()->setTimeStepControlStrategy(tscs);
180  this->isInitialized_ = false;
181 }
182 
183 
184 template<class Scalar>
187 {
188  scIntegrator_->setObserver(obs);
189  this->isInitialized_ = false;
190 }
191 
192 
193 template<class Scalar>
195  bool printDtChanges)
196 {
197  scIntegrator_->getNonConstTimeStepControl()->setPrintDtChanges(printDtChanges);
198  this->isInitialized_ = false;
199 }
200 
201 
202 template<class Scalar>
205 { return scIntegrator_->getStepper(); }
206 
207 
208 template<class Scalar>
210 { return scIntegrator_->getTimeStepControl()->getMinTimeStep(); }
211 
212 
213 template<class Scalar>
215 { return scIntegrator_->getTimeStepControl()->getInitTimeStep(); }
216 
217 
218 template<class Scalar>
220 { return scIntegrator_->getTimeStepControl()->getMaxTimeStep(); }
221 
222 
223 template<class Scalar>
225 { return scIntegrator_->getTimeStepControl()->getStepType(); }
226 
227 
228 template<class Scalar>
230 { return scIntegrator_->getTimeStepControl()->getMaxFailures(); }
231 
232 
233 template<class Scalar>
235 { return scIntegrator_->getTimeStepControl()->getMaxConsecFailures(); }
236 
237 
238 template<class Scalar>
240 { return scIntegrator_->getScreenOutputIndexInterval(); }
241 
242 
243 template<class Scalar>
245 { return scIntegrator_->getScreenOutputIndexListString(); }
246 
247 
248 template<class Scalar>
251 { return scIntegrator_->getTimeStepControl()->getTimeStepControlStrategy(); }
252 
253 template<class Scalar>
256 { return scIntegrator_->getObserver(); }
257 
258 template<class Scalar>
260 { return scIntegrator_->getTimeStepControl()->getPrintDtChanges(); }
261 
262 
263 template<class Scalar>
265  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel)
266 {
267  scIntegrator_->setModel(appModel);
268  this->isInitialized_ = false;
269 }
270 
271 
272 template<class Scalar>
275 {
276  setModel(appModel);
277  this->isInitialized_ = false;
278 }
279 
280 
281 template<class Scalar>
284  {
285  if (appAction == Teuchos::null) {
286  // Create default appAction
287  stepperSCAppAction_ =
289  } else {
290  stepperSCAppAction_ = appAction;
291  }
292  this->isInitialized_ = false;
293 }
294 
295 template<class Scalar>
297 {
298  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
299  bool isValidSetup = true;
300 
301  if ( !(this->getICConsistency() == "None" ||
302  this->getICConsistency() == "Zero" ||
303  this->getICConsistency() == "App" ||
304  this->getICConsistency() == "Consistent") ) {
305  isValidSetup = false;
306  *out << "The IC consistency does not have a valid value!\n"
307  << "('None', 'Zero', 'App' or 'Consistent')\n"
308  << " ICConsistency = " << this->getICConsistency() << "\n";
309  }
310  scIntegrator_->initialize();
311 
312  if (stepperSCAppAction_ == Teuchos::null) {
313  isValidSetup = false;
314  *out << "The Subcycling AppAction is not set!\n";
315  }
316 
317  if (isValidSetup)
318  this->isInitialized_ = true; // Only place it is set to true.
319  else
320  this->describe(*out, Teuchos::VERB_MEDIUM);
321 }
322 
323 
324 template<class Scalar>
326 { return scIntegrator_->getStepper()->isExplicit(); }
327 
328 
329 template<class Scalar>
331 { return scIntegrator_->getStepper()->isImplicit(); }
332 
333 
334 template<class Scalar>
336 { return scIntegrator_->getStepper()->isExplicitImplicit(); }
337 
338 
339 template<class Scalar>
341 { return scIntegrator_->getStepper()->isOneStepMethod(); }
342 
343 
344 template<class Scalar>
346 { return scIntegrator_->getStepper()->isMultiStepMethod(); }
347 
348 
349 template<class Scalar>
351 { return scIntegrator_->getStepper()->getOrder(); }
352 
353 
354 template<class Scalar>
356 { return scIntegrator_->getStepper()->getOrderMin(); }
357 
358 
359 template<class Scalar>
361 { return scIntegrator_->getStepper()->getOrderMax(); }
362 
363 
364 template<class Scalar>
366 { return scIntegrator_->getStepper()->getOrderODE(); }
367 
368 
369 template<class Scalar>
371  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory) const
372 { return scIntegrator_->getStepper()->getInitTimeStep(solutionHistory); }
373 
374 
375 template<class Scalar>
377  Teuchos::RCP<const Thyra::VectorBase<Scalar> > initialGuess)
378 {
379  scIntegrator_->getStepper()->setInitialGuess(initialGuess);
380  this->isInitialized_ = false;
381 }
382 
383 
384 template<class Scalar>
386  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
387 {
388  scIntegrator_->getStepper()->setInitialConditions(solutionHistory);
389  scIntegrator_->setSolutionHistory(solutionHistory);
390 }
391 
392 
393 template<class Scalar>
396 {
397  scIntegrator_->getStepper()->setSolver(solver);
398  this->isInitialized_ = false;
399 }
400 
401 
402 template<class Scalar>
405 { return scIntegrator_->getStepper()->getSolver(); }
406 
407 
408 template<class Scalar>
410  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
411 {
412  using Teuchos::RCP;
413 
414  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperSubcycling::takeStep()");
415  {
416  TEUCHOS_TEST_FOR_EXCEPTION(solutionHistory->getNumStates() < 2,
417  std::logic_error,
418  "Error - StepperSubcycling<Scalar>::takeStep(...)\n"
419  "Need at least two SolutionStates for Subcycling.\n"
420  " Number of States = " << solutionHistory->getNumStates() << "\n"
421  "Try setting in \"Solution History\" \"Storage Type\" = \"Undo\"\n"
422  " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = \"2\"\n");
423 
424  RCP<StepperSubcycling<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
425  stepperSCAppAction_->execute(solutionHistory, thisStepper,
427  RCP<SolutionState<Scalar> > currentState=solutionHistory->getCurrentState();
428  RCP<SolutionState<Scalar> > workingState=solutionHistory->getWorkingState();
429 
430  auto scTSC = scIntegrator_->getNonConstTimeStepControl();
431  scTSC->setInitTime (currentState->getTime());
432  scTSC->setInitIndex (0);
433  scTSC->setFinalTime (workingState->getTime());
434 
435  auto subcyclingState = currentState->clone();
436  subcyclingState->setTimeStep(scTSC->getInitTimeStep());
437  subcyclingState->setOrder(scIntegrator_->getStepper()->getOrder());
438  subcyclingState->setIndex(0);
439  subcyclingState->setNFailures(0);
440  subcyclingState->setNRunningFailures(0);
441  subcyclingState->setNConsecutiveFailures(0);
442  subcyclingState->setOutput(false);
443  subcyclingState->setOutputScreen(false);
444 
445  TEUCHOS_TEST_FOR_EXCEPTION(!subcyclingState->getIsSynced(),std::logic_error,
446  "Error - StepperSubcycling<Scalar>::takeStep(...)\n"
447  " Subcycling requires the the solution is synced!\n"
448  " (i.e., x, xDot, and xDotDot at the same time level.\n");
449 
450  auto scSH = rcp(new Tempus::SolutionHistory<Scalar>());
451  scSH->setName("Subcycling States");
452  scSH->setStorageType(Tempus::STORAGE_TYPE_STATIC);
453  scSH->setStorageLimit(3);
454  scSH->addState(subcyclingState);
455 
456  scIntegrator_->setSolutionHistory(scSH);
457 
458  bool pass = scIntegrator_->advanceTime();
459 
460  RCP<SolutionState<Scalar> > scCS = scSH->getCurrentState();
461 
462  RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
463  RCP<Thyra::VectorBase<Scalar> > scX = scCS->getX();
464  Thyra::V_V(x.ptr(), *(scX));
465 
466  RCP<Thyra::VectorBase<Scalar> > xDot = workingState->getXDot();
467  if (xDot != Teuchos::null) {
468  RCP<Thyra::VectorBase<Scalar> > scXDot = scCS->getXDot();
469  Thyra::V_V(xDot.ptr(), *(scXDot));
470  }
471 
472  RCP<Thyra::VectorBase<Scalar> > xDotDot = workingState->getXDotDot();
473  if (xDotDot != Teuchos::null) {
474  RCP<Thyra::VectorBase<Scalar> > scXDotDot = scCS->getXDotDot();
475  Thyra::V_V(xDotDot.ptr(), *(scXDotDot));
476  }
477 
478  if (pass == true) workingState->setSolutionStatus(Status::PASSED);
479  else workingState->setSolutionStatus(Status::FAILED);
480  workingState->setOrder(scCS->getOrder());
481  workingState->computeNorms(currentState);
482  scSH->clear();
483  stepperSCAppAction_->execute(solutionHistory, thisStepper,
485  }
486  return;
487 }
488 
489 
496 template<class Scalar>
499 {
501  rcp(new StepperState<Scalar>(this->getStepperType()));
502  return stepperState;
503 }
504 
505 
506 template<class Scalar>
509  const Teuchos::EVerbosityLevel verbLevel) const
510 {
511  out << std::endl;
512  Stepper<Scalar>::describe(out, verbLevel);
513 
514  out << "--- StepperSubcycling ---\n";
515  out << " stepperSCAppAction = " << stepperSCAppAction_ << std::endl;
516  out << " scIntegrator = " << scIntegrator_ << std::endl;
517  out << "-------------------------" << std::endl;
518  scIntegrator_->getStepper()->describe(out, verbLevel);
519 }
520 
521 
522 template<class Scalar>
525 {
526  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
527  "Error - StepperSubcycling<Scalar>::getValidParameters()\n"
528  " is not implemented yet.\n");
529 
530  return this->getValidParametersBasic();
531 }
532 
533 
534 // Nonmember constructor - ModelEvaluator and ParameterList
535 // ------------------------------------------------------------------------
536 template<class Scalar>
539  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
541 {
542  auto stepper = Teuchos::rcp(new StepperSubcycling<Scalar>());
543 
544  TEUCHOS_TEST_FOR_EXCEPTION(pl != Teuchos::null, std::logic_error,
545  "Error - Construction of StepperSubcycling with a ParameterList\n"
546  "is not implemented yet!\n");
547 
548  if (pl != Teuchos::null) {
549  stepper->setStepperValues(pl);
550  }
551 
552  if (model != Teuchos::null) {
553  stepper->setModel(model);
554  stepper->initialize();
555  }
556 
557  return stepper;
558 }
559 
560 
561 } // namespace Tempus
562 #endif // Tempus_StepperSubcycling_impl_hpp
virtual int getSubcyclingMaxConsecFailures() const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
virtual void setSubcyclingStepper(Teuchos::RCP< Stepper< Scalar > > stepper)
virtual void initialize()
Initialize during construction and after changing input parameters.
virtual void setSubcyclingMinTimeStep(Scalar MinTimeStep)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
virtual void setSubcyclingScreenOutputIndexInterval(int i)
virtual void setSubcyclingMaxTimeStep(Scalar MaxTimeStep)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual Scalar getSubcyclingMaxTimeStep() const
Teuchos::RCP< StepperSubcycling< Scalar > > createStepperSubcycling(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Get a default (initial) StepperState.
void initialize(const RCP< std::basic_ostream< char_type, traits_type > > &oStream, const std::basic_string< char_type, traits_type > &tabIndentStr=" ", const int startingTab=0, const bool showLinePrefix=false, const int maxLenLinePrefix=10, const bool showTabCount=false, const bool showProcRank=false)
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
virtual void setSolver(Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > solver=Teuchos::null)
Set solver.
virtual void setSubcyclingMaxConsecFailures(int MaxConsecFailures)
virtual void setSubcyclingTimeStepControlStrategy(Teuchos::RCP< TimeStepControlStrategy< Scalar > > tscs)
Application Action for StepperSubcycling.
Thyra Base interface for time steppers.
virtual Teuchos::RCP< IntegratorObserver< Scalar > > getSubcyclingIntegratorObserver() const
virtual int getSubcyclingScreenOutputIndexInterval() const
virtual void setSubcyclingIntegratorObserver(Teuchos::RCP< IntegratorObserver< Scalar > > obs)
StepperState is a simple class to hold state information about the stepper.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
IntegratorObserverNoOp class for time integrators. This basic class has simple no-op functions...
IntegratorObserver class for time integrators.
virtual Teuchos::RCP< Thyra::NonlinearSolverBase< Scalar > > getSolver() const
Get solver.
virtual void setSubcyclingScreenOutputIndexList(std::string s)
virtual std::string getSubcyclingStepType() const
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual Scalar getInitTimeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory) const
Keep a fix number of states.
virtual void setSubcyclingInitTimeStep(Scalar InitTimeStep)
virtual void setInitialGuess(Teuchos::RCP< const Thyra::VectorBase< Scalar > > initial_guess)
Pass initial guess to Newton solver (only relevant for implicit solvers)
virtual Teuchos::RCP< const Stepper< Scalar > > getSubcyclingStepper() const
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
virtual void setSubcyclingPrintDtChanges(bool printDtChanges)
virtual std::string getSubcyclingScreenOutputIndexList() const
virtual Scalar getSubcyclingMinTimeStep() const
virtual bool getSubcyclingPrintDtChanges() const
TimeStepControlStrategy class for TimeStepControl.
virtual Teuchos::RCP< TimeStepControlStrategy< Scalar > > getSubcyclingTimeStepControlStrategy() const
virtual Scalar getSubcyclingInitTimeStep() const
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions, make them consistent, and set needed memory.
virtual void setNonConstModel(const Teuchos::RCP< Thyra::ModelEvaluator< Scalar > > &appModel)
virtual void setAppAction(Teuchos::RCP< StepperSubcyclingAppAction< Scalar > > appAction=Teuchos::null)
virtual void setSubcyclingMaxFailures(int MaxFailures)