Implementing Cubic Periodic Boundaries 3.Nearest-image codes
Simply apply (-1/2,1/2) central-image code to raw difference!
- dr.x = r1.x - r2.x; //unit box length
- if(dr.x > 0.5) dr.x -= 1.0;
- if(dr.x < -0.5) dr.x += 1.0;
- dr.x *= dimensions.x;
Or…
- dr.x = r1.x - r2.x; //true box length
- dr.x -= dimensions.x * Math.round(dr.x/dimensions.x);
Take care not to lose correct sign, if doing force calculation
Nearest image for non-cubic boundary not always given simply in terms of a central-image algorithm