This HTML automatically generated with rman for NEMO
Table of Contents


hackdens - local density estimator using tree algorithm


hackdens in=snapshot out=image [parameter=value]


hackdens calculates the local density in the configuration space using the hierarchical N-body algorithm (J. Barnes and P. Hut, Nature 324, 446). For small N or systems with complex geometry snapdens(1NEMO) may be preferred. For even more performance, density(1falcON) can be used.

The density can be writtin in the slot normally used for Potentials (the default), or if write_at_phi=f be written to a tag named Density.


The following parameters are recognized in order; they may be given in any order if the keyword is also given. Use --help to confirm this man page is up to date.
Input data will be read from in-file in snapshot format.
Output data are written to out-file in snapshot format.
Number of neighbors used to determine the local density. The default of 6 is the recommended value by Casertano & Hut (1985) [default: 6].
Initial radius to search the neighbors. Hackdens adaptively change this search radius during the calculation. Therefore it is definitely required that particles in the snapshot are arranged so that the neighboring particles have similar local density. (See NOTES) [default: 0.1].
Logical if the density data is written in the "Potential" slot of the snapshot file. If false, the density will be written with tag "Density". [default: f]
Intial size of the root box. Box will automatically expanded to keep all particles in it [defaults:4.0]
If given, the coordinates of the lower left corner of the initial box. If not given, the center of the root box will lie at the origin. [defaults:none]
Output control options, specified as a comma-separated list of keywords. Currently recognized keywords are: mass, phase: output mass, phase space data with major data outputs [defaults:mass,phase].
Ratio of cells to bodies, used when allocating cells [defaults:0.9].
nudge overlapping particles with this positional dispersion [0]
Logical if print out the number of particles processed during the calculation [defaults:f].
write density, or alternatively distance to Kth particle. Default: t
3D or 2D computation [Default: 3]
slower N-squared direct density computation. Default: f
Mode of normalization. Earlier (2.x and before) versions would scale equal mass systems by nbody, the current default is norm=1 which normalizes by nbody, so densities from the slower snapdens(1NEMO) agree with hackdens. Default: 1


Since the search radius is adaptively changed during the calculation, the local density of particles which are close in the position in the snapshot file should be similar.

Ideally, it is best if particles are sorted by the local density itself. Since the local density is not available, the second choice is to sort particles with some quantity that has strong correlation with the local density. For example, if the potential is available, sort particles by potential.

In many cases the system is roughly spherically symmetric. That means the density has strong correlation with the distance from the center of the cluster. Then sorting particles by the distance from the center will improve the speed significantly.


The following example takes an N-body snapshot, sort the particles in the order of the distance from the C.M., and calculate local density.

snapcenter nbody.dat nbody.centered
snapsort nbody.centered nbody.sorted
hackdens nbody.sorted nbody.density

One can also weight the particles by its potential to some power. For this we create a Plummer sphere, without centering, and compute the center in two ways:

mkplummer - 100 zerocm=f | snapsort - - rank=r > p.dat
snapcenter p.dat . report=t
hackdens p.dat - | snapcenter - . weight=dens report=t
hackforce p.dat - | snapcenter - . weight="-phi*phi*phi" report=t
hackforce p.dat - | snapcenter - . weight="phi*phi*phi*phi" report=t

Note that in this case the Plummer sphere should have the correct mathematical center, and not centered. This snippet of code is expanded in a script $NEMO/scripts/csh/, in which these different methods can also be applied to different models, as expanded in the following table.

nbody=100 models: Plummer King1 King3 King5 DeVauc
snapcenter on m: 0.102 0.029* 0.030* 0.033 0.299
snapcenter on dens: 0.060 0.039 0.036 0.030 0.030*
snapcenter on phi: 0.053
snapcenter on phi^2: 0.050*
snapcenter on phi^3: 0.052 0.032 0.030 0.026* 0.035
snapcenter on phi^4: 0.054 0.034 0.032 0.027 0.031
snapcenterp: 0.080
snapmnmx on min phi: 0.122
snapmnmx on m 50% phi: 0.058
snapmnmx on m 33% phi: 0.063 0.053
snapmnmx on m 25% phi: 0.067
rockstart on m: 0.082:

A (*) denotes the best method for that model.

Although for a Plummer sphere the best method is more than twice as good than the worst method, this does not apply to other models.

density(1falcON) is considerable faster, about twice with their K=32 default.

mkplummer - 100000 |\ snapscale - - mscale=10 |\
snapsort - - |\
density - - give=mxvr |\
snapshell - ’10**[-4:4:0.1]’ dens |\
tabmath - - ’log(%1),log(%4)’ all |\
tabplot - 1 2 -4 4 -14 2 line=1,1 point=2,0.1


mkplummer - 100000 |\ snapscale - - mscale=10 |\
snapsort - - |\
hackdens - - |\
snapshell - ’10**[-4:4:0.1]’ dens |\
tabmath - - ’log(%1),log(%4)’ all |\
tabplot - 1 2 -4 4 -14 2 line=1,1 point=2,0.1


The local density is calculated using (neib-1)th neighbor. Calculation can become very slow if the input snapshot is not well-formed. There should exist an option that forces the density will be written in the "Aux" slot of the snapshot file. Current version does not use standard get_snap/put_snap macros. KEY and AUX will be lost from the output snapshot.

The density in snapdens(1NEMO) and density(1falcON) are in the correct units, but in hackdens(1NEMO) seem to be off by nbody for equal mass particles. For example, a Plummer sphere in virial (nbody) units should have a central density

    (32/(3.pi^2))^2 ~ 1.168

See Also

hackforce(1NEMO) , snapdens(1NEMO) , snapcenterp(1NEMO) , density(1falcON) , snapshell(1NEMO) - Casertano & Hut (1985)


@ads 1985ApJ...298...80C


N=4096, merger remnant

Presorted snapshot: 5 minutes on Sun-3/60, particles are presorted by the distance from the center of mass of the system.

Random snapshot: 60 minutes Sun-3/60


Jun Makino


2-Dec-88      V1.0:Created      Jun
6-jul-89    doc updated?    Jun
23-oct-90    doc updated    Peter
18-jul-92    printf -> dprintf to make it pipable    Peter
24-may-02    fixed running out of bits for large-N     PJT
11-aug-2022    added example hackforce vs. hackdens    PJT
21-sep-2023    added direct=    PJT
11-oct-2023    V3.0 added norm=1 as a new default    PJT
12-oct-2023    V3.0 proper mass/nbody scaling     PJT

Table of Contents