Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/analyzing/ionization_cube.py
928 views
1
import time
2
3
import h5py
4
import numpy as np
5
6
import yt
7
from yt.utilities.parallel_tools.parallel_analysis_interface import communication_system
8
9
10
@yt.derived_field(
11
name="IonizedHydrogen", units="", display_name=r"\frac{\rho_{HII}}{\rho_H}"
12
)
13
def IonizedHydrogen(field, data):
14
return data["gas", "HII_Density"] / (
15
data["gas", "HI_Density"] + data["gas", "HII_Density"]
16
)
17
18
19
ts = yt.DatasetSeries("SED800/DD*/*.index", parallel=8)
20
21
ionized_z = np.zeros(ts[0].domain_dimensions, dtype="float32")
22
23
t1 = time.time()
24
for ds in ts.piter():
25
z = ds.current_redshift
26
for g in yt.parallel_objects(ds.index.grids, njobs=16):
27
i1, j1, k1 = g.get_global_startindex() # Index into our domain
28
i2, j2, k2 = g.get_global_startindex() + g.ActiveDimensions
29
# Look for the newly ionized gas
30
newly_ion = (g["IonizedHydrogen"] > 0.999) & (
31
ionized_z[i1:i2, j1:j2, k1:k2] < z
32
)
33
ionized_z[i1:i2, j1:j2, k1:k2][newly_ion] = z
34
g.clear_data()
35
36
print(f"Iteration completed {time.time() - t1:0.3e}")
37
comm = communication_system.communicators[-1]
38
for i in range(ionized_z.shape[0]):
39
ionized_z[i, :, :] = comm.mpi_allreduce(ionized_z[i, :, :], op="max")
40
print("Slab % 3i has minimum z of %0.3e" % (i, ionized_z[i, :, :].max()))
41
t2 = time.time()
42
print(f"Completed. {t2 - t1:0.3e}")
43
44
if comm.rank == 0:
45
f = h5py.File("IonizationCube.h5", mode="w")
46
f.create_dataset("/z", data=ionized_z)
47
48