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: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 
14 #include "Thyra_VectorStdOps.hpp"
15 
16 #include "Tempus_StepperFactory.hpp"
17 #include "Tempus_TimeStepControl.hpp"
22 
23 #include "../TestModels/SinCosModel.hpp"
24 #include "../TestModels/DahlquistTestModel.hpp"
25 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
26 
27 #include <fstream>
28 #include <vector>
29 
30 namespace Tempus_Unit_Test {
31 
32 using Teuchos::RCP;
33 using Teuchos::rcp;
34 using Teuchos::rcp_const_cast;
35 using Teuchos::rcp_dynamic_cast;
37 using Teuchos::sublist;
38 using Teuchos::getParametersFromXmlFile;
39 
40 
41 // ************************************************************
42 // ************************************************************
43 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, Default_Construction_1)
44 {
46  TEUCHOS_TEST_FOR_EXCEPT(tscsc->isInitialized()); // Should NOT be initialized.
47 
48  auto tscsConstant = rcp(new Tempus::TimeStepControlStrategyConstant<double>());
49  tscsc->addStrategy(tscsConstant);
50  tscsc->initialize();
51  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
52 
53  TEST_COMPARE(tscsc->size(), ==, 1);
54 
55  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
56  strategies = tscsc->getStrategies();
57 
58  auto strategyConstant = rcp_dynamic_cast<Tempus::TimeStepControlStrategyConstant<double> > (strategies[0]);
59 
60  TEUCHOS_TEST_FOR_EXCEPT(strategyConstant->getStepType() != "Constant");
61  TEUCHOS_TEST_FOR_EXCEPT(strategyConstant->getConstantTimeStep() != 0.0);
62 }
63 
64 
65 // ************************************************************
66 // ************************************************************
67 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, Default_Construction_2)
68 {
70  TEUCHOS_TEST_FOR_EXCEPT(tscsc->isInitialized()); // Should NOT be initialized.
71 
72  auto tscsBasicVS = rcp(new Tempus::TimeStepControlStrategyBasicVS<double>());
73  tscsc->addStrategy(tscsBasicVS);
75  tscsc->addStrategy(tscsIntCtrl);
76  tscsc->initialize();
77  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
78 
79  TEST_COMPARE(tscsc->size(), ==, 2);
80 
81  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
82  strategies = tscsc->getStrategies();
83 
84  auto strategyBasicVS = rcp_dynamic_cast<Tempus::TimeStepControlStrategyBasicVS<double> > (strategies[0]);
85  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getStrategyType() != "Basic VS");
86 
87  auto strategyIntCtrl = rcp_dynamic_cast<Tempus::TimeStepControlStrategyIntegralController<double> > (strategies[1]);
88  TEUCHOS_TEST_FOR_EXCEPT(strategyIntCtrl->getStrategyType() != "Integral Controller");
89 }
90 
91 
92 // ************************************************************
93 // ************************************************************
94 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, Create_Construction)
95 {
96  // Construct ParmeterList for testing.
98  auto tscs_BasicVS =
100  auto tscs_IC =
102 
103  tscsc_temp->addStrategy(tscs_BasicVS);
104  tscsc_temp->addStrategy(tscs_IC);
105  tscsc_temp->initialize();
106  TEUCHOS_TEST_FOR_EXCEPT(!tscsc_temp->isInitialized());
107 
108  auto pl = rcp_const_cast<Teuchos::ParameterList>(tscsc_temp->getValidParameters());
109 
110  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
111 
112  TEST_COMPARE(tscsc->size(), ==, 2);
113 
114  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
115  strategies = tscsc->getStrategies();
116 
117  auto strategyBasicVS = Teuchos::rcp_dynamic_cast<Tempus::TimeStepControlStrategyBasicVS<double> > (strategies[0]);
118 
119  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getStepType() != "Variable");
120  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getAmplFactor() != 1.75);
121  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getReductFactor() != 0.5);
122  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMinEta() != 0.0);
123  TEUCHOS_TEST_FOR_EXCEPT(strategyBasicVS->getMaxEta() != 1.0e+16);
124 
125  auto strategyIC = Teuchos::rcp_dynamic_cast<Tempus::TimeStepControlStrategyIntegralController<double> > (strategies[1]);
126 
127  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getStepType() != "Variable");
128  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getController() != "PID");
129  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKI() != 0.58);
130  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKP() != 0.21);
131  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getKD() != 0.10);
132  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactor() != 0.90);
133  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getSafetyFactorAfterReject() != 0.90);
134  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMax() != 5.0);
135  TEUCHOS_TEST_FOR_EXCEPT(strategyIC->getFacMin() != 0.5);
136 }
137 
138 
139 // ************************************************************
140 // ************************************************************
141 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, getValidParameters)
142 {
143  // Construct ParmeterList for testing.
144  auto tscsc_temp = rcp(new Tempus::TimeStepControlStrategyComposite<double>());
145  auto tscs_BasicVS = rcp(new Tempus::TimeStepControlStrategyBasicVS<double>());
147 
148  tscsc_temp->addStrategy(tscs_BasicVS);
149  tscsc_temp->addStrategy(tscs_IC);
150  tscsc_temp->initialize();
151  TEUCHOS_TEST_FOR_EXCEPT(!tscsc_temp->isInitialized());
152 
153  auto pl = tscsc_temp->getValidParameters();
154 
155  TEST_COMPARE ( pl->get<std::string>("Strategy Type"), ==, "Composite");
156  TEST_COMPARE ( pl->get<std::string>("Strategy List"), ==, "Basic VS, Integral Controller");
157  TEST_COMPARE ( pl->isSublist("Basic VS") , ==, true);
158  TEST_COMPARE ( pl->isSublist("Integral Controller") , ==, true);
159 
160  { // Ensure that parameters are "used", excluding sublists.
161  std::ostringstream unusedParameters;
162  pl->unused(unusedParameters);
163  TEST_COMPARE ( unusedParameters.str(), ==,
164  "WARNING: Parameter \"Basic VS\" [unused] is unused\n"
165  "WARNING: Parameter \"Integral Controller\" [unused] is unused\n");
166  }
167 
168  auto BasicVS_PL = pl->sublist("Basic VS");
169  TEST_COMPARE ( BasicVS_PL.get<std::string>("Strategy Type") , ==, "Basic VS");
170  TEST_FLOATING_EQUALITY( BasicVS_PL.get<double>("Amplification Factor"), 1.75, 1.0e-14);
171  TEST_FLOATING_EQUALITY( BasicVS_PL.get<double>("Reduction Factor") , 0.5 , 1.0e-14);
172  TEST_FLOATING_EQUALITY( BasicVS_PL.get<double>("Minimum Value Monitoring Function"), 0.0 , 1.0e-14);
173  TEST_FLOATING_EQUALITY( BasicVS_PL.get<double>("Maximum Value Monitoring Function"), 1.0e+16, 1.0e-14);
174 
175  { // Ensure that parameters are "used", excluding sublists.
176  std::ostringstream unusedParameters;
177  BasicVS_PL.unused(unusedParameters);
178  TEST_COMPARE ( unusedParameters.str(), ==, "");
179  }
180 
181  auto IntCtrl_PL = pl->sublist("Integral Controller");
182  TEST_COMPARE ( IntCtrl_PL.get<std::string>("Strategy Type") , ==, "Integral Controller");
183  TEST_COMPARE ( IntCtrl_PL.get<std::string>("Controller Type") , ==, "PID");
184  TEST_FLOATING_EQUALITY( IntCtrl_PL.get<double>("KI"), 0.58, 1.0e-14);
185  TEST_FLOATING_EQUALITY( IntCtrl_PL.get<double>("KP"), 0.21, 1.0e-14);
186  TEST_FLOATING_EQUALITY( IntCtrl_PL.get<double>("KD"), 0.1 , 1.0e-14);
187  TEST_FLOATING_EQUALITY( IntCtrl_PL.get<double>("Safety Factor"), 0.9 , 1.0e-14);
188  TEST_FLOATING_EQUALITY( IntCtrl_PL.get<double>("Safety Factor After Step Rejection"), 0.9 , 1.0e-14);
189  TEST_FLOATING_EQUALITY( IntCtrl_PL.get<double>("Maximum Safety Factor"), 5.0 , 1.0e-14);
190  TEST_FLOATING_EQUALITY( IntCtrl_PL.get<double>("Minimum Safety Factor"), 0.5 , 1.0e-14);
191 
192  { // Ensure that parameters are "used", excluding sublists.
193  std::ostringstream unusedParameters;
194  IntCtrl_PL.unused(unusedParameters);
195  TEST_COMPARE ( unusedParameters.str(), ==, "");
196  }
197 }
198 
199 
200 // ************************************************************
201 // ************************************************************
202 TEUCHOS_UNIT_TEST(TimeStepControlStrategyComposite, Setting_Strategies_PLs)
203 {
204 
205  { // Test with default ParameterList
206  auto pl = Tempus::getTimeStepControlStrategyCompositePL<double>();
207 
208  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
209  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
210 
211  std::vector<Teuchos::RCP<Tempus::TimeStepControlStrategy<double>>>
212  strategies = tscsc->getStrategies();
213 
214  // Default strategy is "Constant"
215  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStepType() == "Constant"));
216  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategyType() == "Composite"));
217  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategies().size() == 1));
218  TEUCHOS_TEST_FOR_EXCEPT(!(strategies[0]->getStepType() == "Constant"));
219  TEUCHOS_TEST_FOR_EXCEPT(!(strategies[0]->getStrategyType() == "Constant"));
220  }
221 
222  { // Test with empty "Strategy List"
223  auto pl = Tempus::getTimeStepControlStrategyCompositePL<double>();
224  pl->set("Strategy List", "");
225  pl->remove("Constant");
226 
227  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
228  TEUCHOS_TEST_FOR_EXCEPT(tscsc->isInitialized()); // Should NOT be initialized!
229 
230  // Default strategy is "Variable"
231  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStepType() == "Variable"));
232  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategyType() == "Composite"));
233  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategies().size() == 0));
234  }
235 
236  { // Test with non-Tempus strategy
237  auto pl = Tempus::getTimeStepControlStrategyCompositePL<double>();
238  pl->remove("Constant");
239  pl->set("Strategy List", "Application Strategy");
240 
241  auto nonTempusStrategyPL =
242  Teuchos::parameterList("Application Strategy");
243  nonTempusStrategyPL->set<std::string>("Strategy Type", "Application Strategy");
244  nonTempusStrategyPL->set<double>("Secret Sauce", 1.2345);
245  pl->set("Application Strategy", *nonTempusStrategyPL);
246 
247  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
248  TEUCHOS_TEST_FOR_EXCEPT(tscsc->isInitialized()); // Should NOT be initialized!
249 
250  // Default strategy is "Variable"
251  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStepType() == "Variable"));
252  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategyType() == "Composite"));
253  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategies().size() == 0));
254  }
255 
256  { // Test with two Tempus strategies and a non-Tempus strategy
257 
258  // Setup ParameterList for this test.
260  auto tscsBasicVS = rcp(new Tempus::TimeStepControlStrategyBasicVS<double>());
261  temp->addStrategy(tscsBasicVS);
263  temp->addStrategy(tscsIntCtrl);
264  temp->initialize();
265 
266  auto pl = rcp_const_cast<Teuchos::ParameterList>(temp->getValidParameters());
267  auto sList = pl->get<std::string>("Strategy List");
268  pl->set("Strategy List", sList+", Application Strategy");
269 
270  auto nonTempusStrategyPL =
271  Teuchos::parameterList("Application Strategy");
272  nonTempusStrategyPL->set<std::string>("Strategy Type", "Application Strategy");
273  nonTempusStrategyPL->set<double>("Secret Sauce", 1.2345);
274  pl->set("Application Strategy", *nonTempusStrategyPL);
275 
276  auto tscsc = Tempus::createTimeStepControlStrategyComposite<double>(pl);
277  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
278 
279  // Default strategy is "Constant"
280  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStepType() == "Variable"));
281  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategyType() == "Composite"));
282  TEUCHOS_TEST_FOR_EXCEPT(!(tscsc->getStrategies().size() == 2));
283  }
284 
285 }
286 
287 
288 } // namespace Tempus_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)