| 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 <random> |
| 36 |
#include <stk_util/environment/Scheduler.hpp> |
| 37 |
#include <stk_util/environment/WallTime.hpp> |
| 38 |
|
| 39 |
#include "gtest/gtest.h" |
| 40 |
|
| 41 |
TEST(Scheduler, AdjustDt) |
| 42 |
{ |
| 43 |
stk::util::Scheduler scheduler; |
| 44 |
scheduler.set_lookahead(4); |
| 45 |
scheduler.add_interval(0.0, 1.0); |
| 46 |
|
| 47 |
const double eps = std::numeric_limits<double>::epsilon(); |
| 48 |
|
| 49 |
{ |
| 50 |
const double t = 0.9 - 10 * eps; |
| 51 |
const double dt = 0.1; |
| 52 |
const double dt_new = scheduler.adjust_dt(dt, t); |
| 53 |
EXPECT_NEAR(dt + 10 * eps, dt_new, eps); |
| 54 |
} |
| 55 |
|
| 56 |
{ |
| 57 |
const double t = 1.0 - 10 * eps; |
| 58 |
const double dt = 0.1; |
| 59 |
const double dt_new = scheduler.adjust_dt(dt, t); |
| 60 |
EXPECT_DOUBLE_EQ(dt, dt_new); |
| 61 |
} |
| 62 |
|
| 63 |
{ |
| 64 |
const double t = 0.85; |
| 65 |
const double dt = 0.1; |
| 66 |
const double dt_new = scheduler.adjust_dt(dt, t); |
| 67 |
EXPECT_NEAR(0.075, dt_new, eps); |
| 68 |
} |
| 69 |
|
| 70 |
{ |
| 71 |
const double t = 0.95; |
| 72 |
const double dt = 0.1; |
| 73 |
const double dt_new = scheduler.adjust_dt(dt, t); |
| 74 |
EXPECT_NEAR(0.05, dt_new, eps); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
TEST(Scheduler, LogarithmicOutput) |
| 79 |
{ |
| 80 |
stk::util::Scheduler scheduler; |
| 81 |
scheduler.set_lookahead(4); |
| 82 |
scheduler.add_interval(0.0, 1e-5); |
| 83 |
scheduler.add_interval(1e-4, 1e-4); |
| 84 |
scheduler.add_interval(1e-3, 1e-3); |
| 85 |
scheduler.add_interval(1e-2, 1e-2); |
| 86 |
scheduler.add_interval(1e-1, 1e-1); |
| 87 |
scheduler.add_interval(1.0, 1.0); |
| 88 |
scheduler.add_interval(10.0, 10.0); |
| 89 |
scheduler.add_interval(100.0, 100.0); |
| 90 |
|
| 91 |
double t = 0.0; |
| 92 |
double dt = 1e-6; |
| 93 |
const double dt_max = 100.0; |
| 94 |
|
| 95 |
std::mt19937 rng; |
| 96 |
rng.seed(666); |
| 97 |
|
| 98 |
std::uniform_real_distribution<double> noise(-1.0, 1.0); |
| 99 |
|
| 100 |
int step = 0; |
| 101 |
std::vector<double> output_times; |
| 102 |
while (t < 3600.0) { |
| 103 |
const double fac = 1.1 + (0.1 - std::numeric_limits<double>::epsilon()) * noise(rng); |
| 104 |
dt = std::min(dt_max, fac * dt); |
| 105 |
dt = scheduler.adjust_dt(dt, t); |
| 106 |
|
| 107 |
EXPECT_TRUE(dt >= 1e-6); |
| 108 |
|
| 109 |
if (scheduler.is_it_time(t, step)) { |
| 110 |
output_times.push_back(t); |
| 111 |
} |
| 112 |
|
| 113 |
t += dt; |
| 114 |
++step; |
| 115 |
} |
| 116 |
|
| 117 |
std::vector<double> expected_times = {0.0, 1e-5, 2e-5, 3e-5, 4e-5, 5e-5, 6e-5, 7e-5, 8e-5, 9e-5, 1e-4, 2e-4, 3e-4, |
| 118 |
4e-4, 5e-4, 6e-4, 7e-4, 8e-4, 9e-4, 1e-3, 2e-3, 3e-3, 4e-3, 5e-3, 6e-3, 7e-3, 8e-3, 9e-3, 1e-2, 2e-2, 3e-2, 4e-2, |
| 119 |
5e-2, 6e-2, 7e-2, 8e-2, 9e-2, 1e-1, 2e-1, 3e-1, 4e-1, 5e-1, 6e-1, 7e-1, 8e-1, 9e-1, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, |
| 120 |
7.0, 8.0, 9.0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, |
| 121 |
1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, |
| 122 |
3200, 3300, 3400, 3500}; |
| 123 |
|
| 124 |
const double eps = 10 * std::numeric_limits<double>::epsilon(); |
| 125 |
for (unsigned i = 0; i < expected_times.size(); ++i) { |
| 126 |
EXPECT_NEAR(expected_times[i], output_times[i], expected_times[i] * eps); |
| 127 |
} |
| 128 |
} |