| 1 |
"""Unit tests for the Vector interface""" |
| 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 |
# Modified by Anders Logg 2011 |
| 21 |
|
| 22 |
import pytest |
| 23 |
import numpy |
| 24 |
from copy import copy |
| 25 |
|
| 26 |
from dolfin import * |
| 27 |
from dolfin_utils.test import * |
| 28 |
|
| 29 |
# TODO: Use the fixture setup from matrix in a shared conftest.py when |
| 30 |
# we move tests to one flat folder. |
| 31 |
|
| 32 |
# Lists of backends supporting or not supporting GenericVector::data() |
| 33 |
# access |
| 34 |
data_backends = [] |
| 35 |
no_data_backends = ["PETSc", "Tpetra"] |
| 36 |
|
| 37 |
# Add serial only backends |
| 38 |
if MPI.size(MPI.comm_world) == 1: |
| 39 |
# TODO: What about "Dense" and "Sparse"? The sub_backend wasn't |
| 40 |
# used in the old test. |
| 41 |
data_backends += ["Eigen"] |
| 42 |
|
| 43 |
# Remove backends we haven't built with |
| 44 |
data_backends = list(filter(has_linear_algebra_backend, data_backends)) |
| 45 |
no_data_backends = list(filter(has_linear_algebra_backend, no_data_backends)) |
| 46 |
any_backends = data_backends + no_data_backends |
| 47 |
|
| 48 |
# Fixtures setting up and resetting the global linear algebra backend |
| 49 |
# for a list of backends |
| 50 |
data_backend = set_parameters_fixture("linear_algebra_backend", data_backends) |
| 51 |
no_data_backend = set_parameters_fixture("linear_algebra_backend", |
| 52 |
no_data_backends) |
| 53 |
any_backend = set_parameters_fixture("linear_algebra_backend", any_backends) |
| 54 |
|
| 55 |
class TestVectorForAnyBackend: |
| 56 |
|
| 57 |
def test_create_empty_vector(self, any_backend): |
| 58 |
v0 = Vector() |
| 59 |
info(v0) |
| 60 |
info(v0, True) |
| 61 |
assert v0.size() == 0 |
| 62 |
|
| 63 |
def test_create_vector(self, any_backend): |
| 64 |
n = 301 |
| 65 |
v1 = Vector(MPI.comm_world, n) |
| 66 |
assert v1.size() == n |
| 67 |
|
| 68 |
def test_copy_vector(self, any_backend): |
| 69 |
n = 301 |
| 70 |
v0 = Vector(MPI.comm_world, n) |
| 71 |
v1 = Vector(v0) |
| 72 |
assert v0.size() == n |
| 73 |
del v0 |
| 74 |
assert v1.size() == n |
| 75 |
|
| 76 |
def test_assign_and_copy_vector(self, any_backend): |
| 77 |
n = 301 |
| 78 |
v0 = Vector(MPI.comm_world, n) |
| 79 |
v0[:] = 1.0 |
| 80 |
assert v0.sum() == n |
| 81 |
v1 = Vector(v0) |
| 82 |
del v0 |
| 83 |
assert v1.sum() == n |
| 84 |
|
| 85 |
def test_zero(self, any_backend): |
| 86 |
v0 = Vector(MPI.comm_world, 301) |
| 87 |
v0.zero() |
| 88 |
assert v0.sum() == 0.0 |
| 89 |
|
| 90 |
def test_apply(self, any_backend): |
| 91 |
v0 = Vector(MPI.comm_world, 301) |
| 92 |
v0.apply("insert") |
| 93 |
v0.apply("add") |
| 94 |
|
| 95 |
def test_str(self, any_backend): |
| 96 |
v0 = Vector(MPI.comm_world, 13) |
| 97 |
tmp = v0.str(False) |
| 98 |
tmp = v0.str(True) |
| 99 |
|
| 100 |
def test_init_range(self, any_backend): |
| 101 |
n = 301 |
| 102 |
local_range = MPI.local_range(MPI.comm_world, n) |
| 103 |
v0 = Vector(MPI.comm_world) |
| 104 |
v0.init(local_range) |
| 105 |
assert v0.local_range() == local_range |
| 106 |
|
| 107 |
def test_size(self, any_backend): |
| 108 |
n = 301 |
| 109 |
v0 = Vector(MPI.comm_world, 301) |
| 110 |
assert v0.size() == n |
| 111 |
|
| 112 |
def test_local_size(self, any_backend): |
| 113 |
n = 301 |
| 114 |
local_range = MPI.local_range(MPI.comm_world, n) |
| 115 |
v0 = Vector(MPI.comm_world) |
| 116 |
v0.init(local_range) |
| 117 |
assert v0.local_size() == local_range[1] - local_range[0] |
| 118 |
|
| 119 |
def test_owns_index(self, any_backend): |
| 120 |
m, n = 301, 25 |
| 121 |
v0 = Vector(MPI.comm_world, m) |
| 122 |
local_range = v0.local_range() |
| 123 |
in_range = local_range[0] <= n < local_range[1] |
| 124 |
assert v0.owns_index(n) == in_range |
| 125 |
|
| 126 |
#def test_set(self, any_backend): |
| 127 |
|
| 128 |
#def test_add(self, any_backend): |
| 129 |
|
| 130 |
def test_get_local(self, any_backend): |
| 131 |
from numpy import empty |
| 132 |
n = 301 |
| 133 |
v0 = Vector(MPI.comm_world, n) |
| 134 |
data = v0.get_local() |
| 135 |
|
| 136 |
def test_set_local(self, any_backend): |
| 137 |
from numpy import zeros |
| 138 |
n = 301 |
| 139 |
v0 = Vector(MPI.comm_world, n) |
| 140 |
data = zeros((v0.local_size()), dtype='d') |
| 141 |
v0.set_local(data) |
| 142 |
data = zeros((v0.local_size()*2), dtype='d') |
| 143 |
|
| 144 |
def test_add_local(self, any_backend): |
| 145 |
from numpy import zeros |
| 146 |
n = 301 |
| 147 |
v0 = Vector(MPI.comm_world, n) |
| 148 |
data = zeros((v0.local_size()), dtype='d') |
| 149 |
v0.add_local(data) |
| 150 |
data = zeros((v0.local_size()*2), dtype='d') |
| 151 |
|
| 152 |
def test_gather(self, any_backend): |
| 153 |
# Gather not implemented in Eigen |
| 154 |
if any_backend == "Eigen" or any_backend == "Tpetra": |
| 155 |
return |
| 156 |
|
| 157 |
# Create distributed vector of local size 1 |
| 158 |
x = DefaultFactory().create_vector(MPI.comm_world) |
| 159 |
r = MPI.rank(x.mpi_comm()) |
| 160 |
x.init((r, r+1)) |
| 161 |
|
| 162 |
# Create local vector |
| 163 |
y = DefaultFactory().create_vector(MPI.comm_self) |
| 164 |
|
| 165 |
# Do the actual test across all rank permutations |
| 166 |
for target_rank in range(MPI.size(x.mpi_comm())): |
| 167 |
|
| 168 |
# Set nonzero value on single rank |
| 169 |
if r == target_rank: |
| 170 |
x[0] = 42.0 # Set using local index |
| 171 |
else: |
| 172 |
x[0] = 0.0 # Set using local index |
| 173 |
assert numpy.isclose(x.sum(), 42.0) |
| 174 |
|
| 175 |
# Gather (using global index) and check the result |
| 176 |
x.gather(y, numpy.array([target_rank], dtype=la_index_dtype())) |
| 177 |
assert numpy.isclose(y[0], 42.0) |
| 178 |
|
| 179 |
# NumPy array version |
| 180 |
out = x.gather(numpy.array([target_rank], dtype=la_index_dtype())) |
| 181 |
assert out.shape == (1,) and numpy.isclose(out[0], 42.0) |
| 182 |
|
| 183 |
# Test gather on zero |
| 184 |
out = x.gather_on_zero() |
| 185 |
if r == 0: |
| 186 |
expected = numpy.array([42.0 if i == target_rank else 0.0 |
| 187 |
for i in range(x.size())]) |
| 188 |
else: |
| 189 |
expected = numpy.array([]) |
| 190 |
assert out.shape == expected.shape and numpy.allclose(out, expected) |
| 191 |
|
| 192 |
# Test also the corner case of empty indices on one process |
| 193 |
if r == target_rank: |
| 194 |
out = x.gather(numpy.array([], dtype=la_index_dtype())) |
| 195 |
expected = numpy.array([]) |
| 196 |
else: |
| 197 |
out = x.gather(numpy.array([target_rank], dtype=la_index_dtype())) |
| 198 |
expected = numpy.array([42.0]) |
| 199 |
assert out.shape == expected.shape and numpy.allclose(out, expected) |
| 200 |
|
| 201 |
# Check that distributed gather vector is not accepted |
| 202 |
if MPI.size(MPI.comm_world) > 1: |
| 203 |
z = DefaultFactory().create_vector(MPI.comm_world) |
| 204 |
with pytest.raises(RuntimeError): |
| 205 |
x.gather(z, numpy.array([0], dtype=la_index_dtype())) |
| 206 |
|
| 207 |
# Check that gather vector of wrong size is not accepted |
| 208 |
z = DefaultFactory().create_vector(MPI.comm_self) |
| 209 |
z.init(3) |
| 210 |
with pytest.raises(RuntimeError): |
| 211 |
x.gather(z, numpy.array([0], dtype=la_index_dtype())) |
| 212 |
|
| 213 |
def test_axpy(self, any_backend): |
| 214 |
n = 301 |
| 215 |
v0 = Vector(MPI.comm_world, n) |
| 216 |
v0[:] = 1.0 |
| 217 |
v1 = Vector(v0) |
| 218 |
v0.axpy(2.0, v1) |
| 219 |
assert v0.sum() == 2*n + n |
| 220 |
|
| 221 |
def test_abs(self, any_backend): |
| 222 |
n = 301 |
| 223 |
v0 = Vector(MPI.comm_world, n) |
| 224 |
v0[:] = -1.0 |
| 225 |
v0.abs() |
| 226 |
assert v0.sum() == n |
| 227 |
|
| 228 |
def test_inner(self, any_backend): |
| 229 |
n = 301 |
| 230 |
v0 = Vector(MPI.comm_world, n) |
| 231 |
v0[:] = 2.0 |
| 232 |
v1 = Vector(MPI.comm_world, n) |
| 233 |
v1[:] = 3.0 |
| 234 |
assert v0.inner(v1) == 6*n |
| 235 |
|
| 236 |
def test_norm(self, any_backend): |
| 237 |
n = 301 |
| 238 |
v0 = Vector(MPI.comm_world, n) |
| 239 |
v0[:] = -2.0 |
| 240 |
assert v0.norm("l1") == 2.0*n |
| 241 |
assert v0.norm("l2") == sqrt(4.0*n) |
| 242 |
assert v0.norm("linf") == 2.0 |
| 243 |
|
| 244 |
def test_min(self, any_backend): |
| 245 |
v0 = Vector(MPI.comm_world, 301) |
| 246 |
v0[:] = 2.0 |
| 247 |
assert v0.min() == 2.0 |
| 248 |
|
| 249 |
def test_max(self, any_backend): |
| 250 |
v0 = Vector(MPI.comm_world,301) |
| 251 |
v0[:] = -2.0 |
| 252 |
assert v0.max() == -2.0 |
| 253 |
|
| 254 |
def test_sum(self, any_backend): |
| 255 |
n = 301 |
| 256 |
v0 = Vector(MPI.comm_world, n) |
| 257 |
v0[:] = -2.0 |
| 258 |
assert v0.sum() == -2.0*n |
| 259 |
|
| 260 |
def test_sum_entries(self, any_backend): |
| 261 |
from numpy import zeros |
| 262 |
n = 301 |
| 263 |
v0 = Vector(MPI.comm_world, n) |
| 264 |
v0[:] = -2.0 |
| 265 |
entries = zeros(5, dtype='uintp') |
| 266 |
assert v0.sum(entries) == -2.0 |
| 267 |
entries[0] = 2 |
| 268 |
entries[1] = 1 |
| 269 |
entries[2] = 236 |
| 270 |
entries[3] = 123 |
| 271 |
entries[4] = 97 |
| 272 |
assert v0.sum(entries) == -2.0*5 |
| 273 |
|
| 274 |
def test_scalar_mult(self, any_backend): |
| 275 |
n = 301 |
| 276 |
v0 = Vector(MPI.comm_world, n) |
| 277 |
v0[:] = -1.0 |
| 278 |
v0 *= 2.0 |
| 279 |
assert v0.sum() == -2.0*n |
| 280 |
|
| 281 |
def test_vector_element_mult(self, any_backend): |
| 282 |
n = 301 |
| 283 |
v0 = Vector(MPI.comm_world, n) |
| 284 |
v1 = Vector(MPI.comm_world, n) |
| 285 |
v0[:] = -2.0 |
| 286 |
v1[:] = 3.0 |
| 287 |
v0 *= v1 |
| 288 |
assert v0.sum() == -6.0*n |
| 289 |
|
| 290 |
def test_scalar_divide(self, any_backend): |
| 291 |
n = 301 |
| 292 |
v0 = Vector(MPI.comm_world, n) |
| 293 |
v0[:] = -1.0 |
| 294 |
v0 /= -2.0 |
| 295 |
assert v0.sum() == 0.5*n |
| 296 |
|
| 297 |
def test_vector_add(self, any_backend): |
| 298 |
n = 301 |
| 299 |
v0 = Vector(MPI.comm_world, n) |
| 300 |
v1 = Vector(MPI.comm_world, n) |
| 301 |
v0[:] = -1.0 |
| 302 |
v1[:] = 2.0 |
| 303 |
v0 += v1 |
| 304 |
assert v0.sum() == n |
| 305 |
|
| 306 |
def test_scalar_add(self, any_backend): |
| 307 |
n = 301 |
| 308 |
v0 = Vector(MPI.comm_world, n) |
| 309 |
v1 = Vector(MPI.comm_world, n) |
| 310 |
v0[:] = -1.0 |
| 311 |
v0 += 2.0 |
| 312 |
assert v0.sum() == n |
| 313 |
v0 -= 2.0 |
| 314 |
assert v0.sum() == -n |
| 315 |
v0 = v0 + 3.0 |
| 316 |
assert v0.sum() == 2*n |
| 317 |
v0 = v0 - 1.0 |
| 318 |
assert v0.sum() == n |
| 319 |
|
| 320 |
def test_vector_subtract(self, any_backend): |
| 321 |
n = 301 |
| 322 |
v0 = Vector(MPI.comm_world, n) |
| 323 |
v1 = Vector(MPI.comm_world, n) |
| 324 |
v0[:] = -1.0 |
| 325 |
v1[:] = 2.0 |
| 326 |
v0 -= v1 |
| 327 |
assert v0.sum() == -3.0*n |
| 328 |
|
| 329 |
def test_vector_assignment(self, any_backend): |
| 330 |
m, n = 301, 345 |
| 331 |
v0 = Vector(MPI.comm_world, m) |
| 332 |
v1 = Vector(MPI.comm_world, n) |
| 333 |
v0[:] = -1.0 |
| 334 |
v1[:] = 2.0 |
| 335 |
v0 = v1 |
| 336 |
assert v0.sum() == 2.0*n |
| 337 |
|
| 338 |
def test_vector_assignment_length(self, any_backend): |
| 339 |
# Test that assigning vectors of different lengths fails |
| 340 |
m, n = 301, 345 |
| 341 |
v0 = Vector(MPI.comm_world, m) |
| 342 |
v1 = Vector(MPI.comm_world, n) |
| 343 |
def wrong_assignment(v0, v1): |
| 344 |
v0[:] = v1 |
| 345 |
with pytest.raises(RuntimeError): |
| 346 |
wrong_assignment(v0, v1) |
| 347 |
|
| 348 |
def test_vector_assignment_length(self, any_backend): |
| 349 |
# Test that assigning with diffrent parallel layouts fails |
| 350 |
if MPI.size(MPI.comm_world) > 1: |
| 351 |
m = 301 |
| 352 |
local_range0 = MPI.local_range(MPI.comm_world, m) |
| 353 |
print("local range", local_range0[0], local_range0[1]) |
| 354 |
|
| 355 |
# Shift parallel partitiong but preserve global size |
| 356 |
if MPI.rank(MPI.comm_world) == 0: |
| 357 |
local_range1 = (local_range0[0], local_range0[1] + 1) |
| 358 |
elif MPI.rank(MPI.comm_world) == MPI.size(MPI.comm_world) - 1: |
| 359 |
local_range1 = (local_range0[0] + 1, local_range0[1]) |
| 360 |
else: |
| 361 |
local_range1 = (local_range0[0] + 1, local_range0[1] + 1) |
| 362 |
|
| 363 |
v0 = Vector(MPI.comm_world) |
| 364 |
v0.init(local_range0) |
| 365 |
v1 = Vector(MPI.comm_world) |
| 366 |
v1.init(local_range1) |
| 367 |
assert v0.size() == v1.size() |
| 368 |
|
| 369 |
def wrong_assignment(v0, v1): |
| 370 |
v0[:] = v1 |
| 371 |
with pytest.raises(RuntimeError): |
| 372 |
wrong_assignment(v0, v1) |
| 373 |
|
| 374 |
|
| 375 |
# Test the access of the raw data through pointers |
| 376 |
# This is only available for Eigen backend |
| 377 |
def test_vector_data(self, data_backend): |
| 378 |
# Test for ordinary Vector |
| 379 |
v = Vector(MPI.comm_world, 301) |
| 380 |
v = as_backend_type(v) |
| 381 |
|
| 382 |
rw_array = v.array_view() |
| 383 |
assert rw_array.flags.owndata == False |
| 384 |
with pytest.raises(Exception): |
| 385 |
rw_array.resize([10]) |
| 386 |
|
| 387 |
# Check that the array is a writable view |
| 388 |
rw_array[0] = 42 |
| 389 |
ro_array = v.get_local() |
| 390 |
assert ro_array[0] == 42 |
| 391 |
|
| 392 |
# Test for as_backend_type Vector |
| 393 |
v = as_backend_type(v) |
| 394 |
rw_array2 = v.array_view() |
| 395 |
assert (rw_array2 == ro_array).all() |
| 396 |
|
| 397 |
# xfail on TypeError |
| 398 |
xfail_type = pytest.mark.xfail(strict=True, raises=TypeError) |
| 399 |
xfail_type_py3 = pytest.mark.xfail(strict=True, raises=TypeError) |
| 400 |
|
| 401 |
|
| 402 |
@pytest.mark.parametrize("operand", |
| 403 |
[int(42), 42.0, numpy.sin(1.0), numpy.float(42.0), |
| 404 |
numpy.float64(42.0), numpy.float_(42.0), |
| 405 |
numpy.int(42.0), numpy.long(42.0), |
| 406 |
numpy.float16(42.0), numpy.float16(42.0), |
| 407 |
numpy.float32(42.0), numpy.float128(42.0), |
| 408 |
numpy.longfloat(42.0), numpy.int8(42.0), |
| 409 |
numpy.int16(42.0), numpy.int32(42.0), |
| 410 |
numpy.intc(42.0), numpy.longdouble(42.0), |
| 411 |
numpy.int0(42.0), numpy.int64(42.0), |
| 412 |
numpy.int_(42.0), numpy.longlong(42.0), |
| 413 |
]) |
| 414 |
def test_vector_type_priority_with_numpy(self, any_backend, operand): |
| 415 |
"""Test that DOLFIN return types are prefered over NumPy types for |
| 416 |
binary operations on NumPy objects |
| 417 |
|
| 418 |
""" |
| 419 |
|
| 420 |
def _test_binary_ops(v, operand): |
| 421 |
assert isinstance(v + operand, cpp.la.GenericVector) |
| 422 |
assert isinstance(v - operand, cpp.la.GenericVector) |
| 423 |
assert isinstance(v*operand, cpp.la.GenericVector) |
| 424 |
assert isinstance(v/operand, cpp.la.GenericVector) |
| 425 |
assert isinstance(operand + v, cpp.la.GenericVector) |
| 426 |
assert isinstance(operand - v, cpp.la.GenericVector) |
| 427 |
assert isinstance(operand*v, cpp.la.GenericVector) |
| 428 |
assert isinstance(v+v, cpp.la.GenericVector) |
| 429 |
assert isinstance(v-v, cpp.la.GenericVector) |
| 430 |
assert isinstance(v*v, cpp.la.GenericVector) |
| 431 |
v += v.copy(); assert isinstance(v, cpp.la.GenericVector) |
| 432 |
v -= v.copy(); assert isinstance(v, cpp.la.GenericVector) |
| 433 |
v *= v.copy(); assert isinstance(v, cpp.la.GenericVector) |
| 434 |
v += operand; assert isinstance(v, cpp.la.GenericVector) |
| 435 |
v -= operand; assert isinstance(v, cpp.la.GenericVector) |
| 436 |
v *= operand; assert isinstance(v, cpp.la.GenericVector) |
| 437 |
v /= operand; assert isinstance(v, cpp.la.GenericVector) |
| 438 |
op = copy(operand); op += v; assert isinstance(op, cpp.la.GenericVector) |
| 439 |
op = copy(operand); op -= v; assert isinstance(op, cpp.la.GenericVector) |
| 440 |
op = copy(operand); op *= v; assert isinstance(op, cpp.la.GenericVector) |
| 441 |
|
| 442 |
# Test with vector wrapper |
| 443 |
v = Vector(MPI.comm_world, 8) |
| 444 |
_test_binary_ops(v, operand) |
| 445 |
|
| 446 |
# Test with vector casted to backend type |
| 447 |
v = as_backend_type(v) |
| 448 |
_test_binary_ops(v, operand) |