Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_TimeStepControlStrategyConstant.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_TimeStepControlStrategy_Constant_hpp
11#define Tempus_TimeStepControlStrategy_Constant_hpp
12
13#include "Tempus_config.hpp"
15#include "Tempus_SolutionState.hpp"
16#include "Tempus_SolutionHistory.hpp"
18
19namespace Tempus {
20
24template <class Scalar>
26 : virtual public TimeStepControlStrategy<Scalar> {
27 public:
30 {
31 this->setStrategyType("Constant");
32 this->setStepType("Constant");
33 this->setName("Constant");
34 this->initialize();
35 }
36
38 TimeStepControlStrategyConstant(Scalar constantTimeStep,
39 std::string name = "Constant")
40 : constantTimeStep_(constantTimeStep)
41 {
42 this->setStrategyType("Constant");
43 this->setStepType("Constant");
44 this->setName(name);
45 this->initialize();
46 }
47
50
52 virtual void setNextTimeStep(
53 const TimeStepControl<Scalar> &tsc,
54 Teuchos::RCP<SolutionHistory<Scalar> > solutionHistory,
55 Status &integratorStatus) override
56 {
57 using Teuchos::RCP;
58
59 this->checkInitialized();
60
61 RCP<SolutionState<Scalar> > workingState =
62 solutionHistory->getWorkingState();
63 const Scalar errorAbs = workingState->getErrorAbs();
64 const Scalar errorRel = workingState->getErrorRel();
65 Scalar dt = workingState->getTimeStep();
66
67 RCP<Teuchos::FancyOStream> out = tsc.getOStream();
68 Teuchos::OSTab ostab(out, 1, "setNextTimeStep");
69 out->setOutputToRootOnly(0);
70
71 // Check constant time step
72 if (dt != tsc.getInitTimeStep()) {
73 tsc.printDtChanges(workingState->getIndex(), dt, tsc.getInitTimeStep(),
74 "Resetting constant dt.");
75 dt = tsc.getInitTimeStep();
76 }
77
78 // Stepper failure
79 if (workingState->getSolutionStatus() == Status::FAILED) {
80 *out << "Failure - Stepper failed and can not change time step size!\n"
81 << " Time step type == CONSTANT_STEP_SIZE\n"
82 << std::endl;
83 integratorStatus = FAILED;
84 return;
85 }
86
87 // Absolute error failure
88 if (errorAbs > tsc.getMaxAbsError()) {
89 *out << "Failure - Absolute error failed and can not change time step!\n"
90 << " Time step type == CONSTANT_STEP_SIZE\n"
91 << " (errorAbs =" << errorAbs
92 << ") > (errorMaxAbs =" << tsc.getMaxAbsError() << ")" << std::endl;
93 integratorStatus = FAILED;
94 return;
95 }
96
97 // Relative error failure
98 if (errorRel > tsc.getMaxRelError()) {
99 *out << "Failure - Relative error failed and can not change time step!\n"
100 << " Time step type == CONSTANT_STEP_SIZE\n"
101 << " (errorRel =" << errorRel
102 << ") > (errorMaxRel =" << tsc.getMaxRelError() << ")" << std::endl;
103 integratorStatus = FAILED;
104 return;
105 }
106
107 // update dt
108 workingState->setTimeStep(dt);
109
110 // Set time from initial time, dt, and index to avoid numerical roundoff.
111 const Scalar initTime = tsc.getInitTime();
112 const int initIndex = tsc.getInitIndex();
113 const int index = workingState->getIndex();
114 const Scalar time = (index - initIndex) * dt + initTime;
115 workingState->setTime(time);
116 }
117
119
120 std::string description() const override
121 {
122 return "Tempus::TimeStepControlStrategyConstant";
123 }
124
125 void describe(Teuchos::FancyOStream &out,
126 const Teuchos::EVerbosityLevel verbLevel) const override
127 {
128 auto l_out = Teuchos::fancyOStream(out.getOStream());
129 Teuchos::OSTab ostab(*l_out, 2, this->description());
130 l_out->setOutputToRootOnly(0);
131
132 *l_out << "\n--- " << this->description() << " ---" << std::endl;
133
134 if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_MEDIUM)) {
135 *l_out << " Strategy Type = " << this->getStrategyType() << std::endl
136 << " Step Type = " << this->getStepType() << std::endl
137 << " Time Step = " << getConstantTimeStep() << std::endl;
138
139 *l_out << std::string(this->description().length() + 8, '-') << std::endl;
140 }
141 }
143
145 virtual Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
146 const override
147 {
148 Teuchos::RCP<Teuchos::ParameterList> pl =
149 Teuchos::parameterList("Time Step Control Strategy");
150
151 pl->set<std::string>("Strategy Type", this->getStrategyType(), "Constant");
152 pl->set<double>("Time Step", getConstantTimeStep());
153
154 return pl;
155 }
156
157 virtual void initialize() const override
158 {
159 this->isInitialized_ = true; // Only place where this is set to true!
160 }
161
162 virtual Scalar getConstantTimeStep() const { return constantTimeStep_; }
163
164 virtual void setConstantTimeStep(Scalar dt)
165 {
167 this->isInitialized_ = false;
168 }
169
170 private:
172};
173
175template <class Scalar>
176Teuchos::RCP<TimeStepControlStrategyConstant<Scalar> >
178 const Teuchos::RCP<Teuchos::ParameterList> &pList,
179 std::string name = "Constant")
180{
181 auto tscs = Teuchos::rcp(new TimeStepControlStrategyConstant<Scalar>());
182 if (pList == Teuchos::null || pList->numParams() == 0) return tscs;
183
184 TEUCHOS_TEST_FOR_EXCEPTION(
185 pList->get<std::string>("Strategy Type") != "Constant", std::logic_error,
186 "Error - Strategy Type != 'Constant'. (='" +
187 pList->get<std::string>("Strategy Type") + "')\n");
188
189 pList->validateParametersAndSetDefaults(*tscs->getValidParameters());
190
191 tscs->setConstantTimeStep(pList->get<double>("Time Step"));
192
193 tscs->setName(name);
194 tscs->initialize();
195
196 return tscs;
197}
198
200template <class Scalar>
201Teuchos::RCP<Teuchos::ParameterList> getTimeStepControlStrategyConstantPL()
202{
204 return Teuchos::rcp_const_cast<Teuchos::ParameterList>(
205 t->getValidParameters());
206}
207
208} // namespace Tempus
209#endif // Tempus_TimeStepControlStrategy_Constant_hpp
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
StepControlStrategy class for TimeStepControl.
TimeStepControlStrategyConstant(Scalar constantTimeStep, std::string name="Constant")
Full Constructor.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
virtual void setNextTimeStep(const TimeStepControl< Scalar > &tsc, Teuchos::RCP< SolutionHistory< Scalar > > solutionHistory, Status &integratorStatus) override
Determine the time step size.
virtual Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return ParameterList with current values.
TimeStepControlStrategy class for TimeStepControl.
bool isInitialized_
Bool if strategy is initialized.
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
virtual void printDtChanges(int istep, Scalar dt_old, Scalar dt_new, std::string reason) const
Status
Status for the Integrator, the Stepper and the SolutionState.
Teuchos::RCP< Teuchos::ParameterList > getTimeStepControlStrategyConstantPL()
Nonmember function to return ParameterList with default values.
Teuchos::RCP< TimeStepControlStrategyConstant< Scalar > > createTimeStepControlStrategyConstant(const Teuchos::RCP< Teuchos::ParameterList > &pList, std::string name="Constant")
Nonmember constructor.