Within halo work and NFW fit (#4)

* add listing of snapshots

* change distance to comoving

* ignore cp files

* rename nb

* add str to list

* add NFW profile shapes

* add fits imports

* Rename to Nsnap

* in clumps_read only select props

* make clumpid int

* expand doc

* add import

* edit readme

* distribute halos

* add profile & posterior

* add import

* add import

* add documentation

* add rvs and init guess

* update todo

* update nb

* add file

* return end index too

* change clump_ids format to int32

* skeleton of dump particle

* update nb

* add func to drop 0 clump indxs parts

* add import

* add halo dump

* switch to float32

* Update TODO

* update TODO

* add func that loads a split

* add halo object

* Rename to clump

* make post work with a clump

* add optimiser

* add Nsplits

* ignore submission scripts

* ignore .out

* add dumppath

* add job splitting

* add split halos script

* rename file

* renaem files

* rm file

* rename imports

* edit desc

* add pick clump

* add number of particles

* update TODO

* update todo

* add script

* add dumping

* change dumpdir structure

* change dumpdir

* add import

* Remove tqdm

* Increase the number of splits

* rm shuffle option

* Change to remove split

* add emojis

* fix part counts in splits

* change num of splits

* rm with particle cut

* keep splits

* fit only if 10 part and more

* add min distance

* rm warning about not set vels

* update TODO

* calculate rho0 too

* add results collection

* add import

* add func to combine splits

* update TODO

* add extract cols

* update nb

* update TODO
This commit is contained in:
Richard Stiskalek 2022-10-30 20:16:56 +00:00 committed by GitHub
parent 85a6a6d58a
commit 8a56c22813
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 3815 additions and 397 deletions

File diff suppressed because one or more lines are too long

92
scripts/run_fit_halos.py Normal file
View file

@ -0,0 +1,92 @@
# 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
realisation must have been split in advance by `run_split_halos`.
"""
import numpy
from os.path import join
from mpi4py import MPI
try:
import csiborgtools
except ModuleNotFoundError:
import sys
sys.path.append("../")
import csiborgtools
import utils
F64 = numpy.float64
I64 = numpy.int64
# Simulations and their snapshot to analyze
Nsims = [9844]
Nsnap = 1016
# Get MPI things
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nproc = comm.Get_size()
dumpdir = utils.dumpdir
loaddir = join(utils.dumpdir, "temp")
cols_collect = [("npart", I64), ("totpartmass", F64), ("logRs", F64),
("rho0", F64)]
# NOTE later loop over sims too
Nsim = Nsims[0]
jobs = csiborgtools.fits.split_jobs(utils.Nsplits, nproc)[rank]
for Nsplit in jobs:
print("Rank {} working on {}.".format(rank, Nsplit))
parts, part_clumps, clumps = csiborgtools.fits.load_split_particles(
Nsplit, loaddir, Nsim, Nsnap, remove_split=False)
N = clumps.size
cols = [("index", I64), ("npart", I64), ("totpartmass", F64),
("logRs", F64), ("rho0", F64)]
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)
clump = csiborgtools.fits.Clump.from_arrays(*xs)
out["npart"][n] = clump.Npart
out["totpartmass"][n] = clump.total_particle_mass
# NFW profile fit
if clump.Npart > 10:
nfwpost = csiborgtools.fits.NFWPosterior(clump)
logRs = nfwpost.maxpost_logRs()
if logRs.success:
out["logRs"][n] = logRs.x
out["rho0"][n] = nfwpost.rho0_from_logRs(logRs.x)
csiborgtools.io.dump_split(out, Nsplit, Nsim, Nsnap, dumpdir)
# Force all ranks to wait
comm.Barrier()
# Use the rank 0 to combine outputs for this CSiBORG realisation
if rank == 0:
print("Collecting results!")
out_collected = csiborgtools.io.combine_splits(
utils.Nsplits, Nsim, Nsnap, utils.dumpdir, cols_collect,
remove_splits=True, verbose=False)
fname = join(utils.dumpdir, "ramses_out_{}_{}.npy"
.format(str(Nsim).zfill(5), str(Nsnap).zfill(5)))
print("Saving results to `{}`.".format(fname))
numpy.save(fname, out_collected)
print("All finished! See ya!")

View file

@ -0,0 +1,51 @@
# 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.
"""
Script to split particles into smaller files according to their clump
membership for faster manipulation. Running this will require a lot of memory.
"""
from tqdm import tqdm
from os.path import join
try:
import csiborgtools
except ModuleNotFoundError:
import sys
sys.path.append("../")
import csiborgtools
import utils
Nsims = [9844]
Nsnap = 1016
partcols = ["x", "y", "z", "M", "level"]
dumpdir = join(utils.dumpdir, "temp")
for Nsim in tqdm(Nsims):
simpath = csiborgtools.io.get_sim_path(Nsim)
# Load the clumps, particles' clump IDs and particles.
clumps = csiborgtools.io.read_clumps(Nsnap, simpath)
particle_clumps = csiborgtools.io.read_clumpid(Nsnap, simpath,
verbose=False)
particles = csiborgtools.io.read_particle(partcols, Nsnap, simpath,
verbose=False)
# Drop all particles whose clump index is 0 (not assigned to any halo)
particle_clumps, particles = csiborgtools.io.drop_zero_indx(
particle_clumps, particles)
# Dump it!
csiborgtools.fits.dump_split_particles(particles, particle_clumps, clumps,
utils.Nsplits, dumpdir, Nsim, Nsnap,
verbose=False)
print("All finished!")

View file

@ -1,352 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "5a38ed25",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T21:53:10.387523Z",
"start_time": "2022-10-20T21:53:08.558627Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"try:\n",
" import csiborgtools\n",
"except ModuleNotFoundError:\n",
" import sys\n",
" sys.path.append(\"../\")\n",
" import csiborgtools\n",
" \n",
" \n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "7accd798",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T22:00:25.350404Z",
"start_time": "2022-10-20T22:00:24.775748Z"
}
},
"outputs": [],
"source": [
"simpath = csiborgtools.io.get_sim_path(9844)\n",
"Nsnap = 1016\n",
"# csiborgtools.io.read_info(1016, simpath)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "131c3107",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T22:25:59.104572Z",
"start_time": "2022-10-20T22:25:59.074460Z"
}
},
"outputs": [],
"source": [
"box = csiborgtools.units.BoxUnits(Nsnap, simpath)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "b1a11aa6",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T22:26:12.931468Z",
"start_time": "2022-10-20T22:26:12.671686Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3.765003070876428e+19"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"box.box2solarmass(1)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "19f39a80",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T22:24:59.563137Z",
"start_time": "2022-10-20T22:24:59.530173Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3.749e+19"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3.749e19"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "5d1413fd",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T22:25:17.251057Z",
"start_time": "2022-10-20T22:25:17.223543Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.9957495198343349"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"box.solarmass2box(3.749e19)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90cf9d61",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b53e8166",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3eeca5b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 49,
"id": "93d32800",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T22:09:12.915516Z",
"start_time": "2022-10-20T22:09:12.856213Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.9988759675724455"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"box.kpc2box(677.7 / 0.705 * 1000)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "66d3e98f",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T22:06:46.471218Z",
"start_time": "2022-10-20T22:06:46.437023Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"678.4626139789958"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"box.box2kpc(1) / 1000 * 0.705"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef00a04c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d201838b",
"metadata": {},
"outputs": [],
"source": [
"filename = \"/mnt/extraspace/hdesmond/ramses_out_9844/output_01016/info_01016.txt\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d012a21b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "547f7944",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T19:52:22.548299Z",
"start_time": "2022-10-20T19:52:22.544342Z"
}
},
"outputs": [],
"source": [
"results_filepath = '/mnt/extraspace/deaglan/H-AGN/'\n",
"sugata_filepath = '/mnt/extraspace/jeg/greenwhale/Sugata/'\n",
"\n",
"def info_dir():\n",
" return sugata_filepath + 'INFO/'\n",
"\n",
"def info_file(nout):\n",
" return info_dir() + 'info_' + str(nout).zfill(5) + '.txt'\n",
"\n",
"def h_agn_particle_directory(nout):\n",
" return sugata_filepath + 'H-AGN/output_' + str(nout).zfill(5) + '/'\n",
"\n",
"def cosmo_dir():\n",
" return './data/'\n",
"\n",
"def cosmo_file(nout):\n",
" return cosmo_dir() + 'cosmo_table_' + str(nout).zfill(5) + '.txt'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30630635",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T19:52:22.777853Z",
"start_time": "2022-10-20T19:52:22.774584Z"
}
},
"outputs": [],
"source": [
"info_dir()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3fc21b2",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T19:56:35.946185Z",
"start_time": "2022-10-20T19:56:35.943643Z"
}
},
"outputs": [],
"source": [
"filename = \"/mnt/extraspace/hdesmond/ramses_out_9844/output_01016/info_01016.txt\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fae40a32",
"metadata": {
"ExecuteTime": {
"end_time": "2022-10-20T20:05:04.410987Z",
"start_time": "2022-10-20T20:05:04.403721Z"
},
"scrolled": false
},
"outputs": [],
"source": [
"e"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de7e3ae7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.0 ('venv_galomatch': virtualenv)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
},
"vscode": {
"interpreter": {
"hash": "f29d02a8350410abc2a9fb79641689d10bf7ab64afc03ec87ca3cf6ed2daa499"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -28,6 +28,10 @@ except ModuleNotFoundError:
sys.path.append("../")
Nsplits = 200
dumpdir = "/mnt/extraspace/rstiskalek/csiborg/"
def load_mmain_convert(n):
srcdir = "/users/hdesmond/Mmain"
arr = csiborgtools.io.read_mmain(n, srcdir)