Prevent out of bounds read in potential energy calculation