Generator

class randomfield.generate.Generator(nx, ny, nz, grid_spacing_Mpc_h, num_plot_sections=4, cosmology=None, power=None, verbose=False)[source] [edit on github]

Bases: object

Manage random field generation for a specified geometry.

The constructor allocates the (potentially large) memory buffer required to store field values but does not initialize it. You will normally initialize the field using generate_delta_field().

The optional classy package is used to calculate the power spectrum of an arbitrary cosmology on the fly. If classy is not installed, you can either use the default Planck13 cosmology (do not set the cosmology or power parameters) or else specify your own cosmology (using create_cosmology for simple cases or else astropy.cosmology) and provided your own externally calculated tabulated power spectrum (from CAMB, for example).

The optional matplotlib package is used to draw 2D slices of the 3D field after each processing step. The default options to generator methods do not create any plots, but plots can be enabled using each method’s show_plot and save_plot_name options if matplotlib is available.

Parameters:

nx : int

Size of the generator grid along the x axis, corresponding to the right-ascension direction relative to the line of sight.

ny : int

Size of the generator grid along the y axis, corresponding to the declination direction relative to the line of sight.

nz : int

Size of the generator grid along the y axis, corresponding to the line-of-sight drection.

grid_spacing_Mpc_h: float

Uniform grid spacing in Mpc/h.

cosmology: astropy.cosmology.FLRW, optional

Homogeneous background cosmology to use for distances and for calculating the power spectrum of inhomogeneities. Should be an instance of astropy.cosmology.FLRW. Simple cases can be conveniently created using randomfield.cosmotools.create_cosmology(). If no cosmology is specified, a default Planck13 cosmology will be used.

power: numpy.ndarray, optional

Power spectrum to use, which meet the criteria tested by randomfield.powertools.validate_power(). If not specified, the power spectrum will be calculated for the specified cosmology using the optional classy package.

verbose: bool, optional.

Print a summary of this generator’s parameters.

Methods Summary

calculate_newtonian_potential([show_plot, ...]) Calculate the Newtonian potential Phi(x,y,z) at redshift zero.
convert_delta_to_density([...]) Convert a delta field into a density field with light-cone evolution.
generate_delta_field([...]) Generate a delta-field realization.
plot_slice([slice_index, figure_width, ...]) Plot a 2D slice of the most recently calculated real-valued field, with the redshift direction (axis=2) displayed horizontally and the declination direction (axis=1) displayed vertically.

Methods Documentation

calculate_newtonian_potential(show_plot=False, save_plot_name=None)[source] [edit on github]

Calculate the Newtonian potential Phi(x,y,z) at redshift zero.

The potential is calculated as the inverse Fourier transform of:

Phi(kx, ky, kz) = -4 * pi * G * rho_m_0 * delta(kx, ky, kz) / k**2

where phi_m_0 is the present-day matter density and the result is in units of (Mpc/h)**2 / s**2. Note that this calculation only fixes the potential up to a constant and the returned values will always have a spatially averaged mean of zero.

This method must be called after generate_delta_field() using the save_potential option set to True.

Parameters:

show_plot: bool, optional

Show a (y,z) slice through the calculated potential using the optional matplotlib library. The plot will need to be dismissed after it displays before the program continues. Use the save_plot_name option to generate and save the plot without requiring any user interaction.

save_plot_name: str, optional

Name of a file where the calculated potential slice plot should be saved. The file extension provided determines the image file format that will be used. This option can be used with show_plot either True or False.

Returns:

Phi : numpy array

3D numpy array of Newtonian potential values in (Mpc/h)**2 / s**2. The returned array is a view into our internal memory buffer and will be overwritten by subsequent operations.

convert_delta_to_density(apply_lognormal_transform=True, show_plot=False, save_plot_name=None)[source] [edit on github]

Convert a delta field into a density field with light-cone evolution.

Results are in units of g / cm**3. The density at each grid point is calculated at a lookback time equal to its distance from the observer. We use the plane-parallel approximation.

Parameters:

apply_lognormal_transform: bool, optional

Use randomfield.cosmotools.apply_lognormal_transform() to transform the distribution of density fluctuations so that all densities are positive.

show_plot: bool, optional

Show a (y,z) slice through the calculated density field using the optional matplotlib library. The plot will need to be dismissed after it displays before the program continues. Use the save_plot_name option to generate and save the plot without requiring any user interaction.

save_plot_name: str, optional

Name of a file where the calculated density field slice plot should be saved. The file extension provided determines the image file format that will be used. This option can be used with show_plot either True or False.

Returns:

density : numpy array

3D numpy array of light-cone density field values in g/cm**3. The returned array is a view into our internal memory buffer and will be overwritten by subsequent operations.

generate_delta_field(smoothing_length_Mpc_h=0.0, seed=None, save_potential=True, show_plot=False, save_plot_name=None)[source] [edit on github]

Generate a delta-field realization.

The delta field is calculated at redshift zero and sampled from a distribution with mean zero and k-space variance proportional to the smoothed power spectrum.

No new memory is allocated unless the save_potential option is selected.

Parameters:

smoothing_length : float, optional

Length scale on which to smooth the generated delta field in Mpc/h. If not specified, no smoothing will be applied.

seed: int, optional

Random number seed to use. Specifying an explicit seed enables you to generate a reproducible delta field. If no seed is specified, a randomized seed will be used.

save_potential: bool, optional

Save the k-space field delta(kx,ky,kz) / k**2 so that it can be used for later calculations of the lensing potential or the bulk velocity vector field. The first time this option is used, additional memory is allocated, approximately doubling the total memory usage.

show_plot: bool, optional

Show a (y,z) slice through the generated delta field using the optional matplotlib library. The plot will need to be dismissed after it displays before the program continues. Use the save_plot_name option to generate and save the plot without requiring any user interaction.

save_plot_name: str, optional

Name of a file where the generated delta field slice plot should be saved. The file extension provided determines the image file format that will be used. This option can be used with show_plot either True or False.

Returns:

delta : numpy array

3D numpy array of delta field values. The returned array is a view into our internal memory buffer and will be overwritten by subsequent operations.

plot_slice(slice_index=0, figure_width=10.0, label='Field values', cmap='jet', clip_percent=1.0, clip_symmetric=False, axis_dz=0.1, field_of_view_deg=3.5, show_plot=False, save_plot_name=None)[source] [edit on github]

Plot a 2D slice of the most recently calculated real-valued field, with the redshift direction (axis=2) displayed horizontally and the declination direction (axis=1) displayed vertically. The plot is divided into num_sections sections in the redshift direction and a histogram of all field values is displayed using the colormap.

This function will fail with an ImportError if matplotlib is not installed.

You do not normally need to call this function directly. Instead, it is invoked using the show_plot and save_plot_name options to other generator methods.