[yt-users] Derived fields for generic array data

Will Barnes Will_Barnes at alumni.baylor.edu
Sat Oct 15 21:28:02 PDT 2016


Hi yt users,

I have a question about derived fields in a dataset loaded from generic array data. I’ve created a dataset from a three dimensional Numpy array and traced some streamlines (say N of them) through the volume (this is a 3D magnetic field) such that I now have a set of N fieldlines. External to the dataset, I then have N Numpy arrays corresponding to each of the N streamlines, each the same length of the corresponding streamline, that represent the density as a function of the position along the streamline.

What I’d like to do is create a new derived field (“density”) where the values from each of my external numpy arrays are mapped to the corresponding coordinate (as determined by the streamlines) and then “density” is zero everywhere else. I’ve read the documentation about creating derived fields and adding fields to a dataset, but it is not clear to me how to use external information (e.g. my set of N arrays) to create a new or derived field as all the examples show only how to use already existing fields in the dataset to create a new field.

This seems like a relatively simple thing so perhaps I am just missing something or not quite understanding how a new field is added to a dataset? Since my explanation above is a bit confusing, I’ve included a code snippet below that hopefully illustrates a bit better what I’d like to do.

Thanks!

Will Barnes

```
# create 3d numpy array
arr = np.random.random(size=(64,64,64))

# load generic data into a dataset
data = dict(density = (arr, "g/cm**3"))
bbox = np.array([[-1.5, 1.5], [-1.5, 1.5], [-1.5, 1.5]])
ds = yt.load_uniform_grid(data, arr.shape, length_unit="Mpc", bbox=bbox, nprocs=64)

# set seed points
c = ds.domain_center
N = 100
scale = ds.domain_width[0]
pos_dx = np.random.random((N,3))*scale-scale/2.
pos = c+pos_dx

# Create streamlines 
streamlines = Streamlines(ds, pos, 'velocity_x', 'velocity_y', 'velocity_z',length=1.0*Mpc, get_magnitude=True)
streamlines.integrate_through_volume()

# create a density field in ds initialized as 0 everywhere
# for each streamline in streamlines, find coordinates from streamline and add the appropriate density value to the coordinates
```


More information about the yt-users mailing list