Finding the indices of a cell's neighbors
Hello all, I'm trying to access the density for a cell and its six neighbors (in the x,y,z directions), but I can't figure out how I can get the information for the neighboring cells. The problem is that dd[ 'NumberDensity' ] is a onedimensional array, and I don't know the original 3d shape of the data, so I can't reshape the array into three dimensions. Is there a way to get the indices of a cell's neighbors? Thanks for any help! Morgan
Hi Morgan, Thanks for writing, and welcome to yt. :) On Fri, Jul 26, 2013 at 6:39 AM, Morgan Presley <mpresley@princeton.edu> wrote:
Hello all,
I'm trying to access the density for a cell and its six neighbors (in the x,y,z directions), but I can't figure out how I can get the information for the neighboring cells. The problem is that dd[ 'NumberDensity' ] is a onedimensional array, and I don't know the original 3d shape of the data, so I can't reshape the array into three dimensions. Is there a way to get the indices of a cell's neighbors?
Ah, I think I see. So there are a couple different ways to select data in yt. One of them is the way you've found, which selects regions and then presents those regions as flattened arrays. The reason this is done is because for variable resolution data, there is no welldefined ordering for nested regions that have varying extents and widths for cells. So for data that you want to analyze in a spatially aware way, we have a couple *different* ways of getting at it. These break down into two types of datamanipulation operations: 1) Defining a field (which is necessarily defined everywhere) that is specified in terms of a difference of adjoining cells. For instance, this would be something like defining a (local) spatialaverage or a divergence or a gradient. A variety of these fields exist in yt  for instance, DivV and gradVelocityX, gradVelocityY, gradVelocityZ (for the components.) If you look in the documentation, this would be found under anything that turns up when searching for ValidateSpatial. 2) Identifying a region of space and requesting a 3D array of *fixed* resolution. These data objects can either provide noninterpolated/nonsmoothed data (where if a cell is of coarser resolution it is repeated multiple times) or smoothed data, which would be constructed via cascading interpolation. Examples of this type of thing can be found if you search in the documentation for covering_grid or smoothed_covering_grid. This bootcamp notebook contains an example of a covering grid: https://hub.ytproject.org/nb/vu3lia . I hope that helps  let us know if you have any other questions or if there's anything that's unclear. Best, Matt
Thanks for any help! Morgan
_______________________________________________ ytusers mailing list ytusers@lists.spacepope.org http://lists.spacepope.org/listinfo.cgi/ytusersspacepope.org
Hi, Morgan I do this by adding a new field, and getting the new field to work with the grid data. This works for Enzo, it's probably similar in other codes. Follow the examples on the website for adding new fields, my example is a little out of date. def _XAverageDensity(field,data): #this is to make sure your output is the right shape new_field = na.zeros(data['Density'].shape) #watch these indices, I just made this up on the spot new_field[1:,:,:] = 0.5*(data['Density'][:1,:,:] + data['Density'][1:,:,:]) return new_field add_field('XAverageDensity', function = _XAverageDensity, validators=[ValidateSpatial(1,['Density'])]) The last bit about validators forces this function to work on the 3d data cube that yt gets off disk, rather than the flattened arrays that are better for variable resolution. The 1 indicates how many extra ghost zones yt gets, and ['Density'] can be a whole list of fields. d. On Fri, Jul 26, 2013 at 7:39 AM, Morgan Presley <mpresley@princeton.edu>wrote:
Hello all,
I'm trying to access the density for a cell and its six neighbors (in the x,y,z directions), but I can't figure out how I can get the information for the neighboring cells. The problem is that dd[ 'NumberDensity' ] is a onedimensional array, and I don't know the original 3d shape of the data, so I can't reshape the array into three dimensions. Is there a way to get the indices of a cell's neighbors?
Thanks for any help! Morgan
_______________________________________________ ytusers mailing list ytusers@lists.spacepope.org http://lists.spacepope.org/listinfo.cgi/ytusersspacepope.org
  Sent from a computer.
participants (3)

David Collins

Matthew Turk

Morgan Presley