[yt-users] Problem trying to define a new field

k.grisdale at surrey.ac.uk k.grisdale at surrey.ac.uk
Mon Nov 4 08:12:53 PST 2013


Hi Britton,

Yes that makes a lot of sense. I have made the modifications and the code seems to work up to the point of the pc.add line and then crashes:

Here is the code as is it stands:

from yt.mods  import *

p6 = load("output_00006/info_00006.txt")
cen = [0.5, 0.5, 0.5]

sphere = p6.h.sphere(cen, 0.5/p6['cm'])
r_max = sphere['Radius'][sphere['Density'] == sphere['Density'].max()]
sphere.set_field_parameter('r_max', r_max)



@derived_field(name = "Denin", units = "g/cm^3", take_log = False)
def denunits(field, data):
    return(data["Density"]*1)
@derived_field(name = "Presin", units = "g/cm^2", take_log = False)
def presunits(field, data):
    return(data["Pressure"]*1)

@derived_field(name = "RRsh", take_log = False)
def RRshunits(field, data):
    r_max = data.get_field_parameter('rmax')
    return(data["Radius"]/r_max)



pc = PlotCollection(p6, center = cen)
pc.add_profile_object(sphere, ["RRsh", "Denin"], x_log=False)
pc.add_profile_object(sphere, ["RRsh", "Presin"], x_log=False)
pc.add_profile_object(sphere, ["RRsh", "VelocityMagnitude"], x_log=False)
pc.save('test103')

print sphere['Density']


and the error message:

Traceback (most recent call last):
  File "profiles2.py", line 27, in <module>
    pc.add_profile_object(sphere, ["RRsh", "Denin"], x_log=False)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/visualization/plot_collection.py", line 961, in add_profile_object
    lazy_reader=lazy_reader)[0]
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.py", line 92, in __call__
    return self._call_func_lazy(args, kwargs)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.py", line 99, in _call_func_lazy
    rv = self.func(GridChildMaskWrapper(g, self._data_source), *args, **kwargs)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.py", line 615, in _Extrema
    if data[field].size < 1:
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.py", line 60, in __getitem__
    data = self.data_source._get_data_from_grid(self.grid, item)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.py", line 95, in save_state
    tr = func(self, grid, field, *args, **kwargs)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.py", line 2645, in _get_data_from_grid
    tr = grid[field]
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/grid_patch.py", line 157, in __getitem__
    self.get_data(key)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/grid_patch.py", line 200, in get_data
    self._generate_field(field)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/grid_patch.py", line 145, in _generate_field
    self[field] = self.pf.field_info[field](self)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/field_info_container.py", line 395, in __call__
    dd = self._function(self, data)
  File "profiles2.py", line 22, in RRshunits
    return(data["Radius"]/r_max)
TypeError: unsupported operand type(s) for /: 'float' and ‘NoneType'

Thanks

Kearn
On 4 Nov 2013, at 15:53, Britton Smith <brittonsmith at gmail.com<mailto:brittonsmith at gmail.com>> wrote:

Hi Kearn,

Sorry, that was a typo on my part.  sp should be "sphere".  So, what's happening here is the following:

I have made a sphere data container with the first line, and now I can access all of the field data for that sphere.  So for example,
sphere["Density"]
will give me an array of all the densities for all the cells in the sphere.  Since it's a numpy array, I can get the maximum density in the sphere by doing:
sphere["Density"].max()

Then, I can figure out which cell is the one that has the maximum density with:
sphere["Density"] == sphere["Density"].max()

If you print out that line by itself, it will be an array of Trues and Falses.  Really, it will have only one True value and all the rest will be Falses.  The point is that this True/False array can then be used as a filter on any other array of the same shape.  So then, if I do:
my_filter = sphere["Density"] == sphere["Density"].max()

Then,
sphere["Radius"][my_filter]
will be the radius value for the cell that was True in the filter array, which is the cell with the maximum density.

Does that make sense?

Britton


On Mon, Nov 4, 2013 at 3:38 PM, <k.grisdale at surrey.ac.uk<mailto:k.grisdale at surrey.ac.uk>> wrote:
Hi Britton,

Thanks for the reply.

I have tried to implement what you have suggest. But I get the following error:

  File "profiles2.py", line 7, in <module>
    r_max = sp['Radius'][sp['Density'] == sp['Density'].max()]
NameError: name 'sp' is not defined.

To be honest I don’t quite follow the r_max = sp…etc line. could you explain that a little?

Thanks

Kearn
On 4 Nov 2013, at 15:21, Britton Smith <brittonsmith at gmail.com<mailto:brittonsmith at gmail.com>> wrote:

Hi Kearn,

I recommend doing something like the following:

Create a sphere on you're own with
sphere = pf.h.sphere(center, (radius, units))

Then, calculate the maximum density and position of that with some numpy array operations and set it as a "field parameter":
r_max = sp['Radius'][sp['Density'] == sp['Density'].max()]
sp.set_field_parameter('r_max', r_max)

Then, in your field definition, you can do:
r_max = data.get_field_parameter('rmax')

and then do the rest of the field definition just as you described.

Finally, instead of add_profile_sphere, you can you add_profile_object which will take the sphere you just created as an argument.

That should do what you want.  Feel free to write back if you have more questions.

Britton


On Mon, Nov 4, 2013 at 2:49 PM, <k.grisdale at surrey.ac.uk<mailto:k.grisdale at surrey.ac.uk>> wrote:
Hi yt-users

I’m attempting to plot some profiles as a function of radius/radiusSH, where radiusSh is the radius of a shock. However I am not sure what the simplest way of getting yt to plot this. I have been using the following commands to produce the profiles.
:

pc = PlotCollection(p6, center = cen)
pc.add_profile_sphere(0.5, "cm", ["Radius", "Density"], weight = "Denin”)

I have also tried to define a new field:


@derived_field(name = "RRsh", take_log = False, validators = [ValidateParameter("rsh")])
def RRshunits(field, data):
    value, rsh = p6.h.find_max("Density")
    return(data["Radius"]/rsh)

but this leads to the following error:

Traceback (most recent call last):
  File "profiles.py", line 21, in <module>
    pc.add_profile_sphere(0.5, "cm", ["RRsh", "Denin"], weight = "Denin")
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/visualization/plot_collection.py", line 1058, in add_profile_sphere
    figure=figure, axes=axes)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/visualization/plot_collection.py", line 961, in add_profile_object
    lazy_reader=lazy_reader)[0]
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.py", line 92, in __call__
    return self._call_func_lazy(args, kwargs)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.py", line 99, in _call_func_lazy
    rv = self.func(GridChildMaskWrapper(g, self._data_source), *args, **kwargs)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.py", line 615, in _Extrema
    if data[field].size < 1:
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.py", line 60, in __getitem__
    data = self.data_source._get_data_from_grid(self.grid, item)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.py", line 95, in save_state
    tr = func(self, grid, field, *args, **kwargs)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.py", line 2645, in _get_data_from_grid
    tr = grid[field]
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/grid_patch.py", line 157, in __getitem__
    self.get_data(key)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/grid_patch.py", line 200, in get_data
    self._generate_field(field)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/grid_patch.py", line 132, in _generate_field
    self.pf.field_info[field].check_available(self)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/field_info_container.py", line 366, in check_available
    validator(data)
  File "/Applications/Code/yt/yt-x86_64/src/yt-hg/yt/data_objects/field_info_container.py", line 442, in __call__
    raise NeedsParameter(doesnt_have)
yt.data_objects.field_info_container.NeedsParameter: (['rsh’])

Am I missing something in my field definition or is their a simpler way to go about this?

Thanks

Kearn
_______________________________________________
yt-users mailing list
yt-users at lists.spacepope.org<mailto:yt-users at lists.spacepope.org>
http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org

_______________________________________________
yt-users mailing list
yt-users at lists.spacepope.org<mailto:yt-users at lists.spacepope.org>
http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org


_______________________________________________
yt-users mailing list
yt-users at lists.spacepope.org<mailto:yt-users at lists.spacepope.org>
http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org


_______________________________________________
yt-users mailing list
yt-users at lists.spacepope.org<mailto:yt-users at lists.spacepope.org>
http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.org/attachments/20131104/1564af2b/attachment.html>


More information about the yt-users mailing list