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 namespace Tempus {
22 
23 template <class Scalar>
25 {
27  using Teuchos::RCP;
28 
29  this->setStepperName("Subcycling");
30  this->setStepperType("Subcycling");
31  this->setUseFSAL(false);
32  this->setICConsistency("None");
33  this->setICConsistencyCheck(false);
34 
35  this->setAppAction(Teuchos::null);
36  scIntegrator_ = Teuchos::rcp(new IntegratorBasic<Scalar>());
37 
38  scIntegrator_->setObserver(
40 
41  RCP<ParameterList> tempusPL = Teuchos::rcp_const_cast<Teuchos::ParameterList>(
42  scIntegrator_->getValidParameters());
43 
44  { // Set default subcycling Stepper to Forward Euler.
45  tempusPL->sublist("Default Integrator")
46  .set("Stepper Name", "Default Subcycling Stepper");
47  RCP<ParameterList> stepperPL = Teuchos::parameterList();
48  stepperPL->set("Stepper Type", "Forward Euler");
49  tempusPL->set("Default Subcycling Stepper", *stepperPL);
50 
51  auto stepperFE = Teuchos::rcp(new StepperForwardEuler<Scalar>());
52  setSubcyclingStepper(stepperFE);
53  }
54 
55  // Keep the default SolutionHistory settings:
56  // * 'Storage Type' = "Undo"
57  // * 'Storage Limit' = 2
58  // Also
59  // * No checkpointing within the subcycling, but can restart from
60  // failed subcycle step.
61 
62  // Keep the default TimeStepControl settings for subcycling:
63  // * Finish exactly on the full timestep.
64  // * No solution output during the subcycling.
65  // * Variable time step size.
66  // Set the default initial time step size.
67  {
68  tempusPL->sublist("Default Integrator")
69  .sublist("Time Step Control")
70  .set("Initial Time Step", std::numeric_limits<Scalar>::max());
71  }
72 
73  scIntegrator_->setTimeStepControl();
74  this->setSubcyclingPrintDtChanges(false);
75 }
76 
77 template <class Scalar>
79  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
80  const Teuchos::RCP<IntegratorBasic<Scalar> >& scIntegrator, bool useFSAL,
81  std::string ICConsistency, bool ICConsistencyCheck,
82  const Teuchos::RCP<StepperSubcyclingAppAction<Scalar> >& stepperSCAppAction)
83 {
84  this->setStepperName("Subcycling");
85  this->setStepperType("Subcycling");
86  this->setUseFSAL(useFSAL);
87  this->setICConsistency(ICConsistency);
88  this->setICConsistencyCheck(ICConsistencyCheck);
89  this->setAppAction(stepperSCAppAction);
90  scIntegrator_ = scIntegrator;
91  this->setSubcyclingPrintDtChanges(false);
92 
93  if (appModel != Teuchos::null) {
94  this->setModel(appModel);
95  this->initialize();
96  }
97 }
98 
99 template <class Scalar>
101  Teuchos::RCP<Stepper<Scalar> > stepper)
102 {
103  scIntegrator_->setStepper(stepper);
104  this->isInitialized_ = false;
105 }
106 
107 template <class Scalar>
109 {
110  scIntegrator_->getNonConstTimeStepControl()->setMinTimeStep(MinTimeStep);
111  this->isInitialized_ = false;
112 }
113 
114 template <class Scalar>
116 {
117  scIntegrator_->getNonConstTimeStepControl()->setInitTimeStep(InitTimeStep);
118  this->isInitialized_ = false;
119 }
120 
121 template <class Scalar>
123 {
124  scIntegrator_->getNonConstTimeStepControl()->setMaxTimeStep(MaxTimeStep);
125  this->isInitialized_ = false;
126 }
127 
128 template <class Scalar>
130 {
131  scIntegrator_->getNonConstTimeStepControl()->setMaxFailures(MaxFailures);
132  this->isInitialized_ = false;
133 }
134 
135 template <class Scalar>
137  int MaxConsecFailures)
138 {
139  scIntegrator_->getNonConstTimeStepControl()->setMaxConsecFailures(
140  MaxConsecFailures);
141  this->isInitialized_ = false;
142 }
143 
144 template <class Scalar>
146 {
147  scIntegrator_->setScreenOutputIndexInterval(i);
148  this->isInitialized_ = false;
149 }
150 
151 template <class Scalar>
153  std::string s)
154 {
155  scIntegrator_->setScreenOutputIndexList(s);
156  this->isInitialized_ = false;
157 }
158 
159 template <class Scalar>
162 {
163  scIntegrator_->getNonConstTimeStepControl()->setTimeStepControlStrategy(tscs);
164  this->isInitialized_ = false;
165 }
166 
167 template <class Scalar>
170 {
171  scIntegrator_->setObserver(obs);
172  this->isInitialized_ = false;
173 }
174 
175 template <class Scalar>
177 {
178  scIntegrator_->getNonConstTimeStepControl()->setPrintDtChanges(
179  printDtChanges);
180  this->isInitialized_ = false;
181 }
182 
183 template <class Scalar>
186 {
187  return scIntegrator_->getStepper();
188 }
189 
190 template <class Scalar>
192 {
193  return scIntegrator_->getTimeStepControl()->getMinTimeStep();
194 }
195 
196 template <class Scalar>
198 {
199  return scIntegrator_->getTimeStepControl()->getInitTimeStep();
200 }
201 
202 template <class Scalar>
204 {
205  return scIntegrator_->getTimeStepControl()->getMaxTimeStep();
206 }
207 
208 template <class Scalar>
210 {
211  return scIntegrator_->getTimeStepControl()->getStepType();
212 }
213 
214 template <class Scalar>
216 {
217  return scIntegrator_->getTimeStepControl()->getMaxFailures();
218 }
219 
220 template <class Scalar>
222 {
223  return scIntegrator_->getTimeStepControl()->getMaxConsecFailures();
224 }
225 
226 template <class Scalar>
228 {
229  return scIntegrator_->getScreenOutputIndexInterval();
230 }
231 
232 template <class Scalar>
234  const
235 {
236  return scIntegrator_->getScreenOutputIndexListString();
237 }
238 
239 template <class Scalar>
242 {
243  return scIntegrator_->getTimeStepControl()->getTimeStepControlStrategy();
244 }
245 
246 template <class Scalar>
249 {
250  return scIntegrator_->getObserver();
251 }
252 
253 template <class Scalar>
255 {
256  return scIntegrator_->getTimeStepControl()->getPrintDtChanges();
257 }
258 
259 template <class Scalar>
261  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel)
262 {
263  scIntegrator_->setModel(appModel);
264  this->isInitialized_ = false;
265 }
266 
267 template <class Scalar>
270 {
271  setModel(appModel);
272  this->isInitialized_ = false;
273 }
274 
275 template <class Scalar>
278 {
279  if (appAction == Teuchos::null) {
280  // Create default appAction
281  stepperSCAppAction_ =
283  }
284  else {
285  stepperSCAppAction_ = appAction;
286  }
287  this->isInitialized_ = false;
288 }
289 
290 template <class Scalar>
292 {
293  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
294  bool isValidSetup = true;
295 
296  if (!(this->getICConsistency() == "None" ||
297  this->getICConsistency() == "Zero" ||
298  this->getICConsistency() == "App" ||
299  this->getICConsistency() == "Consistent")) {
300  isValidSetup = false;
301  *out << "The IC consistency does not have a valid value!\n"
302  << "('None', 'Zero', 'App' or 'Consistent')\n"
303  << " ICConsistency = " << this->getICConsistency() << "\n";
304  }
305  scIntegrator_->initialize();
306 
307  if (stepperSCAppAction_ == Teuchos::null) {
308  isValidSetup = false;
309  *out << "The Subcycling AppAction is not set!\n";
310  }
311 
312  if (isValidSetup)
313  this->isInitialized_ = true; // Only place it is set to true.
314  else
315  this->describe(*out, Teuchos::VERB_MEDIUM);
316 }
317 
318 template <class Scalar>
320 {
321  return scIntegrator_->getStepper()->isExplicit();
322 }
323 
324 template <class Scalar>
326 {
327  return scIntegrator_->getStepper()->isImplicit();
328 }
329 
330 template <class Scalar>
332 {
333  return scIntegrator_->getStepper()->isExplicitImplicit();
334 }
335 
336 template <class Scalar>
338 {
339  return scIntegrator_->getStepper()->isOneStepMethod();
340 }
341 
342 template <class Scalar>
344 {
345  return scIntegrator_->getStepper()->isMultiStepMethod();
346 }
347 
348 template <class Scalar>
350 {
351  return scIntegrator_->getStepper()->getOrder();
352 }
353 
354 template <class Scalar>
356 {
357  return scIntegrator_->getStepper()->getOrderMin();
358 }
359 
360 template <class Scalar>
362 {
363  return scIntegrator_->getStepper()->getOrderMax();
364 }
365 
366 template <class Scalar>
368 {
369  return scIntegrator_->getStepper()->getOrderODE();
370 }
371 
372 template <class Scalar>
374  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory) const
375 {
376  return scIntegrator_->getStepper()->getInitTimeStep(solutionHistory);
377 }
378 
379 template <class Scalar>
381  Teuchos::RCP<const Thyra::VectorBase<Scalar> > initialGuess)
382 {
383  scIntegrator_->getStepper()->setInitialGuess(initialGuess);
384  this->isInitialized_ = false;
385 }
386 
387 template <class Scalar>
389  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
390 {
391  scIntegrator_->getStepper()->setInitialConditions(solutionHistory);
392  scIntegrator_->setSolutionHistory(solutionHistory);
393 }
394 
395 template <class Scalar>
398 {
399  scIntegrator_->getStepper()->setSolver(solver);
400  this->isInitialized_ = false;
401 }
402 
403 template <class Scalar>
406 {
407  return scIntegrator_->getStepper()->getSolver();
408 }
409 
410 template <class Scalar>
412  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
413 {
414  using Teuchos::RCP;
415 
416  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperSubcycling::takeStep()");
417  {
419  solutionHistory->getNumStates() < 2, std::logic_error,
420  "Error - StepperSubcycling<Scalar>::takeStep(...)\n"
421  << "Need at least two SolutionStates for Subcycling.\n"
422  << " Number of States = " << solutionHistory->getNumStates()
423  << "\nTry setting in \"Solution History\" \"Storage Type\" = "
424  << "\"Undo\"\n"
425  << " or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
426  << "\"2\"\n");
427 
428  RCP<StepperSubcycling<Scalar> > thisStepper = Teuchos::rcpFromRef(*this);
429  stepperSCAppAction_->execute(
430  solutionHistory, thisStepper,
432  RCP<SolutionState<Scalar> > currentState =
433  solutionHistory->getCurrentState();
434  RCP<SolutionState<Scalar> > workingState =
435  solutionHistory->getWorkingState();
436 
437  auto scTSC = scIntegrator_->getNonConstTimeStepControl();
438  scTSC->setInitTime(currentState->getTime());
439  scTSC->setInitIndex(0);
440  scTSC->setFinalTime(workingState->getTime());
441 
442  auto subcyclingState = currentState->clone();
443  subcyclingState->setTimeStep(scTSC->getInitTimeStep());
444  subcyclingState->setOrder(scIntegrator_->getStepper()->getOrder());
445  subcyclingState->setIndex(0);
446  subcyclingState->setNFailures(0);
447  subcyclingState->setNRunningFailures(0);
448  subcyclingState->setNConsecutiveFailures(0);
449  subcyclingState->setOutput(false);
450  subcyclingState->setOutputScreen(false);
451 
453  !subcyclingState->getIsSynced(), std::logic_error,
454  "Error - StepperSubcycling<Scalar>::takeStep(...)\n"
455  " Subcycling requires the the solution is synced!\n"
456  " (i.e., x, xDot, and xDotDot at the same time level.\n");
457 
458  auto scSH = rcp(new Tempus::SolutionHistory<Scalar>());
459  scSH->setName("Subcycling States");
460  scSH->setStorageType(Tempus::STORAGE_TYPE_STATIC);
461  scSH->setStorageLimit(3);
462  scSH->addState(subcyclingState);
463 
464  scIntegrator_->setSolutionHistory(scSH);
465 
466  bool pass = scIntegrator_->advanceTime();
467 
468  RCP<SolutionState<Scalar> > scCS = scSH->getCurrentState();
469 
470  RCP<Thyra::VectorBase<Scalar> > x = workingState->getX();
471  RCP<Thyra::VectorBase<Scalar> > scX = scCS->getX();
472  Thyra::V_V(x.ptr(), *(scX));
473 
474  RCP<Thyra::VectorBase<Scalar> > xDot = workingState->getXDot();
475  if (xDot != Teuchos::null) {
476  RCP<Thyra::VectorBase<Scalar> > scXDot = scCS->getXDot();
477  Thyra::V_V(xDot.ptr(), *(scXDot));
478  }
479 
480  RCP<Thyra::VectorBase<Scalar> > xDotDot = workingState->getXDotDot();
481  if (xDotDot != Teuchos::null) {
482  RCP<Thyra::VectorBase<Scalar> > scXDotDot = scCS->getXDotDot();
483  Thyra::V_V(xDotDot.ptr(), *(scXDotDot));
484  }
485 
486  if (pass == true)
487  workingState->setSolutionStatus(Status::PASSED);
488  else
489  workingState->setSolutionStatus(Status::FAILED);
490  workingState->setOrder(scCS->getOrder());
491  workingState->computeNorms(currentState);
492  scSH->clear();
493  stepperSCAppAction_->execute(
494  solutionHistory, thisStepper,
496  }
497  return;
498 }
499 
506 template <class Scalar>
509 {
511  rcp(new StepperState<Scalar>(this->getStepperType()));
512  return stepperState;
513 }
514 
515 template <class Scalar>
517  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
518 {
519  out.setOutputToRootOnly(0);
520  out << std::endl;
521  Stepper<Scalar>::describe(out, verbLevel);
522 
523  out << "--- StepperSubcycling ---\n";
524  out << " stepperSCAppAction = " << stepperSCAppAction_ << std::endl;
525  out << " scIntegrator = " << scIntegrator_ << std::endl;
526  out << "-------------------------" << std::endl;
527  scIntegrator_->getStepper()->describe(out, verbLevel);
528 }
529 
530 template <class Scalar>
533 {
535  true, std::logic_error,
536  "Error - StepperSubcycling<Scalar>::getValidParameters()\n"
537  " is not implemented yet.\n");
538 
539  return this->getValidParametersBasic();
540 }
541 
542 // Nonmember constructor - ModelEvaluator and ParameterList
543 // ------------------------------------------------------------------------
544 template <class Scalar>
546  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
548 {
549  auto stepper = Teuchos::rcp(new StepperSubcycling<Scalar>());
550 
552  pl != Teuchos::null, std::logic_error,
553  "Error - Construction of StepperSubcycling with a ParameterList\n"
554  "is not implemented yet!\n");
555 
556  if (pl != Teuchos::null) {
557  stepper->setStepperValues(pl);
558  }
559 
560  if (model != Teuchos::null) {
561  stepper->setModel(model);
562  stepper->initialize();
563  }
564 
565  return stepper;
566 }
567 
568 } // namespace Tempus
569 #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...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
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)