Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeStepControlStrategyBasicVS.cpp
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 
11 
12 #include "Tempus_StepperFactory.hpp"
13 #include "Tempus_TimeStepControl.hpp"
15 
16 #include "../TestModels/DahlquistTestModel.hpp"
17 
18 namespace Tempus_Unit_Test {
19 
21 using Teuchos::RCP;
22 using Teuchos::rcp;
23 using Teuchos::rcp_const_cast;
24 using Teuchos::rcp_dynamic_cast;
25 using Teuchos::sublist;
26 
27 // ************************************************************
28 // ************************************************************
29 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, Default_Construction)
30 {
32  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
33 
34  // Test the get functions (i.e., defaults).
35  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Basic VS");
36  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
37  TEUCHOS_TEST_FOR_EXCEPT(tscs->getAmplFactor() != 1.75);
38  TEUCHOS_TEST_FOR_EXCEPT(tscs->getReductFactor() != 0.5);
39  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMinEta() != 0.0);
40  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMaxEta() != 1.0e+16);
41 
42  // Test the set functions.
43  tscs->setAmplFactor(1.33);
44  tscs->initialize();
45  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
46  tscs->setReductFactor(0.75);
47  tscs->initialize();
48  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
49  tscs->setMinEta(0.01);
50  tscs->initialize();
51  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
52  tscs->setMaxEta(0.05);
53  tscs->initialize();
54  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
55 
56  TEUCHOS_TEST_FOR_EXCEPT(tscs->getAmplFactor() != 1.33);
57  TEUCHOS_TEST_FOR_EXCEPT(tscs->getReductFactor() != 0.75);
58  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMinEta() != 0.01);
59  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMaxEta() != 0.05);
60 }
61 
62 // ************************************************************
63 // ************************************************************
64 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, Full_Construction)
65 {
67  1.33, 0.75, 0.01, 0.05));
68  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
69 
70  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Basic VS");
71  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
72  TEUCHOS_TEST_FOR_EXCEPT(tscs->getAmplFactor() != 1.33);
73  TEUCHOS_TEST_FOR_EXCEPT(tscs->getReductFactor() != 0.75);
74  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMinEta() != 0.01);
75  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMaxEta() != 0.05);
76 }
77 
78 // ************************************************************
79 // ************************************************************
80 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, Create_Construction)
81 {
82  auto pl = Tempus::getTimeStepControlStrategyBasicVS_PL<double>();
83 
84  pl->set<double>("Amplification Factor", 1.33);
85  pl->set<double>("Reduction Factor", 0.75);
86  pl->set<double>("Minimum Value Monitoring Function", 0.01);
87  pl->set<double>("Maximum Value Monitoring Function", 0.05);
88 
89  auto tscs = Tempus::createTimeStepControlStrategyBasicVS<double>(pl);
90 
91  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Basic VS");
92  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
93  TEUCHOS_TEST_FOR_EXCEPT(tscs->getAmplFactor() != 1.33);
94  TEUCHOS_TEST_FOR_EXCEPT(tscs->getReductFactor() != 0.75);
95  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMinEta() != 0.01);
96  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMaxEta() != 0.05);
97 }
98 
99 // ************************************************************
100 // ************************************************************
101 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, setNextTimeStep)
102 {
104  tscs->setAmplFactor(1.1);
105  tscs->setReductFactor(0.5);
106  tscs->setMinEta(0.01);
107  tscs->setMaxEta(0.05);
108  tscs->initialize();
109  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
110 
111  // Setup the TimeStepControl --------------------------------
112  auto tsc = rcp(new Tempus::TimeStepControl<double>());
113  tsc->setTimeStepControlStrategy(tscs);
114  tsc->setInitTime(0.0);
115  tsc->setFinalTime(10.0);
116  tsc->setMinTimeStep(0.01);
117  tsc->setInitTimeStep(0.1);
118  tsc->setMaxTimeStep(1.0);
119  tsc->setFinalIndex(100);
120  tsc->initialize();
121  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
123 
124  // Setup the SolutionHistory --------------------------------
125  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>());
126  auto inArgsIC = model->getNominalValues();
127  auto icSolution =
128  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
129  auto icState = Tempus::createSolutionStateX<double>(icSolution);
130  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
131 
132  // Test Reducing timestep
133  {
134  solutionHistory->addState(icState);
135  solutionHistory->getCurrentState()->setTimeStep(0.5);
136  solutionHistory->getCurrentState()->setTime(0.0);
137  solutionHistory->getCurrentState()->setIndex(0);
138 
139  // Set up solution history with two time steps.
140  for (int i = 0; i < 2; i++) {
141  solutionHistory->initWorkingState();
142 
143  tsc->setNextTimeStep(solutionHistory, status);
144 
145  { // Mock takeStep
146  auto currentState = solutionHistory->getCurrentState();
147  auto workingState = solutionHistory->getWorkingState();
148  auto xN = workingState->getX();
149  Thyra::Vp_S(xN.ptr(), 1.0);
150  workingState->setSolutionStatus(Tempus::Status::PASSED);
151  workingState->computeNorms(currentState);
152  }
153 
154  solutionHistory->promoteWorkingState();
155  // out << " x = " << get_ele(*(x), 0) << std::endl;
156  }
157 
158  auto currentState = solutionHistory->getCurrentState();
159  TEST_FLOATING_EQUALITY(currentState->getTimeStep(), 0.25, 1.0e-14);
160  TEST_FLOATING_EQUALITY(currentState->getTime(), 0.75, 1.0e-14);
161  }
162 
163  // Test increasing timestep
164  {
165  solutionHistory->clear();
166  solutionHistory->addState(icState);
167  solutionHistory->getCurrentState()->setTimeStep(0.5);
168  solutionHistory->getCurrentState()->setTime(0.0);
169  solutionHistory->getCurrentState()->setIndex(0);
170 
171  // Set up solution history with two time steps.
172  for (int i = 0; i < 2; i++) {
173  solutionHistory->initWorkingState();
174 
175  tsc->setNextTimeStep(solutionHistory, status);
176 
177  { // Mock takeStep
178  auto currentState = solutionHistory->getCurrentState();
179  auto workingState = solutionHistory->getWorkingState();
180  auto xN = workingState->getX();
181  Thyra::Vp_S(xN.ptr(), 0.0);
182  workingState->setSolutionStatus(Tempus::Status::PASSED);
183  workingState->computeNorms(currentState);
184  }
185 
186  solutionHistory->promoteWorkingState();
187  // auto x = solutionHistory->getCurrentState()->getX();
188  // out << " x = " << get_ele(*(x), 0) << std::endl;
189  }
190 
191  auto currentState = solutionHistory->getCurrentState();
192  TEST_FLOATING_EQUALITY(currentState->getTimeStep(), 0.55, 1.0e-14);
193  TEST_FLOATING_EQUALITY(currentState->getTime(), 1.05, 1.0e-14);
194  }
195 }
196 
197 // ************************************************************
198 // ************************************************************
199 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, getValidParameters)
200 {
202 
203  auto pl = tscs->getValidParameters();
204 
205  TEST_COMPARE(pl->get<std::string>("Strategy Type"), ==, "Basic VS");
206  TEST_FLOATING_EQUALITY(pl->get<double>("Amplification Factor"), 1.75,
207  1.0e-14);
208  TEST_FLOATING_EQUALITY(pl->get<double>("Reduction Factor"), 0.5, 1.0e-14);
209  TEST_FLOATING_EQUALITY(pl->get<double>("Minimum Value Monitoring Function"),
210  0.0, 1.0e-14);
211  TEST_FLOATING_EQUALITY(pl->get<double>("Maximum Value Monitoring Function"),
212  1.0e+16, 1.0e-14);
213 
214  { // Ensure that parameters are "used", excluding sublists.
215  std::ostringstream unusedParameters;
216  pl->unused(unusedParameters);
217  TEST_COMPARE(unusedParameters.str(), ==, "");
218  }
219 }
220 
221 } // namespace Tempus_Unit_Test
StepControlStrategy class for TimeStepControl.
The classic Dahlquist Test Problem.
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Status
Status for the Integrator, the Stepper and the SolutionState.
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
TimeStepControl manages the time step size. There several mechanisms that effect the time step size a...
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)