[yt-users] a problem switching versions of yt?

John Zuhone jzuhone at gmail.com
Tue Apr 7 13:39:00 PDT 2015


Hi Kathy,

The reason it does not work is because data[“dens”]/(2.36*10**(-24)) has units of g/cm**3, but the field you want to define has units of cm**-3. yt 3.x is unit-aware, unlike yt 2.x. You’ll have to get your field definition into dimensionally correct units. 

It looks like you’re taking the gas density and dividing it by some mean molecular weight times the proton mass. It would be better to do this a bit more explicitly:

from yt.utilities.physical_constants import mp
mu = 1.0 # set mu to whatever your mean molecular weight is

def _numdens(field, data):
    return data["dens"]/(mu*mp)
ds.add_field("numdens", function=_numdens, units="1/cm**3”)

Since mp has units of grams, this will get the array you’re returning into a dimensionally correct state. 

To find out more about how units are now handled in yt, see:

http://yt-project.org/docs/3.1/analyzing/units/index.html#units <http://yt-project.org/docs/3.1/analyzing/units/index.html#units>

Best,

John

> On Apr 7, 2015, at 4:33 PM, Kathy Eastwood <kathy.eastwood at nau.edu> wrote:
> 
> Hi, I *think* I introduced this problem when switching from version 2 to version 3...I am currently running version 3.1.
> 
> I define a derived field, number density, from the original density field, but something seems to be wrong with the units. I cannot make a sliceplot (as I used to be able to, in the old version), and I get other "unit conversion errors".
> 
> thanks,
> kathy
> 
> This cell works ok:
> 
> def _numdens(field, data):
>     return data["dens"]/(2.36*10**(-24)) 
> ds.add_field("numdens", function=_numdens, units="1/cm**3")
> 
> This cell does NOT work, and I get a very similar error when trying to do a sliceplot:
> 
> maxval_numdens, maxloc_numdens = ds.find_max('numdens')
> print maxval_numdens
> print maxloc_numdens
> 
> 
> YTUnitConversionError                     Traceback (most recent call last)
> <ipython-input-12-8b3af9af1cf1> in <module>()
> ----> 1 maxval_numdens, maxloc_numdens = ds.find_max('numdens')
>       2 print maxval_numdens
>       3 print maxloc_numdens
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/data_objects/static_output.pyc in find_max(self, field)
>     524         source = self.all_data()
>     525         max_val, maxi, mx, my, mz = \
> --> 526             source.quantities.max_location(field)
>     527         mylog.info <http://mylog.info/>("Max Value is %0.5e at %0.16f %0.16f %0.16f",
>     528               max_val, mx, my, mz)
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.pyc in __call__(self, field)
>     524 
>     525     def __call__(self, field):
> --> 526         rv = super(MaxLocation, self).__call__(field)
>     527         if len(rv) == 1: rv = rv[0]
>     528         return rv
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.pyc in __call__(self, *args, **kwargs)
>      54         storage = {}
>      55         for sto, ds in parallel_objects(chunks, -1, storage = storage):
> ---> 56             sto.result = self.process_chunk(ds, *args, **kwargs)
>      57         # Now storage will have everything, and will be done via pickling, so
>      58         # the units will be preserved.  (Credit to Nathan for this
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/data_objects/derived_quantities.pyc in process_chunk(self, data, field)
>     535         mz = array_like_field(data, -1, "z")
>     536         maxi = -1
> --> 537         if data[field].size > 0:
>     538             maxi = np.argmax(data[field])
>     539             ma = data[field][maxi]
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.pyc in __getitem__(self, key)
>     246                 return self.field_data[f]
>     247             else:
> --> 248                 self.get_data(f)
>     249         # fi.units is the unit expression string. We depend on the registry
>     250         # hanging off the dataset to define this unit object.
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.pyc in get_data(self, fields)
>     691 
>     692         fields_to_generate += gen_fluids + gen_particles
> --> 693         self._generate_fields(fields_to_generate)
>     694         for field in self.field_data.keys():
>     695             if field not in ofields:
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/data_objects/data_containers.pyc in _generate_fields(self, fields_to_generate)
>     717                         raise RuntimeError
>     718                     self.field_data[field] = fd
> --> 719                     fd.convert_to_units(fi.units)
>     720                 except GenerationInProgress as gip:
>     721                     for f in gip.fields:
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/units/yt_array.pyc in convert_to_units(self, units)
>     387 
>     388         """
> --> 389         new_units = self._unit_repr_check_same(units)
>     390         (conversion_factor, offset) = self.units.get_conversion_factor(new_units)
>     391 
> 
> /home/kde/yt-x86_64/src/yt-hg/yt/units/yt_array.pyc in _unit_repr_check_same(self, units)
>     373         if not self.units.same_dimensions_as(units):
>     374             raise YTUnitConversionError(
> --> 375                 self.units, self.units.dimensions, units, units.dimensions)
>     376 
>     377         return units
> 
> YTUnitConversionError: Unit dimensionalities do not match. Tried to convert between code_mass/code_length**3 (dim (mass)/(length)**3) and cm**(-3) (dim (length)**(-3)).
> 
> 
> 
> 
> 
> -- 
> Kathy DeGioia Eastwood, Ph.D.
> Professor of Physics and Astronomy
> Northern Arizona University
> Flagstaff, AZ 86011-6010
> Ph: 928-523-7159   FX: 928-523-1371
> Kathy.Eastwood at nau.edu <mailto:Kathy.Eastwood at nau.edu>
> deliveries: 602 S. Humphreys St., Bldg 19 Rm 209
> _______________________________________________
> yt-users mailing list
> 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/20150407/fc0fdf04/attachment.html>
-------------- next part --------------
_______________________________________________
yt-users mailing list
yt-users at lists.spacepope.org
http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org


More information about the yt-users mailing list