File: test.py

package info (click to toggle)
ffc 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 50,704 kB
  • sloc: cpp: 572,515; python: 15,790; makefile: 134; sh: 67
file content (330 lines) | stat: -rw-r--r-- 12,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"Unit tests for FFC"

# Copyright (C) 2007-2009 Anders Logg
#
# This file is part of FFC.
#
# FFC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FFC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FFC. If not, see <http://www.gnu.org/licenses/>.
#
# Modified by Marie E. Rognes, 2010
#
# First added:  2007-02-06
# Last changed: 2009-02-24

import unittest
import sys
import numpy
import math
import os
import instant
from time import time

sys.path.append(os.path.join(os.pardir, os.pardir))

from ufl import *
from ffc.fiatinterface import create_element as create
from ffc import jit

interval = [(0,), (1,)]
triangle = [(0, 0), (1, 0), (0, 1)]
tetrahedron = [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)]
num_points = 5

# FIXME: A hack that will make the test run on Ubuntu 4.11, which ships two
# FIXME: versions of SWIG. UFC automatically uses the swig2.0 binary, which
# FIXME: FFC also need to use
use_swig2_binary = instant.check_and_set_swig_binary("swig2.0")

def random_point(shape):
    w = numpy.random.random(len(shape))
    return sum([numpy.array(shape[i])*w[i] for i in range(len(shape))]) / sum(w)

class SpaceDimensionTests(unittest.TestCase):

    def testContinuousLagrange(self):
        "Test space dimensions of continuous Lagrange elements."

        P1 = create(FiniteElement("Lagrange", "triangle", 1))
        self.assertEqual(P1.space_dimension(), 3)

        P2 = create(FiniteElement("Lagrange", "triangle", 2))
        self.assertEqual(P2.space_dimension(), 6)

        P3 = create(FiniteElement("Lagrange", "triangle", 3))
        self.assertEqual(P3.space_dimension(), 10)

    def testDiscontinuousLagrange(self):
        "Test space dimensions of discontinuous Lagrange elements."

        P0 = create(FiniteElement("DG", "triangle", 0))
        self.assertEqual(P0.space_dimension(), 1)

        P1 = create(FiniteElement("DG", "triangle", 1))
        self.assertEqual(P1.space_dimension(), 3)

        P2 = create(FiniteElement("DG", "triangle", 2))
        self.assertEqual(P2.space_dimension(), 6)

        P3 = create(FiniteElement("DG", "triangle", 3))
        self.assertEqual(P3.space_dimension(), 10)

class FunctionValueTests(unittest.TestCase):
    """
    These tests examine tabulate gives the correct answers for a the
    supported (non-mixed) elements of polynomial degree less than or
    equal to 3
    """

    # FIXME: Add tests for NED and BDM/RT in 3D.

    def _check_function_values(self, points, element, reference):
        for x in points:
            table = element.tabulate(0, (x,))
            basis = table[table.keys()[0]]
            for i in range(len(basis)):
                if element.value_shape() == ():
                    self.assertAlmostEqual(basis[i], reference[i](x))
                else:
                    for k in range(element.value_shape()[0]):
                        self.assertAlmostEqual(basis[i][k][0],
                                               reference[i](x)[k])

    def testContinuousLagrange1D(self):
        "Test values of continuous Lagrange functions in 1D."

        element = create(FiniteElement("Lagrange", "interval", 1))
        reference = [lambda x: 1 - x[0],
                     lambda x: x[0]]

        points = [random_point(interval) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testContinuousLagrange2D(self):
        "Test values of continuous Lagrange functions in 2D."

        element = create(FiniteElement("Lagrange", "triangle", 1))
        reference = [lambda x: 1 - x[0] - x[1],
                     lambda x: x[0],
                     lambda x: x[1]]

        points = [random_point(triangle) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testContinuousLagrange3D(self):
        "Test values of continuous Lagrange functions in 3D."

        element = create(FiniteElement("Lagrange", "tetrahedron", 1))
        reference = [lambda x: 1 - x[0] - x[1] - x[2],
                     lambda x: x[0],
                     lambda x: x[1],
                     lambda x: x[2]]

        points = [random_point(tetrahedron) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testDiscontinuousLagrange1D(self):
        "Test values of discontinuous Lagrange functions in 1D."

        element = create(FiniteElement("DG", "interval", 1))
        reference = [lambda x: 1 - x[0],
                     lambda x: x[0]]

        points = [random_point(interval) for i in range(num_points)]
        self._check_function_values(points, element, reference)


    def testDiscontinuousLagrange2D(self):
        "Test values of discontinuous Lagrange functions in 2D."

        element = create(FiniteElement("DG", "triangle", 1))
        reference = [lambda x: 1 - x[0] - x[1],
                     lambda x: x[0],
                     lambda x: x[1]]

        points = [random_point(triangle) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testDiscontinuousLagrange3D(self):
        "Test values of discontinuous Lagrange functions in 3D."

        element = create(FiniteElement("DG", "tetrahedron", 1))
        reference = [lambda x: 1 - x[0] - x[1] - x[2],
                     lambda x: x[0],
                     lambda x: x[1],
                     lambda x: x[2]]

        points = [random_point(tetrahedron) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testBDM1_2D(self):
        "Test values of BDM1."

        element = create(FiniteElement("Brezzi-Douglas-Marini", "triangle", 1))
        reference = [lambda x: (2*x[0], -x[1]),
                     lambda x: (-x[0], 2*x[1]),
                     lambda x: (2 - 2*x[0] - 3*x[1], x[1]),
                     lambda x: (- 1 + x[0] + 3*x[1], - 2*x[1]),
                     lambda x: (-x[0], -2 + 3*x[0] + 2*x[1]),
                     lambda x: (2*x[0], 1 - 3*x[0] - x[1])]

        points = [random_point(triangle) for i in range(num_points)]
        self._check_function_values(points, element, reference)


    def testRT1_2D(self):
        "Test values of RT1."

        element = create(FiniteElement("Raviart-Thomas", "triangle", 1))
        reference = [lambda x: (x[0], x[1]),
                     lambda x: (1 - x[0], -x[1]),
                     lambda x: (x[0], x[1] - 1)]

        points = [random_point(triangle) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testRT2_2D(self):
        "Test values of RT2."

        element = create(FiniteElement("Raviart-Thomas", "triangle", 2))

        reference = [ lambda x: (-x[0] + 3*x[0]**2,
                                 -x[1] + 3*x[0]*x[1]),
                      lambda x: (-x[0] + 3*x[0]*x[1],
                                 -x[1] + 3*x[1]**2),
                      lambda x: ( 2 - 5*x[0] - 3*x[1] + 3*x[0]*x[1] + 3*x[0]**2,
                                  -2*x[1] + 3*x[0]*x[1] + 3*x[1]**2),
                      lambda x: (-1.0 + x[0] + 3*x[1] - 3*x[0]*x[1],
                                 x[1] - 3*x[1]**2),
                      lambda x: (2*x[0] - 3*x[0]*x[1] - 3*x[0]**2,
                                 -2 + 3*x[0]+ 5*x[1] - 3*x[0]*x[1] - 3*x[1]**2),
                      lambda x: (- x[0] + 3*x[0]**2,
                                 + 1 - 3*x[0] - x[1] + 3*x[0]*x[1]),
                      lambda x: (6*x[0] - 3*x[0]*x[1] - 6*x[0]**2,
                                 3*x[1] - 6*x[0]*x[1] - 3*x[1]**2),
                      lambda x: (3*x[0] - 6*x[0]*x[1] - 3*x[0]**2,
                                 6*x[1]- 3*x[0]*x[1] - 6*x[1]**2),
                      ]


        points = [random_point(triangle) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testNED1_2D(self):
        "Test values of NED1."

        element = create(FiniteElement("N1curl", "triangle", 1))
        reference = [ lambda x: (-x[1], x[0]),
                      lambda x: ( x[1], 1 - x[0]),
                      lambda x: ( 1 - x[1], x[0]),
                      ]

        points = [random_point(triangle) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testRT1_3D(self):
        element = create(FiniteElement("RT", "tetrahedron", 1))
        reference = [lambda x: (-x[0], -x[1], -x[2]),
                     lambda x: (-1.0 + x[0], x[1], x[2]),
                     lambda x: (-x[0], 1.0 - x[1], -x[2]),
                     lambda x: ( x[0], x[1], -1.0 + x[2])
                     ]
        points = [random_point(tetrahedron) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testBDM1_3D(self):
        element = create(FiniteElement("BDM", "tetrahedron", 1))
        reference = [ lambda x: (-3*x[0], x[1], x[2]),
                      lambda x: (x[0], -3*x[1], x[2]),
                      lambda x: (x[0], x[1], -3*x[2]),
                      lambda x: (-3.0 + 3*x[0] + 4*x[1] + 4*x[2], -x[1], -x[2]),
                      lambda x: (1.0 - x[0] - 4*x[1], 3*x[1], -x[2]),
                      lambda x: (1.0 - x[0] - 4*x[2], -x[1], 3*x[2]),
                      lambda x: (x[0], 3.0 - 4*x[0] - 3*x[1] - 4*x[2], x[2]),
                      lambda x: (-3*x[0], -1.0 + 4*x[0] + x[1], x[2]),
                      lambda x: (x[0], -1.0 + x[1] + 4*x[2], -3*x[2]),
                      lambda x: (-x[0], -x[1], -3.0 + 4*x[0] + 4*x[1] + 3*x[2]),
                      lambda x: (3*x[0], -x[1], 1.0 - 4*x[0] - x[2]),
                      lambda x: (-x[0], 3*x[1], 1.0 - 4*x[1] - x[2])
                      ]
        points = [random_point(tetrahedron) for i in range(num_points)]
        self._check_function_values(points, element, reference)

    def testNED1_3D(self):
        element = create(FiniteElement("N1curl", "tetrahedron", 1))
        reference = [ lambda x: (0.0, -x[2], x[1]),
                      lambda x: (-x[2], 0.0,  x[0]),
                      lambda x: (-x[1],  x[0], 0.0),
                      lambda x: ( x[2], x[2], 1.0 - x[0] - x[1]),
                      lambda x: (x[1], 1.0 - x[0] - x[2], x[1]),
                      lambda x: (1.0 - x[1] - x[2], x[0], x[0])
                      ]
        points = [random_point(tetrahedron) for i in range(num_points)]
        self._check_function_values(points, element, reference)

class JITTests(unittest.TestCase):

    def testPoisson(self):
        "Test that JIT compiler is fast enough."

        # FIXME: Use local cache: cache_dir argument to instant.build_module
        #options = {"log_level": INFO + 5}
        #options = {"log_level": 5}
        options = {"log_level": WARNING}

        # FIXME: A hack to get the unit test pass on Ubuntu 11.04, see above.
        if use_swig2_binary:
            options["swig_binary"] = "swig2.0"

        # Define two forms with the same signatures
        element = FiniteElement("Lagrange", "triangle", 1)
        v = TestFunction(element)
        u = TrialFunction(element)
        f = Coefficient(element)
        g = Coefficient(element)
        a0 = f*dot(grad(v), grad(u))*dx
        a1 = g*dot(grad(v), grad(u))*dx

        # Strange this needs to be done twice

        # Compile a0 so it will be in the cache (both in-memory and disk)
        jit(a0, options)
        jit(a0, options)

        # Compile a0 again (should be really fast, using in-memory cache)
        t = time()
        jit(a0, options)
        dt0 = time() - t

        print ""

        # Compile a1 (should be fairly fast, using disk cache)
        t = time()
        jit(a1, options)
        dt1 = time() - t

        # Good values
        dt0_good = 0.005
        dt1_good = 0.01

        print ""
        print "JIT in-memory cache:", dt0
        print "JIT disk cache:     ", dt1
        print "Reasonable values are %g and %g" % (dt0_good, dt1_good)

        # Check times
        self.assertTrue(dt0 < 10*dt0_good)
        self.assertTrue(dt1 < 10*dt1_good)

if __name__ == "__main__":
    unittest.main()