1 {-# LANGUAGE BangPatterns #-}
2 -- | The Grid module just contains the Grid type and two constructors
3 -- for it. We hide the main Grid constructor because we don't want
4 -- to allow instantiation of a grid with h <= 0.
13 import qualified Data.Array.Repa as R
14 import Test.HUnit (Assertion, assertEqual)
15 import Test.Framework (Test, testGroup)
16 import Test.Framework.Providers.HUnit (testCase)
17 import Test.Framework.Providers.QuickCheck2 (testProperty)
18 import Test.QuickCheck ((==>),
24 import Assertions (assertAlmostEqual, assertTrue)
25 import Comparisons ((~=))
26 import Cube (Cube(Cube),
27 find_containing_tetrahedron,
30 import Examples (trilinear, trilinear9x9x9, zeros)
31 import FunctionValues (make_values, value_at)
32 import Point (Point(..))
33 import ScaleFactor (ScaleFactor)
35 Tetrahedron(v0,v1,v2,v3),
39 import Values (Values3D, dims, empty3d, zoom_shape)
42 -- | Our problem is defined on a Grid. The grid size is given by the
43 -- positive number h. The function values are the values of the
44 -- function at the grid points, which are distance h from one
45 -- another in each direction (x,y,z).
46 data Grid = Grid { h :: Double, -- MUST BE GREATER THAN ZERO!
47 function_values :: Values3D }
51 instance Arbitrary Grid where
53 (Positive h') <- arbitrary :: Gen (Positive Double)
54 fvs <- arbitrary :: Gen Values3D
59 -- | Takes a grid and a position as an argument and returns the cube
60 -- centered on that position. If there is no cube there, well, you
61 -- shouldn't have done that. The omitted "otherwise" case actually
62 -- does improve performance.
63 cube_at :: Grid -> Int -> Int -> Int -> Cube
65 Cube delta i j k fvs' tet_vol
67 fvs = function_values g
68 fvs' = make_values fvs i j k
70 tet_vol = (1/24)*(delta^(3::Int))
73 -- The first cube along any axis covers (-h/2, h/2). The second
74 -- covers (h/2, 3h/2). The third, (3h/2, 5h/2), and so on.
76 -- We translate the (x,y,z) coordinates forward by 'h/2' so that the
77 -- first covers (0, h), the second covers (h, 2h), etc. This makes
78 -- it easy to figure out which cube contains the given point.
79 calculate_containing_cube_coordinate :: Grid -> Double -> Int
80 calculate_containing_cube_coordinate g coord
81 -- Don't use a cube on the boundary if we can help it. This
82 -- returns cube #1 if we would have returned cube #0 and cube #1
85 | coord == offset && (xsize > 1 && ysize > 1 && zsize > 1) = 1
86 | otherwise = (ceiling ( (coord + offset) / cube_width )) - 1
88 (xsize, ysize, zsize) = dims (function_values g)
90 offset = cube_width / 2
93 -- | Takes a 'Grid', and returns a 'Cube' containing the given 'Point'.
94 -- Since our grid is rectangular, we can figure this out without having
95 -- to check every cube.
96 find_containing_cube :: Grid -> Point -> Cube
97 find_containing_cube g (Point x y z) =
100 i = calculate_containing_cube_coordinate g x
101 j = calculate_containing_cube_coordinate g y
102 k = calculate_containing_cube_coordinate g z
105 zoom_lookup :: Values3D -> ScaleFactor -> a -> (R.DIM3 -> Double)
106 zoom_lookup v3d scale_factor _ =
107 zoom_result v3d scale_factor
110 zoom_result :: Values3D -> ScaleFactor -> R.DIM3 -> Double
111 zoom_result v3d (sfx, sfy, sfz) (R.Z R.:. m R.:. n R.:. o) =
116 m' = (fromIntegral m) / (fromIntegral sfx) - offset
117 n' = (fromIntegral n) / (fromIntegral sfy) - offset
118 o' = (fromIntegral o) / (fromIntegral sfz) - offset
120 cube = find_containing_cube g p
121 t = find_containing_tetrahedron cube p
125 zoom :: Values3D -> ScaleFactor -> Values3D
126 zoom v3d scale_factor
127 | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
129 R.compute $ R.unsafeTraverse v3d transExtent f
131 (xsize, ysize, zsize) = dims v3d
132 transExtent = zoom_shape scale_factor
133 f = zoom_lookup v3d scale_factor
136 -- | Check all coefficients of tetrahedron0 belonging to the cube
137 -- centered on (1,1,1) with a grid constructed from the trilinear
138 -- values. See example one in the paper.
140 -- We also verify that the four vertices on face0 of the cube are
141 -- in the correct location.
143 trilinear_c0_t0_tests :: Test.Framework.Test
144 trilinear_c0_t0_tests =
145 testGroup "trilinear c0 t0"
146 [testGroup "coefficients"
147 [testCase "c0030 is correct" test_trilinear_c0030,
148 testCase "c0003 is correct" test_trilinear_c0003,
149 testCase "c0021 is correct" test_trilinear_c0021,
150 testCase "c0012 is correct" test_trilinear_c0012,
151 testCase "c0120 is correct" test_trilinear_c0120,
152 testCase "c0102 is correct" test_trilinear_c0102,
153 testCase "c0111 is correct" test_trilinear_c0111,
154 testCase "c0210 is correct" test_trilinear_c0210,
155 testCase "c0201 is correct" test_trilinear_c0201,
156 testCase "c0300 is correct" test_trilinear_c0300,
157 testCase "c1020 is correct" test_trilinear_c1020,
158 testCase "c1002 is correct" test_trilinear_c1002,
159 testCase "c1011 is correct" test_trilinear_c1011,
160 testCase "c1110 is correct" test_trilinear_c1110,
161 testCase "c1101 is correct" test_trilinear_c1101,
162 testCase "c1200 is correct" test_trilinear_c1200,
163 testCase "c2010 is correct" test_trilinear_c2010,
164 testCase "c2001 is correct" test_trilinear_c2001,
165 testCase "c2100 is correct" test_trilinear_c2100,
166 testCase "c3000 is correct" test_trilinear_c3000],
168 testGroup "face0 vertices"
169 [testCase "v0 is correct" test_trilinear_f0_t0_v0,
170 testCase "v1 is correct" test_trilinear_f0_t0_v1,
171 testCase "v2 is correct" test_trilinear_f0_t0_v2,
172 testCase "v3 is correct" test_trilinear_f0_t0_v3]
176 cube = cube_at g 1 1 1
177 t = tetrahedron cube 0
179 test_trilinear_c0030 :: Assertion
180 test_trilinear_c0030 =
181 assertAlmostEqual "c0030 is correct" (c t 0 0 3 0) (17/8)
183 test_trilinear_c0003 :: Assertion
184 test_trilinear_c0003 =
185 assertAlmostEqual "c0003 is correct" (c t 0 0 0 3) (27/8)
187 test_trilinear_c0021 :: Assertion
188 test_trilinear_c0021 =
189 assertAlmostEqual "c0021 is correct" (c t 0 0 2 1) (61/24)
191 test_trilinear_c0012 :: Assertion
192 test_trilinear_c0012 =
193 assertAlmostEqual "c0012 is correct" (c t 0 0 1 2) (71/24)
195 test_trilinear_c0120 :: Assertion
196 test_trilinear_c0120 =
197 assertAlmostEqual "c0120 is correct" (c t 0 1 2 0) (55/24)
199 test_trilinear_c0102 :: Assertion
200 test_trilinear_c0102 =
201 assertAlmostEqual "c0102 is correct" (c t 0 1 0 2) (73/24)
203 test_trilinear_c0111 :: Assertion
204 test_trilinear_c0111 =
205 assertAlmostEqual "c0111 is correct" (c t 0 1 1 1) (8/3)
207 test_trilinear_c0210 :: Assertion
208 test_trilinear_c0210 =
209 assertAlmostEqual "c0210 is correct" (c t 0 2 1 0) (29/12)
211 test_trilinear_c0201 :: Assertion
212 test_trilinear_c0201 =
213 assertAlmostEqual "c0201 is correct" (c t 0 2 0 1) (11/4)
215 test_trilinear_c0300 :: Assertion
216 test_trilinear_c0300 =
217 assertAlmostEqual "c0300 is correct" (c t 0 3 0 0) (5/2)
219 test_trilinear_c1020 :: Assertion
220 test_trilinear_c1020 =
221 assertAlmostEqual "c1020 is correct" (c t 1 0 2 0) (8/3)
223 test_trilinear_c1002 :: Assertion
224 test_trilinear_c1002 =
225 assertAlmostEqual "c1002 is correct" (c t 1 0 0 2) (23/6)
227 test_trilinear_c1011 :: Assertion
228 test_trilinear_c1011 =
229 assertAlmostEqual "c1011 is correct" (c t 1 0 1 1) (13/4)
231 test_trilinear_c1110 :: Assertion
232 test_trilinear_c1110 =
233 assertAlmostEqual "c1110 is correct" (c t 1 1 1 0) (23/8)
235 test_trilinear_c1101 :: Assertion
236 test_trilinear_c1101 =
237 assertAlmostEqual "c1101 is correct" (c t 1 1 0 1) (27/8)
239 test_trilinear_c1200 :: Assertion
240 test_trilinear_c1200 =
241 assertAlmostEqual "c1200 is correct" (c t 1 2 0 0) 3
243 test_trilinear_c2010 :: Assertion
244 test_trilinear_c2010 =
245 assertAlmostEqual "c2010 is correct" (c t 2 0 1 0) (10/3)
247 test_trilinear_c2001 :: Assertion
248 test_trilinear_c2001 =
249 assertAlmostEqual "c2001 is correct" (c t 2 0 0 1) 4
251 test_trilinear_c2100 :: Assertion
252 test_trilinear_c2100 =
253 assertAlmostEqual "c2100 is correct" (c t 2 1 0 0) (7/2)
255 test_trilinear_c3000 :: Assertion
256 test_trilinear_c3000 =
257 assertAlmostEqual "c3000 is correct" (c t 3 0 0 0) 4
259 test_trilinear_f0_t0_v0 :: Assertion
260 test_trilinear_f0_t0_v0 =
261 assertEqual "v0 is correct" (v0 t) (Point 1 1 1)
263 test_trilinear_f0_t0_v1 :: Assertion
264 test_trilinear_f0_t0_v1 =
265 assertEqual "v1 is correct" (v1 t) (Point 0.5 1 1)
267 test_trilinear_f0_t0_v2 :: Assertion
268 test_trilinear_f0_t0_v2 =
269 assertEqual "v2 is correct" (v2 t) (Point 0.5 0.5 1.5)
271 test_trilinear_f0_t0_v3 :: Assertion
272 test_trilinear_f0_t0_v3 =
273 assertEqual "v3 is correct" (v3 t) (Point 0.5 1.5 1.5)
276 test_trilinear_reproduced :: Assertion
277 test_trilinear_reproduced =
278 assertTrue "trilinears are reproduced correctly" $
279 and [p (Point i' j' k') ~= value_at trilinear i j k
285 let p = polynomial t,
286 let i' = fromIntegral i,
287 let j' = fromIntegral j,
288 let k' = fromIntegral k]
291 cs = [ cube_at g ci cj ck | ci <- [0..2], cj <- [0..2], ck <- [0..2] ]
294 test_zeros_reproduced :: Assertion
295 test_zeros_reproduced =
296 assertTrue "the zero function is reproduced correctly" $
297 and [p (Point i' j' k') ~= value_at zeros i j k
301 let i' = fromIntegral i,
302 let j' = fromIntegral j,
303 let k' = fromIntegral k,
306 let p = polynomial t0 ]
309 cs = [ cube_at g ci cj ck | ci <- [0..2], cj <- [0..2], ck <- [0..2] ]
312 -- | Make sure we can reproduce a 9x9x9 trilinear from the 3x3x3 one.
313 test_trilinear9x9x9_reproduced :: Assertion
314 test_trilinear9x9x9_reproduced =
315 assertTrue "trilinear 9x9x9 is reproduced correctly" $
316 and [p (Point i' j' k') ~= value_at trilinear9x9x9 i j k
321 let p = polynomial t,
322 let i' = (fromIntegral i) * 0.5,
323 let j' = (fromIntegral j) * 0.5,
324 let k' = (fromIntegral k) * 0.5]
331 prop_cube_indices_never_go_out_of_bounds :: Grid -> Gen Bool
332 prop_cube_indices_never_go_out_of_bounds g =
335 let coordmin = negate (delta/2)
337 let (xsize, ysize, zsize) = dims $ function_values g
338 let xmax = delta*(fromIntegral xsize) - (delta/2)
339 let ymax = delta*(fromIntegral ysize) - (delta/2)
340 let zmax = delta*(fromIntegral zsize) - (delta/2)
342 x <- choose (coordmin, xmax)
343 y <- choose (coordmin, ymax)
344 z <- choose (coordmin, zmax)
346 let idx_x = calculate_containing_cube_coordinate g x
347 let idx_y = calculate_containing_cube_coordinate g y
348 let idx_z = calculate_containing_cube_coordinate g z
352 idx_x <= xsize - 1 &&
354 idx_y <= ysize - 1 &&
359 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). Note that the
360 -- third and fourth indices of c-t10 have been switched. This is
361 -- because we store the triangles oriented such that their volume is
362 -- positive. If T and T-tilde share \<v1,v2,v3\> and v0,v0-tilde point
363 -- in opposite directions, one of them has to have negative volume!
364 prop_c0120_identity :: Grid -> Property
365 prop_c0120_identity g =
366 xsize >= 3 && ysize >= 3 && zsize >= 3 ==>
367 c t0 0 1 2 0 ~= (c t0 1 0 2 0 + c t10 1 0 0 2) / 2
369 fvs = function_values g
370 (xsize, ysize, zsize) = dims fvs
371 cube0 = cube_at g 1 1 1
372 cube1 = cube_at g 0 1 1
373 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
374 t10 = tetrahedron cube1 10
377 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
378 -- 'prop_c0120_identity'.
379 prop_c0111_identity :: Grid -> Property
380 prop_c0111_identity g =
381 xsize >= 3 && ysize >= 3 && zsize >= 3 ==>
382 c t0 0 1 1 1 ~= (c t0 1 0 1 1 + c t10 1 0 1 1) / 2
384 fvs = function_values g
385 (xsize, ysize, zsize) = dims fvs
386 cube0 = cube_at g 1 1 1
387 cube1 = cube_at g 0 1 1
388 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
389 t10 = tetrahedron cube1 10
392 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
393 -- 'prop_c0120_identity'.
394 prop_c0201_identity :: Grid -> Property
395 prop_c0201_identity g =
396 xsize >= 3 && ysize >= 3 && zsize >= 3 ==>
397 c t0 0 2 0 1 ~= (c t0 1 1 0 1 + c t10 1 1 1 0) / 2
399 fvs = function_values g
400 (xsize, ysize, zsize) = dims fvs
401 cube0 = cube_at g 1 1 1
402 cube1 = cube_at g 0 1 1
403 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
404 t10 = tetrahedron cube1 10
407 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
408 -- 'prop_c0120_identity'.
409 prop_c0102_identity :: Grid -> Property
410 prop_c0102_identity g =
411 xsize >= 3 && ysize >= 3 && zsize >= 3 ==>
412 c t0 0 1 0 2 ~= (c t0 1 0 0 2 + c t10 1 0 2 0) / 2
414 fvs = function_values g
415 (xsize, ysize, zsize) = dims fvs
416 cube0 = cube_at g 1 1 1
417 cube1 = cube_at g 0 1 1
418 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
419 t10 = tetrahedron cube1 10
422 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
423 -- 'prop_c0120_identity'.
424 prop_c0210_identity :: Grid -> Property
425 prop_c0210_identity g =
426 xsize >= 3 && ysize >= 3 && zsize >= 3 ==>
427 c t0 0 2 1 0 ~= (c t0 1 1 1 0 + c t10 1 1 0 1) / 2
429 fvs = function_values g
430 (xsize, ysize, zsize) = dims fvs
431 cube0 = cube_at g 1 1 1
432 cube1 = cube_at g 0 1 1
433 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
434 t10 = tetrahedron cube1 10
437 -- | Given in Sorokina and Zeilfelder, p. 80, (2.9). See
438 -- 'prop_c0120_identity'.
439 prop_c0300_identity :: Grid -> Property
440 prop_c0300_identity g =
441 xsize >= 3 && ysize >= 3 && zsize >= 3 ==>
442 c t0 0 3 0 0 ~= (c t0 1 2 0 0 + c t10 1 2 0 0) / 2
444 fvs = function_values g
445 (xsize, ysize, zsize) = dims fvs
446 cube0 = cube_at g 1 1 1
447 cube1 = cube_at g 0 1 1
448 t0 = tetrahedron cube0 0 -- These two tetrahedra share a face.
449 t10 = tetrahedron cube1 10
452 -- | All of the properties from Section (2.9), p. 80. These require a
453 -- grid since they refer to two adjacent cubes.
454 p80_29_properties :: Test.Framework.Test
456 testGroup "p. 80, Section (2.9) Properties" [
457 testProperty "c0120 identity" prop_c0120_identity,
458 testProperty "c0111 identity" prop_c0111_identity,
459 testProperty "c0201 identity" prop_c0201_identity,
460 testProperty "c0102 identity" prop_c0102_identity,
461 testProperty "c0210 identity" prop_c0210_identity,
462 testProperty "c0300 identity" prop_c0300_identity ]
465 grid_tests :: Test.Framework.Test
467 testGroup "Grid Tests" [
468 trilinear_c0_t0_tests,
470 testProperty "cube indices within bounds"
471 prop_cube_indices_never_go_out_of_bounds ]
474 -- Do the slow tests last so we can stop paying attention.
475 slow_tests :: Test.Framework.Test
477 testGroup "Slow Tests" [
478 testCase "trilinear reproduced" test_trilinear_reproduced,
479 testCase "trilinear9x9x9 reproduced" test_trilinear9x9x9_reproduced,
480 testCase "zeros reproduced" test_zeros_reproduced ]