Particle Generators

Generating particle initial conditions is now possible in yt. The
following shows how to generate particle fields from pre-defined
particle lists, lattice distributions, and distributions based on
density fields.

First, we define a gridded density field where the particle density
field has been "cloud-in-cell" (CIC) interpolated to the grid, and
define a function that assigns a set of particle indices based on a
number of particles and a starting index. This is for a case where we
want to add particles to an already existing set but make sure they have
uniqune indices.

As a first example, we'll generate particle fields from pre-existing
NumPy arrays. First, we define a list of particle field names, and then
assign random positions to the particles in one corner of the grid. We
then call FromListParticleGenerator, which generates the particles.
assign_indices assigns the indices (using numpy.arange by default).
apply_to_stream applies the particle fields to the grid.

Now that the particles are part of the parameter file, they may be
manipulated and plotted:

In[4]:

slc=SlicePlot(ug,2,["Density"],center=ug.domain_center)slc.set_cmap("Density","spring")slc.annotate_particles(0.2,p_size=10.0)# Display all particles within a thick slab 0.2 times the domain widthslc.show()

Now let's try adding a particle distribution in a lattice-shaped spatial
arrangement. Let's choose ten particles on a side, and place them in a
small region away from the random particles. We'll use the special
add_indices function we defined earlier to assign indices that are all
different from the ones the already existing particles have.

In[5]:

pdims=np.array([10,10,10])# number of particles on a side in each dimensionple=np.array([0.6,0.6,0.6])# left edge of particle positionspre=np.array([0.9,0.9,0.9])# right edge of particle positionsparticles2=LatticeParticleGenerator(ug,pdims,ple,pre,field_list)particles2.assign_indices(function=add_indices,npart=np.product(pdims),start_num=num_particles1)particles2.apply_to_stream()

Now let's get fancy. We will use the initial conditions capabilities of
yt to apply a spherically symmetric density distribution based on the
"beta-model" functional form, and set up a refinement method based on
overdensity. Then, we will call refine_amr to apply this density
distribution and refine the grid based on the overdensity over some
value.

Now, we have an interesting density field to serve as a distribution
function for particle positions. What we do next is define a spherical
region over which particle positions will be generated based on the
local grid density. We also will map the grid density to a particle
density field using cloud-in-cell interpolation. Finally, when we apply
these particles, we will set the optional argument clobber=True, which
will remove the particles we already created.

Now we'll plot up both the grid density field and the
"particle_density_cic" field (defined at the top of the script), which
is mapped from the particles onto the grid. We also overplot the
particle positions. These should roughly correspond to the non-zero
values of "particle_density_cic", but there will be some discrepancies
due to the fact that they are taken from a thick slab and only a slice
of the grid-based field is shown.