Changeset 2509


Ignore:
Timestamp:
Mar 9, 2006, 2:29:45 PM (18 years ago)
Author:
ole
Message:

Clean up following move of util_ext.*

Location:
inundation
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/least_squares.py

    r2503 r2509  
    3434
    3535
    36 try:
    37     from util import gradient
    38 except ImportError, e:
    39     #FIXME (DSG-ON) reduce the dependency of modules in pyvolution
    40     # Have util in a dir, working like load_mesh, and get rid of this
    41     def gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2):
    42         """
    43         """
    44 
    45         det = (y2-y0)*(x1-x0) - (y1-y0)*(x2-x0)
    46         a = (y2-y0)*(q1-q0) - (y1-y0)*(q2-q0)
    47         a /= det
    48 
    49         b = (x1-x0)*(q2-q0) - (x2-x0)*(q1-q0)
    50         b /= det
    51 
    52         return a, b
    53 
     36from utilities.numerical_tools import gradient
    5437
    5538DEFAULT_ALPHA = 0.001
  • inundation/pyvolution/test_util.py

    r2314 r2509  
    2121        pass
    2222
    23 
    24     def test_gradient(self):
    25         x0 = 0.0; y0 = 0.0; z0 = 0.0
    26         x1 = 1.0; y1 = 0.0; z1 = -1.0
    27         x2 = 0.0; y2 = 1.0; z2 = 0.0
    28 
    29         zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2)
    30 
    31         assert zx == -1.0
    32         assert zy == 0.0
    33 
    34     def test_gradient_more(self):
    35         x0 = 2.0/3; y0 = 2.0/3
    36         x1=  8.0/3; y1 = 2.0/3
    37         x2 = 2.0/3; y2 = 8.0/3
    38 
    39         q0 = 2.0+2.0/3
    40         q1 = 8.0+2.0/3
    41         q2 = 2.0+8.0/3
    42 
    43         #Gradient of fitted pwl surface
    44         a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2)
    45 
    46         assert abs(a - 3.0) < epsilon
    47         assert abs(b - 1.0) < epsilon
    48 
    49 
    50     def test_gradient2(self):
    51         """Test two-point gradient
    52         """
    53        
    54         x0 = 5.0; y0 = 5.0; z0 = 10.0
    55         x1 = 8.0; y1 = 2.0; z1 = 1.0
    56         x2 = 8.0; y2 = 8.0; z2 = 10.0
    57 
    58         #Reference
    59         zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2)
    60         a, b = gradient2(x0, y0, x1, y1, z0, z1)
    61 
    62         assert zx == a
    63         assert zy == b
    64 
    65         z2_computed = z0 + a*(x2-x0) + b*(y2-y0)
    66         assert z2_computed == z2
    67        
    68     def test_gradient2_more(self):
    69         """Test two-point gradient more
    70         """
    71         x0 = 2.0; y0 = 2.0
    72         x1 = 8.0; y1 = 3.0
    73         x2 = 1.0; y2 = 8.0
    74 
    75         q0 = 2.0
    76         q1 = 8.0
    77         q2 = q0
    78 
    79         #Gradient of fitted pwl surface
    80         a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2)
    81         a, b = gradient2(x0, y0, x1, y1, q0, q1)       
    82 
    83         assert a == a_ref
    84         assert b == b_ref
    85 
    86 
    87     def test_that_C_extension_compiles(self):
    88         FN = 'util_ext.c'
    89         try:
    90             import util_ext
    91         except:
    92             from compile import compile
    93 
    94             try:
    95                 compile(FN)
    96             except:
    97                 raise 'Could not compile %s' %FN
    98             else:
    99                 import util_ext
    100 
    101 
    102     def test_gradient_C_extension(self):
    103         from util_ext import gradient as gradient_c
    104 
    105         x0 = 2.0/3; y0 = 2.0/3
    106         x1=  8.0/3; y1 = 2.0/3
    107         x2 = 2.0/3; y2 = 8.0/3
    108 
    109         q0 = 2.0+2.0/3
    110         q1 = 8.0+2.0/3
    111         q2 = 2.0+8.0/3
    112 
    113         #Gradient of fitted pwl surface
    114         a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2)
    115 
    116         assert abs(a - 3.0) < epsilon
    117         assert abs(b - 1.0) < epsilon
    118 
    119 
    120     def test_gradient_C_extension3(self):
    121         from util_ext import gradient as gradient_c
    122 
    123         from RandomArray import uniform, seed
    124         seed(17, 53)
    125 
    126         x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6)
    127 
    128         q0 = uniform(0.0, 10.0, 4)
    129         q1 = uniform(1.0, 3.0, 4)
    130         q2 = uniform(7.0, 20.0, 4)
    131 
    132 
    133         for i in range(4):
    134             #Gradient of fitted pwl surface
    135             from util import gradient_python
    136             a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2,
    137                                     q0[i], q1[i], q2[i])
    138 
    139             #print a_ref, b_ref
    140             a, b = gradient_c(x0, y0, x1, y1, x2, y2,
    141                               q0[i], q1[i], q2[i])
    142 
    143             #print a, a_ref, b, b_ref
    144             assert abs(a - a_ref) < epsilon
    145             assert abs(b - b_ref) < epsilon
    14623
    14724
  • inundation/utilities/test_numerical_tools.py

    r1910 r2509  
    55from Numeric import zeros, array, allclose
    66from math import sqrt, pi
     7from pyvolution.config import epsilon
    78
    89from numerical_tools import *
     
    6667        assert B.typecode() == 'd'       
    6768        assert A == B   
    68         assert A is not B   #Not the same object       
     69        assert A is not B   #Not the same object
     70
     71
     72    def test_gradient(self):
     73        x0 = 0.0; y0 = 0.0; z0 = 0.0
     74        x1 = 1.0; y1 = 0.0; z1 = -1.0
     75        x2 = 0.0; y2 = 1.0; z2 = 0.0
     76
     77        zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2)
     78
     79        assert zx == -1.0
     80        assert zy == 0.0
     81
     82    def test_gradient_more(self):
     83        x0 = 2.0/3; y0 = 2.0/3
     84        x1=  8.0/3; y1 = 2.0/3
     85        x2 = 2.0/3; y2 = 8.0/3
     86
     87        q0 = 2.0+2.0/3
     88        q1 = 8.0+2.0/3
     89        q2 = 2.0+8.0/3
     90
     91        #Gradient of fitted pwl surface
     92        a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2)
     93
     94        assert abs(a - 3.0) < epsilon
     95        assert abs(b - 1.0) < epsilon
     96
     97
     98    def test_gradient2(self):
     99        """Test two-point gradient
     100        """
     101       
     102        x0 = 5.0; y0 = 5.0; z0 = 10.0
     103        x1 = 8.0; y1 = 2.0; z1 = 1.0
     104        x2 = 8.0; y2 = 8.0; z2 = 10.0
     105
     106        #Reference
     107        zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2)
     108        a, b = gradient2(x0, y0, x1, y1, z0, z1)
     109
     110        assert zx == a
     111        assert zy == b
     112
     113        z2_computed = z0 + a*(x2-x0) + b*(y2-y0)
     114        assert z2_computed == z2
     115       
     116    def test_gradient2_more(self):
     117        """Test two-point gradient more
     118        """
     119        x0 = 2.0; y0 = 2.0
     120        x1 = 8.0; y1 = 3.0
     121        x2 = 1.0; y2 = 8.0
     122
     123        q0 = 2.0
     124        q1 = 8.0
     125        q2 = q0
     126
     127        #Gradient of fitted pwl surface
     128        a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2)
     129        a, b = gradient2(x0, y0, x1, y1, q0, q1)       
     130
     131        assert a == a_ref
     132        assert b == b_ref
     133
     134
     135    def test_that_C_extension_compiles(self):
     136        FN = 'util_ext.c'
     137        try:
     138            import util_ext
     139        except:
     140            from compile import compile
     141
     142            try:
     143                compile(FN)
     144            except:
     145                raise 'Could not compile %s' %FN
     146            else:
     147                import util_ext
     148
     149
     150    def test_gradient_C_extension(self):
     151        from util_ext import gradient as gradient_c
     152
     153        x0 = 2.0/3; y0 = 2.0/3
     154        x1=  8.0/3; y1 = 2.0/3
     155        x2 = 2.0/3; y2 = 8.0/3
     156
     157        q0 = 2.0+2.0/3
     158        q1 = 8.0+2.0/3
     159        q2 = 2.0+8.0/3
     160
     161        #Gradient of fitted pwl surface
     162        a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2)
     163
     164        assert abs(a - 3.0) < epsilon
     165        assert abs(b - 1.0) < epsilon
     166
     167
     168    def test_gradient_C_extension3(self):
     169        from util_ext import gradient as gradient_c
     170
     171        from RandomArray import uniform, seed
     172        seed(17, 53)
     173
     174        x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6)
     175
     176        q0 = uniform(0.0, 10.0, 4)
     177        q1 = uniform(1.0, 3.0, 4)
     178        q2 = uniform(7.0, 20.0, 4)
     179
     180
     181        for i in range(4):
     182            #Gradient of fitted pwl surface
     183            from util import gradient_python
     184            a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2,
     185                                    q0[i], q1[i], q2[i])
     186
     187            #print a_ref, b_ref
     188            a, b = gradient_c(x0, y0, x1, y1, x2, y2,
     189                              q0[i], q1[i], q2[i])
     190
     191            #print a, a_ref, b, b_ref
     192            assert abs(a - a_ref) < epsilon
     193            assert abs(b - b_ref) < epsilon
     194
     195       
    69196
    70197
Note: See TracChangeset for help on using the changeset viewer.