[Yt-svn] yt-commit r1131 - trunk/yt/lagos

britton at wrangler.dreamhost.com britton at wrangler.dreamhost.com
Tue Jan 20 09:57:27 PST 2009


Author: britton
Date: Tue Jan 20 09:57:25 2009
New Revision: 1131
URL: http://yt.spacepope.org/changeset/1131

Log:
Clump methods, pass_down and add_info_item, now check to see if their arugments 
(only the first one in the case of add_info_item) are callable.  If so, they 
are called.  If not, they are exec'ed or eval'ed.

Added a recipes dictionary to Clump.py with an entry for adding an info item 
that is the distance from a clump to the center of mass of the top level 
object.  The entry itself is a fuction that called with the master clump as an 
argument and a keyword arg for units.

Example:

recipes['DistanceToMainClump'](master_clump,units='au')

The above line adds the info item to the clump_info dict so that it will appear 
when write_clumps or write_clump_hierarchy is called.


Modified:
   trunk/yt/lagos/Clump.py

Modified: trunk/yt/lagos/Clump.py
==============================================================================
--- trunk/yt/lagos/Clump.py	(original)
+++ trunk/yt/lagos/Clump.py	Tue Jan 20 09:57:25 2009
@@ -97,7 +97,11 @@
         "Writes information for clump using the list of items in clump_info."
 
         for item in self.clump_info:
-            value = eval(item['quantity'])
+            # Call if callable, otherwise do an eval.
+            if callable(item['quantity']):
+                value = item['quantity']()
+            else:
+                value = eval(item['quantity'])
             output = eval(item['format'])
             f_ptr.write("%s%s" % ('\t'*level,output))
             f_ptr.write("\n")
@@ -118,7 +122,11 @@
     def pass_down(self,operation):
         "Performs an operation on a clump with an exec and passes the instruction down to clump children."
 
-        exec(operation)
+        # Call if callable, otherwise do an exec.
+        if callable(operation):
+            operation()
+        else:
+            exec(operation)
 
         for child in self.children:
             child.pass_down(operation)
@@ -249,3 +257,22 @@
     fmt_dict['min_density'] =  clump.data["NumberDensity"].min()
     fmt_dict['max_density'] =  clump.data["NumberDensity"].max()
     f_ptr.write(__clump_info_template % fmt_dict)
+
+# Recipes for various clump calculations.
+recipes = {}
+
+# Distance from clump center of mass to center of mass of top level object.
+def _DistanceToMainClump(master,units='pc'):
+    masterCOM = master.data.quantities['CenterOfMass']()
+    pass_command = "self.masterCOM = [%.10f, %.10f, %.10f]" % (masterCOM[0],
+                                                               masterCOM[1],
+                                                               masterCOM[2])
+    master.pass_down(pass_command)
+    master.pass_down("self.com = self.data.quantities['CenterOfMass']()")
+
+    quantity = "((self.com[0]-self.masterCOM[0])**2 + (self.com[1]-self.masterCOM[1])**2 + (self.com[2]-self.masterCOM[2])**2)**(0.5)*self.data.pf.units['%s']" % units
+    format = "%s%s%s" % ("'Distance from center: %.6e ",units,"' % value")
+
+    master.add_info_item(quantity,format)
+
+recipes['DistanceToMainClump'] = _DistanceToMainClump



More information about the yt-svn mailing list