| 1 |
# SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
# SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
|
| 4 |
import tempfile,os |
| 5 |
import dune.common |
| 6 |
from dune.istl import blockVector, BlockVector, bcrsMatrix, BCRSMatrix, SeqJacobi, CGSolver, BuildMode |
| 7 |
|
| 8 |
def identity(n, buildMode=BuildMode.random): |
| 9 |
if buildMode is BuildMode.random: |
| 10 |
mat = bcrsMatrix((n, n), n, BuildMode.random) |
| 11 |
for i in range(n): |
| 12 |
mat.setRowSize(i, 1) |
| 13 |
mat.endRowSizes() |
| 14 |
for i in range(n): |
| 15 |
mat.setIndices(i, [i]) |
| 16 |
mat.endIndices() |
| 17 |
for i in range(n): |
| 18 |
mat[i, i] = 1 |
| 19 |
return mat |
| 20 |
elif buildMode is BuildMode.implicit: |
| 21 |
mat = bcrsMatrix((n, n), n, 0, BuildMode.implicit) |
| 22 |
for i in range(n): |
| 23 |
mat[i, i] = 1 |
| 24 |
mat.compress() |
| 25 |
return mat |
| 26 |
else: |
| 27 |
raise Exception("buildMode " + str(buildMode) + " not supported by identity") |
| 28 |
|
| 29 |
def isIdentity(mat): |
| 30 |
if mat.rows != mat.cols: |
| 31 |
return False |
| 32 |
for i in range(mat.rows): |
| 33 |
if not (i, i) in mat: |
| 34 |
return False |
| 35 |
for j in range(mat.cols): |
| 36 |
if (i, j) in mat and mat[i, j] != (1 if i == j else 0): |
| 37 |
return False |
| 38 |
return True |
| 39 |
|
| 40 |
mat = bcrsMatrix((5, 5), 5, BuildMode.random) |
| 41 |
if mat.shape != (5, 5) or mat.nonZeroes != 5 or mat.buildMode != BuildMode.random: |
| 42 |
raise Exception("Matrix does not return constructor arguments correctly") |
| 43 |
|
| 44 |
mat = bcrsMatrix((5, 5), 5, BuildMode.implicit, blockType=(2,3)) |
| 45 |
if mat.shape != (5, 5) or mat.nonZeroes != 5 or mat.buildMode != BuildMode.implicit \ |
| 46 |
or mat[0,0].rows != 2 or mat[0,0].cols != 3: |
| 47 |
raise Exception("Matrix does not return constructor arguments correctly") |
| 48 |
|
| 49 |
Matrix = BCRSMatrix((2,3)) |
| 50 |
mat = Matrix((5, 5), 5, Matrix.BuildMode.implicit) |
| 51 |
if mat.shape != (5, 5) or mat.nonZeroes != 5 or mat.buildMode != BuildMode.implicit \ |
| 52 |
or mat[0,0].rows != 2 or mat[0,0].cols != 3: |
| 53 |
raise Exception("Matrix does not return constructor arguments correctly") |
| 54 |
|
| 55 |
# the following not working yet since the matrix market exported fails |
| 56 |
# BMatrix = BCRSMatrix(Matrix) |
| 57 |
# mat = BMatrix((5,5), 5, BuildMode.implicit) |
| 58 |
|
| 59 |
# generate a temporary directory to test 'store', 'load': |
| 60 |
|
| 61 |
with tempfile.TemporaryDirectory() as tmpDir: |
| 62 |
# store identity matrix |
| 63 |
mat = identity(5) |
| 64 |
if not isIdentity(mat): |
| 65 |
raise Exception("Identity matrix not setup correctly") |
| 66 |
mat.store(os.path.join(tmpDir,"bcrsmatrix.mm"), "matrixmarket") |
| 67 |
|
| 68 |
for i, row in mat.enumerate: |
| 69 |
for j, col in row.enumerate: |
| 70 |
if i != j: |
| 71 |
raise Exception("Wrong sparsity pattern") |
| 72 |
if col != 1: |
| 73 |
raise Exception("Diagonal entry is not 1") |
| 74 |
|
| 75 |
mat = identity(5, BuildMode.implicit) |
| 76 |
if not isIdentity(mat): |
| 77 |
raise Exception("Identity matrix not setup correctly") |
| 78 |
|
| 79 |
# manipulate diagonal to 2 |
| 80 |
for i in range(mat.rows): |
| 81 |
mat[i, i] *= 2 |
| 82 |
if isIdentity(mat): |
| 83 |
raise Exception("Matrix not manipulated") |
| 84 |
|
| 85 |
# reload identity matrix |
| 86 |
mat = bcrsMatrix() |
| 87 |
mat.load(os.path.join(tmpDir,"bcrsmatrix.mm"), "matrixmarket") |
| 88 |
if not isIdentity(mat): |
| 89 |
raise Exception("Matrix not loaded correctly") |
| 90 |
|
| 91 |
# store in matlab format |
| 92 |
mat.store(os.path.join(tmpDir,"bcrsmatrix.txt"), "matlab") |
| 93 |
|
| 94 |
# solve trivial linear system |
| 95 |
x = blockVector(5) |
| 96 |
for i in range(5): |
| 97 |
x[i] = i+1 # float(i+1) # dune.common.FieldVector([i+1]) |
| 98 |
|
| 99 |
y1, y2 = blockVector(5), blockVector(5) |
| 100 |
mat.mv(x, y1) |
| 101 |
mat.asLinearOperator().apply(x, y2) |
| 102 |
|
| 103 |
if (y1 - y2).two_norm > 1e-12: |
| 104 |
raise Exception("mat.mv != mat.asLinearOperator().apply") |
| 105 |
|
| 106 |
S = CGSolver(mat.asLinearOperator(), SeqJacobi(mat), 1e-10) |
| 107 |
z = blockVector(5) |
| 108 |
|
| 109 |
_, _, converged, _, _ = S(z, y1) |
| 110 |
if not converged: |
| 111 |
raise Exception("CGSolver has not converged") |
| 112 |
if (z - x).two_norm > 1e-8: |
| 113 |
raise Exception("CGSolver unable to solve identity") |
| 114 |
vec = dune.common.FieldVector([1]) |
| 115 |
z2 = BlockVector(vec,5) |
| 116 |
|
| 117 |
S2=CGSolver(mat.asLinearOperator(),SeqJacobi(mat),1e-10) |
| 118 |
|
| 119 |
_, _, converged2, _, _ = S2(z2, y2) |
| 120 |
if not converged2: |
| 121 |
raise Exception("CGSolver has not converged") |
| 122 |
if (z2 - x).two_norm > 1e-8: |
| 123 |
raise Exception("CGSolver unable to solve identity") |
| 124 |
|
| 125 |
s = "(" + ", ".join(str(v) for v in x) + ")" |
| 126 |
|
| 127 |
str_x = "(" |
| 128 |
for i in range(0,5): |
| 129 |
str_x = str_x + "(" + "{0:.6f}".format(x[i][0]) + "), " |
| 130 |
str_x = str_x[:-2] |
| 131 |
str_x = str_x +")" |
| 132 |
|
| 133 |
if str_x != s: |
| 134 |
raise Exception(str(x) + " = str(x) != " + s) |
| 135 |
|
| 136 |
q=x.copy() |
| 137 |
q+=q |
| 138 |
for i in range(0,5): |
| 139 |
assert(q[i][0] == 2*x[i][0]) |
| 140 |
|
| 141 |
q-=x |
| 142 |
for i in range(0,5): |
| 143 |
assert(q[i][0] == x[i][0]) |