Function Repository Resource:

PeriodicBoxDisplacement

Source Notebook

Get the shortest displacement between two points in a box with periodic boundary conditions

Contributed by: Matt Kafker

ResourceFunction["PeriodicBoxDisplacement"][u,v,L]

gives the shortest displacement between vectors u and v in a cubic box of side length L centered on the origin with periodic boundary conditions.

Details

ResourceFunction["PeriodicBoxDisplacement"] works for boxes in any spatial dimension.

Examples

Basic Examples (3) 

Compute a 1D periodic box displacement in the "box" represented by the interval [-1,1]:

In[1]:=
ResourceFunction["PeriodicBoxDisplacement"][{0.9}, {-0.9}, 2]
Out[1]=

PeriodicBoxDisplacement reverts to Euclidean displacement when points are close by within the box:

In[2]:=
ResourceFunction["PeriodicBoxDisplacement"][{0}, {0.1}, 2]
Out[2]=

Parallel points on the boundaries of the box are mapped to the same point:

In[3]:=
ResourceFunction["PeriodicBoxDisplacement"][{-1}, {1}, 2]
Out[3]=

Scope (1) 

PeriodicBoxDisplacement also works in higher dimensions:

In[4]:=
ResourceFunction["PeriodicBoxDisplacement"][{3, 4}, {-3, -4}, 10]
Out[4]=

Neat Examples (2) 

Visualize the displacement vector between two points in different boxes (dashed boxes are "copies" of the main box due to periodic boundary conditions):

In[5]:=
With[{u = {3, 4}, v = {-3, -4}, L = 10},
 Graphics[{Table[{{
Directive[
FaceForm[], 
EdgeForm[
If[{x, y} == {0, 0}, 
Directive[Thick, 
Opacity[1], Black], 
Directive[Gray, Dashed]]]], 
Rectangle[{Rational[-1, 2] L - x, Rational[-1, 2] L - y}, {Rational[1, 2] L + x, Rational[1, 2] L + y}]},
     {Blue, Disk[u + {x, y}]},
     {Directive[Gray, Opacity[0.5]], Disk[v + {x, y}]}},
    {x, {-L, 0, L}}, {y, {-L, 0, L}}],
   Arrow[{u, u + ResourceFunction["PeriodicBoxDisplacement"][u, v, L]}]}]
 ]
Out[5]=

In three dimensions:

In[6]:=
With[{u = {2, 3, 4}, v = {-2, -3, -4}, L = 10},
 Graphics3D[{Table[{{
Directive[
FaceForm[], 
EdgeForm[
If[{x, y, z} == {0, 0, 0}, 
Directive[Thick, Black], 
Directive[Gray, Dashed]]]], 
Cuboid[{Rational[-1, 2] L + x, Rational[-1, 2] L + y, Rational[-1, 2] L + z}, {Rational[1, 2] L + x, Rational[1, 2] L + y, Rational[1, 2] L + z}]},
     {Blue, Sphere[u + {x, y, z}]},
     {Gray, Sphere[v + {x, y, z}]}},
    {x, {0, L}}, {y, {0, L}}, {z, {0, L}}],
   {Red, Arrow[
     Tube[{u, u + ResourceFunction["PeriodicBoxDisplacement"][u, v, L]}]]}},
  Boxed -> False, Lighting -> "Accent"]
 ]
Out[6]=

Publisher

Wolfram Summer School

Version History

  • 1.0.0 – 05 August 2021

Related Resources

License Information