Function Repository Resource:

PeriodicBoxDistance

Source Notebook

Evaluate the shortest distance between two points in a box with periodic boundary conditions

Contributed by: Matt Kafker

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

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

Details

ResourceFunction["PeriodicBoxDistance"] works for boxes in any number of spatial dimensions.

Examples

Basic Examples (3) 

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

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

PeriodicBoxDistance reverts to Euclidean distance when points are close by within the box:

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

Parallel points on the boundaries of the box have zero separation:

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

Scope (1) 

PeriodicBoxDistance also works in higher dimensions:

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

Neat Examples (1) 

Compare PeriodicBoxDistance from the origin with Euclidean distance in two dimensions:

In[5]:=
Plot3D[{EuclideanDistance[{0, 0}, {x, y}], ResourceFunction["PeriodicBoxDistance"][{0, 0}, {x, y}, 5]}, {x, -5,
   5}, {y, -5, 5}, Sequence[PlotStyle -> {
Opacity[0.1], 
Opacity[1]}, PlotLegends -> {"Euclidean Distance", "Periodic Box Distance"}]]
Out[5]=

Publisher

Wolfram Summer School

Version History

  • 1.0.0 – 24 August 2021

Related Resources

License Information