import numpy as np12import yt3from yt.data_objects.level_sets.api import Clump, find_clumps45ds = yt.load("IsolatedGalaxy/galaxy0030/galaxy0030")67data_source = ds.disk([0.5, 0.5, 0.5], [0.0, 0.0, 1.0], (8, "kpc"), (1, "kpc"))89# the field to be used for contouring10field = ("gas", "density")1112# This is the multiplicative interval between contours.13step = 2.01415# Now we set some sane min/max values between which we want to find contours.16# This is how we tell the clump finder what to look for -- it won't look for17# contours connected below or above these threshold values.18c_min = 10 ** np.floor(np.log10(data_source[field]).min())19c_max = 10 ** np.floor(np.log10(data_source[field]).max() + 1)2021# Now find get our 'base' clump -- this one just covers the whole domain.22master_clump = Clump(data_source, field)2324# Add a "validator" to weed out clumps with less than 20 cells.25# As many validators can be added as you want.26master_clump.add_validator("min_cells", 20)2728# Calculate center of mass for all clumps.29master_clump.add_info_item("center_of_mass")3031# Begin clump finding.32find_clumps(master_clump, c_min, c_max, step)3334# Save the clump tree as a reloadable dataset35fn = master_clump.save_as_dataset(fields=[("gas", "density"), ("all", "particle_mass")])3637# We can traverse the clump hierarchy to get a list of all of the 'leaf' clumps38leaf_clumps = master_clump.leaves3940# Get total cell and particle masses for each leaf clump41leaf_masses = [leaf.quantities.total_mass() for leaf in leaf_clumps]4243# If you'd like to visualize these clumps, a list of clumps can be supplied to44# the "clumps" callback on a plot. First, we create a projection plot:45prj = yt.ProjectionPlot(ds, 2, field, center="c", width=(20, "kpc"))4647# Next we annotate the plot with contours on the borders of the clumps48prj.annotate_clumps(leaf_clumps)4950# Save the plot to disk.51prj.save("clumps")5253# Reload the clump dataset.54cds = yt.load(fn)5556# Clump annotation can also be done with the reloaded clump dataset.5758# Remove the original clump annotation59prj.clear_annotations()6061# Get the leaves and add the callback.62leaf_clumps_reloaded = cds.leaves63prj.annotate_clumps(leaf_clumps_reloaded)64prj.save("clumps_reloaded")6566# Query fields for clumps in the tree.67print(cds.tree["clump", "center_of_mass"])68print(cds.tree.children[0]["grid", "density"])69print(cds.tree.children[1]["all", "particle_mass"])7071# Get all of the leaf clumps.72print(cds.leaves)73print(cds.leaves[0]["clump", "cell_mass"])747576