From 084012eac669b6750d2f856bc19f8caf4d330ac5 Mon Sep 17 00:00:00 2001 From: Florian Weik Date: Fri, 15 Feb 2019 22:19:33 +0100 Subject: [PATCH 1/2] core: Make tabulated pair IA not crash if distance is too small --- src/core/TabulatedPotential.hpp | 9 +++++---- src/core/utils/linear_interpolation.hpp | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/core/TabulatedPotential.hpp b/src/core/TabulatedPotential.hpp index 282473ca6c0..3177df044ce 100644 --- a/src/core/TabulatedPotential.hpp +++ b/src/core/TabulatedPotential.hpp @@ -24,6 +24,7 @@ along with this program. If not, see . #include #include +#include #include #include @@ -36,13 +37,13 @@ struct TabulatedPotential { std::vector energy_tab; double force(double x) const { - assert(x <= maxval); - return Utils::linear_interpolation(force_tab, invstepsize, minval, x); + using boost::algorithm::clamp; + return Utils::linear_interpolation(force_tab, invstepsize, minval, clamp(x, minval, maxval)); } double energy(double x) const { - assert(x <= maxval); - return Utils::linear_interpolation(energy_tab, invstepsize, minval, x); + using boost::algorithm::clamp; + return Utils::linear_interpolation(energy_tab, invstepsize, minval, clamp(x, minval, maxval)); } double cutoff() const { return maxval; } diff --git a/src/core/utils/linear_interpolation.hpp b/src/core/utils/linear_interpolation.hpp index 8bf96436f94..cedc691a18f 100644 --- a/src/core/utils/linear_interpolation.hpp +++ b/src/core/utils/linear_interpolation.hpp @@ -27,6 +27,7 @@ T linear_interpolation(Container const &table, T hi, T offset, T x) { auto const dind = (x - offset) * hi; auto const ind = static_cast(dind); assert(ind <= dind); + assert((ind >= 0) and (ind < table.size())); auto const dx = dind - ind; /* linear interpolation between data points */ From d8c053a9bbf92623c7277897ea0fc1e0d022069d Mon Sep 17 00:00:00 2001 From: Florian Weik Date: Fri, 15 Feb 2019 22:29:43 +0100 Subject: [PATCH 2/2] Formatting --- src/core/TabulatedPotential.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/TabulatedPotential.hpp b/src/core/TabulatedPotential.hpp index 3177df044ce..f867b1e0f57 100644 --- a/src/core/TabulatedPotential.hpp +++ b/src/core/TabulatedPotential.hpp @@ -22,9 +22,9 @@ along with this program. If not, see . #include "utils/linear_interpolation.hpp" #include "utils/serialization/List.hpp" +#include #include #include -#include #include #include @@ -38,12 +38,14 @@ struct TabulatedPotential { double force(double x) const { using boost::algorithm::clamp; - return Utils::linear_interpolation(force_tab, invstepsize, minval, clamp(x, minval, maxval)); + return Utils::linear_interpolation(force_tab, invstepsize, minval, + clamp(x, minval, maxval)); } double energy(double x) const { using boost::algorithm::clamp; - return Utils::linear_interpolation(energy_tab, invstepsize, minval, clamp(x, minval, maxval)); + return Utils::linear_interpolation(energy_tab, invstepsize, minval, + clamp(x, minval, maxval)); } double cutoff() const { return maxval; }