source: inundation-numpy-branch/utilities/test_numerical_tools.py @ 3514

Last change on this file since 3514 was 3514, checked in by duncan, 18 years ago

Hi all,
I'm doing a change in the anuga structure, moving the code to

\anuga_core\source\anuga

After you have done an svn update, the PYTHONPATH has to be changed to;
PYTHONPATH = anuga_core/source/

This is part of changes required to make installation of anuga quicker and reducing the size of our sandpits.

If any imports are broken, try fixing them. With adding anuga. to them for example. If this seems to have really broken things, email/phone me.

Cheers
Duncan

File size: 5.8 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5from numpy import zeros, array, allclose
6from math import sqrt, pi
7from anuga.pyvolution.config import epsilon
8
9from numerical_tools import *
10
11def test_function(x, y):
12    return x+y
13
14class Test_Numerical_Tools(unittest.TestCase):
15    def setUp(self):
16        pass
17
18    def tearDown(self):
19        pass
20
21
22    def test_angle(self):
23        assert allclose(angle([1.0, 1.0])/pi*180, 45.0)
24
25
26    def test_anglediff(self):
27        assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0)
28
29
30
31       
32    def test_ensure_numeric(self):
33        from numerical_tools import ensure_numeric
34        from numpy import ArrayType, Float, array
35
36        A = [1,2,3,4]
37        B = ensure_numeric(A)
38        assert type(B) == ArrayType
39        assert B.dtype.char == 'l'
40        assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4
41
42
43        A = [1,2,3.14,4]
44        B = ensure_numeric(A)
45        assert type(B) == ArrayType
46        assert B.dtype.char == 'd'
47        assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4
48
49
50        A = [1,2,3,4]
51        B = ensure_numeric(A, Float)
52        assert type(B) == ArrayType
53        assert B.dtype.char == 'd'
54        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
55
56
57        A = [1,2,3,4]
58        B = ensure_numeric(A, Float)
59        assert type(B) == ArrayType
60        assert B.dtype.char == 'd'
61        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
62
63
64        A = array([1,2,3,4])
65        B = ensure_numeric(A)
66        assert type(B) == ArrayType
67        assert B.dtype.char == 'l'
68        assert (A == B).all()   
69        assert A is B   #Same object
70
71
72        A = array([1,2,3,4])
73        B = ensure_numeric(A, Float)
74        assert type(B) == ArrayType
75        assert B.dtype.char == 'd'       
76        assert (A == B).all()           
77        assert A is not B   #Not the same object
78
79
80    def test_gradient(self):
81        x0 = 0.0; y0 = 0.0; z0 = 0.0
82        x1 = 1.0; y1 = 0.0; z1 = -1.0
83        x2 = 0.0; y2 = 1.0; z2 = 0.0
84
85        zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2)
86
87        assert zx == -1.0
88        assert zy == 0.0
89
90    def test_gradient_more(self):
91        x0 = 2.0/3; y0 = 2.0/3
92        x1=  8.0/3; y1 = 2.0/3
93        x2 = 2.0/3; y2 = 8.0/3
94
95        q0 = 2.0+2.0/3
96        q1 = 8.0+2.0/3
97        q2 = 2.0+8.0/3
98
99        #Gradient of fitted pwl surface
100        a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2)
101
102        assert abs(a - 3.0) < epsilon
103        assert abs(b - 1.0) < epsilon
104
105
106    def test_gradient2(self):
107        """Test two-point gradient
108        """
109       
110        x0 = 5.0; y0 = 5.0; z0 = 10.0
111        x1 = 8.0; y1 = 2.0; z1 = 1.0
112        x2 = 8.0; y2 = 8.0; z2 = 10.0
113
114        #Reference
115        zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2)
116        a, b = gradient2(x0, y0, x1, y1, z0, z1)
117
118        assert zx == a
119        assert zy == b
120
121        z2_computed = z0 + a*(x2-x0) + b*(y2-y0)
122        assert z2_computed == z2
123       
124    def test_gradient2_more(self):
125        """Test two-point gradient more
126        """
127        x0 = 2.0; y0 = 2.0
128        x1 = 8.0; y1 = 3.0
129        x2 = 1.0; y2 = 8.0
130
131        q0 = 2.0
132        q1 = 8.0
133        q2 = q0
134
135        #Gradient of fitted pwl surface
136        a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2)
137        a, b = gradient2(x0, y0, x1, y1, q0, q1)       
138
139        assert a == a_ref
140        assert b == b_ref
141
142
143    def test_histogram(self):
144        """Test histogram with different bin boundaries
145        """
146       
147        a = [1,1,1,1,1,2,1,3,2,3,1,2,3,4,1]
148
149
150        #There are four elements greater than or equal to 3
151        bins = [3]
152        assert allclose(histogram(a, bins), [4])
153
154
155        bins = [ min(a) ]
156        assert allclose(histogram(a, bins), [len(a)])
157
158
159        bins = [ max(a)+0.00001 ]
160        assert allclose(histogram(a, bins), [0])       
161
162       
163        bins = [1,2,3,4]
164        assert allclose(histogram(a, bins), [8,3,3,1])
165
166
167        bins = [0.5,1.5,2,3]
168        assert allclose(histogram(a, bins), [8,0,3,4])
169
170
171       
172
173    def test_that_C_extension_compiles(self):
174        FN = 'util_ext.c'
175        try:
176            import util_ext
177        except:
178            from compile import compile
179
180            try:
181                compile(FN)
182            except:
183                raise 'Could not compile %s' %FN
184            else:
185                import util_ext
186
187
188    def test_gradient_C_extension(self):
189        from util_ext import gradient as gradient_c
190
191        x0 = 2.0/3; y0 = 2.0/3
192        x1=  8.0/3; y1 = 2.0/3
193        x2 = 2.0/3; y2 = 8.0/3
194
195        q0 = 2.0+2.0/3
196        q1 = 8.0+2.0/3
197        q2 = 2.0+8.0/3
198
199        #Gradient of fitted pwl surface
200        a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2)
201
202        assert abs(a - 3.0) < epsilon
203        assert abs(b - 1.0) < epsilon
204
205
206    def test_gradient_C_extension3(self):
207        from util_ext import gradient as gradient_c
208
209        from numpy.random import uniform, seed
210        seed((17,53))
211
212        x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6)
213
214        q0 = uniform(0.0, 10.0, 4)
215        q1 = uniform(1.0, 3.0, 4)
216        q2 = uniform(7.0, 20.0, 4)
217
218
219        for i in range(4):
220            #Gradient of fitted pwl surface
221            a_ref, b_ref = gradient_python(x0, y0, x1, y1, x2, y2,
222                                           q0[i], q1[i], q2[i])
223
224            #print a_ref, b_ref
225            a, b = gradient_c(x0, y0, x1, y1, x2, y2,
226                              q0[i], q1[i], q2[i])
227
228            #print a, a_ref, b, b_ref
229            assert abs(a - a_ref) < epsilon
230            assert abs(b - b_ref) < epsilon
231
232       
233
234
235#-------------------------------------------------------------
236if __name__ == "__main__":
237    suite = unittest.makeSuite(Test_Numerical_Tools,'test')
238    runner = unittest.TextTestRunner()
239    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.