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:
YouTube Channelhttps://www.youtube.com/user/NicholasMSchneider
Twitterhttps://twitter.com/NMSchneider 
Facebookhttp://facebook.com/Schneider.NicholasM
LinkedInhttp://www.linkedin.com/in/nmschneider/
Google+https://plus.google.com/+NicholasSchneiderPennRIT/

Comment

Nicholas M Schneider

Nicholas M Schneider is a 2010 graduate from the Kate Gleason College of Engineering who is now a Doctoral Candidate at the University of Pennsylvania. Originally from an obscure town south of Buffalo, New York, he attended the Rochester Institute of Technology where he received concurrent Bachelor of Science and Master of Science degrees. While there he had a number of Co-ops including a six month stay as a Design Engineer at Lockheed Martin and Research positions with Dr. Satish Kandlikar. Nicholas currently works with Dr. Haim H Bau in the field dubbed “in situ electron microscopy of liquid systems” where he studies applications in energy and biological systems. Outside of the lab, Nicholas Schneider is a Graduate Associate in Rodin College House and enjoys running (he ran his second Philly Marathon this past November), cooking, baking, reading, and justifying his coffee addiction by making it a hobby.