1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | from math import sqrt |
---|
5 | |
---|
6 | from least_squares import * |
---|
7 | from config import epsilon |
---|
8 | from Numeric import allclose, array |
---|
9 | |
---|
10 | def distance(x, y): |
---|
11 | return sqrt( sum( (array(x)-array(y))**2 )) |
---|
12 | |
---|
13 | class TestCase(unittest.TestCase): |
---|
14 | def setUp(self): |
---|
15 | pass |
---|
16 | |
---|
17 | def tearDown(self): |
---|
18 | pass |
---|
19 | |
---|
20 | |
---|
21 | def test_datapoint_at_centroid(self): |
---|
22 | a = [0.0, 0.0] |
---|
23 | b = [0.0, 2.0] |
---|
24 | c = [2.0,0.0] |
---|
25 | points = [a, b, c] |
---|
26 | triangles = [ [1,0,2] ] #bac |
---|
27 | |
---|
28 | data = [ [2.0/3, 2.0/3] ] #Use centroid as one data point |
---|
29 | |
---|
30 | mesh = Interpolation(points, triangles, data) |
---|
31 | assert allclose(mesh.matrix, [[1./3, 1./3, 1./3]]) |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | def test_datapoints_at_vertices(self): |
---|
36 | """Test that data points coinciding with vertices yield a diagonal matrix |
---|
37 | """ |
---|
38 | |
---|
39 | a = [0.0, 0.0] |
---|
40 | b = [0.0, 2.0] |
---|
41 | c = [2.0,0.0] |
---|
42 | points = [a, b, c] |
---|
43 | triangles = [ [1,0,2] ] #bac |
---|
44 | |
---|
45 | data = points #Use data at vertices |
---|
46 | |
---|
47 | mesh = Interpolation(points, triangles, data) |
---|
48 | assert allclose(mesh.matrix, [[1., 0., 0.], |
---|
49 | [0., 1., 0.], |
---|
50 | [0., 0., 1.]]) |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | def test_datapoints_on_edge_midpoints(self): |
---|
55 | """Try datapoints midway on edges - |
---|
56 | each point should affect two matrix entries equally |
---|
57 | """ |
---|
58 | |
---|
59 | a = [0.0, 0.0] |
---|
60 | b = [0.0, 2.0] |
---|
61 | c = [2.0,0.0] |
---|
62 | points = [a, b, c] |
---|
63 | triangles = [ [1,0,2] ] #bac |
---|
64 | |
---|
65 | data = [ [0., 1.], [1., 0.], [1., 1.] ] |
---|
66 | |
---|
67 | mesh = Interpolation(points, triangles, data) |
---|
68 | |
---|
69 | assert allclose(mesh.matrix, [[0.5, 0.5, 0.0], #Affects vertex 1 and 0 |
---|
70 | [0.5, 0.0, 0.5], #Affects vertex 0 and 2 |
---|
71 | [0.0, 0.5, 0.5]]) #Affects vertex 1 and 2 |
---|
72 | |
---|
73 | |
---|
74 | def test_datapoints_on_edges(self): |
---|
75 | """Try datapoints on edges - |
---|
76 | each point should affect two matrix entries in proportion |
---|
77 | """ |
---|
78 | |
---|
79 | a = [0.0, 0.0] |
---|
80 | b = [0.0, 2.0] |
---|
81 | c = [2.0,0.0] |
---|
82 | points = [a, b, c] |
---|
83 | triangles = [ [1,0,2] ] #bac |
---|
84 | |
---|
85 | data = [ [0., 1.5], [1.5, 0.], [1.5, 0.5] ] |
---|
86 | |
---|
87 | mesh = Interpolation(points, triangles, data) |
---|
88 | |
---|
89 | assert allclose(mesh.matrix, [[0.25, 0.75, 0.0], #Affects vertex 1 and 0 |
---|
90 | [0.25, 0.0, 0.75], #Affects vertex 0 and 2 |
---|
91 | [0.0, 0.25, 0.75]]) #Affects vertex 1 and 2 |
---|
92 | |
---|
93 | def test_arbitrary_datapoints(self): |
---|
94 | """Try arbitrary datapoints |
---|
95 | """ |
---|
96 | |
---|
97 | from Numeric import sum |
---|
98 | |
---|
99 | a = [0.0, 0.0] |
---|
100 | b = [0.0, 2.0] |
---|
101 | c = [2.0,0.0] |
---|
102 | points = [a, b, c] |
---|
103 | triangles = [ [1,0,2] ] #bac |
---|
104 | |
---|
105 | data = [ [0.2, 1.5], [0.123, 1.768], [1.43, 0.44] ] |
---|
106 | |
---|
107 | mesh = Interpolation(points, triangles, data) |
---|
108 | |
---|
109 | assert allclose(sum(mesh.matrix, axis=1), 1.0) |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | def test_more_triangles(self): |
---|
114 | |
---|
115 | |
---|
116 | #FIXME: Not finished yet |
---|
117 | a = [0.0, 0.0] |
---|
118 | b = [0.0, 2.0] |
---|
119 | c = [2.0,0.0] |
---|
120 | d = [0.0, 4.0] |
---|
121 | e = [2.0, 2.0] |
---|
122 | f = [4.0,0.0] |
---|
123 | |
---|
124 | points = [a, b, c, d, e, f] |
---|
125 | #bac, bce, ecf, dbe, daf, dae |
---|
126 | triangles = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4], [3,0,5], [3,0,4]] |
---|
127 | |
---|
128 | |
---|
129 | #Data points |
---|
130 | data = [ [0., 2.0], [1.15, 0.75], [0.8888, 0.21], [1.000001, 0.00001] ] |
---|
131 | |
---|
132 | mesh = Interpolation(points, triangles, data) |
---|
133 | |
---|
134 | #print mesh.matrix |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | #------------------------------------------------------------- |
---|
140 | if __name__ == "__main__": |
---|
141 | suite = unittest.makeSuite(TestCase,'test') |
---|
142 | runner = unittest.TextTestRunner() |
---|
143 | runner.run(suite) |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | |
---|