| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"Unit tests for FFC" |
| 3 |
|
| 4 |
# Copyright (C) 2007-2017 Anders Logg and Garth N. Wells |
| 5 |
# |
| 6 |
# This file is part of FFC. |
| 7 |
# |
| 8 |
# FFC is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU Lesser General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# FFC is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public License |
| 19 |
# along with FFC. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
# Modified by Marie E. Rognes, 2010 |
| 22 |
# Modified by Lizao Li, 2016 |
| 23 |
|
| 24 |
|
| 25 |
import pytest |
| 26 |
import os |
| 27 |
import sys |
| 28 |
import numpy |
| 29 |
import math |
| 30 |
from time import time |
| 31 |
|
| 32 |
from ufl_legacy import * |
| 33 |
from ffc.fiatinterface import create_element |
| 34 |
from ffc import jit |
| 35 |
|
| 36 |
|
| 37 |
def element_coords(cell): |
| 38 |
if cell == "interval": |
| 39 |
return [(0,), (1,)] |
| 40 |
elif cell == "triangle": |
| 41 |
return [(0, 0), (1, 0), (0, 1)] |
| 42 |
elif cell == "tetrahedron": |
| 43 |
return [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)] |
| 44 |
elif cell == "quadrilateral": |
| 45 |
return [(0, 0), (1, 0), (0, 1), (1, 1)] |
| 46 |
elif cell == "hexahedron": |
| 47 |
return [(0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0), (0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1)] |
| 48 |
else: |
| 49 |
RuntimeError("Unknown cell type") |
| 50 |
|
| 51 |
|
| 52 |
def random_point(shape): |
| 53 |
w = numpy.random.random(len(shape)) |
| 54 |
return sum([numpy.array(shape[i])*w[i] for i in range(len(shape))])/sum(w) |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.parametrize("degree, expected_dim", [(1, 3), (2, 6), (3, 10)]) |
| 58 |
def test_continuous_lagrange(degree, expected_dim): |
| 59 |
"Test space dimensions of continuous Lagrange elements." |
| 60 |
P = create_element(FiniteElement("Lagrange", "triangle", degree)) |
| 61 |
assert P.space_dimension() == expected_dim |
| 62 |
|
| 63 |
@pytest.mark.parametrize("degree, expected_dim", [(1, 4), (2, 9), (3, 16)]) |
| 64 |
def test_continuous_lagrange_quadrilateral(degree, expected_dim): |
| 65 |
"Test space dimensions of continuous TensorProduct elements (quadrilateral)." |
| 66 |
P = create_element(FiniteElement("Lagrange", "quadrilateral", degree)) |
| 67 |
assert P.space_dimension() == expected_dim |
| 68 |
|
| 69 |
@pytest.mark.parametrize("degree, expected_dim", [(0, 1), (1, 3), (2, 6), (3, 10)]) |
| 70 |
def test_discontinuous_lagrange(degree, expected_dim): |
| 71 |
"Test space dimensions of discontinuous Lagrange elements." |
| 72 |
P = create_element(FiniteElement("DG", "triangle", degree)) |
| 73 |
assert P.space_dimension() == expected_dim |
| 74 |
|
| 75 |
@pytest.mark.parametrize("degree, expected_dim", |
| 76 |
[(0, 3), (1, 9), (2, 18), (3, 30)]) |
| 77 |
def test_regge(degree, expected_dim): |
| 78 |
"Test space dimensions of generalized Regge element." |
| 79 |
P = create_element(FiniteElement("Regge", "triangle", degree)) |
| 80 |
assert P.space_dimension() == expected_dim |
| 81 |
|
| 82 |
@pytest.mark.parametrize("degree, expected_dim", |
| 83 |
[(0, 3), (1, 9), (2, 18), (3, 30)]) |
| 84 |
def test_hhj(degree, expected_dim): |
| 85 |
"Test space dimensions of Hellan-Herrmann-Johnson element." |
| 86 |
P = create_element(FiniteElement("HHJ", "triangle", degree)) |
| 87 |
assert P.space_dimension() == expected_dim |
| 88 |
|
| 89 |
|
| 90 |
class TestFunctionValues(): |
| 91 |
"""These tests examine tabulate gives the correct answers for a the |
| 92 |
supported (non-mixed) for low degrees""" |
| 93 |
|
| 94 |
# FIXME: Add tests for NED and BDM/RT in 3D. |
| 95 |
|
| 96 |
# Shape (basis) functions on reference element |
| 97 |
reference_interval_1 = [lambda x: 1 - x[0], lambda x: x[0]] |
| 98 |
reference_triangle_1 = [lambda x: 1 - x[0] - x[1], lambda x: x[0], lambda x: x[1]] |
| 99 |
reference_tetrahedron_1 = [lambda x: 1 - x[0] - x[1] - x[2], lambda x: x[0], |
| 100 |
lambda x: x[1], lambda x: x[2]] |
| 101 |
reference_triangle_bdm1 = [lambda x: (2*x[0], -x[1]), |
| 102 |
lambda x: (-x[0], 2*x[1]), |
| 103 |
lambda x: (2 - 2*x[0] - 3*x[1], x[1]), |
| 104 |
lambda x: (- 1 + x[0] + 3*x[1], - 2*x[1]), |
| 105 |
lambda x: (-x[0], -2 + 3*x[0] + 2*x[1]), |
| 106 |
lambda x: (2*x[0], 1 - 3*x[0] - x[1])] |
| 107 |
reference_triangle_rt1 = [lambda x: (x[0], x[1]), lambda x: (1 - x[0], -x[1]), |
| 108 |
lambda x: (x[0], x[1] - 1)] |
| 109 |
reference_triangle_rt2 = [lambda x: (-x[0] + 3*x[0]**2, -x[1] + 3*x[0]*x[1]), |
| 110 |
lambda x: (-x[0] + 3*x[0]*x[1], -x[1] + 3*x[1]**2), |
| 111 |
lambda x: ( 2 - 5*x[0] - 3*x[1] + 3*x[0]*x[1] + 3*x[0]**2, |
| 112 |
-2*x[1] + 3*x[0]*x[1] + 3*x[1]**2), |
| 113 |
lambda x: (-1.0 + x[0] + 3*x[1] - 3*x[0]*x[1], x[1] - 3*x[1]**2), |
| 114 |
lambda x: (2*x[0] - 3*x[0]*x[1] - 3*x[0]**2, |
| 115 |
-2 + 3*x[0]+ 5*x[1] - 3*x[0]*x[1] - 3*x[1]**2), |
| 116 |
lambda x: (- x[0] + 3*x[0]**2, |
| 117 |
+ 1 - 3*x[0] - x[1] + 3*x[0]*x[1]), |
| 118 |
lambda x: (6*x[0] - 3*x[0]*x[1] - 6*x[0]**2, |
| 119 |
3*x[1] - 6*x[0]*x[1] - 3*x[1]**2), |
| 120 |
lambda x: (3*x[0] - 6*x[0]*x[1] - 3*x[0]**2, |
| 121 |
6*x[1]- 3*x[0]*x[1] - 6*x[1]**2)] |
| 122 |
reference_triangle_ned1 = [lambda x: (-x[1], x[0]), lambda x: ( x[1], 1 - x[0]), |
| 123 |
lambda x: ( 1 - x[1], x[0])] |
| 124 |
reference_tetrahedron_rt1 = [lambda x: (-x[0], -x[1], -x[2]), |
| 125 |
lambda x: (-1.0 + x[0], x[1], x[2]), |
| 126 |
lambda x: (-x[0], 1.0 - x[1], -x[2]), |
| 127 |
lambda x: ( x[0], x[1], -1.0 + x[2])] |
| 128 |
reference_tetrahedron_bdm1 = [lambda x: (-3*x[0], x[1], x[2]), |
| 129 |
lambda x: (x[0], -3*x[1], x[2]), |
| 130 |
lambda x: (x[0], x[1], -3*x[2]), |
| 131 |
lambda x: (-3.0 + 3*x[0] + 4*x[1] + 4*x[2], -x[1], -x[2]), |
| 132 |
lambda x: (1.0 - x[0] - 4*x[1], 3*x[1], -x[2]), |
| 133 |
lambda x: (1.0 - x[0] - 4*x[2], -x[1], 3*x[2]), |
| 134 |
lambda x: (x[0], 3.0 - 4*x[0] - 3*x[1] - 4*x[2], x[2]), |
| 135 |
lambda x: (-3*x[0], -1.0 + 4*x[0] + x[1], x[2]), |
| 136 |
lambda x: (x[0], -1.0 + x[1] + 4*x[2], -3*x[2]), |
| 137 |
lambda x: (-x[0], -x[1], -3.0 + 4*x[0] + 4*x[1] + 3*x[2]), |
| 138 |
lambda x: (3*x[0], -x[1], 1.0 - 4*x[0] - x[2]), |
| 139 |
lambda x: (-x[0], 3*x[1], 1.0 - 4*x[1] - x[2])] |
| 140 |
reference_tetrahedron_ned1 = [lambda x: (0.0, -x[2], x[1]), |
| 141 |
lambda x: (-x[2], 0.0, x[0]), |
| 142 |
lambda x: (-x[1], x[0], 0.0), |
| 143 |
lambda x: ( x[2], x[2], 1.0 - x[0] - x[1]), |
| 144 |
lambda x: (x[1], 1.0 - x[0] - x[2], x[1]), |
| 145 |
lambda x: (1.0 - x[1] - x[2], x[0], x[0])] |
| 146 |
reference_quadrilateral_1 = [lambda x: (1-x[0])*(1-x[1]), |
| 147 |
lambda x: (1-x[0])*x[1], |
| 148 |
lambda x: x[0]*(1-x[1]), |
| 149 |
lambda x: x[0]*x[1]] |
| 150 |
reference_hexahedron_1 = [lambda x: (1-x[0])*(1-x[1])*(1-x[2]), |
| 151 |
lambda x: (1-x[0])*(1-x[1])*x[2], |
| 152 |
lambda x: (1-x[0])*x[1]*(1-x[2]), |
| 153 |
lambda x: (1-x[0])*x[1]*x[2], |
| 154 |
lambda x: x[0]*(1-x[1])*(1-x[2]), |
| 155 |
lambda x: x[0]*(1-x[1])*x[2], |
| 156 |
lambda x: x[0]*x[1]*(1-x[2]), |
| 157 |
lambda x: x[0]*x[1]*x[2]] |
| 158 |
|
| 159 |
# Tests to perform |
| 160 |
tests = [("Lagrange", "interval", 1, reference_interval_1), |
| 161 |
("Lagrange", "triangle", 1, reference_triangle_1), |
| 162 |
("Lagrange", "tetrahedron", 1, reference_tetrahedron_1), |
| 163 |
("Lagrange", "quadrilateral", 1, reference_quadrilateral_1), |
| 164 |
("Lagrange", "hexahedron", 1, reference_hexahedron_1), |
| 165 |
("Discontinuous Lagrange", "interval", 1, reference_interval_1), |
| 166 |
("Discontinuous Lagrange", "triangle", 1, reference_triangle_1), |
| 167 |
("Discontinuous Lagrange", "tetrahedron", 1, reference_tetrahedron_1), |
| 168 |
("Brezzi-Douglas-Marini", "triangle", 1, reference_triangle_bdm1), |
| 169 |
("Raviart-Thomas", "triangle", 1, reference_triangle_rt1), |
| 170 |
("Raviart-Thomas", "triangle", 2, reference_triangle_rt2), |
| 171 |
("Discontinuous Raviart-Thomas", "triangle", 1, reference_triangle_rt1), |
| 172 |
("Discontinuous Raviart-Thomas", "triangle", 2, reference_triangle_rt2), |
| 173 |
("N1curl", "triangle", 1, reference_triangle_ned1), |
| 174 |
("Raviart-Thomas", "tetrahedron", 1, reference_tetrahedron_rt1), |
| 175 |
("Discontinuous Raviart-Thomas", "tetrahedron", 1, reference_tetrahedron_rt1), |
| 176 |
("Brezzi-Douglas-Marini", "tetrahedron", 1, reference_tetrahedron_bdm1), |
| 177 |
("N1curl", "tetrahedron", 1, reference_tetrahedron_ned1), |
| 178 |
] |
| 179 |
|
| 180 |
|
| 181 |
@pytest.mark.parametrize("family, cell, degree, reference", tests) |
| 182 |
def test_values(self, family, cell, degree, reference): |
| 183 |
# Create element |
| 184 |
element = create_element(FiniteElement(family, cell, degree)) |
| 185 |
|
| 186 |
# Get some points and check basis function values at points |
| 187 |
points = [random_point(element_coords(cell)) for i in range(5)] |
| 188 |
for x in points: |
| 189 |
table = element.tabulate(0, (x,)) |
| 190 |
basis = table[list(table.keys())[0]] |
| 191 |
for i in range(len(basis)): |
| 192 |
if not element.value_shape(): |
| •193 |
assert round(float(basis[i, 0]) - reference[i](x), 10) == 0.0 |
| 194 |
else: |
| 195 |
for k in range(element.value_shape()[0]): |
| 196 |
assert round(basis[i][k][0] - reference[i](x)[k] , 10) == 0.0 |
| 197 |
|
| 198 |
|
| 199 |
class Test_JIT(): |
| 200 |
|
| 201 |
def test_poisson(self): |
| 202 |
"Test that JIT compiler is fast enough." |
| 203 |
|
| 204 |
# FIXME: Use local cache: cache_dir argument to instant.build_module |
| 205 |
#options = {"log_level": INFO + 5} |
| 206 |
#options = {"log_level": 5} |
| 207 |
options = {"log_level": WARNING} |
| 208 |
|
| 209 |
# Define two forms with the same signatures |
| 210 |
element = FiniteElement("Lagrange", "triangle", 1) |
| 211 |
v = TestFunction(element) |
| 212 |
u = TrialFunction(element) |
| 213 |
f = Coefficient(element) |
| 214 |
g = Coefficient(element) |
| 215 |
a0 = f*dot(grad(v), grad(u))*dx |
| 216 |
a1 = g*dot(grad(v), grad(u))*dx |
| 217 |
|
| 218 |
# Strange this needs to be done twice |
| 219 |
|
| 220 |
# Compile a0 so it will be in the cache (both in-memory and disk) |
| 221 |
jit(a0, options) |
| 222 |
jit(a0, options) |
| 223 |
|
| 224 |
# Compile a0 again (should be really fast, using in-memory cache) |
| 225 |
t = time() |
| 226 |
jit(a0, options) |
| 227 |
dt0 = time() - t |
| 228 |
|
| 229 |
# Compile a1 (should be fairly fast, using disk cache) |
| 230 |
t = time() |
| 231 |
jit(a1, options) |
| 232 |
dt1 = time() - t |
| 233 |
|
| 234 |
# Good values |
| 235 |
dt0_good = 0.005 |
| 236 |
dt1_good = 0.01 |
| 237 |
|
| 238 |
print("\nJIT in-memory cache:", dt0) |
| 239 |
print("JIT disk cache: ", dt1) |
| 240 |
print("Reasonable values are %g and %g" % (dt0_good, dt1_good)) |
| 241 |
|
| 242 |
# Check times |
| 243 |
assert dt0 < 10*dt0_good |
| 244 |
assert dt1 < 10*dt1_good |