<div dir="ltr"><div><div><div><div><div><div>Dear Tazkera,<br><br></div>You got this error that dd['Gas', 'PhysicalMass'] doesn't exist because while defining new field you didn't add it to yt available field. To do so you have to add one more line after <b>return M</b> in derived field <b>PhysicalMass</b>...<br><br></div>ds.add_field(("gas", "PhysicalMass"), function = _PhysicalMass, units="g")<br><br></div>Now why you are getting the wrong output: Actually now Yt itself do the conversion between the units. For doing so you can do one thing:<br></div>1) if you want to use ["Gas","Mass"] as a field while plotting or deriving new field you can you this:<br>       data["Gas", "Mass"].in_units('g')<br></div>2) if you want to see it in output on terminal then do this:<br>       PhysicalMass = dd["Gas","Mass"];        then           PhysicalMass.in_units('g') will give you the correct output.<br><br></div><div>Actually, the problem is with your code for getting mass wrong (in the gram of order 74) is that while giving output Yt itself first converting ["Gas","Mass"] in units of "g" and then it multiplied with your conversion formula.<br><br>Please ask if you didn't understand any point in this reply. Hope this will help you.<br><br></div><div>-Prateek <br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 4, 2017 at 8:59 AM, tazkera haque <span dir="ltr"><<a href="mailto:h.tazkera@gmail.com" target="_blank">h.tazkera@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Dear yt community,<br><br></div>I am analyzing some Tipsy data output from Gadget 3. I have three different particles, Gas, Star and Dark Matter. I want to derive a new field 'PhysicalMass', that will make a unit conversion of code_mass to some physical mass units. My code so far looks like this:<br><br><b>import yt<br>from yt import derived_field<br><br>@derived_field(name="<wbr>PhysicalMass", units="g")<br>def _PhysicalMass(field, data):<br>    M = data['Gas','Mass']*((5995.*(<wbr>1e10)*(1.98855e33))/(0.7))<br>    return M<br>ds = yt.load('snap_p6n36fof_031.<wbr>bin', n_ref=64)<br>dd= ds.all_data</b><br><div><div class="m_-1413229261404765485gmail-prompt m_-1413229261404765485gmail-output_prompt" style="box-sizing:border-box;min-width:14ex;padding:0.4em;margin:0px;font-family:monospace;text-align:right;line-height:1.21429em;color:rgb(216,67,21)"><pre style="box-sizing:border-box;overflow:auto;font-family:monospace;font-size:inherit;display:block;padding:0px;margin:0px;line-height:inherit;word-break:break-all;color:black;background-color:transparent;border-width:0px;border-style:none;border-color:currentcolor;border-radius:0px;white-space:pre-wrap;vertical-align:baseline"><br></pre></div><div>My default ('Gas', 'Mass') array looks like this:</div><div><b>dd['Gas','Mass']</b></div><div><pre style="box-sizing:border-box;overflow:auto;font-family:monospace;font-size:14px;display:block;padding:0px;margin:0px;line-height:inherit;word-break:break-all;color:rgb(0,0,0);background-color:rgb(255,255,255);border-width:0px;border-style:none;border-color:currentcolor;border-radius:0px;white-space:pre-wrap;vertical-align:baseline;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial">YTArray([  9.64448986e-07,   9.64448986e-07,   9.64448986e-07, ...,
         9.64448986e-07,   9.64448986e-07,   9.64448986e-07]) code_mass<br><br></pre><pre style="box-sizing:border-box;overflow:auto;font-family:monospace;font-size:14px;display:block;padding:0px;margin:0px;line-height:inherit;word-break:break-all;color:rgb(0,0,0);background-color:rgb(255,255,255);border-width:0px;border-style:none;border-color:currentcolor;border-radius:0px;white-space:pre-wrap;vertical-align:baseline;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial">But when I call the new derived field, it takes some values which does not correspond to the conversion constants I have multiplied with:<br><br>dd['PhysicalMass']<br>YTArray([  3.26598467e+74,   3.26598467e+74,   3.26598467e+74, ...,
         3.26598467e+74,   3.26598467e+74,   3.26598467e+74]) g<br><br></pre><pre style="box-sizing:border-box;overflow:auto;font-family:monospace;font-size:14px;display:block;padding:0px;margin:0px;line-height:inherit;word-break:break-all;color:rgb(0,0,0);background-color:rgb(255,255,255);border-width:0px;border-style:none;border-color:currentcolor;border-radius:0px;white-space:pre-wrap;vertical-align:baseline;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:left;text-indent:0px;text-transform:none;word-spacing:0px;text-decoration-style:initial;text-decoration-color:initial">I can directly multiply the ('Gas', 'Mass') array with some conversion factors to get accurate values, but I need to define a new field for later calls:<br>dd['Gas','Mass']*((5995.*(<wbr>1e10)*(1.98855e33))/(0.7))<br></pre></div><div><br><br></div><div>So my question is how do I get a new field which will only contain PhysicalMass from Gas particles. it seems dd['Gas', 'PhysicalMass'] does not exist.</div><div><br></div><div><br></div><div>Thanks very much for your time.</div><div><br></div><div>Best</div><span class="HOEnZb"><font color="#888888"><div>Tazkera<br></div><div><br></div></font></span></div></div>
<br>______________________________<wbr>_________________<br>
yt-users mailing list<br>
<a href="mailto:yt-users@lists.spacepope.org">yt-users@lists.spacepope.org</a><br>
<a href="http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org" rel="noreferrer" target="_blank">http://lists.spacepope.org/<wbr>listinfo.cgi/yt-users-<wbr>spacepope.org</a><br>
<br></blockquote></div><br></div>