mirror of
https://github.com/Richard-Sti/csiborgtools_public.git
synced 2025-05-13 14:11:11 +00:00
New matches (#69)
* Remove old file * Add velocity plotting * add smooth scale * Fix bug * Improve paths * Edit plotting * Add smoothed density * Update boundaries * Add basics * Further docs * Remove blank * Better catalog broadcasting * Update high res size * Update plotting routines * Update routine * Update plotting * Fix field saving name * Add better colormap for environemnt
This commit is contained in:
parent
73687fd8cc
commit
35ccfb5c67
9 changed files with 343 additions and 169 deletions
|
@ -1,76 +0,0 @@
|
|||
# Copyright (C) 2022 Richard Stiskalek
|
||||
# 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.
|
||||
"""
|
||||
MPI script to calculate the density fields on CSiBORG simulations in the final
|
||||
snapshot.
|
||||
"""
|
||||
from argparse import ArgumentParser
|
||||
from datetime import datetime
|
||||
from distutils.util import strtobool
|
||||
|
||||
import numpy
|
||||
from mpi4py import MPI
|
||||
|
||||
try:
|
||||
import csiborgtools
|
||||
except ModuleNotFoundError:
|
||||
import sys
|
||||
sys.path.append("../")
|
||||
import csiborgtools
|
||||
|
||||
comm = MPI.COMM_WORLD
|
||||
rank = comm.Get_rank()
|
||||
nproc = comm.Get_size()
|
||||
verbose = nproc == 1
|
||||
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("--ics", type=int, nargs="+", default=None,
|
||||
help="IC realisations. If `-1` processes all simulations.")
|
||||
parser.add_argument("--kind", type=str, choices=["density", "velocity"],
|
||||
help="Calculate the density or velocity field?")
|
||||
parser.add_argument("--MAS", type=str, choices=["NGP", "CIC", "TSC", "PCS"],
|
||||
help="Mass assignment scheme.")
|
||||
parser.add_argument("--grid", type=int, help="Grid resolution.")
|
||||
parser.add_argument("--in_rsp", type=lambda x: bool(strtobool(x)),
|
||||
help="Calculate the density field in redshift space?")
|
||||
args = parser.parse_args()
|
||||
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
||||
mpart = 1.1641532e-10 # Particle mass in CSiBORG simulations.
|
||||
|
||||
if args.ics is None or args.ics[0] == -1:
|
||||
ics = paths.get_ics("csiborg")
|
||||
else:
|
||||
ics = args.ics
|
||||
|
||||
|
||||
for i in csiborgtools.fits.split_jobs(len(ics), nproc)[rank]:
|
||||
nsim = ics[i]
|
||||
print(f"{datetime.now()}: rank {rank} working on simulation {nsim}.",
|
||||
flush=True)
|
||||
|
||||
nsnap = max(paths.get_snapshots(nsim))
|
||||
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
||||
parts = csiborgtools.read.read_h5(paths.particles(nsim))["particles"]
|
||||
|
||||
if args.kind == "density":
|
||||
gen = csiborgtools.field.DensityField(box, args.MAS)
|
||||
field = gen(parts, args.grid, in_rsp=args.in_rsp, verbose=verbose)
|
||||
else:
|
||||
gen = csiborgtools.field.VelocityField(box, args.MAS)
|
||||
field = gen(parts, args.grid, mpart, verbose=verbose)
|
||||
|
||||
fout = paths.field(args.kind, args.MAS, args.grid, nsim, args.in_rsp)
|
||||
print(f"{datetime.now()}: rank {rank} saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
|
@ -40,7 +40,23 @@ from utils import get_nsims
|
|||
###############################################################################
|
||||
|
||||
|
||||
def density_field(nsim, parser_args):
|
||||
def density_field(nsim, parser_args, to_save=True):
|
||||
"""
|
||||
Calculate the density field in the CSiBORG simulation.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
nsim : int
|
||||
Simulation index.
|
||||
parser_args : argparse.Namespace
|
||||
Parsed arguments.
|
||||
to_save : bool, optional
|
||||
Whether to save the output to disk.
|
||||
|
||||
Returns
|
||||
-------
|
||||
field : 3-dimensional array
|
||||
"""
|
||||
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
||||
nsnap = max(paths.get_snapshots(nsim))
|
||||
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
||||
|
@ -50,10 +66,47 @@ def density_field(nsim, parser_args):
|
|||
field = gen(parts, parser_args.grid, in_rsp=parser_args.in_rsp,
|
||||
verbose=parser_args.verbose)
|
||||
|
||||
fout = paths.field("density", parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
||||
if to_save:
|
||||
fout = paths.field("density", parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
||||
return field
|
||||
|
||||
|
||||
def density_field_smoothed(nsim, parser_args, to_save=True):
|
||||
"""
|
||||
Calculate the smoothed density field in the CSiBORG simulation. The
|
||||
unsmoothed density field must already be precomputed.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
nsim : int
|
||||
Simulation index.
|
||||
parser_args : argparse.Namespace
|
||||
Parsed arguments.
|
||||
to_save : bool, optional
|
||||
Whether to save the output to disk.
|
||||
|
||||
Returns
|
||||
-------
|
||||
smoothed_density : 3-dimensional array
|
||||
"""
|
||||
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
||||
nsnap = max(paths.get_snapshots(nsim))
|
||||
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
||||
|
||||
# Load the real space overdensity field
|
||||
rho = numpy.load(paths.field("density", parser_args.MAS, parser_args.grid,
|
||||
nsim, in_rsp=False))
|
||||
rho = csiborgtools.field.smoothen_field(rho, parser_args.smooth_scale,
|
||||
box.boxsize, threads=1)
|
||||
if to_save:
|
||||
fout = paths.field("density", parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp, parser_args.smooth_scale)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, rho)
|
||||
return rho
|
||||
|
||||
|
||||
###############################################################################
|
||||
|
@ -61,9 +114,28 @@ def density_field(nsim, parser_args):
|
|||
###############################################################################
|
||||
|
||||
|
||||
def velocity_field(nsim, parser_args):
|
||||
def velocity_field(nsim, parser_args, to_save=True):
|
||||
"""
|
||||
Calculate the velocity field in the CSiBORG simulation.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
nsim : int
|
||||
Simulation index.
|
||||
parser_args : argparse.Namespace
|
||||
Parsed arguments.
|
||||
to_save : bool, optional
|
||||
Whether to save the output to disk.
|
||||
|
||||
Returns
|
||||
-------
|
||||
velfield : 4-dimensional array
|
||||
"""
|
||||
if parser_args.in_rsp:
|
||||
raise NotImplementedError("Velocity field in RSP is not implemented.")
|
||||
if parser_args.smooth_scale > 0:
|
||||
raise NotImplementedError(
|
||||
"Smoothed velocity field is not implemented.")
|
||||
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
||||
mpart = 1.1641532e-10 # Particle mass in CSiBORG simulations.
|
||||
nsnap = max(paths.get_snapshots(nsim))
|
||||
|
@ -73,10 +145,12 @@ def velocity_field(nsim, parser_args):
|
|||
gen = csiborgtools.field.VelocityField(box, parser_args.MAS)
|
||||
field = gen(parts, parser_args.grid, mpart, verbose=parser_args.verbose)
|
||||
|
||||
fout = paths.field("velocity", parser_args.MAS, parser_args.grid,
|
||||
nsim, in_rsp=False)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
||||
if to_save:
|
||||
fout = paths.field("velocity", parser_args.MAS, parser_args.grid,
|
||||
nsim, in_rsp=False)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
||||
return field
|
||||
|
||||
|
||||
###############################################################################
|
||||
|
@ -84,7 +158,23 @@ def velocity_field(nsim, parser_args):
|
|||
###############################################################################
|
||||
|
||||
|
||||
def potential_field(nsim, parser_args):
|
||||
def potential_field(nsim, parser_args, to_save=True):
|
||||
"""
|
||||
Calculate the potential field in the CSiBORG simulation.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
nsim : int
|
||||
Simulation index.
|
||||
parser_args : argparse.Namespace
|
||||
Parsed arguments.
|
||||
to_save : bool, optional
|
||||
Whether to save the output to disk.
|
||||
|
||||
Returns
|
||||
-------
|
||||
potential : 3-dimensional array
|
||||
"""
|
||||
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
||||
nsnap = max(paths.get_snapshots(nsim))
|
||||
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
||||
|
@ -93,6 +183,9 @@ def potential_field(nsim, parser_args):
|
|||
density_gen = csiborgtools.field.DensityField(box, parser_args.MAS)
|
||||
rho = numpy.load(paths.field("density", parser_args.MAS, parser_args.grid,
|
||||
nsim, in_rsp=False))
|
||||
if parser_args.smooth_scale > 0:
|
||||
rho = csiborgtools.field.smoothen_field(rho, parser_args.smooth_scale,
|
||||
box.boxsize, threads=1)
|
||||
rho = density_gen.overdensity_field(rho)
|
||||
# Calculate the real space potentiel field
|
||||
gen = csiborgtools.field.PotentialField(box, parser_args.MAS)
|
||||
|
@ -102,10 +195,12 @@ def potential_field(nsim, parser_args):
|
|||
parts = csiborgtools.read.read_h5(paths.particles(nsim))["particles"]
|
||||
field = csiborgtools.field.field2rsp(field, parts=parts, box=box,
|
||||
verbose=parser_args.verbose)
|
||||
fout = paths.field(parser_args.kind, parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
||||
if to_save:
|
||||
fout = paths.field(parser_args.kind, parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp, parser_args.smooth_scale)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
||||
return field
|
||||
|
||||
|
||||
###############################################################################
|
||||
|
@ -113,9 +208,28 @@ def potential_field(nsim, parser_args):
|
|||
###############################################################################
|
||||
|
||||
|
||||
def radvel_field(nsim, parser_args):
|
||||
def radvel_field(nsim, parser_args, to_save=True):
|
||||
"""
|
||||
Calculate the radial velocity field in the CSiBORG simulation.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
nsim : int
|
||||
Simulation index.
|
||||
parser_args : argparse.Namespace
|
||||
Parsed arguments.
|
||||
to_save : bool, optional
|
||||
Whether to save the output to disk.
|
||||
|
||||
Returns
|
||||
-------
|
||||
radvel : 3-dimensional array
|
||||
"""
|
||||
if parser_args.in_rsp:
|
||||
raise NotImplementedError("Radial vel. field in RSP not implemented.")
|
||||
if parser_args.smooth_scale > 0:
|
||||
raise NotImplementedError(
|
||||
"Smoothed radial vel. field not implemented.")
|
||||
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
||||
nsnap = max(paths.get_snapshots(nsim))
|
||||
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
||||
|
@ -124,11 +238,12 @@ def radvel_field(nsim, parser_args):
|
|||
nsim, parser_args.in_rsp))
|
||||
gen = csiborgtools.field.VelocityField(box, parser_args.MAS)
|
||||
field = gen.radial_velocity(vel)
|
||||
|
||||
fout = paths.field("radvel", parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
||||
if to_save:
|
||||
fout = paths.field("radvel", parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, field)
|
||||
return field
|
||||
|
||||
|
||||
###############################################################################
|
||||
|
@ -136,7 +251,23 @@ def radvel_field(nsim, parser_args):
|
|||
###############################################################################
|
||||
|
||||
|
||||
def environment_field(nsim, parser_args):
|
||||
def environment_field(nsim, parser_args, to_save=True):
|
||||
"""
|
||||
Calculate the environmental classification in the CSiBORG simulation.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
nsim : int
|
||||
Simulation index.
|
||||
parser_args : argparse.Namespace
|
||||
Parsed arguments.
|
||||
to_save : bool, optional
|
||||
Whether to save the output to disk.
|
||||
|
||||
Returns
|
||||
-------
|
||||
env : 3-dimensional array
|
||||
"""
|
||||
if parser_args.in_rsp:
|
||||
raise NotImplementedError("Env. field in RSP not implemented.")
|
||||
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
||||
|
@ -150,6 +281,9 @@ def environment_field(nsim, parser_args):
|
|||
print(f"{datetime.now()}: loading density field.")
|
||||
rho = numpy.load(paths.field("density", parser_args.MAS, parser_args.grid,
|
||||
nsim, in_rsp=False))
|
||||
if parser_args.smooth_scale > 0:
|
||||
rho = csiborgtools.field.smoothen_field(rho, parser_args.smooth_scale,
|
||||
box.boxsize, threads=1)
|
||||
rho = density_gen.overdensity_field(rho)
|
||||
# Calculate the real space tidal tensor field, delete overdensity.
|
||||
if parser_args.verbose:
|
||||
|
@ -157,12 +291,16 @@ def environment_field(nsim, parser_args):
|
|||
tensor_field = gen(rho)
|
||||
del rho
|
||||
collect()
|
||||
|
||||
# TODO: Optionally drag the field to RSP.
|
||||
|
||||
# Calculate the eigenvalues of the tidal tensor field, delete tensor field.
|
||||
if parser_args.verbose:
|
||||
print(f"{datetime.now()}: calculating eigenvalues.")
|
||||
eigvals = gen.tensor_field_eigvals(tensor_field)
|
||||
del tensor_field
|
||||
collect()
|
||||
|
||||
# Classify the environment based on the eigenvalues.
|
||||
if parser_args.verbose:
|
||||
print(f"{datetime.now()}: classifying environment.")
|
||||
|
@ -170,10 +308,12 @@ def environment_field(nsim, parser_args):
|
|||
del eigvals
|
||||
collect()
|
||||
|
||||
fout = paths.field("environment", parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, env)
|
||||
if to_save:
|
||||
fout = paths.field("environment", parser_args.MAS, parser_args.grid,
|
||||
nsim, parser_args.in_rsp, parser_args.smooth_scale)
|
||||
print(f"{datetime.now()}: saving output to `{fout}`.")
|
||||
numpy.save(fout, env)
|
||||
return env
|
||||
|
||||
|
||||
###############################################################################
|
||||
|
@ -194,6 +334,7 @@ if __name__ == "__main__":
|
|||
parser.add_argument("--grid", type=int, help="Grid resolution.")
|
||||
parser.add_argument("--in_rsp", type=lambda x: bool(strtobool(x)),
|
||||
help="Calculate in RSP?")
|
||||
parser.add_argument("--smooth_scale", type=float, default=0)
|
||||
parser.add_argument("--verbose", type=lambda x: bool(strtobool(x)),
|
||||
help="Verbosity flag for reading in particles.")
|
||||
parser_args = parser.parse_args()
|
||||
|
@ -203,7 +344,10 @@ if __name__ == "__main__":
|
|||
|
||||
def main(nsim):
|
||||
if parser_args.kind == "density":
|
||||
density_field(nsim, parser_args)
|
||||
if parser_args.smooth_scale > 0:
|
||||
density_field_smoothed(nsim, parser_args)
|
||||
else:
|
||||
density_field(nsim, parser_args)
|
||||
elif parser_args.kind == "velocity":
|
||||
velocity_field(nsim, parser_args)
|
||||
elif parser_args.kind == "radvel":
|
||||
|
|
|
@ -119,7 +119,6 @@ def collect_dist(args, paths):
|
|||
out = data["counts"]
|
||||
else:
|
||||
out += data["counts"]
|
||||
|
||||
remove(fname)
|
||||
|
||||
fout = paths.cross_nearest(args.simname, args.run, "tot_counts",
|
||||
|
|
|
@ -19,7 +19,6 @@ nbins_marks: 10
|
|||
- totpartmass
|
||||
- group_mass
|
||||
min: 12.4
|
||||
max: 12.8
|
||||
islog: true
|
||||
|
||||
"mass002":
|
||||
|
@ -28,7 +27,6 @@ nbins_marks: 10
|
|||
- totpartmass
|
||||
- group_mass
|
||||
min: 12.6
|
||||
max: 13.0
|
||||
islog: true
|
||||
|
||||
"mass003":
|
||||
|
@ -37,7 +35,6 @@ nbins_marks: 10
|
|||
- totpartmass
|
||||
- group_mass
|
||||
min: 12.8
|
||||
max: 13.2
|
||||
islog: true
|
||||
|
||||
"mass004":
|
||||
|
@ -46,7 +43,6 @@ nbins_marks: 10
|
|||
- totpartmass
|
||||
- group_mass
|
||||
min: 13.0
|
||||
max: 13.4
|
||||
islog: true
|
||||
|
||||
"mass005":
|
||||
|
@ -55,7 +51,6 @@ nbins_marks: 10
|
|||
- totpartmass
|
||||
- group_mass
|
||||
min: 13.2
|
||||
max: 13.6
|
||||
islog: true
|
||||
|
||||
"mass006":
|
||||
|
@ -64,7 +59,6 @@ nbins_marks: 10
|
|||
- totpartmass
|
||||
- group_mass
|
||||
min: 13.4
|
||||
max: 13.8
|
||||
islog: true
|
||||
|
||||
"mass007":
|
||||
|
@ -73,7 +67,6 @@ nbins_marks: 10
|
|||
- totpartmass
|
||||
- group_mass
|
||||
min: 13.6
|
||||
max: 14.0
|
||||
islog: true
|
||||
|
||||
"mass008":
|
||||
|
@ -82,7 +75,6 @@ nbins_marks: 10
|
|||
- totpartmass
|
||||
- group_mass
|
||||
min: 13.8
|
||||
max: 14.2
|
||||
islog: true
|
||||
|
||||
"mass009":
|
||||
|
|
|
@ -165,6 +165,8 @@ def open_catalogues(args, config, paths, comm):
|
|||
if args.verbose and rank == 0:
|
||||
print(f"{datetime.now()}: opening catalogues.", flush=True)
|
||||
|
||||
# We first load all catalogues on the zeroth rank and broadcast their
|
||||
# names.
|
||||
if rank == 0:
|
||||
cats = {}
|
||||
if args.simname == "csiborg":
|
||||
|
@ -182,12 +184,27 @@ def open_catalogues(args, config, paths, comm):
|
|||
name = paths.quijote_fiducial_nsim(nsim, nobs)
|
||||
cat = ref_cat.pick_fiducial_observer(nobs, rmax=args.Rmax)
|
||||
cats.update({name: cat})
|
||||
|
||||
names = list(cats.keys())
|
||||
if nproc > 1:
|
||||
for i in range(1, nproc):
|
||||
comm.send(cats, dest=i, tag=nproc + i)
|
||||
comm.send(names, dest=i, tag=nproc + i)
|
||||
else:
|
||||
cats = comm.recv(source=0, tag=nproc + rank)
|
||||
names = comm.recv(source=0, tag=nproc + rank)
|
||||
|
||||
comm.Barrier()
|
||||
# We then broadcast the catalogues to all ranks, one-by-one as MPI can
|
||||
# only pass messages smaller than 2GB.
|
||||
if nproc == 1:
|
||||
return cats
|
||||
|
||||
if rank > 0:
|
||||
cats = {}
|
||||
for name in names:
|
||||
if rank == 0:
|
||||
for i in range(1, nproc):
|
||||
comm.send(cats[name], dest=i, tag=nproc + i)
|
||||
else:
|
||||
cats.update({name: comm.recv(source=0, tag=nproc + rank)})
|
||||
return cats
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue