# Particle Trajectories¶

Notebook

One can create particle trajectories from a DatasetSeries object for a specified list of particles identified by their unique indices using the particle_trajectories method.

In [1]:
%matplotlib inline
import yt
import glob
from yt.config import ytcfg
path = ytcfg.get("yt", "test_data_dir")
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

/usr/lib64/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters


First, let's start off with a FLASH dataset containing only two particles in a mutual circular orbit. We can get the list of filenames this way:

In [2]:
my_fns = glob.glob(path+"/Orbit/orbit_hdf5_chk_00[0-9][0-9]")
my_fns.sort()


And let's define a list of fields that we want to include in the trajectories. The position fields will be included by default, so let's just ask for the velocity fields:

In [3]:
fields = ["particle_velocity_x", "particle_velocity_y", "particle_velocity_z"]


There are only two particles, but for consistency's sake let's grab their indices from the dataset itself:

In [4]:
ds = yt.load(my_fns[0])
dd = ds.all_data()
indices = dd["particle_index"].astype("int")
print (indices)

[1 2] dimensionless


which is what we expected them to be. Now we're ready to create a DatasetSeries object and use it to create particle trajectories:

In [5]:
ts = yt.DatasetSeries(my_fns)
# suppress_logging=True cuts down on a lot of noise
trajs = ts.particle_trajectories(indices, fields=fields, suppress_logging=True)


The ParticleTrajectories object trajs is essentially a dictionary-like container for the particle fields along the trajectory, and can be accessed as such:

In [6]:
print (trajs["particle_position_x"])
print (trajs["particle_position_x"].shape)

[[0.25       0.25131274 0.25524942 0.26175659 0.27075597 0.28215068
0.2958247  0.31164457 0.32945507 0.3491025  0.37039668 0.39309658
0.41697977 0.44181091 0.46731412 0.49320738 0.51918241 0.54492553
0.57013802 0.5945386  0.61785738 0.63986883 0.66032111 0.67898584
0.69568959 0.71026758 0.72255999 0.73244151 0.73979734 0.7445359
0.74658248]
[0.75       0.74868726 0.74475058 0.7382434  0.72924402 0.71784928
0.70417524 0.68835534 0.67054478 0.65089727 0.62960299 0.60690298
0.58301967 0.5581884  0.53268507 0.50679169 0.48081655 0.45507332
0.42986072 0.40546004 0.38214117 0.36012962 0.33967725 0.32101242
0.30430854 0.28973042 0.27743785 0.26755617 0.26020016 0.25546143
0.25341467]] code_length
(2, 31)


Note that each field is a 2D NumPy array with the different particle indices along the first dimension and the times along the second dimension. As such, we can access them individually by indexing the field:

In [7]:
plt.figure(figsize=(6, 6))
plt.plot(trajs["particle_position_x"][0], trajs["particle_position_y"][0])
plt.plot(trajs["particle_position_x"][1], trajs["particle_position_y"][1])

Out[7]:
[<matplotlib.lines.Line2D at 0x7fd6fa97c710>]

And we can plot the velocity fields as well:

In [8]:
plt.figure(figsize=(6, 6))
plt.plot(trajs["particle_velocity_x"][0], trajs["particle_velocity_y"][0])
plt.plot(trajs["particle_velocity_x"][1], trajs["particle_velocity_y"][1])

Out[8]:
[<matplotlib.lines.Line2D at 0x7fd6fa1e1c50>]

If we want to access the time along the trajectory, we use the key "particle_time":

In [9]:
plt.figure(figsize=(6, 6))
plt.plot(trajs["particle_time"], trajs["particle_velocity_x"][1])
plt.plot(trajs["particle_time"], trajs["particle_velocity_y"][1])

Out[9]:
[<matplotlib.lines.Line2D at 0x7fd6fa1bebe0>]

Alternatively, if we know the particle index we'd like to examine, we can get an individual trajectory corresponding to that index:

In [10]:
particle1 = trajs.trajectory_from_index(1)
plt.figure(figsize=(6, 6))
plt.plot(particle1["particle_time"], particle1["particle_position_x"])
plt.plot(particle1["particle_time"], particle1["particle_position_y"])

Out[10]:
[<matplotlib.lines.Line2D at 0x7fd6f9f1b588>]

Now let's look at a more complicated (and fun!) example. We'll use an Enzo cosmology dataset. First, we'll find the maximum density in the domain, and obtain the indices of the particles within some radius of the center. First, let's have a look at what we're getting:

In [11]:
ds = yt.load("enzo_tiny_cosmology/DD0046/DD0046")
slc = yt.SlicePlot(ds, "x", ["density","dark_matter_density"], center="max", width=(3.0, "Mpc"))
slc.show()


So far, so good--it looks like we've centered on a galaxy cluster. Let's grab all of the dark matter particles within a sphere of 0.5 Mpc (identified by "particle_type == 1"):

In [12]:
sp = ds.sphere("max", (0.5, "Mpc"))
indices = sp["particle_index"][sp["particle_type"] == 1]


Next we'll get the list of datasets we want, and create trajectories for these particles:

In [13]:
my_fns = glob.glob(path+"/enzo_tiny_cosmology/DD*/*.hierarchy")
my_fns.sort()
ts = yt.DatasetSeries(my_fns)
trajs = ts.particle_trajectories(indices, fields=fields, suppress_logging=True)


Matplotlib can make 3D plots, so let's pick three particle trajectories at random and look at them in the volume:

In [14]:
fig = plt.figure(figsize=(8.0, 8.0))
ax.plot(trajs["particle_position_x"][100], trajs["particle_position_y"][100], trajs["particle_position_z"][100])
ax.plot(trajs["particle_position_x"][8], trajs["particle_position_y"][8], trajs["particle_position_z"][8])
ax.plot(trajs["particle_position_x"][25], trajs["particle_position_y"][25], trajs["particle_position_z"][25])

Out[14]:
[<mpl_toolkits.mplot3d.art3d.Line3D at 0x7fd6faadf780>]

It looks like these three different particles fell into the cluster along different filaments. We can also look at their x-positions only as a function of time:

In [15]:
plt.figure(figsize=(6,6))
plt.plot(trajs["particle_time"], trajs["particle_position_x"][100])
plt.plot(trajs["particle_time"], trajs["particle_position_x"][8])
plt.plot(trajs["particle_time"], trajs["particle_position_x"][25])

Out[15]:
[<matplotlib.lines.Line2D at 0x7fd6f5b55160>]

Suppose we wanted to know the gas density along the particle trajectory, but there wasn't a particle field corresponding to that in our dataset. Never fear! If the field exists as a grid field, yt will interpolate this field to the particle positions and add the interpolated field to the trajectory. To add such a field (or any field, including additional particle fields) we can call the add_fields method:

In [16]:
trajs.add_fields(["density"])


We also could have included "density" in our original field list. Now, plot up the gas density for each particle as a function of time:

In [17]:
plt.figure(figsize=(6,6))
plt.plot(trajs["particle_time"], trajs["density"][100])
plt.plot(trajs["particle_time"], trajs["density"][8])
plt.plot(trajs["particle_time"], trajs["density"][25])
plt.yscale("log")


Finally, the particle trajectories can be written to disk. Two options are provided: ASCII text files with a column for each field and the time, and HDF5 files:

In [18]:
trajs.write_out("halo_trajectories") # This will write a separate file for each trajectory
trajs.write_out_h5("halo_trajectories.h5") # This will write all trajectories to a single file