[cig-commits] [commit] master: rework Mineral.set_method (093a2bc)

cig_noreply at geodynamics.org cig_noreply at geodynamics.org
Thu Dec 11 12:20:19 PST 2014


Repository : https://github.com/geodynamics/burnman

On branch  : master
Link       : https://github.com/geodynamics/burnman/compare/c5ec36a308413012ee5b0763ccb541c2ad56f382...bc315ff9a2ed1d4871962882f0626468342f1ffc

>---------------------------------------------------------------

commit 093a2bc634b03379ee1fd2284a5fa1cd8eddaf8a
Author: Timo Heister <timo.heister at gmail.com>
Date:   Thu Dec 11 12:10:15 2014 -0800

    rework Mineral.set_method
    
    Now the method is always set to something (at least None) and the
    warning to check for a change in method now works with method objects.


>---------------------------------------------------------------

093a2bc634b03379ee1fd2284a5fa1cd8eddaf8a
 burnman/mineral.py         | 67 +++++++++++++++++++++++++++++-----------------
 burnman/mineral_helpers.py | 11 +++++---
 2 files changed, 51 insertions(+), 27 deletions(-)

diff --git a/burnman/mineral.py b/burnman/mineral.py
index d0fd7d6..8fb5b65 100644
--- a/burnman/mineral.py
+++ b/burnman/mineral.py
@@ -11,6 +11,8 @@ import burnman.equation_of_state as eos
 import burnman.birch_murnaghan as bm
 import burnman.slb as slb
 import burnman.mie_grueneisen_debye as mgd
+import inspect
+
 
 class Mineral(Material):
     """
@@ -41,10 +43,9 @@ class Mineral(Material):
     def __init__(self):
         if 'params' not in self.__dict__:
             self.params={}
+        self.method = None
         if 'equation_of_state' in self.params:
             self.set_method(self.params['equation_of_state'])
-        else:
-            self.method=None
 
     def set_method(self, method):
         """
@@ -54,28 +55,46 @@ class Mineral(Material):
         or 'slb3'.  Alternatively, you can pass a user defined
         class which derives from the equation_of_state base class.
         """
-        if 'equation_of_state' in self.params:
-            if self.params['equation_of_state'] is not method:
-                warnings.warn('Overriding database equation of state. From '+self.params['equation_of_state'] +' to ' + method)
-        if( isinstance(method, basestring)):
-            if (method == "slb2"):
-                self.method = slb.SLB2()
-            elif (method == "mgd2"):
-                self.method = mgd.MGD2()
-            elif (method == "mgd3"):
-                self.method = mgd.MGD3()
-            elif (method == "slb3"):
-                self.method = slb.SLB3()
-            elif (method == "bm2"):
-                self.method = bm.BM2()
-            elif (method == "bm3"):
-                self.method = bm.BM3()
+
+        if method is None:
+            self.method = None
+            return
+
+        def to_method_obj(method):
+
+            if isinstance(method, basestring):
+                if (method == "slb2"):
+                    return slb.SLB2()
+                elif (method == "mgd2"):
+                    return mgd.MGD2()
+                elif (method == "mgd3"):
+                    return mgd.MGD3()
+                elif (method == "slb3"):
+                    return slb.SLB3()
+                elif (method == "bm2"):
+                    return bm.BM2()
+                elif (method == "bm3"):
+                    return bm.BM3()
+                else:
+                    raise Exception("unsupported material method " + method)
+            elif isinstance(method, eos.EquationOfState):
+                return method
+            elif inspect.isclass(method) and issubclass(method, eos.EquationOfState):
+                return method()
             else:
-                raise Exception("unsupported material method " + method)
-        elif ( issubclass(method, eos.EquationOfState) ):
-            self.method = method()
-        else:
-            raise Exception("unsupported material method " + method.__class__.__name__ )
+                raise Exception("unsupported material method " + method.__class__.__name__ )
+
+        new_method = to_method_obj(method)
+        if self.method is not None and 'equation_of_state' in self.params:
+            self.method = to_method_obj(self.params['equation_of_state'])
+
+        if type(new_method).__name__ == 'instance':
+            raise Exception("Please derive your method from object (see python old style classes)")
+
+        if self.method is not None and type(new_method) is not type(self.method):
+            warnings.warn('Overriding database equation of state. From ' + self.method.__class__.__name__ + ' to ' + new_method.__class__.__name__, stacklevel=2)
+
+        self.method = new_method
 
     def to_string(self):
         """
@@ -107,7 +126,7 @@ class Mineral(Material):
         self.temperature = temperature
         self.old_params = self.params
 
-        if self.method==None:
+        if self.method is None:
             raise AttributeError, "no method set for mineral, or equation_of_state given in mineral.params"
 
         self.V = self.method.volume(self.pressure, self.temperature, self.params)
diff --git a/burnman/mineral_helpers.py b/burnman/mineral_helpers.py
index 05f2a26..ace9683 100644
--- a/burnman/mineral_helpers.py
+++ b/burnman/mineral_helpers.py
@@ -40,16 +40,22 @@ class HelperSolidSolution(Mineral):
         assert(len(base_materials) == len(molar_fraction))
         assert(sum(molar_fraction) > 0.9999)
         assert(sum(molar_fraction) < 1.0001)
+
+        self.method = base_materials[0].method
+
         #does not make sense to do a solid solution with different number of
         #atoms per formula unit or different equations of state, at least not simply...
         for m in base_materials:
+            m.set_method(self.method)
             if(base_materials[0].params.has_key('n')):
                 assert(m.params['n'] == base_materials[0].params['n'])
         
-        self.method=base_materials[0].method
-        
         self.params = {}
 
+    def set_method(self, method):
+        for m in self.base_materials:
+            m.set_method(method)
+
     def debug_print(self, indent=""):
         print "%sHelperSolidSolution(%s):" % (indent, self.to_string())
         indent += "  "
@@ -59,7 +65,6 @@ class HelperSolidSolution(Mineral):
 
     def set_state(self, pressure, temperature):
         for mat in self.base_materials:
-            mat.method = self.method
             mat.set_state(pressure, temperature)
 
         itrange = range(0, len(self.base_materials))



More information about the CIG-COMMITS mailing list