Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeEventListIndex.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 "Tempus_TimeEventListIndex.hpp"
15 
16 #include "../TestUtils/Tempus_ConvergenceTestUtils.hpp"
17 
18 
19 namespace Tempus_Unit_Test {
20 
21 using Teuchos::RCP;
22 using Teuchos::rcp;
23 using Teuchos::rcp_const_cast;
24 using Teuchos::rcp_dynamic_cast;
25 
26 
27 // ************************************************************
28 // ************************************************************
29 TEUCHOS_UNIT_TEST(TimeEventListIndex, Default_Construction)
30 {
31  auto te = rcp(new Tempus::TimeEventListIndex<double>());
32 
33  TEST_COMPARE(te->getName(), ==, "TimeEventListIndex");
34 
35  TEST_COMPARE(te->getIndexList().size(), ==, 0);
36 
37  TEST_COMPARE(te->isIndex(-1), ==, false);
38  TEST_COMPARE(te->isIndex(te->getDefaultIndex()), ==, true );
39  TEST_COMPARE(te->isIndex( 1), ==, false);
40 
41  TEST_COMPARE(te->indexToNextEvent(0), ==, te->getDefaultIndex());
42  TEST_COMPARE(te->indexOfNextEvent(0), ==, te->getDefaultIndex());
43  TEST_COMPARE(te->eventInRangeIndex(2, -1), ==, false);
44 
45  // Check base class defaults (functions not implemented in TimeEventListIndex).
46  TEST_COMPARE(te->isTime(1.0), ==, false);
47  TEST_COMPARE(te->timeToNextEvent(1.0), ==, te->getDefaultTime());
48  TEST_COMPARE(te->timeOfNextEvent(1.0), ==, te->getDefaultTime());
49  TEST_COMPARE(te->eventInRange(1.0, 4.0), ==, false);
50 }
51 
52 
53 // ************************************************************
54 // ************************************************************
55 TEUCHOS_UNIT_TEST(TimeEventListIndex, Construction)
56 {
57  std::vector<int> testVector;
58  testVector.push_back(-2);
59  testVector.push_back( 0);
60  testVector.push_back( 7);
61  testVector.push_back( 3);
62  testVector.push_back(-5);
63 
65  "TestName", testVector));
66 
67  TEST_COMPARE(te->getName(), ==, "TestName");
68 
69  auto testList = te->getIndexList();
70  TEST_COMPARE(testList.size(), ==, 5);
71  TEST_COMPARE(testList[0], ==, -5);
72  TEST_COMPARE(testList[1], ==, -2);
73  TEST_COMPARE(testList[2], ==, 0);
74  TEST_COMPARE(testList[3], ==, 3);
75  TEST_COMPARE(testList[4], ==, 7);
76 
77  // Test adding a duplicate event index.
78  te->addIndex(3);
79  TEST_COMPARE(te->getIndexList().size(), ==, 5);
80  te->addIndex(1);
81  TEST_COMPARE(te->getIndexList().size(), ==, 6);
82 }
83 
84 
85 // ************************************************************
86 // ************************************************************
87 TEUCHOS_UNIT_TEST(TimeEventListIndex, isIndex)
88 {
89  auto te = rcp(new Tempus::TimeEventListIndex<double>());
90 
91  te->setName("TestName");
92  TEST_COMPARE(te->getName(), ==, "TestName");
93 
94  // Test isIndex with one element.
95  te->addIndex(-5);
96  TEST_COMPARE(te->isIndex(-6), ==, false);
97  TEST_COMPARE(te->isIndex(-5), ==, true );
98  TEST_COMPARE(te->isIndex(-4), ==, false);
99 
100  // Test isIndex with two elements.
101  te->addIndex(1);
102  TEST_COMPARE(te->isIndex(0), ==, false);
103  TEST_COMPARE(te->isIndex(1), ==, true );
104  TEST_COMPARE(te->isIndex(2), ==, false);
105 
106  // Test addIndex.
107  te->addIndex(-2);
108  te->addIndex( 4);
109  te->addIndex(-9);
110  auto testList = te->getIndexList();
111  TEST_COMPARE(testList.size(), ==, 5);
112  TEST_COMPARE(testList[0], ==, -9);
113  TEST_COMPARE(testList[1], ==, -5);
114  TEST_COMPARE(testList[2], ==, -2);
115  TEST_COMPARE(testList[3], ==, 1);
116  TEST_COMPARE(testList[4], ==, 4);
117 }
118 
119 
120 // ************************************************************
121 // ************************************************************
122 TEUCHOS_UNIT_TEST(TimeEventListIndex, indexToNextEvent)
123 {
124  std::vector<int> testListIndex;
125  testListIndex.push_back(-5);
126  testListIndex.push_back( 1);
127  testListIndex.push_back(-2);
128  testListIndex.push_back( 4);
129  testListIndex.push_back(-9);
130 
132  "teListIndex", testListIndex));
133 
134  // Test indexToNextEvent.
135  // Around first event.
136  TEST_COMPARE(te->indexToNextEvent(-12), ==, 3);
137  TEST_COMPARE(te->indexToNextEvent( -9), ==, 0);
138  TEST_COMPARE(te->indexToNextEvent( -8), ==, 3);
139 
140  // Around mid event.
141  TEST_COMPARE(te->indexToNextEvent(-4), ==, 2);
142  TEST_COMPARE(te->indexToNextEvent(-2), ==, 0);
143  TEST_COMPARE(te->indexToNextEvent( 0), ==, 1);
144 
145  // Around last event.
146  TEST_COMPARE(te->indexToNextEvent(2), ==, 2);
147  TEST_COMPARE(te->indexToNextEvent(4), ==, 0);
148  TEST_COMPARE(te->indexToNextEvent(9), ==, -5);
149 }
150 
151 
152 // ************************************************************
153 // ************************************************************
154 TEUCHOS_UNIT_TEST(TimeEventListIndex, indexOfNextEvent)
155 {
156  std::vector<int> testListIndex;
157  testListIndex.push_back(-5);
158  testListIndex.push_back( 1);
159  testListIndex.push_back(-2);
160  testListIndex.push_back( 4);
161  testListIndex.push_back(-9);
162 
164  "teListIndex", testListIndex));
165 
166  // Test indexOfNextEvent.
167  // Around first event.
168  TEST_COMPARE(te->indexOfNextEvent(-12), ==, -9);
169  TEST_COMPARE(te->indexOfNextEvent( -9), ==, -9);
170  TEST_COMPARE(te->indexOfNextEvent( -8), ==, -5);
171 
172  // Around mid event.
173  TEST_COMPARE(te->indexOfNextEvent(-4), ==, -2);
174  TEST_COMPARE(te->indexOfNextEvent(-2), ==, -2);
175  TEST_COMPARE(te->indexOfNextEvent( 0), ==, 1);
176 
177  // Around last event.
178  TEST_COMPARE(te->indexOfNextEvent(2), ==, 4);
179  TEST_COMPARE(te->indexOfNextEvent(4), ==, 4);
180  TEST_COMPARE(te->indexOfNextEvent(9), ==, 4);
181 }
182 
183 
184 // ************************************************************
185 // ************************************************************
186 TEUCHOS_UNIT_TEST(TimeEventListIndex, eventInRangeIndex)
187 {
188  std::vector<int> testListIndex;
189  testListIndex.push_back(-5);
190  testListIndex.push_back( 1);
191  testListIndex.push_back(-2);
192  testListIndex.push_back( 4);
193  testListIndex.push_back(-9);
194 
196  "teListIndex", testListIndex));
197 
198  // Test eventInRangeIndex.
199  // Right end.
200  TEST_COMPARE(te->eventInRangeIndex(-12.0, -10), ==, false); // Around first event.
201  TEST_COMPARE(te->eventInRangeIndex(-12.0, -9), ==, true );
202  TEST_COMPARE(te->eventInRangeIndex(-12.0, -8), ==, true );
203 
204  TEST_COMPARE(te->eventInRangeIndex(-4, -3), ==, false); // Around mid event.
205  TEST_COMPARE(te->eventInRangeIndex(-4, -2), ==, true );
206  TEST_COMPARE(te->eventInRangeIndex(-4, -1), ==, true );
207 
208  TEST_COMPARE(te->eventInRangeIndex(3, 3), ==, false); // Around last event.
209  TEST_COMPARE(te->eventInRangeIndex(3, 4), ==, true );
210  TEST_COMPARE(te->eventInRangeIndex(3, 6), ==, true );
211 
212  // Left end.
213  TEST_COMPARE(te->eventInRangeIndex(-12, -7), ==, true ); // Around first event.
214  TEST_COMPARE(te->eventInRangeIndex( -9, -7), ==, true );
215  TEST_COMPARE(te->eventInRangeIndex( -8, -7), ==, false);
216 
217  TEST_COMPARE(te->eventInRangeIndex(-3, 0), ==, true ); // Around mid event.
218  TEST_COMPARE(te->eventInRangeIndex(-2, 0), ==, true );
219  TEST_COMPARE(te->eventInRangeIndex(-1, 0), ==, false);
220 
221  TEST_COMPARE(te->eventInRangeIndex(3, 8), ==, true ); // Around last event.
222  TEST_COMPARE(te->eventInRangeIndex(4, 8), ==, true );
223  TEST_COMPARE(te->eventInRangeIndex(5, 8), ==, false);
224 }
225 
226 
227 } // namespace Tempus_Test
TimeEventListIndex specifies a list of index events.
#define TEST_COMPARE(v1, comp, v2)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)