Tempus  Version of the Day
Time Integration
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
Tempus::TimeStepControl::getMaxRelError
virtual Scalar getMaxRelError() const
Definition: Tempus_TimeStepControl_decl.hpp:110
Tempus::TimeStepControl::getMaxOrder
virtual int getMaxOrder() const
Definition: Tempus_TimeStepControl_decl.hpp:116
Tempus::solutionHistory
Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory(Teuchos::RCP< Teuchos::ParameterList > pList=Teuchos::null)
Nonmember constructor.
Definition: Tempus_SolutionHistory_impl.hpp:504
Tempus_TimeStepControlStrategy.hpp
Tempus::TimeStepControlStrategyConstant
StepControlStrategy class for TimeStepControl.
Definition: Tempus_TimeStepControlStrategyConstant.hpp:26
Tempus
Definition: Tempus_AdjointAuxSensitivityModelEvaluator_decl.hpp:20
Tempus::TimeStepControl::getMinOrder
virtual int getMinOrder() const
Definition: Tempus_TimeStepControl_decl.hpp:112
Tempus::TimeStepControlStrategyConstant::~TimeStepControlStrategyConstant
virtual ~TimeStepControlStrategyConstant()
Destructor.
Definition: Tempus_TimeStepControlStrategyConstant.hpp:35
Tempus::TimeStepControlStrategyConstant::getNextTimeStep
virtual void getNextTimeStep(const TimeStepControl< Scalar > tsc, Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory, Status &integratorStatus) override
Determine the time step size.
Definition: Tempus_TimeStepControlStrategyConstant.hpp:38
Tempus_StepperState.hpp
Tempus::TimeStepControlStrategyConstant::TimeStepControlStrategyConstant
TimeStepControlStrategyConstant()
Constructor.
Definition: Tempus_TimeStepControlStrategyConstant.hpp:32
Tempus::TimeStepControl::getInitTimeStep
virtual Scalar getInitTimeStep() const
Definition: Tempus_TimeStepControl_decl.hpp:100
Tempus::TimeStepControl
TimeStepControl manages the time step size. There several mechanicisms that effect the time step size...
Definition: Tempus_Integrator.hpp:26
Tempus::SolutionHistory
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
Definition: Tempus_Integrator.hpp:25
Tempus::TimeStepControl::getMaxAbsError
virtual Scalar getMaxAbsError() const
Definition: Tempus_TimeStepControl_decl.hpp:108
Tempus::TimeStepControlStrategy
StepControlStrategy class for TimeStepControl.
Definition: Tempus_TimeStepControlStrategy.hpp:26
Tempus::FAILED
Definition: Tempus_Types.hpp:18
Tempus::Status
Status
Status for the Integrator, the Stepper and the SolutionState.
Definition: Tempus_Types.hpp:16