2022-10-30 20:16:56 +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.
|
|
|
|
"""
|
|
|
|
A script to fit halos (concentration, ...). The particle array of each CSiBORG
|
2023-04-04 14:22:00 +00:00
|
|
|
realisation must have been split in advance by `runsplit_halos`.
|
2022-10-30 20:16:56 +00:00
|
|
|
"""
|
|
|
|
from os.path import join
|
2023-04-04 14:22:00 +00:00
|
|
|
from datetime import datetime
|
|
|
|
import numpy
|
2022-10-30 20:16:56 +00:00
|
|
|
from mpi4py import MPI
|
|
|
|
try:
|
|
|
|
import csiborgtools
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
import sys
|
|
|
|
sys.path.append("../")
|
|
|
|
import csiborgtools
|
|
|
|
import utils
|
|
|
|
|
|
|
|
|
|
|
|
# Get MPI things
|
|
|
|
comm = MPI.COMM_WORLD
|
|
|
|
rank = comm.Get_rank()
|
|
|
|
nproc = comm.Get_size()
|
|
|
|
|
2022-11-25 10:44:08 +00:00
|
|
|
paths = csiborgtools.read.CSiBORGPaths()
|
2023-04-04 14:22:00 +00:00
|
|
|
dumpdir = "/mnt/extraspace/rstiskalek/csiborg/"
|
|
|
|
loaddir = join(dumpdir, "temp")
|
|
|
|
cols_collect = [("npart", numpy.int64), ("totpartmass", numpy.float64),
|
|
|
|
("Rs", numpy.float64), ("vx", numpy.float64),
|
|
|
|
("vy", numpy.float64), ("vz", numpy.float64),
|
|
|
|
("Lx", numpy.float64), ("Ly", numpy.float64),
|
|
|
|
("Lz", numpy.float64), ("rho0", numpy.float64),
|
|
|
|
("conc", numpy.float64), ("rmin", numpy.float64),
|
|
|
|
("rmax", numpy.float64), ("r200", numpy.float64),
|
|
|
|
("r500", numpy.float64), ("m200", numpy.float64),
|
|
|
|
("m500", numpy.float64), ("lambda200c", numpy.float64)]
|
|
|
|
|
|
|
|
|
|
|
|
for i, nsim in enumerate(paths.ic_ids(tonew=False)):
|
2022-11-22 10:00:01 +00:00
|
|
|
if rank == 0:
|
|
|
|
print("{}: calculating {}th simulation.".format(datetime.now(), i))
|
2023-04-04 14:22:00 +00:00
|
|
|
nsnap = max(paths.get_snapshots(nsim))
|
|
|
|
box = csiborgtools.units.BoxUnits(nsnap, nsim, paths)
|
2022-11-22 10:00:01 +00:00
|
|
|
|
|
|
|
jobs = csiborgtools.fits.split_jobs(utils.Nsplits, nproc)[rank]
|
2023-04-04 14:22:00 +00:00
|
|
|
for nsplit in jobs:
|
2022-11-22 10:00:01 +00:00
|
|
|
parts, part_clumps, clumps = csiborgtools.fits.load_split_particles(
|
2023-04-04 14:22:00 +00:00
|
|
|
nsplit, nsnap, nsim, paths, remove_split=False)
|
2022-11-22 10:00:01 +00:00
|
|
|
|
|
|
|
N = clumps.size
|
2023-04-04 14:22:00 +00:00
|
|
|
cols = [("index", numpy.int64), ("npart", numpy.int64),
|
|
|
|
("totpartmass", numpy.float64), ("Rs", numpy.float64),
|
|
|
|
("rho0", numpy.float64), ("conc", numpy.float64),
|
|
|
|
("lambda200c", numpy.float64), ("vx", numpy.float64),
|
|
|
|
("vy", numpy.float64), ("vz", numpy.float64),
|
|
|
|
("Lx", numpy.float64), ("Ly", numpy.float64),
|
|
|
|
("Lz", numpy.float64), ("rmin", numpy.float64),
|
|
|
|
("rmax", numpy.float64), ("r200", numpy.float64),
|
|
|
|
("r500", numpy.float64), ("m200", numpy.float64),
|
|
|
|
("m500", numpy.float64)]
|
2022-11-22 10:00:01 +00:00
|
|
|
out = csiborgtools.utils.cols_to_structured(N, cols)
|
|
|
|
out["index"] = clumps["index"]
|
|
|
|
|
|
|
|
for n in range(N):
|
|
|
|
# Pick clump and its particles
|
|
|
|
xs = csiborgtools.fits.pick_single_clump(n, parts, part_clumps,
|
|
|
|
clumps)
|
2022-11-25 12:24:06 +00:00
|
|
|
clump = csiborgtools.fits.Clump.from_arrays(
|
|
|
|
*xs, rhoc=box.box_rhoc, G=box.box_G)
|
2022-11-22 10:00:01 +00:00
|
|
|
out["npart"][n] = clump.Npart
|
|
|
|
out["rmin"][n] = clump.rmin
|
|
|
|
out["rmax"][n] = clump.rmax
|
|
|
|
out["totpartmass"][n] = clump.total_particle_mass
|
2022-11-25 10:44:08 +00:00
|
|
|
out["vx"][n] = numpy.average(clump.vel[:, 0], weights=clump.m)
|
|
|
|
out["vy"][n] = numpy.average(clump.vel[:, 1], weights=clump.m)
|
|
|
|
out["vz"][n] = numpy.average(clump.vel[:, 2], weights=clump.m)
|
2022-11-30 16:12:39 +00:00
|
|
|
out["Lx"][n], out["Ly"][n], out["Lz"][n] = clump.angular_momentum
|
2022-11-22 10:00:01 +00:00
|
|
|
|
|
|
|
# Spherical overdensity radii and masses
|
|
|
|
rs, ms = clump.spherical_overdensity_mass([200, 500])
|
|
|
|
out["r200"][n] = rs[0]
|
|
|
|
out["r500"][n] = rs[1]
|
|
|
|
out["m200"][n] = ms[0]
|
|
|
|
out["m500"][n] = ms[1]
|
2022-11-25 12:24:06 +00:00
|
|
|
out["lambda200c"][n] = clump.lambda200c
|
2022-11-22 10:00:01 +00:00
|
|
|
|
|
|
|
# NFW profile fit
|
|
|
|
if clump.Npart > 10 and numpy.isfinite(out["r200"][n]):
|
|
|
|
nfwpost = csiborgtools.fits.NFWPosterior(clump)
|
|
|
|
logRs, __ = nfwpost.maxpost_logRs()
|
|
|
|
Rs = 10**logRs
|
|
|
|
if not numpy.isnan(logRs):
|
|
|
|
out["Rs"][n] = Rs
|
|
|
|
out["rho0"][n] = nfwpost.rho0_from_Rs(Rs)
|
|
|
|
out["conc"][n] = out["r200"][n] / Rs
|
|
|
|
|
2023-04-04 14:22:00 +00:00
|
|
|
csiborgtools.read.dump_split(out, nsplit, nsnap, nsim, paths)
|
2022-11-22 10:00:01 +00:00
|
|
|
|
|
|
|
# Wait until all jobs finished before moving to another simulation
|
|
|
|
comm.Barrier()
|
|
|
|
|
|
|
|
# Use the rank 0 to combine outputs for this CSiBORG realisation
|
|
|
|
if rank == 0:
|
|
|
|
print("Collecting results!")
|
2022-11-25 10:44:08 +00:00
|
|
|
partreader = csiborgtools.read.ParticleReader(paths)
|
2022-11-22 10:00:01 +00:00
|
|
|
out_collected = csiborgtools.read.combine_splits(
|
2023-04-04 14:22:00 +00:00
|
|
|
utils.Nsplits, nsnap, nsim, partreader, cols_collect,
|
|
|
|
remove_splits=True, verbose=False)
|
2022-11-25 10:44:08 +00:00
|
|
|
fname = join(paths.dumpdir, "ramses_out_{}_{}.npy"
|
2023-04-04 14:22:00 +00:00
|
|
|
.format(str(nsim).zfill(5), str(nsnap).zfill(5)))
|
2022-11-22 10:00:01 +00:00
|
|
|
print("Saving results to `{}`.".format(fname))
|
|
|
|
numpy.save(fname, out_collected)
|
|
|
|
|
|
|
|
comm.Barrier()
|
|
|
|
|
2022-10-30 20:16:56 +00:00
|
|
|
if rank == 0:
|
|
|
|
print("All finished! See ya!")
|