Hi,

thanks for the advice on data objects. It appears that assigning the derived field to a variable no longer creates a problem anymore. However, I'm still having trouble plotting the field. When I try to plot the field by using the data object as the first argument of SlicePlot, such as in the construction below, I get an error stating that this isn't possible since the object lacks inbuilt coordinates:

import yt
from yt.units import pc, Msun, Myr
from yt.units.yt_array import YTArray
from yt import derived_field
import numpy as np


yt.enable_parallelism()

ds = yt.load("DD0045/output_0045")

val, loc = ds.find_max('density')
               
ds.periodicity = (True, True, True)    
       
ds.add_gradient_fields(('gas', 'velocity_x'))

right_edge = loc - YTArray(np.full((3), 6.0), 'pc')
left_edge = loc + YTArray(np.full((3), 6.0), 'pc')
box = ds.region(loc, right_edge, left_edge)
       
grad_x = box['velocity_x_gradient_x']

plt = yt.SlicePlot(box, 'z', grad_x, center= loc, width= (6.0, 'pc'))

plt.save("plot.png")

 

File "plot_curl.py", line 54, in <module>
    plt = yt.SlicePlot(box, 'z', grad_x, center= loc, width= (6.0, 'pc'))
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/visualization/plot_window.py", line 1897, in SlicePlot
    return AxisAlignedSlicePlot(ds, normal, fields, *args, **kwargs)
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/visualization/plot_window.py", line 1240, in __init__
    axis = fix_axis(axis, ds)
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/funcs.py", line 708, in fix_axis
    return ds.coordinates.axis_id.get(axis, axis)
P009 yt : [ERROR    ] 2018-01-29 10:58:59,487 AttributeError: 'YTRegion' object has no attribute 'coordinates'


When I instead attempt to use the dataset itself as the first argument of SlicePlot, by replacing the penultimate line of code with:

plt = yt.SlicePlot(ds, 'z', grad_x, center= loc, width= (6.0, 'pc'))

I get the following error message:

  File "plot_curl.py", line 49, in <module>
    plt = yt.SlicePlot(ds, 'z', curl_mag, center= loc, width= (6.0, 'pc')) 
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/visualization/plot_window.py", line 1897, in SlicePlot
    return AxisAlignedSlicePlot(ds, normal, fields, *args, **kwargs)
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/visualization/plot_window.py", line 1255, in __init__
    slc.get_data(fields)
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/data_objects/data_containers.py", line 1123, in get_data
    for field in self._determine_fields(fields):
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/data_objects/data_containers.py", line 993, in _determine_fields
    finfo = self.ds._get_field_info("unknown", fname)
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/data_objects/static_output.py", line 638, in _get_field_info
    if field in self.field_info:
  File "/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/fields/field_info_container.py", line 337, in __contains__
    if dict.__contains__(self, key): return True
P011 yt : [ERROR    ] 2018-01-26 14:09:47,834 TypeError: unhashable type: 'YTArray'

Is there a possible solution to this?

Many thanks,
Sam

On 25/01/18 14:32, Nathan Goldbaum wrote:
Hi Sam,

A construction like this:

ds[key]

Is a way of getting runtime parameters from the dataset, not accessing a field. The reason your script is failing is that “velocity_x_gradient_x” is a field, not a runtime parameter, so the key error you get is appropriate.

If you want to access field values, you need to create a data object first:

ad = ds.all_data()
ad[“velocity_x_gradient_x”]

There are a number of data objects in yt, see:

http://yt-project.org/doc/analyzing/objects.html

Finally, yt 3.3.1 is somewhat out of date, the latest version is yt 3.4.0. You likely want to upgrade if you are able to since 3.4.0 includes a number of fixes for issues in the 3.3.x series.

Best,

Nathan

On Thu, Jan 25, 2018 at 8:25 AM Sam Patrick <sam.patrick@port.ac.uk> wrote:
Hello all,

I am rather new to yt and have recently tried using the gradient fields
functionality. I'm attempting to write a script which involves basic
calculations with the gradient of the velocity field, however I keep
coming across the same error when assigning the gradient field to a
variable.

My current version of yt is 3.3.1 and my version of Python is 2.7.12.
The data dumps I'm reading from were produced by an Enzo cosmology
simulation (version 2.5).

The following code is a simplified version of the script which runs
successfully and outputs a slice plot of the velocity gradient:

import yt
from yt import derived_field

yt.enable_parallelism()

ds = yt.load("DD0045/output_0045")
val, loc = ds.find_max('density')

ds.periodicity = (True, True, True)

ds.add_gradient_fields(('gas', 'velocity_x'))

#grad_x = ds['velocity_x_gradient_x']

plt = yt.SlicePlot(ds, 'z', 'velocity_x_gradient_x', center= loc, width=
(6.0, 'pc')

plt.save("plot.png")


However, when I try to assign this gradient field to a variable by
removing the comment and adding the line "grad_x =
ds['velocity_x_gradient_x']" and plotting this instead I come across the
following error:

File
"/opt/apps/yt/3.3.1/yt-conda/lib/python2.7/site-packages/yt/data_objects/static_output.py",
line 352, in __getitem__
return self.parameters[key]
P015 yt : [ERROR    ] 2018-01-25 10:49:00,443 KeyError:
'velocity_x_gradient_x'

I am unsure what I am doing wrong here. Any help will be very much
appreciated.

Thanks,
Sam Patrick

_______________________________________________
yt-users mailing list -- yt-users@python.org
To unsubscribe send an email to yt-users-leave@python.org


_______________________________________________
yt-users mailing list -- yt-users@python.org
To unsubscribe send an email to yt-users-leave@python.org