<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Hi Matt,</span><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">yes, I am using Enzo. </div>

<div style="font-family:arial,sans-serif;font-size:13px">I am sure that my gravitational field does not include the particles, in fact the gravity contribution of their masses is missing from the potential (I am not using standard dark matter particles and I think the contribution to gravity of this particle type is added after Enzo computes the standard potential field). In fact if for example I plot the equipotential contours for the default Grav_Potential I get just the equipotential surfaces for the gas.</div>

<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">I dug in yt yesterday to understand what was going on and why it did not like my potential, in fact I checked exactly the quantities you asked me for bot Grav_Potential and my user defined Total_Grav_Potential (in cgs):</div>

<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><div><i>In [1309]: grav_pot.min()</i></div><div><i>Out[1309]: -38532761511174.227</i></div><div>
<i>In [1310]: grav_pot.max()</i></div>
<div><i>Out[1310]: -1240331838330.0188</i></div><div><i>In [1311]: grav_pot.size</i></div><div><i>Out[1311]: 16777216</i></div></div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">

<i><div>In [1313]: tot_grav_pot.min()</div><div>Out[1313]: -417049888736805.5</div><div>In [1314]: tot_grav_pot.max()</div><div>Out[1314]: -3796359609406.9697</div><div>In [1315]: tot_grav_pot.size</div><div>Out[1315]: 16777216</div>

</i></div><div style="font-family:arial,sans-serif;font-size:13px"><i><br></i></div><div style="font-family:arial,sans-serif;font-size:13px">I checked that the values were changing correctly as the sum of the the three partial potentials (gas, part1 and part2), and they are ok. Also the total length of the array is ok. So what I did next was defining the Total_Grav_Potential field as </div>

<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><div><i><span style="white-space:pre-wrap"> </span>def _Total_Grav_Potential(field,data): </i></div>

<div><div><br></div><div><i><span style="white-space:pre-wrap">             </span>#adds the current particle potential to the total potential</i></div></div><div><i><span style="white-space:pre-wrap">               </span>total_grav_potential = data['Grav_Potential'] </i></div>

<div><span style="white-space:pre-wrap"><i>                 </i></span></div><div><i><span style="white-space:pre-wrap">              </span>return total_grav_potential</i></div></div><div style="font-family:arial,sans-serif;font-size:13px"><i><br>

</i></div><div style="font-family:arial,sans-serif;font-size:13px">so that it just returned the standard Grav_Potential field. Also in this case the result was the same, i.e. I got the same error if clim was not defined and no contours when clim was defined. Hence the problem was in how the field was added to the yt fields.</div>

<div style="font-family:arial,sans-serif;font-size:13px">I went checking the Creating Derived Fields page in the yt docs and the fields.py file from my yt to see how fields were created, so I started adding parameters to the add_field() function one at the time to understand if they were responsible for the problem.</div>

<div style="font-family:arial,sans-serif;font-size:13px">Turned out that the validators were the problem, in particular I had to validate the following fields: </div><div style="font-family:arial,sans-serif;font-size:13px">

<br></div><div style="font-family:arial,sans-serif;font-size:13px"><i>validators=ValidateDataField(["Grav_Potential","particle_position_x","particle_position_z","particle_position_z"])</i><br>

</div><div style="font-family:arial,sans-serif;font-size:13px"><i><br></i></div><div style="font-family:arial,sans-serif;font-size:13px">which are the exactly the native Enzo fields that I used inside the function definition (the used derived fields are instead ok!). So in the end I used this:</div>

<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><div><i><span style="white-space:pre-wrap"> </span>add_field("Total_Grav_Potential", \</i></div>

<div><i><span style="white-space:pre-wrap">         </span>       function=_Total_Grav_Potential, \</i></div><div><i><span style="white-space:pre-wrap">              </span>       take_log= False , \</i></div><div><i><span style="white-space:pre-wrap">            </span>       particle_type= True, \</i></div>

<div><i><span style="white-space:pre-wrap">         </span>       validators=ValidateDataField(["Grav_Potential","particle_position_x","particle_position_z","particle_position_z"]), \</i></div>

<div><i><span style="white-space:pre-wrap">         </span>       vector_field= False, \</i></div><div><i><span style="white-space:pre-wrap">         </span>       units=r"\rm{erg}/\rm{g}")</i></div></div><div style="font-family:arial,sans-serif;font-size:13px">

<br></div><div style="font-family:arial,sans-serif;font-size:13px">and now I get the contours.</div><div style="font-family:arial,sans-serif;font-size:13px">I uploaded the plots here: <a href="http://postimg.org/gallery/5zomtyto/">http://postimg.org/gallery/5zomtyto/</a></div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
The problem is solved but I do not really quite understand why the native Enzo fields have to be validated.</div><div style="font-family:arial,sans-serif;font-size:13px">Can I ask you if you can explain me what is happening in your opinion?</div>

<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">Concerning the coding suggestions, looks definitely better to do the conversions at the end and use more derived fields, I will rewrite it starting from your template.</div>

<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">Thank you very much for the help!</div>

<div style="font-family:arial,sans-serif;font-size:13px">                                                      Roberto </div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 25 September 2013 07:22, Matthew Turk <span dir="ltr"><<a href="mailto:matthewturk@gmail.com" target="_blank">matthewturk@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Roberto,<br>
<div class="im"><br>
On Mon, Sep 23, 2013 at 8:59 PM, trobolo dinni<br>
<<a href="mailto:trobolo.trobolo.dinni5@gmail.com">trobolo.trobolo.dinni5@gmail.com</a>> wrote:<br>
> Dear yt users,<br>
><br>
> I am running a binary star simulation with enzo, and in my box there is a<br>
> star made up of a gas envelope and a particle as core, and a secondary star<br>
> represented by a single particle (no gas).<br>
><br>
> I would like to plot the equipotential surfaces of the system on a slice<br>
> plot, hence, since the default Grav_Potential field does not include the<br>
> particles potential, I created a custom made field as follows:<br>
<br>
</div>It looks like you're using Enzo -- is that right?  If so, are you sure<br>
the Grav_Potential field does not include your particle's potentials?<br>
If not, you may be able to apply them as analytic expressions.<br>
Regardless, I've supplied some comments below:<br>
<div class="im"><br>
><br>
>   def _Total_Grav_Potential(field,data):<br>
> #G in cgs<br>
> grav_const_cgs = 6.674e-8<br>
> #data for gas<br>
> gas_grav_potential_cgs = data['Grav_Potential']  *<br>
> (length_unit1/time_unit1)**2.0 #in units of velocity^2<br>
> gas_position_x_cgs = data['x'] * length_unit1<br>
> gas_position_y_cgs = data['y'] * length_unit1<br>
> gas_position_z_cgs = data['z'] * length_unit1<br>
<br>
</div>You like will want to apply the conversion at the end of your<br>
computation.  You can also use fields like Radius and ParticleMass to<br>
get things back in cm and g, respectively.  There are probably better<br>
ways than what I am about to propose (Stephen Skory and Elizabeth<br>
Tasker have both done things like this) but here's just something off<br>
the top of my head.<br>
<br>
f = data["Grav_Potential"].copy()<br>
for i in range(len(data["ParticleMass"])):<br>
    c = np.array([data["particle_position_%s" % ax] for ax in 'xyz'])<br>
    data.set_field_parameter("center", c)<br>
    R = data["Radius"] # an array of gas radii<br>
    m = data["ParticleMass"][i]<br>
    # compute grav potential with particle here ...<br>
    f += ...<br>
return f<br>
<div><div class="h5"><br>
> #initialize the array for the total grav potential to the gas grav potential<br>
> total_grav_potential_cgs = gas_grav_potential_cgs<br>
> #loops on the particles<br>
> for i in range(len(data['ParticleMassMsun'])):<br>
> #data of the current particle<br>
> current_particle_mass_cgs= data['ParticleMassMsun'][i] * mass_unit<br>
> current_particle_position_x_cgs = data['particle_position_x'][i] *<br>
> length_unit1<br>
> current_particle_position_y_cgs = data['particle_position_y'][i] *<br>
> length_unit1<br>
> current_particle_position_z_cgs = data['particle_position_z'][i] *<br>
> length_unit1<br>
> #computes the array of grav potential for the current particle<br>
> current_particle_grav_potential_cgs = -(grav_const_cgs *<br>
> current_particle_mass_cgs)/(((gas_position_x_cgs -<br>
> current_particle_position_x_cgs)**2.0 + (gas_position_y_cgs -<br>
> current_particle_position_y_cgs)**2.0 + (gas_position_z_cgs -<br>
> current_particle_position_z_cgs)**2.0)**0.5)<br>
> #adds the current particle potential to the total potential<br>
> total_grav_potential_cgs = total_grav_potential_cgs +<br>
> current_particle_grav_potential_cgs<br>
> return total_grav_potential_cgs<br>
> #adds the field to the available yt fields<br>
> add_field("Total_Grav_Potential", function=_Total_Grav_Potential,<br>
> take_log=True ,units=r"\rm{erg}/\rm{g}")<br>
><br>
> where the length unit and time unit variables have the function to convert<br>
> my quantities in cgs.<br>
> If I then call my "Total_Grav_Potential" field, I get reasonable values, but<br>
> when I try to inspect the shape of the equipotential surfaces on the<br>
> xy-plane by plotting the following:<br>
><br>
> sp = SlicePlot(pf, 'z', "Density", width = 1.0)<br>
> sp.annotate_velocity(factor=16, normalize=True)<br>
> sp.annotate_contour('Total_Grav_Potential',clim=(max_total_pot,min_total_pot))<br>
> sp.annotate_particles(1.0, p_size=50.0, marker='o', col='black')<br>
> sp.annotate_text((0.7,1.05), "time = "+str(pf.current_time)+"yr")<br>
> sp.save(plot_dir+"density_slice_z_"+str(index)+".png")<br>
><br>
> nothing happens, and the plot does not show any additional contour; while if<br>
> I do the same with the default "Grav_Potential" field, the gas equipotential<br>
> contours get plotted without problems.<br>
> I also tried to play with the "annotate_contour" options "ncont", "factor"<br>
> and "clim" to refine my plot but the result is always the same.<br>
> Additionally, if I do not set the "clim" option the following error shows<br>
> up:<br>
><br>
> ValueError: zero-size array to minimum.reduce without identity<br>
<br>
</div></div>This message suggests something funny may be going on.  What is the result of:<br>
<br>
dd = pf.h.all_data()<br>
dd["Total_Grav_Potential"].min(), dd["Total_Grav_Potential"].max(),<br>
dd["Total_Grav_Potential"].size<br>
<div class="im"><br>
><br>
><br>
> I would like to ask if my custom definition of the Total_Grav_Potential<br>
> field is correct from a yt coding point of view and if I should modify it<br>
> (or if there is any improvement I can do).<br>
> Also, I would like to ask if you have any idea of what could be the problem<br>
> with the plotting.<br>
<br>
</div>Let us know how it goes, and I hope this helps!<br>
<br>
-Matt<br>
<div class="im"><br>
><br>
><br>
> Thanks for the help,<br>
>                                  Roberto<br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
</div>> _______________________________________________<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" target="_blank">http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org</a><br>
><br>
_______________________________________________<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" target="_blank">http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org</a><br>
</blockquote></div><br></div>