Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeStepControlStrategyBasicVS.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_StepperFactory.hpp"
12 #include "Tempus_TimeStepControl.hpp"
14 
15 #include "../TestModels/DahlquistTestModel.hpp"
16 
17 namespace Tempus_Unit_Test {
18 
20 using Teuchos::RCP;
21 using Teuchos::rcp;
22 using Teuchos::rcp_const_cast;
23 using Teuchos::rcp_dynamic_cast;
24 using Teuchos::sublist;
25 
26 // ************************************************************
27 // ************************************************************
28 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, Default_Construction)
29 {
31  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
32 
33  // Test the get functions (i.e., defaults).
34  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Basic VS");
35  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
36  TEUCHOS_TEST_FOR_EXCEPT(tscs->getAmplFactor() != 1.75);
37  TEUCHOS_TEST_FOR_EXCEPT(tscs->getReductFactor() != 0.5);
38  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMinEta() != 0.0);
39  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMaxEta() != 1.0e+16);
40 
41  // Test the set functions.
42  tscs->setAmplFactor(1.33);
43  tscs->initialize();
44  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
45  tscs->setReductFactor(0.75);
46  tscs->initialize();
47  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
48  tscs->setMinEta(0.01);
49  tscs->initialize();
50  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
51  tscs->setMaxEta(0.05);
52  tscs->initialize();
53  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
54 
55  TEUCHOS_TEST_FOR_EXCEPT(tscs->getAmplFactor() != 1.33);
56  TEUCHOS_TEST_FOR_EXCEPT(tscs->getReductFactor() != 0.75);
57  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMinEta() != 0.01);
58  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMaxEta() != 0.05);
59 }
60 
61 // ************************************************************
62 // ************************************************************
63 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, Full_Construction)
64 {
66  1.33, 0.75, 0.01, 0.05));
67  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
68 
69  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Basic VS");
70  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
71  TEUCHOS_TEST_FOR_EXCEPT(tscs->getAmplFactor() != 1.33);
72  TEUCHOS_TEST_FOR_EXCEPT(tscs->getReductFactor() != 0.75);
73  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMinEta() != 0.01);
74  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMaxEta() != 0.05);
75 }
76 
77 // ************************************************************
78 // ************************************************************
79 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, Create_Construction)
80 {
81  auto pl = Tempus::getTimeStepControlStrategyBasicVS_PL<double>();
82 
83  pl->set<double>("Amplification Factor", 1.33);
84  pl->set<double>("Reduction Factor", 0.75);
85  pl->set<double>("Minimum Value Monitoring Function", 0.01);
86  pl->set<double>("Maximum Value Monitoring Function", 0.05);
87 
88  auto tscs = Tempus::createTimeStepControlStrategyBasicVS<double>(pl);
89 
90  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Basic VS");
91  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Variable");
92  TEUCHOS_TEST_FOR_EXCEPT(tscs->getAmplFactor() != 1.33);
93  TEUCHOS_TEST_FOR_EXCEPT(tscs->getReductFactor() != 0.75);
94  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMinEta() != 0.01);
95  TEUCHOS_TEST_FOR_EXCEPT(tscs->getMaxEta() != 0.05);
96 }
97 
98 // ************************************************************
99 // ************************************************************
100 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, setNextTimeStep)
101 {
103  tscs->setAmplFactor(1.1);
104  tscs->setReductFactor(0.5);
105  tscs->setMinEta(0.01);
106  tscs->setMaxEta(0.05);
107  tscs->initialize();
108  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
109 
110  // Setup the TimeStepControl --------------------------------
111  auto tsc = rcp(new Tempus::TimeStepControl<double>());
112  tsc->setTimeStepControlStrategy(tscs);
113  tsc->setInitTime(0.0);
114  tsc->setFinalTime(10.0);
115  tsc->setMinTimeStep(0.01);
116  tsc->setInitTimeStep(0.1);
117  tsc->setMaxTimeStep(1.0);
118  tsc->setFinalIndex(100);
119  tsc->initialize();
120  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
122 
123  // Setup the SolutionHistory --------------------------------
124  auto model = rcp(new Tempus_Test::DahlquistTestModel<double>());
125  auto inArgsIC = model->getNominalValues();
126  auto icSolution =
127  rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
128  auto icState = Tempus::createSolutionStateX<double>(icSolution);
129  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
130 
131  // Test Reducing timestep
132  {
133  solutionHistory->addState(icState);
134  solutionHistory->getCurrentState()->setTimeStep(0.5);
135  solutionHistory->getCurrentState()->setTime(0.0);
136  solutionHistory->getCurrentState()->setIndex(0);
137 
138  // Set up solution history with two time steps.
139  for (int i = 0; i < 2; i++) {
140  solutionHistory->initWorkingState();
141 
142  tsc->setNextTimeStep(solutionHistory, status);
143 
144  { // Mock takeStep
145  auto currentState = solutionHistory->getCurrentState();
146  auto workingState = solutionHistory->getWorkingState();
147  auto xN = workingState->getX();
148  Thyra::Vp_S(xN.ptr(), 1.0);
149  workingState->setSolutionStatus(Tempus::Status::PASSED);
150  workingState->computeNorms(currentState);
151  }
152 
153  solutionHistory->promoteWorkingState();
154  // out << " x = " << get_ele(*(x), 0) << std::endl;
155  }
156 
157  auto currentState = solutionHistory->getCurrentState();
158  TEST_FLOATING_EQUALITY(currentState->getTimeStep(), 0.25, 1.0e-14);
159  TEST_FLOATING_EQUALITY(currentState->getTime(), 0.75, 1.0e-14);
160  }
161 
162  // Test increasing timestep
163  {
164  solutionHistory->clear();
165  solutionHistory->addState(icState);
166  solutionHistory->getCurrentState()->setTimeStep(0.5);
167  solutionHistory->getCurrentState()->setTime(0.0);
168  solutionHistory->getCurrentState()->setIndex(0);
169 
170  // Set up solution history with two time steps.
171  for (int i = 0; i < 2; i++) {
172  solutionHistory->initWorkingState();
173 
174  tsc->setNextTimeStep(solutionHistory, status);
175 
176  { // Mock takeStep
177  auto currentState = solutionHistory->getCurrentState();
178  auto workingState = solutionHistory->getWorkingState();
179  auto xN = workingState->getX();
180  Thyra::Vp_S(xN.ptr(), 0.0);
181  workingState->setSolutionStatus(Tempus::Status::PASSED);
182  workingState->computeNorms(currentState);
183  }
184 
185  solutionHistory->promoteWorkingState();
186  // auto x = solutionHistory->getCurrentState()->getX();
187  // out << " x = " << get_ele(*(x), 0) << std::endl;
188  }
189 
190  auto currentState = solutionHistory->getCurrentState();
191  TEST_FLOATING_EQUALITY(currentState->getTimeStep(), 0.55, 1.0e-14);
192  TEST_FLOATING_EQUALITY(currentState->getTime(), 1.05, 1.0e-14);
193  }
194 }
195 
196 // ************************************************************
197 // ************************************************************
198 TEUCHOS_UNIT_TEST(TimeStepControlStrategyBasicVS, getValidParameters)
199 {
201 
202  auto pl = tscs->getValidParameters();
203 
204  TEST_COMPARE(pl->get<std::string>("Strategy Type"), ==, "Basic VS");
205  TEST_FLOATING_EQUALITY(pl->get<double>("Amplification Factor"), 1.75,
206  1.0e-14);
207  TEST_FLOATING_EQUALITY(pl->get<double>("Reduction Factor"), 0.5, 1.0e-14);
208  TEST_FLOATING_EQUALITY(pl->get<double>("Minimum Value Monitoring Function"),
209  0.0, 1.0e-14);
210  TEST_FLOATING_EQUALITY(pl->get<double>("Maximum Value Monitoring Function"),
211  1.0e+16, 1.0e-14);
212 
213  { // Ensure that parameters are "used", excluding sublists.
214  std::ostringstream unusedParameters;
215  pl->unused(unusedParameters);
216  TEST_COMPARE(unusedParameters.str(), ==, "");
217  }
218 }
219 
220 } // namespace Tempus_Unit_Test
StepControlStrategy class for TimeStepControl.
The classic Dahlquist Test Problem.
#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)
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...
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)