Created
June 7, 2022 19:54
-
-
Save smsharma/e27fd05b9e3f75895da5766067c81c5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from classy import Class\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"from scipy.interpolate import interp1d\n", | |
"from nbodykit.lab import LinearMesh" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"z = 0. # which redshift" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Get power spectrum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# You can change the cosmo parameters here\n", | |
"class_parameters = {'output': 'mTk,mPk',\n", | |
" 'H0': 67.66,\n", | |
" 'Omega_b': 0.04897,\n", | |
" 'N_ur': 3.046,\n", | |
" 'Omega_cdm': 0.2607,\n", | |
" 'YHe': 0.245,\n", | |
" 'z_reio': 7.82,\n", | |
" 'n_s': 0.9665,\n", | |
" 'A_s': 2.105e-9,\n", | |
" 'P_k_max_1/Mpc': 100.0,\n", | |
" 'perturbed recombination': 'n',\n", | |
" 'non linear': 'halofit'\n", | |
" }\n", | |
"\n", | |
"M = Class()\n", | |
"M.set(class_parameters)\n", | |
"M.set({'z_pk': z})\n", | |
"M.compute()\n", | |
"\n", | |
"h = M.h() # get reduced Hubble for conversions to 1/Mpc\n", | |
"\n", | |
"one_time = M.get_transfer(z)\n", | |
"\n", | |
"# Convert to units of Mpc^{-1}\n", | |
"k_ary = one_time['k (h/Mpc)'] * h\n", | |
"\n", | |
"Pk_tot_nonlin_ary = [M.pk(k, z) * h ** 3 for k in k_ary] # Get non-linear matter PS" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.plot(k_ary / h, Pk_tot_nonlin_ary)\n", | |
"plt.xscale(\"log\")\n", | |
"plt.yscale(\"log\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Generate Gaussian random field with nbodykit" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"seed = 42\n", | |
"\n", | |
"k_max = ... # scales up to which to use power spectrun in Mpc^-1 h\n", | |
"delta_d_comoving = ... # box size in Mpc h^-1 (?)\n", | |
"pk_interp = interp1d(k_ary / h, Pk_tot_nonlin_ary) # This is the power spectrum interpolation function P(k)\n", | |
"\n", | |
"n_points = np.ceil(k_max * delta_d_comoving_ary / (2 * np.pi)) # Number of points in simulation mesh\n", | |
"\n", | |
"mesh = LinearMesh(pk_interp, Nmesh=n_points, BoxSize=delta_d_comoving, seed=seed)\n", | |
"field = mesh_smooth.preview()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Do log-normal transform" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def lognormal_transform(delta, sigma):\n", | |
" t = 1 + sigma ** 2\n", | |
" delta /= sigma\n", | |
" delta *= np.sqrt(np.log(t))\n", | |
" delta = np.exp(delta, out=delta)\n", | |
" delta /= np.sqrt(t)\n", | |
" return delta" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"sigma = np.std(field)\n", | |
"delta = field - 1\n", | |
"\n", | |
"lognormal_field = lognormal_transform(delta, sigma)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment