Update evaluate density scripts (#105)

* Edit docs

* Updated interpolated field paths

* Update field sampling script

* Add comments about flipping fields

* Fix little typo

* Edit docs

* Edit hard-coded values

* Fix paths issue

* Add docs

* Switch uncorrected dist to corrected

* Improve error message

* Convert numpy int to Python int

* Add flip of x and z

* Update README

* Edit README

* Fix bug in velocity field calculation

* Fix simple bug

* Add checked axes flipping

* Fix field units

* Update README
This commit is contained in:
Richard Stiskalek 2024-01-08 13:56:22 +01:00 committed by GitHub
parent f61f69dfab
commit 1a5477805a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 268 additions and 167 deletions

View file

@ -249,17 +249,18 @@ class VelocityField(BaseField):
batch_vel = force_single_precision(batch_vel)
batch_mass = force_single_precision(batch_mass)
vel *= mass.reshape(-1, 1)
batch_vel *= batch_mass.reshape(-1, 1)
for i in range(3):
MASL.MA(pos, rho_vel[i], self.boxsize, self.MAS, W=vel[:, i],
verbose=False)
MASL.MA(batch_pos, rho_vel[i], self.boxsize, self.MAS,
W=batch_vel[:, i], verbose=False)
MASL.MA(pos, cellcounts, self.boxsize, self.MAS, W=mass,
verbose=False)
MASL.MA(batch_pos, cellcounts, self.boxsize, self.MAS,
W=batch_mass, verbose=False)
if end == nparts:
break
start = end
for i in range(3):
@ -272,7 +273,7 @@ class VelocityField(BaseField):
def radial_velocity(rho_vel, observer_velocity):
"""
Calculate the radial velocity field around the observer in the centre
of the box.
of the box, such that the observer velocity is 0.
Parameters
----------

View file

@ -367,10 +367,7 @@ class Paths:
-------
str
"""
if MAS == "SPH":
if kind not in ["density", "velocity"]:
raise ValueError("SPH field must be either `density` or `velocity`.") # noqa
if MAS == "SPH" and kind in ["density", "velocity"]:
if simname == "csiborg1":
raise ValueError("SPH field not available for CSiBORG1.")
elif simname == "csiborg2_main":
@ -416,7 +413,7 @@ class Paths:
fname = f"observer_peculiar_velocity_{simname}_{MAS}_{str(nsim).zfill(5)}_{grid}.npz" # noqa
return join(fdir, fname)
def field_interpolated(self, survey, kind, MAS, grid, nsim, in_rsp):
def field_interpolated(self, survey, simname, nsim, kind, MAS, grid):
"""
Path to the files containing the CSiBORG interpolated field for a given
survey.
@ -425,35 +422,32 @@ class Paths:
----------
survey : str
Survey name.
simname : str
Simulation name.
nsim : int
IC realisation index.
kind : str
Field type. Must be one of: `density`, `velocity`, `potential`,
`radvel`, `environment`.
Field type.
MAS : str
Mass-assignment scheme.
grid : int
Grid size.
nsim : int
IC realisation index.
in_rsp : bool
Whether the calculation is performed in redshift space.
Returns
-------
str
"""
raise NotImplementedError("This function is not implemented yet.")
assert kind in ["density", "velocity", "potential", "radvel",
"environment"]
fdir = join(self.postdir, "environment_interpolated")
if "csiborg" not in simname:
raise ValueError("Interpolated field only available for CSiBORG.")
if kind not in ["density", "potential", "radvel"]:
raise ValueError("Unsupported field type.")
fdir = join(self.postdir, "field_interpolated")
try_create_directory(fdir)
if in_rsp:
kind = kind + "_rsp"
fname = f"{survey}_{kind}_{MAS}_{str(nsim).zfill(5)}_grid{grid}.npz"
return join(fdir, fname)
nsim = str(nsim).zfill(5)
return join(fdir, f"{survey}_{simname}_{kind}_{MAS}_{nsim}_{grid}.npz")
def cross_nearest(self, simname, run, kind, nsim=None, nobs=None):
"""

View file

@ -516,10 +516,12 @@ class BaseField(ABC):
Base class for reading fields such as density or velocity fields.
"""
def __init__(self, nsim, paths):
if isinstance(nsim, numpy.integer):
nsim = int(nsim)
if not isinstance(nsim, int):
raise TypeError("`nsim` must be an integer")
self._nsim = nsim
raise TypeError(f"`nsim` must be an integer. Received `{type(nsim)}`.") # noqa
self._nsim = nsim
self._paths = paths
@property
@ -542,6 +544,8 @@ class BaseField(ABC):
-------
Paths
"""
if self._paths is None:
self._paths = Paths(**paths_glamdring)
return self._paths
@abstractmethod
@ -594,11 +598,12 @@ class CSiBORG1Field(BaseField):
----------
nsim : int
Simulation index.
paths : Paths
Paths object.
paths : Paths, optional
Paths object. By default, the paths are set to the `glamdring` paths.
"""
def __init__(self, nsim, paths):
def __init__(self, nsim, paths=None):
super().__init__(nsim, paths)
self._simname = "csiborg1"
def density_field(self, MAS, grid):
fpath = self.paths.field("density", MAS, grid, self.nsim, "csiborg1")
@ -606,9 +611,14 @@ class CSiBORG1Field(BaseField):
if MAS == "SPH":
with File(fpath, "r") as f:
field = f["density"][:]
field /= (677.7 * 1e3 / grid)**3 # Convert to h^2 Msun / kpc^3
else:
field = numpy.load(fpath)
# Flip x- and z-axes
if self._simname == "csiborg1":
field = field.T
return field
def velocity_field(self, MAS, grid):
@ -624,6 +634,13 @@ class CSiBORG1Field(BaseField):
else:
field = numpy.load(fpath)
# Flip x- and z-axes
if self._simname == "csiborg1":
field[0, ...] = field[0, ...].T
field[1, ...] = field[1, ...].T
field[2, ...] = field[2, ...].T
field[[0, 2], ...] = field[[2, 0], ...]
return field
@ -640,13 +657,13 @@ class CSiBORG2Field(BaseField):
----------
nsim : int
Simulation index.
paths : Paths
Paths object.
kind : str
CSiBORG2 run kind. One of `main`, `random`, or `varysmall`.
paths : Paths, optional
Paths object. By default, the paths are set to the `glamdring` paths.
"""
def __init__(self, nsim, paths, kind):
def __init__(self, nsim, kind, paths=None):
super().__init__(nsim, paths)
self.kind = kind
@ -675,11 +692,11 @@ class CSiBORG2Field(BaseField):
with File(fpath, "r") as f:
field = f["density"][:]
field *= 1e10 # Convert to Msun / h
field /= (676.6 * 1e3 / 1024)**3 # Convert to h^2 Msun / kpc^3
field = field.T # Flip x- and z-axes
field /= (676.6 * 1e3 / grid)**3 # Convert to h^2 Msun / kpc^3
else:
field = numpy.load(fpath)
field = field.T # Flip x- and z-axes
return field
def velocity_field(self, MAS, grid):
@ -688,7 +705,6 @@ class CSiBORG2Field(BaseField):
if MAS == "SPH":
with File(fpath, "r") as f:
# TODO: the x and z still have to be flipped.
density = f["density"][:]
v0 = f["p0"][:] / density
v1 = f["p1"][:] / density
@ -697,6 +713,12 @@ class CSiBORG2Field(BaseField):
else:
field = numpy.load(fpath)
# Flip x- and z-axes
field[0, ...] = field[0, ...].T
field[1, ...] = field[1, ...].T
field[2, ...] = field[2, ...].T
field[[0, 2], ...] = field[[2, 0], ...]
return field
@ -718,6 +740,7 @@ class QuijoteField(CSiBORG1Field):
"""
def __init__(self, nsim, paths):
super().__init__(nsim, paths)
self._simname = "quijote"
###############################################################################