Changeset 664


Ignore:
Timestamp:
Dec 2, 2004, 3:28:33 PM (20 years ago)
Author:
ole
Message:

Added arbitrary functions to Polygon_function

Location:
inundation/ga/storm_surge/pyvolution
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/flatbed.py

    r660 r664  
    1717
    1818#Create basic mesh
    19 N = 20
     19N = 50
    2020points, vertices, boundary = rectangular(N, N, 100, 100)
    2121
     
    2525domain.set_name('polygons')
    2626print "Output being written to " + data_dir + sep + \
    27               domain.filename + "." + domain.format
     27              domain.filename + "_size%d." %len(domain) + domain.format
    2828       
    2929
     
    3636domain.set_quantity('friction', manning)
    3737
     38def wiggle(x, y):
     39    from Numeric import sin
     40    from math import pi
     41    return 10 + sin(2*pi*x/10)
    3842
    39 
     43def slope(x, y):
     44    return 20*(x/100+y/100)   
    4045
    4146#Define polynomials
     
    4752#Set elevation 
    4853domain.set_quantity('elevation',
    49         Polygon_function([(p0, 23), (p1,10), (p2,15)]))
     54        Polygon_function([(p0,slope), (p1,wiggle), (p2,15)]))
     55#domain.set_quantity('level',
     56#       Polygon_function([(p0,slope), (p1,wiggle), (p2,15)]))   
    5057
    5158
  • inundation/ga/storm_surge/pyvolution/test_util.py

    r659 r664  
    88from util import *
    99from config import epsilon
     10
     11
     12def test_function(x, y):
     13    return x+y
    1014
    1115class TestCase(unittest.TestCase):
     
    334338       
    335339       
    336     def test_polygon_function(self):
     340    def test_polygon_function_constants(self):
    337341        p1 = [[0,0], [10,0], [10,10], [0,10]]
    338342        p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
     
    353357        assert allclose(z, [2,1,0,2])   
    354358       
     359       
     360    def test_polygon_function_callable(self):
     361        """Check that values passed into Polygon_function can be callable
     362        themselves.
     363        """
     364        p1 = [[0,0], [10,0], [10,10], [0,10]]
     365        p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
     366       
     367        f = Polygon_function( [(p1, test_function)] )   
     368        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
     369        assert allclose(z, [10,14,0,0])
     370               
     371        #Combined       
     372        f = Polygon_function( [(p1, test_function), (p2, 2.0)] )
     373        z = f([5, 5, 27, 35], [5, 9, 8, -5])
     374        assert allclose(z, [2,14,0,2]) 
     375       
     376       
     377        #Combined w default     
     378        f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14)
     379        z = f([5, 5, 27, 35], [5, 9, 8, -5])
     380        assert allclose(z, [2,14,3.14,2])               
     381       
     382       
     383        #Combined w default func       
     384        f = Polygon_function( [(p1, test_function), (p2, 2.0)],
     385                              default = test_function)
     386        z = f([5, 5, 27, 35], [5, 9, 8, -5])
     387        assert allclose(z, [2,14,35,2])                 
    355388       
    356389       
     
    467500        assert inside_polygon( (10.5, 10.5), polygon )                 
    468501       
    469         #FIXME: Fails
    470         #assert not inside_polygon( (5.5, 5.5), polygon )                               
     502        #FIXME: Fails if point is 5.5, 5.5
     503        assert not inside_polygon( (0, 5.5), polygon )                         
    471504
    472505        #Polygon with a hole   
     
    475508                       
    476509        assert inside_polygon( (0, -0.5), polygon )
    477        
    478         #FIXME: Fails   
    479         #assert not inside_polygon( (0.5, 0.5), polygon )       
     510        assert not inside_polygon( (0.5, 0.5), polygon )       
    480511       
    481512
  • inundation/ga/storm_surge/pyvolution/util.py

    r659 r664  
    5757    """
    5858   
    59     #FIXME: Could do with some optimisation         
     59    #FIXME: Could do with some C-optimisation         
    6060       
    6161    from Numeric import array, dot, allclose
     
    9494       point - Tuple of (x, y) coordinates, or list of tuples
    9595       polygon - list of vertices of polygon
     96       closed - (optional) determine whether points on boundary should be
     97       regarded as belonging to the polygon (closed = True)
     98       or not (closed = False)
    9699   
    97100    Output:
     
    109112       
    110113    Remarks:
    111        <Copy from source Franklins code >   
     114       The vertices may be listed clockwise or counterclockwise and
     115       the first point may optionally be repeated.   
     116       Polygons do not need to be convex.
     117       Polygons can have holes in them and points inside a hole is
     118       regarded as being outside the polygon.         
    112119               
    113                
    114        points at a vertex are considered to be inside         
    115120       
    116     Algorithm due to Darel Finley, http://www.alienryderflex.com/polygon/
    117    
    118    
    119     FIXME: More comments about algoritm
    120     FIXME: Deal with case where point on border of polygon (closed)
    121        
     121    Algorithm is based on work by Darel Finley,
     122    http://www.alienryderflex.com/polygon/
     123   
    122124    """   
    123125
     
    166168        y = points[k, 1]       
    167169
    168        
    169170        inside = False
    170171        for i in range(N):
     
    187188        if inside: indices.append(k)
    188189
    189         #print x, y, polygon, inside
    190 
    191    
    192190    if one_point:
    193191        return inside
     
    222220    Note: If two polygons overlap, the one last in the list takes precedence
    223221
    224     Note: default can only be a constant.   
    225222    """
    226223   
     
    253250        x = array(x).astype(Float)
    254251        y = array(y).astype(Float)     
    255         x = reshape(x, (len(x), 1))
    256         y = reshape(y, (len(y), 1))     
    257                
    258         points = concatenate( (x,y), axis=1 )
    259        
    260         z = ones(x.shape[0], Float) * self.default
     252       
     253        N = len(x)
     254        assert len(y) == N
     255       
     256        points = concatenate( (reshape(x, (N, 1)),
     257                               reshape(y, (N, 1))), axis=1 )
     258
     259        if callable(self.default):
     260            z = self.default(x,y)
     261        else:                   
     262            z = ones(N, Float) * self.default
     263           
    261264        for polygon, value in self.regions:
    262265            indices = inside_polygon(points, polygon)
    263             for i in indices:
    264                 z[i] = value
    265 
    266         #from sys import exit; exit()   
     266           
     267            #FIXME: This needs to be vectorised
     268            if callable(value):
     269                for i in indices:
     270                    xx = array([x[i]])
     271                    yy = array([y[i]])
     272                    z[i] = value(xx, yy)[0]
     273            else:   
     274                for i in indices:
     275                    z[i] = value
     276
    267277        return z                 
    268278
Note: See TracChangeset for help on using the changeset viewer.