Particle dumping (#49)

* Add paths

* Add dumping script

* Remove snap from density fields

* Add progress statement

* Update import ordering

* Update nb
This commit is contained in:
Richard Stiskalek 2023-04-28 13:57:23 +01:00 committed by GitHub
parent 4f8335e0a3
commit c14be720b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 175 additions and 3 deletions

View file

@ -326,6 +326,49 @@ class CSiBORGPaths:
fname = f"radpos_{str(nsim).zfill(5)}_{str(nsnap).zfill(5)}.npz"
return join(fdir, fname)
def particle_h5py_path(self, nsim):
"""
Path to the files containing all particles in a `.hdf5` file. Used for
the SPH calculation.
Parameters
----------
nsim : int
IC realisation index.
Returns
-------
path : str
"""
fdir = join(self.postdir, "environment")
if not isdir(fdir):
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
fname = f"particles_{str(nsim).zfill(5)}.h5"
return join(fdir, fname)
def density_field_path(self, mas, nsim):
"""
Path to the files containing the calculated density fields.
Parameters
----------
mas : str
Mass-assignment scheme. Currently only SPH is supported.
nsim : int
IC realisation index.
Returns
-------
path : str
"""
fdir = join(self.postdir, "environment")
if not isdir(fdir):
makedirs(fdir)
warn(f"Created directory `{fdir}`.", UserWarning, stacklevel=1)
fname = f"density_{mas}_{str(nsim).zfill(5)}.npy"
return join(fdir, fname)
def knnauto_path(self, run, nsim=None):
"""
Path to the `knn` auto-correlation files. If `nsim` is not specified

View file

@ -74,18 +74,72 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "f2986dd2",
"metadata": {},
"outputs": [],
"source": [
"import h5py"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1d8ad8fe",
"metadata": {},
"outputs": [],
"source": [
"f = h5py.File(\"../data/particles_7444.h5\", \"r\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "020ac8e4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 4.42346358e-01, 7.09757663e-03, 4.64053304e-01, -1.96926287e-03,\n",
" -2.67177823e-03, -6.45721859e-04, 1.16415322e-10])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f[\"particles\"][0, :]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1eb74f46",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv_galomatch",
"display_name": "venv_csiborg",
"language": "python",
"name": "venv_galomatch"
"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.9.7"
}
},
"nbformat": 4,

75
scripts/pre_dumppath5.py Normal file
View file

@ -0,0 +1,75 @@
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
Script to load in the simulation particles and dump them to a HDF5 file for the
SPH density field calculation.
"""
from datetime import datetime
from gc import collect
import h5py
import numpy
from mpi4py import MPI
try:
import csiborgtools
except ModuleNotFoundError:
import sys
sys.path.append("../")
import csiborgtools
from argparse import ArgumentParser
# We set up the MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nproc = comm.Get_size()
# And next parse all the arguments and set up CSiBORG objects
parser = ArgumentParser()
parser.add_argument("--ics", type=int, nargs="+", default=None,
help="IC realisatiosn. If `-1` processes all simulations.")
args = parser.parse_args()
paths = csiborgtools.read.CSiBORGPaths(**csiborgtools.paths_glamdring)
partreader = csiborgtools.read.ParticleReader(paths)
pars_extract = ['x', 'y', 'z', 'vx', 'vy', 'vz', 'M']
if args.ics is None or args.ics == -1:
ics = paths.get_ics(tonew=False)
else:
ics = args.ics
# We MPI loop over individual simulations.
jobs = csiborgtools.fits.split_jobs(len(ics), nproc)[rank]
for i in jobs:
nsim = ics[i]
nsnap = max(paths.get_snapshots(nsim))
print(f"{datetime.now()}: Rank {rank} completing simulation {nsim}.",
flush=True)
# We read in the particles from RASMSES files, switch from a
# structured array to 2-dimensional array and dump it.
parts = partreader.read_particle(nsnap, nsim, pars_extract,
verbose=nproc == 1)
out = numpy.full((parts.size, len(pars_extract)), numpy.nan,
dtype=numpy.float32)
for j, par in enumerate(pars_extract):
out[:, j] = parts[par]
with h5py.File(paths.particle_h5py_path(nsim), "w") as f:
dset = f.create_dataset("particles", data=out)
del parts, out
collect()