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