Debian Codegraph @

python/test/unit/la/test_tensor_layout.py

raw
1 """Unit tests for TensorLayout and SparsityPattern interface"""
2
3 # Copyright (C) 2015 Jan Blechta
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 numpy as np
23
24 from dolfin_utils.test import *
25
26
27 backends = sorted(linear_algebra_backends().keys())
28 if MPI.size(MPI.comm_world) > 1 and 'Eigen' in backends:
29 backends.remove('Eigen')
30 backend = set_parameters_fixture("linear_algebra_backend", backends)
31
32
33 @fixture
34 def mesh():
35 return UnitSquareMesh(10, 10)
36
37
38 @pytest.mark.parametrize("element", [
39 FiniteElement("P", triangle, 1),
40 VectorElement("P", triangle, 1),
41 VectorElement("P", triangle, 2)*FiniteElement("P", triangle, 1),
42 VectorElement("P", triangle, 1)*FiniteElement("R", triangle, 0),
43 ])
44 def test_layout_and_pattern_interface(backend, mesh, element):
45 # Strange Tpetra segfault with Reals in sequential
46 if (backend == "Tpetra"
47 and MPI.size(mesh.mpi_comm()) == 1
48 and element == VectorElement("P", triangle, 1)*FiniteElement("R", triangle, 0)
49 ):
50 pytest.xfail(reason="This testcase segfaults")
51
52 V = FunctionSpace(mesh, element)
53 m = V.mesh()
54 c = m.mpi_comm()
55 d = V.dofmap()
56 i = d.index_map()
57
58 # Poisson problem
59 u = TrialFunction(V)
60 v = TestFunction(V)
61 a = inner(grad(u), grad(v))*dx + dot(u, v)*dx
62 f = Expression(np.full(v.ufl_shape, "x[0]*x[1]", dtype=object).tolist(), degree=2)
63 L = inner(f, v)*dx
64
65 # Test ghosted vector (for use as dofs of FE function)
66 t0 = TensorLayout(c, 0, TensorLayout.Sparsity.DENSE)
67 t0.init([i], TensorLayout.Ghosts.GHOSTED)
68 x = Vector(c)
69 x.init(t0)
70 u = Function(V, x)
71
72 # Test unghosted vector (for assembly of rhs)
73 t1 = TensorLayout(c, 0, TensorLayout.Sparsity.DENSE)
74 t1.init([i], TensorLayout.Ghosts.UNGHOSTED)
75 b = Vector()
76 b.init(t1)
77 assemble(L, tensor=b)
78
79 # Build sparse tensor layout (for assembly of matrix)
80 t2 = TensorLayout(c, 0, TensorLayout.Sparsity.SPARSE)
81 t2.init([i, i], TensorLayout.Ghosts.UNGHOSTED)
82 s2 = t2.sparsity_pattern()
83 s2.init([i, i])
84 SparsityPatternBuilder.build(s2, m, [d, d],
85 True, False, False, False,
86 False, init=False)
87 A = Matrix()
88 A.init(t2)
89 assemble(a, tensor=A)
90
91 # Test sparsity pattern consistency
92 diag = s2.num_nonzeros_diagonal()
93 off_diag = s2.num_nonzeros_off_diagonal()
94 # Sequential pattern returns just empty off_diagonal
95 off_diag = off_diag if off_diag.any() else np.zeros(diag.shape, diag.dtype)
96 local = s2.num_local_nonzeros()
97 assert (local == diag + off_diag).all()
98 assert local.sum() == s2.num_nonzeros()
99
100 # Check that solve passes smoothly
101 ud = np.full(v.ufl_shape, 0, dtype=object).tolist()
102 bc = DirichletBC(V, ud, lambda x, b: b)
103 bc.apply(A)
104 bc.apply(b)
105 solve(A, x, b)