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

britton at wrangler.dreamhost.com britton at wrangler.dreamhost.com
Fri Jan 9 18:41:26 PST 2009


Author: britton
Date: Fri Jan  9 18:41:25 2009
New Revision: 1099
URL: http://yt.spacepope.org/changeset/1099

Log:
Removed hard-coded isBound function as a check of a clump's validity and 
replaced it with the _isValid function, which runs a user-specified validity 
function with an eval statement.  This is done with the keyword, function, 
thrown at instantiation of the master clump.  If none is given, then the 
validity function uses the functional equivalent of the original isBound 
calculation.

For an example of a proper validity function, see the definition of 
self.default_function in Clump.py.  The simplest function, of course, is 
'True', which would result in just pure contouring.


Modified:
   trunk/yt/lagos/Clump.py

Modified: trunk/yt/lagos/Clump.py
==============================================================================
--- trunk/yt/lagos/Clump.py	(original)
+++ trunk/yt/lagos/Clump.py	Fri Jan  9 18:41:25 2009
@@ -28,15 +28,33 @@
 
 class Clump(object):
     children = None
-    def __init__(self, data, parent, field, cached_fields = None):
+    def __init__(self, data, parent, field, cached_fields = None, function=None):
         self.parent = parent
         self.data = data
         self.field = field
         self.min = self.data[field].min()
         self.max = self.data[field].max()
-        self.isBound = None
         self.cached_fields = cached_fields
 
+        # Function determining whether a clump is valid and should be kept.
+        self.default_function = 'self.data.quantities["IsBound"](truncate=True,include_thermal_energy=True) > 1.0'
+        if function is None:
+            self.function = self.default_function
+        else:
+            self.function = function
+
+        # Return value of validity function, saved so it does not have to be calculated again.
+        self.function_value = None
+
+    def _isValid(self):
+        "Perform user specified function to determine if child clumps should be kept."
+
+        # Only call function is it has not been already.
+        if self.function_value is None:
+            self.function_value = eval(self.function)
+
+        return self.function_value
+
     def find_children(self, min, max = None):
         if self.children is not None:
             print "Wiping out existing children clumps."
@@ -47,25 +65,19 @@
         for cid in contour_info:
             new_clump = self.data.extract_region(contour_info[cid])
             self.children.append(Clump(new_clump, self, self.field,
-                                    self.cached_fields))
-
-    def get_IsBound(self):
-        if self.isBound is None:
-            self.isBound = self.data.quantities["IsBound"](
-                    truncate=True,include_thermal_energy=True)
-        return self.isBound
+                                    self.cached_fields,function=self.function))
 
     def __reduce__(self):
         return (_reconstruct_clump, 
                 (self.parent, self.field, self.min, self.max,
-                 self.isBound, self.children, self.data))
+                 self.function_value, self.children, self.data, self.function))
 
-def _reconstruct_clump(parent, field, mi, ma, isBound, children, data):
+def _reconstruct_clump(parent, field, mi, ma, function_value, children, data, function):
     obj = object.__new__(Clump)
     if iterable(parent): parent = parent[1]
     if children is None: children = []
-    obj.parent, obj.field, obj.min, obj.max, obj.isBound, \
-       obj.children = parent, field, mi, ma, isBound, children
+    obj.parent, obj.field, obj.min, obj.max, obj.function_value, \
+       obj.children, obj.function = parent, field, mi, ma, function_value, children, function
     # Now we override, because the parent/child relationship seems a bit
     # unreliable in the unpickling
     for child in children: child.parent = obj
@@ -88,10 +100,10 @@
             find_clumps(child, min*d_clump, max, d_clump)
             if ((child.children is not None) and (len(child.children) > 0)):
                 these_children.append(child)
-            elif (child.get_IsBound() > 1.0):
+            elif (child._isValid()):
                 these_children.append(child)
             else:
-                print "Eliminating unbound, childless clump with %d cells." % len(child.data["CellMassMsun"])
+                print "Eliminating invalid, childless clump with %d cells." % len(child.data["CellMassMsun"])
         if (len(these_children) > 1):
             print "%d of %d children survived." % (len(these_children),len(clump.children))            
             clump.children = these_children



More information about the yt-svn mailing list