# Easy Periodic Boundary Conditions for Particle Simulations in MATLAB

Often when working on particle simulations you need to employ Periodic Boundary Conditions. After working out several implementations in MATLAB, I have come to favor the approach shown here. We employ a simple method with a few assumptions that make implementation easier but can be extended. This works great for diffusion or random walk simulations or for your molecular dynamics code.

# Periodic Boundary Conditions or Screen Wrapping

In order to simulate an infinite domain, one can employ Periodic Boundary Conditions (PBCs) in their particle simulations. This screen wrapping is similar to what happens in astroids like games where you leave one side of the domain (screen) and show up on the other along your path of motion and with the same velocity. The video above shows one particle running around with this behavior.

## Assumptions

We make the following assumptions:

• Domain is of equal length in each spatial dimension (square or cube)

• Particles cannot move more than 1 times the boxSize before we check for screen wrapping

• Domain is from 0 to boxSize (not +-boxSize/2)

We also assume the following variables exist in your code to track particles:

• particlePosition: MATRIX of SIZE [Spatial Dimension by Number of particles]

• boxSize: SCALAR  (size of physical domain)

## MATLAB Implementation

MATLAB's logical indexing makes this process a piece of cake. Once I discovered this, life became a lot easier; not to mention logical indexing makes for very readable code. If you haven't already, you should look at the Documented Indexing Options

We really only have to check for when a particle has a position greater than the boxSize or less than 0. Since we assumed the domain is a square or cube, each of these can be done in one line. For boxSize that is different in each dimension, you would need to check each dimension separately. But sticking with the cube/square we can use the following code.

```% Crossed Positive Boundary
% Find the indices of any particle (any Dimension) that is outside boxSize

particlePosition(particlePosition > boxSize) = particlePosition(particlePosition > boxSize) - boxSize ;

% Crossed Zero Boundary
% Find the indices of any particle (any Dimension) that is less than 0

particlePosition(particlePosition < 0) = particlePosition(particlePosition < 0) + boxSize ;```

All we are doing is asking if the particlePosition is greater than the boxSize, then subtract the length of the domain from the position so it shows up on the other side of the screen. For example if your domain is, in one-dimension, 10 units long and you particle moves to 11, we would subtract 10 and place the particle at the position 1.

Similarly, when the particle moves in the other direction, less than 0, we shift the particlePosition by adding the boxSize. So if a particle, similar to the above example, were to move to the position -1, we would add 10 and place it at position 9.

This is a nice and straight forward way to handle any number of particles in any number of dimension in single lines of code that evaluate rather quickly in MATLAB land.

### Questions?

Let me know by leaving a comment or connecting: