Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Make tabulated pair IA not crash if distance is too small #2520

Merged
merged 2 commits into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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