2023-06-05 15:24:20 +00:00
|
|
|
# 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 density field-derived fields in the CSiBORG
|
|
|
|
simulations' final snapshot.
|
|
|
|
"""
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from datetime import datetime
|
|
|
|
from distutils.util import strtobool
|
2023-06-16 17:31:43 +00:00
|
|
|
from gc import collect
|
2023-06-05 15:24:20 +00:00
|
|
|
|
|
|
|
import numpy
|
|
|
|
from mpi4py import MPI
|
|
|
|
from taskmaster import work_delegation
|
|
|
|
|
2023-09-01 15:29:50 +00:00
|
|
|
import csiborgtools
|
2023-06-05 15:24:20 +00:00
|
|
|
from utils import get_nsims
|
|
|
|
|
2023-12-07 14:23:32 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Cosmotool SPH density & velocity field #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
def cosmotool_sph(nsim, parser_args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-06-05 15:24:20 +00:00
|
|
|
###############################################################################
|
|
|
|
# Density field #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2023-06-17 18:52:26 +00:00
|
|
|
def density_field(nsim, parser_args, to_save=True):
|
|
|
|
"""
|
|
|
|
Calculate the density field in the CSiBORG simulation.
|
|
|
|
"""
|
2023-06-05 15:24:20 +00:00
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
2023-07-27 16:41:00 +00:00
|
|
|
nsnap = max(paths.get_snapshots(nsim, "csiborg"))
|
2023-06-05 15:24:20 +00:00
|
|
|
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
2023-12-07 14:23:32 +00:00
|
|
|
fname = paths.processed_output(nsim, "csiborg", "halo_catalogue")
|
2023-08-30 22:27:20 +00:00
|
|
|
|
|
|
|
if not parser_args.in_rsp:
|
2023-12-07 14:23:32 +00:00
|
|
|
snap = csiborgtools.read.read_h5(fname)["snapshot_final"]
|
|
|
|
pos = snap["pos"]
|
|
|
|
mass = snap["mass"]
|
2023-08-30 22:27:20 +00:00
|
|
|
|
|
|
|
gen = csiborgtools.field.DensityField(box, parser_args.MAS)
|
2023-12-07 14:23:32 +00:00
|
|
|
field = gen(pos, mass, parser_args.grid, verbose=parser_args.verbose)
|
2023-06-26 19:41:07 +00:00
|
|
|
else:
|
2023-08-30 22:27:20 +00:00
|
|
|
field = numpy.load(paths.field(
|
|
|
|
"density", parser_args.MAS, parser_args.grid, nsim, False))
|
|
|
|
radvel_field = numpy.load(paths.field(
|
|
|
|
"radvel", parser_args.MAS, parser_args.grid, nsim, False))
|
2023-06-17 18:52:26 +00:00
|
|
|
|
2023-09-01 15:29:50 +00:00
|
|
|
if parser_args.verbose:
|
|
|
|
print(f"{datetime.now()}: converting density field to RSP.",
|
|
|
|
flush=True)
|
|
|
|
|
2023-08-30 22:27:20 +00:00
|
|
|
field = csiborgtools.field.field2rsp(field, radvel_field, box,
|
|
|
|
parser_args.MAS)
|
2023-06-17 18:52:26 +00:00
|
|
|
|
|
|
|
if to_save:
|
2023-06-26 19:41:07 +00:00
|
|
|
fout = paths.field(parser_args.kind, parser_args.MAS, parser_args.grid,
|
2023-08-30 22:27:20 +00:00
|
|
|
nsim, parser_args.in_rsp)
|
2023-06-17 18:52:26 +00:00
|
|
|
print(f"{datetime.now()}: saving output to `{fout}`.")
|
2023-06-26 19:41:07 +00:00
|
|
|
numpy.save(fout, field)
|
|
|
|
return field
|
2023-06-05 15:24:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Velocity field #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2023-06-17 18:52:26 +00:00
|
|
|
def velocity_field(nsim, parser_args, to_save=True):
|
|
|
|
"""
|
2023-08-30 22:27:20 +00:00
|
|
|
Calculate the velocity field in a CSiBORG simulation.
|
2023-06-17 18:52:26 +00:00
|
|
|
"""
|
2023-06-05 15:24:20 +00:00
|
|
|
if parser_args.in_rsp:
|
|
|
|
raise NotImplementedError("Velocity field in RSP is not implemented.")
|
2023-08-30 22:27:20 +00:00
|
|
|
|
2023-06-05 15:24:20 +00:00
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
2023-07-27 16:41:00 +00:00
|
|
|
nsnap = max(paths.get_snapshots(nsim, "csiborg"))
|
2023-06-05 15:24:20 +00:00
|
|
|
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
2023-12-07 14:23:32 +00:00
|
|
|
fname = paths.processed_output(nsim, "csiborg", "halo_catalogue")
|
2023-08-30 22:27:20 +00:00
|
|
|
|
2023-12-07 14:23:32 +00:00
|
|
|
snap = csiborgtools.read.read_h5(fname)["snapshot_final"]
|
|
|
|
pos = snap["pos"]
|
|
|
|
vel = snap["vel"]
|
|
|
|
mass = snap["mass"]
|
2023-06-05 15:24:20 +00:00
|
|
|
|
|
|
|
gen = csiborgtools.field.VelocityField(box, parser_args.MAS)
|
2023-12-07 14:23:32 +00:00
|
|
|
field = gen(pos, vel, mass, parser_args.grid, verbose=parser_args.verbose)
|
2023-06-05 15:24:20 +00:00
|
|
|
|
2023-06-17 18:52:26 +00:00
|
|
|
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
|
2023-06-05 15:24:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
2023-08-30 22:27:20 +00:00
|
|
|
# Radial velocity field #
|
2023-06-05 15:24:20 +00:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2023-08-30 22:27:20 +00:00
|
|
|
def radvel_field(nsim, parser_args, to_save=True):
|
2023-06-17 18:52:26 +00:00
|
|
|
"""
|
2023-08-30 22:27:20 +00:00
|
|
|
Calculate the radial velocity field in the CSiBORG simulation.
|
2023-06-17 18:52:26 +00:00
|
|
|
"""
|
2023-08-30 22:27:20 +00:00
|
|
|
if parser_args.in_rsp:
|
|
|
|
raise NotImplementedError("Radial vel. field in RSP not implemented.")
|
|
|
|
|
2023-06-05 15:24:20 +00:00
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
2023-07-27 16:41:00 +00:00
|
|
|
nsnap = max(paths.get_snapshots(nsim, "csiborg"))
|
2023-06-05 15:24:20 +00:00
|
|
|
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
|
|
|
|
2023-08-30 22:27:20 +00:00
|
|
|
vel = numpy.load(paths.field("velocity", parser_args.MAS, parser_args.grid,
|
|
|
|
nsim, parser_args.in_rsp))
|
|
|
|
observer_velocity = csiborgtools.field.observer_vobs(vel)
|
|
|
|
|
|
|
|
gen = csiborgtools.field.VelocityField(box, parser_args.MAS)
|
|
|
|
field = gen.radial_velocity(vel, observer_velocity)
|
2023-06-05 15:24:20 +00:00
|
|
|
|
2023-06-17 18:52:26 +00:00
|
|
|
if to_save:
|
2023-08-30 22:27:20 +00:00
|
|
|
fout = paths.field("radvel", parser_args.MAS, parser_args.grid,
|
|
|
|
nsim, parser_args.in_rsp)
|
2023-06-17 18:52:26 +00:00
|
|
|
print(f"{datetime.now()}: saving output to `{fout}`.")
|
|
|
|
numpy.save(fout, field)
|
|
|
|
return field
|
2023-06-05 15:24:20 +00:00
|
|
|
|
|
|
|
###############################################################################
|
2023-08-30 22:27:20 +00:00
|
|
|
# Potential field #
|
2023-06-05 15:24:20 +00:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2023-08-30 22:27:20 +00:00
|
|
|
def potential_field(nsim, parser_args, to_save=True):
|
2023-06-17 18:52:26 +00:00
|
|
|
"""
|
2023-08-30 22:27:20 +00:00
|
|
|
Calculate the potential field in the CSiBORG simulation.
|
2023-06-17 18:52:26 +00:00
|
|
|
"""
|
2023-06-05 15:24:20 +00:00
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
2023-07-27 16:41:00 +00:00
|
|
|
nsnap = max(paths.get_snapshots(nsim, "csiborg"))
|
2023-06-05 15:24:20 +00:00
|
|
|
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
|
|
|
|
2023-08-30 22:27:20 +00:00
|
|
|
if not parser_args.in_rsp:
|
|
|
|
rho = numpy.load(paths.field(
|
|
|
|
"density", parser_args.MAS, parser_args.grid, nsim, in_rsp=False))
|
|
|
|
density_gen = csiborgtools.field.DensityField(box, parser_args.MAS)
|
|
|
|
rho = density_gen.overdensity_field(rho)
|
|
|
|
|
|
|
|
gen = csiborgtools.field.PotentialField(box, parser_args.MAS)
|
|
|
|
field = gen(rho)
|
|
|
|
else:
|
|
|
|
field = numpy.load(paths.field(
|
|
|
|
"potential", parser_args.MAS, parser_args.grid, nsim, False))
|
|
|
|
radvel_field = numpy.load(paths.field(
|
|
|
|
"radvel", parser_args.MAS, parser_args.grid, nsim, False))
|
|
|
|
|
|
|
|
field = csiborgtools.field.field2rsp(field, radvel_field, box,
|
|
|
|
parser_args.MAS)
|
|
|
|
|
2023-06-17 18:52:26 +00:00
|
|
|
if to_save:
|
2023-08-30 22:27:20 +00:00
|
|
|
fout = paths.field(parser_args.kind, parser_args.MAS, parser_args.grid,
|
2023-06-17 18:52:26 +00:00
|
|
|
nsim, parser_args.in_rsp)
|
|
|
|
print(f"{datetime.now()}: saving output to `{fout}`.")
|
|
|
|
numpy.save(fout, field)
|
|
|
|
return field
|
2023-06-05 15:24:20 +00:00
|
|
|
|
|
|
|
|
2023-06-16 17:31:43 +00:00
|
|
|
###############################################################################
|
|
|
|
# Environment classification #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
2023-06-17 18:52:26 +00:00
|
|
|
def environment_field(nsim, parser_args, to_save=True):
|
|
|
|
"""
|
|
|
|
Calculate the environmental classification in the CSiBORG simulation.
|
|
|
|
"""
|
2023-06-16 17:31:43 +00:00
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
2023-07-27 16:41:00 +00:00
|
|
|
nsnap = max(paths.get_snapshots(nsim, "csiborg"))
|
2023-06-16 17:31:43 +00:00
|
|
|
box = csiborgtools.read.CSiBORGBox(nsnap, nsim, paths)
|
2023-08-30 22:27:20 +00:00
|
|
|
|
|
|
|
rho = numpy.load(paths.field(
|
|
|
|
"density", parser_args.MAS, parser_args.grid, nsim, in_rsp=False))
|
2023-06-16 17:31:43 +00:00
|
|
|
density_gen = csiborgtools.field.DensityField(box, parser_args.MAS)
|
2023-08-30 22:27:20 +00:00
|
|
|
rho = density_gen.overdensity_field(rho)
|
|
|
|
|
2023-09-01 15:29:50 +00:00
|
|
|
if parser_args.smooth_scale > 0.0:
|
|
|
|
rho = csiborgtools.field.smoothen_field(
|
|
|
|
rho, parser_args.smooth_scale, box.box2mpc(1.))
|
|
|
|
|
2023-06-16 17:31:43 +00:00
|
|
|
gen = csiborgtools.field.TidalTensorField(box, parser_args.MAS)
|
2023-08-30 22:27:20 +00:00
|
|
|
field = gen(rho)
|
2023-06-16 17:31:43 +00:00
|
|
|
|
|
|
|
del rho
|
|
|
|
collect()
|
2023-06-17 18:52:26 +00:00
|
|
|
|
2023-06-18 10:42:21 +00:00
|
|
|
if parser_args.in_rsp:
|
2023-08-30 22:27:20 +00:00
|
|
|
radvel_field = numpy.load(paths.field(
|
|
|
|
"radvel", parser_args.MAS, parser_args.grid, nsim, False))
|
|
|
|
args = (radvel_field, box, parser_args.MAS)
|
|
|
|
|
|
|
|
field.T00 = csiborgtools.field.field2rsp(field.T00, *args)
|
|
|
|
field.T11 = csiborgtools.field.field2rsp(field.T11, *args)
|
|
|
|
field.T22 = csiborgtools.field.field2rsp(field.T22, *args)
|
|
|
|
field.T01 = csiborgtools.field.field2rsp(field.T01, *args)
|
|
|
|
field.T02 = csiborgtools.field.field2rsp(field.T02, *args)
|
|
|
|
field.T12 = csiborgtools.field.field2rsp(field.T12, *args)
|
|
|
|
|
|
|
|
del radvel_field
|
2023-06-18 10:42:21 +00:00
|
|
|
collect()
|
2023-06-17 18:52:26 +00:00
|
|
|
|
2023-08-30 22:27:20 +00:00
|
|
|
eigvals = gen.tensor_field_eigvals(field)
|
|
|
|
|
|
|
|
del field
|
2023-06-16 17:31:43 +00:00
|
|
|
collect()
|
2023-06-17 18:52:26 +00:00
|
|
|
|
2023-06-16 17:31:43 +00:00
|
|
|
env = gen.eigvals_to_environment(eigvals)
|
|
|
|
|
2023-06-17 18:52:26 +00:00
|
|
|
if to_save:
|
|
|
|
fout = paths.field("environment", parser_args.MAS, parser_args.grid,
|
2023-09-01 15:29:50 +00:00
|
|
|
nsim, parser_args.in_rsp, parser_args.smooth_scale)
|
2023-06-17 18:52:26 +00:00
|
|
|
print(f"{datetime.now()}: saving output to `{fout}`.")
|
|
|
|
numpy.save(fout, env)
|
|
|
|
return env
|
2023-06-16 17:31:43 +00:00
|
|
|
|
|
|
|
|
2023-06-05 15:24:20 +00:00
|
|
|
###############################################################################
|
|
|
|
# Command line interface #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument("--nsims", type=int, nargs="+", default=None,
|
|
|
|
help="IC realisations. `-1` for all simulations.")
|
|
|
|
parser.add_argument("--kind", type=str,
|
2023-06-26 19:41:07 +00:00
|
|
|
choices=["density", "rspdensity", "velocity", "radvel",
|
|
|
|
"potential", "environment"],
|
2023-06-05 15:24:20 +00:00
|
|
|
help="What derived field to calculate?")
|
|
|
|
parser.add_argument("--MAS", type=str,
|
|
|
|
choices=["NGP", "CIC", "TSC", "PCS"])
|
|
|
|
parser.add_argument("--grid", type=int, help="Grid resolution.")
|
|
|
|
parser.add_argument("--in_rsp", type=lambda x: bool(strtobool(x)),
|
|
|
|
help="Calculate in RSP?")
|
2023-09-01 15:29:50 +00:00
|
|
|
parser.add_argument("--smooth_scale", type=float, default=0.0,
|
|
|
|
help="Smoothing scale in Mpc / h. Only used for the environment field.") # noqa
|
2023-06-05 15:24:20 +00:00
|
|
|
parser.add_argument("--verbose", type=lambda x: bool(strtobool(x)),
|
|
|
|
help="Verbosity flag for reading in particles.")
|
2023-06-26 19:41:07 +00:00
|
|
|
parser.add_argument("--simname", type=str, default="csiborg",
|
2023-12-07 14:23:32 +00:00
|
|
|
choices=["csiborg", "csiborg2"],
|
2023-06-26 19:41:07 +00:00
|
|
|
help="Verbosity flag for reading in particles.")
|
2023-06-05 15:24:20 +00:00
|
|
|
parser_args = parser.parse_args()
|
|
|
|
comm = MPI.COMM_WORLD
|
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
|
|
|
nsims = get_nsims(parser_args, paths)
|
|
|
|
|
|
|
|
def main(nsim):
|
2023-06-26 19:41:07 +00:00
|
|
|
if parser_args.kind == "density" or parser_args.kind == "rspdensity":
|
|
|
|
density_field(nsim, parser_args)
|
2023-06-05 15:24:20 +00:00
|
|
|
elif parser_args.kind == "velocity":
|
|
|
|
velocity_field(nsim, parser_args)
|
|
|
|
elif parser_args.kind == "radvel":
|
|
|
|
radvel_field(nsim, parser_args)
|
|
|
|
elif parser_args.kind == "potential":
|
|
|
|
potential_field(nsim, parser_args)
|
2023-06-16 17:31:43 +00:00
|
|
|
elif parser_args.kind == "environment":
|
|
|
|
environment_field(nsim, parser_args)
|
2023-06-05 15:24:20 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError(f"Field {parser_args.kind} is not implemented.")
|
|
|
|
|
|
|
|
work_delegation(main, nsims, comm, master_verbose=True)
|