|
| 1 | +/*! |
| 2 | + * \file windowing.cpp |
| 3 | + * \brief Unit tests for windowed time-averaging. |
| 4 | + * \author C. Bauer |
| 5 | + * \version 7.3.0 "Blackbird" |
| 6 | + * |
| 7 | + * SU2 Project Website: https://su2code.github.io |
| 8 | + * |
| 9 | + * The SU2 Project is maintained by the SU2 Foundation |
| 10 | + * (http://su2foundation.org) |
| 11 | + * |
| 12 | + * Copyright 2012-2022, SU2 Contributors (cf. AUTHORS.md) |
| 13 | + * |
| 14 | + * SU2 is free software; you can redistribute it and/or |
| 15 | + * modify it under the terms of the GNU Lesser General Public |
| 16 | + * License as published by the Free Software Foundation; either |
| 17 | + * version 2.1 of the License, or (at your option) any later version. |
| 18 | + * |
| 19 | + * SU2 is distributed in the hope that it will be useful, |
| 20 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | + * Lesser General Public License for more details. |
| 23 | + * |
| 24 | + * You should have received a copy of the GNU Lesser General Public |
| 25 | + * License along with SU2. If not, see <http://www.gnu.org/licenses/>. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "catch.hpp" |
| 29 | +#include "../../SU2_CFD/include/output/COutput.hpp" |
| 30 | +#include "../../SU2_CFD/include/output/tools/CWindowingTools.hpp" |
| 31 | + |
| 32 | +/*! |
| 33 | + * \brief Wrapper class that provides reference values and methods for |
| 34 | + * averaging these over different windows. |
| 35 | + */ |
| 36 | +struct CWindowingTest { |
| 37 | + static constexpr size_t MAX_INNER_ITERATIONS = 5; |
| 38 | + |
| 39 | + /*! |
| 40 | + * \brief Calculates sample value at the specified time-step. Simple SIN function. |
| 41 | + */ |
| 42 | + static su2double GetSampleAtIteration(unsigned long currentTimeStep) { |
| 43 | + return 0.5 * sin(0.1 * currentTimeStep + 2) + 1.; |
| 44 | + } |
| 45 | + |
| 46 | + /*! |
| 47 | + * \brief Perform averaging over simulated inner and outer solver iterations. |
| 48 | + */ |
| 49 | + static su2double calcAverage(WINDOW_FUNCTION win, unsigned long nIterations, unsigned long startIteration) { |
| 50 | + CWindowedAverage avg{win}; // Create averaging object with the specified window. |
| 51 | + su2double previousSample = 0.; |
| 52 | + su2double currentSample = 0.; |
| 53 | + su2double input = 0.; // Value that is specified as actual input to the addValue function. |
| 54 | + su2double weightPrevious = 0.; |
| 55 | + su2double weightCurrent = 0.; |
| 56 | + // Simulate solver time-steps |
| 57 | + for (size_t outerIteration = 0; outerIteration < nIterations; outerIteration++) { |
| 58 | + previousSample = currentSample; |
| 59 | + currentSample = GetSampleAtIteration(outerIteration); |
| 60 | + // Simulate inner iterations |
| 61 | + for (size_t innerIteration = 0; innerIteration < MAX_INNER_ITERATIONS; innerIteration++) { |
| 62 | + weightPrevious = static_cast<su2double>(MAX_INNER_ITERATIONS - innerIteration - 1); |
| 63 | + weightCurrent = innerIteration; |
| 64 | + // Simulate gradual change of sample during inner iterations. |
| 65 | + input = (weightPrevious * previousSample + weightCurrent * currentSample) / (weightCurrent + weightPrevious); |
| 66 | + avg.addValue(input, outerIteration, startIteration); |
| 67 | + } |
| 68 | + } |
| 69 | + return avg.GetVal(); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +TEST_CASE("BUMP", "[Windowing]") { |
| 74 | + su2double avg = 0; |
| 75 | + avg = CWindowingTest::calcAverage(WINDOW_FUNCTION::BUMP, 10, 0); |
| 76 | + CHECK(avg == Approx(1.1851).epsilon(0.001)); |
| 77 | + avg = CWindowingTest::calcAverage(WINDOW_FUNCTION::BUMP, 100, 10); |
| 78 | + CHECK(avg == Approx(1.1883).epsilon(0.001)); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_CASE("HANN", "[Windowing]") { |
| 82 | + su2double avg = 0; |
| 83 | + avg = CWindowingTest::calcAverage(WINDOW_FUNCTION::HANN, 10, 0); |
| 84 | + CHECK(avg == Approx(1.1832).epsilon(0.001)); |
| 85 | + avg = CWindowingTest::calcAverage(WINDOW_FUNCTION::HANN, 100, 10); |
| 86 | + CHECK(avg == Approx(1.0869).epsilon(0.001)); |
| 87 | +} |
| 88 | + |
| 89 | +TEST_CASE("HANN_SQUARE", "[Windowing]") { |
| 90 | + su2double avg = 0; |
| 91 | + avg = CWindowingTest::calcAverage(WINDOW_FUNCTION::HANN_SQUARE, 10, 0); |
| 92 | + CHECK(avg == Approx(1.1847).epsilon(0.001)); |
| 93 | + avg = CWindowingTest::calcAverage(WINDOW_FUNCTION::HANN_SQUARE, 100, 10); |
| 94 | + CHECK(avg == Approx(1.1856).epsilon(0.001)); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_CASE("SQUARE", "[Windowing]") { |
| 98 | + su2double avg = 0; |
| 99 | + avg = CWindowingTest::calcAverage(WINDOW_FUNCTION::SQUARE, 10, 0); |
| 100 | + CHECK(avg == Approx(1.3059).epsilon(0.001)); |
| 101 | + avg = CWindowingTest::calcAverage(WINDOW_FUNCTION::SQUARE, 100, 10); |
| 102 | + CHECK(avg == Approx(0.9001).epsilon(0.001)); |
| 103 | +} |
0 commit comments