Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/hse_field.py
928 views
1
import numpy as np
2
3
import yt
4
5
# Open a dataset from when there's a lot of sloshing going on.
6
7
ds = yt.load("GasSloshingLowRes/sloshing_low_res_hdf5_plt_cnt_0350")
8
9
# Define the components of the gravitational acceleration vector field by
10
# taking the gradient of the gravitational potential
11
grad_fields = ds.add_gradient_fields(("gas", "gravitational_potential"))
12
13
# We don't need to do the same for the pressure field because yt already
14
# has pressure gradient fields. Now, define the "degree of hydrostatic
15
# equilibrium" field.
16
17
18
def _hse(field, data):
19
# Remember that g is the negative of the potential gradient
20
gx = -data["gas", "density"] * data["gas", "gravitational_potential_gradient_x"]
21
gy = -data["gas", "density"] * data["gas", "gravitational_potential_gradient_y"]
22
gz = -data["gas", "density"] * data["gas", "gravitational_potential_gradient_z"]
23
hx = data["gas", "pressure_gradient_x"] - gx
24
hy = data["gas", "pressure_gradient_y"] - gy
25
hz = data["gas", "pressure_gradient_z"] - gz
26
h = np.sqrt((hx * hx + hy * hy + hz * hz) / (gx * gx + gy * gy + gz * gz))
27
return h
28
29
30
ds.add_field(
31
("gas", "HSE"),
32
function=_hse,
33
units="",
34
take_log=False,
35
display_name="Hydrostatic Equilibrium",
36
sampling_type="cell",
37
)
38
39
# The gradient operator requires periodic boundaries. This dataset has
40
# open boundary conditions.
41
ds.force_periodicity()
42
43
# Take a slice through the center of the domain
44
slc = yt.SlicePlot(ds, 2, [("gas", "density"), ("gas", "HSE")], width=(1, "Mpc"))
45
46
slc.save("hse")
47
48