<div dir="ltr">Hi Yusuke,<div><br></div><div>This error comes from the fact that your field function is being evaluated for an array of elements all at once.  You will likely have portions of the array that satisfy both criteria.  You'll need to first define a field array and then set the values for the portions above the density threshold and then below.  I recommend doing something like the following:</div><div><br></div><font face="monospace, monospace">def _SFR_Density(field, data):<br>   f_sf = 0.01<br>   rho_thresh = 57.5 * Mu * hydrogen_mass</font><div><font face="monospace, monospace">   # make field array<br></font><div><font face="monospace, monospace">   field_data = data["density"].copy()</font></div><div><font face="monospace, monospace">   # density threshold</font></div><div><font face="monospace, monospace">   threshold = </font><span style="font-family:monospace,monospace">data["density"] > rho_thresh</span></div><div><font face="monospace, monospace">   # set values above threshold<br></font><div><font face="monospace, monospace">   field_data[threshold</font><span style="font-family:monospace,monospace">] *= </span><span style="font-family:monospace,monospace">f_sf * / (3*pi/32/Grav/data["density"])**(0.5)</span></div><div><span style="font-family:monospace,monospace">   # set values below</span></div><div><span style="font-family:monospace,monospace">   field_data[np.invert(threshold] = 0</span></div><div><span style="font-family:monospace,monospace">   return field_data</span></div><div><br></div></div><div>The threshold array is a boolean array that can be used to access all the values satisfying your criteria all at once.  Then, the np.invert gives you all the values not satisfying it.  Hope that helps!</div><div><br></div><div>Britton<br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 1, 2017 at 10:46 PM, Yusuke Fujimoto <span dir="ltr"><<a href="mailto:yusuke.fujimoto.jp@gmail.com" target="_blank">yusuke.fujimoto.jp@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">Dear yt-users,<div><br></div><div>I am trying to define a new field, SFR_Density, which includes if statements.</div><div>That is defined by,</div><div>f_sf * rho / t_ff  if rho > rho_threshold</div><div>0                      if rho < rho_threshold</div><div>(f_sf is star formation efficiency, rho is gas density, and t_ff is free fall time)</div><div><br></div><div>Here is my definition I tried.</div><div><br></div><div><p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(175,173,36)">def</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(51,187,200)">_SFR_Density</span><span style="font-variant-ligatures:no-common-ligatures">(field, data):</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">   f_sf = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">0.01</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">   rho_thresh = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">57.5</span><span style="font-variant-ligatures:no-common-ligatures"> * Mu * hydrogen_mass</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(175,173,36)">if</span><span style="font-variant-ligatures:no-common-ligatures"> data[</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"density"</span><span style="font-variant-ligatures:no-common-ligatures">] > rho_thresh:</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">      </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(175,173,36)">return</span><span style="font-variant-ligatures:no-common-ligatures"> f_sf * data[</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"density"</span><span style="font-variant-ligatures:no-common-ligatures">] / (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">3</span><span style="font-variant-ligatures:no-common-ligatures">*pi/</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">32</span><span style="font-variant-ligatures:no-common-ligatures">/Grav/data[</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"density"</span><span style="font-variant-ligatures:no-common-ligatures">])<wbr>**(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">0.5</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(175,173,36)">else</span><span style="font-variant-ligatures:no-common-ligatures">:</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">      </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(175,173,36)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">0.0</span><span style="font-variant-ligatures:no-common-ligatures"> * f_sf * data[</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"density"</span><span style="font-variant-ligatures:no-common-ligatures">] / (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">3</span><span style="font-variant-ligatures:no-common-ligatures">*pi/</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">32</span><span style="font-variant-ligatures:no-common-ligatures">/Grav/data[</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"density"</span><span style="font-variant-ligatures:no-common-ligatures">])<wbr>**(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">0.5</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">add_field(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(195,55,32)">"SFR_Density"</span><span style="font-variant-ligatures:no-common-ligatures">, function=_SFR_Density)</span></p></div><div><br></div><div>But it fails with an error message of "<span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px">ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</span>"</div><div>I am sure this is because data["density"] is a list, so I need to specify the exact index number of elements at the if statement. I have tried many tests, but now I am in stuck.</div><div><br></div><div>Any help would be very appreciated.</div><div><br></div><div>Thanks,</div><div>Yusuke</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>