Box guide

The box geometry is the one of a simulation snapshot, or of a cube cut from one: tracers in a rectangular volume, in comoving Cartesian coordinates, with a single mean particle separation (mps). This page explains how to run a search in this geometry, from the C++ library, from Python and with the executables, how to choose its parameters and how to read what it returns. The method explains what each step computes and why; the C++ and Python references give every function in full.

Which function

Starting from

C++

Python

a tracer catalogue: reconstruction, optional correction of redshift-space distortions, search

bitvf::runBox()

bitvf.run_box()

an otswap box reconstruction, kept in memory or read from otswap’s complete or mean displacement file

bitvf::findVoidsBox(), the otswap::Result overload

bitvf.find_voids_box() with the otswap.Result

displacement arrays, from any source

bitvf::findVoidsBox()

bitvf.find_voids_box()

a divergence grid kept or written earlier, to search it again with another smoothing

bitvf::findVoidsBoxFromDivergence()

bitvf.find_voids_box_from_divergence()

any field at the centres of a box lattice, with no tracers

bitvf::findVoidsBoxFromField()

bitvf.find_voids_box_from_field()

a field on a grid, split into basins only

bitvf::watershedBox()

bitvf.watershed_box()

a grid to share among several searches

bitvf::boxGrid()

bitvf.box_grid()

The pipeline is the simplest start. Calling otswap and the search separately keeps the reconstruction, so that it can be written, or searched again with other parameters without being computed again. The box examples show both, and the box executables run them from a parameter file.

Each search returns a result holding the catalogues of the voids and of the overdense basins, with what it counted. Two larger products are kept only when asked for, since they take memory: the divergence grid, with keepDivergence (Python keep_divergence), and where each tracer ended up, its cell, its basin and its distance from the basin’s centre, with keepAssignment (Python keep_assignment).

A typical run

The runs below are the pipeline on the halos of a simulation box observed in redshift space: otswap reconstructs them with 8 realizations and a fixed seed, drawing the randoms uniformly in the box, the redshift-space distortions along the y axis are corrected, and the voids are searched on the second, real-space, reconstruction and on the first as well. They are the core of the full reconstruction example, which also locates the data folder data; the example page shows it whole. The excerpts end with the writers, one file per product and pass.

# Comoving Cartesian coordinates in Mpc/h, shape (N, 3), in redshift
# space with the line of sight along y
tracers = otswap.io.read(data / "box_halos_redshift_space.dat", [0, 1, 2]).values

print(f"Tracers: {len(tracers)}")

# --------------------------------------------------
# ------------ Configure the pipeline --------------
# --------------------------------------------------

# The reconstruction, for both passes: 8 realizations, a convergence
# threshold of 1e-2 and a fixed seed. No randoms are given, so bitvf
# draws 8 per halo, uniformly in the box, from a seed derived from this
# one (Result.random_seed)
ot_config = otswap.Config(n_realizations=8, convergence=1e-2, seed=12345)

# The halos fill a cube of side 400 Mpc/h, [269, 669) on each axis
box = ((269.0, 269.0, 269.0), (669.0, 669.0, 669.0))

# The correction: the line of sight (axis 0, 1 or 2 for x, y or z), the
# box's redshift, at which the growth rate is read from the distance
# table below, and the tracers' linear bias. sigma is the width of the
# Gaussian average of the displacements around each tracer, in Mpc/h:
# 10 Mpc/h is a reasonable starting value, but the best value depends on
# the sample and should be checked in each analysis. zspace_voids also
# searches the redshift-space pass
rsd = bitvf.BoxRsd(axis=1, redshift=1.0, bias=1.91255, sigma=10.0, zspace_voids=True)

# The void search, on both passes: the default smoothing of 1 mps and the
# default cell size. The divergence grid is kept, with the field before
# and after smoothing, so that it can be written and searched again later
# without the reconstruction (see the field example)
voids = bitvf.Config(keep_divergence=True, verbosity="detailed")

config = bitvf.BoxConfig(ot=ot_config, limits=box, rsd=rsd, voids=voids)

# The correction needs only the linear growth rate f at the box's
# redshift. The table computes it from Omega_m, h, w0 and wa of a flat
# cosmology
distances = otswap.DistanceTable.flat(0.3186, 0.67, w0=-1.0, wa=0.0)

# ----------------------------------------------
# ------------ Run the pipeline ----------------
# ----------------------------------------------

run = bitvf.run_box(tracers, config=config, distances=distances)

# ----------------------------------------------
# ------------ Summarize the result ------------
# ----------------------------------------------

# run.voids is the real-space search, whose per-halo outputs keep one
# row per input halo, and run.zspace_voids the redshift-space one.
# Redshift-space distortions stretch voids along the line of sight and
# change their number
real, zspace = run.voids, run.zspace_voids

print(f"Randoms drawn with seed {real.random_seed}")
print(f"Halos left out of the second pass: {real.counts.tracers_uncorrected} uncorrected, "
      f"{real.counts.tracers_outside_grid} moved outside the grid")
print(f"Voids in redshift space: {zspace.voids.n_basins}")
print(f"Voids in real space: {real.voids.n_basins}")

# ------------------------------------------
# ------------ Write the output ------------
# ------------------------------------------

# The voids and the divergence grid of each pass. The redshift-space
# files have zspace_ in their names: voids_zspace_box.dat and
# divergence_zspace_box.dat, then voids_box.dat and divergence_box.dat.
# The writers are the C++ library's, so the files are the C++ example's,
# byte for byte
product = bitvf.io.Product
bitvf.io.write_voids(bitvf.io.file_name(product.VOIDS, output, "box.dat", zspace=True), zspace)
bitvf.io.write_divergence(bitvf.io.file_name(product.DIVERGENCE, output, "box.dat", zspace=True), zspace)
bitvf.io.write_voids(bitvf.io.file_name(product.VOIDS, output, "box.dat", zspace=False), real)
bitvf.io.write_divergence(bitvf.io.file_name(product.DIVERGENCE, output, "box.dat", zspace=False), real)
// Comoving Cartesian coordinates in Mpc/h, x, y, z for each halo, one
// row after the other, in redshift space with the line of sight along y
const std::vector<double> tracers = otswap::io::read(data + "/box_halos_redshift_space.dat",
                                                     {"0", "1", "2"}).values;

std::cout << "Tracers: " << tracers.size() / 3 << std::endl;


// --------------------------------------------------
// ------------ Configure the pipeline --------------
// --------------------------------------------------

// The reconstruction, for both passes: 8 realizations, a convergence
// threshold of 1e-2 and a fixed seed. No randoms are given, so bitvf
// draws 8 per halo, uniformly in the box, from a seed derived from
// this one (Result::randomSeed)
bitvf::BoxConfig config;
config.ot.nRealizations = 8;
config.ot.convergence = 1.e-2;
config.ot.seed = 12345;
config.ot.log = &std::cout;

// The halos fill a cube of side 400 Mpc/h, [269, 669) on each axis
config.limits = otswap::BoxLimits{{269., 269., 269.}, {669., 669., 669.}};

// The correction: the line of sight (axis 0, 1 or 2 for x, y or z),
// the box's redshift, at which the growth rate is read from the
// distance table below, and the tracers' linear bias. sigma is the
// width of the Gaussian average of the displacements around each
// tracer, in Mpc/h: 10 Mpc/h is a reasonable starting value, but the
// best value depends on the sample and should be checked in each
// analysis. zspaceVoids also searches the redshift-space pass
bitvf::BoxRsd rsd;
rsd.axis = 1;
rsd.redshift = 1.;
rsd.bias = 1.91255;
rsd.sigma = 10.;
rsd.correction.log = &std::cout;
rsd.zspaceVoids = true;
config.rsd = rsd;

// The void search, on both passes: the default smoothing of 1 mps and
// the default cell size. The divergence grid is kept, with the field
// before and after smoothing, so that it can be written and searched
// again later without the reconstruction (see the field example)
config.voids.keepDivergence = true;
config.voids.verbosity = otswap::Verbosity::Detailed;
config.voids.log = &std::cout;

// The correction needs only the linear growth rate f at the box's
// redshift. The table computes it from Omega_m, h, w0 and wa of a flat
// cosmology
const otswap::DistanceTable distances(0.3186, 0.67, -1., 0.);


// ----------------------------------------------
// ------------ Run the pipeline ----------------
// ----------------------------------------------

const bitvf::BitVFResult run = bitvf::runBox(tracers, {}, distances, config);


// ----------------------------------------------
// ------------ Summarize the result ------------
// ----------------------------------------------

// run.voids is the real-space search, whose per-halo outputs keep
// one row per input halo, and run.zspaceVoids the redshift-space one.
// Redshift-space distortions stretch voids along the line of sight and
// change their number
const bitvf::Result& real = run.voids;
const bitvf::Result& zspace = *run.zspaceVoids;

std::cout << "Randoms drawn with seed " << *real.randomSeed << std::endl;
std::cout << "Halos left out of the second pass: " << real.counts.tracersUncorrected << " uncorrected, "
          << real.counts.tracersOutsideGrid << " moved outside the grid" << std::endl;
std::cout << "Voids in redshift space: " << zspace.voids.nBasins << std::endl;
std::cout << "Voids in real space: " << real.voids.nBasins << std::endl;


// ------------------------------------------
// ------------ Write the output ------------
// ------------------------------------------

// The voids and the divergence grid of each pass. The redshift-space
// files have zspace_ in their names: voids_zspace_box.dat and
// divergence_zspace_box.dat, then voids_box.dat and divergence_box.dat
using bitvf::io::Product;
bitvf::io::writeVoids(bitvf::io::fileName(Product::Voids, output, "box.dat", true), zspace);
bitvf::io::writeDivergence(bitvf::io::fileName(Product::Divergence, output, "box.dat", true), zspace);
bitvf::io::writeVoids(bitvf::io::fileName(Product::Voids, output, "box.dat", false), real);
bitvf::io::writeDivergence(bitvf::io::fileName(Product::Divergence, output, "box.dat", false), real);

The real-space voids are in run.voids, the redshift-space ones in run.zspaceVoids (Python run.zspace_voids). Without rsd, the pipeline reconstructs once and searches that reconstruction. To search a reconstruction kept from otswap instead, pass it to findVoidsBox (find_voids_box), as the box example does.

The executable runs the same pipeline from a parameter file, with no code to write:

bitvf examples/executable/box.ini

The box executables page lists its keys, and the commented file examples/executable/box.ini sets each of them.

Coordinates and units

  • Cartesian coordinates and every length (the mps, the box limits, the displacements, the smoothing width, the radii) are in coordinate units: the unit of the tracers given. bitvf assumes none, and its files give lengths no unit.

  • In C++ the arrays are flat and row-major, [object][xyz], with displacements [realization][tracer][xyz]; the array layout section of the reference lists every array of a result.

  • In Python a coordinate array has shape (N, 3), converted to float64. Every per-cell array of a box grid has shape (nx, ny, nz), C order, so that a[i, j, k] is cell (i, j, k). Flags are bool.

The box and its limits

The box of a pipeline is the one given in BoxConfig::limits (Python limits) or, without it, the smallest box containing the tracers and, when given, the randoms. It is fixed once, before the reconstruction, and shared by every later step. For a simulation snapshot, give the box of the simulation.

Box limits, given to the search or recorded in the reconstruction, act like a mask: a tracer outside them keeps its row in every per-tracer output but takes no part, flagged in TracerAssignment::inBox (Python in_box). A point on a face is inside. When searching displacement arrays, pass the limits the reconstruction used: a valid displacement ending outside the grid is an error.

Mean particle separation and cell size

The mps sets the scale of the search: the cell size, the smoothing in mps, the sphere of delta2Mps and the mean density of the density contrasts. bitvf takes it from the reconstruction, otswap::Result::mps, or from the caller (BoxConfig::mps); it never computes its own.

The cell side is divergenceCellSize (Python divergence_cell_size) times the mps. Left empty, it is min(1, (20 / R)^(1/3)) mps, R the number of realizations: 1 mps up to 20 realizations, smaller above. A search on a mean displacement file counts as one realization, so its default is 1. Smaller cells resolve smaller voids but hold fewer displacements each, so the divergence is noisier. The value used is in Result::config.

The grid

The grid is made of cubic cells covering the box limits of the search, those of the reconstruction (otswap::Result::box) or, for displacement arrays without limits, the tracers and the endpoints of their valid displacements. It is padded by 0.1 mps on each side, and has the same number of cells on the three axes, so it may overhang a box that is not a cube. It is not periodic, even for a periodic simulation: a cell on a face has fewer neighbours, and a basin touching the faces is flagged.

Each cell has 26 neighbours, the cells sharing a face, an edge or a corner with it. A point on a face between two cells belongs to the upper one.

A grid can be reused: the grid argument of findVoidsBox takes one from an earlier result (Result::divergence), from boxGrid (Python box_grid) or from a divergence file (bitvf::io::readDivergence, Python bitvf.io.read_divergence). The search then uses its cells and its mps, and excludes the tracers outside it, flagged in Result::outsideGrid. Several searches on one grid give catalogues on the same cells.

Smoothing

Config::smoothing (Python smoothing) sets the width of the Gaussian and its unit, mps (the default) or coordinate units; 1 mps, the default, is a reasonable start, and a width of 0 turns the smoothing off. In a box, each cell takes the mean of the cells within 3.5 widths along each axis, a cube rather than a sphere.

findVoidsBoxFromDivergence (Python find_voids_box_from_divergence) searches a divergence again with another width, from a grid kept with keepDivergence or read from a divergence file, without the reconstruction.

The correction of redshift-space distortions

With BoxConfig::rsd (Python rsd), runBox takes the tracers as redshift-space positions: after the first reconstruction they are moved along the line of sight with otswap’s correction, reconstructed again with the same randoms and seed, and searched in real space on the grid of the first pass, with its mps. The method explains the correction.

bitvf::BoxRsd (Python bitvf.BoxRsd) sets the axis of the line of sight, the redshift of the box and the linear bias of the tracers, all fixed by the simulation, and the width of the average of the displacements over neighbouring tracers: 10 Mpc/h is a reasonable start for coordinates in Mpc/h, to be checked in each analysis. The distance table given to the run carries the growth rate. zspaceVoids (Python zspace_voids) also searches the first, redshift-space, reconstruction.

Tracers the correction leaves unmoved, or moves outside the grid, keep their rows and take no part in the second pass, flagged in Result::uncorrected and Result::outsideGrid.

Reading and checking the results

The catalogues of voids and overdense basins have the same columns, one row per basin, in the order of the seed cells, whatever the number of threads. The method defines each one. In Python, the columns are NumPy arrays with snake-case names, such as result.voids.radius, radius_ext, delta_av, delta_2mps, edge_flag and contamination; names side by side gives each C++ name.

edgeFlag marks the basins that touch the faces of the grid: 1 when only the extended basin does, 2 when the core does. The flow is not known beyond the faces, so for statistics that need whole voids keep those with edgeFlag 0, or 0 and 1. With keepDivergence, the smoothed weight of each cell (weightSmoothed, Python weight_smoothed) shows how much of its smoothed divergence comes from displacements: near 0, it comes mostly from cells that no displacement reached, and should be read with care.

Writing the results

A search writes no file. The writers of bitvf::io (Python bitvf.io) write the four products of a result, each to its own file: writeVoids and writeOverdense, one row per basin; writeDivergence, one row per cell, which needs keepDivergence; and writeGalaxies, one row per tracer, which needs keepAssignment. fileName (Python file_name) gives the conventional names, such as voids_<name> and, for a redshift-space pass, voids_zspace_<name>. The extension chooses the format: FITS for .fits, .fit and .fits.gz, ASCII otherwise. Each file records the main parameters of the search that made it as keywords of its header, such as the smoothing and the cell size.

The box file formats page lists every column and keyword.

Reproducibility and threads

Given the same inputs and a seed other than 0, every result is the same bit for bit whatever the number of threads, on every supported platform and compiler; only the elapsed times differ. A seed of 0, otswap’s default, is replaced by one drawn at the start of the run and recorded in otswap’s result (otswap::Result::config), so that any run can be repeated; setting a seed of your own saves reading it back. The number of threads follows OMP_NUM_THREADS.

A box pipeline given no randoms draws them uniformly in the box, as many as the realizations times the tracers inside it, from a seed derived from the reconstruction’s, recorded in Result::randomSeed (Python random_seed) and in the files as RANDSEED.

What it prints

Config::verbosity (Python verbosity) sets the report, at otswap’s levels: Silent prints nothing; Normal, the default, prints what the selection left out and one line per call with its time; Detailed also prints one line per step of the search. Each line starts with bitvf: and is written as soon as its step finishes, to Config::log in C++ (std::clog by default), to sys.stdout in Python. Every count printed can also be read from the result’s counts. In Python, Ctrl+C stops a search at its next report line.

At Normal verbosity a box search prints the tracers left out by the box limits or a reused grid, when there are limits or a grid, and a last line with the number of voids and overdense basins and the time; bitvf::Config lists every line.

Errors

Every failure raises bitvf::Error (Python bitvf.Error), with a message that names the quantity and the rule broken; an error raised by otswap inside a bitvf call has a message starting otswap: . Arguments are checked before any work, so that a wrong value fails at once rather than after the reconstruction. bitvf::Error is unrelated to otswap::Error: catching one does not catch the other.