Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yt-project
GitHub Repository: yt-project/yt
Path: blob/main/doc/source/cookbook/find_clumps.py
928 views
1
import numpy as np
2
3
import yt
4
from yt.data_objects.level_sets.api import Clump, find_clumps
5
6
ds = yt.load("IsolatedGalaxy/galaxy0030/galaxy0030")
7
8
data_source = ds.disk([0.5, 0.5, 0.5], [0.0, 0.0, 1.0], (8, "kpc"), (1, "kpc"))
9
10
# the field to be used for contouring
11
field = ("gas", "density")
12
13
# This is the multiplicative interval between contours.
14
step = 2.0
15
16
# Now we set some sane min/max values between which we want to find contours.
17
# This is how we tell the clump finder what to look for -- it won't look for
18
# contours connected below or above these threshold values.
19
c_min = 10 ** np.floor(np.log10(data_source[field]).min())
20
c_max = 10 ** np.floor(np.log10(data_source[field]).max() + 1)
21
22
# Now find get our 'base' clump -- this one just covers the whole domain.
23
master_clump = Clump(data_source, field)
24
25
# Add a "validator" to weed out clumps with less than 20 cells.
26
# As many validators can be added as you want.
27
master_clump.add_validator("min_cells", 20)
28
29
# Calculate center of mass for all clumps.
30
master_clump.add_info_item("center_of_mass")
31
32
# Begin clump finding.
33
find_clumps(master_clump, c_min, c_max, step)
34
35
# Save the clump tree as a reloadable dataset
36
fn = master_clump.save_as_dataset(fields=[("gas", "density"), ("all", "particle_mass")])
37
38
# We can traverse the clump hierarchy to get a list of all of the 'leaf' clumps
39
leaf_clumps = master_clump.leaves
40
41
# Get total cell and particle masses for each leaf clump
42
leaf_masses = [leaf.quantities.total_mass() for leaf in leaf_clumps]
43
44
# If you'd like to visualize these clumps, a list of clumps can be supplied to
45
# the "clumps" callback on a plot. First, we create a projection plot:
46
prj = yt.ProjectionPlot(ds, 2, field, center="c", width=(20, "kpc"))
47
48
# Next we annotate the plot with contours on the borders of the clumps
49
prj.annotate_clumps(leaf_clumps)
50
51
# Save the plot to disk.
52
prj.save("clumps")
53
54
# Reload the clump dataset.
55
cds = yt.load(fn)
56
57
# Clump annotation can also be done with the reloaded clump dataset.
58
59
# Remove the original clump annotation
60
prj.clear_annotations()
61
62
# Get the leaves and add the callback.
63
leaf_clumps_reloaded = cds.leaves
64
prj.annotate_clumps(leaf_clumps_reloaded)
65
prj.save("clumps_reloaded")
66
67
# Query fields for clumps in the tree.
68
print(cds.tree["clump", "center_of_mass"])
69
print(cds.tree.children[0]["grid", "density"])
70
print(cds.tree.children[1]["all", "particle_mass"])
71
72
# Get all of the leaf clumps.
73
print(cds.leaves)
74
print(cds.leaves[0]["clump", "cell_mass"])
75
76