1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | #TEST |
---|
4 | import sys |
---|
5 | import unittest |
---|
6 | from math import sqrt |
---|
7 | import tempfile |
---|
8 | import os |
---|
9 | from Numeric import zeros, take, compress, Float, Int, dot, concatenate, \ |
---|
10 | ArrayType, allclose, array |
---|
11 | |
---|
12 | from fit import * |
---|
13 | from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh |
---|
14 | from anuga.utilities.sparse import Sparse, Sparse_CSR |
---|
15 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
16 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
17 | from anuga.geospatial_data.geospatial_data import Geospatial_data |
---|
18 | |
---|
19 | def distance(x, y): |
---|
20 | return sqrt( sum( (array(x)-array(y))**2 )) |
---|
21 | |
---|
22 | def linear_function(point): |
---|
23 | point = array(point) |
---|
24 | return point[:,0]+point[:,1] |
---|
25 | |
---|
26 | |
---|
27 | class Test_Fit(unittest.TestCase): |
---|
28 | |
---|
29 | def setUp(self): |
---|
30 | pass |
---|
31 | |
---|
32 | def tearDown(self): |
---|
33 | pass |
---|
34 | |
---|
35 | |
---|
36 | def test_smooth_attributes_to_mesh(self): |
---|
37 | a = [0.0, 0.0] |
---|
38 | b = [0.0, 5.0] |
---|
39 | c = [5.0, 0.0] |
---|
40 | points = [a, b, c] |
---|
41 | triangles = [ [1,0,2] ] #bac |
---|
42 | |
---|
43 | d1 = [1.0, 1.0] |
---|
44 | d2 = [1.0, 3.0] |
---|
45 | d3 = [3.0,1.0] |
---|
46 | z1 = 2 |
---|
47 | z2 = 4 |
---|
48 | z3 = 4 |
---|
49 | data_coords = [d1, d2, d3] |
---|
50 | |
---|
51 | z = [z1, z2, z3] |
---|
52 | fit = Fit(points, triangles, alpha=0) |
---|
53 | #print "interp.get_A()", interp.get_A() |
---|
54 | fit._build_matrix_AtA_Atz(ensure_numeric(data_coords), |
---|
55 | ensure_numeric(z)) |
---|
56 | #print "Atz - from fit", fit.Atz |
---|
57 | #print "AtA - from fit", fit.AtA.todense() |
---|
58 | #print "z",z |
---|
59 | |
---|
60 | assert allclose(fit.Atz, [2.8, 3.6, 3.6], atol=1e-7) |
---|
61 | |
---|
62 | f = fit.fit() |
---|
63 | |
---|
64 | answer = [0, 5., 5.] |
---|
65 | |
---|
66 | #print "f\n",f |
---|
67 | #print "answer\n",answer |
---|
68 | |
---|
69 | assert allclose(f, answer, atol=1e-7) |
---|
70 | |
---|
71 | def test_smooth_att_to_meshII(self): |
---|
72 | |
---|
73 | a = [0.0, 0.0] |
---|
74 | b = [0.0, 5.0] |
---|
75 | c = [5.0, 0.0] |
---|
76 | points = [a, b, c] |
---|
77 | triangles = [ [1,0,2] ] #bac |
---|
78 | |
---|
79 | d1 = [1.0, 1.0] |
---|
80 | d2 = [1.0, 2.0] |
---|
81 | d3 = [3.0,1.0] |
---|
82 | data_coords = [d1, d2, d3] |
---|
83 | z = linear_function(data_coords) |
---|
84 | #print "z",z |
---|
85 | |
---|
86 | interp = Fit(points, triangles, alpha=0.0) |
---|
87 | f = interp.fit(data_coords, z) |
---|
88 | answer = linear_function(points) |
---|
89 | #print "f\n",f |
---|
90 | #print "answer\n",answer |
---|
91 | |
---|
92 | assert allclose(f, answer) |
---|
93 | |
---|
94 | def test_smooth_attributes_to_meshIII(self): |
---|
95 | |
---|
96 | a = [-1.0, 0.0] |
---|
97 | b = [3.0, 4.0] |
---|
98 | c = [4.0,1.0] |
---|
99 | d = [-3.0, 2.0] #3 |
---|
100 | e = [-1.0,-2.0] |
---|
101 | f = [1.0, -2.0] #5 |
---|
102 | |
---|
103 | vertices = [a, b, c, d,e,f] |
---|
104 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
105 | |
---|
106 | point_coords = [[-2.0, 2.0], |
---|
107 | [-1.0, 1.0], |
---|
108 | [0.0,2.0], |
---|
109 | [1.0, 1.0], |
---|
110 | [2.0, 1.0], |
---|
111 | [0.0,0.0], |
---|
112 | [1.0, 0.0], |
---|
113 | [0.0, -1.0], |
---|
114 | [-0.2,-0.5], |
---|
115 | [-0.9, -1.5], |
---|
116 | [0.5, -1.9], |
---|
117 | [3.0,1.0]] |
---|
118 | |
---|
119 | z = linear_function(point_coords) |
---|
120 | interp = Fit(vertices, triangles, |
---|
121 | alpha=0.0) |
---|
122 | |
---|
123 | #print 'z',z |
---|
124 | f = interp.fit(point_coords,z) |
---|
125 | answer = linear_function(vertices) |
---|
126 | #print "f\n",f |
---|
127 | #print "answer\n",answer |
---|
128 | assert allclose(f, answer) |
---|
129 | |
---|
130 | |
---|
131 | def test_smooth_attributes_to_meshIV(self): |
---|
132 | """ Testing 2 attributes smoothed to the mesh |
---|
133 | """ |
---|
134 | |
---|
135 | a = [0.0, 0.0] |
---|
136 | b = [0.0, 5.0] |
---|
137 | c = [5.0, 0.0] |
---|
138 | points = [a, b, c] |
---|
139 | triangles = [ [1,0,2] ] #bac |
---|
140 | |
---|
141 | d1 = [1.0, 1.0] |
---|
142 | d2 = [1.0, 3.0] |
---|
143 | d3 = [3.0, 1.0] |
---|
144 | z1 = [2, 4] |
---|
145 | z2 = [4, 8] |
---|
146 | z3 = [4, 8] |
---|
147 | data_coords = [d1, d2, d3] |
---|
148 | |
---|
149 | z = [z1, z2, z3] |
---|
150 | fit = Fit(points, triangles, alpha=0) |
---|
151 | |
---|
152 | f = fit.fit(data_coords,z) |
---|
153 | answer = [[0,0], [5., 10.], [5., 10.]] |
---|
154 | assert allclose(f, answer) |
---|
155 | |
---|
156 | def test_smooth_attributes_to_mesh_build_fit_subset(self): |
---|
157 | |
---|
158 | a = [-1.0, 0.0] |
---|
159 | b = [3.0, 4.0] |
---|
160 | c = [4.0,1.0] |
---|
161 | d = [-3.0, 2.0] #3 |
---|
162 | e = [-1.0,-2.0] |
---|
163 | f = [1.0, -2.0] #5 |
---|
164 | |
---|
165 | vertices = [a, b, c, d,e,f] |
---|
166 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
167 | |
---|
168 | interp = Fit(vertices, triangles, |
---|
169 | alpha=0.0) |
---|
170 | |
---|
171 | point_coords = [[-2.0, 2.0], |
---|
172 | [-1.0, 1.0], |
---|
173 | [0.0,2.0], |
---|
174 | [1.0, 1.0], |
---|
175 | ] |
---|
176 | |
---|
177 | z = linear_function(point_coords) |
---|
178 | |
---|
179 | f = interp.build_fit_subset(point_coords,z) |
---|
180 | |
---|
181 | point_coords = [ |
---|
182 | [2.0, 1.0], |
---|
183 | [0.0,0.0], |
---|
184 | [1.0, 0.0], |
---|
185 | [0.0, -1.0], |
---|
186 | [-0.2,-0.5], |
---|
187 | [-0.9, -1.5], |
---|
188 | [0.5, -1.9], |
---|
189 | [3.0,1.0]] |
---|
190 | |
---|
191 | z = linear_function(point_coords) |
---|
192 | |
---|
193 | f = interp.build_fit_subset(point_coords,z) |
---|
194 | |
---|
195 | #print 'z',z |
---|
196 | f = interp.fit() |
---|
197 | answer = linear_function(vertices) |
---|
198 | #print "f\n",f |
---|
199 | #print "answer\n",answer |
---|
200 | assert allclose(f, answer) |
---|
201 | |
---|
202 | # test fit 2 mesh as well. |
---|
203 | def test_fit_file_blocking(self): |
---|
204 | |
---|
205 | a = [-1.0, 0.0] |
---|
206 | b = [3.0, 4.0] |
---|
207 | c = [4.0,1.0] |
---|
208 | d = [-3.0, 2.0] #3 |
---|
209 | e = [-1.0,-2.0] |
---|
210 | f = [1.0, -2.0] #5 |
---|
211 | |
---|
212 | vertices = [a, b, c, d,e,f] |
---|
213 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
214 | |
---|
215 | interp = Fit(vertices, triangles, |
---|
216 | alpha=0.0) |
---|
217 | |
---|
218 | |
---|
219 | fileName = tempfile.mktemp(".ddd") |
---|
220 | file = open(fileName,"w") |
---|
221 | file.write(" x,y, elevation \n\ |
---|
222 | -2.0, 2.0, 0.\n\ |
---|
223 | -1.0, 1.0, 0.\n\ |
---|
224 | 0.0, 2.0 , 2.\n\ |
---|
225 | 1.0, 1.0 , 2.\n\ |
---|
226 | 2.0, 1.0 ,3. \n\ |
---|
227 | 0.0, 0.0 , 0.\n\ |
---|
228 | 1.0, 0.0 , 1.\n\ |
---|
229 | 0.0, -1.0, -1.\n\ |
---|
230 | -0.2, -0.5, -0.7\n\ |
---|
231 | -0.9, -1.5, -2.4\n\ |
---|
232 | 0.5, -1.9, -1.4\n\ |
---|
233 | 3.0, 1.0 , 4.\n") |
---|
234 | file.close() |
---|
235 | |
---|
236 | f = interp.fit(fileName, max_read_lines=2) |
---|
237 | answer = linear_function(vertices) |
---|
238 | #print "f\n",f |
---|
239 | #print "answer\n",answer |
---|
240 | assert allclose(f, answer) |
---|
241 | os.remove(fileName) |
---|
242 | |
---|
243 | |
---|
244 | def test_fit_to_mesh_pts(self): |
---|
245 | a = [-1.0, 0.0] |
---|
246 | b = [3.0, 4.0] |
---|
247 | c = [4.0,1.0] |
---|
248 | d = [-3.0, 2.0] #3 |
---|
249 | e = [-1.0,-2.0] |
---|
250 | f = [1.0, -2.0] #5 |
---|
251 | |
---|
252 | vertices = [a, b, c, d,e,f] |
---|
253 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
254 | |
---|
255 | |
---|
256 | fileName = tempfile.mktemp(".txt") |
---|
257 | file = open(fileName,"w") |
---|
258 | file.write(" x, y, elevation \n\ |
---|
259 | -2.0, 2.0, 0.\n\ |
---|
260 | -1.0, 1.0, 0.\n\ |
---|
261 | 0.0, 2.0 , 2.\n\ |
---|
262 | 1.0, 1.0 , 2.\n\ |
---|
263 | 2.0, 1.0 ,3. \n\ |
---|
264 | 0.0, 0.0 , 0.\n\ |
---|
265 | 1.0, 0.0 , 1.\n\ |
---|
266 | 0.0, -1.0, -1.\n\ |
---|
267 | -0.2, -0.5, -0.7\n\ |
---|
268 | -0.9, -1.5, -2.4\n\ |
---|
269 | 0.5, -1.9, -1.4\n\ |
---|
270 | 3.0, 1.0 , 4.\n") |
---|
271 | file.close() |
---|
272 | print "fileName",fileName |
---|
273 | geo = Geospatial_data(fileName) |
---|
274 | fileName_pts = tempfile.mktemp(".pts") |
---|
275 | geo.export_points_file(fileName_pts) |
---|
276 | f = fit_to_mesh(vertices, triangles,fileName_pts, |
---|
277 | alpha=0.0, max_read_lines=2) #, |
---|
278 | #use_cache=True, verbose=True) |
---|
279 | answer = linear_function(vertices) |
---|
280 | #print "f\n",f |
---|
281 | #print "answer\n",answer |
---|
282 | assert allclose(f, answer) |
---|
283 | os.remove(fileName) |
---|
284 | os.remove(fileName_pts) |
---|
285 | |
---|
286 | def test_fit_to_mesh(self): |
---|
287 | |
---|
288 | a = [-1.0, 0.0] |
---|
289 | b = [3.0, 4.0] |
---|
290 | c = [4.0,1.0] |
---|
291 | d = [-3.0, 2.0] #3 |
---|
292 | e = [-1.0,-2.0] |
---|
293 | f = [1.0, -2.0] #5 |
---|
294 | |
---|
295 | vertices = [a, b, c, d,e,f] |
---|
296 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
297 | |
---|
298 | |
---|
299 | fileName = tempfile.mktemp(".ddd") |
---|
300 | file = open(fileName,"w") |
---|
301 | file.write(" x,y, elevation \n\ |
---|
302 | -2.0, 2.0, 0.\n\ |
---|
303 | -1.0, 1.0, 0.\n\ |
---|
304 | 0.0, 2.0 , 2.\n\ |
---|
305 | 1.0, 1.0 , 2.\n\ |
---|
306 | 2.0, 1.0 ,3. \n\ |
---|
307 | 0.0, 0.0 , 0.\n\ |
---|
308 | 1.0, 0.0 , 1.\n\ |
---|
309 | 0.0, -1.0, -1.\n\ |
---|
310 | -0.2, -0.5, -0.7\n\ |
---|
311 | -0.9, -1.5, -2.4\n\ |
---|
312 | 0.5, -1.9, -1.4\n\ |
---|
313 | 3.0, 1.0 , 4.\n") |
---|
314 | file.close() |
---|
315 | |
---|
316 | f = fit_to_mesh(vertices, triangles,fileName, |
---|
317 | alpha=0.0, max_read_lines=2) |
---|
318 | #use_cache=True, verbose=True) |
---|
319 | answer = linear_function(vertices) |
---|
320 | #print "f\n",f |
---|
321 | #print "answer\n",answer |
---|
322 | assert allclose(f, answer) |
---|
323 | |
---|
324 | os.remove(fileName) |
---|
325 | |
---|
326 | def test_fit_to_mesh_2_atts(self): |
---|
327 | |
---|
328 | a = [-1.0, 0.0] |
---|
329 | b = [3.0, 4.0] |
---|
330 | c = [4.0,1.0] |
---|
331 | d = [-3.0, 2.0] #3 |
---|
332 | e = [-1.0,-2.0] |
---|
333 | f = [1.0, -2.0] #5 |
---|
334 | |
---|
335 | vertices = [a, b, c, d,e,f] |
---|
336 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
337 | |
---|
338 | |
---|
339 | fileName = tempfile.mktemp(".ddd") |
---|
340 | file = open(fileName,"w") |
---|
341 | # the 2nd att name is wacky so it's the first key off a hash table |
---|
342 | file.write(" x,y, elevation, afriqction \n\ |
---|
343 | -2.0, 2.0, 0., 0.\n\ |
---|
344 | -1.0, 1.0, 0., 0.\n\ |
---|
345 | 0.0, 2.0 , 2., 20.\n\ |
---|
346 | 1.0, 1.0 , 2., 20.\n\ |
---|
347 | 2.0, 1.0 ,3., 30. \n\ |
---|
348 | 0.0, 0.0 , 0., 0.\n\ |
---|
349 | 1.0, 0.0 , 1., 10.\n\ |
---|
350 | 0.0, -1.0, -1., -10.\n\ |
---|
351 | -0.2, -0.5, -0.7, -7.\n\ |
---|
352 | -0.9, -1.5, -2.4, -24. \n\ |
---|
353 | 0.5, -1.9, -1.4, -14. \n\ |
---|
354 | 3.0, 1.0 , 4., 40. \n") |
---|
355 | file.close() |
---|
356 | |
---|
357 | f = fit_to_mesh(vertices, triangles,fileName, |
---|
358 | alpha=0.0, |
---|
359 | attribute_name='elevation', max_read_lines=2) |
---|
360 | answer = linear_function(vertices) |
---|
361 | #print "f\n",f |
---|
362 | #print "answer\n",answer |
---|
363 | assert allclose(f, answer) |
---|
364 | os.remove(fileName) |
---|
365 | |
---|
366 | def test_fit_and_interpolation(self): |
---|
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 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
378 | |
---|
379 | #Get (enough) datapoints |
---|
380 | data_points = [[ 0.66666667, 0.66666667], |
---|
381 | [ 1.33333333, 1.33333333], |
---|
382 | [ 2.66666667, 0.66666667], |
---|
383 | [ 0.66666667, 2.66666667], |
---|
384 | [ 0.0, 1.0], |
---|
385 | [ 0.0, 3.0], |
---|
386 | [ 1.0, 0.0], |
---|
387 | [ 1.0, 1.0], |
---|
388 | [ 1.0, 2.0], |
---|
389 | [ 1.0, 3.0], |
---|
390 | [ 2.0, 1.0], |
---|
391 | [ 3.0, 0.0], |
---|
392 | [ 3.0, 1.0]] |
---|
393 | |
---|
394 | z = linear_function(data_points) |
---|
395 | interp = Fit(points, triangles, |
---|
396 | alpha=0.0) |
---|
397 | |
---|
398 | answer = linear_function(points) |
---|
399 | |
---|
400 | f = interp.fit(data_points, z) |
---|
401 | |
---|
402 | #print "f",f |
---|
403 | #print "answer",answer |
---|
404 | assert allclose(f, answer) |
---|
405 | |
---|
406 | |
---|
407 | def test_smoothing_and_interpolation(self): |
---|
408 | |
---|
409 | a = [0.0, 0.0] |
---|
410 | b = [0.0, 2.0] |
---|
411 | c = [2.0, 0.0] |
---|
412 | d = [0.0, 4.0] |
---|
413 | e = [2.0, 2.0] |
---|
414 | f = [4.0, 0.0] |
---|
415 | |
---|
416 | points = [a, b, c, d, e, f] |
---|
417 | #bac, bce, ecf, dbe, daf, dae |
---|
418 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
419 | |
---|
420 | #Get (too few!) datapoints |
---|
421 | data_points = [[ 0.66666667, 0.66666667], |
---|
422 | [ 1.33333333, 1.33333333], |
---|
423 | [ 2.66666667, 0.66666667], |
---|
424 | [ 0.66666667, 2.66666667]] |
---|
425 | |
---|
426 | z = linear_function(data_points) |
---|
427 | answer = linear_function(points) |
---|
428 | |
---|
429 | #Make interpolator with too few data points and no smoothing |
---|
430 | interp = Fit(points, triangles, alpha=0.0) |
---|
431 | #Must raise an exception |
---|
432 | try: |
---|
433 | f = interp.fit(data_points,z) |
---|
434 | except ToFewPointsError: |
---|
435 | pass |
---|
436 | |
---|
437 | #Now try with smoothing parameter |
---|
438 | interp = Fit(points, triangles, alpha=1.0e-13) |
---|
439 | |
---|
440 | f = interp.fit(data_points,z) |
---|
441 | #f will be different from answer due to smoothing |
---|
442 | assert allclose(f, answer,atol=5) |
---|
443 | |
---|
444 | |
---|
445 | #Tests of smoothing matrix |
---|
446 | def test_smoothing_matrix_one_triangle(self): |
---|
447 | from Numeric import dot |
---|
448 | a = [0.0, 0.0] |
---|
449 | b = [0.0, 2.0] |
---|
450 | c = [2.0,0.0] |
---|
451 | points = [a, b, c] |
---|
452 | |
---|
453 | vertices = [ [1,0,2] ] #bac |
---|
454 | |
---|
455 | interp = Fit(points, vertices) |
---|
456 | |
---|
457 | assert allclose(interp.get_D(), [[1, -0.5, -0.5], |
---|
458 | [-0.5, 0.5, 0], |
---|
459 | [-0.5, 0, 0.5]]) |
---|
460 | |
---|
461 | #Define f(x,y) = x |
---|
462 | f = array([0,0,2]) #Value at global vertex 2 |
---|
463 | |
---|
464 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
465 | # int 1 dx dy = area = 2 |
---|
466 | assert dot(dot(f, interp.get_D()), f) == 2 |
---|
467 | |
---|
468 | #Define f(x,y) = y |
---|
469 | f = array([0,2,0]) #Value at global vertex 1 |
---|
470 | |
---|
471 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
472 | # int 1 dx dy = area = 2 |
---|
473 | assert dot(dot(f, interp.get_D()), f) == 2 |
---|
474 | |
---|
475 | #Define f(x,y) = x+y |
---|
476 | f = array([0,2,2]) #Values at global vertex 1 and 2 |
---|
477 | |
---|
478 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
479 | # int 2 dx dy = 2*area = 4 |
---|
480 | assert dot(dot(f, interp.get_D()), f) == 4 |
---|
481 | |
---|
482 | |
---|
483 | def test_smoothing_matrix_more_triangles(self): |
---|
484 | from Numeric import dot |
---|
485 | |
---|
486 | a = [0.0, 0.0] |
---|
487 | b = [0.0, 2.0] |
---|
488 | c = [2.0,0.0] |
---|
489 | d = [0.0, 4.0] |
---|
490 | e = [2.0, 2.0] |
---|
491 | f = [4.0,0.0] |
---|
492 | |
---|
493 | points = [a, b, c, d, e, f] |
---|
494 | #bac, bce, ecf, dbe, daf, dae |
---|
495 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
496 | |
---|
497 | interp = Fit(points, vertices) |
---|
498 | |
---|
499 | |
---|
500 | #assert allclose(interp.get_D(), [[1, -0.5, -0.5], |
---|
501 | # [-0.5, 0.5, 0], |
---|
502 | # [-0.5, 0, 0.5]]) |
---|
503 | |
---|
504 | #Define f(x,y) = x |
---|
505 | f = array([0,0,2,0,2,4]) #f evaluated at points a-f |
---|
506 | |
---|
507 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
508 | # int 1 dx dy = total area = 8 |
---|
509 | assert dot(dot(f, interp.get_D()), f) == 8 |
---|
510 | |
---|
511 | #Define f(x,y) = y |
---|
512 | f = array([0,2,0,4,2,0]) #f evaluated at points a-f |
---|
513 | |
---|
514 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
515 | # int 1 dx dy = area = 8 |
---|
516 | assert dot(dot(f, interp.get_D()), f) == 8 |
---|
517 | |
---|
518 | #Define f(x,y) = x+y |
---|
519 | f = array([0,2,2,4,4,4]) #f evaluated at points a-f |
---|
520 | |
---|
521 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
522 | # int 2 dx dy = 2*area = 16 |
---|
523 | assert dot(dot(f, interp.get_D()), f) == 16 |
---|
524 | |
---|
525 | |
---|
526 | |
---|
527 | def test_fit_and_interpolation_with_different_origins(self): |
---|
528 | """Fit a surface to one set of points. Then interpolate that surface |
---|
529 | using another set of points. |
---|
530 | This test tests situtaion where points and mesh belong to a different |
---|
531 | coordinate system as defined by origin. |
---|
532 | """ |
---|
533 | |
---|
534 | #Setup mesh used to represent fitted function |
---|
535 | a = [0.0, 0.0] |
---|
536 | b = [0.0, 2.0] |
---|
537 | c = [2.0, 0.0] |
---|
538 | d = [0.0, 4.0] |
---|
539 | e = [2.0, 2.0] |
---|
540 | f = [4.0, 0.0] |
---|
541 | |
---|
542 | points = [a, b, c, d, e, f] |
---|
543 | #bac, bce, ecf, dbe, daf, dae |
---|
544 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
545 | |
---|
546 | #Datapoints to fit from |
---|
547 | data_points1 = [[ 0.66666667, 0.66666667], |
---|
548 | [ 1.33333333, 1.33333333], |
---|
549 | [ 2.66666667, 0.66666667], |
---|
550 | [ 0.66666667, 2.66666667], |
---|
551 | [ 0.0, 1.0], |
---|
552 | [ 0.0, 3.0], |
---|
553 | [ 1.0, 0.0], |
---|
554 | [ 1.0, 1.0], |
---|
555 | [ 1.0, 2.0], |
---|
556 | [ 1.0, 3.0], |
---|
557 | [ 2.0, 1.0], |
---|
558 | [ 3.0, 0.0], |
---|
559 | [ 3.0, 1.0]] |
---|
560 | |
---|
561 | |
---|
562 | #First check that things are OK when using same origin |
---|
563 | mesh_origin = (56, 290000, 618000) #zone, easting, northing |
---|
564 | data_origin = (56, 290000, 618000) #zone, easting, northing |
---|
565 | |
---|
566 | |
---|
567 | #Fit surface to mesh |
---|
568 | interp = Fit(points, triangles, |
---|
569 | alpha=0.0, |
---|
570 | mesh_origin = mesh_origin) |
---|
571 | |
---|
572 | data_geo_spatial = Geospatial_data(data_points1, |
---|
573 | geo_reference = Geo_reference(56, 290000, 618000)) |
---|
574 | z = linear_function(data_points1) #Example z-values |
---|
575 | f = interp.fit(data_geo_spatial, z) #Fitted values at vertices |
---|
576 | |
---|
577 | #Shift datapoints according to new origins |
---|
578 | for k in range(len(data_points1)): |
---|
579 | data_points1[k][0] += mesh_origin[1] - data_origin[1] |
---|
580 | data_points1[k][1] += mesh_origin[2] - data_origin[2] |
---|
581 | |
---|
582 | |
---|
583 | |
---|
584 | #Fit surface to mesh |
---|
585 | interp = Fit(points, triangles, |
---|
586 | alpha=0.0) #, |
---|
587 | # mesh_origin = mesh_origin) |
---|
588 | |
---|
589 | f1 = interp.fit(data_points1,z) #Fitted values at vertices (using same z as before) |
---|
590 | |
---|
591 | assert allclose(f,f1), 'Fit should have been unaltered' |
---|
592 | |
---|
593 | |
---|
594 | def test_smooth_attributes_to_mesh_function(self): |
---|
595 | """ Testing 2 attributes smoothed to the mesh |
---|
596 | """ |
---|
597 | |
---|
598 | a = [0.0, 0.0] |
---|
599 | b = [0.0, 5.0] |
---|
600 | c = [5.0, 0.0] |
---|
601 | points = [a, b, c] |
---|
602 | triangles = [ [1,0,2] ] #bac |
---|
603 | |
---|
604 | d1 = [1.0, 1.0] |
---|
605 | d2 = [1.0, 3.0] |
---|
606 | d3 = [3.0, 1.0] |
---|
607 | z1 = [2, 4] |
---|
608 | z2 = [4, 8] |
---|
609 | z3 = [4, 8] |
---|
610 | data_coords = [d1, d2, d3] |
---|
611 | z = [z1, z2, z3] |
---|
612 | |
---|
613 | f = fit_to_mesh(points, triangles, data_coords, z, alpha=0.0) |
---|
614 | answer = [[0, 0], [5., 10.], [5., 10.]] |
---|
615 | |
---|
616 | assert allclose(f, answer) |
---|
617 | |
---|
618 | def test_fit_to_mesh_w_georef(self): |
---|
619 | """Simple check that georef works at the fit_to_mesh level |
---|
620 | """ |
---|
621 | |
---|
622 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
623 | |
---|
624 | #Mesh |
---|
625 | vertex_coordinates = [[0.76, 0.76], |
---|
626 | [0.76, 5.76], |
---|
627 | [5.76, 0.76]] |
---|
628 | triangles = [[0,2,1]] |
---|
629 | |
---|
630 | mesh_geo = Geo_reference(56,-0.76,-0.76) |
---|
631 | #print "mesh_geo.get_absolute(vertex_coordinates)", \ |
---|
632 | # mesh_geo.get_absolute(vertex_coordinates) |
---|
633 | |
---|
634 | #Data |
---|
635 | data_points = [[ 201.0, 401.0], |
---|
636 | [ 201.0, 403.0], |
---|
637 | [ 203.0, 401.0]] |
---|
638 | |
---|
639 | z = [2, 4, 4] |
---|
640 | |
---|
641 | data_geo = Geo_reference(56,-200,-400) |
---|
642 | |
---|
643 | #print "data_geo.get_absolute(data_points)", \ |
---|
644 | # data_geo.get_absolute(data_points) |
---|
645 | |
---|
646 | #Fit |
---|
647 | zz = fit_to_mesh(vertex_coordinates, triangles, data_points, z, |
---|
648 | data_origin = data_geo.get_origin(), |
---|
649 | mesh_origin = mesh_geo.get_origin(), |
---|
650 | alpha = 0) |
---|
651 | assert allclose( zz, [0,5,5] ) |
---|
652 | |
---|
653 | |
---|
654 | def Not_yet_test_smooth_att_to_mesh_with_excess_verts(self): |
---|
655 | |
---|
656 | a = [0.0, 0.0] |
---|
657 | b = [0.0, 5.0] |
---|
658 | c = [5.0, 0.0] |
---|
659 | d = [1.0, 1.0] |
---|
660 | e = [18.0, 1000.0] |
---|
661 | |
---|
662 | points = [a, b, c, d, e] |
---|
663 | triangles = [ [1,0,2] ] #bac |
---|
664 | |
---|
665 | d1 = [1.0, 1.0] |
---|
666 | d2 = [1.0, 2.0] |
---|
667 | d3 = [3.0,1.0] |
---|
668 | d4 = [2.0,3.0] |
---|
669 | d5 = [2.0,2.0] |
---|
670 | d6 = [1.0,3.0] |
---|
671 | data_coords = [d1, d2, d3, d4, d5, d6] |
---|
672 | z = linear_function(data_coords) |
---|
673 | #print "z",z |
---|
674 | |
---|
675 | interp = Fit(points, triangles, alpha=0.0) |
---|
676 | |
---|
677 | try: |
---|
678 | f = interp.fit(data_coords, z) |
---|
679 | except VertsWithNoTrianglesError: |
---|
680 | pass |
---|
681 | else: |
---|
682 | raise 'Verts with no triangles did not raise error!' |
---|
683 | |
---|
684 | #f = interp.fit(data_coords, z) |
---|
685 | #answer = linear_function(points) |
---|
686 | |
---|
687 | # Removing the bad verts that we don't care about |
---|
688 | #f = f[0:3] |
---|
689 | #answer = answer[0:3] |
---|
690 | #assert allclose(f, answer) |
---|
691 | |
---|
692 | #------------------------------------------------------------- |
---|
693 | if __name__ == "__main__": |
---|
694 | suite = unittest.makeSuite(Test_Fit,'test') |
---|
695 | #suite = unittest.makeSuite(Test_Fit,'test_smooth_att_to_mesh_with_excess_verts') |
---|
696 | #suite = unittest.makeSuite(Test_Fit,'test_smooth_attributes_to_mesh_one_point') |
---|
697 | runner = unittest.TextTestRunner(verbosity=1) |
---|
698 | runner.run(suite) |
---|
699 | |
---|
700 | |
---|
701 | |
---|
702 | |
---|
703 | |
---|