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 
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"
20 
21 #include "../TestModels/SinCosModel.hpp"
22 #include "../TestModels/DahlquistTestModel.hpp"
23 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
24 
25 #include <fstream>
26 #include <vector>
27 
28 namespace Tempus_Unit_Test {
29 
30 using Teuchos::RCP;
31 using Teuchos::rcp;
32 using Teuchos::rcp_const_cast;
33 using Teuchos::rcp_dynamic_cast;
35 using Teuchos::sublist;
36 using Teuchos::getParametersFromXmlFile;
37 
38 
39 // ************************************************************
40 // ************************************************************
41 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, Default_Construction)
42 {
44  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
45 
46  // Test the get functions (i.e., defaults).
47  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Constant");
48  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Constant");
49  TEUCHOS_TEST_FOR_EXCEPT(tscs->getName() != "Constant");
50  TEST_FLOATING_EQUALITY (tscs->getConstantTimeStep(), 0.0, 1.0e-14);
51 
52  // Test the set functions.
53  tscs->setConstantTimeStep(0.989); tscs->initialize(); TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
54 
55  TEST_FLOATING_EQUALITY (tscs->getConstantTimeStep(), 0.989, 1.0e-14);
56 
57 }
58 
59 
60 // ************************************************************
61 // ************************************************************
62 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, Full_Construction)
63 {
65  0.123, "Full_Construction_Test"));
66  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
67 
68  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStrategyType() != "Constant");
69  TEUCHOS_TEST_FOR_EXCEPT(tscs->getStepType() != "Constant");
70  TEUCHOS_TEST_FOR_EXCEPT(tscs->getName() != "Full_Construction_Test");
71  TEST_FLOATING_EQUALITY (tscs->getConstantTimeStep(), 0.123, 1.0e-14);
72 }
73 
74 
75 // ************************************************************
76 // ************************************************************
77 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, Create_Construction)
78 {
79  auto pl = Tempus::getTimeStepControlStrategyConstantPL<double>();
80 
81  // Set strategy parameters.
82  pl->set<double>("Time Step", 0.02);
83 
84  auto tscsc = Tempus::createTimeStepControlStrategyConstant<double>(pl);
85  TEUCHOS_TEST_FOR_EXCEPT(!tscsc->isInitialized());
86 
87  TEST_FLOATING_EQUALITY (tscsc->getConstantTimeStep(), 0.02, 1.0e-14);
88 }
89 
90 
91 // ************************************************************
92 // ************************************************************
93 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, setNextTimeStep)
94 {
96  TEUCHOS_TEST_FOR_EXCEPT(!tscs->isInitialized());
97 
98  double initTime = 1.0;
99  int initIndex = -100;
100 
101  // Setup the SolutionHistory --------------------------------
102  auto model = rcp(new Tempus_Test::SinCosModel<double>());
103  auto inArgsIC = model->getNominalValues();
104  auto icSolution = rcp_const_cast<Thyra::VectorBase<double> >(inArgsIC.get_x());
105  auto icState = Tempus::createSolutionStateX<double>(icSolution);
106  auto solutionHistory = rcp(new Tempus::SolutionHistory<double>());
107  solutionHistory->addState(icState);
108  solutionHistory->getCurrentState()->setTimeStep(0.9);
109  solutionHistory->getCurrentState()->setTime(initTime);
110  solutionHistory->getCurrentState()->setIndex(initIndex);
111 
112  // Setup the TimeStepControl --------------------------------
113  auto tsc = rcp(new Tempus::TimeStepControl<double>());
114  tsc->setTimeStepControlStrategy(tscs);
115  tsc->setInitTime(initTime);
116  tsc->setFinalTime(100.0);
117  tsc->setMinTimeStep(0.01);
118  tsc->setInitTimeStep(0.02);
119  tsc->setMaxTimeStep(0.05);
120  tsc->setInitIndex(initIndex);
121  tsc->setFinalIndex(100);
122  tsc->initialize();
123  TEUCHOS_TEST_FOR_EXCEPT(!tsc->isInitialized());
125 
126  // ** Mock Timestep ** //
127  solutionHistory->initWorkingState();
128 
129  tsc->setNextTimeStep(solutionHistory, status);
130  // ** Mock Timestep ** //
131 
132  auto workingState = solutionHistory->getWorkingState();
133  TEST_FLOATING_EQUALITY( workingState->getTimeStep(), 0.02, 1.0e-14);
134  TEST_FLOATING_EQUALITY( workingState->getTime(), 1.02, 1.0e-14);
135 }
136 
137 
138 // ************************************************************
139 // ************************************************************
140 TEUCHOS_UNIT_TEST(TimeStepControlStrategyConstant, getValidParameters)
141 {
143 
144  auto pl = tscsc->getValidParameters();
145 
146  TEST_COMPARE ( pl->get<std::string>("Strategy Type"), ==,"Constant");
147  TEST_FLOATING_EQUALITY( pl->get<double>("Time Step"), 0.0, 1.0e-14);
148 
149  { // Ensure that parameters are "used", excluding sublists.
150  std::ostringstream unusedParameters;
151  pl->unused(unusedParameters);
152  TEST_COMPARE ( unusedParameters.str(), ==, "");
153  }
154 }
155 
156 
157 } // namespace Tempus_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)