Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_TimeEventRangeIndex.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_TimeEventRangeIndex.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(TimeEventRangeIndex, Default_Construction)
30 {
32 
33  TEST_COMPARE(te->getName(), ==, "TimeEventRangeIndex");
34 
35  // Test when everything is zero.
36  TEST_COMPARE(te->getIndexStart (), ==, 0);
37  TEST_COMPARE(te->getIndexStop (), ==, 0);
38  TEST_COMPARE(te->getIndexStride(), ==, 1);
39  TEST_COMPARE(te->getNumEvents (), ==, 1);
40 
41  // Check base class defaults (functions not implemented in TimeEventRangeIndex).
42  TEST_COMPARE(te->isTime(1.0), ==, false);
43  TEST_COMPARE(te->timeToNextEvent(1.0), ==, te->getDefaultTime());
44  TEST_COMPARE(te->timeOfNextEvent(1.0), ==, te->getDefaultTime());
45  TEST_COMPARE(te->eventInRange(1.0, 4.0), ==, false);
46 }
47 
48 
49 // ************************************************************
50 // ************************************************************
51 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, Construction)
52 {
54  "TestName", -1, 10, 2));
55 
56  TEST_COMPARE(te->getName(), ==, "TestName");
57 
58  // Test when everything is zero.
59  TEST_COMPARE(te->getIndexStart (), ==, -1);
60  TEST_COMPARE(te->getIndexStop (), ==, 10);
61  TEST_COMPARE(te->getIndexStride(), ==, 2);
62  TEST_COMPARE(te->getNumEvents (), ==, 6);
63 }
64 
65 
66 // ************************************************************
67 // ************************************************************
68 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, Basic_Accesors)
69 {
71 
72  te->setName("TestName");
73  TEST_COMPARE(te->getName(), ==, "TestName");
74 
75  // Reset start after stop.
76  te->setIndexStart(1);
77  TEST_COMPARE(te->getIndexStart (), ==, 1);
78  TEST_COMPARE(te->getIndexStop (), ==, 1);
79  TEST_COMPARE(te->getIndexStride(), ==, 1);
80  TEST_COMPARE(te->getNumEvents (), ==, 1);
81 
82  // Reset stop.
83  te->setIndexStop(5);
84  TEST_COMPARE(te->getIndexStart (), ==, 1);
85  TEST_COMPARE(te->getIndexStop (), ==, 5);
86  TEST_COMPARE(te->getIndexStride(), ==, 1);
87  TEST_COMPARE(te->getNumEvents (), ==, 5);
88 
89  // Reset stride.
90  te->setIndexStride(2);
91  TEST_COMPARE(te->getIndexStart (), ==, 1);
92  TEST_COMPARE(te->getIndexStop (), ==, 5);
93  TEST_COMPARE(te->getIndexStride(), ==, 2);
94  TEST_COMPARE(te->getNumEvents (), ==, 3);
95 
96  // Set with index range.
97  te->setIndexRange(-5, 5, 3);
98  TEST_COMPARE(te->getIndexStart (), ==, -5);
99  TEST_COMPARE(te->getIndexStop (), ==, 5);
100  TEST_COMPARE(te->getIndexStride(), ==, 3);
101  TEST_COMPARE(te->getNumEvents (), ==, 4);
102 }
103 
104 
105 // ************************************************************
106 // ************************************************************
107 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, Stride)
108 {
109  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
110  te->setIndexStart(1);
111  te->setIndexStop (5);
112  TEST_COMPARE(te->getIndexStart (), ==, 1);
113  TEST_COMPARE(te->getIndexStop (), ==, 5);
114 
115  // Negative stride should be reset to stop_-start_.
116  te->setIndexStride(-1);
117  TEST_COMPARE(te->getIndexStride(), ==, 1);
118  TEST_COMPARE(te->getNumEvents (), ==, 5);
119 
120  // Large stride should be reset to stop_-start_.
121  te->setIndexStride(5);
122  TEST_COMPARE(te->getIndexStride(), ==, 4);
123  TEST_COMPARE(te->getNumEvents (), ==, 2);
124 }
125 
126 
127 // ************************************************************
128 // ************************************************************
129 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, isIndex)
130 {
131  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
132  te->setIndexRange(-5, 5, 3);
133 
134  // Test isIndex.
135  TEST_COMPARE(te->isIndex(-6), ==, false); // Around first event.
136  TEST_COMPARE(te->isIndex(-5), ==, true );
137  TEST_COMPARE(te->isIndex(-4), ==, false);
138 
139  TEST_COMPARE(te->isIndex(0), ==, false); // Around mid event.
140  TEST_COMPARE(te->isIndex(1), ==, true );
141  TEST_COMPARE(te->isIndex(2), ==, false);
142 
143  TEST_COMPARE(te->isIndex(3), ==, false); // Around last event.
144  TEST_COMPARE(te->isIndex(4), ==, true );
145  TEST_COMPARE(te->isIndex(5), ==, false);
146 }
147 
148 
149 // ************************************************************
150 // ************************************************************
151 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, indexToNextEvent)
152 {
153  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
154  te->setIndexRange(-5, 5, 3);
155 
156  // Test indexToNextEvent.
157  TEST_COMPARE(te->indexToNextEvent(-9), ==, 4); // Around first event.
158  TEST_COMPARE(te->indexToNextEvent(-5), ==, 0);
159  TEST_COMPARE(te->indexToNextEvent(-4), ==, 2);
160 
161  TEST_COMPARE(te->indexToNextEvent(-1), ==, 2); // Around mid event.
162  TEST_COMPARE(te->indexToNextEvent( 1), ==, 0);
163  TEST_COMPARE(te->indexToNextEvent( 3), ==, 1);
164 
165  TEST_COMPARE(te->indexToNextEvent( 2), ==, 2); // Around last event.
166  TEST_COMPARE(te->indexToNextEvent( 4), ==, 0);
167  TEST_COMPARE(te->indexToNextEvent( 8), ==,-4);
168 }
169 
170 
171 // ************************************************************
172 // ************************************************************
173 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, indexOfNextEvent)
174 {
175  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
176  te->setIndexRange(-5, 5, 3);
177 
178  // Test indexOfNextEvent.
179  TEST_COMPARE(te->indexOfNextEvent(-9), ==, -5); // Around first event.
180  TEST_COMPARE(te->indexOfNextEvent(-5), ==, -5);
181  TEST_COMPARE(te->indexOfNextEvent(-4), ==, -2);
182 
183  TEST_COMPARE(te->indexOfNextEvent(-1), ==, 1); // Around mid event.
184  TEST_COMPARE(te->indexOfNextEvent( 1), ==, 1);
185  TEST_COMPARE(te->indexOfNextEvent( 3), ==, 4);
186 
187  TEST_COMPARE(te->indexOfNextEvent( 2), ==, 4); // Around last event.
188  TEST_COMPARE(te->indexOfNextEvent( 4), ==, 4);
189  TEST_COMPARE(te->indexOfNextEvent( 8), ==, 4);
190 }
191 
192 
193 // ************************************************************
194 // ************************************************************
195 TEUCHOS_UNIT_TEST(TimeEventRangeIndex, eventInRangeIndex)
196 {
197  auto te = rcp(new Tempus::TimeEventRangeIndex<double>());
198  te->setIndexRange(-5, 5, 3);
199 
200  // Test eventInRangeIndex.
201  // Right end.
202  TEST_COMPARE(te->eventInRangeIndex(-9, -6), ==, false); // Around first event.
203  TEST_COMPARE(te->eventInRangeIndex(-9, -5), ==, true );
204  TEST_COMPARE(te->eventInRangeIndex(-9, -4), ==, true );
205 
206  TEST_COMPARE(te->eventInRangeIndex(-1, 0), ==, false); // Around mid event.
207  TEST_COMPARE(te->eventInRangeIndex(-1, 1), ==, true );
208  TEST_COMPARE(te->eventInRangeIndex(-1, 2), ==, true );
209 
210  TEST_COMPARE(te->eventInRangeIndex(2, 3), ==, false); // Around last event.
211  TEST_COMPARE(te->eventInRangeIndex(2, 4), ==, true );
212  TEST_COMPARE(te->eventInRangeIndex(2, 5), ==, true );
213 
214  // Left end.
215  TEST_COMPARE(te->eventInRangeIndex(-6.0, -3), ==, true ); // Around first event.
216  TEST_COMPARE(te->eventInRangeIndex(-5.0, -3), ==, true );
217  TEST_COMPARE(te->eventInRangeIndex(-4.0, -3), ==, false);
218 
219  TEST_COMPARE(te->eventInRangeIndex(-3, 0), ==, true ); // Around mid event.
220  TEST_COMPARE(te->eventInRangeIndex(-2, 0), ==, true );
221  TEST_COMPARE(te->eventInRangeIndex(-1, 0), ==, false);
222 
223  TEST_COMPARE(te->eventInRangeIndex(3, 8), ==, true ); // Around last event.
224  TEST_COMPARE(te->eventInRangeIndex(4, 8), ==, true );
225  TEST_COMPARE(te->eventInRangeIndex(5, 8), ==, false);
226 }
227 
228 
229 } // namespace Tempus_Test
TimeEventRangeIndex specifies a start, stop and stride index.
#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)