Debian Codegraph @

python/test/unit/la/test_petsc4py.py

raw
1 """Unit tests for the pybind11 wrapping of PETSc / petsc4py"""
2
3 # Copyright (C) 2017 Tormod Landet
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 gc
21 from dolfin import (UnitSquareMesh, TrialFunction, TestFunction,
22 FunctionSpace, Function, assemble, dx,
23 parameters, as_backend_type, has_petsc)
24
25 if has_petsc() :
26 from dolfin import (PETScVector, PETScMatrix)
27
28 from dolfin_utils.test import skip_if_not_petsc4py, pushpop_parameters
29
30
31 @skip_if_not_petsc4py
32 def test_petsc4py_vector(pushpop_parameters):
33 "Test PETScVector <-> petsc4py.PETSc.Vec conversions"
34 parameters["linear_algebra_backend"] = "PETSc"
35
36 # Assemble a test matrix
37 mesh = UnitSquareMesh(4, 4)
38 V = FunctionSpace(mesh, "Lagrange", 1)
39 v = TestFunction(V)
40 a = v*dx
41 b1 = assemble(a)
42
43 # Test conversion dolfin.PETScVector -> petsc4py.PETSc.Vec
44 b1 = as_backend_type(b1)
45 v1 = b1.vec()
46
47 # Copy and scale vector with petsc4py
48 v2 = v1.copy()
49 v2.scale(2.0)
50
51 # Test conversion petsc4py.PETSc.Vec -> PETScVector
52 b2 = PETScVector(v2)
53
54 assert (b1.get_local()*2.0 == b2.get_local()).all()
55
56
57 @skip_if_not_petsc4py
58 def test_petsc4py_matrix(pushpop_parameters):
59 "Test PETScMatrix <-> petsc4py.PETSc.Mat conversions"
60 parameters["linear_algebra_backend"] = "PETSc"
61
62 # Assemble a test matrix
63 mesh = UnitSquareMesh(4, 4)
64 V = FunctionSpace(mesh, "Lagrange", 1)
65 u, v = TrialFunction(V), TestFunction(V)
66 a = u*v*dx
67 A1 = assemble(a)
68
69 # Test conversion dolfin.PETScMatrix -> petsc4py.PETSc.Mat
70 A1 = as_backend_type(A1)
71 M1 = A1.mat()
72
73 # Copy and scale matrix with petsc4py
74 M2 = M1.copy()
75 M2.scale(2.0)
76
77 # Test conversion petsc4py.PETSc.Mat -> PETScMatrix
78 A2 = PETScMatrix(M2)
79
80 assert (A1.array()*2.0 == A2.array()).all()
81
82 @skip_if_not_petsc4py
83 def test_ref_count(pushpop_parameters):
84 "Test petsc4py reference counting"
85 parameters["linear_algebra_backend"] = "PETSc"
86
87 mesh = UnitSquareMesh(3, 3)
88 V = FunctionSpace(mesh, "P", 1)
89
90 # Check u and x own the vector
91 u = Function(V)
92 x = as_backend_type(u.vector()).vec()
93 assert x.refcount == 2
94
95 # Check decref
96 del u; gc.collect() # destroy u
97 assert x.refcount == 1
98
99 # Check incref
100 vec = PETScVector(x)
101 assert x.refcount == 2