Write particles to ASCII (#117)

* Add option to output high-resolution particles only

* Add ASCII writing of simulatios
This commit is contained in:
Richard Stiskalek 2024-03-19 08:23:52 +00:00 committed by GitHub
parent d0266584fa
commit 0a859a9a06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 25 deletions

View File

@ -474,7 +474,7 @@ class CSiBORG2Snapshot(BaseSnapshot):
self._kind = value self._kind = value
def _get_particles(self, kind): def _get_particles(self, kind, high_resolution_only=False):
with File(self._snapshot_path, "r") as f: with File(self._snapshot_path, "r") as f:
if kind == "Masses": if kind == "Masses":
npart = f["Header"].attrs["NumPart_Total"][1] npart = f["Header"].attrs["NumPart_Total"][1]
@ -483,6 +483,7 @@ class CSiBORG2Snapshot(BaseSnapshot):
else: else:
x = f[f"PartType1/{kind}"][...] x = f[f"PartType1/{kind}"][...]
if not high_resolution_only:
if x.ndim == 1: if x.ndim == 1:
x = numpy.hstack([x, f[f"PartType5/{kind}"][...]]) x = numpy.hstack([x, f[f"PartType5/{kind}"][...]])
else: else:
@ -493,17 +494,17 @@ class CSiBORG2Snapshot(BaseSnapshot):
return x return x
def coordinates(self): def coordinates(self, high_resolution_only=False):
return self._get_particles("Coordinates") return self._get_particles("Coordinates", high_resolution_only)
def velocities(self): def velocities(self, high_resolution_only=False):
return self._get_particles("Velocities") return self._get_particles("Velocities", high_resolution_only)
def masses(self): def masses(self, high_resolution_only=False):
return self._get_particles("Masses") * 1e10 return self._get_particles("Masses", high_resolution_only) * 1e10
def particle_ids(self): def particle_ids(self, high_resolution_only=False):
return self._get_particles("ParticleIDs") return self._get_particles("ParticleIDs", high_resolution_only)
def _get_halo_particles(self, halo_id, kind, is_group): def _get_halo_particles(self, halo_id, kind, is_group):
if not is_group: if not is_group:

View File

@ -19,14 +19,48 @@ galaxies in a survey as an ASCII file.
from os.path import join from os.path import join
import csiborgtools import csiborgtools
import numpy import numpy as np
from tqdm import tqdm from tqdm import tqdm
DIR_OUT = "/mnt/extraspace/rstiskalek/csiborg_postprocessing/ascii_positions" DIR_OUT = "/mnt/extraspace/rstiskalek/csiborg_postprocessing/ascii_positions"
def process_simulation(simname): def write_simulation(simname, high_resolution_only=True):
"""Watch out about the distinction between real and redshift space.""" """"
Write the positions, velocities and IDs of the particles in a simulation
to an ASCII file. The output is `X Y Z VX VY VZ ID`.
"""
if not high_resolution_only:
raise RuntimeError("Writing low-res particles is not implemented.")
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
if "csiborg2" in simname:
nsims = paths.get_ics(simname)
kind = simname.split("_")[-1]
for nsim in tqdm(nsims, desc="Simulations"):
reader = csiborgtools.read.CSiBORG2Snapshot(nsim, 99, kind, paths,
flip_xz=False)
x = np.hstack(
[reader.coordinates(high_resolution_only=True),
reader.velocities(high_resolution_only=True),
reader.particle_ids(high_resolution_only=True).reshape(-1, 1)]
)
# Store positions and velocities with 6 decimal places and IDs as
# integers.
fmt_string = "%1.6f %1.6f %1.6f %1.6f %1.6f %1.6f %d"
fname = join(DIR_OUT, f"high_res_particles_{simname}_{nsim}.txt")
np.savetxt(fname, x, fmt=fmt_string)
else:
raise RuntimeError("Simulation not implemented..")
def write_halos(simname):
"""
Watch out about the distinction between real and redshift space. The output
is `X Y Z MASS`.
"""
paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring) paths = csiborgtools.read.Paths(**csiborgtools.paths_glamdring)
if "csiborg2" in simname: if "csiborg2" in simname:
nsims = paths.get_ics(simname) nsims = paths.get_ics(simname)
@ -37,16 +71,16 @@ def process_simulation(simname):
pos = cat["cartesian_pos"] pos = cat["cartesian_pos"]
mass = cat["totmass"] mass = cat["totmass"]
# Stack positions and masses # Stack positions and masses
x = numpy.hstack([pos, mass.reshape(-1, 1)]) x = np.hstack([pos, mass.reshape(-1, 1)])
# Save to a file # Save to a file
fname = join(DIR_OUT, f"halos_real_{simname}_{nsim}.txt") fname = join(DIR_OUT, f"halos_real_{simname}_{nsim}.txt")
numpy.savetxt(fname, x) np.savetxt(fname, x)
else: else:
raise RuntimeError("Simulation not implemented..") raise RuntimeError("Simulation not implemented..")
def process_survey(survey_name, boxsize): def write_survey(survey_name, boxsize):
"""Watch out about the distance definition.""" """Watch out about the distance definition."""
if survey_name == "SDSS": if survey_name == "SDSS":
survey = csiborgtools.SDSS()() survey = csiborgtools.SDSS()()
@ -58,19 +92,21 @@ def process_survey(survey_name, boxsize):
raise RuntimeError("Survey not implemented..") raise RuntimeError("Survey not implemented..")
# Convert to Cartesian coordinates # Convert to Cartesian coordinates
X = numpy.vstack([dist, ra, dec]).T X = np.vstack([dist, ra, dec]).T
X = csiborgtools.radec_to_cartesian(X) X = csiborgtools.radec_to_cartesian(X)
# Center the coordinates in the box # Center the coordinates in the box
X += boxsize / 2 X += boxsize / 2
fname = join(DIR_OUT, f"survey_{survey_name}.txt") fname = join(DIR_OUT, f"survey_{survey_name}.txt")
numpy.savetxt(fname, X) np.savetxt(fname, X)
if __name__ == "__main__": if __name__ == "__main__":
# process_simulation("csiborg2_main") write_simulation("csiborg2_main")
boxsize = 676.6 # write_halos("csiborg2_main")
for survey in ["SDSS", "SDSSxALFALFA"]:
process_survey(survey, boxsize) # boxsize = 676.6
# for survey in ["SDSS", "SDSSxALFALFA"]:
# write_survey(survey, boxsize)