This repository was archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathIterativeSolverTestAbstract.java
More file actions
170 lines (142 loc) · 4.29 KB
/
IterativeSolverTestAbstract.java
File metadata and controls
170 lines (142 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (C) 2003-2006 Bjørn-Ove Heimsund
*
* This file is part of MTJ.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package no.uib.cipr.matrix.sparse;
import no.uib.cipr.matrix.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* Test of the iterative solvers and preconditioners
*/
public abstract class IterativeSolverTestAbstract {
/**
* Number of times to repeat tests
*/
private int repeat = 5;
/**
* Sizes of the system matrix
*/
protected int max = 50, bmax = 10;
/**
* Numerical tolerance
*/
protected double tol = 1e-4;
/**
* Diagonal shift for singularity handling
*/
protected double shift = 100;
/**
* Square system matrix
*/
protected Matrix A;
/**
* Right hand side, right hand for transpose system, and the solution vector
* in both cases
*/
protected Vector b, bt, x;
/**
* Stores the data of x
*/
protected double[] xd;
/**
* Iterative solver to use
*/
protected IterativeSolver solver;
/**
* Preconditioner to use
*/
protected Preconditioner M;
@Before
public void setUp() throws Exception {
createMatrix();
int n = A.numRows();
x = Matrices.random(n);
b = Matrices.random(n);
bt = Matrices.random(n);
// Create solver and preconditioner
createSolver();
if (M == null)
M = solver.getPreconditioner();
M.setMatrix(A);
// Compute the correct right hand sides
b = A.mult(x, b);
bt = A.transMult(x, bt);
// Store x for later. It is overwritten
xd = Matrices.getArray(x);
// Randomize the inital solution vector
Matrices.random(x);
}
protected abstract void createSolver() throws Exception;
protected void createMatrix() throws Exception {
// Create an arbitrary matrix
int n = Utilities.getInt(1, max);
int b = Utilities.getInt(Math.min(bmax, n));
A = new FlexCompRowMatrix(n, n);
Utilities.rowPopulate(A, b);
// Make it non-singular
addDiagonal(A, shift);
DenseLU lu = DenseLU.factorize(A);
while (lu.isSingular()) {
addDiagonal(A, shift);
lu = DenseLU.factorize(A);
}
}
protected void addDiagonal(Matrix A, double shift) {
int n = A.numRows(), m = A.numColumns();
for (int i = 0; i < Math.min(n, m); ++i)
A.add(i, i, shift);
}
@After
public void tearDown() throws Exception {
A = null;
b = bt = x = null;
xd = null;
solver = null;
}
@Test
public void testSolve() {
try {
solver.solve(A, b, x);
assertSolved();
} catch (IterativeSolverNotConvergedException e) {
fail("Solver did not converge: " + e.getReason() + ". Residual="
+ e.getResidual());
}
}
@Test
public void testRepeatSolve() {
try {
for (int i = 0; i < repeat; ++i) {
solver.solve(A, b, x);
assertSolved();
x = Matrices.random(A.numRows());
}
} catch (IterativeSolverNotConvergedException e) {
fail("Solver did not converge: " + e.getReason() + ". Residual="
+ e.getResidual());
}
}
protected void assertSolved() {
for (int i = 0; i < xd.length; ++i)
assertEquals(xd[i], x.get(i), tol);
}
}