• No results found

6.4 Velocity-Vortex Fire Implementation

6.4.2 Dynamic Spatial Grid

A dynamic spatial grid is required for the fire particle system as the particles are not restricted to a container with set dimensions in a static position. Fire particles simply rise from an emitter body and go where they please. In practice, for a standard simulation case the fire particle system will come to some equilibrium size, where particles will very rarely rise higher than the average lifespan will allow them to and subsequently recreate them back at the emitter body. In this case, a static spatial grid would be appropriate once it reaches this equilibrium state, but before this time - such as when the particles initially rise - it would not be as efficient. Additionally, a static spatial grid cannot account for behaviour in the fire system when external wind forces are applied to the system. The grid needs to account for particles which are blown in the farx- andz-extremities in this case, or else these particles will be classified as outside the grid and will be unable to interact with other particles in a proper fluid-like manner.

/ / Atomic max o f p a r t i c l e p o s i t i o n f o r d yn am ic s p a t i a l g r i d d e v i c e i n l i n e f l o a t fAtomicMax (f l o a t a d d r e s s , f l o a t p o s ){ i n t a d d r e s s a s i = (i n t) a d d r e s s ; i n t o l d = a d d r e s s a s i , a s s u m e d ; do{ a s s u m e d = o l d ; o l d = atomicCAS ( a d d r e s s a s i , assumed , f l o a t a s i n t ( f m a x f ( pos , i n t a s f l o a t ( as su me d ) ) ) ) ; }w h i l e( as s u m e d ! = o l d ) ; r e t u r n i n t a s f l o a t ( o l d ) ; }

Listing 6.5: Using the atomic compare-and-swap to calculate the maximum value for the positions of all particles. This is done for each of thex-y- andz- elements of the particle’s position vector. Additionally, a second implementation of atomicMin is used, which differs only in that the functionf minf is used as the comparison for the atomicCAS instead off maxf. The code compares and swaps the position value in parallel if it is larger than the previous value, and keeps doing this until maximum value has not been modified by any other thread.

The dynamic spatial grid used in this simulation is a simple implementation where addi- tional spatial grid cells are added or removed as the system grows or shrinks. The size of the spatial grid cell is set based on the vorton radius and remains static for the entire simulation, while the dimensions of the grid change over time. Determining the dimensions of the grid is done by calculating the maximum and minimum values of the positions of all particles in

thex-,y- andz-dimensions. The total difference between the maximum and minimum values

for each direction makes up the entire dimensions for the spatial grid. For each x-, y- and

z-dimension in the grid, it is divided by the corresponding grid cell size calculated earlier to

obtain the number of cells in each dimension in the grid. This will change dynamically over time as the particles in the system move.

Calculating the maximum and minimum values for all particle positions can be very costly performance-wise for large particle systems. For this reason a parallel method is implemented

to speed up this process, using theatomicM axandatomicM infunctions built into CUDA.

However, these typically do not work with floating point numbers (positions are stored as floating point values) so a work around must be used using the atomic compare-and-swap as described in the CUDA Toolkit Documentation [91]. This workaround is shown in Listing 6.5. In practice, the atomic maximum and minimum functions should not need to compare very many times, as the number of particles which increase or decrease the maximum or minimum values each timestep will be very few. However, this is only the case if the spatial grid is only allowed to increase the dimension size and not decrease it. In order for the grid to decrease as well, the maximum and minimum values need to be reset and the comparison would be performed many more times on average. In the case of a steady flame simulation, the first case would be fine to implement as the grid dimensions would not decrease in size beyond some equilibrium size, as discussed earlier. However, if wind effects are introduced, the spatial grid must be able to increase and decrease its maximum and minimum values, because as the flame

SECTION 6.4: VELOCITY-VORTEX FIRE IMPLEMENTATION 103

is blown left and right, backwards and forwards, the maximum values in those directions must decrease opposite to the direction of the wind, and vice versa for the minimum values. If this does not occur, the spatial grid becomes unnecessarily large relative to the number of particles in the system, decreasing the effectiveness of the spatial indexing and affecting the vorticity diffusion between cells at the edges of the flame.