In [1]:
# this cell is hidden via cell metadata
import tempfile, os
startdir = os.path.abspath('.')
tmpdir = tempfile.mkdtemp()
os.chdir(tmpdir)
from nbodykit import style
import matplotlib.pyplot as plt
plt.style.use(style.notebook)
In [2]:
from nbodykit.lab import LinearMesh, cosmology
from matplotlib import pyplot as plt
cosmo = cosmology.Planck15
Plin = cosmology.LinearPower(cosmo, redshift=0, transfer='EisensteinHu')
mesh = LinearMesh(Plin, Nmesh=128, BoxSize=1380, seed=42)
density = mesh.preview(Nmesh=64, axes=(0,1))
plt.imshow(density)
Out[2]:
In [3]:
# save the RealField
mesh.save('linear-mesh-real.bigfile', mode='real', dataset='Field')
# save the ComplexField
mesh.save('linear-mesh-complex.bigfile', mode='real', dataset='Field')
In [4]:
from nbodykit.lab import BigFileMesh
import numpy
# load the mesh in the form of a RealField
real_mesh = BigFileMesh('linear-mesh-real.bigfile', 'Field')
# return the RealField via paint
rfield = real_mesh.paint(mode='real')
# load the mesh in the form of a ComplexField
complex_mesh = BigFileMesh('linear-mesh-complex.bigfile', 'Field')
# FFT to get the ComplexField as a RealField
rfield2 = complex_mesh.paint(mode='real')
# the two RealFields must be the same!
numpy.allclose(rfield.value, rfield2.value)
Out[4]:
In [5]:
def filter(k, v):
kk = sum(ki ** 2 for ki in k) # k^2 on the mesh
kk[kk == 0] = 1
return v / kk # divide the mesh by k^2
# apply the filter and get a new mesh
filtered_mesh = mesh.apply(filter, mode='complex', kind='wavenumber')
# get the filtered RealField object
filtered_rfield = filtered_mesh.paint(mode='real')
print("head of filtered Realfield = ", filtered_rfield[:10,0,0])
print("head of original RealField = ", rfield[:10,0,0])
In [6]:
from nbodykit.lab import LinearMesh, cosmology
# linear mesh
Plin = cosmology.LinearPower(cosmology.Planck15, redshift=0.55, transfer='EisensteinHu')
source = LinearMesh(Plin, Nmesh=64, BoxSize=512, seed=42)
# paint, re-sampling to Nmesh=32
real = source.paint(mode='real', Nmesh=32)
print("original Nmesh = ", source.attrs['Nmesh'])
print("resampled Nmesh = ", real.Nmesh)
print("shape of resampled density field = ", real.cshape)
In [7]:
import shutil
os.chdir(startdir)
shutil.rmtree(tmpdir)