2023-02-05 11:46:19 +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.
|
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
|
|
|
|
from datetime import datetime
|
2023-04-18 09:02:36 +00:00
|
|
|
from os.path import join
|
|
|
|
|
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
|
|
|
|
sys.path.append("../")
|
|
|
|
import csiborgtools
|
2023-04-18 09:02:36 +00:00
|
|
|
|
2023-02-05 11:46:19 +00:00
|
|
|
import utils
|
|
|
|
|
|
|
|
# Argument parser
|
|
|
|
parser = ArgumentParser()
|
2023-03-09 14:55:38 +00:00
|
|
|
parser.add_argument("--nsim0", type=int)
|
|
|
|
parser.add_argument("--nsimx", type=int)
|
2023-02-05 11:46:19 +00:00
|
|
|
parser.add_argument("--nmult", type=float)
|
2023-03-27 08:22:03 +00:00
|
|
|
parser.add_argument("--sigma", type=float)
|
2023-02-05 11:46:19 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# File paths
|
2023-04-14 22:24:52 +00:00
|
|
|
paths = csiborgtools.read.CSiBORGPaths(**csiborgtools.paths_glamdring)
|
2023-03-27 08:22:03 +00:00
|
|
|
fout = join(utils.dumpdir, "overlap",
|
|
|
|
"cross_{}_{}.npz".format(args.nsim0, args.nsimx))
|
|
|
|
smooth_kwargs = {"sigma": args.sigma, "mode": "constant", "cval": 0.0}
|
|
|
|
overlapper = csiborgtools.match.ParticleOverlap()
|
2023-02-05 11:46:19 +00:00
|
|
|
|
2023-03-27 08:22:03 +00:00
|
|
|
# Load catalogues
|
|
|
|
print("{}: loading catalogues {} and {}."
|
|
|
|
.format(datetime.now(), args.nsim0, args.nsimx), flush=True)
|
2023-04-18 09:02:36 +00:00
|
|
|
cat0 = csiborgtools.read.ClumpsCatalogue(args.nsim0, paths)
|
|
|
|
catx = csiborgtools.read.ClumpsCatalogue(args.nsimx, paths)
|
2023-02-05 11:46:19 +00:00
|
|
|
|
2023-03-27 08:22:03 +00:00
|
|
|
|
|
|
|
print("{}: loading simulation {} and converting positions to cell numbers."
|
|
|
|
.format(datetime.now(), args.nsim0), flush=True)
|
2023-04-18 09:02:36 +00:00
|
|
|
|
|
|
|
with open(paths.initmatch_path(args.nsim0, "particles"), "rb") as f:
|
2023-03-27 08:22:03 +00:00
|
|
|
clumps0 = numpy.load(f, allow_pickle=True)
|
|
|
|
overlapper.clumps_pos2cell(clumps0)
|
|
|
|
print("{}: loading simulation {} and converting positions to cell numbers."
|
|
|
|
.format(datetime.now(), args.nsimx), flush=True)
|
2023-04-18 09:02:36 +00:00
|
|
|
with open(paths.initmatch_path(args.nsimx, "particles"), 'rb') as f:
|
2023-03-27 08:22:03 +00:00
|
|
|
clumpsx = numpy.load(f, allow_pickle=True)
|
|
|
|
overlapper.clumps_pos2cell(clumpsx)
|
|
|
|
|
|
|
|
|
|
|
|
print("{}: generating the background density fields.".format(datetime.now()),
|
|
|
|
flush=True)
|
|
|
|
delta_bckg = overlapper.make_bckg_delta(clumps0)
|
|
|
|
delta_bckg = overlapper.make_bckg_delta(clumpsx, delta=delta_bckg)
|
|
|
|
|
|
|
|
|
2023-02-05 11:46:19 +00:00
|
|
|
print("{}: crossing the simulations.".format(datetime.now()), flush=True)
|
2023-03-27 08:22:03 +00:00
|
|
|
matcher = csiborgtools.match.RealisationsMatcher()
|
|
|
|
ref_indxs, cross_indxs, match_indxs, ngp_overlap = matcher.cross(
|
|
|
|
cat0, catx, clumps0, clumpsx, delta_bckg)
|
|
|
|
|
|
|
|
|
|
|
|
print("{}: smoothing the background field.".format(datetime.now()), flush=True)
|
|
|
|
gaussian_filter(delta_bckg, output=delta_bckg, **smooth_kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
print("{}: calculating smoothed overlaps.".format(datetime.now()), flush=True)
|
|
|
|
smoothed_overlap = matcher.smoothed_cross(clumps0, clumpsx, delta_bckg,
|
|
|
|
ref_indxs, cross_indxs, match_indxs,
|
|
|
|
smooth_kwargs)
|
2023-03-09 14:55:38 +00:00
|
|
|
|
2023-02-05 11:46:19 +00:00
|
|
|
# Dump the result
|
|
|
|
print("Saving results to `{}`.".format(fout), flush=True)
|
|
|
|
with open(fout, "wb") as f:
|
2023-03-15 20:55:50 +00:00
|
|
|
numpy.savez(fout, ref_indxs=ref_indxs, cross_indxs=cross_indxs,
|
2023-03-27 08:22:03 +00:00
|
|
|
match_indxs=match_indxs, ngp_overlap=ngp_overlap,
|
|
|
|
smoothed_overlap=smoothed_overlap, sigma=args.sigma)
|
2023-04-18 09:02:36 +00:00
|
|
|
print("All finished.", flush=True)
|