In fluids modeling, a really easy way to build an model of a scalar (e.g., a polutant or chemical) that is transported by a flowfield is using a particle tracking model.
Here, you have millions of finite particles, with x and y locations (and possibly mass). building code to advect the particles by the flow-field and diffuse them based on a random walk Gaussian diffusion model is pretty trivial.
What's hard is when you want to actually make sense of these millions of particles: you need to convert them into a concentration. How many particles are in a unit of space? Binning codes are usually pretty slow, and can be the major hold-up in a particle tracking model.
That's why I was stoked yesterday to find MATLAB's accumarray function. It offers a quick way to bin particles, and operates slightly (10-15%) faster than my old bench-mark binning code. Here's my binning function for anybody who's interested:
function [Conc]=bin_Particles(Xpos,Ypos,Ms,L,dx) %-------------------------------------------------------------------------% % PDF2d(C1, C2, nBins) % Written By: Mike Soltys (www.MikeSoltys.com) % Last modified: 4/25/13 % % Required Inputs: % Xpos: 1 x n vector of particles X locations. % Ypos: 1 x n vector of particles y locations. % Ms: 1 x n vector of particles masses % % L: The domain will reach from -L to L in both x and y % dx: The domain will be broken into dx sized bits. % % output: % Conc: a (2L/dx+1) X (2L/dx+1) matrix with concentrations % %-------------------------------------------------------------------------% %Detrmine Bin Locations Xs=round(( Xpos+L)*L/dx )+1;Xs(Xs<1)=1;Xs(Xs>(1+2*L/dx))=(1+2*L/dx); Ys=round(( Ypos+L)*L/dx )+1;Ys(Ys<1)=1;Ys(Ys>(1+2*L/dx))=(1+2*L/dx); %Use accumarray to create matrix. Conc(:,:)=accumarray([Ys, Xs],Ms); end