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.


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 one-dimensional 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!

yt-users mailing list

-- Sent from a computer.