2023-02-05 11:46:19 +00:00
|
|
|
# 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.
|
2023-03-27 08:22:03 +00:00
|
|
|
"""A script to calculate overlap between two CSiBORG realisations."""
|
2023-02-05 11:46:19 +00:00
|
|
|
from argparse import ArgumentParser
|
2023-05-06 15:52:48 +00:00
|
|
|
from copy import deepcopy
|
2023-02-05 11:46:19 +00:00
|
|
|
from datetime import datetime
|
2023-04-20 23:35:06 +00:00
|
|
|
from distutils.util import strtobool
|
2023-04-18 09:02:36 +00:00
|
|
|
|
2023-03-27 08:22:03 +00:00
|
|
|
import numpy
|
|
|
|
from scipy.ndimage import gaussian_filter
|
2023-04-18 09:02:36 +00:00
|
|
|
|
2023-02-05 11:46:19 +00:00
|
|
|
try:
|
|
|
|
import csiborgtools
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
import sys
|
2023-04-20 23:35:06 +00:00
|
|
|
|
2023-02-05 11:46:19 +00:00
|
|
|
sys.path.append("../")
|
|
|
|
import csiborgtools
|
2023-05-09 15:18:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pair_match(nsim0, nsimx, sigma, smoothen, verbose):
|
2023-05-06 15:52:48 +00:00
|
|
|
from csiborgtools.read import HaloCatalogue, read_h5
|
2023-04-18 09:02:36 +00:00
|
|
|
|
2023-05-13 16:37:34 +00:00
|
|
|
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
|
2023-05-09 15:18:01 +00:00
|
|
|
smooth_kwargs = {"sigma": sigma, "mode": "constant", "cval": 0.0}
|
|
|
|
overlapper = csiborgtools.match.ParticleOverlap()
|
|
|
|
matcher = csiborgtools.match.RealisationsMatcher()
|
|
|
|
|
|
|
|
# Load the raw catalogues (i.e. no selection) including the initial CM
|
|
|
|
# positions and the particle archives.
|
2023-05-15 22:30:10 +00:00
|
|
|
bounds = {"totpartmass": (1e12, None)}
|
|
|
|
cat0 = HaloCatalogue(nsim0, paths, load_initial=True, bounds=bounds,
|
|
|
|
with_lagpatch=True, load_clumps_cat=True)
|
|
|
|
catx = HaloCatalogue(nsimx, paths, load_initial=True, bounds=bounds,
|
|
|
|
with_lagpatch=True, load_clumps_cat=True)
|
2023-05-09 15:18:01 +00:00
|
|
|
|
2023-05-21 21:46:28 +00:00
|
|
|
clumpmap0 = read_h5(paths.particles(nsim0))["clumpmap"]
|
|
|
|
parts0 = read_h5(paths.initmatch(nsim0, "particles"))["particles"]
|
2023-05-09 15:18:01 +00:00
|
|
|
clid2map0 = {clid: i for i, clid in enumerate(clumpmap0[:, 0])}
|
|
|
|
|
2023-05-21 21:46:28 +00:00
|
|
|
clumpmapx = read_h5(paths.particles(nsimx))["clumpmap"]
|
|
|
|
partsx = read_h5(paths.initmatch(nsimx, "particles"))["particles"]
|
2023-05-09 15:18:01 +00:00
|
|
|
clid2mapx = {clid: i for i, clid in enumerate(clumpmapx[:, 0])}
|
|
|
|
|
|
|
|
# We generate the background density fields. Loads halos's particles one by
|
|
|
|
# one from the archive, concatenates them and calculates the NGP density
|
|
|
|
# field.
|
|
|
|
if verbose:
|
|
|
|
print(f"{datetime.now()}: generating the background density fields.",
|
|
|
|
flush=True)
|
|
|
|
delta_bckg = overlapper.make_bckg_delta(parts0, clumpmap0, clid2map0, cat0,
|
|
|
|
verbose=verbose)
|
|
|
|
delta_bckg = overlapper.make_bckg_delta(partsx, clumpmapx, clid2mapx, catx,
|
|
|
|
delta=delta_bckg, verbose=verbose)
|
|
|
|
|
|
|
|
# We calculate the overlap between the NGP fields.
|
|
|
|
if verbose:
|
|
|
|
print(f"{datetime.now()}: crossing the simulations.", flush=True)
|
|
|
|
match_indxs, ngp_overlap = matcher.cross(cat0, catx, parts0, partsx,
|
|
|
|
clumpmap0, clumpmapx, delta_bckg,
|
|
|
|
verbose=verbose)
|
|
|
|
# We wish to store the halo IDs of the matches, not their array positions
|
|
|
|
# in the catalogues
|
|
|
|
match_hids = deepcopy(match_indxs)
|
|
|
|
for i, matches in enumerate(match_indxs):
|
|
|
|
for j, match in enumerate(matches):
|
|
|
|
match_hids[i][j] = catx["index"][match]
|
|
|
|
|
2023-05-21 21:46:28 +00:00
|
|
|
fout = paths.overlap(nsim0, nsimx, smoothed=False)
|
2023-05-09 15:18:01 +00:00
|
|
|
numpy.savez(fout, ref_hids=cat0["index"], match_hids=match_hids,
|
|
|
|
ngp_overlap=ngp_overlap)
|
|
|
|
if verbose:
|
|
|
|
print(f"{datetime.now()}: calculated NGP overlap, saved to {fout}.",
|
|
|
|
flush=True)
|
|
|
|
|
|
|
|
if not smoothen:
|
|
|
|
quit()
|
|
|
|
|
|
|
|
# We now smoothen up the background density field for the smoothed overlap
|
|
|
|
# calculation.
|
|
|
|
if verbose:
|
|
|
|
print(f"{datetime.now()}: smoothing the background field.", flush=True)
|
|
|
|
gaussian_filter(delta_bckg, output=delta_bckg, **smooth_kwargs)
|
|
|
|
|
|
|
|
# We calculate the smoothed overlap for the pairs whose NGP overlap is > 0.
|
|
|
|
smoothed_overlap = matcher.smoothed_cross(cat0, catx, parts0, partsx,
|
|
|
|
clumpmap0, clumpmapx, delta_bckg,
|
2023-05-09 16:43:18 +00:00
|
|
|
match_indxs, smooth_kwargs,
|
|
|
|
verbose=verbose)
|
2023-05-09 15:18:01 +00:00
|
|
|
|
2023-05-21 21:46:28 +00:00
|
|
|
fout = paths.overlap(nsim0, nsimx, smoothed=True)
|
2023-05-09 15:18:01 +00:00
|
|
|
numpy.savez(fout, smoothed_overlap=smoothed_overlap, sigma=sigma)
|
|
|
|
if verbose:
|
|
|
|
print(f"{datetime.now()}: calculated smoothing, saved to {fout}.",
|
|
|
|
flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument("--nsim0", type=int)
|
|
|
|
parser.add_argument("--nsimx", type=int)
|
|
|
|
parser.add_argument("--sigma", type=float, default=None)
|
|
|
|
parser.add_argument("--smoothen", type=lambda x: bool(strtobool(x)),
|
|
|
|
default=None)
|
|
|
|
parser.add_argument("--verbose", type=lambda x: bool(strtobool(x)),
|
|
|
|
default=False)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
pair_match(args.nsim0, args.nsimx, args.sigma, args.smoothen, args.verbose)
|