Debian Codegraph @

packages/stk/stk_unit_tests/stk_util/schedulerTest.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include "gtest/gtest.h"
36 #include "stk_util/environment/Scheduler.hpp" // for Scheduler, Time, Step
37 #include <iomanip> // for setprecision, _Setprecision
38 #include <iostream> // for cout
39 #include <limits> // for numeric_limits, numeric_limits<>::max_digi...
40
41 namespace
42 {
43 TEST(SchedulerTest, noBonusTimeWithRestart)
44 {
45 stk::util::Scheduler scheduler;
46
47 const stk::util::Time startTime = 0.0;
48 const stk::util::Time dt = 0.1;
49 scheduler.add_interval(startTime, dt);
50
51 const stk::util::Time terminationTime = 10;
52 scheduler.set_termination_time(terminationTime);
53
54 const stk::util::Step unusedStep = 0;
55 const stk::util::Time restartTime = 5.0;
56 scheduler.set_restart_time(restartTime);
57 EXPECT_TRUE(scheduler.is_it_time(5.0,unusedStep));
58 EXPECT_FALSE(scheduler.is_it_time(5.02,unusedStep));
59 EXPECT_TRUE(scheduler.is_it_time(5.1,unusedStep));
60 }
61
62 TEST(SchedulerTest, timeInterval)
63 {
64 stk::util::Scheduler scheduler;
65
66 const stk::util::Time startTime = 0.0;
67 const stk::util::Time dt = 1.0;
68 scheduler.add_interval(startTime, dt);
69
70 const stk::util::Time terminationTime = 5.2;
71 scheduler.set_termination_time(terminationTime);
72
73 const stk::util::Step unusedStep = 0;
74 EXPECT_TRUE(scheduler.is_it_time(0.0, unusedStep));
75 EXPECT_FALSE(scheduler.is_it_time(0.5, unusedStep));
76 EXPECT_TRUE(scheduler.is_it_time(1.0, unusedStep));
77 EXPECT_FALSE(scheduler.is_it_time(1.1, unusedStep));
78 EXPECT_TRUE(scheduler.is_it_time(2.5, unusedStep));
79 EXPECT_FALSE(scheduler.is_it_time(2.6, unusedStep));
80 EXPECT_TRUE(scheduler.is_it_time(2.999999999, unusedStep));
81 EXPECT_FALSE(scheduler.is_it_time(3.0, unusedStep));
82 EXPECT_TRUE(scheduler.is_it_time(4.0, unusedStep));
83 EXPECT_TRUE(scheduler.is_it_time(4.0, unusedStep));
84 EXPECT_TRUE(scheduler.is_it_time(5.0, unusedStep));
85 EXPECT_FALSE(scheduler.is_it_time(terminationTime-0.1, unusedStep));
86 EXPECT_TRUE(scheduler.is_it_time(terminationTime+0.1, unusedStep));
87 EXPECT_FALSE(scheduler.is_it_time(terminationTime+0.2, unusedStep));
88 }
89
90 TEST(SchedulerTest, stepInterval)
91 {
92 stk::util::Scheduler scheduler;
93
94 const stk::util::Step startStep = 0;
95 const stk::util::Step stepInterval = 2;
96 scheduler.add_interval(startStep, stepInterval);
97
98 stk::util::Time unusedTime = 0.0;
99 const stk::util::Time dt = 0.1;
100 EXPECT_TRUE(scheduler.is_it_time(unusedTime, 0));
101 unusedTime+=dt;
102 EXPECT_FALSE(scheduler.is_it_time(unusedTime, 1));
103 unusedTime+=dt;
104 EXPECT_TRUE(scheduler.is_it_time(unusedTime, 2));
105 unusedTime+=dt;
106 EXPECT_FALSE(scheduler.is_it_time(unusedTime, 3));
107 unusedTime+=dt;
108 EXPECT_TRUE(scheduler.is_it_time(unusedTime, 4));
109 }
110
111 TEST(SchedulerTest, explicitStep)
112 {
113 stk::util::Scheduler scheduler;
114
115 const stk::util::Step startStep = 0;
116 const stk::util::Step stepInterval = 2;
117 scheduler.add_interval(startStep, stepInterval);
118 const stk::util::Step additionalStepToOutput = 3;
119 scheduler.add_explicit(additionalStepToOutput);
120
121 stk::util::Time unusedTime = 0.0;
122 const stk::util::Time dt = 0.1;
123 EXPECT_TRUE(scheduler.is_it_time(unusedTime, 0));
124 unusedTime+=dt;
125 EXPECT_FALSE(scheduler.is_it_time(unusedTime, 1));
126 unusedTime+=dt;
127 EXPECT_TRUE(scheduler.is_it_time(unusedTime, 2));
128 unusedTime+=dt;
129 EXPECT_TRUE(scheduler.is_it_time(unusedTime, 3));
130 unusedTime+=dt;
131 EXPECT_TRUE(scheduler.is_it_time(unusedTime, 4));
132 unusedTime+=dt;
133 EXPECT_FALSE(scheduler.is_it_time(unusedTime, 5));
134 }
135
136 TEST(SchedulerTest, forceWrite)
137 {
138 stk::util::Scheduler scheduler;
139
140 const stk::util::Time startTime = 0.0;
141 const stk::util::Time dt = 1.0;
142 scheduler.add_interval(startTime, dt);
143
144 const stk::util::Step unusedStep = 0;
145 EXPECT_TRUE(scheduler.is_it_time(0.0, unusedStep));
146 scheduler.set_force_schedule();
147 EXPECT_TRUE(scheduler.is_it_time(0.5, unusedStep));
148 EXPECT_TRUE(scheduler.is_it_time(1.0, unusedStep));
149 EXPECT_FALSE(scheduler.is_it_time(1.1, unusedStep));
150 EXPECT_TRUE(scheduler.is_it_time(2.5, unusedStep));
151 EXPECT_FALSE(scheduler.is_it_time(2.6, unusedStep));
152 EXPECT_TRUE(scheduler.is_it_time(2.999999999, unusedStep));
153 }
154
155 TEST(SchedulerTest, emptyScheduler)
156 {
157 stk::util::Scheduler scheduler;
158 const stk::util::Time terminationTime = 5.0;
159 scheduler.set_termination_time(terminationTime);
160 EXPECT_FALSE(scheduler.is_it_time(0.0, 0));
161 EXPECT_FALSE(scheduler.is_it_time(0.5, 1));
162 EXPECT_FALSE(scheduler.is_it_time(3.5, 2));
163 EXPECT_TRUE(scheduler.is_it_time(terminationTime, 2));
164 EXPECT_FALSE(scheduler.is_it_time(terminationTime+0.5, 2));
165 }
166
167 TEST(SchedulerTest, stepIntervalWithTerminationTime)
168 {
169 using stk::util::Step;
170
171 stk::util::Scheduler scheduler;
172 const stk::util::Time terminationTime = 4.5;
173 scheduler.set_termination_time(terminationTime);
174 scheduler.add_interval(Step(0), Step(2));
175 EXPECT_TRUE(scheduler.is_it_time(0.0, 0));
176 EXPECT_FALSE(scheduler.is_it_time(0.5, 1));
177 EXPECT_TRUE(scheduler.is_it_time(2.0, 2));
178 EXPECT_FALSE(scheduler.is_it_time(3.5, 3));
179 EXPECT_TRUE(scheduler.is_it_time(4.0, 4));
180 EXPECT_TRUE(scheduler.is_it_time(terminationTime, 5));
181 EXPECT_FALSE(scheduler.is_it_time(terminationTime+0.5, 6));
182 }
183
184 TEST(SchedulerTest, largeStartingTimeFollowedBySmallStep)
185 {
186 stk::util::Scheduler scheduler;
187
188 const int number_of_time_steps = 10000;
189 const stk::util::Time startTime = 8.0e4;
190 const stk::util::Time dt = 1.0e-8;
191 const int skip = 4;
192 scheduler.add_interval(startTime, skip*dt);
193
194 const stk::util::Time terminationTime = startTime + number_of_time_steps * dt;
195 scheduler.set_termination_time(terminationTime);
196
197 stk::util::Time t = startTime;
198 for(int i = 0; i < number_of_time_steps; ++i) {
199 t = startTime + dt * i;
200 bool write = scheduler.is_it_time(t, i);
201 if (i % skip == 0) {
202 EXPECT_TRUE(write) << "time= " << t << ", step= " << i << ", write= " << write;
203 }
204 else {
205 EXPECT_FALSE(write) << "time= " << t << ", step= " << i << ", write= " << write;
206 }
207 }
208 }
209
210 TEST(SchedulerTest, timePlusDtEqualsTime)
211 {
212 using Time = stk::util::Time;
213
214 stk::util::Scheduler scheduler;
215
216 const int number_of_time_steps = 10;
217 const Time start_time = 1l;
218
219 const Time simulation_dt = std::numeric_limits<Time>::epsilon();
220 const Time termination_time = start_time + number_of_time_steps * simulation_dt;
221
222 const Time output_dt = 2 * simulation_dt;
223 scheduler.add_interval(start_time, output_dt);
224 scheduler.set_termination_time(termination_time);
225
226 Time t = start_time;
227 EXPECT_TRUE( scheduler.is_it_time(t, 0) );
228
229 t = start_time + simulation_dt;
230
231 if( simulation_dt < std::numeric_limits<Time>::epsilon() ) {
232 // In this situation, adding a time step that is less than machine
233 // epsilon will not be observable.
234 EXPECT_TRUE(t == start_time);
235 auto p = std::numeric_limits<Time>::max_digits10;
236 // This is the expected behavior for time steps less than machine epsilon
237 // for a time value in [-1, 1]
238 EXPECT_TRUE( scheduler.is_it_time(t, 1) )
239 << std::setprecision(p) << t
240 << " = " << std::setprecision(p) << start_time
241 << " + " << std::setprecision(p) << simulation_dt
242 << " = " << std::setprecision(p) << start_time + simulation_dt
243 << '\n';
244 }
245
246 scheduler.print(std::cout);
247 }
248
249 }
250