Changeset 2828 for trunk

Show
Ignore:
Timestamp:
11/20/2008 11:12:13 AM (7 weeks ago)
Author:
guyer
Message:

Resolving issues with dimensional values

Location:
trunk/fipy
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/fipy/tools/dimensions/physicalField.py

    r2826 r2828  
    558558        else: 
    559559            self.value[index] = value 
    560              
     560           
     561##    __array_priority__ and __array_wrap__ are required to override 
     562##    the default behavior of numpy. If a numpy array and a Variable 
     563##    are in a binary operation and numpy is first, then numpy will, 
     564##    by default, try and do everything it can to get a a raw numpy 
     565##    array out of Variable. __array_wrap__ seems to have been 
     566##    introduced into masked array to fix this issue. __array_wrap__ is 
     567##    called after the operation is done so it could hurt efficiency badly. 
     568##    Something else needs to be done to stop the initial evaluation. 
     569 
     570    __array_priority__ = 100.0     
     571 
     572    def __array_wrap__(self, arr, context=None): 
     573        """ 
     574        Required to prevent numpy not calling the reverse binary operations. 
     575        Both the following tests are examples ufuncs. 
     576         
     577           >>> print type(numerix.array([1.0, 2.0]) * PhysicalField([1.0, 2.0], 'm')) 
     578           <class 'fipy.tools.dimensions.physicalField.PhysicalField'> 
     579 
     580           >>> from scipy.special import gamma as Gamma 
     581           >>> print type(Gamma(PhysicalField([1.0, 2.0]))) 
     582           <type 'numpy.ndarray'> 
     583 
     584        """ 
     585        if context is not None and len(context[1])==2: 
     586            return NotImplemented 
     587        else: 
     588            return arr 
     589 
    561590    def __array__(self, t = None): 
    562591        """ 
     
    592621                return numerix.array(self.getNumericValue(), t) 
    593622        else: 
    594             raise TypeError, 'Numeric array value must be dimensionless' 
     623            return self.getNumericValue() 
     624#             raise TypeError, 'Numeric array value must be dimensionless' 
    595625         
    596626    def _getArray(self): 
  • trunk/fipy/variables/variable.py

    r2827 r2828  
    486486        PF = physicalField.PhysicalField 
    487487 
    488         if not isinstance(value, PF): 
     488        if isinstance(value, Variable): 
     489            value = value.getValue() 
     490        elif not isinstance(value, PF): 
    489491             
    490492            if getattr(self, 'value', None) is not None: