nbodykit.source.mesh.linear module

class nbodykit.source.mesh.linear.LinearMesh(Plin, BoxSize, Nmesh, seed=None, remove_variance=False, comm=None)[source]

Bases: nbodykit.base.mesh.MeshSource

A MeshSource object that generates a RealField density mesh from a linear power spectrum function \(P(k)\).

Parameters:
  • Plin (callable) – the callable linear power spectrum function, which takes the wavenumber as its single argument
  • BoxSize (float, 3-vector of floats) – the size of the box to generate the grid on
  • Nmesh (int, 3-vector of int) – the number of the mesh cells per side
  • seed (int, optional) – the global random seed, used to set the seeds across all ranks
  • remove_variance (bool, optional) – True to remove variance from the complex field by fixing the amplitude to \(P(k)\) and only the phase is random.
  • comm (MPI communicator) – the MPI communicator

Attributes

actions A list of actions to apply to the density field when interpolating to the mesh.
attrs A dictionary storing relevant meta-data about the MeshSource.

Methods

apply(func[, kind, mode]) Return a view of the mesh, with actions updated to
paint([mode, Nmesh]) Paint the density on the mesh and apply any transformation functions specified in actions.
preview([axes, Nmesh, root]) Gather the mesh into as a numpy array, with (reduced) resolution.
save(output[, dataset, mode]) Save the mesh as a BigFileMesh on disk, either in real or complex space.
to_complex_field() Return a ComplexField, generating from the linear power spectrum.
to_field([mode, out]) Return the mesh as a pmesh Field object, either in Fourier space or configuration space, based on mode.
to_real_field([out, normalize]) Convert the mesh source to the configuration-space field, returning a pmesh.pm.RealField object.
view() Return a “view” of the MeshSource, in the spirit of numpy’s ndarray view.
__len__()

Length of a mesh source is zero

actions

A list of actions to apply to the density field when interpolating to the mesh.

This stores tuples of (mode, func, kind); see apply() for more details.

apply(func, kind='wavenumber', mode='complex')

Return a view of the mesh, with actions updated to apply the specified function, either in Fourier space or configuration space, based on mode

Parameters:
  • func (callable) – func(x, y) where x is a list of r (k) values that broadcasts into a full array, when mode is ‘real’ (‘complex’); the value of x depends on kind. y is the value of the mesh field on the corresponding locations.
  • kind (string, optional) –

    The kind of value in x.

    • When mode is ‘complex’:
      • ’wavenumber’ means wavenumber from [- 2 pi / L * N / 2, 2 pi / L * N / 2).
      • ’circular’ means circular frequency from [- pi, pi).
      • ’index’ means [0, Nmesh )
    • When mode is ‘real’:
      • ’relative’ means distance from [-0.5 Boxsize, 0.5 BoxSize).
      • ’index’ means [0, Nmesh )
  • mode ('complex' or 'real', optional) – whether to apply the function to the mesh in configuration space or Fourier space
Returns:

a view of the mesh object with the actions attribute updated to include the new action

Return type:

MeshSource

attrs

A dictionary storing relevant meta-data about the MeshSource.

logger = <logging.Logger object>
paint(mode='real', Nmesh=None)

Paint the density on the mesh and apply any transformation functions specified in actions.

The return type of the pmesh Field object is specified by mode. This calls to_field() to convert the mesh to a Field.

See the documentation on painting for more details on painting catalogs to a mesh.

Parameters:
  • mode ('real' or 'complex') – the type of the returned Field object, either a RealField or ComplexField
  • Nmesh (int or array_like, or None) – If given and different from the intrinsic Nmesh of the source, resample the mesh to the given resolution
Returns:

either a RealField of ComplexField, with the functions in actions applied to it

Return type:

RealField, ComplexField

preview(axes=None, Nmesh=None, root=0)

Gather the mesh into as a numpy array, with (reduced) resolution. The result is broadcast to all ranks, so this uses \(\mathrm{Nmesh}^3\) per rank.

Parameters:
  • Nmesh (int, array_like) – The desired Nmesh of the result. Be aware this function allocates memory to hold a full Nmesh on each rank.
  • axes (int, array_like) – The axes to project the preview onto., e.g. (0, 1)
  • root (int, optional) – the rank number to treat as root when gathering to a single rank
Returns:

out – An numpy array holding the real density field.

Return type:

array_like

save(output, dataset='Field', mode='real')

Save the mesh as a BigFileMesh on disk, either in real or complex space.

Parameters:
  • output (str) – name of the bigfile file
  • dataset (str, optional) – name of the bigfile data set where the field is stored
  • mode (str, optional) – real or complex; the form of the field to store
to_complex_field()[source]

Return a ComplexField, generating from the linear power spectrum.

Note

The density field is normalized to \(1+\delta\) such that the mean of the return field in real space is unity.

Returns:an array-like object holding the generated linear density field in Fourier space
Return type:pmesh.pm.ComplexField
to_field(mode='real', out=None)

Return the mesh as a pmesh Field object, either in Fourier space or configuration space, based on mode.

This will call to_real_field() or to_complex_field() based on mode.

Parameters:mode ('real' or 'complex') – the return type of the field
Returns:either a RealField of ComplexField, storing the value of the field on the mesh
Return type:RealField, ComplexField
to_real_field(out=None, normalize=True)

Convert the mesh source to the configuration-space field, returning a pmesh.pm.RealField object.

Not implemented in the base class, unless object is a view.

view()

Return a “view” of the MeshSource, in the spirit of numpy’s ndarray view.

This returns a new MeshSource whose memory is owned by self.