#include "NumMeth.h" #include "SortList.h" void sorter( Matrix& x, double L, SortList &sD ) { // sorter - Function to sort particles into cells // Inputs // x Positions of particles // L System size // sD Object containing lists used in sorting // Output // sD Object containing lists used in sorting //* Find the cell address for each particle int ncell = sD.ncell; int npart = sD.npart; int ipart, *jx; jx = new int [npart+1]; for( ipart=1; ipart<=npart; ipart++ ) { int j = (int)(x(ipart)*ncell/L) + 1; jx[ipart] = ( j <= ncell ) ? j : ncell; } //* Count the number of particles in each cell int jcell; for( jcell=1; jcell<=ncell; jcell++ ) sD.cell_n[ jcell ] = 0; for( ipart=1; ipart<=npart; ipart++ ) sD.cell_n[ jx[ipart] ]++; //* Build index list as cumulative sum of the // number of particles in each cell int m=1; for( jcell=1; jcell<=ncell; jcell++ ) { sD.index[jcell] = m; m += sD.cell_n[jcell]; } //* Build cross-reference list int *temp; temp = new int [ncell+1]; // Temporary array for( jcell=1; jcell<=ncell; jcell++ ) temp[jcell] = 0; for( ipart=1; ipart<=npart; ipart++ ) { jcell = jx[ipart]; // Cell address of ipart int k = sD.index[jcell] + temp[jcell]; sD.Xref[k] = ipart; temp[jcell] = temp[jcell] + 1; } delete [] jx; delete [] temp; }