| 1 |
"""System integration tests for ufl-derivative-jit-assembly chain.""" |
| 2 |
|
| 3 |
# Copyright (C) 2011 Martin S. Alnaes |
| 4 |
# |
| 5 |
# This file is part of DOLFIN. |
| 6 |
# |
| 7 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# DOLFIN is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
# First added: 2011-20-09 |
| 21 |
# Last changed: 2011-20-09 |
| 22 |
|
| 23 |
import unittest |
| 24 |
import numpy |
| 25 |
import math |
| 26 |
from dolfin import * |
| 27 |
|
| 28 |
|
| 29 |
def test_diff_then_integrate(): |
| 30 |
|
| 31 |
# Define 1D geometry |
| 32 |
n = 21 |
| 33 |
mesh = UnitIntervalMesh(n) |
| 34 |
|
| 35 |
# Shift and scale mesh |
| 36 |
x0, x1 = 1.5, 3.14 |
| 37 |
mesh.coordinates()[:] *= (x1 - x0) |
| 38 |
mesh.coordinates()[:] += x0 |
| 39 |
|
| 40 |
x = SpatialCoordinate(mesh)[0] |
| 41 |
xs = 0.1+0.8*x/x1 # scaled to be within [0.1,0.9] |
| 42 |
|
| 43 |
# Define list of expressions to test, and configure |
| 44 |
# accuracies these expressions are known to pass with. |
| 45 |
# The reason some functions are less accurately integrated is |
| 46 |
# likely that the default choice of quadrature rule is not perfect |
| 47 |
F_list = [] |
| 48 |
def reg(exprs, acc=10): |
| 49 |
for expr in exprs: |
| 50 |
F_list.append((expr, acc)) |
| 51 |
|
| 52 |
# FIXME: 0*dx and 1*dx fails in the ufl-ffc-jit framework somewhere |
| 53 |
#reg([Constant(0.0, cell=cell)]) |
| 54 |
#reg([Constant(1.0, cell=cell)]) |
| 55 |
monomial_list = [x**q for q in range(2, 6)] |
| 56 |
reg(monomial_list) |
| 57 |
reg([2.3*p+4.5*q for p in monomial_list for q in monomial_list]) |
| 58 |
reg([x**x]) |
| 59 |
reg([x**(x**2)], 8) |
| 60 |
reg([x**(x**3)], 6) |
| 61 |
reg([x**(x**4)], 2) |
| 62 |
# Special functions: |
| 63 |
reg([atan(xs)], 8) |
| 64 |
reg([sin(x), cos(x), exp(x)], 5) |
| 65 |
reg([ln(xs), pow(x, 2.7), pow(2.7, x)], 3) |
| 66 |
reg([asin(xs), acos(xs)], 1) |
| 67 |
reg([tan(xs)], 7) |
| 68 |
|
| 69 |
try: |
| 70 |
import scipy |
| 71 |
except: |
| 72 |
scipy = None |
| 73 |
|
| 74 |
if hasattr(math, 'erf') or scipy is not None: |
| 75 |
reg([erf(xs)]) |
| 76 |
else: |
| 77 |
print("Warning: skipping test of erf, old python version and no scipy.") |
| 78 |
|
| 79 |
if 0: |
| 80 |
print("Warning: skipping tests of bessel functions, doesn't build on all platforms.") |
| 81 |
elif scipy is None: |
| 82 |
print("Warning: skipping tests of bessel functions, missing scipy.") |
| 83 |
else: |
| 84 |
for nu in (0,1,2): |
| 85 |
# Many of these are possibly more accurately integrated, |
| 86 |
# but 4 covers all and is sufficient for this test |
| 87 |
reg([bessel_J(nu, xs), bessel_Y(nu, xs), bessel_I(nu, xs), bessel_K(nu, xs)], 4) |
| 88 |
|
| 89 |
# To handle tensor algebra, make an x dependent input tensor |
| 90 |
# xx and square all expressions |
| 91 |
def reg2(exprs, acc=10): |
| 92 |
for expr in exprs: |
| 93 |
F_list.append((inner(expr,expr), acc)) |
| 94 |
xx = as_matrix([[2*x**2, 3*x**3], [11*x**5, 7*x**4]]) |
| 95 |
x3v = as_vector([3*x**2, 5*x**3, 7*x**4]) |
| 96 |
cc = as_matrix([[2, 3], [4, 5]]) |
| 97 |
reg2([xx]) |
| 98 |
reg2([x3v]) |
| 99 |
reg2([cross(3*x3v, as_vector([-x3v[1], x3v[0], x3v[2]]))]) |
| 100 |
reg2([xx.T]) |
| 101 |
reg2([tr(xx)]) |
| 102 |
reg2([det(xx)]) |
| 103 |
reg2([dot(xx, 0.1*xx)]) |
| 104 |
reg2([outer(xx, xx.T)]) |
| 105 |
reg2([dev(xx)]) |
| 106 |
reg2([sym(xx)]) |
| 107 |
reg2([skew(xx)]) |
| 108 |
reg2([elem_mult(7*xx, cc)]) |
| 109 |
reg2([elem_div(7*xx, xx+cc)]) |
| 110 |
reg2([elem_pow(1e-3*xx, 1e-3*cc)]) |
| 111 |
reg2([elem_pow(1e-3*cc, 1e-3*xx)]) |
| 112 |
reg2([elem_op(lambda z: sin(z) + 2, 0.03*xx)], 2) # pretty inaccurate... |
| 113 |
|
| 114 |
# FIXME: Add tests for all UFL operators: |
| 115 |
# These cause discontinuities and may be harder to test in the |
| 116 |
# above fashion: |
| 117 |
#'inv', 'cofac', |
| 118 |
#'eq', 'ne', 'le', 'ge', 'lt', 'gt', 'And', 'Or', 'Not', |
| 119 |
#'conditional', 'sign', |
| 120 |
#'jump', 'avg', |
| 121 |
#'LiftingFunction', 'LiftingOperator', |
| 122 |
|
| 123 |
# FIXME: Test other derivatives: (but algorithms for operator |
| 124 |
# derivatives are the same!): |
| 125 |
#'variable', 'diff', |
| 126 |
#'Dx', 'grad', 'div', 'curl', 'rot', 'Dn', 'exterior_derivative', |
| 127 |
|
| 128 |
# Run through all operators defined above and compare integrals |
| 129 |
debug = 0 |
| 130 |
for F, acc in F_list: |
| 131 |
# Apply UFL differentiation |
| 132 |
f = diff(F, SpatialCoordinate(mesh))[...,0] |
| 133 |
if debug: |
| 134 |
print(F) |
| 135 |
print(x) |
| 136 |
print(f) |
| 137 |
|
| 138 |
# Apply integration with DOLFIN |
| 139 |
# (also passes through form compilation and jit) |
| 140 |
M = f*dx |
| 141 |
f_integral = assemble(M) |
| 142 |
|
| 143 |
# Compute integral of f manually from anti-derivative F |
| 144 |
# (passes through PyDOLFIN interface and uses UFL evaluation) |
| 145 |
F_diff = F((x1,)) - F((x0,)) |
| 146 |
|
| 147 |
# Compare results. Using custom relative delta instead |
| 148 |
# of decimal digits here because some numbers are >> 1. |
| 149 |
delta = min(abs(f_integral), abs(F_diff)) * 10**-acc |
| 150 |
assert f_integral - F_diff <= delta |
| 151 |
|
| 152 |
|
| 153 |
def test_div_grad_then_integrate_over_cells_and_boundary(): |
| 154 |
|
| 155 |
# Define 2D geometry |
| 156 |
n = 10 |
| 157 |
mesh = RectangleMesh(Point(0.0, 0.0), Point(2.0, 3.0), 2*n, 3*n) |
| 158 |
|
| 159 |
x, y = SpatialCoordinate(mesh) |
| 160 |
xs = 0.1+0.8*x/2 # scaled to be within [0.1,0.9] |
| 161 |
ys = 0.1+0.8*y/3 # scaled to be within [0.1,0.9] |
| 162 |
n = FacetNormal(mesh) |
| 163 |
|
| 164 |
# Define list of expressions to test, and configure accuracies |
| 165 |
# these expressions are known to pass with. The reason some |
| 166 |
# functions are less accurately integrated is likely that the |
| 167 |
# default choice of quadrature rule is not perfect |
| 168 |
F_list = [] |
| 169 |
def reg(exprs, acc=10): |
| 170 |
for expr in exprs: |
| 171 |
F_list.append((expr, acc)) |
| 172 |
|
| 173 |
# FIXME: 0*dx and 1*dx fails in the ufl-ffc-jit framework somewhere |
| 174 |
#reg([Constant(0.0, cell=cell)]) |
| 175 |
#reg([Constant(1.0, cell=cell)]) |
| 176 |
monomial_list = [x**q for q in range(2, 6)] |
| 177 |
reg(monomial_list) |
| 178 |
reg([2.3*p+4.5*q for p in monomial_list for q in monomial_list]) |
| 179 |
reg([xs**xs]) |
| 180 |
reg([xs**(xs**2)], 8) # Note: Accuracies here are from 1D case, not checked against 2D results. |
| 181 |
reg([xs**(xs**3)], 6) |
| 182 |
reg([xs**(xs**4)], 2) |
| 183 |
# Special functions: |
| 184 |
reg([atan(xs)], 8) |
| 185 |
reg([sin(x), cos(x), exp(x)], 5) |
| 186 |
reg([ln(xs), pow(x, 2.7), pow(2.7, x)], 3) |
| 187 |
reg([asin(xs), acos(xs)], 1) |
| 188 |
reg([tan(xs)], 7) |
| 189 |
|
| 190 |
# To handle tensor algebra, make an x dependent input tensor |
| 191 |
# xx and square all expressions |
| 192 |
def reg2(exprs, acc=10): |
| 193 |
for expr in exprs: |
| 194 |
F_list.append((inner(expr,expr), acc)) |
| 195 |
xx = as_matrix([[2*x**2, 3*x**3], [11*x**5, 7*x**4]]) |
| 196 |
xxs = as_matrix([[2*xs**2, 3*xs**3], [11*xs**5, 7*xs**4]]) |
| 197 |
x3v = as_vector([3*x**2, 5*x**3, 7*x**4]) |
| 198 |
cc = as_matrix([[2, 3], [4, 5]]) |
| 199 |
reg2([xx]) # TODO: Make unit test for UFL from this, results in listtensor with free indices |
| 200 |
reg2([x3v]) |
| 201 |
reg2([cross(3*x3v, as_vector([-x3v[1], x3v[0], x3v[2]]))]) |
| 202 |
reg2([xx.T]) |
| 203 |
reg2([tr(xx)]) |
| 204 |
reg2([det(xx)]) |
| 205 |
reg2([dot(xx,0.1*xx)]) |
| 206 |
reg2([outer(xx,xx.T)]) |
| 207 |
reg2([dev(xx)]) |
| 208 |
reg2([sym(xx)]) |
| 209 |
reg2([skew(xx)]) |
| 210 |
reg2([elem_mult(7*xx, cc)]) |
| 211 |
reg2([elem_div(7*xx, xx+cc)]) |
| 212 |
reg2([elem_pow(1e-3*xxs, 1e-3*cc)]) |
| 213 |
reg2([elem_pow(1e-3*cc, 1e-3*xx)]) |
| 214 |
reg2([elem_op(lambda z: sin(z)+2, 0.03*xx)], 2) # pretty inaccurate... |
| 215 |
|
| 216 |
# FIXME: Add tests for all UFL operators: |
| 217 |
# These cause discontinuities and may be harder to test in the |
| 218 |
# above fashion: |
| 219 |
#'inv', 'cofac', |
| 220 |
#'eq', 'ne', 'le', 'ge', 'lt', 'gt', 'And', 'Or', 'Not', |
| 221 |
#'conditional', 'sign', |
| 222 |
#'jump', 'avg', |
| 223 |
#'LiftingFunction', 'LiftingOperator', |
| 224 |
|
| 225 |
# FIXME: Test other derivatives: (but algorithms for operator |
| 226 |
# derivatives are the same!): |
| 227 |
#'variable', 'diff', |
| 228 |
#'Dx', 'grad', 'div', 'curl', 'rot', 'Dn', 'exterior_derivative', |
| 229 |
|
| 230 |
# Run through all operators defined above and compare integrals |
| 231 |
debug = 0 |
| 232 |
if debug: |
| 233 |
k = 2 |
| 234 |
F_list = F_list[1:] |
| 235 |
|
| 236 |
for F,acc in F_list: |
| 237 |
if debug: print('\n', "F:", str(F)) |
| 238 |
|
| 239 |
# Integrate over domain and its boundary |
| 240 |
int_dx = assemble(div(grad(F))*dx(mesh)) |
| 241 |
int_ds = assemble(dot(grad(F), n)*ds(mesh)) |
| 242 |
|
| 243 |
if debug: print(int_dx, int_ds) |
| 244 |
|
| 245 |
# Compare results. Using custom relative delta instead of |
| 246 |
# decimal digits here because some numbers are >> 1. |
| 247 |
delta = min(abs(int_dx), abs(int_ds)) * 10**-acc |
| 248 |
assert int_dx - int_ds <= delta |