Skip to content

Commit

Permalink
all tests run
Browse files Browse the repository at this point in the history
  • Loading branch information
daviesje committed Nov 4, 2024
1 parent 1cd9c90 commit 8e5a734
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
20 changes: 15 additions & 5 deletions src/py21cmfast/wrapper/cfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,8 @@ def halo_sample_test(
# TODO: make this able to take a proper HaloField/PerturbHaloField
# with corresponding Ts/ion/Ic fields for feedback
def convert_halo_properties(
*redshift: float,
*,
redshift: float,
astro_params: AstroParams,
flag_options: FlagOptions,
ics: InitialConditions,
Expand Down Expand Up @@ -1260,13 +1261,19 @@ def convert_halo_properties(
astro_params.cstruct,
flag_options.cstruct,
)
inputs = InputParameters.from_output_structs(
[
ics,
],
astro_params=astro_params,
flag_options=flag_options,
redshift=redshift,
)

# HACK: Make the fake halo list
fake_pthalos = PerturbHaloField(
redshift=redshift,
inputs=inputs,
buffer_size=halo_masses.size,
initial_conditions=ics,
astro_params=astro_params,
flag_options=flag_options,
)
fake_pthalos() # initialise memory
fake_pthalos.halo_masses = halo_masses.astype("f4")
Expand All @@ -1276,6 +1283,9 @@ def convert_halo_properties(
fake_pthalos.xray_rng = halo_rng.astype("f4")
fake_pthalos.n_halos = halo_masses.size

# TODO: ask Steven if this is a memory leak
fake_pthalos._init_cstruct()

# single element zero array to act as the grids (vcb, J_21_LW, z_reion, Gamma12)
zero_array = ffi.cast("float *", np.zeros(1).ctypes.data)

Expand Down
20 changes: 10 additions & 10 deletions tests/test_halo_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
options_hmf = list(cint.OPTIONS_HMF.keys())

options_delta = [-0.9, -0.5, 0, 1, 1.45] # cell densities to draw samples from
options_log10mass = [9, 10, 11, 12] # halo masses to draw samples from
options_log10mass = [9, 10, 11, 12, 13] # halo masses to draw samples from


@pytest.mark.parametrize("name", options_hmf)
Expand Down Expand Up @@ -135,18 +135,18 @@ def test_halo_scaling_relations(ic):
fo = opts["flag_options"]

# setup the halo masses to test
halo_mass_vals = [1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12]
halo_mass_vals = np.array([1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12])
n_halo_per_mass = 1000
halo_masses = np.array(
[n_halo_per_mass * [val] for val in halo_mass_vals]
halo_masses = np.broadcast_to(
halo_mass_vals[:, None], (halo_mass_vals.size, n_halo_per_mass)
).flatten()
halo_rng = np.random.normal(size=n_halo_per_mass * len(halo_mass_vals))
halo_rng = np.random.normal(size=n_halo_per_mass * halo_mass_vals.size)

# (n_halo*n_mass*n_prop) --> (n_prop,n_mass,n_halo)

# mass,star,sfr,xray,nion,wsfr,starmini,sfrmini,mturna,mturnm,mturnr,Z
out_dict = cf.convert_halo_properties(
initial_conditions=ic,
ics=ic,
redshift=redshift,
astro_params=ap,
flag_options=fo,
Expand All @@ -155,14 +155,14 @@ def test_halo_scaling_relations(ic):
)

halo_mass_out = out_dict["halo_mass"].reshape(
(len(halo_mass_vals), n_halo_per_mass)
(halo_mass_vals.size, n_halo_per_mass)
)
halo_stars_out = out_dict["halo_stars"].reshape(
(len(halo_mass_vals), n_halo_per_mass)
(halo_mass_vals.size, n_halo_per_mass)
)
halo_sfr_out = out_dict["halo_sfr"].reshape((len(halo_mass_vals), n_halo_per_mass))
halo_sfr_out = out_dict["halo_sfr"].reshape((halo_mass_vals.size, n_halo_per_mass))
halo_xray_out = out_dict["halo_xray"].reshape(
(len(halo_mass_vals), n_halo_per_mass)
(halo_mass_vals.size, n_halo_per_mass)
)

# assuming same value for all halos
Expand Down

0 comments on commit 8e5a734

Please sign in to comment.