| 1 |
"Unit tests for SparsityPattern" |
| 2 |
|
| 3 |
# Copyright (C) 2017 Nathan Sime |
| 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 |
import numpy as np |
| 22 |
from dolfin import * |
| 23 |
from dolfin_utils.test import * |
| 24 |
|
| 25 |
|
| 26 |
def count_on_and_off_diagonal_nnz(primary_codim_entries, local_range): |
| 27 |
nnz_on_diagonal = sum(1 for entry in primary_codim_entries |
| 28 |
if local_range[0] <= entry < local_range[1]) |
| 29 |
nnz_off_diagonal = sum(1 for entry in primary_codim_entries |
| 30 |
if not local_range[0] <= entry < local_range[1]) |
| 31 |
return nnz_on_diagonal, nnz_off_diagonal |
| 32 |
|
| 33 |
|
| 34 |
@fixture |
| 35 |
def mesh(): |
| 36 |
return UnitSquareMesh(10, 10) |
| 37 |
|
| 38 |
|
| 39 |
@fixture |
| 40 |
def V(mesh): |
| 41 |
return FunctionSpace(mesh, "CG", 1) |
| 42 |
|
| 43 |
|
| 44 |
def test_str(mesh, V): |
| 45 |
dm = V.dofmap() |
| 46 |
index_map = dm.index_map() |
| 47 |
|
| 48 |
# Build sparse tensor layout (for assembly of matrix) |
| 49 |
tl = TensorLayout(mesh.mpi_comm(), 0, TensorLayout.Sparsity.SPARSE) |
| 50 |
tl.init([index_map, index_map], TensorLayout.Ghosts.UNGHOSTED) |
| 51 |
sp = tl.sparsity_pattern() |
| 52 |
sp.init([index_map, index_map]) |
| 53 |
SparsityPatternBuilder.build(sp, mesh, [dm, dm], |
| 54 |
True, False, False, False, |
| 55 |
False, init=False, finalize=True) |
| 56 |
|
| 57 |
sp.str(False) |
| 58 |
sp.str(True) |
| 59 |
|
| 60 |
|
| 61 |
def test_insert_local(mesh, V): |
| 62 |
dm = V.dofmap() |
| 63 |
index_map = dm.index_map() |
| 64 |
|
| 65 |
# Build sparse tensor layout |
| 66 |
tl = TensorLayout(mesh.mpi_comm(), 0, TensorLayout.Sparsity.SPARSE) |
| 67 |
tl.init([index_map, index_map], TensorLayout.Ghosts.UNGHOSTED) |
| 68 |
sp = tl.sparsity_pattern() |
| 69 |
sp.init([index_map, index_map]) |
| 70 |
|
| 71 |
primary_dim_entries = [0, 1, 2] |
| 72 |
primary_codim_entries = [0, 1, 2] |
| 73 |
entries = np.array([primary_dim_entries, primary_codim_entries], dtype=np.intc) |
| 74 |
sp.insert_local(entries) |
| 75 |
|
| 76 |
sp.apply() |
| 77 |
|
| 78 |
assert len(primary_dim_entries) * len(primary_codim_entries) == sp.num_nonzeros() |
| 79 |
|
| 80 |
nnz_d = sp.num_nonzeros_diagonal() |
| 81 |
for local_row in range(len(nnz_d)): |
| 82 |
assert nnz_d[local_row] == (len(primary_codim_entries) if local_row in primary_dim_entries else 0) |
| 83 |
|
| 84 |
|
| 85 |
def test_insert_global(mesh, V): |
| 86 |
dm = V.dofmap() |
| 87 |
index_map = dm.index_map() |
| 88 |
local_range = index_map.local_range() |
| 89 |
|
| 90 |
# Build sparse tensor layout |
| 91 |
tl = TensorLayout(mesh.mpi_comm(), 0, TensorLayout.Sparsity.SPARSE) |
| 92 |
tl.init([index_map, index_map], TensorLayout.Ghosts.UNGHOSTED) |
| 93 |
sp = tl.sparsity_pattern() |
| 94 |
sp.init([index_map, index_map]) |
| 95 |
|
| 96 |
# Primary dim (row) entries need to be local to the process, so we ensure |
| 97 |
# they're in the local range of the index map |
| 98 |
primary_dim_local_entries = np.array([0, 1, 2], dtype=np.intc) |
| 99 |
primary_dim_entries = primary_dim_local_entries + local_range[0] |
| 100 |
|
| 101 |
# The codim (column) entries will be added to the same global entries |
| 102 |
# on each process. |
| 103 |
primary_codim_entries = np.array([0, 1, 2], dtype=np.intc) |
| 104 |
entries = np.array([primary_dim_entries, primary_codim_entries], dtype=np.intc) |
| 105 |
|
| 106 |
sp.insert_global(entries) |
| 107 |
sp.apply() |
| 108 |
|
| 109 |
nnz_d = sp.num_nonzeros_diagonal() |
| 110 |
nnz_od = sp.num_nonzeros_off_diagonal() |
| 111 |
|
| 112 |
rank = MPI.rank(mesh.mpi_comm()) |
| 113 |
size = MPI.size(mesh.mpi_comm()) |
| 114 |
|
| 115 |
# Tabulate on diagonal and off diagonal nnzs |
| 116 |
nnz_on_diagonal, nnz_off_diagonal = count_on_and_off_diagonal_nnz( |
| 117 |
primary_codim_entries, local_range) |
| 118 |
|
| 119 |
# Compare tabulated and sparsity pattern nnzs |
| 120 |
for local_row in range(len(nnz_d)): |
| 121 |
if local_range[0] <= local_row < local_range[1]: |
| 122 |
assert nnz_d[local_row] == (nnz_on_diagonal if local_row in primary_dim_local_entries else 0) |
| 123 |
else: |
| 124 |
assert nnz_od[local_row] == (nnz_off_diagonal if local_row in primary_dim_local_entries else 0) |
| 125 |
|
| 126 |
|
| 127 |
def test_insert_local_global(mesh, V): |
| 128 |
dm = V.dofmap() |
| 129 |
index_map = dm.index_map() |
| 130 |
local_range = index_map.local_range() |
| 131 |
|
| 132 |
# Build sparse tensor layout |
| 133 |
tl = TensorLayout(mesh.mpi_comm(), 0, TensorLayout.Sparsity.SPARSE) |
| 134 |
tl.init([index_map, index_map], TensorLayout.Ghosts.UNGHOSTED) |
| 135 |
sp = tl.sparsity_pattern() |
| 136 |
sp.init([index_map, index_map]) |
| 137 |
|
| 138 |
# Primary dim (row) entries need to be local to the process, so we ensure |
| 139 |
# they're in the local range of the index map |
| 140 |
primary_dim_local_entries = np.array([0, 1, 2], dtype=np.intc) |
| 141 |
primary_dim_entries = primary_dim_local_entries |
| 142 |
|
| 143 |
# The codim (column) entries will be added to the same global entries |
| 144 |
# on each process. |
| 145 |
primary_codim_entries = np.array([0, 1, 2], dtype=np.intc) |
| 146 |
entries = np.array([primary_dim_entries, primary_codim_entries], dtype=np.intc) |
| 147 |
|
| 148 |
sp.insert_local_global(entries) |
| 149 |
sp.apply() |
| 150 |
|
| 151 |
nnz_d = sp.num_nonzeros_diagonal() |
| 152 |
nnz_od = sp.num_nonzeros_off_diagonal() |
| 153 |
|
| 154 |
rank = MPI.rank(mesh.mpi_comm()) |
| 155 |
size = MPI.size(mesh.mpi_comm()) |
| 156 |
|
| 157 |
# Tabulate on diagonal and off diagonal nnzs |
| 158 |
nnz_on_diagonal, nnz_off_diagonal = count_on_and_off_diagonal_nnz( |
| 159 |
primary_codim_entries, local_range) |
| 160 |
|
| 161 |
# Compare tabulated and sparsity pattern nnzs |
| 162 |
for local_row in range(len(nnz_d)): |
| 163 |
if local_range[0] <= local_row < local_range[1]: |
| 164 |
assert nnz_d[local_row] == (nnz_on_diagonal if local_row in primary_dim_local_entries else 0) |
| 165 |
else: |
| 166 |
assert nnz_od[local_row] == (nnz_off_diagonal if local_row in primary_dim_local_entries else 0) |