The N-Body Tree-Code
CHAPTER 3. THE N-BODY TREE-CODE
a mesh or grid which is used for the potential computation. The positions of the particles are used to nd the density at the grid points, the potential is computed at the grid points, and then the potential is extrapolated from the grid points back to the particles. Ecient techniques involving the use of discrete Fourier transforms have been developed for the potential computation step of this method. PM techniques may be inappropriate for inhomogeneous systems with large density contrasts, because if some volume of space is devoid of particles, the corresponding portion of the grid will be super uous. Also, the overall resolution is limited by the grid size. This has led to the development of hybrid techniques which compute both the short-range force between nearby particles and the long-range force on the grid, as well as adaptive grid techniques which increase the grid resolution in denser regions. The Barnes-Hut algorithm described below is in the spirit of these latter techniques.
2. The Barnes-Hut Algorithm
Codes employing the Barnes-Hut (1986) algorithm are often referred to as \tree codes" because a tree data structure is usually employed. Rather than use a simple, xed spatial gridding, the tree algorithm uses a many-level partitioning hierarchy of cells within cells which is organized using a tree data structure. Computing the force on a given particle involves traversing the tree to increasing depth to determine which particles to treat directly and which to include as members of distant cells. The evolutions presented here were carried out using a modied version of a a fully vectorized tree code (Hernquist 1987, Hernquist 1990, Makino 1990) kindly supplied by Lars Hernquist. The great advantage of using a tree-code is that it reduced the task of computing the potential of an N-body
system to an O(N log N) operation, while at the same time being well suited for
simulating inhomogeneous systems with little spatial symmetry such as galaxy clusters. Hernquist's (1987, 1990) tests indicate that the tree-algorithm is the most ecient method for simulating systems which do not admit a high degree of spatial symmetry when N exceeds a few thousand. Thus is it the algorithm of choice for our investigation.
In this code, space is partitioned into an hierarchy of cubical cells. The `root' of the tree is a cell which contains all of the particles. This root has eight `branches' corresponding to the bisection of the root cell along each dimension, creating eight smaller cubical subcells. This subdivision is iterated, corresponding to further branching of the tree. The process is continued for each cell that contains more than one particle; thus the branching of the tree ends at `leaves' corresponding to individual particles.
To compute the gravitational force on a given test particle, the contents of a cell can be approximated as a point with mass equal to the sum of all the masses inside the cube and position equal to their center of mass; this approximation is more accurate the farther the cell is from the test particle. If greater accuracy is needed, the cell can be divided into its eight smaller subcells. The force calculation thus involves a tree traversal; each node is tested against some tolerance criterion, and either that cell is used or one moves further along the tree. Using the standard Barnes-Hut algorithm, a cell is used only if
d > s= (3:2)
where d is the distance from the particle to the center of mass of the cell, s is the size of the cell, and is a xed tolerance parameter; if the cell does not satisfy this criterion, the eight subcells are examined.
CHAPTER3. THEN-BODYTREE-CODE 22
The positions and velocities are updated using a standard leapfrog integrator, which is accurate to second order in the time step. Since long-range forces are computed approximately, there is no advantage in using a higher-order integrator. Another consequence of approximation is that energy and momentum will not be exactly conserved, since the approximate forces are not guaranteed to be symmetric. However, for reasonable values of and of the time step, this source of nonconservation is comparable that to due to truncation errors in the time integrator (Hernquist 1987).
3. Modications to Hernquist's Code
In most N-body simulations it is not desirable for the particles to behave like point masses, since one is sampling a more continuous distribution. Therefore most simulations use a \softened potential". For example, one popular method is to alter equation 3.1 to ~Fij = ( Gmimj j~xj ? ~xij 2+2) 3=2(~xj ?~xi) (3:3)
where is known as the softening length. This length gives a lower limit on the scale of particle clustering. Equation 3.3 can be derived analytically as the force between a point mass and a Plummer sphere with a core radius of .
In the tree code, gravitational interactions for separations of less than 2 are softened using a cubic spline kernel. For separations larger than 2 the force is that of equation 3.1. For smaller separations the force is equation 3.1 evaluated at r = 2 and multiplied by a factor which is less than unity. This factor is a function of on the separation; details are given in Hernquist and Katz (1989) or Dyer and Ip (1993). If the force is being calculated for two particles with dierent softening lengths, the larger is used.
The use of softening in the tree code can introduce a source of error (Hernquist 1987). When using equation 3.2, it is possible that a cell will not be opened even though it contains a particle within 2 of the particle for which the force is being calculated; i.e., a sphere of radius 2 centered on this latter particle could still intersect with some portion of the cell's volume even though d > 2s. If this happens the interaction of the two particles will not be properly softened. Therefore, the above criterion was replaced by one which guarantees that only cells which could not possibly contain particles within 2 of the particle under consideration are used, namely:
d > 2 +p
3s (3:4)
Requiring that this inequality holds ensures that the entire cell is further than 2 away from the particle, even if the center of mass is in the furthest corner of the box. If more than one softening length is used in a simulation, the longest is the one used in equation 3.4.
It might be asked if there are situations where cells would be opened when using equation 3.2 but not equation 3.4; this would make the criterion used here less accurate than the Barnes-Hut method. In such a case d < s= but d > p
3s; this is not possible for > 1=p
3'0:577. Thus the method used here is at least as
accurate as the Barnes-Hut method with = 0:577 and generally more so, at the expense of having to calculate a greater number of force terms. This becomes more obvious if we rewrite equation 3.4 as
d?2 > s=(1= p
3) (3:5)
Useful values for the tolerance parameter are 0:3 1:0 (Hernquist 1987), since
must be small enough to provide accurate force calculations but not so small that an unreasonable number of force terms is included. While employing equation 3.4
CHAPTER3. THEN-BODYTREE-CODE 24
does increase the number of force terms used, this method avoids the introduction of an additional loop in the frequently used subroutine which creates interaction lists; this routine already accounts for almost half the CPU time consumed by the code. Also, using = 1=p
3 avoids the pathological situation described by Salmon and Warren (1994), which can lead to large systematic errors.
Hernquist specically optimized the code for the Cray Y-MP environment using Cray-specic routines. These were eliminated so that the code could run on several dierent machine architectures. However, with newer Fortran compilers the code still vectorizes, and this change actually had the eect of improving the performance of the code on a Cray Y-MP. Quadrupole corrections to the gravitational force were not used, since they would not signicantly increase the accuracy (Hernquist 1987), so that portion of the code was excised. These changes improved the speed of the code by roughly a factor of two and reduced the memory requirements.