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