Changeset 7590


Ignore:
Timestamp:
Dec 10, 2009, 12:43:43 PM (15 years ago)
Author:
ole
Message:

Simplified and optimised get_values in quantity.py

Location:
anuga_core/source/anuga/abstract_2d_finite_volumes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/abstract_2d_finite_volumes/quantity.py

    r7573 r7590  
    11421142
    11431143
    1144         The returned values will have the leading dimension equal to length of the indices list or
    1145         N (all values) if indices is None.
     1144        The returned values will have the leading dimension equal to
     1145        length of the indices list or N (all values) if indices is None.
    11461146
    11471147        In case of location == 'centroids' the dimension of returned
     
    11571157
    11581158        Indices is the set of element ids that the operation applies to.
     1159        If indices is None (or omitted) all elements are returned as
     1160        a copy of the relevant array. If performance is critical,
     1161        use arrays domain.centroid_values, domain.vertex_values and
     1162        domain.edge_values directly.
    11591163
    11601164        The values will be stored in elements following their
     
    11781182        # Edges have already been deprecated in set_values, see changeset:5521,
    11791183        # but *might* be useful in get_values. Any thoughts anyone?
    1180         # YES (Ole): Edge values are necessary for volumetric balance check and inflow boundary. Keep them.
     1184        # YES (Ole): Edge values are necessary for volumetric balance
     1185        # check and inflow boundary. Keep them!
    11811186
    11821187        if location not in ['vertices', 'centroids',
     
    11851190            raise Exception, msg
    11861191
    1187         import types
    1188 
    1189         msg = "'indices' must be a list, array or None"
     1192
     1193        msg = '\'indices\' must be a list, array or None'
    11901194        assert isinstance(indices, (NoneType, list, num.ndarray)), msg
    11911195
    11921196        if location == 'centroids':
    1193             if (indices ==  None):
    1194                 indices = range(len(self))
    1195             return num.take(self.centroid_values, indices, axis=0)
     1197            if indices is None:
     1198                return self.centroid_values.copy()
     1199            else:
     1200                return num.take(self.centroid_values, indices, axis=0)
    11961201        elif location == 'edges':
    1197             if (indices ==  None):
    1198                 indices = range(len(self))
    1199             return num.take(self.edge_values, indices, axis=0)
     1202            if indices is  None:
     1203                return self.edge_values.copy()
     1204            else:   
     1205                return num.take(self.edge_values, indices, axis=0)
    12001206        elif location == 'unique vertices':
    1201             if (indices ==  None):
     1207            if indices is None:
    12021208                indices=range(self.domain.get_number_of_nodes())
    12031209            vert_values = []
     
    12211227            return num.array(vert_values, num.float)
    12221228        else:
    1223             if (indices is None):
    1224                 indices = range(len(self))
    1225             return num.take(self.vertex_values, indices, axis=0)
     1229            if indices is None:
     1230                return self.vertex_values.copy()
     1231            else:   
     1232                return num.take(self.vertex_values, indices, axis=0)
    12261233
    12271234    ##
  • anuga_core/source/anuga/abstract_2d_finite_volumes/test_quantity.py

    r7543 r7590  
    21652165        indices = [1,5]
    21662166        quantity.set_values(value,
    2167                             location = 'unique vertices',
    2168                             indices = indices)
     2167                            location='unique vertices',
     2168                            indices=indices)
    21692169        #print "quantity.centroid_values",quantity.centroid_values
    21702170        assert num.allclose(quantity.vertex_values[0], [0,7,0])
     
    22022202        answer = [0.5,2,4,5,0,1,3,4.5]
    22032203        assert num.allclose(answer,
    2204                             quantity.get_values(location = 'unique vertices'))
     2204                            quantity.get_values(location='unique vertices'))
    22052205
    22062206        indices = [0,5,3]
     
    22082208        assert num.allclose(answer,
    22092209                            quantity.get_values(indices=indices,
    2210                                                 location = 'unique vertices'))
     2210                                                location='unique vertices'))
    22112211        #print "quantity.centroid_values",quantity.centroid_values
    22122212        #print "quantity.get_values(location = 'centroids') ",\
     
    22372237        quantity.set_values(lambda x, y: x+2*y) #2 4 4 6
    22382238       
    2239         assert num.allclose(quantity.get_values(location='centroids'), [2,4,4,6])
    2240         assert num.allclose(quantity.get_values(location='centroids', indices=[1,3]), [4,6])
    2241 
    2242 
    2243         assert num.allclose(quantity.get_values(location='vertices'), [[4,0,2],
    2244                                                                        [4,2,6],
    2245                                                                        [6,2,4],
    2246                                                                        [8,4,6]])
    2247        
    2248         assert num.allclose(quantity.get_values(location='vertices', indices=[1,3]), [[4,2,6],
    2249                                                                                       [8,4,6]])
    2250 
    2251 
    2252         assert num.allclose(quantity.get_values(location='edges'), [[1,3,2],
    2253                                                                     [4,5,3],
    2254                                                                     [3,5,4],
    2255                                                                     [5,7,6]])
    2256         assert num.allclose(quantity.get_values(location='edges', indices=[1,3]),
     2239        assert num.allclose(quantity.get_values(location='centroids'),
     2240                            [2,4,4,6])
     2241                           
     2242        assert num.allclose(quantity.get_values(location='centroids',
     2243                                                indices=[1,3]), [4,6])
     2244
     2245
     2246        assert num.allclose(quantity.get_values(location='vertices'),
     2247                            [[4,0,2],
     2248                             [4,2,6],
     2249                             [6,2,4],
     2250                             [8,4,6]])
     2251       
     2252        assert num.allclose(quantity.get_values(location='vertices',
     2253                                                indices=[1,3]), [[4,2,6],
     2254                                                                 [8,4,6]])
     2255
     2256
     2257        assert num.allclose(quantity.get_values(location='edges'),
     2258                            [[1,3,2],
     2259                             [4,5,3],
     2260                             [3,5,4],
     2261                             [5,7,6]])
     2262        assert num.allclose(quantity.get_values(location='edges',
     2263                                                indices=[1,3]),
    22572264                            [[4,5,3],
    22582265                             [5,7,6]])       
     
    22652272        #e: (6+6+6)/3       
    22662273        #f: 4
    2267         assert num.allclose(quantity.get_values(location='unique vertices'), [0, 4, 2, 8, 6, 4])       
    2268                                                                                  
    2269        
    2270 
     2274        assert num.allclose(quantity.get_values(location='unique vertices'),
     2275                            [0, 4, 2, 8, 6, 4])
    22712276
    22722277
     
    23062311        #print answer
    23072312        #print quantity.get_values(interpolation_points=interpolation_points)
    2308         assert num.allclose(answer, quantity.get_values(interpolation_points=interpolation_points,
    2309                                                         verbose=False))       
     2313        assert num.allclose(answer,
     2314                            quantity.get_values(interpolation_points=interpolation_points,
     2315                                                verbose=False))       
    23102316                       
    23112317
     
    25072513        assert num.allclose(answer_vertex_values,
    25082514                            quantity.vertex_values)
    2509         #print "quantity.centroid_values",quantity.centroid_values
    2510         #print "quantity.get_values(location = 'centroids') ",\
    2511         #      quantity.get_values(location = 'centroids')
     2515                           
     2516        # Just another (slightly larger) test of get_values
     2517
     2518        assert num.allclose(quantity.get_values(location='centroids'),
     2519                            quantity.centroid_values)
     2520                           
     2521                           
     2522        assert num.allclose(quantity.get_values(location='vertices'),
     2523                            quantity.vertex_values)                           
     2524                           
     2525        assert num.allclose(quantity.get_values(location='edges'),
     2526                            quantity.edge_values)
     2527                           
    25122528
    25132529
Note: See TracChangeset for help on using the changeset viewer.