Implementing Cubic Periodic Boundaries 2.Central-image codes
Involved in most time-consuming part of simulation
(0,1) coordinates, decision based
- r.x -= (r.x > 0.0) ? Math.floor(r.x) : Math.ceil(r.x-1.0); //Java syntax
- examples: -0.2 ? +0.8; -1.4 ? +0.4; +0.4 ? +0.4; +0.6 ? +0.6; +1.5 ? +0.5
(0,L) coordinates, decision based
- r.x -= dimensions.x * ((r.x > 0.0) ? Math.floor(r.x/dimensions.x) : Math.ceil(r.x/dimensions.x-1.0));
(-1/2, 1/2), decision based
- if(r.x > 0.5) r.x -= 1.0; if(r.x < -0.5) r.x += 1.0; //only first shell
- examples: -0.2 ? -0.2; -1.4 ? -0.4; +0.4 ? +0.4; +0.6 ? -0.4; +1.5 ? +0.5
(-1/2, 1/2), function based
- r.x -= Math.round(r.x);//nearest integer (r.x must be float, not double)
(0,L), function based
- r.x %= dimensions.x; if(r.x < 0.0) r.x += dimensions.x;//modulo operator
N.B. Most code segments are untested