Changeset 2509
- Timestamp:
- Mar 9, 2006, 2:29:45 PM (19 years ago)
- Location:
- inundation
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/least_squares.py
r2503 r2509 34 34 35 35 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 36 from utilities.numerical_tools import gradient 54 37 55 38 DEFAULT_ALPHA = 0.001 -
inundation/pyvolution/test_util.py
r2314 r2509 21 21 pass 22 22 23 24 def test_gradient(self):25 x0 = 0.0; y0 = 0.0; z0 = 0.026 x1 = 1.0; y1 = 0.0; z1 = -1.027 x2 = 0.0; y2 = 1.0; z2 = 0.028 29 zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2)30 31 assert zx == -1.032 assert zy == 0.033 34 def test_gradient_more(self):35 x0 = 2.0/3; y0 = 2.0/336 x1= 8.0/3; y1 = 2.0/337 x2 = 2.0/3; y2 = 8.0/338 39 q0 = 2.0+2.0/340 q1 = 8.0+2.0/341 q2 = 2.0+8.0/342 43 #Gradient of fitted pwl surface44 a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2)45 46 assert abs(a - 3.0) < epsilon47 assert abs(b - 1.0) < epsilon48 49 50 def test_gradient2(self):51 """Test two-point gradient52 """53 54 x0 = 5.0; y0 = 5.0; z0 = 10.055 x1 = 8.0; y1 = 2.0; z1 = 1.056 x2 = 8.0; y2 = 8.0; z2 = 10.057 58 #Reference59 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 == a63 assert zy == b64 65 z2_computed = z0 + a*(x2-x0) + b*(y2-y0)66 assert z2_computed == z267 68 def test_gradient2_more(self):69 """Test two-point gradient more70 """71 x0 = 2.0; y0 = 2.072 x1 = 8.0; y1 = 3.073 x2 = 1.0; y2 = 8.074 75 q0 = 2.076 q1 = 8.077 q2 = q078 79 #Gradient of fitted pwl surface80 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_ref84 assert b == b_ref85 86 87 def test_that_C_extension_compiles(self):88 FN = 'util_ext.c'89 try:90 import util_ext91 except:92 from compile import compile93 94 try:95 compile(FN)96 except:97 raise 'Could not compile %s' %FN98 else:99 import util_ext100 101 102 def test_gradient_C_extension(self):103 from util_ext import gradient as gradient_c104 105 x0 = 2.0/3; y0 = 2.0/3106 x1= 8.0/3; y1 = 2.0/3107 x2 = 2.0/3; y2 = 8.0/3108 109 q0 = 2.0+2.0/3110 q1 = 8.0+2.0/3111 q2 = 2.0+8.0/3112 113 #Gradient of fitted pwl surface114 a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2)115 116 assert abs(a - 3.0) < epsilon117 assert abs(b - 1.0) < epsilon118 119 120 def test_gradient_C_extension3(self):121 from util_ext import gradient as gradient_c122 123 from RandomArray import uniform, seed124 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 surface135 from util import gradient_python136 a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2,137 q0[i], q1[i], q2[i])138 139 #print a_ref, b_ref140 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_ref144 assert abs(a - a_ref) < epsilon145 assert abs(b - b_ref) < epsilon146 23 147 24 -
inundation/utilities/test_numerical_tools.py
r1910 r2509 5 5 from Numeric import zeros, array, allclose 6 6 from math import sqrt, pi 7 from pyvolution.config import epsilon 7 8 8 9 from numerical_tools import * … … 66 67 assert B.typecode() == 'd' 67 68 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 69 196 70 197
Note: See TracChangeset
for help on using the changeset viewer.