• No results found

4.4 Velocity-Vortex Model

5.1.1 Spatial Grid-based Collision Detection

The simplest approach to grid-based collision algorithms [126] [127] is to apply a uniform

grid to the simulation, allowing theidof each particle to be mapped to a specific cell in the

grid. The particle’s position is used to calculate a cell index value which determines which

cell in the grid the particle is currently in. When calculating collisions between particles, only the particles within the same cell as the current particle, as well as those within neighbouring cells, are considered. All other particles are ignored. This achieves significant reduction in the number of particle-particle collisions needing to be calculated within any one timestep. See Figure 5.1 for an illustration of this method.

It should be noted that while a standard uniform grid is sufficient for speed-ups in most cases, it is by no means the only grid-based approach. Harada et. al. [45] proposed an approach using “sliced” two-dimensional planes to divide up the computational space, improving the memory efficiency of the simulation when compared to using a uniform grid by reducing the large number of empty grid cells that would inevitably occur when the particle distribution became irregular within the simulation space. Dobashi et. al. [31] introduce a method of

SECTION 5.1: SPATIAL GRID ALGORITHM 65

overlapping grids, solving problems arising when a uniform grid is not a sufficient enough resolution when dealing with interactions with complex rigid-body objects. These methods are more efficient when used in these specific simulation contexts, but for testing many different simulation models and the comparisons between them, using a uniform grid would provide more reliable comparable results during real-time simulations when it is not certain of the exact behaviour and subsequent distribution of the particles within the simulation space.

Grid-based collision detection approaches can further be improved bysortingall particles

by their cell indices. Oliveira et al. [97] describe three sorting algorithms for use with grid- based particle collision detection methods, including a standard grid-based approach with no adaptive sorting. This approach was also used in Simon Green’s particles demo [41]. Depend- ing on the timestep used, an adaptive sorting algorithm might be preferable, as it would be unlikely that particles would move from one spatial grid cell to another in consecutive itera- tions if the timestep is relatively small. When sorting particle arrays, the sort is based on the cell index, so that particles with the same cell indices are contiguous in the array [97].

The spatial grid algorithm used in this thesis involves firstly selecting grid dimensions such that any object should not intersect more than eight cells in the grid. The size of the cell will vary depending on the simulation that is using the spatial grid. For a spring-mass model in- tending on utilizing a soft-body collision approach, the size of the cell should be no larger than twice the particle’s radius, while if using a Smoothed Particle Hydrodynamics approach, the size of the cell should be the width of the smoothing kernel. Setting the dimensions to these sizes seeks to reduce the number of element interactions to the most efficient amount - ideally, keeping at most one particle per grid cell will produce the minimum number of inter- actions between particles in the local environment. Specifying the cell size is critical to the performance of the system; it is not guaranteed that the mapping of grid cells to particle ids will be unique [126], and every time multiple particles are mapped to the same cell, additional collisions will need to be computed. Ultimately this is unavoidable in numerically-integrated simulations, as the discrete timestep will allow some particles movement into cells which they otherwise would not be able to go, although this can be minimized by specifying a good cell size (as above) and a reasonably small timestep. In general, the smaller the number of particle ids per cell index, the greater the performance of the system.

Once the particle ids and cell indices have been mapped and sorted, they are scanned to

determine the first andlastparticle ids which have the same cell indices. The result is two

arrays of sizedx×dy×dz, one containing the ids of the first- and one containing the ids of

last particles contained in each cell. Given a cell index, it is then possible to find the group of particles in the sorted particle array which are inside the particular cell. When processing collisions, the cell index is calculated using the particle’s position, and additional neighbouring cell indices are also included. The particle ids which are mapped to these cell indices are the particles with which collisions are possible. The overall method of the grid-based collision algorithm just described is shown in Algorithm 5.

The general approach Algorithm 5 takes has been chosen when implementing particle in- teractions in parallel. Although non-parallel implementations could simply use dynamic arrays

Algorithm 5General method for spatial grid particle interactions

Given array P ofnparticles

Given array K of cell index / particle id pair, K.cell indexand K.id

Givendx,dy anddzgrid dimensions

//Calculate the cell index for each particle in the array fori= 0;i<n;i++in parallel do

posx,y,z = cell coordinates for P[i]

cell index=cellfunc(posx, posy, posz) K[i] = (cell index,i)

end for

//Sort particles by cell index so that all particles with same cell index are together for alliin Kin parallel do

Sort K such that K[i].cell index≤K[i+ 1].cell index

end for

Givenfirstandlastarrays of sizedx*dy *dz

Resetfirstandlastto default value D

//Find the particle ids of the first and last particle within each cell //Use shared memory B of blocksize+1

fori= 0;i<n; i++in parallel do

cell index= K[i].cell index

Neighbour indexn index= K[i-1].cell index

Synchronize threads

ifcell index=n indexORi== 0then

first[cell index] =i ifi >0then last[n index] =i end if end if end for

//Interact only between particles in each cell and neighbouring cells fori= 0;i<n;i++in parallel do

posx,y,z = cell coordinates for P[i]

for allneighbouring cellsposx1,y−1,z−1 TOposx+1,y+1,z+1 do

cell index=cellfunc(posx, posy, posz) Letf=first[cell index]

iff =Dthen

Letl=last[cell index] forj=f;j≤l;j++do

ifi=jthen

Interact particles P[i] and P[j] end if

end for end if end for end for

SECTION 5.1: SPATIAL GRID ALGORITHM 67

to store particle data by cell index and then process interactions between particles only in this list and neighbouring cell lists. This is difficult to do in parallel implementations because al- locating memory dynamically on the device is very slow. Using Algorithm 5, there is no need for memory re-allocation, and sorting the particles reduces the search space when processing neighbour interactions. Additionally, it improves the spatial locality of the data, making cached memory access more efficient.