| 1 |
"""Unit tests for the LUSolver interface""" |
| 2 |
|
| 3 |
# Copyright (C) 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 |
from dolfin import * |
| 21 |
import pytest |
| 22 |
from dolfin_utils.test import skip_if_not_PETSc, skip_in_parallel |
| 23 |
|
| 24 |
backends = ["PETSc", pytest.param(("Eigen"), marks=skip_in_parallel)] |
| 25 |
|
| 26 |
@pytest.mark.parametrize('backend', backends) |
| 27 |
def test_lu_solver(backend): |
| 28 |
|
| 29 |
# Check whether backend is available |
| 30 |
if not has_linear_algebra_backend(backend): |
| 31 |
pytest.skip('Need %s as backend to run this test' % backend) |
| 32 |
|
| 33 |
# Set linear algebra backend |
| 34 |
prev_backend = parameters["linear_algebra_backend"] |
| 35 |
parameters["linear_algebra_backend"] = backend |
| 36 |
|
| 37 |
mesh = UnitSquareMesh(12, 12) |
| 38 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 39 |
u, v = TrialFunction(V), TestFunction(V) |
| 40 |
A = assemble(Constant(1.0)*u*v*dx) |
| 41 |
b = assemble(Constant(1.0)*v*dx) |
| 42 |
|
| 43 |
norm = 13.0 |
| 44 |
|
| 45 |
solver = LUSolver() |
| 46 |
x = Vector() |
| 47 |
solver.solve(A, x, b) |
| 48 |
assert round(x.norm("l2") - norm, 10) == 0 |
| 49 |
|
| 50 |
solver = LUSolver(A) |
| 51 |
x = Vector() |
| 52 |
solver.solve(x, b) |
| 53 |
assert round(x.norm("l2") - norm, 10) == 0 |
| 54 |
|
| 55 |
solver = LUSolver() |
| 56 |
x = Vector() |
| 57 |
solver.set_operator(A) |
| 58 |
solver.solve(x, b) |
| 59 |
assert round(x.norm("l2") - norm, 10) == 0 |
| 60 |
|
| 61 |
solver = LUSolver() |
| 62 |
x = Vector() |
| 63 |
solver.solve(A, x, b) |
| 64 |
assert round(x.norm("l2") - norm, 10) == 0 |
| 65 |
|
| 66 |
# Reset backend |
| 67 |
parameters["linear_algebra_backend"] = prev_backend |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize('backend', backends) |
| 71 |
def test_lu_solver_reuse(backend): |
| 72 |
"""Test that LU re-factorisation is only performed after |
| 73 |
set_operator(A) is called""" |
| 74 |
|
| 75 |
# Test requires PETSc version 3.5 or later. Use petsc4py to check |
| 76 |
# version number. |
| 77 |
try: |
| 78 |
from petsc4py import PETSc |
| 79 |
except ImportError: |
| 80 |
pytest.skip("petsc4py required to check PETSc version") |
| 81 |
else: |
| 82 |
if not PETSc.Sys.getVersion() >= (3, 5, 0): |
| 83 |
pytest.skip("PETSc version must be 3.5 of higher") |
| 84 |
|
| 85 |
|
| 86 |
# Check whether backend is available |
| 87 |
if not has_linear_algebra_backend(backend): |
| 88 |
pytest.skip('Need %s as backend to run this test' % backend) |
| 89 |
|
| 90 |
# Set linear algebra backend |
| 91 |
prev_backend = parameters["linear_algebra_backend"] |
| 92 |
parameters["linear_algebra_backend"] = backend |
| 93 |
|
| 94 |
mesh = UnitSquareMesh(12, 12) |
| 95 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 96 |
u, v = TrialFunction(V), TestFunction(V) |
| 97 |
b = assemble(Constant(1.0)*v*dx) |
| 98 |
|
| 99 |
A = assemble(Constant(1.0)*u*v*dx) |
| 100 |
norm = 13.0 |
| 101 |
|
| 102 |
solver = LUSolver(A) |
| 103 |
x = Vector() |
| 104 |
solver.solve(x, b) |
| 105 |
assert round(x.norm("l2") - norm, 10) == 0 |
| 106 |
|
| 107 |
assemble(Constant(0.5)*u*v*dx, tensor=A) |
| 108 |
x = Vector() |
| 109 |
solver.solve(x, b) |
| 110 |
if backend != 'Eigen': |
| 111 |
# The eigen backend only recomputes the factorization once set_operator is called |
| 112 |
assert round(x.norm("l2") - 2.0*norm, 10) == 0 |
| 113 |
|
| 114 |
solver.set_operator(A) |
| 115 |
solver.solve(x, b) |
| 116 |
assert round(x.norm("l2") - 2.0*norm, 10) == 0 |
| 117 |
|
| 118 |
# Reset backend |
| 119 |
parameters["linear_algebra_backend"] = prev_backend |