Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tempus_TimeStepControlStrategyConstant.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_TimeStepControlStrategy_Constant_hpp
10 #define Tempus_TimeStepControlStrategy_Constant_hpp
11 
12 #include "Tempus_TimeStepControl.hpp"
14 #include "Tempus_SolutionState.hpp"
15 #include "Tempus_SolutionHistory.hpp"
16 #include "Tempus_StepperState.hpp"
17 
18 
19 namespace Tempus {
20 
21 /** \brief StepControlStrategy class for TimeStepControl
22  *
23  */
24 template<class Scalar>
26  : virtual public TimeStepControlStrategy<Scalar>
27 {
28 public:
29 
30  /// Constructor
32 
33  /// Destructor
35 
36  /** \brief Determine the time step size.*/
37  virtual void getNextTimeStep(const TimeStepControl<Scalar> tsc,
38  Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
39  Status & integratorStatus) override
40  {
41  using Teuchos::RCP;
42  RCP<SolutionState<Scalar> >workingState=solutionHistory->getWorkingState();
43  const Scalar errorAbs = workingState->getErrorAbs();
44  const Scalar errorRel = workingState->getErrorRel();
45  int order = workingState->getOrder();
46  Scalar dt = workingState->getTimeStep();
47  bool printDtChanges = tsc.getPrintDtChanges();
48 
49  dt = tsc.getInitTimeStep();
50 
51  RCP<Teuchos::FancyOStream> out = tsc.getOStream();
52  Teuchos::OSTab ostab(out,1,"getNextTimeStep");
53 
54  auto changeOrder = [] (int order_old, int order_new, std::string reason) {
55  std::stringstream message;
56  message << " (order = " << std::setw(2) << order_old
57  << ", new = " << std::setw(2) << order_new
58  << ") " << reason << std::endl;
59  return message.str();
60  };
61 
62  // Stepper failure
63  if (workingState->getSolutionStatus() == Status::FAILED) {
64  if (order+1 <= tsc.getMaxOrder()) {
65  if (printDtChanges) *out << changeOrder(order, order+1,
66  "Stepper failure, increasing order.");
67  order++;
68  } else {
69  *out << "Failure - Stepper failed and can not change time step size "
70  << "or order!\n"
71  << " Time step type == CONSTANT_STEP_SIZE\n"
72  << " order = " << order << std::endl;
73  integratorStatus = FAILED;
74  return;
75  }
76  }
77 
78  // Absolute error failure
79  if (errorAbs > tsc.getMaxAbsError()) {
80  if (order+1 <= tsc.getMaxOrder()) {
81  if (printDtChanges) *out << changeOrder(order, order+1,
82  "Absolute error is too large. Increasing order.");
83  order++;
84  } else {
85  *out
86  << "Failure - Absolute error failed and can not change time step "
87  << "size or order!\n"
88  << " Time step type == CONSTANT_STEP_SIZE\n"
89  << " order = " << order
90  << " (errorAbs ="<<errorAbs<<") > (errorMaxAbs ="
91  << tsc.getMaxAbsError()<<")"
92  << std::endl;
93  integratorStatus = FAILED;
94  return;
95  }
96  }
97 
98  // Relative error failure
99  if (errorRel > tsc.getMaxRelError()) {
100  if (order+1 <= tsc.getMaxOrder()) {
101  if (printDtChanges) *out << changeOrder(order, order+1,
102  "Relative error is too large. Increasing order.");
103  order++;
104  } else {
105  *out
106  << "Failure - Relative error failed and can not change time step "
107  << "size or order!\n"
108  << " Time step type == CONSTANT_STEP_SIZE\n"
109  << " order = " << order
110  << " (errorRel ="<<errorRel<<") > (errorMaxRel ="
111  << tsc.getMaxRelError()<<")"
112  << std::endl;
113  integratorStatus = FAILED;
114  return;
115  }
116  }
117 
118  // Consistency checks
119  TEUCHOS_TEST_FOR_EXCEPTION(
120  (order < tsc.getMinOrder() || order > tsc.getMaxOrder()),
121  std::out_of_range,
122  "Error - Solution order is out of range and can not change "
123  "time step size!\n"
124  " Time step type == CONSTANT_STEP_SIZE\n"
125  " [order_min, order_max] = [" <<tsc.getMinOrder()<< ", "
126  <<tsc.getMaxOrder()<< "]\n"
127  " order = " << order << "\n");
128 
129  // update order and dt
130  workingState->setOrder(order);
131  workingState->setTimeStep(dt);
132  }
133 
134 };
135 } // namespace Tempus
136 #endif // Tempus_TimeStepControlStrategy_Constant_hpp
virtual Scalar getMaxRelError() const
virtual Scalar getInitTimeStep() const
Status
Status for the Integrator, the Stepper and the SolutionState.
virtual Scalar getMaxAbsError() const
TimeStepControl manages the time step size. There several mechanicisms that effect the time step size...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
virtual void getNextTimeStep(const TimeStepControl< Scalar > tsc, Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory, Status &integratorStatus) override
Determine the time step size.
StepControlStrategy class for TimeStepControl.
StepControlStrategy class for TimeStepControl.