Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeStepControlStrategyComposite.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_TimeStepControl.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(TimeStepControlStrategyComposite, Default_Construction_1)
30 {
33  tscsc->isInitialized()); // Should NOT be initialized.
34 
35  auto tscsConstant =
37  tscsc->addStrategy(tscsConstant);
38  tscsc->initialize();
39  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
40 
41  TEST_COMPARE(tscsc->size(), ==, 1);
42 
43  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
44  strategies = tscsc->getStrategies();
45 
46  auto strategyConstant =
48  strategies[0]);
49 
50  TEUCHOS_TEST_FOR_EXCEPT(strategyConstant->getStepType() != "Constant");
51  TEUCHOS_TEST_FOR_EXCEPT(strategyConstant->getConstantTimeStep() != 0.0);
52 }
53 
54 // ************************************************************
55 // ************************************************************
56 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, Default_Construction_2)
57 {
60  tscsc->isInitialized()); // Should NOT be initialized.
61 
62  auto tscsBasicVS = rcp(new Tempus::TimeStepControlStrategyBasicVS<double>());
63  tscsc->addStrategy(tscsBasicVS);
64  auto tscsIntCtrl =
66  tscsc->addStrategy(tscsIntCtrl);
67  tscsc->initialize();
68  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
69 
70  TEST_COMPARE(tscsc->size(), ==, 2);
71 
72  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
73  strategies = tscsc->getStrategies();
74 
75  auto strategyBasicVS =
77  strategies[0]);
78  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getStrategyType() != "Basic VS");
79 
80  auto strategyIntCtrl = rcp_dynamic_cast<
82  TEUCHOS_TEST_FOR_EXCEPT(strategyIntCtrl->getStrategyType() !=
83  "Integral Controller");
84 }
85 
86 // ************************************************************
87 // ************************************************************
88 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, Create_Construction)
89 {
90  // Construct ParmeterList for testing.
92  auto tscs_BasicVS = rcp(new Tempus::TimeStepControlStrategyBasicVS<double>());
93  auto tscs_IC =
95 
96  tscsc_temp->addStrategy(tscs_BasicVS);
97  tscsc_temp->addStrategy(tscs_IC);
98  tscsc_temp->initialize();
99  TEUCHOS_TEST_FOR_EXCEPT(!tscsc_temp->isInitialized());
100 
101  auto pl =
102  rcp_const_cast<Teuchos::ParameterList>(tscsc_temp->getValidParameters());
103 
104  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
105 
106  TEST_COMPARE(tscsc->size(), ==, 2);
107 
108  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
109  strategies = tscsc->getStrategies();
110 
111  auto strategyBasicVS =
112  Teuchos::rcp_dynamic_cast<Tempus::TimeStepControlStrategyBasicVS<double>>(
113  strategies[0]);
114 
115  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getStepType() != "Variable");
116  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getAmplFactor() != 1.75);
117  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getReductFactor() != 0.5);
118  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMinEta() != 0.0);
119  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMaxEta() != 1.0e+16);
120 
121  auto strategyIC = Teuchos::rcp_dynamic_cast<
123 
124  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getStepType() != "Variable");
125  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getController() != "PID");
126  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKI() != 0.58);
127  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKP() != 0.21);
128  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKD() != 0.10);
129  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactor() != 0.90);
130  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactorAfterReject() != 0.90);
131  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMax() != 5.0);
132  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMin() != 0.5);
133 }
134 
135 // ************************************************************
136 // ************************************************************
137 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, getValidParameters)
138 {
139  // Construct ParmeterList for testing.
140  auto tscsc_temp = rcp(new Tempus::TimeStepControlStrategyComposite<double>());
141  auto tscs_BasicVS = rcp(new Tempus::TimeStepControlStrategyBasicVS<double>());
142  auto tscs_IC =
144 
145  tscsc_temp->addStrategy(tscs_BasicVS);
146  tscsc_temp->addStrategy(tscs_IC);
147  tscsc_temp->initialize();
148  TEUCHOS_TEST_FOR_EXCEPT(!tscsc_temp->isInitialized());
149 
150  auto pl = tscsc_temp->getValidParameters();
151 
152  TEST_COMPARE(pl->get<std::string>("Strategy Type"), ==, "Composite");
153  TEST_COMPARE(pl->get<std::string>("Strategy List"), ==,
154  "Basic VS, Integral Controller");
155  TEST_COMPARE(pl->isSublist("Basic VS"), ==, true);
156  TEST_COMPARE(pl->isSublist("Integral Controller"), ==, true);
157 
158  { // Ensure that parameters are "used", excluding sublists.
159  std::ostringstream unusedParameters;
160  pl->unused(unusedParameters);
161  TEST_COMPARE(
162  unusedParameters.str(), ==,
163  "WARNING: Parameter \"Basic VS\" [unused] is unused\n"
164  "WARNING: Parameter \"Integral Controller\" [unused] is unused\n");
165  }
166 
167  auto BasicVS_PL = pl->sublist("Basic VS");
168  TEST_COMPARE(BasicVS_PL.get<std::string>("Strategy Type"), ==, "Basic VS");
169  TEST_FLOATING_EQUALITY(BasicVS_PL.get<double>("Amplification Factor"), 1.75,
170  1.0e-14);
171  TEST_FLOATING_EQUALITY(BasicVS_PL.get<double>("Reduction Factor"), 0.5,
172  1.0e-14);
174  BasicVS_PL.get<double>("Minimum Value Monitoring Function"), 0.0,
175  1.0e-14);
177  BasicVS_PL.get<double>("Maximum Value Monitoring Function"), 1.0e+16,
178  1.0e-14);
179 
180  { // Ensure that parameters are "used", excluding sublists.
181  std::ostringstream unusedParameters;
182  BasicVS_PL.unused(unusedParameters);
183  TEST_COMPARE(unusedParameters.str(), ==, "");
184  }
185 
186  auto IntCtrl_PL = pl->sublist("Integral Controller");
187  TEST_COMPARE(IntCtrl_PL.get<std::string>("Strategy Type"), ==,
188  "Integral Controller");
189  TEST_COMPARE(IntCtrl_PL.get<std::string>("Controller Type"), ==, "PID");
190  TEST_FLOATING_EQUALITY(IntCtrl_PL.get<double>("KI"), 0.58, 1.0e-14);
191  TEST_FLOATING_EQUALITY(IntCtrl_PL.get<double>("KP"), 0.21, 1.0e-14);
192  TEST_FLOATING_EQUALITY(IntCtrl_PL.get<double>("KD"), 0.1, 1.0e-14);
193  TEST_FLOATING_EQUALITY(IntCtrl_PL.get<double>("Safety Factor"), 0.9, 1.0e-14);
195  IntCtrl_PL.get<double>("Safety Factor After Step Rejection"), 0.9,
196  1.0e-14);
197  TEST_FLOATING_EQUALITY(IntCtrl_PL.get<double>("Maximum Safety Factor"), 5.0,
198  1.0e-14);
199  TEST_FLOATING_EQUALITY(IntCtrl_PL.get<double>("Minimum Safety Factor"), 0.5,
200  1.0e-14);
201 
202  { // Ensure that parameters are "used", excluding sublists.
203  std::ostringstream unusedParameters;
204  IntCtrl_PL.unused(unusedParameters);
205  TEST_COMPARE(unusedParameters.str(), ==, "");
206  }
207 }
208 
209 // ************************************************************
210 // ************************************************************
211 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, Setting_Strategies_PLs)
212 {
213  { // Test with default ParameterList
214  auto pl = Tempus::getTimeStepControlStrategyCompositePL<double>();
215 
216  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
217  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
218 
219  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
220  strategies = tscsc->getStrategies();
221 
222  // Default strategy is "Constant"
223  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStepType() == "Constant"));
224  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategyType() == "Composite"));
225  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategies().size() == 1));
226  TEUCHOS_TEST_FOR_EXCEPT(!(strategies[0]->getStepType() == "Constant"));
227  TEUCHOS_TEST_FOR_EXCEPT(!(strategies[0]->getStrategyType() == "Constant"));
228  }
229 
230  { // Test with empty "Strategy List"
231  auto pl = Tempus::getTimeStepControlStrategyCompositePL<double>();
232  pl->set("Strategy List", "");
233  pl->remove("Constant");
234 
235  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
237  tscsc->isInitialized()); // Should NOT be initialized!
238 
239  // Default strategy is "Variable"
240  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStepType() == "Variable"));
241  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategyType() == "Composite"));
242  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategies().size() == 0));
243  }
244 
245  { // Test with non-Tempus strategy
246  auto pl = Tempus::getTimeStepControlStrategyCompositePL<double>();
247  pl->remove("Constant");
248  pl->set("Strategy List", "Application Strategy");
249 
250  auto nonTempusStrategyPL = Teuchos::parameterList("Application Strategy");
251  nonTempusStrategyPL->set<std::string>("Strategy Type",
252  "Application Strategy");
253  nonTempusStrategyPL->set<double>("Secret Sauce", 1.2345);
254  pl->set("Application Strategy", *nonTempusStrategyPL);
255 
256  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
258  tscsc->isInitialized()); // Should NOT be initialized!
259 
260  // Default strategy is "Variable"
261  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStepType() == "Variable"));
262  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategyType() == "Composite"));
263  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategies().size() == 0));
264  }
265 
266  { // Test with two Tempus strategies and a non-Tempus strategy
267 
268  // Setup ParameterList for this test.
270  auto tscsBasicVS =
272  temp->addStrategy(tscsBasicVS);
273  auto tscsIntCtrl =
275  temp->addStrategy(tscsIntCtrl);
276  temp->initialize();
277 
278  auto pl =
279  rcp_const_cast<Teuchos::ParameterList>(temp->getValidParameters());
280  auto sList = pl->get<std::string>("Strategy List");
281  pl->set("Strategy List", sList + ", Application Strategy");
282 
283  auto nonTempusStrategyPL = Teuchos::parameterList("Application Strategy");
284  nonTempusStrategyPL->set<std::string>("Strategy Type",
285  "Application Strategy");
286  nonTempusStrategyPL->set<double>("Secret Sauce", 1.2345);
287  pl->set("Application Strategy", *nonTempusStrategyPL);
288 
289  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
290  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
291 
292  // Default strategy is "Constant"
293  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStepType() == "Variable"));
294  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategyType() == "Composite"));
295  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategies().size() == 2));
296  }
297 }
298 
299 } // namespace Tempus_Unit_Test
T & get(const std::string &name, T def_value)
StepControlStrategy class for TimeStepControl.
#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)
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
StepControlStrategy class for TimeStepControl.
TimeStepControlStrategyComposite loops over a vector of TimeStepControlStrategies.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)