Changeset 331
- Timestamp:
- Sep 21, 2004, 4:58:25 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/test_least_squares.py
r328 r331 9 9 def distance(x, y): 10 10 return sqrt( sum( (array(x)-array(y))**2 )) 11 12 def linear_function(point): 13 point = array(point) 14 return point[:,0]+point[:,1] 15 11 16 12 17 class TestCase(unittest.TestCase): 18 13 19 def setUp(self): 14 20 pass … … 46 52 interp = Interpolation(points, vertices, data) 47 53 assert allclose(interp.A, [[1., 0., 0.], 48 49 54 [0., 1., 0.], 55 [0., 0., 1.]]) 50 56 51 57 … … 66 72 interp = Interpolation(points, vertices, data) 67 73 68 assert allclose(interp.A, [[0.5, 0.5, 0.0], #Affects vertex 1 and 0 69 70 74 assert allclose(interp.A, [[0.5, 0.5, 0.0], #Affects vertex 1 and 0 75 [0.5, 0.0, 0.5], #Affects vertex 0 and 2 76 [0.0, 0.5, 0.5]]) #Affects vertex 1 and 2 71 77 72 78 … … 86 92 interp = Interpolation(points, vertices, data) 87 93 88 assert allclose(interp.A, [[0.25, 0.75, 0.0], #Affects vertex 1 and 0 89 90 94 assert allclose(interp.A, [[0.25, 0.75, 0.0], #Affects vertex 1 and 0 95 [0.25, 0.0, 0.75], #Affects vertex 0 and 2 96 [0.0, 0.25, 0.75]]) #Affects vertex 1 and 2 91 97 92 98 def test_arbitrary_datapoints(self): … … 126 132 interp = Interpolation(points, triangles, data) 127 133 128 answer = [[0.0, 0.0, 0.0, 1.0, 0.0, 0.0], #Affects point d129 [0.5, 0.0, 0.0, 0.5, 0.0, 0.0], #Affects points a and d134 answer = [[0.0, 0.0, 0.0, 1.0, 0.0, 0.0], #Affects point d 135 [0.5, 0.0, 0.0, 0.5, 0.0, 0.0], #Affects points a and d 130 136 [0.75, 0.25, 0.0, 0.0, 0.0, 0.0], #Affects points a and b 131 [0.0, 0.5, 0.0, 0.5, 0.0, 0.0], #Affects points a and d 132 [0.25, 0.75, 0.0, 0.0, 0.0, 0.0], #Affects points a and b 133 [1./3, 0.0, 0.0, 0.0, 1./3, 1./3]] #Affects points a,e and f 134 #print interp.A 135 #print answer 136 137 [0.0, 0.5, 0.0, 0.5, 0.0, 0.0], #Affects points a and d 138 [0.25, 0.75, 0.0, 0.0, 0.0, 0.0], #Affects points a and b 139 [1./3, 0.0, 0.0, 0.0, 1./3, 1./3]] #Affects points a,e and f 137 140 assert allclose(interp.A, answer) 141 142 143 138 144 139 145 def test_smooth_attributes_to_mesh(self): … … 150 156 z2 = 4 151 157 z3 = 4 152 data_coords = [ 153 154 interp = Interpolation(points, triangles, data_coords )158 data_coords = [d1, d2, d3] 159 160 interp = Interpolation(points, triangles, data_coords, alpha=0.0) 155 161 z = [z1, z2, z3] 156 f = interp.smooth_attributes_to_mesh(z)162 f = interp.fit(z) 157 163 answer = [0, 5., 5.] 164 158 165 assert allclose(f, answer) 159 166 160 def test_smooth_attributes_to_meshII(self): 167 168 def test_smooth_att_to_meshII(self): 161 169 a = [0.0, 0.0] 162 170 b = [0.0, 5.0] … … 168 176 d2 = [1.0, 2.0] 169 177 d3 = [3.0,1.0] 170 data_coords = [ d1, d2, d3] 171 z = self.linear_function(data_coords) 172 interp = Interpolation(points, triangles, data_coords) 173 f = interp.smooth_attributes_to_mesh(z) 174 answer = self.linear_function(points) 178 data_coords = [d1, d2, d3] 179 z = linear_function(data_coords) 180 interp = Interpolation(points, triangles, data_coords, alpha=0.0) 181 f = interp.fit(z) 182 answer = linear_function(points) 183 175 184 assert allclose(f, answer) 176 185 177 186 def test_smooth_attributes_to_meshIII(self): 187 a = [-1.0, 0.0] 188 b = [3.0, 4.0] 189 c = [4.0,1.0] 190 d = [-3.0, 2.0] #3 191 e = [-1.0,-2.0] 192 f = [1.0, -2.0] #5 193 194 vertices = [a, b, c, d,e,f] 195 triangles = [[0,1,3],[1,0,2],[0,4,5], [0,5,2]] #abd bac aef afc 196 197 point_coords = [[-2.0, 2.0], 198 [-1.0, 1.0], 199 [0.0,2.0], 200 [1.0, 1.0], 201 [2.0, 1.0], 202 [0.0,0.0], 203 [1.0, 0.0], 204 [0.0, -1.0], 205 [-0.2,-0.5], 206 [-0.9, -1.5], 207 [0.5, -1.9], 208 [3.0,1.0]] 209 210 z = linear_function(point_coords) 211 interp = Interpolation(vertices, triangles, point_coords, alpha=0.0) 212 f = interp.fit(z) 213 answer = linear_function(vertices) 214 assert allclose(f, answer) 215 216 217 def test_smooth_attributes_to_meshIV(self): 218 """ Testing 2 attributes smoothed to the mesh 219 """ 220 a = [0.0, 0.0] 221 b = [0.0, 5.0] 222 c = [5.0, 0.0] 223 points = [a, b, c] 224 triangles = [ [1,0,2] ] #bac 225 226 d1 = [1.0, 1.0] 227 d2 = [1.0, 3.0] 228 d3 = [3.0,1.0] 229 z1 = [2,4] 230 z2 = [4,8] 231 z3 = [4,8] 232 data_coords = [ d1, d2, d3] 233 234 interp = Interpolation(points, triangles, data_coords, alpha=0.0) 235 z = [z1, z2, z3] 236 f = interp.fit(z) 237 answer = [[0,0], [5., 10.], [5., 10.]] 238 assert allclose(f, answer) 239 240 def test_interpolate_attributes_to_points(self): 241 v0 = [0.0, 0.0] 242 v1 = [0.0, 5.0] 243 v2 = [5.0, 0.0] 244 245 vertices = [v0, v1, v2] 246 triangles = [ [1,0,2] ] #bac 247 248 d0 = [1.0, 1.0] 249 d1 = [1.0, 2.0] 250 d2 = [3.0,1.0] 251 point_coords = [ d0, d1, d2] 252 253 interp = Interpolation(vertices, triangles, point_coords) 254 f = linear_function(vertices) 255 z = interp.interpolate(f) 256 answer = linear_function(point_coords) 257 258 259 assert allclose(z, answer) 260 261 262 def test_interpolate_attributes_to_pointsII(self): 178 263 a = [-1.0, 0.0] 179 264 b = [3.0, 4.0] … … 200 285 [3.0,1.0]] 201 286 202 z = self.linear_function(point_coords)203 287 interp = Interpolation(vertices, triangles, point_coords) 204 f = interp.smooth_attributes_to_mesh(z) 205 answer = self.linear_function(vertices) 206 assert allclose(f, answer) 207 208 def linear_function(self,point): 209 point = array(point) 210 return point[:,0]+point[:,1] 211 212 def test_smooth_attributes_to_meshIV(self): 213 """ Testing 2 attributes smoothed to the mesh 214 """ 215 a = [0.0, 0.0] 216 b = [0.0, 5.0] 217 c = [5.0, 0.0] 218 points = [a, b, c] 219 triangles = [ [1,0,2] ] #bac 220 221 d1 = [1.0, 1.0] 222 d2 = [1.0, 3.0] 223 d3 = [3.0,1.0] 224 z1 = [2,4] 225 z2 = [4,8] 226 z3 = [4,8] 227 data_coords = [ d1, d2, d3] 228 229 interp = Interpolation(points, triangles, data_coords) 230 z = [z1, z2, z3] 231 f = interp.smooth_attributes_to_mesh(z) 232 answer = [[0,0], [5., 10.], [5., 10.]] 233 assert allclose(f, answer) 234 235 def test_interpolate_attributes_to_points(self): 236 v0 = [0.0, 0.0] 237 v1 = [0.0, 5.0] 238 v2 = [5.0, 0.0] 239 240 vertices = [v0, v1, v2] 241 triangles = [ [1,0,2] ] #bac 242 243 244 d0 = [1.0, 1.0] 245 d1 = [1.0, 2.0] 246 d2 = [3.0,1.0] 247 point_coords = [ d0, d1, d2] 248 249 interp = Interpolation(vertices, triangles, point_coords) 250 f = self.linear_function(vertices) 251 z = interp.interpolate_attributes_to_points(f) 252 answer = self.linear_function(point_coords) 288 f = linear_function(vertices) 289 z = interp.interpolate(f) 290 answer = linear_function(point_coords) 253 291 #print "z",z 254 292 #print "answer",answer 255 293 assert allclose(z, answer) 256 257 def test_interpolate_attributes_to_pointsII(self):258 a = [-1.0, 0.0]259 b = [3.0, 4.0]260 c = [4.0,1.0]261 d = [-3.0, 2.0] #3262 e = [-1.0,-2.0]263 f = [1.0, -2.0] #5264 265 vertices = [a, b, c, d,e,f]266 triangles = [[0,1,3],[1,0,2],[0,4,5], [0,5,2]] #abd bac aef afc267 268 269 point_coords = [[-2.0, 2.0],270 [-1.0, 1.0],271 [0.0,2.0],272 [1.0, 1.0],273 [2.0, 1.0],274 [0.0,0.0],275 [1.0, 0.0],276 [0.0, -1.0],277 [-0.2,-0.5],278 [-0.9, -1.5],279 [0.5, -1.9],280 [3.0,1.0]]281 282 interp = Interpolation(vertices, triangles, point_coords)283 f = self.linear_function(vertices)284 z = interp.interpolate_attributes_to_points(f)285 answer = self.linear_function(point_coords)286 #print "z",z287 #print "answer",answer288 assert allclose(z, answer)289 294 290 295 … … 293 298 """ Testing 2 attributes smoothed to the mesh 294 299 """ 300 """Test multiple attributes 301 """ 302 295 303 a = [0.0, 0.0] 296 304 b = [0.0, 5.0] … … 307 315 data_coords = [ d1, d2, d3] 308 316 317 interp = Interpolation(points, triangles, data_coords, alpha=0.0) 309 318 z = [z1, z2, z3] 310 f = smooth_attributes_to_mesh(points, triangles, data_coords,z) 311 answer = [[0,0], [5., 10.], [5., 10.]] 319 f = interp.fit(z) 320 answer = [[0, 0], [5., 10.], [5., 10.]] 321 312 322 assert allclose(f, answer) 323 324 325 326 #Tests of smoothing matrix 327 def test_smoothing_matrix_one_triangle(self): 328 from Numeric import dot 329 a = [0.0, 0.0] 330 b = [0.0, 2.0] 331 c = [2.0,0.0] 332 points = [a, b, c] 333 334 vertices = [ [1,0,2] ] #bac 335 336 interp = Interpolation(points, vertices) 337 338 assert allclose(interp.D, [[1, -0.5, -0.5], 339 [-0.5, 0.5, 0], 340 [-0.5, 0, 0.5]]) 341 342 #Define f(x,y) = x 343 f = array([0,0,2]) #Value at global vertex 2 344 345 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 346 # int 1 dx dy = area = 2 347 assert dot(dot(f, interp.D), f) == 2 348 349 #Define f(x,y) = y 350 f = array([0,2,0]) #Value at global vertex 1 351 352 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 353 # int 1 dx dy = area = 2 354 assert dot(dot(f, interp.D), f) == 2 355 356 #Define f(x,y) = x+y 357 f = array([0,2,2]) #Values at global vertex 1 and 2 358 359 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 360 # int 2 dx dy = 2*area = 4 361 assert dot(dot(f, interp.D), f) == 4 362 363 364 365 def test_smoothing_matrix_more_triangles(self): 366 from Numeric import dot 367 368 a = [0.0, 0.0] 369 b = [0.0, 2.0] 370 c = [2.0,0.0] 371 d = [0.0, 4.0] 372 e = [2.0, 2.0] 373 f = [4.0,0.0] 374 375 points = [a, b, c, d, e, f] 376 #bac, bce, ecf, dbe, daf, dae 377 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 378 379 interp = Interpolation(points, vertices) 380 381 382 #assert allclose(interp.D, [[1, -0.5, -0.5], 383 # [-0.5, 0.5, 0], 384 # [-0.5, 0, 0.5]]) 385 386 #Define f(x,y) = x 387 f = array([0,0,2,0,2,4]) #f evaluated at points a-f 388 389 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 390 # int 1 dx dy = total area = 8 391 assert dot(dot(f, interp.D), f) == 8 392 393 #Define f(x,y) = y 394 f = array([0,2,0,4,2,0]) #f evaluated at points a-f 395 396 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 397 # int 1 dx dy = area = 8 398 assert dot(dot(f, interp.D), f) == 8 399 400 #Define f(x,y) = x+y 401 f = array([0,2,2,4,4,4]) #f evaluated at points a-f 402 403 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 404 # int 2 dx dy = 2*area = 16 405 assert dot(dot(f, interp.D), f) == 16 406 407 408 def test_fit_and_interpolation(self): 409 from mesh import Mesh 410 411 a = [0.0, 0.0] 412 b = [0.0, 2.0] 413 c = [2.0, 0.0] 414 d = [0.0, 4.0] 415 e = [2.0, 2.0] 416 f = [4.0, 0.0] 417 418 points = [a, b, c, d, e, f] 419 #bac, bce, ecf, dbe, daf, dae 420 triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 421 422 #Get (enough) datapoints 423 data_points = [[ 0.66666667, 0.66666667], 424 [ 1.33333333, 1.33333333], 425 [ 2.66666667, 0.66666667], 426 [ 0.66666667, 2.66666667], 427 [ 0.0, 1.0], 428 [ 0.0, 3.0], 429 [ 1.0, 0.0], 430 [ 1.0, 1.0], 431 [ 1.0, 2.0], 432 [ 1.0, 3.0], 433 [ 2.0, 1.0], 434 [ 3.0, 0.0], 435 [ 3.0, 1.0]] 436 437 interp = Interpolation(points, triangles, data_points, alpha=0.0) 438 439 z = linear_function(data_points) 440 answer = linear_function(points) 441 442 f = interp.fit(z) 443 444 assert allclose(f, answer) 445 446 #Map back 447 z1 = interp.interpolate(f) 448 assert allclose(z, z1) 449 450 451 def test_smoothing_and_interpolation(self): 452 453 a = [0.0, 0.0] 454 b = [0.0, 2.0] 455 c = [2.0, 0.0] 456 d = [0.0, 4.0] 457 e = [2.0, 2.0] 458 f = [4.0, 0.0] 459 460 points = [a, b, c, d, e, f] 461 #bac, bce, ecf, dbe, daf, dae 462 triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 463 464 #Get (too few!) datapoints 465 data_points = [[ 0.66666667, 0.66666667], 466 [ 1.33333333, 1.33333333], 467 [ 2.66666667, 0.66666667], 468 [ 0.66666667, 2.66666667]] 469 470 z = linear_function(data_points) 471 answer = linear_function(points) 472 473 #Make interpolator with too few data points and no smoothing 474 interp = Interpolation(points, triangles, data_points, alpha=0.0) 475 #Must raise an exception 476 try: 477 f = interp.fit(z) 478 except: 479 pass 480 481 #Now try with smoothing parameter 482 interp = Interpolation(points, triangles, data_points, alpha=1.0e-13) 483 484 f = interp.fit(z) 485 486 #f will be different from answerr due to smoothing 487 #assert allclose(f, answer) 488 489 #Map back 490 z1 = interp.interpolate(f) 491 assert allclose(z, z1) 313 492 314 493 #-------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.