| 1 |
"""Unit tests for the Function class""" |
| 2 |
|
| 3 |
# Copyright (C) 2011-2014 Garth N. Wells |
| 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 |
import pytest |
| 21 |
from dolfin import * |
| 22 |
import ufl_legacy as ufl |
| 23 |
|
| 24 |
from dolfin_utils.test import skip_in_parallel, pushpop_parameters, fixture |
| 25 |
|
| 26 |
@fixture |
| 27 |
def mesh(): |
| 28 |
return UnitCubeMesh(8, 8, 8) |
| 29 |
|
| 30 |
|
| 31 |
@fixture |
| 32 |
def R(mesh): |
| 33 |
return FunctionSpace(mesh, 'R', 0) |
| 34 |
|
| 35 |
|
| 36 |
@fixture |
| 37 |
def V(mesh): |
| 38 |
return FunctionSpace(mesh, 'CG', 1) |
| 39 |
|
| 40 |
|
| 41 |
@fixture |
| 42 |
def W(mesh): |
| 43 |
return VectorFunctionSpace(mesh, 'CG', 1) |
| 44 |
|
| 45 |
|
| 46 |
def test_name_argument(W): |
| 47 |
u = Function(W) |
| 48 |
v = Function(W, name="v") |
| 49 |
assert u.name() == "f_%d" % u.count() |
| 50 |
assert v.name() == "v" |
| 51 |
assert str(v) == "v" |
| 52 |
|
| 53 |
|
| 54 |
def test_in_function_space(W): |
| 55 |
u = Function(W) |
| 56 |
v = Function(W) |
| 57 |
assert u in W |
| 58 |
assert u in u.function_space() |
| 59 |
assert u in v.function_space() |
| 60 |
for i, usub in enumerate(u.split()): |
| 61 |
assert usub in W.sub(i) |
| 62 |
|
| 63 |
|
| 64 |
def test_compute_vertex_values(V, W, mesh): |
| 65 |
from numpy import zeros, all, array |
| 66 |
u = Function(V) |
| 67 |
v = Function(W) |
| 68 |
|
| 69 |
u.vector()[:] = 1. |
| 70 |
v.vector()[:] = 1. |
| 71 |
|
| 72 |
u_values = u.compute_vertex_values(mesh) |
| 73 |
v_values = v.compute_vertex_values(mesh) |
| 74 |
|
| 75 |
assert all(u_values == 1) |
| 76 |
|
| 77 |
u_values2 = u.compute_vertex_values() |
| 78 |
|
| 79 |
assert all(u_values == u_values2) |
| 80 |
|
| 81 |
|
| 82 |
def test_assign(V, W): |
| 83 |
from ufl_legacy.algorithms import replace |
| 84 |
|
| 85 |
for V0, V1, vector_space in [(V, W, False), (W, V, True)]: |
| 86 |
u = Function(V0) |
| 87 |
u0 = Function(V0) |
| 88 |
u1 = Function(V0) |
| 89 |
u2 = Function(V0) |
| 90 |
u3 = Function(V1) |
| 91 |
|
| 92 |
u.vector()[:] = 1.0 |
| 93 |
u0.vector()[:] = 2.0 |
| 94 |
u1.vector()[:] = 3.0 |
| 95 |
u2.vector()[:] = 4.0 |
| 96 |
u3.vector()[:] = 5.0 |
| 97 |
|
| 98 |
scalars = {u: 1.0, u0: 2.0, u1: 3.0, u2: 4.0, u3: 5.0} |
| 99 |
|
| 100 |
uu = Function(V0) |
| 101 |
uu.assign(2*u) |
| 102 |
assert uu.vector().sum() == u0.vector().sum() |
| 103 |
|
| 104 |
uu = Function(V1) |
| 105 |
uu.assign(3*u) |
| 106 |
assert uu.vector().sum() == u1.vector().sum() |
| 107 |
|
| 108 |
# Test complex assignment |
| 109 |
expr = 3*u - 4*u1 - 0.1*4*u*4 + u2 + 3*u0/3./0.5 |
| 110 |
expr_scalar = 3 - 4*3 - 0.1*4*4+4. + 3*2./3./0.5 |
| 111 |
uu.assign(expr) |
| 112 |
assert (round(uu.vector().sum() - |
| 113 |
float(expr_scalar*uu.vector().size()), 7) == 0) |
| 114 |
|
| 115 |
# Test expression scaling |
| 116 |
expr = 3*expr |
| 117 |
expr_scalar *= 3 |
| 118 |
uu.assign(expr) |
| 119 |
assert (round(uu.vector().sum() - |
| 120 |
float(expr_scalar*uu.vector().size()), 7) == 0) |
| 121 |
|
| 122 |
# Test expression scaling |
| 123 |
expr = expr/4.5 |
| 124 |
expr_scalar /= 4.5 |
| 125 |
uu.assign(expr) |
| 126 |
assert (round(uu.vector().sum() - |
| 127 |
float(expr_scalar*uu.vector().size()), 7) == 0) |
| 128 |
|
| 129 |
# Test self assignment |
| 130 |
expr = 3*u - Constant(5)*u2 + u1 - 5*u |
| 131 |
expr_scalar = 3 - 5*4. + 3. - 5 |
| 132 |
u.assign(expr) |
| 133 |
assert (round(u.vector().sum() - |
| 134 |
float(expr_scalar*u.vector().size()), 7) == 0) |
| 135 |
|
| 136 |
# Test zero assignment |
| 137 |
u.assign(-u2/2 + 2*u1 - u1/0.5 + u2*0.5) |
| 138 |
assert round(u.vector().sum() - 0.0, 7) == 0 |
| 139 |
|
| 140 |
# Test erroneous assignments |
| 141 |
uu = Function(V1) |
| 142 |
f = Expression("1.0", degree=0) |
| 143 |
with pytest.raises(RuntimeError): |
| 144 |
uu.assign(1.0) |
| 145 |
with pytest.raises(RuntimeError): |
| 146 |
uu.assign(4*f) |
| 147 |
|
| 148 |
if not vector_space: |
| 149 |
with pytest.raises(RuntimeError): |
| 150 |
uu.assign(u*u0) |
| 151 |
with pytest.raises(RuntimeError): |
| 152 |
uu.assign(4/u0) |
| 153 |
with pytest.raises(RuntimeError): |
| 154 |
uu.assign(4*u*u1) |
| 155 |
|
| 156 |
|
| 157 |
def test_axpy(V, W): |
| 158 |
for V0, V1, vector_space in [(V, W, False), (W, V, True)]: |
| 159 |
u = Function(V0) |
| 160 |
u0 = Function(V0) |
| 161 |
u1 = Function(V0) |
| 162 |
u2 = Function(V0) |
| 163 |
u3 = Function(V1) |
| 164 |
|
| 165 |
u.vector()[:] = 1.0 |
| 166 |
u0.vector()[:] = 2.0 |
| 167 |
u1.vector()[:] = 3.0 |
| 168 |
u2.vector()[:] = 4.0 |
| 169 |
u3.vector()[:] = 5.0 |
| 170 |
|
| 171 |
axpy = FunctionAXPY(u1, 2.0) |
| 172 |
u.assign(axpy) |
| 173 |
expr_scalar = 3*2 |
| 174 |
|
| 175 |
assert (round(u.vector().sum() - |
| 176 |
float(expr_scalar*u.vector().size()), 7) == 0) |
| 177 |
|
| 178 |
axpy = FunctionAXPY([(2.0, u1), (3.0, u2)]) |
| 179 |
|
| 180 |
u.assign(axpy) |
| 181 |
expr_scalar = 3*2+3*4.0 |
| 182 |
|
| 183 |
assert (round(u.vector().sum() - |
| 184 |
float(expr_scalar*u.vector().size()), 7) == 0) |
| 185 |
|
| 186 |
axpy = axpy*3.0 |
| 187 |
u.assign(axpy) |
| 188 |
expr_scalar *= 3.0 |
| 189 |
|
| 190 |
assert (round(u.vector().sum() - |
| 191 |
float(expr_scalar*u.vector().size()), 7) == 0) |
| 192 |
|
| 193 |
axpy0 = axpy/5.0 |
| 194 |
u.assign(axpy0) |
| 195 |
expr_scalar0 = expr_scalar/5.0 |
| 196 |
|
| 197 |
assert (round(u.vector().sum() - |
| 198 |
float(expr_scalar0*u.vector().size()), 7) == 0) |
| 199 |
|
| 200 |
axpy1 = axpy0+axpy |
| 201 |
u.assign(axpy1) |
| 202 |
expr_scalar1 = expr_scalar0 + expr_scalar |
| 203 |
|
| 204 |
assert (round(u.vector().sum() - |
| 205 |
float(expr_scalar1*u.vector().size()), 7) == 0) |
| 206 |
|
| 207 |
axpy1 = axpy0-axpy |
| 208 |
u.assign(axpy1) |
| 209 |
expr_scalar1 = expr_scalar0 - expr_scalar |
| 210 |
|
| 211 |
assert (round(u.vector().sum() - |
| 212 |
float(expr_scalar1*u.vector().size()), 7) == 0) |
| 213 |
|
| 214 |
axpy1 = axpy0+u1 |
| 215 |
u.assign(axpy1) |
| 216 |
expr_scalar1 = expr_scalar0 + 3.0 |
| 217 |
|
| 218 |
assert (round(u.vector().sum() - |
| 219 |
float(expr_scalar1*u.vector().size()), 7) == 0) |
| 220 |
|
| 221 |
axpy1 = axpy0 - u2 |
| 222 |
u.assign(axpy1) |
| 223 |
expr_scalar1 = expr_scalar0 - 4.0 |
| 224 |
|
| 225 |
assert (round(u.vector().sum() - |
| 226 |
float(expr_scalar1*u.vector().size()), 7) == 0) |
| 227 |
|
| 228 |
with pytest.raises((RuntimeError, TypeError)): |
| 229 |
FunctionAXPY(u, u3, 0) |
| 230 |
|
| 231 |
axpy = FunctionAXPY(u3, 2.0) |
| 232 |
|
| 233 |
with pytest.raises(RuntimeError): |
| 234 |
axpy + u |
| 235 |
|
| 236 |
def test_call(R, V, W, mesh): |
| 237 |
from numpy import zeros, all, array |
| 238 |
u0 = Function(R) |
| 239 |
u1 = Function(V) |
| 240 |
u2 = Function(W) |
| 241 |
e0 = Expression("x[0] + x[1] + x[2]", degree=1) |
| 242 |
e1 = Expression(("x[0] + x[1] + x[2]", |
| 243 |
"x[0] - x[1] - x[2]", |
| 244 |
"x[0] + x[1] + x[2]"), degree=1) |
| 245 |
|
| 246 |
u0.vector()[:] = 1.0 |
| 247 |
u1.interpolate(e0) |
| 248 |
u2.interpolate(e1) |
| 249 |
|
| 250 |
p0 = (Vertex(mesh, 0).point() + Vertex(mesh, 1).point())/2.0 |
| 251 |
x0 = (mesh.coordinates()[0] + mesh.coordinates()[1])/2.0 |
| 252 |
x1 = tuple(x0) |
| 253 |
|
| 254 |
assert round(u0(*x1) - u0(x0), 7) == 0 |
| 255 |
assert round(u0(x1) - u0(p0), 7) == 0 |
| 256 |
assert round(u1(x1) - u1(x0), 7) == 0 |
| 257 |
assert round(u1(*x1) - u1(p0), 7) == 0 |
| 258 |
assert round(u2(x1)[0] - u1(p0), 7) == 0 |
| 259 |
|
| 260 |
assert all(u2(*x1) == u2(x0)) |
| 261 |
assert all(u2(*x1) == u2(p0)) |
| 262 |
|
| 263 |
values = zeros(mesh.geometry().dim(), dtype='d') |
| 264 |
u2(p0, values=values) |
| 265 |
assert all(values == u2(x0)) |
| 266 |
|
| 267 |
with pytest.raises(TypeError): |
| 268 |
u0([0, 0, 0, 0]) |
| 269 |
with pytest.raises(TypeError): |
| 270 |
u0([0, 0]) |
| 271 |
|
| 272 |
def test_constant_float_conversion(): |
| 273 |
c = Constant(3.45) |
| 274 |
assert float(c) == 3.45 |
| 275 |
|
| 276 |
|
| 277 |
def test_real_function_float_conversion1(R): |
| 278 |
c = Function(R) |
| 279 |
assert float(c) == 0.0 |
| 280 |
|
| 281 |
|
| 282 |
def test_real_function_float_conversion2(R): |
| 283 |
c = Function(R) |
| 284 |
c.assign(Constant(2.34)) |
| 285 |
assert float(c) == 2.34 |
| 286 |
|
| 287 |
|
| 288 |
def test_real_function_float_conversion3(R): |
| 289 |
c = Function(R) |
| 290 |
c.vector()[:] = 1.23 |
| 291 |
assert float(c) == 1.23 |
| 292 |
|
| 293 |
|
| 294 |
def test_scalar_conditions(R): |
| 295 |
c = Function(R) |
| 296 |
c.vector()[:] = 1.5 |
| 297 |
|
| 298 |
# Float conversion does not interfere with boolean ufl expressions |
| 299 |
assert isinstance(lt(c, 3), ufl.classes.LT) |
| 300 |
assert not isinstance(lt(c, 3), bool) |
| 301 |
|
| 302 |
# Float conversion is not implicit in boolean Python expressions |
| 303 |
assert isinstance(c < 3, ufl.classes.LT) |
| 304 |
assert not isinstance(c < 3, bool) |
| 305 |
|
| 306 |
# == is used in ufl to compare equivalent representations, |
| 307 |
# <,> result in LT/GT expressions, bool conversion is illegal |
| 308 |
|
| 309 |
# Note that 1.5 < 0 == False == 1.5 < 1, but that is not what we |
| 310 |
# compare here: |
| 311 |
assert not (c < 0) == (c < 1) |
| 312 |
# This protects from "if c < 0: ..." misuse: |
| 313 |
with pytest.raises(ufl.UFLException): |
| 314 |
bool(c < 0) |
| 315 |
with pytest.raises(ufl.UFLException): |
| 316 |
not c < 0 |
| 317 |
|
| 318 |
|
| 319 |
def test_interpolation_mismatch_rank0(W): |
| 320 |
f = Expression("1.0", degree=0) |
| 321 |
with pytest.raises(RuntimeError): |
| 322 |
interpolate(f, W) |
| 323 |
|
| 324 |
|
| 325 |
def test_interpolation_mismatch_rank1(W): |
| 326 |
f = Expression(("1.0", "1.0"), degree=0) |
| 327 |
with pytest.raises(RuntimeError): |
| 328 |
interpolate(f, W) |
| 329 |
|
| 330 |
|
| 331 |
def test_interpolation_jit_rank0(V): |
| 332 |
f = Expression("1.0", degree=0) |
| 333 |
w = interpolate(f, V) |
| 334 |
x = w.vector() |
| 335 |
assert x.max() == 1 |
| 336 |
assert x.min() == 1 |
| 337 |
|
| 338 |
|
| 339 |
@skip_in_parallel |
| 340 |
def test_extrapolation(V, pushpop_parameters): |
| 341 |
original_parameters = parameters["allow_extrapolation"] |
| 342 |
|
| 343 |
f0 = Function(V) |
| 344 |
with pytest.raises(RuntimeError): |
| 345 |
f0.__call__((0., 0, -1)) |
| 346 |
|
| 347 |
mesh1 = UnitSquareMesh(3, 3) |
| 348 |
V1 = FunctionSpace(mesh1, "CG", 1) |
| 349 |
|
| 350 |
mesh2 = UnitTriangleMesh.create() |
| 351 |
V2 = FunctionSpace(mesh2, "CG", 1) |
| 352 |
|
| 353 |
parameters["allow_extrapolation"] = True |
| 354 |
f1 = Function(V1) |
| 355 |
f1.vector()[:] = 1.0 |
| 356 |
assert round(f1(0., -1) - 1.0, 7) == 0 |
| 357 |
|
| 358 |
parameters["allow_extrapolation"] = False |
| 359 |
f2 = Function(V2) |
| 360 |
with pytest.raises(RuntimeError): |
| 361 |
f2.__call__((0., -1.)) |
| 362 |
|
| 363 |
parameters["allow_extrapolation"] = True |
| 364 |
f3 = Function(V2) |
| 365 |
f3.vector()[:] = 1.0 |
| 366 |
assert round(f3(0., -1) - 1.0, 7) == 0 |
| 367 |
|
| 368 |
parameters["allow_extrapolation"] = original_parameters |
| 369 |
|
| 370 |
f1 = Function(V1) |
| 371 |
f1.set_allow_extrapolation(True) |
| 372 |
f1.vector()[:] = 1.0 |
| 373 |
assert round(f1(0., -1) - 1.0, 7) == 0 |
| 374 |
|
| 375 |
f2 = Function(V2) |
| 376 |
|
| 377 |
f2.set_allow_extrapolation(False) |
| 378 |
with pytest.raises(RuntimeError): |
| 379 |
f2.__call__((0., -1.)) |
| 380 |
|
| 381 |
f2.set_allow_extrapolation(True) |
| 382 |
f2.vector()[:] = 1.0 |
| 383 |
assert round(f2(0., -1) - 1.0, 7) == 0 |
| 384 |
|
| 385 |
f2.set_allow_extrapolation(True) |
| 386 |
assert f2.get_allow_extrapolation() is True |
| 387 |
f2.set_allow_extrapolation(False) |
| 388 |
assert f2.get_allow_extrapolation() is False |
| 389 |
|
| 390 |
|
| 391 |
@skip_in_parallel |
| 392 |
def test_near_evaluations(R, mesh): |
| 393 |
# Test that we allow point evaluation that are slightly outside |
| 394 |
parameters["allow_extrapolation"] = False |
| 395 |
|
| 396 |
u0 = Function(R) |
| 397 |
u0.vector()[:] = 1.0 |
| 398 |
a = Vertex(mesh, 0).point() |
| 399 |
offset = 0.99*DOLFIN_EPS |
| 400 |
|
| 401 |
a_shift_x = Point(a[0] - offset, a[1], a[2]) |
| 402 |
assert round(u0(a) - u0(a_shift_x), 7) == 0 |
| 403 |
|
| 404 |
a_shift_xyz = Point(a[0] - offset / sqrt(3), |
| 405 |
a[1] - offset / sqrt(3), |
| 406 |
a[2] - offset / sqrt(3)) |
| 407 |
assert round(u0(a) - u0(a_shift_xyz), 7) == 0 |
| 408 |
|
| 409 |
|
| 410 |
def test_interpolation_jit_rank1(W): |
| 411 |
f = Expression(("1.0", "1.0", "1.0"), degree=0) |
| 412 |
w = interpolate(f, W) |
| 413 |
x = w.vector() |
| 414 |
assert x.max() == 1 |
| 415 |
assert x.min() == 1 |
| 416 |
|
| 417 |
|
| 418 |
@skip_in_parallel |
| 419 |
def test_interpolation_old(V, W, mesh): |
| 420 |
|
| 421 |
class F0(UserExpression): |
| 422 |
def eval(self, values, x): |
| 423 |
values[0] = 1.0 |
| 424 |
|
| 425 |
class F1(UserExpression): |
| 426 |
def eval(self, values, x): |
| 427 |
values[0] = 1.0 |
| 428 |
values[1] = 1.0 |
| 429 |
values[2] = 1.0 |
| 430 |
|
| 431 |
def value_shape(self): |
| 432 |
return (3,) |
| 433 |
|
| 434 |
# Scalar interpolation |
| 435 |
f0 = F0(degree=0) |
| 436 |
f = Function(V) |
| 437 |
f.interpolate(f0) |
| 438 |
assert round(f.vector().norm("l1") - mesh.num_vertices(), 7) == 0 |
| 439 |
|
| 440 |
# Vector interpolation |
| 441 |
f1 = F1(degree=0) |
| 442 |
f = Function(W) |
| 443 |
f.interpolate(f1) |
| 444 |
assert round(f.vector().norm("l1") - 3*mesh.num_vertices(), 7) == 0 |
| 445 |
|
| 446 |
def test_restrict(mesh, V): |
| 447 |
from numpy import allclose |
| 448 |
|
| 449 |
expr = Expression('x[0]+x[1]+x[2]', degree=1) |
| 450 |
u = interpolate(expr, V) |
| 451 |
|
| 452 |
cell = list(cells(mesh))[-1] # Arbitrary cell |
| 453 |
element = V.dolfin_element() |
| 454 |
|
| 455 |
u_restr = u.restrict(element, cell) |
| 456 |
expr_restr = expr.restrict(element, cell) # Covered in test_expression.py |
| 457 |
|
| 458 |
assert allclose(u_restr, expr_restr) |