| 1 |
"""Unit tests for the linear algebra interface""" |
| 2 |
|
| 3 |
# Copyright (C) 2008 Johan Hake |
| 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: 2008-09-30 |
| 21 |
# Last changed: 2014-05-30 |
| 22 |
|
| 23 |
import pytest |
| 24 |
from dolfin import * |
| 25 |
import sys |
| 26 |
|
| 27 |
from dolfin_utils.test import * |
| 28 |
|
| 29 |
# Lists of backends supporting or not supporting data access |
| 30 |
data_backends = [] |
| 31 |
no_data_backends = [("PETSc", ""), ("Tpetra", "")] |
| 32 |
|
| 33 |
# Add serial only backends |
| 34 |
if MPI.size(MPI.comm_world) == 1: |
| 35 |
# TODO: What about "Dense" and "Sparse"? The sub_backend wasn't |
| 36 |
# used in the old test. |
| 37 |
data_backends += [("Eigen", "")] |
| 38 |
|
| 39 |
# Remove backends we haven't built with |
| 40 |
data_backends = [b for b in data_backends if has_linear_algebra_backend(b[0])] |
| 41 |
no_data_backends = [b for b in no_data_backends if has_linear_algebra_backend(b[0])] |
| 42 |
any_backends = data_backends + no_data_backends |
| 43 |
|
| 44 |
# Fixtures setting up and resetting the global linear algebra backend |
| 45 |
# for a list of backends |
| 46 |
any_backend = set_parameters_fixture("linear_algebra_backend", |
| 47 |
any_backends, lambda x: x[0]) |
| 48 |
data_backend = set_parameters_fixture("linear_algebra_backend", |
| 49 |
data_backends, lambda x: x[0]) |
| 50 |
no_data_backend = set_parameters_fixture("linear_algebra_backend", |
| 51 |
no_data_backends, lambda x: x[0]) |
| 52 |
|
| 53 |
# With and without explicit backend choice |
| 54 |
use_backend = true_false_fixture |
| 55 |
|
| 56 |
|
| 57 |
def xtest_deterministic_partition(): |
| 58 |
# Works with parmetis, not with scotch with mpirun -np 3 |
| 59 |
mesh1 = UnitSquareMesh(3, 3) |
| 60 |
mesh2 = UnitSquareMesh(3, 3) |
| 61 |
V1 = FunctionSpace(mesh1, "Lagrange", 1) |
| 62 |
V2 = FunctionSpace(mesh2, "Lagrange", 1) |
| 63 |
assert V1.dofmap().ownership_range() == V2.dofmap().ownership_range() |
| 64 |
|
| 65 |
|
| 66 |
def assemble_vectors(mesh): |
| 67 |
V = FunctionSpace(mesh, "Lagrange", 2) |
| 68 |
W = FunctionSpace(mesh, "Lagrange", 1) |
| 69 |
v = TestFunction(V) |
| 70 |
t = TestFunction(W) |
| 71 |
return assemble(v*dx), assemble(t*dx) |
| 72 |
|
| 73 |
|
| 74 |
def get_forms(mesh): |
| 75 |
V = FunctionSpace(mesh, "Lagrange", 2) |
| 76 |
W = FunctionSpace(mesh, "Lagrange", 1) |
| 77 |
|
| 78 |
v = TestFunction(V) |
| 79 |
u = TrialFunction(V) |
| 80 |
s = TrialFunction(W) |
| 81 |
|
| 82 |
# Forms |
| 83 |
a = dot(grad(u),grad(v))*dx |
| 84 |
b = v*s*dx |
| 85 |
return a, b |
| 86 |
|
| 87 |
|
| 88 |
class TestBasicLaOperations: |
| 89 |
def test_vector(self, any_backend): |
| 90 |
self.backend, self.sub_backend = any_backend |
| 91 |
from numpy import ndarray, linspace, array, fromiter |
| •92 |
from numpy import intp,int16,int32,int64 |
| 93 |
from numpy import uint,uintp,uint16,uint32,uint64 |
| 94 |
mesh = UnitSquareMesh(3, 3) |
| 95 |
v, w = assemble_vectors(mesh) |
| 96 |
|
| 97 |
# Get local ownership range (relevant for parallel vectors) |
| 98 |
n0, n1 = v.local_range() |
| 99 |
distributed = True |
| 100 |
if (n1 - n0) == v.size(): |
| 101 |
distributed = False |
| 102 |
|
| 103 |
# Test set and access with different integers |
| 104 |
lind = 2 |
| •105 |
for T in [int,int16,int32,int64,uint,uintp,uint16,uint32,uint64,\ |
| 106 |
intp]: |
| 107 |
v[T(lind)] = 2.0 |
| 108 |
assert round(v[T(lind)] - 2.0, 7) == 0 |
| 109 |
|
| 110 |
A = v.copy() |
| 111 |
B = as_backend_type(v.copy()) |
| 112 |
gind = 5 |
| 113 |
lind = gind-n0 |
| 114 |
|
| 115 |
# Test global index access |
| 116 |
if A.owns_index(gind): |
| 117 |
assert round(A[lind] - B[lind], 7) == 0 |
| 118 |
|
| 119 |
lind0 = 5 |
| 120 |
round(A[lind0] - B[lind0], 7) == 0 |
| 121 |
|
| 122 |
B *= 0.5 |
| 123 |
A *= 2 |
| 124 |
assert round(A[lind0] - 4*B[lind0], 7) == 0 |
| 125 |
|
| 126 |
B /= 2 |
| 127 |
A /= 0.5 |
| 128 |
assert round(A[lind0] - 16*B[lind0], 7) == 0 |
| 129 |
|
| 130 |
val1 = A[lind0] |
| 131 |
val2 = B[lind0] |
| 132 |
|
| 133 |
A += B |
| 134 |
assert round(A[lind0] - val1 - val2, 7) == 0 |
| 135 |
|
| 136 |
A -= B |
| 137 |
assert round(A[lind0] - val1, 7) == 0 |
| 138 |
|
| 139 |
C = 16*B |
| 140 |
assert round(A[lind0] - C[lind0], 7) == 0 |
| 141 |
|
| 142 |
D = (C + B)*5 |
| 143 |
assert round(D[lind0] - (val1 + val2)*5, 7) == 0 |
| 144 |
|
| 145 |
|
| 146 |
F = (A-B)/4 |
| 147 |
assert round(F[lind0] - (val1 - val2)/4, 7) == 0 |
| 148 |
|
| 149 |
A.axpy(100, B) |
| 150 |
assert round(A[lind0] - val1 - val2*100, 7) == 0 |
| 151 |
|
| 152 |
A2 = A.get_local() |
| 153 |
assert isinstance(A2,ndarray) |
| 154 |
assert A2.shape == (n1 - n0, ) |
| 155 |
|
| 156 |
assert round(A2[lind0] - A[lind0], 7) == 0 |
| 157 |
|
| 158 |
assert round(MPI.sum(A.mpi_comm(), A2.sum()) - A.sum(), 7) == 0 |
| 159 |
|
| 160 |
B2 = B.get_local() |
| 161 |
|
| 162 |
inds = [1,3,6,9,15,20,24,28,32,40,50,60,70,100000] |
| 163 |
|
| 164 |
# Extract owned local indices |
| 165 |
inds0 = array([i for i in inds if v.owns_index(i)]) |
| 166 |
linds0 = [i-n0 for i in inds0] |
| 167 |
linds1 = array(linds0, 'i') |
| 168 |
linds2 = array(linds0, 'I') |
| 169 |
|
| 170 |
A[linds2] = inds0 |
| 171 |
A2[linds0] = inds0 |
| 172 |
|
| 173 |
G = A[linds0] |
| 174 |
G1 = A[linds1] |
| 175 |
G2 = A2[linds2] |
| 176 |
|
| 177 |
G3 = A[A > 1] |
| 178 |
G4 = A2[A2 > 1] |
| 179 |
|
| 180 |
A3 = fromiter(A, "d") |
| 181 |
|
| 182 |
if A.owns_index(15): |
| 183 |
a = A[15-n0] |
| 184 |
|
| 185 |
b = 1.e10 |
| 186 |
|
| 187 |
assert round(G1.sum() - G.sum(), 7) == 0 |
| 188 |
assert round(G2.sum() - G.sum(), 7) == 0 |
| 189 |
assert len(G3) == len(G4) |
| 190 |
assert round(G3.sum() - G4.sum(), 7) == 0 |
| 191 |
assert all(val==G[i] for i, val in enumerate(G)) |
| 192 |
assert (G==G1).all() |
| 193 |
assert (G<=G1).all() |
| 194 |
assert (G>=G1).all() |
| 195 |
assert not (G<G1).any() |
| 196 |
assert not (G>G1).any() |
| 197 |
if A.owns_index(15): assert a in A |
| 198 |
assert b not in A |
| 199 |
|
| 200 |
assert (A3==A2).all() |
| 201 |
|
| 202 |
A[:] = A2 |
| 203 |
assert (A.get_local()==A2).all() |
| 204 |
|
| 205 |
H = A.copy() |
| 206 |
H[linds0] = G |
| 207 |
|
| 208 |
C[:] = 2 |
| 209 |
D[:] = 2 |
| 210 |
|
| 211 |
assert round(C[0] - 2, 7) == 0 |
| 212 |
assert round(C[len(linds0)-1] - 2, 7) == 0 |
| 213 |
assert round(C.sum() - D.sum(), 7) == 0 |
| 214 |
|
| 215 |
C[linds0] = 3 |
| 216 |
assert round(C[linds0].sum() - 3*len(linds0), 7) == 0 |
| 217 |
|
| 218 |
def wrong_index(ind): |
| 219 |
A[ind] |
| 220 |
|
| 221 |
with pytest.raises(IndexError): |
| 222 |
wrong_index(-len(A)-1) |
| 223 |
with pytest.raises(IndexError): |
| 224 |
wrong_index(A[-1]) |
| 225 |
with pytest.raises(IndexError): |
| 226 |
wrong_index(len(A)+1) |
| 227 |
with pytest.raises(TypeError): |
| 228 |
wrong_index("jada") |
| 229 |
with pytest.raises(TypeError): |
| 230 |
wrong_index(.5) |
| 231 |
with pytest.raises(IndexError): |
| 232 |
wrong_index([-len(A)-1, 2]) |
| 233 |
with pytest.raises(IndexError): |
| 234 |
wrong_index([len(A), 2]) |
| 235 |
|
| 236 |
def wrong_dim(ind0, ind1): |
| 237 |
A[ind0] = B[ind1] |
| 238 |
|
| 239 |
with pytest.raises(IndexError): |
| 240 |
wrong_dim([0,2], [0,2,4]) |
| 241 |
with pytest.raises(IndexError): |
| 242 |
wrong_dim([0,2], slice(0,4,1)) |
| 243 |
|
| 244 |
A2 = A.get_local() |
| 245 |
A *= B |
| 246 |
A2 *= B2 |
| 247 |
I = A*B |
| 248 |
I2 = A2*B2 |
| 249 |
|
| 250 |
assert round(A.sum() - MPI.sum(A.mpi_comm(), A2.sum()), 7) == 0 |
| 251 |
assert round(I.sum() - MPI.sum(A.mpi_comm(), I2.sum()), 7) == 0 |
| 252 |
|
| 253 |
def wrong_assign(A, ind): |
| 254 |
A[linds0[::2]] = linds0[::2] |
| 255 |
|
| 256 |
|
| 257 |
def test_matrix_vector(self, any_backend, use_backend): |
| 258 |
self.backend, self.sub_backend = any_backend |
| 259 |
from numpy import dot, absolute |
| 260 |
|
| 261 |
mesh = UnitSquareMesh(3, 3) |
| 262 |
|
| 263 |
a, b = get_forms(mesh) |
| 264 |
if use_backend: |
| 265 |
backend = getattr(cpp.la, self.backend+self.sub_backend+'Factory').instance() |
| 266 |
else: |
| 267 |
backend = None |
| 268 |
|
| 269 |
A = assemble(a, backend=backend) |
| 270 |
B = assemble(b, backend=backend) |
| 271 |
|
| 272 |
v, w = assemble_vectors(mesh) |
| 273 |
|
| 274 |
# Get local ownership range (relevant for parallel vectors) |
| 275 |
n0, n1 = v.local_range() |
| 276 |
|
| 277 |
distributed = True |
| 278 |
if (n1 - n0) == v.size(): |
| 279 |
distributed = False |
| 280 |
|
| 281 |
# Reference values |
| 282 |
v_norm = 0.181443684651 |
| 283 |
w_norm = 0.278394377377 |
| 284 |
A_norm = 31.947874212 |
| 285 |
B_norm = 0.11052313564 |
| 286 |
Av_norm = 0.575896483442 |
| 287 |
Bw_norm = 0.0149136743079 |
| 288 |
Cv_norm = 0.00951459156865 |
| 289 |
|
| 290 |
assert round(v.norm('l2') - v_norm, 7) == 0 |
| 291 |
assert round(w.norm('l2') - w_norm, 7) == 0 |
| 292 |
assert round(A.norm('frobenius') - A_norm, 7) == 0 |
| 293 |
assert round(B.norm('frobenius') - B_norm, 7) == 0 |
| 294 |
|
| 295 |
u = A*v |
| 296 |
|
| 297 |
assert len(u) == len(v) |
| 298 |
|
| 299 |
# Test basic square matrix multiply results |
| 300 |
assert round(v.norm('l2') - v_norm, 7) == 0 |
| 301 |
assert round(u.norm('l2') - Av_norm, 7) == 0 |
| 302 |
|
| 303 |
# Test rectangular matrix multiply results |
| 304 |
assert round((B*w).norm('l2') - Bw_norm, 7) == 0 |
| 305 |
|
| 306 |
# Test transpose multiply (rectangular) |
| 307 |
x = Vector() |
| 308 |
B.transpmult(v, x) |
| 309 |
assert round(x.norm('l2') - Cv_norm, 7) == 0 |
| 310 |
|
| 311 |
# Miscellaneous tests |
| 312 |
u2 = 2*u - A*v |
| 313 |
assert round(u2[4] - u[4], 7) == 0 |
| 314 |
|
| 315 |
u3 = 2*u + -1.0*(A*v) |
| 316 |
assert round(u3[4] - u[4], 7) == 0 |
| 317 |
|
| 318 |
# Numpy arrays are not aligned in parallel |
| 319 |
if distributed: |
| 320 |
return |
| 321 |
|
| 322 |
v_numpy = v.get_local() |
| 323 |
A_numpy = A.array() |
| 324 |
|
| 325 |
u_numpy = dot(A_numpy, v_numpy) |
| 326 |
u_numpy2 = A*v_numpy |
| 327 |
|
| 328 |
assert absolute(u.get_local() - u_numpy).sum() < DOLFIN_EPS*len(v) |
| 329 |
assert absolute(u_numpy2 - u_numpy).sum() < DOLFIN_EPS*len(v) |
| 330 |
|
| 331 |
|
| 332 |
def test_vector_data(self, no_data_backend): |
| 333 |
mesh = UnitSquareMesh(3, 3) |
| 334 |
v, w = assemble_vectors(mesh) |
| 335 |
v = as_backend_type(v) |
| 336 |
def no_attribute(): |
| 337 |
v.data() |
| 338 |
|
| 339 |
with pytest.raises(AttributeError): |
| 340 |
no_attribute() |
| 341 |
|
| 342 |
def test_vector_negation(self): |
| 343 |
V = FunctionSpace(UnitSquareMesh(10,10), "CG",1) |
| 344 |
u = TestFunction(V) |
| 345 |
v = Function(V) |
| 346 |
b = u*v*dx |
| 347 |
B = assemble(b) |
| 348 |
assert((-B == -1*B).all()) |