mirror of
https://github.com/Richard-Sti/csiborgtools_public.git
synced 2025-05-14 06:31:11 +00:00
Update field calculations (#51)
* Update density field routines * Add utils * Add possibility to return 2d array * Styling * Fixed density field * Fix bugs * add mpc2box * Fix evaluating sky * Fix sky map making * Rename file * Add paths if only positions * Add option to dump particles only * Add comments
This commit is contained in:
parent
b5fefe4196
commit
553eec8228
7 changed files with 462 additions and 399 deletions
|
@ -18,9 +18,9 @@ SPH density field calculation.
|
|||
|
||||
from datetime import datetime
|
||||
from gc import collect
|
||||
from distutils.util import strtobool
|
||||
|
||||
import h5py
|
||||
import numpy
|
||||
from mpi4py import MPI
|
||||
|
||||
try:
|
||||
|
@ -42,16 +42,22 @@ nproc = comm.Get_size()
|
|||
parser = ArgumentParser()
|
||||
parser.add_argument("--ics", type=int, nargs="+", default=None,
|
||||
help="IC realisatiosn. If `-1` processes all simulations.")
|
||||
parser.add_argument("--with_vel", type=lambda x: bool(strtobool(x)),
|
||||
help="Whether to include velocities in the particle file.")
|
||||
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.with_vel:
|
||||
pars_extract = ['x', 'y', 'z', 'vx', 'vy', 'vz', 'M']
|
||||
else:
|
||||
pars_extract = ['x', 'y', 'z', '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.
|
||||
# MPI loop over individual simulations. We read in the particles from RAMSES
|
||||
# files and dump them to a HDF5 file.
|
||||
jobs = csiborgtools.fits.split_jobs(len(ics), nproc)[rank]
|
||||
for i in jobs:
|
||||
nsim = ics[i]
|
||||
|
@ -59,17 +65,11 @@ for i in jobs:
|
|||
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]
|
||||
out = partreader.read_particle(
|
||||
nsnap, nsim, pars_extract, return_structured=False, verbose=nproc == 1)
|
||||
|
||||
with h5py.File(paths.particle_h5py_path(nsim), "w") as f:
|
||||
dset = f.create_dataset("particles", data=out)
|
||||
|
||||
del parts, out
|
||||
del out
|
||||
collect()
|
|
@ -16,13 +16,18 @@
|
|||
Script to calculate the particle centre of mass and Lagrangian patch size in
|
||||
the initial snapshot. Optinally dumps the particle files, however this requires
|
||||
a lot of memory.
|
||||
|
||||
TODO:
|
||||
- stop saving the particle IDs. Unnecessary.
|
||||
- Switch to h5py files. This way can save the positions in the particle
|
||||
array only.
|
||||
"""
|
||||
from argparse import ArgumentParser
|
||||
from datetime import datetime
|
||||
from distutils.util import strtobool
|
||||
from gc import collect
|
||||
from os import remove
|
||||
from os.path import join
|
||||
from os.path import isfile, join
|
||||
|
||||
import numpy
|
||||
from mpi4py import MPI
|
||||
|
@ -129,6 +134,10 @@ for i, nsim in enumerate(paths.get_ics(tonew=True)):
|
|||
out = numpy.full(parent_ids.size, numpy.nan, dtype=dtype)
|
||||
for i, clid in enumerate(parent_ids):
|
||||
fpath = ftemp.format(nsim, clid, "fit")
|
||||
# There is no file if the halo was skipped due to too few
|
||||
# particles.
|
||||
if not isfile(fpath):
|
||||
continue
|
||||
with open(fpath, "rb") as f:
|
||||
inp = numpy.load(f)
|
||||
out["index"][i] = clid
|
||||
|
@ -151,8 +160,11 @@ for i, nsim in enumerate(paths.get_ics(tonew=True)):
|
|||
out = {}
|
||||
for clid in parent_ids:
|
||||
fpath = ftemp.format(nsim, clid, "particles")
|
||||
if not isfile(fpath):
|
||||
continue
|
||||
with open(fpath, "rb") as f:
|
||||
out.update({str(clid): numpy.load(f)})
|
||||
remove(fpath)
|
||||
|
||||
fout = paths.initmatch_path(nsim, "particles")
|
||||
print(f"{datetime.now()}: dumping particles to .. `{fout}`.",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue