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