Commits

John ZuHone committed 66cd765

Small improvements to the particle generator that I had thought had already been committed, but it turns out they weren't. The notebook at http://blog.yt-project.org/post/ParticleGenerator.html actually assumed these changes(!)

1) The caller no longer has to specify "particle_index" as a field; it is added to the field list automatically by __init__.
2) "ParticleIndices" has been changed to "ParticleGridIndices" to avoid confusion with the "particle_index" field
3) Slight modification to the determination if a particle is in a grid or not, for positions that are exactly on a grid boundary.

  • Participants
  • Parent commits 9823f77

Comments (0)

Files changed (2)

 [egg_info]
 #tag_build = .dev
 #tag_svn_revision = 1
-
-[nosetests]
-detailed-errors=1
-where=yt
-exclude=answer_testing
-with-xunit=1

yt/utilities/particle_generator.py

 
     default_fields = ["particle_position_x",
                       "particle_position_y",
-                      "particle_position_z",
-                      "particle_index"]
-
+                      "particle_position_z"]
+    
     def __init__(self, pf, num_particles, field_list) :
         """
         Base class for generating particle fields which may be applied to
         self.pf = pf
         self.num_particles = num_particles
         self.field_list = field_list
-            
+        self.field_list.append("particle_index")
+        
         try :
             self.posx_index = self.field_list.index(self.default_fields[0])
             self.posy_index = self.field_list.index(self.default_fields[1])
             self.posz_index = self.field_list.index(self.default_fields[2])
-            self.index_index = self.field_list.index(self.default_fields[3])
         except :
             raise KeyError("Field list must contain the following fields: " +
                            "\'particle_position_x\', \'particle_position_y\'" +
-                           ", \'particle_position_z\', \'particle_index\' ")
-
+                           ", \'particle_position_z\' ")
+        self.index_index = self.field_list.index("particle_index")
+        
         self.num_grids = self.pf.h.num_grids
         self.NumberOfParticles = np.zeros((self.num_grids), dtype='int64')
-        self.ParticleIndices = np.zeros(self.num_grids + 1, dtype='int64')
+        self.ParticleGridIndices = np.zeros(self.num_grids + 1, dtype='int64')
         
         self.num_fields = len(self.field_list)
         
         Return a dict containing all of the particle fields in the specified grid.
         """
         ind = grid.id-grid._id_offset
-        start = self.ParticleIndices[ind]
-        end = self.ParticleIndices[ind+1]
+        start = self.ParticleGridIndices[ind]
+        end = self.ParticleGridIndices[ind+1]
         return dict([(field, self.particles[start:end,self.field_list.index(field)])
                      for field in self.field_list])
     
                                              minlength=self.num_grids)
         if self.num_grids > 1 :
             np.add.accumulate(self.NumberOfParticles.squeeze(),
-                              out=self.ParticleIndices[1:])
+                              out=self.ParticleGridIndices[1:])
         else :
-            self.ParticleIndices[1] = self.NumberOfParticles.squeeze()
+            self.ParticleGridIndices[1] = self.NumberOfParticles.squeeze()
         if setup_fields is not None:
             for key, value in setup_fields.items():
                 if key not in self.default_fields:
         for i, grid in enumerate(self.pf.h.grids) :
             pbar.update(i)
             if self.NumberOfParticles[i] > 0:
-                start = self.ParticleIndices[i]
-                end = self.ParticleIndices[i+1]
+                start = self.ParticleGridIndices[i]
+                end = self.ParticleGridIndices[i+1]
                 # Note we add one ghost zone to the grid!
                 cube = grid.retrieve_ghost_zones(1, mapping_dict.keys())
                 le = np.array(grid.LeftEdge).astype(np.float64)
         y = data.pop("particle_position_y")
         z = data.pop("particle_position_z")
 
-        xcond = np.logical_or(x < pf.domain_left_edge[0],
+        xcond = np.logical_or(x <= pf.domain_left_edge[0],
                               x >= pf.domain_right_edge[0])
-        ycond = np.logical_or(y < pf.domain_left_edge[1],
+        ycond = np.logical_or(y <= pf.domain_left_edge[1],
                               y >= pf.domain_right_edge[1])
-        zcond = np.logical_or(z < pf.domain_left_edge[2],
+        zcond = np.logical_or(z <= pf.domain_left_edge[2],
                               z >= pf.domain_right_edge[2])
         cond = np.logical_or(xcond, ycond)
         cond = np.logical_or(zcond, cond)
         >>> le = np.array([0.25,0.25,0.25])
         >>> re = np.array([0.75,0.75,0.75])
         >>> fields = ["particle_position_x","particle_position_y",
-        >>>           "particle_position_z","particle_index",
+        >>>           "particle_position_z",
         >>>           "particle_density","particle_temperature"]
         >>> particles = LatticeParticleGenerator(pf, dims, le, re, fields)
         """
         ymax = particles_right_edge[1]
         zmax = particles_right_edge[2]
 
-        xcond = (xmin < pf.domain_left_edge[0]) or \
+        xcond = (xmin <= pf.domain_left_edge[0]) or \
                 (xmax >= pf.domain_right_edge[0])
-        ycond = (ymin < pf.domain_left_edge[1]) or \
+        ycond = (ymin <= pf.domain_left_edge[1]) or \
                 (ymax >= pf.domain_right_edge[1])
-        zcond = (zmin < pf.domain_left_edge[2]) or \
+        zcond = (zmin <= pf.domain_left_edge[2]) or \
                 (zmax >= pf.domain_right_edge[2])
         cond = xcond or ycond or zcond
 
         >>> sphere = pf.h.sphere(pf.domain_center, 0.5)
         >>> num_p = 100000
         >>> fields = ["particle_position_x","particle_position_y",
-        >>>           "particle_position_z","particle_index",
+        >>>           "particle_position_z",
         >>>           "particle_density","particle_temperature"]
         >>> particles = WithDensityParticleGenerator(pf, sphere, num_particles,
         >>>                                          fields, density_field='Dark_Matter_Density')