Ignore:
Timestamp:
Sep 14, 2004, 5:41:22 PM (21 years ago)
Author:
steve
Message:
 
Location:
inundation/ga/storm_surge/pyvolution-1d
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution-1d/quantity.py

    r279 r296  
    180180
    181181
    182 if __name__ == "__main__":
    183     from domain import Domain
    184 
    185     points1 = [0.0, 1.0, 2.0, 3.0]
    186     vertex_values = [[1.0,2.0],[4.0,5.0],[-1.0,2.0]]
    187    
    188     D1 = Domain(points1)
    189 
    190     Q1 = Quantity(D1, vertex_values)
    191 
    192     print Q1.vertex_values
    193     print Q1.centroid_values
    194 
    195     new_vertex_values = [[2.0,1.0],[3.0,4.0],[-2.0,4.0]]
    196 
    197     Q1.set_values(new_vertex_values)
    198    
    199    
    200     print Q1.vertex_values
    201     print Q1.centroid_values
    202 
    203     new_centroid_values = [20,30,40]
    204     Q1.set_values(new_centroid_values,'centroids')
    205            
    206     print Q1.vertex_values
    207     print Q1.centroid_values
    208 
    209     def fun(x):
    210 
    211         return x**2
    212 
    213     Q1.set_values(fun,'vertices')   
    214            
    215     print Q1.vertex_values
    216     print Q1.centroid_values
    217 
    218 
    219 ## class Conserved_quantity(Quantity):
    220 ##     """Class conserved quantity adds to Quantity:
    221 
    222 ##     boundary values, storage and method for updating, and
    223 ##     methods for extrapolation from centropid to vertices inluding
    224 ##     gradients and limiters
    225 ##     """
    226 
    227 ##     def __init__(self, domain, vertex_values=None):
    228 ##         Quantity.__init__(self, domain, vertex_values)
    229        
    230 ##         from Numeric import zeros, Float
    231 
    232 ##         #Allocate space for boundary values
    233 ##         L = len(domain.boundary)
    234 ##         self.boundary_values = zeros(L, Float)
    235 
    236 ##         #Allocate space for updates of conserved quantities by
    237 ##         #flux calculations and forcing functions
    238 
    239 ##         N = domain.number_of_elements
    240 ##         self.explicit_update = zeros(N, Float )
    241 ##         self.semi_implicit_update = zeros(N, Float )
    242                
    243 
    244 ##     def update(self, timestep):
    245 ##         """Update centroid values based on values stored in
    246 ##         explicit_update and semi_implicit_update as well as given timestep
    247 ##         """
    248 
    249 ##         from Numeric import sum, equal, ones, Float
    250        
    251 ##         N = self.centroid_values.shape[0]
    252        
    253 ##         #Explicit updates
    254 ##         self.centroid_values += timestep*self.explicit_update
    255            
    256 ##         #Semi implicit updates
    257 ##         denominator = ones(N, Float)-timestep*self.semi_implicit_update
    258 
    259 ##         if sum(equal(denominator, 0.0)) > 0.0:
    260 ##             msg = 'Zero division in semi implicit update. Call Stephen :-)'
    261 ##             raise msg
    262 ##         else:
    263 ##             #Update conserved_quantities from semi implicit updates
    264 ##             self.centroid_values /= denominator
    265 
    266 
    267 ##     def compute_gradients(self):
    268 ##         """Compute gradients of triangle surfaces defined by centroids of
    269 ##         neighbouring volumes.
    270 ##         If one face is on the boundary, use own centroid as neighbour centroid.
    271 ##         If two or more are on the boundary, fall back to first order scheme.
    272        
    273 ##         Also return minimum and maximum of conserved quantities
    274 ##         """
    275 
    276        
    277 ##         from Numeric import array, zeros, Float
    278 ##         from util import gradient
    279 
    280 ##         N = self.centroid_values.shape[0]
    281 
    282 ##         a = zeros(N, Float)
    283 ##         b = zeros(N, Float)
    284        
    285 ##         for k in range(N):
    286        
    287 ##             number_of_boundaries = self.domain.number_of_boundaries[k]
    288 
    289 ##             if number_of_boundaries == 3:                 
    290 ##                 #We have zero neighbouring volumes -
    291 ##                 #Fall back to first order scheme
    292 ##                 pass
    293            
    294 ##             elif number_of_boundaries == 2:
    295 ##                 #Special case where we have only one neighbouring volume.
    296 
    297 ##                 #Find index of the one neighbour
    298 ##                 for k0 in self.domain.neighbours[k,:]:
    299 ##                     if k0 >= 0:
    300 ##                         break
    301 
    302 ##                 assert k0 != k
    303 ##                 assert k0 >= 0
    304 
    305 ##                 k1 = k  #Self
    306 
    307 ##                 #Get data
    308 ##                 q0 = self.centroid_values[k0]
    309 ##                 q1 = self.centroid_values[k1]
    310 
    311 ##                 x0, y0 = self.domain.centroids[k0] #V0 centroid
    312 ##                 x1, y1 = self.domain.centroids[k1] #V1 centroid       
    313 
    314 ##                 #Gradient
    315 ##                 det = x0*y1 - x1*y0
    316 ##                 if det != 0.0:
    317 ##                     a[k] = (y1*q0 - y0*q1)/det
    318 ##                     b[k] = (x0*q1 - x1*q0)/det
    319 
    320 ##             else:
    321 ##                 #One or zero missing neighbours
    322 ##                 #In case of one boundary - own centroid
    323 ##                 #has been inserted as a surrogate for the one
    324 ##                 #missing neighbour in neighbour_surrogates
    325 
    326 ##                 #Get data       
    327 ##                 k0 = self.domain.surrogate_neighbours[k,0]
    328 ##                 k1 = self.domain.surrogate_neighbours[k,1]
    329 ##                 k2 = self.domain.surrogate_neighbours[k,2]
    330 
    331 ##                 q0 = self.centroid_values[k0]
    332 ##                 q1 = self.centroid_values[k1]
    333 ##                 q2 = self.centroid_values[k2]                   
    334 
    335 ##                 x0, y0 = self.domain.centroids[k0] #V0 centroid
    336 ##                 x1, y1 = self.domain.centroids[k1] #V1 centroid
    337 ##                 x2, y2 = self.domain.centroids[k2] #V2 centroid
    338 
    339 ##                 #Gradient
    340 ##                 a[k], b[k] = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2)
    341        
    342 ##         return a, b
     182
     183class Conserved_quantity(Quantity):
     184    """Class conserved quantity adds to Quantity:
     185
     186    storage and method for updating, and
     187    methods for extrapolation from centropid to vertices inluding
     188    gradients and limiters
     189    """
     190
     191    def __init__(self, domain, vertex_values=None):
     192        Quantity.__init__(self, domain, vertex_values)
     193       
     194        from Numeric import zeros, Float
     195
     196        #Allocate space for updates of conserved quantities by
     197        #flux calculations and forcing functions
     198
     199        N = domain.number_of_elements
     200        self.explicit_update = zeros(N, Float )
     201        self.semi_implicit_update = zeros(N, Float )
     202               
     203
     204    def update(self, timestep):
     205        """Update centroid values based on values stored in
     206        explicit_update and semi_implicit_update as well as given timestep
     207        """
     208
     209        from Numeric import sum, equal, ones, Float
     210       
     211        N = self.centroid_values.shape[0]
     212       
     213        #Explicit updates
     214        self.centroid_values += timestep*self.explicit_update
     215           
     216        #Semi implicit updates
     217        denominator = ones(N, Float)-timestep*self.semi_implicit_update
     218
     219        if sum(equal(denominator, 0.0)) > 0.0:
     220            msg = 'Zero division in semi implicit update. Call Stephen :-)'
     221            raise msg
     222        else:
     223            #Update conserved_quantities from semi implicit updates
     224            self.centroid_values /= denominator
     225
     226
     227    def compute_gradients(self):
     228        """Compute gradients of triangle surfaces defined by centroids of
     229        neighbouring volumes.
     230        If one face is on the boundary, use own centroid as neighbour centroid.
     231        If two or more are on the boundary, fall back to first order scheme.
     232       
     233        Also return minimum and maximum of conserved quantities
     234        """
     235
     236       
     237        from Numeric import array, zeros, Float
     238        from util import gradient
     239
     240        N = self.centroid_values.shape[0]
     241
     242        a = zeros(N, Float)
     243       
     244        for k in range(N):
     245
     246            # first and last elements have boundaries
     247
     248            if k == 1:
     249
     250                #Get data
     251                k0 = k
     252                k1 = k+1
     253               
     254                q0 = self.centroid_values[k0]
     255                q1 = self.centroid_values[k1]
     256
     257                x0 = self.domain.centroids[k0] #V0 centroid
     258                x1 = self.domain.centroids[k1] #V1 centroid       
     259
     260                #Gradient
     261                a[k] = (q1 - q0)/(x1 - x0)
     262
     263            elif k == N-1:
     264               
     265                #Get data
     266                k0 = k
     267                k1 = k-1
     268               
     269                q0 = self.centroid_values[k0]
     270                q1 = self.centroid_values[k1]
     271
     272                x0 = self.domain.centroids[k0] #V0 centroid
     273                x1 = self.domain.centroids[k1] #V1 centroid       
     274
     275                #Gradient
     276                a[k] = (q1 - q0)/(x1 - x0)
     277               
     278            else
     279                #Interior Volume (2 neighbours)
     280
     281                #Get data
     282                k0 = self.domain.neighbours[k,0]
     283                k2 = self.domain.neighbours[k,1]
     284
     285                q0 = self.centroid_values[k0]
     286                q1 = self.centroid_values[k]
     287                q2 = self.centroid_values[k2]                   
     288
     289                x0 = self.domain.centroids[k0] #V0 centroid
     290                x1 = self.domain.centroids[k1] #V1 centroid (Self)
     291                x2 = self.domain.centroids[k2] #V2 centroid
     292
     293                #Gradient
     294                a[k] = ((q0-q1)/(x0-x1)*(x2-x1) - (q2-q1)/(x2-x1)*(x0-x1))/(x2-x0)
     295       
     296        return a
    343297       
    344298
     
    452406
    453407
     408
     409if __name__ == "__main__":
     410    from domain import Domain
     411
     412    points1 = [0.0, 1.0, 2.0, 3.0]
     413    vertex_values = [[1.0,2.0],[4.0,5.0],[-1.0,2.0]]
     414   
     415    D1 = Domain(points1)
     416
     417    Q1 = Quantity(D1, vertex_values)
     418
     419    print Q1.vertex_values
     420    print Q1.centroid_values
     421
     422    new_vertex_values = [[2.0,1.0],[3.0,4.0],[-2.0,4.0]]
     423
     424    Q1.set_values(new_vertex_values)
     425   
     426   
     427    print Q1.vertex_values
     428    print Q1.centroid_values
     429
     430    new_centroid_values = [20,30,40]
     431    Q1.set_values(new_centroid_values,'centroids')
     432           
     433    print Q1.vertex_values
     434    print Q1.centroid_values
     435
     436    def fun(x):
     437
     438        return x**2
     439
     440    Q1.set_values(fun,'vertices')   
     441           
     442    print Q1.vertex_values
     443    print Q1.centroid_values
     444
     445
     446
     447
  • inundation/ga/storm_surge/pyvolution-1d/test_quantity.py

    r279 r296  
    7676                            [[1,2], [5,5], [0,9], [-6, 3], [3,4]])
    7777        assert allclose(quantity.centroid_values, [1.5, 5., 4.5, -1.5, 3.5 ]) #Centroid
    78 
    79 
    80 ##     def test_boundary_allocation(self):
    81 ##         quantity = Conserved_quantity(self.mesh4,
    82 ##                             [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]])
    83 
    84 ##         assert quantity.boundary_values.shape[0] == len(self.mesh4.boundary)
    85 
    8678
    8779    def test_set_values(self):
     
    275267## #         assert v2.conserved_quantities_vertex2 <= qmax
    276268## #         assert v2.conserved_quantities_vertex2 >= qmin                   
    277 163.968025
     269
    278270
    279271## #         #Check that volume has been preserved   
     
    390382
    391383
    392 ##     def test_update_explicit(self):
    393 ##         quantity = Conserved_quantity(self.mesh4)
    394 
    395 ##         #Test centroids
    396 ##         quantity.set_values([1.,2.,3.,4.], location = 'centroids')
    397 ##         assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid
    398 
    399 ##         #Set explicit_update
    400 ##         quantity.explicit_update = array( [1.,1.,1.,1.] )
    401 
    402 ##         #Update with given timestep
    403 ##         quantity.update(0.1)
    404 
    405 ##         x = array([1, 2, 3, 4]) + array( [.1,.1,.1,.1] )
    406 ##         assert allclose( quantity.centroid_values, x)
    407 
    408 ##     def test_update_semi_implicit(self):
    409 ##         quantity = Conserved_quantity(self.mesh4)
    410 
    411 ##         #Test centroids
    412 ##         quantity.set_values([1.,2.,3.,4.], location = 'centroids')
    413 ##         assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid
    414 
    415 ##         #Set semi implicit update
    416 ##         quantity.semi_implicit_update = array( [1.,1.,1.,1.] )
    417 
    418 ##         #Update with given timestep
    419 ##         quantity.update(0.1)
    420 
    421 ##         x = array([1, 2, 3, 4])/array( [.9,.9,.9,.9] )
    422 ##         assert allclose( quantity.centroid_values, x)
    423 
    424 ##     def test_both_updates(self):
    425 ##         quantity = Conserved_quantity(self.mesh4)
    426 
    427 ##         #Test centroids
    428 ##         quantity.set_values([1.,2.,3.,4.], location = 'centroids')
    429 ##         assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid
    430 
    431 ##         #Set explicit_update
    432 ##         quantity.explicit_update = array( [4.,3.,2.,1.] )
    433        
    434 ##         #Set semi implicit update
    435 ##         quantity.semi_implicit_update = array( [1.,1.,1.,1.] )
    436 
    437 ##         #Update with given timestep
    438 ##         quantity.update(0.1)
    439 
    440 ##         x = array([1, 2, 3, 4]) + array( [.4,.3,.2,.1] )
    441 ##         x /= array( [.9,.9,.9,.9] )
    442 ##         assert allclose( quantity.centroid_values, x)       
     384    def test_update_explicit(self):
     385        quantity = Conserved_quantity(self.domain5)
     386
     387        #Test centroids
     388        quantity.set_values([1.,2.,3.,4.,5.], location = 'centroids')
     389        assert allclose(quantity.centroid_values, [1, 2, 3, 4, 5]) #Centroid
     390
     391        #Set explicit_update
     392        quantity.explicit_update = array( [1.,1.,1.,1.,1.] )
     393
     394        #Update with given timestep
     395        quantity.update(0.1)
     396
     397        x = array([1, 2, 3, 4, 5]) + array( [.1,.1,.1,.1,.1] )
     398        assert allclose( quantity.centroid_values, x)
     399
     400    def test_update_semi_implicit(self):
     401        quantity = Conserved_quantity(self.domain5)
     402
     403        #Test centroids
     404        quantity.set_values([1.,2.,3.,4.,5.], location = 'centroids')
     405        assert allclose(quantity.centroid_values, [1, 2, 3, 4, 5]) #Centroid
     406
     407        #Set semi implicit update
     408        quantity.semi_implicit_update = array( [1.,1.,1.,1.,1.] )
     409
     410        #Update with given timestep
     411        quantity.update(0.1)
     412
     413        x = array([1, 2, 3, 4, 5])/array( [.9,.9,.9,.9,.9] )
     414        assert allclose( quantity.centroid_values, x)
     415
     416    def test_both_updates(self):
     417        quantity = Conserved_quantity(self.domain5)
     418
     419        #Test centroids
     420        quantity.set_values([1.,2.,3.,4.,5.], location = 'centroids')
     421        assert allclose(quantity.centroid_values, [1, 2, 3, 4, 5]) #Centroid
     422
     423        #Set explicit_update
     424        quantity.explicit_update = array( [4.,3.,2.,1.,0.0] )
     425       
     426        #Set semi implicit update
     427        quantity.semi_implicit_update = array( [1.,1.,1.,1.,1.] )
     428
     429        #Update with given timestep
     430        quantity.update(0.1)
     431
     432        x = array([1, 2, 3, 4, 5]) + array( [.4,.3,.2,.1,0.0] )
     433        x /= array( [.9,.9,.9,.9,.9] )
     434        assert allclose( quantity.centroid_values, x)       
    443435
    444436       
Note: See TracChangeset for help on using the changeset viewer.