test_monotonecubicspline.cc 6.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
 *   See the file COPYING for full copying permissions.                      *
 *                                                                           *
 *   This program is free software: you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation, either version 3 of the License, or       *
 *   (at your option) any later version.                                     *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 *****************************************************************************/
/*!
 * \file
 * \brief Test the simple cubic spline implementation
 */
#include <config.h>
#include <cmath>
#include <vector>
#include <numeric>
#include <algorithm>
#include <functional>

#include <dune/common/exceptions.hh>
#include <dune/common/parallel/mpihelper.hh>
32
#include <dumux/common/math.hh>
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <dumux/common/monotonecubicspline.hh>
#include <dumux/io/gnuplotinterface.hh>

template<class Function>
std::vector<double> eval(const Function& f, const std::vector<double>& x)
{
    auto y = x;
    std::transform(x.begin(), x.end(), y.begin(), [&](const double x) { return f(x); });
    return y;
}

int main(int argc, char** argv)
{
    Dune::MPIHelper::instance(argc, argv);

    // we test the spline interpolation against a sample function
    const auto f = [](double x){ return x*x*x; };
    const auto df = [](double x){ return 3*x*x; };

    // create some test samples
53
    const auto testPoints = Dumux::linspace(0.0, 4.0, 1000);
54
55
56
57
    const auto ref = eval(f, testPoints);
    const auto refDeriv = eval(df, testPoints);

    // create the spline sample points
58
    const auto samplePoints = Dumux::linspace(0.0, 5.0, 10);
59
60
61
    const auto y = eval(f, samplePoints);

    // create the spline
62
    Dumux::MonotoneCubicSpline<double> spline(samplePoints, y);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    // evaluate spline and derivative
    const auto result = eval([&](const double x) { return spline.eval(x); }, testPoints);
    const auto resultDeriv = eval([&](const double x) { return spline.evalDerivative(x); }, testPoints);

    // compute largest difference
    auto diff = result; auto diffDeriv = result;
    std::transform(result.begin(), result.end(), ref.begin(), diff.begin(), [](auto a, auto b){ return std::abs(a-b); });
    std::transform(resultDeriv.begin(), resultDeriv.end(), refDeriv.begin(), diffDeriv.begin(), [](auto a, auto b){ return std::abs(a-b); });
    const auto maxNorm = std::accumulate(diff.begin(), diff.end(), diff[0], [](auto a, auto b){ return std::max(a, b); })
                         /(*std::max_element(ref.begin(), ref.end()));
    const auto maxNormDeriv = std::accumulate(diffDeriv.begin(), diffDeriv.end(), diffDeriv[0], [](auto a, auto b){ return std::max(a, b); })
                              /(*std::max_element(refDeriv.begin(), refDeriv.end()));
    std::cout << "Maximum error: " << maxNorm << "\n";
    std::cout << "Maximum error in derivative: " << maxNormDeriv << "\n";

    if (maxNorm > 0.0008 || maxNormDeriv > 0.013)
        DUNE_THROW(Dune::Exception, "Maximum error in spline interpolation too large!");

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    // test inverse by evaluating (x = f^-1(f(x))) for monotonically increasing function
    {
        const auto resultX = eval([&](double x){ return spline.evalInverse(spline.eval(x)); }, testPoints);
        auto diffInverse = resultX;
        std::transform(resultX.begin(), resultX.end(), testPoints.begin(), diffInverse.begin(), [](auto a, auto b){ return std::abs(a-b); });
        const auto maxNormInverse = std::accumulate(diffInverse.begin(), diffInverse.end(), diffInverse[0], [](auto a, auto b){ return std::max(a, b); })
                             /(*std::max_element(testPoints.begin(), testPoints.end()));


        std::cout << "Maximum error in identity using the inverse (mon. incr.): " << std::scientific << maxNormInverse << "\n";
        if (maxNormInverse > 1e-13)
            DUNE_THROW(Dune::Exception, "Maximum error in spline interpolation too large!");
    }
    // test inverse by evaluating (x = f^-1(f(x))) for monotonically decreasing function
    {
        auto reverseTest = testPoints;
        std::reverse(reverseTest.begin(), reverseTest.end());
        const auto resultX = eval([&](double x){ return spline.evalInverse(spline.eval(x)); }, reverseTest);
        auto diffInverse = resultX;
        std::transform(resultX.begin(), resultX.end(), reverseTest.begin(), diffInverse.begin(), [](auto a, auto b){ return std::abs(a-b); });
        const auto maxNormInverse = std::accumulate(diffInverse.begin(), diffInverse.end(), diffInverse[0], [](auto a, auto b){ return std::max(a, b); })
                             /(*std::max_element(reverseTest.begin(), reverseTest.end()));

        std::cout << "Maximum error in identity using the inverse (mon. decr.): " << std::scientific << maxNormInverse << "\n";
        if (maxNormInverse > 1e-13)
            DUNE_THROW(Dune::Exception, "Maximum error in spline interpolation too large!");
    }

110
    // plot with Gnuplot (plot a bit more so we can see the linear extension)
111
    const auto plotPoints = Dumux::linspace(-1.0, 5.0, 1000);
112
113
114
115
116
    const auto refPlot = eval(f, plotPoints);
    const auto refDerivPlot = eval(df, plotPoints);
    const auto resultPlot = eval([&](const double x) { return spline.eval(x); }, plotPoints);
    const auto resultDerivPlot = eval([&](const double x) { return spline.evalDerivative(x); }, plotPoints);

117
    Dumux::GnuplotInterface<double> gnuplot(/*persist=*/false);
118
119
120
121
122
123
124
125
    gnuplot.addDataSetToPlot(plotPoints, refPlot, "exp_reference");
    gnuplot.addDataSetToPlot(plotPoints, refDerivPlot, "exp_reference_derivative");
    gnuplot.addDataSetToPlot(plotPoints, resultPlot, "monotspline");
    gnuplot.addDataSetToPlot(plotPoints, resultDerivPlot, "monotspline_derivative");
    gnuplot.plot("monotspline");

    return 0;
}