Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/derived_field.py
928 views
1
import yt
2
3
# Load the dataset.
4
ds = yt.load("IsolatedGalaxy/galaxy0030/galaxy0030")
5
6
# You can create a derived field by manipulating any existing derived fields
7
# in any way you choose. In this case, let's just make a simple one:
8
# thermal_energy_density = 3/2 nkT
9
10
11
# First create a function which yields your new derived field
12
def thermal_energy_dens(field, data):
13
return (3 / 2) * data["gas", "number_density"] * data["gas", "kT"]
14
15
16
# Then add it to your dataset and define the units
17
ds.add_field(
18
("gas", "thermal_energy_density"),
19
units="erg/cm**3",
20
function=thermal_energy_dens,
21
sampling_type="cell",
22
)
23
24
# It will now show up in your derived_field_list
25
for i in sorted(ds.derived_field_list):
26
print(i)
27
28
# Let's use it to make a projection
29
ad = ds.all_data()
30
yt.ProjectionPlot(
31
ds,
32
"x",
33
("gas", "thermal_energy_density"),
34
weight_field=("gas", "density"),
35
width=(200, "kpc"),
36
).save()
37
38