From 7a5d7f6c4bf20ceaa6426fee3f91d2f8b6718322 Mon Sep 17 00:00:00 2001 From: Ned Coltman <edward.coltman@iws.uni-stuttgart.de> Date: Wed, 29 May 2019 15:22:17 +0200 Subject: [PATCH] [math][linspace] move linspace to math.hh --- dumux/common/math.hh | 20 +++++++++++++++++++ test/common/spline/test_cubicspline.cc | 16 ++++----------- .../common/spline/test_monotonecubicspline.cc | 16 ++++----------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/dumux/common/math.hh b/dumux/common/math.hh index dec8cb6684..8c7eba9533 100644 --- a/dumux/common/math.hh +++ b/dumux/common/math.hh @@ -562,6 +562,26 @@ struct LinearTable } // end namespace InterpolationPolicy +/*! + * \ingroup Common + * \brief Generates linearly spaced vectors + * + * \param begin The first value in the vector + * \param end The last value in the vector + * \param samples The size of the vector + */ +template <class Scalar> +std::vector<Scalar> linspace(const Scalar begin, const Scalar end, std::size_t samples) +{ + using std::max; + samples = max(std::size_t{2}, samples); // only makes sense for 2 or more samples + const Scalar delta = (end-begin)/static_cast<Scalar>(samples-1); + std::vector<Scalar> vec(samples); + for (std::size_t i = 0; i < samples; ++i) + vec[i] = begin + i*delta; + return vec; +} + /*! * \ingroup Common diff --git a/test/common/spline/test_cubicspline.cc b/test/common/spline/test_cubicspline.cc index d83e589cd7..a95ebb81cf 100644 --- a/test/common/spline/test_cubicspline.cc +++ b/test/common/spline/test_cubicspline.cc @@ -29,18 +29,10 @@ #include <dune/common/exceptions.hh> #include <dune/common/parallel/mpihelper.hh> +#include <dumux/common/math.hh> #include <dumux/common/cubicspline.hh> #include <dumux/io/gnuplotinterface.hh> -std::vector<double> linspace(const double begin, const double end, const double samples) -{ - const double delta = (end-begin)/static_cast<double>(samples-1); - std::vector<double> vec(samples); - for (int i = 0; i < samples; ++i) - vec[i] = begin + i*delta; - return vec; -} - template<class Function> std::vector<double> eval(const Function& f, const std::vector<double>& x) { @@ -58,12 +50,12 @@ int main(int argc, char** argv) const auto df = [](double x){ return -2.0*x/(( 1.0 + x*x )*( 1.0 + x*x )); }; // create some test samples - const auto testPoints = linspace(-4.0, 4.0, 1000); + const auto testPoints = Dumux::linspace(-4.0, 4.0, 1000); const auto ref = eval(f, testPoints); const auto refDeriv = eval(df, testPoints); // create the spline sample points - const auto samplePoints = linspace(-4.0, 4.0, 15); + const auto samplePoints = Dumux::linspace(-4.0, 4.0, 15); const auto y = eval(f, samplePoints); // create the spline @@ -86,7 +78,7 @@ int main(int argc, char** argv) DUNE_THROW(Dune::Exception, "Maximum error in spline interpolation too large!"); // plot with Gnuplot (plot a bit more so we can see the linear extension) - const auto plotPoints = linspace(-8.0, 8.0, 1000); + const auto plotPoints = Dumux::linspace(-8.0, 8.0, 1000); const auto refPlot = eval(f, plotPoints); const auto refDerivPlot = eval(df, plotPoints); const auto resultPlot = eval([&](const double x) { return spline.eval(x); }, plotPoints); diff --git a/test/common/spline/test_monotonecubicspline.cc b/test/common/spline/test_monotonecubicspline.cc index 3ce80a1de7..378efe2648 100644 --- a/test/common/spline/test_monotonecubicspline.cc +++ b/test/common/spline/test_monotonecubicspline.cc @@ -29,18 +29,10 @@ #include <dune/common/exceptions.hh> #include <dune/common/parallel/mpihelper.hh> +#include <dumux/common/math.hh> #include <dumux/common/monotonecubicspline.hh> #include <dumux/io/gnuplotinterface.hh> -std::vector<double> linspace(const double begin, const double end, const double samples) -{ - const double delta = (end-begin)/static_cast<double>(samples-1); - std::vector<double> vec(samples); - for (int i = 0; i < samples; ++i) - vec[i] = begin + i*delta; - return vec; -} - template<class Function> std::vector<double> eval(const Function& f, const std::vector<double>& x) { @@ -58,12 +50,12 @@ int main(int argc, char** argv) const auto df = [](double x){ return 3*x*x; }; // create some test samples - const auto testPoints = linspace(0.0, 4.0, 1000); + const auto testPoints = Dumux::linspace(0.0, 4.0, 1000); const auto ref = eval(f, testPoints); const auto refDeriv = eval(df, testPoints); // create the spline sample points - const auto samplePoints = linspace(0.0, 5.0, 10); + const auto samplePoints = Dumux::linspace(0.0, 5.0, 10); const auto y = eval(f, samplePoints); // create the spline @@ -88,7 +80,7 @@ int main(int argc, char** argv) DUNE_THROW(Dune::Exception, "Maximum error in spline interpolation too large!"); // plot with Gnuplot (plot a bit more so we can see the linear extension) - const auto plotPoints = linspace(-1.0, 5.0, 1000); + const auto plotPoints = Dumux::linspace(-1.0, 5.0, 1000); const auto refPlot = eval(f, plotPoints); const auto refDerivPlot = eval(df, plotPoints); const auto resultPlot = eval([&](const double x) { return spline.eval(x); }, plotPoints); -- GitLab