Skip to content

Commit

Permalink
Merge pull request espressomd#2520 from fweik/tab
Browse files Browse the repository at this point in the history
core: Make tabulated pair IA not crash if distance is too small
  • Loading branch information
fweik authored and RudolfWeeber committed Mar 12, 2019
1 parent fa859b3 commit a1a7e04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/core/TabulatedPotential.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "utils/linear_interpolation.hpp"
#include "utils/serialization/List.hpp"

#include <boost/algorithm/clamp.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>

Expand All @@ -36,13 +37,15 @@ struct TabulatedPotential {
std::vector<double> 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; }
Expand Down
1 change: 1 addition & 0 deletions src/core/utils/linear_interpolation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(dind);
assert(ind <= dind);
assert((ind >= 0) and (ind < table.size()));
auto const dx = dind - ind;

/* linear interpolation between data points */
Expand Down

0 comments on commit a1a7e04

Please sign in to comment.