Skip to content

Commit fee6091

Browse files
authored
gh-19: add steps() to iterate over steps (#20)
1 parent 1b03dec commit fee6091

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

glass/ext/pkdgrav/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
__all__ = [
66
"ClassCosmology",
7-
"SimpleCosmology",
87
"ParfileError",
8+
"SimpleCosmology",
9+
"Simulation",
10+
"Step",
911
"load",
1012
"read_gowerst",
13+
"steps",
1114
]
1215

1316
from ._cosmology import ClassCosmology, SimpleCosmology
1417
from ._gowerst import read_gowerst
1518
from ._parfile import ParfileError
16-
from ._pkdgrav import load
19+
from ._pkdgrav import Simulation, Step, load, steps

glass/ext/pkdgrav/_gowerst.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import dataclasses
12
import os
2-
from typing import Literal
33
from collections.abc import Iterator
4+
from typing import Literal
45

56
import healpy as hp
67
import numpy as np
78

8-
from ._pkdgrav import Simulation
9+
from ._pkdgrav import Simulation, steps
910

1011

1112
class NpyLoader:
@@ -63,36 +64,26 @@ def read_gowerst(
6364
else:
6465
raise ValueError(f"unknown format: {format}")
6566

66-
# pre-compute some simulation properties
67-
boxsize = sim.parameters["dBoxSize"] / sim.cosmology.h
68-
density = (sim.parameters["nGrid"] / boxsize) ** 3
69-
70-
steps = range(sim.parameters["nSteps"], 0, -1)
71-
redshifts = sim.redshifts
72-
for step, z_near, z_far in zip(steps, redshifts, redshifts[1:]):
73-
if zmax is not None and zmax < z_near:
67+
# iterate shells
68+
for step in steps(sim):
69+
if zmax is not None and zmax < step.near_redshift:
7470
break
7571

76-
data = loader(step)
72+
data = loader(step.step)
7773

78-
comoving_volume = sim.cosmology.comoving_volume(z_near, z_far)
74+
# number of particles in shell
75+
particles = data.sum()
7976

80-
metadata = {
81-
"comoving volume": comoving_volume,
82-
"far redshift": z_far,
83-
"mean density": density,
84-
"mean particles": density * comoving_volume,
85-
"near redshift": z_near,
86-
"particles": data.sum(),
87-
}
77+
metadata = dataclasses.asdict(step)
78+
metadata["particles"] = particles
8879

8980
if nside is not None and nside != hp.get_nside(data):
9081
# keep the number of particles constant
9182
data = hp.ud_grade(data, nside, power=-2)
92-
assert data.sum() == metadata["particles"], "resampling lost particles!"
83+
assert data.sum() == particles, "resampling lost particles!"
9384

9485
if not raw:
95-
nbar = metadata["mean particles"] / data.size
86+
nbar = step.mean_particles / data.size
9687
data = data / nbar - 1.0
9788

9889
# attach metadata

glass/ext/pkdgrav/_pkdgrav.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import dataclasses
22
import os
3+
from collections.abc import Iterator
34

45
import numpy as np
56

@@ -52,5 +53,43 @@ def __post_init__(self):
5253
self.redshifts = z[::-1]
5354

5455

56+
@dataclasses.dataclass
57+
class Step:
58+
"""
59+
Metadata for simulation steps.
60+
"""
61+
62+
step: int
63+
near_redshift: float
64+
far_redshift: float
65+
comoving_volume: float
66+
mean_density: float
67+
mean_particles: float
68+
69+
5570
def load(path: str | os.PathLike[str]) -> Simulation:
5671
return Simulation(path)
72+
73+
74+
def steps(sim: Simulation) -> Iterator[Step]:
75+
"""
76+
Returns an iterator of metadata for each simulation step.
77+
"""
78+
79+
# pre-compute some simulation properties
80+
boxsize = sim.parameters["dBoxSize"] / sim.cosmology.h
81+
density = (sim.parameters["nGrid"] / boxsize) ** 3
82+
83+
steps = range(sim.parameters["nSteps"], 0, -1)
84+
redshifts = sim.redshifts
85+
for step, z_near, z_far in zip(steps, redshifts, redshifts[1:]):
86+
comoving_volume = sim.cosmology.comoving_volume(z_near, z_far)
87+
88+
yield Step(
89+
step=step,
90+
near_redshift=z_near,
91+
far_redshift=z_far,
92+
comoving_volume=comoving_volume,
93+
mean_density=density,
94+
mean_particles=density * comoving_volume,
95+
)

0 commit comments

Comments
 (0)