Derived fields for generic array data
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))*scalescale/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 ```
Hi Will, There isn't a way to do what you're describing in yt right now because fields are defined locally in terms of the values of other fields. We have longterm plans to make it possible to define a field in terms of women nonlocal property of other fields (e.g. a field defined in terms of streamlines) but this functionality does not exist yet. That said, if you could make a 3D numpy array of the same shape as your other fields, you could load *another* dataset using load_uniform_grid and pass it the original fields along with the new interpolated density field. Hope that's helpful, Nathan Goldbaum On Saturday, October 15, 2016, Will Barnes <Will_Barnes@alumni.baylor.edu> wrote:
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))*scalescale/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 ``` _______________________________________________ ytusers mailing list ytusers@lists.spacepope.org <javascript:;> http://lists.spacepope.org/listinfo.cgi/ytusersspacepope.org
Hi Nathan, Thanks for the quick reply. I was able to just load it into a separate dataset as a NumPy array. Will
On Oct 16, 2016, at 8:33 AM, Nathan Goldbaum <nathan12343@gmail.com> wrote:
Hi Will,
There isn't a way to do what you're describing in yt right now because fields are defined locally in terms of the values of other fields. We have longterm plans to make it possible to define a field in terms of women nonlocal property of other fields (e.g. a field defined in terms of streamlines) but this functionality does not exist yet.
That said, if you could make a 3D numpy array of the same shape as your other fields, you could load *another* dataset using load_uniform_grid and pass it the original fields along with the new interpolated density field.
Hope that's helpful,
Nathan Goldbaum
On Saturday, October 15, 2016, Will Barnes <Will_Barnes@alumni.baylor.edu <mailto:Will_Barnes@alumni.baylor.edu>> wrote: 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))*scalescale/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 ``` _______________________________________________ ytusers mailing list ytusers@lists.spacepope.org <javascript:;> http://lists.spacepope.org/listinfo.cgi/ytusersspacepope.org <http://lists.spacepope.org/listinfo.cgi/ytusersspacepope.org> _______________________________________________ ytusers mailing list ytusers@lists.spacepope.org http://lists.spacepope.org/listinfo.cgi/ytusersspacepope.org
participants (3)

Nathan Goldbaum

Will Barnes

Will Barnes