Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_IntegratorBasic.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 
12 
13 #include "Tempus_StepperBackwardEuler.hpp"
15 
16 namespace Tempus_Unit_Test {
17 
19 using Teuchos::RCP;
20 using Teuchos::rcp;
21 using Teuchos::rcp_const_cast;
22 using Teuchos::rcp_dynamic_cast;
23 using Teuchos::sublist;
24 
26 
27 // ************************************************************
28 // ************************************************************
29 TEUCHOS_UNIT_TEST(IntegratorBasic, Default_Construction)
30 {
31  // Default Construction.
32  auto integrator = Teuchos::rcp(new Tempus::IntegratorBasic<double>());
33  TEST_ASSERT(integrator->isInitialized() == false);
34 
35  TEST_COMPARE(integrator->getIntegratorName(), ==, "Integrator Basic");
36  TEST_COMPARE(integrator->getIntegratorType(), ==, "Integrator Basic");
37  TEST_COMPARE(integrator->getStepper()->getStepperName(), ==, "Forward Euler");
38  TEST_ASSERT(integrator->getStepper()->getModel() == Teuchos::null);
39  TEST_ASSERT(integrator->getSolutionHistory() != Teuchos::null);
40  TEST_COMPARE(integrator->getSolutionHistory()->getNumStates(), ==, 0);
41  TEST_ASSERT(integrator->getTimeStepControl() != Teuchos::null);
42  TEST_ASSERT(integrator->getTimeStepControl()->getStepType() == "Constant");
43  TEST_ASSERT(integrator->getObserver() != Teuchos::null);
44 
45  // Setup ModelEvaluator -------------------------------------
46  auto model = rcp(new Tempus_Test::SinCosModel<double>());
47  integrator->setModel(model);
48 
49  // Setup SolutionHistory ------------------------------------
50  auto inArgsIC = model->getNominalValues();
51  auto icSolution =
52  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
53  auto icState = Tempus::createSolutionStateX(icSolution);
54  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
55  solutionHistory->addState(icState);
56 
57  integrator->setSolutionHistory(solutionHistory);
58  integrator->initialize();
59 
60  TEST_ASSERT(integrator->isInitialized() == true);
61 }
62 
63 // ************************************************************
64 // ************************************************************
65 TEUCHOS_UNIT_TEST(IntegratorBasic, Full_Construction)
66 {
67  auto stepper = rcp(new Tempus::StepperBackwardEuler<double>());
68  auto model = rcp(new Tempus_Test::SinCosModel<double>());
69  stepper->setModel(model);
70 
71  // Setup SolutionHistory ------------------------------------
72  auto inArgsIC = model->getNominalValues();
73  auto icSolution =
74  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
75  auto icState = Tempus::createSolutionStateX(icSolution);
76  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
77  solutionHistory->addState(icState);
78 
79  // Setup TimeStepControl ------------------------------------
80  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
81 
82  // Setup IntegratorObserver ---------------------------------
83  auto integratorObserver = rcp(new Tempus::IntegratorObserverBasic<double>());
84 
85  std::vector<int> outputScreenIndices{10, 20, 30};
86  int outputScreenInterval = 72;
87 
88  // Full argument list construction.
89  auto integrator = Teuchos::rcp(new Tempus::IntegratorBasic<double>(
90  stepper, solutionHistory, timeStepControl, integratorObserver,
91  outputScreenIndices, outputScreenInterval));
92 
93  TEST_ASSERT(integrator->isInitialized() == true);
94 
95  TEST_COMPARE(integrator->getIntegratorName(), ==, "Integrator Basic");
96  TEST_COMPARE(integrator->getIntegratorType(), ==, "Integrator Basic");
97  TEST_COMPARE(integrator->getStepper()->getStepperName(), ==,
98  "Backward Euler");
99  TEST_ASSERT(integrator->getStepper()->getModel() != Teuchos::null);
100  TEST_ASSERT(integrator->getSolutionHistory() != Teuchos::null);
101  TEST_COMPARE(integrator->getSolutionHistory()->getNumStates(), ==, 1);
102  TEST_ASSERT(integrator->getTimeStepControl() != Teuchos::null);
103  TEST_ASSERT(integrator->getTimeStepControl()->getStepType() == "Constant");
104  TEST_ASSERT(integrator->getObserver() != Teuchos::null);
105  TEST_ASSERT(integrator->getScreenOutputIndexList() == outputScreenIndices);
106  TEST_ASSERT(integrator->getScreenOutputIndexInterval() ==
107  outputScreenInterval);
108 }
109 
110 // ************************************************************
111 // ************************************************************
112 TEUCHOS_UNIT_TEST(IntegratorBasic, Describe)
113 {
114  // 1) Setup the ParameterList (here we start with params from .xml file)
115  RCP<ParameterList> pl =
116  Teuchos::getParametersFromXmlFile("Tempus_IntegratorBasic_default.xml");
117 
118  // 2) Setup the ModelEvaluator
120 
121  // 3) Setup the Integrator
122  RCP<ParameterList> tempusPL = sublist(pl, "Tempus", true);
124  Tempus::createIntegratorBasic<double>(tempusPL, model);
125 
126  std::ostringstream ss;
128  Teuchos::fancyOStream(Teuchos::rcpFromRef(ss));
129 
130  integrator->describe(*myOut, Teuchos::VERB_EXTREME);
131 
132  auto testS = ss.str();
133 
134  // Find major headers.
135  auto npos = std::string::npos;
136  TEST_ASSERT(npos != testS.find("--- Tempus::IntegratorBasic ---"));
137  TEST_ASSERT(npos != testS.find("--- Tempus::SolutionHistory"));
138  TEST_ASSERT(npos != testS.find("--- SolutionState (index = 0; time = "
139  " 0; dt = 1e+99) ---"));
140  TEST_ASSERT(npos != testS.find("--- Tempus::SolutionStateMetaData ---"));
141  TEST_ASSERT(npos != testS.find("--- Tempus::StepperState"));
142  TEST_ASSERT(npos != testS.find("--- Tempus::PhysicsState"));
143  TEST_ASSERT(npos != testS.find("--- Tempus::TimeStepControl ---"));
144  TEST_ASSERT(npos !=
145  testS.find("--- Tempus::TimeStepControlStrategyConstant ---"));
146  TEST_ASSERT(npos != testS.find("--- Stepper ---"));
147  TEST_ASSERT(npos != testS.find("stepperType_ = Forward Euler"));
148  TEST_ASSERT(npos != testS.find("--- StepperExplicit ---"));
149 
150  integrator->setStatus(Tempus::Status::FAILED);
151  TEST_ASSERT(integrator->getStatus() == Tempus::Status::FAILED);
152  integrator->setStatus(Tempus::Status::WORKING);
153  TEST_ASSERT(integrator->getStatus() == Tempus::Status::WORKING);
154  integrator->setStatus(Tempus::Status::PASSED);
155  TEST_ASSERT(integrator->getStatus() == Tempus::Status::PASSED);
156 }
157 
158 // ************************************************************
159 // ************************************************************
160 TEUCHOS_UNIT_TEST(IntegratorBasic, checkTimeStep)
161 {
163  auto integrator = Tempus::createIntegratorBasic<double>(
164  model, std::string("Backward Euler"));
165 
166  // Ensure initial status is working and unchanged by checkTimeStep.
167  integrator->getNonConstSolutionHistory()->initWorkingState();
168  TEST_ASSERT(integrator->getStatus() == Tempus::Status::WORKING);
169 
170  auto tsc = integrator->getNonConstTimeStepControl();
171  auto ws = integrator->getSolutionHistory()->getWorkingState();
172 
173  // integrator->checkTimeStep();
174 
175  // Test "Too many TimeStep failures"
176  ws->setNFailures(11);
177  integrator->checkTimeStep();
178  TEST_ASSERT(integrator->getStatus() == Tempus::Status::FAILED);
179  // Reset test.
180  ws->setNFailures(0);
181  integrator->setStatus(Tempus::Status::WORKING);
182 
183  // Test "Too many consecutive TimeStep failures"
184  ws->setNConsecutiveFailures(6);
185  integrator->checkTimeStep();
186  TEST_ASSERT(integrator->getStatus() == Tempus::Status::FAILED);
187  // Reset test.
188  ws->setNConsecutiveFailures(0);
189  integrator->setStatus(Tempus::Status::WORKING);
190 
191  // Test "Timestep size is at the minimum timestep size and the step failed."
192  ws->setTimeStep(1.0);
193  ws->setSolutionStatus(Tempus::Status::FAILED);
194  tsc->setMinTimeStep(1.0);
195  integrator->checkTimeStep();
196  TEST_ASSERT(integrator->getStatus() == Tempus::Status::FAILED);
197  // Reset test.
198  ws->setSolutionStatus(Tempus::Status::PASSED);
199  tsc->setMinTimeStep(0.1);
200  integrator->setStatus(Tempus::Status::WORKING);
201 
202  // Test "Stepper failure."
203  ws->setSolutionStatus(Tempus::Status::FAILED);
204  integrator->checkTimeStep();
205  TEST_ASSERT(ws->getNFailures() == 1);
206  TEST_ASSERT(ws->getNRunningFailures() == 1);
207  TEST_ASSERT(ws->getNConsecutiveFailures() == 1);
208  TEST_ASSERT(ws->getSolutionStatus() == Tempus::Status::FAILED);
209  // Reset test.
210  ws->setNFailures(0);
211  ws->setNRunningFailures(0);
212  ws->setNConsecutiveFailures(0);
213  ws->setSolutionStatus(Tempus::Status::PASSED);
214 
215  // Test "Constant time step failure."
217  tsc->setTimeStepControlStrategy(tscs);
218  ws->setTimeStep(0.1);
219  tsc->setInitTimeStep(1.0);
220  integrator->checkTimeStep();
221  TEST_ASSERT(ws->getNFailures() == 1);
222  TEST_ASSERT(ws->getNRunningFailures() == 1);
223  TEST_ASSERT(ws->getNConsecutiveFailures() == 1);
224  TEST_ASSERT(ws->getSolutionStatus() == Tempus::Status::FAILED);
225  // Not resetting test as it is the last test.
226 }
227 
228 // ************************************************************
229 // ************************************************************
230 // Test Integrator creation from ParameterList and ModelEvaluator.
231 TEUCHOS_UNIT_TEST(IntegratorBasic, PL_ME_Creation)
232 {
233  // 1) Setup default Integrator
235  Tempus::createIntegratorBasic<double>();
236 
237  // 2) Setup the ParameterList
238  // - Start with the default Tempus PL
239  // - Add Stepper PL
240  RCP<ParameterList> tempusPL =
241  Teuchos::rcp_const_cast<ParameterList>(integrator->getValidParameters());
242 
243  tempusPL->sublist("Default Integrator").set("Stepper Name", "Demo Stepper");
244  RCP<ParameterList> stepperPL = Teuchos::parameterList();
245  stepperPL->set("Stepper Type", "Forward Euler");
246  tempusPL->set("Demo Stepper", *stepperPL);
247 
248  // 3) Create integrator from non-member function
250  integrator = Tempus::createIntegratorBasic<double>(tempusPL, model);
251 
252  // Test the ParameterList
253  auto testPL = integrator->getValidParameters();
254  // Write out ParameterList to rebaseline test.
255  // writeParameterListToXmlFile(*testPL,"Tempus_IntegratorBasic_ref2-test.xml");
256 
257  // Read params from reference .xml file
258  RCP<ParameterList> referencePL =
259  Teuchos::getParametersFromXmlFile("Tempus_IntegratorBasic_ref.xml");
260 
261  bool pass = haveSameValuesSorted(*testPL, *referencePL, true);
262  if (!pass) {
263  out << std::endl;
264  out << "testPL -------------- \n"
265  << *testPL << std::endl;
266  out << "referencePL -------------- \n"
267  << *referencePL << std::endl;
268  }
269  TEST_ASSERT(pass)
270 }
271 
272 } // namespace Tempus_Unit_Test
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateX(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdot=Teuchos::null, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdotdot=Teuchos::null)
Nonmember constructor from non-const solution vectors, x.
#define TEST_COMPARE(v1, comp, v2)
IntegratorObserverBasic class for time integrators. This basic class has simple no-op functions...
#define TEST_ASSERT(v1)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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...
StepControlStrategy class for TimeStepControl.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")