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: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
11 
12 #include "Tempus_StepperBackwardEuler.hpp"
14 
15 namespace Tempus_Unit_Test {
16 
17 using Teuchos::RCP;
18 using Teuchos::rcp;
19 using Teuchos::rcp_const_cast;
20 using Teuchos::rcp_dynamic_cast;
22 using Teuchos::sublist;
23 
25 
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 =rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
52  auto icState = Tempus::createSolutionStateX(icSolution);
53  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
54  solutionHistory->addState(icState);
55 
56  integrator->setSolutionHistory(solutionHistory);
57  integrator->initialize();
58 
59  TEST_ASSERT ( integrator->isInitialized() == true);
60 }
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 =rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
74  auto icState = Tempus::createSolutionStateX(icSolution);
75  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
76  solutionHistory->addState(icState);
77 
78  // Setup TimeStepControl ------------------------------------
79  auto timeStepControl = rcp(new Tempus::TimeStepControl<double>());
80 
81  // Setup IntegratorObserver ---------------------------------
82  auto integratorObserver = rcp(new Tempus::IntegratorObserverBasic<double>());
83 
84  std::vector<int> outputScreenIndices{ 10, 20, 30 };
85  int outputScreenInterval = 72;
86 
87  // Full argument list construction.
88  auto integrator = Teuchos::rcp(
90  solutionHistory,
91  timeStepControl,
92  integratorObserver,
93  outputScreenIndices,
94  outputScreenInterval ));
95 
96  TEST_ASSERT ( integrator->isInitialized() == true);
97 
98  TEST_COMPARE ( integrator->getIntegratorName(), ==, "Integrator Basic");
99  TEST_COMPARE ( integrator->getIntegratorType(), ==, "Integrator Basic");
100  TEST_COMPARE ( integrator->getStepper()->getStepperName(), ==, "Backward Euler");
101  TEST_ASSERT( integrator->getStepper()->getModel() != Teuchos::null);
102  TEST_ASSERT( integrator->getSolutionHistory() != Teuchos::null);
103  TEST_COMPARE ( integrator->getSolutionHistory()->getNumStates(), ==, 1);
104  TEST_ASSERT( integrator->getTimeStepControl() != Teuchos::null);
105  TEST_ASSERT( integrator->getTimeStepControl()->getStepType() == "Constant");
106  TEST_ASSERT( integrator->getObserver() != Teuchos::null);
107  TEST_ASSERT( integrator->getScreenOutputIndexList() == outputScreenIndices);
108  TEST_ASSERT( integrator->getScreenOutputIndexInterval() == outputScreenInterval);
109 
110 }
111 
112 
113 // ************************************************************
114 // ************************************************************
115 TEUCHOS_UNIT_TEST(IntegratorBasic, Describe)
116 {
117  // 1) Setup the ParameterList (here we start with params from .xml file)
118  RCP<ParameterList> pl = Teuchos::getParametersFromXmlFile("Tempus_IntegratorBasic_default.xml");
119 
120  // 2) Setup the ModelEvaluator
121  auto model = Teuchos::rcp(new Tempus_Test::SinCosModel<double> ());
122 
123  // 3) Setup the Integrator
124  RCP<ParameterList> tempusPL = sublist(pl, "Tempus", true);
126  Tempus::createIntegratorBasic<double>(tempusPL, model);
127 
128  std::ostringstream ss;
130  Teuchos::fancyOStream(Teuchos::rcpFromRef(ss));
131 
132  integrator->describe(*myOut, Teuchos::VERB_EXTREME);
133 
134  auto testS = ss.str();
135 
136  // Find major headers.
137  auto npos = std::string::npos;
138  TEST_ASSERT(npos != testS.find("--- Tempus::IntegratorBasic ---"));
139  TEST_ASSERT(npos != testS.find("--- Tempus::SolutionHistory"));
140  TEST_ASSERT(npos != testS.find("--- SolutionState (index = 0; time = 0; dt = 1e+99) ---"));
141  TEST_ASSERT(npos != testS.find("--- Tempus::SolutionStateMetaData ---"));
142  TEST_ASSERT(npos != testS.find("--- Tempus::StepperState"));
143  TEST_ASSERT(npos != testS.find("--- Tempus::PhysicsState"));
144  TEST_ASSERT(npos != testS.find("--- Tempus::TimeStepControl ---"));
145  TEST_ASSERT(npos != 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 // ************************************************************
161 TEUCHOS_UNIT_TEST(IntegratorBasic, checkTimeStep)
162 {
163  auto model = Teuchos::rcp(new Tempus_Test::SinCosModel<double> ());
164  auto integrator = Tempus::createIntegratorBasic<double>(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 // ************************************************************
231 // ************************************************************
232 // Test Integrator creation from ParameterList and ModelEvaluator.
233 TEUCHOS_UNIT_TEST(IntegratorBasic, PL_ME_Creation)
234 {
235  // 1) Setup default Integrator
237  Tempus::createIntegratorBasic<double>();
238 
239  // 2) Setup the ParameterList
240  // - Start with the default Tempus PL
241  // - Add Stepper PL
242  RCP<ParameterList> tempusPL = Teuchos::rcp_const_cast<ParameterList>(
243  integrator->getValidParameters());
244 
245  tempusPL->sublist("Default Integrator").set("Stepper Name", "Demo Stepper");
246  RCP<ParameterList> stepperPL = Teuchos::parameterList();
247  stepperPL->set("Stepper Type", "Forward Euler");
248  tempusPL->set("Demo Stepper", *stepperPL);
249 
250  // 3) Create integrator from non-member function
251  auto model = Teuchos::rcp(new Tempus_Test::SinCosModel<double> ());
252  integrator = Tempus::createIntegratorBasic<double>(tempusPL, model);
253 
254  // Test the ParameterList
255  auto testPL = integrator->getValidParameters();
256  // Write out ParameterList to rebaseline test.
257  //writeParameterListToXmlFile(*testPL,"Tempus_IntegratorBasic_ref2-test.xml");
258 
259  // Read params from reference .xml file
260  RCP<ParameterList> referencePL =
261  Teuchos::getParametersFromXmlFile("Tempus_IntegratorBasic_ref.xml");
262 
263  bool pass = haveSameValuesSorted(*testPL, *referencePL, true);
264  if (!pass) {
265  out << std::endl;
266  out << "testPL -------------- \n" << *testPL << std::endl;
267  out << "referencePL -------------- \n" << *referencePL << std::endl;
268  }
269  TEST_ASSERT(pass)
270 }
271 
272 
273 } // namespace Tempus_Unit_Test
274 
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.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#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...
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="")