Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscovillaescusa committed Jun 28, 2023
1 parent 579c15a commit 983ea08
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 2 deletions.
227 changes: 226 additions & 1 deletion docs/source/Examples/Density_fields.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1,226 @@
{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Creating density fields from snapshots\n\n[![Binder](https://mybinder.org/badge_logo.svg)](https://binder.flatironinstitute.org/v2/user/fvillaescusa/Quijote?filepath=/Tutorials/Density_fields.ipynb)"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport readgadget\nimport MAS_library as MASL","execution_count":1,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Define the value of the parameters"},{"metadata":{"trusted":true},"cell_type":"code","source":"snapshot = 'Data/Snapshots/fiducial/0/snapdir_004/snap_004' #location of the snapshot\ngrid = 512 #the density field will have grid^3 voxels\nMAS = 'CIC' #Mass-assignment scheme:'NGP', 'CIC', 'TSC', 'PCS'\nverbose = True #whether to print information about the progress\nptype = [1] #[1](CDM), [2](neutrinos) or [1,2](CDM+neutrinos)","execution_count":2,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Read the header and the particle positions"},{"metadata":{"trusted":true},"cell_type":"code","source":"# read header\nheader = readgadget.header(snapshot)\nBoxSize = header.boxsize/1e3 #Mpc/h\nredshift = header.redshift #redshift of the snapshot\n\n# read positions, velocities and IDs of the particles\npos = readgadget.read_block(snapshot, \"POS \", ptype)/1e3 #positions in Mpc/h","execution_count":3,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Print some information about the data"},{"metadata":{"trusted":true},"cell_type":"code","source":"print('BoxSize: %.3f Mpc/h'%BoxSize)\nprint('Redshift: %.3f'%redshift)\nprint('%.3f < X < %.3f'%(np.min(pos[:,0]), np.max(pos[:,0])))\nprint('%.3f < Y < %.3f'%(np.min(pos[:,1]), np.max(pos[:,1])))\nprint('%.3f < Z < %.3f'%(np.min(pos[:,2]), np.max(pos[:,2])))","execution_count":4,"outputs":[{"output_type":"stream","text":"BoxSize: 1000.000 Mpc/h\nRedshift: 0.000\n0.000 < X < 999.992\n0.000 < Y < 999.992\n0.000 < Z < 999.992\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"Define the matrix that will contain the value of the density / overdensity field"},{"metadata":{"trusted":true},"cell_type":"code","source":"delta = np.zeros((grid,grid,grid), dtype=np.float32)","execution_count":5,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now construct the 3D density field"},{"metadata":{"trusted":true},"cell_type":"code","source":"# construct 3D density field\nMASL.MA(pos, delta, BoxSize, MAS, verbose=verbose)","execution_count":6,"outputs":[{"output_type":"stream","text":"\nUsing CIC mass assignment scheme\nTime taken = 6.515 seconds\n\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"We can make some tests to make sure the density field has been computed properly"},{"metadata":{"trusted":true},"cell_type":"code","source":"# the sum of the values in all voxels should be equal to the number of particles\nprint('%.3f should be equal to\\n%.3f'%(np.sum(delta, dtype=np.float64), pos.shape[0]))","execution_count":7,"outputs":[{"output_type":"stream","text":"134217728.019 should be equal to\n134217728.000\n","name":"stdout"}]},{"metadata":{},"cell_type":"markdown","source":"If needed, the overdensity is easy to calculate"},{"metadata":{"trusted":true},"cell_type":"code","source":"# at this point, delta contains the effective number of particles in each voxel\n# now compute overdensity and density constrast\ndelta /= np.mean(delta, dtype=np.float64); delta -= 1.0","execution_count":8,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('%.3f < delta < %.3f'%(np.min(delta), np.max(delta)))\nprint('<delta> = %.3f'%np.mean(delta))","execution_count":9,"outputs":[{"output_type":"stream","text":"-1.000 < delta < 1195.511\n<delta> = -0.000\n","name":"stdout"}]}],"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3 (ipykernel)","language":"python"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":5}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating density fields from snapshots\n",
"\n",
"[![Binder](https://mybinder.org/badge_logo.svg)](https://binder.flatironinstitute.org/v2/user/fvillaescusa/Quijote?filepath=/Tutorials/Density_fields.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import readgadget\n",
"import MAS_library as MASL"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the value of the parameters"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"snapshot = '/home/jovyan/Data/Snapshots/fiducial/0/snapdir_004/snap_004' #location of the snapshot\n",
"grid = 512 #the density field will have grid^3 voxels\n",
"MAS = 'CIC' #Mass-assignment scheme:'NGP', 'CIC', 'TSC', 'PCS'\n",
"verbose = True #whether to print information about the progress\n",
"ptype = [1] #[1](CDM), [2](neutrinos) or [1,2](CDM+neutrinos)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read the header and the particle positions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# read header\n",
"header = readgadget.header(snapshot)\n",
"BoxSize = header.boxsize/1e3 #Mpc/h\n",
"redshift = header.redshift #redshift of the snapshot\n",
"\n",
"# read positions, velocities and IDs of the particles\n",
"pos = readgadget.read_block(snapshot, \"POS \", ptype)/1e3 #positions in Mpc/h"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print some information about the data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BoxSize: 1000.000 Mpc/h\n",
"Redshift: 0.000\n",
"0.000 < X < 999.992\n",
"0.000 < Y < 999.992\n",
"0.000 < Z < 999.992\n"
]
}
],
"source": [
"print('BoxSize: %.3f Mpc/h'%BoxSize)\n",
"print('Redshift: %.3f'%redshift)\n",
"print('%.3f < X < %.3f'%(np.min(pos[:,0]), np.max(pos[:,0])))\n",
"print('%.3f < Y < %.3f'%(np.min(pos[:,1]), np.max(pos[:,1])))\n",
"print('%.3f < Z < %.3f'%(np.min(pos[:,2]), np.max(pos[:,2])))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the matrix that will contain the value of the density / overdensity field"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"delta = np.zeros((grid,grid,grid), dtype=np.float32)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now construct the 3D density field"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Using CIC mass assignment scheme\n",
"Time taken = 6.515 seconds\n",
"\n"
]
}
],
"source": [
"# construct 3D density field\n",
"MASL.MA(pos, delta, BoxSize, MAS, verbose=verbose)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can make some tests to make sure the density field has been computed properly"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"134217728.019 should be equal to\n",
"134217728.000\n"
]
}
],
"source": [
"# the sum of the values in all voxels should be equal to the number of particles\n",
"print('%.3f should be equal to\\n%.3f'%(np.sum(delta, dtype=np.float64), pos.shape[0]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If needed, the overdensity is easy to calculate"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# at this point, delta contains the effective number of particles in each voxel\n",
"# now compute overdensity and density constrast\n",
"delta /= np.mean(delta, dtype=np.float64); delta -= 1.0"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-1.000 < delta < 1195.511\n",
"<delta> = -0.000\n"
]
}
],
"source": [
"print('%.3f < delta < %.3f'%(np.min(delta), np.max(delta)))\n",
"print('<delta> = %.3f'%np.mean(delta))"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
#html_static_path = ['_static']

html_logo = 'logo.png'

Expand Down
6 changes: 6 additions & 0 deletions docs/source/df.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ The files can be read simply as
df = np.load('/home/fvillaescusa/Quijote/3D_cubes/Om_p/df_m_128_PCS_z=0.npy')
.. warning::

Density fields with a large number of voxels occupy a significant amount of disk space, so they may not be available in our public data. However, constructing these fields are straightforward and it can be done directly in binder, so there is no need to download and process the data. We have examples of how to create these density fields directly on binder in :ref:`tutorials`.



2D fields
---------

Expand Down
2 changes: 2 additions & 0 deletions docs/source/tutorials.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _tutorials:

Tutorials
=========

Expand Down

0 comments on commit 983ea08

Please sign in to comment.