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_SolutionStateMetaData.hpp"
16 #include "Tempus_SolutionHistory.hpp"
17 #include "Tempus_StepperState.hpp"
18 
19 
20 namespace Tempus {
21 
22 /** \brief StepControlStrategy class for TimeStepControl
23  *
24  */
25 template<class Scalar>
27  : virtual public TimeStepControlStrategy<Scalar>
28 {
29 public:
30 
31  /// Constructor
33 
34  /// Destructor
36 
37  /** \brief Determine the time step size.*/
38  virtual void getNextTimeStep(const TimeStepControl<Scalar> tsc,
40  Status & integratorStatus) override
41  {
42  using Teuchos::RCP;
43  RCP<SolutionState<Scalar> >workingState=solutionHistory->getWorkingState();
44  RCP<SolutionStateMetaData<Scalar> > metaData = workingState->getMetaData();
45  const Scalar errorAbs = metaData->getErrorAbs();
46  const Scalar errorRel = metaData->getErrorRel();
47  int order = metaData->getOrder();
48  Scalar dt = metaData->getDt();
49  bool printChanges = solutionHistory->getVerbLevel() !=
50  Teuchos::as<int>(Teuchos::VERB_NONE);
51 
52  dt = tsc.getInitTimeStep();
53 
54  RCP<Teuchos::FancyOStream> out = tsc.getOStream();
55  Teuchos::OSTab ostab(out,1,"getNextTimeStep");
56 
57  auto changeOrder = [] (int order_old, int order_new, std::string reason) {
58  std::stringstream message;
59  message << " (order = " << std::setw(2) << order_old
60  << ", new = " << std::setw(2) << order_new
61  << ") " << reason << std::endl;
62  return message.str();
63  };
64 
65  // Stepper failure
66  if (workingState->getSolutionStatus() == Status::FAILED) {
67  if (order+1 <= tsc.getMaxOrder()) {
68  if (printChanges) *out << changeOrder(order, order+1,
69  "Stepper failure, increasing order.");
70  order++;
71  } else {
72  *out << "Failure - Stepper failed and can not change time step size "
73  << "or order!\n"
74  << " Time step type == CONSTANT_STEP_SIZE\n"
75  << " order = " << order << std::endl;
76  integratorStatus = FAILED;
77  return;
78  }
79  }
80 
81  // Absolute error failure
82  if (errorAbs > tsc.getMaxAbsError()) {
83  if (order+1 <= tsc.getMaxOrder()) {
84  if (printChanges) *out << changeOrder(order, order+1,
85  "Absolute error is too large. Increasing order.");
86  order++;
87  } else {
88  *out
89  << "Failure - Absolute error failed and can not change time step "
90  << "size or order!\n"
91  << " Time step type == CONSTANT_STEP_SIZE\n"
92  << " order = " << order
93  << " (errorAbs ="<<errorAbs<<") > (errorMaxAbs ="
94  << tsc.getMaxAbsError()<<")"
95  << std::endl;
96  integratorStatus = FAILED;
97  return;
98  }
99  }
100 
101  // Relative error failure
102  if (errorRel > tsc.getMaxRelError()) {
103  if (order+1 <= tsc.getMaxOrder()) {
104  if (printChanges) *out << changeOrder(order, order+1,
105  "Relative error is too large. Increasing order.");
106  order++;
107  } else {
108  *out
109  << "Failure - Relative error failed and can not change time step "
110  << "size or order!\n"
111  << " Time step type == CONSTANT_STEP_SIZE\n"
112  << " order = " << order
113  << " (errorRel ="<<errorRel<<") > (errorMaxRel ="
114  << tsc.getMaxRelError()<<")"
115  << std::endl;
116  integratorStatus = FAILED;
117  return;
118  }
119  }
120 
121  // Consistency checks
122  TEUCHOS_TEST_FOR_EXCEPTION(
123  (order < tsc.getMinOrder() || order > tsc.getMaxOrder()),
124  std::out_of_range,
125  "Error - Solution order is out of range and can not change "
126  "time step size!\n"
127  " Time step type == CONSTANT_STEP_SIZE\n"
128  " [order_min, order_max] = [" <<tsc.getMinOrder()<< ", "
129  <<tsc.getMaxOrder()<< "]\n"
130  " order = " << order << "\n");
131 
132  // update order and dt
133  metaData->setOrder(order);
134  metaData->setDt(dt);
135  }
136 
137 };
138 } // namespace Tempus
139 #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...
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
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.