Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeStepControlStrategyConstant.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 
10 
11 #include "Tempus_TimeStepControl.hpp"
14 
15 namespace Tempus_Unit_Test {
16 
18 using Teuchos::RCP;
19 using Teuchos::rcp;
20 using Teuchos::rcp_const_cast;
21 using Teuchos::rcp_dynamic_cast;
22 using Teuchos::sublist;
23 
24 // ************************************************************
25 // ************************************************************
26 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, Default_Construction)
27 {
29  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
30 
31  // Test the get functions (i.e., defaults).
32  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Constant");
33  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Constant");
34  TEUCHOS_TEST_FOR_EXCEPT(tscs->getName() != "Constant");
35  TEST_FLOATING_EQUALITY(tscs->getConstantTimeStep(), 0.0, 1.0e-14);
36 
37  // Test the set functions.
38  tscs->setConstantTimeStep(0.989);
39  tscs->initialize();
40  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
41 
42  TEST_FLOATING_EQUALITY(tscs->getConstantTimeStep(), 0.989, 1.0e-14);
43 }
44 
45 // ************************************************************
46 // ************************************************************
47 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, Full_Construction)
48 {
50  0.123, "Full_Construction_Test"));
51  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
52 
53  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Constant");
54  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Constant");
55  TEUCHOS_TEST_FOR_EXCEPT(tscs->getName() != "Full_Construction_Test");
56  TEST_FLOATING_EQUALITY(tscs->getConstantTimeStep(), 0.123, 1.0e-14);
57 }
58 
59 // ************************************************************
60 // ************************************************************
61 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, Create_Construction)
62 {
63  auto pl = Tempus::getTimeStepControlStrategyConstantPL<double>();
64 
65  // Set strategy parameters.
66  pl->set<double>("Time Step", 0.02);
67 
68  auto tscsc = Tempus::createTimeStepControlStrategyConstant<double>(pl);
69  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
70 
71  TEST_FLOATING_EQUALITY(tscsc->getConstantTimeStep(), 0.02, 1.0e-14);
72 }
73 
74 // ************************************************************
75 // ************************************************************
76 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, setNextTimeStep)
77 {
79  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
80 
81  double initTime = 1.0;
82  int initIndex = -100;
83 
84  // Setup the SolutionHistory --------------------------------
85  auto model = rcp(new Tempus_Test::SinCosModel<double>());
86  auto inArgsIC = model->getNominalValues();
87  auto icSolution =
88  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
89  auto icState = Tempus::createSolutionStateX<double>(icSolution);
90  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
91  solutionHistory->addState(icState);
92  solutionHistory->getCurrentState()->setTimeStep(0.9);
93  solutionHistory->getCurrentState()->setTime(initTime);
94  solutionHistory->getCurrentState()->setIndex(initIndex);
95 
96  // Setup the TimeStepControl --------------------------------
97  auto tsc = rcp(new Tempus::TimeStepControl<double>());
98  tsc->setTimeStepControlStrategy(tscs);
99  tsc->setInitTime(initTime);
100  tsc->setFinalTime(100.0);
101  tsc->setMinTimeStep(0.01);
102  tsc->setInitTimeStep(0.02);
103  tsc->setMaxTimeStep(0.05);
104  tsc->setInitIndex(initIndex);
105  tsc->setFinalIndex(100);
106  tsc->initialize();
107  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
109 
110  // ** Mock Timestep ** //
111  solutionHistory->initWorkingState();
112 
113  tsc->setNextTimeStep(solutionHistory, status);
114  // ** Mock Timestep ** //
115 
116  auto workingState = solutionHistory->getWorkingState();
117  TEST_FLOATING_EQUALITY(workingState->getTimeStep(), 0.02, 1.0e-14);
118  TEST_FLOATING_EQUALITY(workingState->getTime(), 1.02, 1.0e-14);
119 }
120 
121 // ************************************************************
122 // ************************************************************
123 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, getValidParameters)
124 {
126 
127  auto pl = tscsc->getValidParameters();
128 
129  TEST_COMPARE(pl->get<std::string>("Strategy Type"), ==, "Constant");
130  TEST_FLOATING_EQUALITY(pl->get<double>("Time Step"), 0.0, 1.0e-14);
131 
132  { // Ensure that parameters are "used", excluding sublists.
133  std::ostringstream unusedParameters;
134  pl->unused(unusedParameters);
135  TEST_COMPARE(unusedParameters.str(), ==, "");
136  }
137 }
138 
139 } // namespace Tempus_Unit_Test
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
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)
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...
StepControlStrategy class for TimeStepControl.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)