-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSolver.cpp
147 lines (113 loc) · 3.55 KB
/
LinearSolver.cpp
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
/**<!-------------------------------------------------------------------->
@file LinearSolver.cpp
@author Travis Fischer ([email protected])
@date Spring 2009
@brief
Wrapper around UMFPack for efficiently solving sparse linear systems of
the standard form Ax=b, including a cache of the factorization of A=LU such
that repeated invocations of solving Ax=b with the same A=LU can be
performed in nearly linear time by back-substitution of LUx=b.
@note for more info on UMFPack (the internal linear solver), see
http://www.cise.ufl.edu/research/sparse/umfpack
<!-------------------------------------------------------------------->**/
#include "LinearSolver.h"
#include <UMFPACK/umfpack.h>
#include <algorithm>
struct SparseMatrixElement {
unsigned row;
unsigned col;
double value;
inline SparseMatrixElement() {
row = 0;
col = 0;
value = 0;
}
inline SparseMatrixElement(unsigned r, unsigned c, double v) {
row = r;
col = c;
value = v;
}
inline bool operator< (const SparseMatrixElement& e) const {
if (col < e.col)
return true;
if (col > e.col)
return false;
return (row < e.row);
}
inline bool operator==(const SparseMatrixElement &e) const {
return (row == e.row && col == e.col && value == e.value);
}
};
SparseMatrix LinearSolver::s_dummyA(2, 2);
void LinearData::clean() {
umfpack_di_free_symbolic(&symbolic);
umfpack_di_free_numeric(&numeric);
safeDeleteArray(Ap);
safeDeleteArray(Ai);
safeDeleteArray(Ax);
}
void LinearData::init(unsigned n_, unsigned nElements_) {
clean();
n = n_;
nElements = nElements_;
Ap = new int[n + 1];
Ai = new int[nElements];
Ax = new double[nElements];
}
bool LinearData::init(const SparseMatrix &A) {
const SparseArray &elements = A.getData();
vector<SparseMatrixElement> temp;
SparseColArrayConstIterator j;
SparseArrayConstIterator i;
for(i = elements.begin(); i != elements.end(); ++i) {
for(j = i->second.begin(); j != i->second.end(); ++j) {
SparseMatrixElement element(i->first, j->first, j->second);
temp.push_back(element);
}
}
std::sort(temp.begin(), temp.end());
init(A.getN(), temp.size());
Ap[0] = 0;
Ap[1] = 0;
unsigned cur = 1;
unsigned index = 0;
// convert the sparse data into the format umfpack expects
for(unsigned i = 0; i < temp.size(); ++i) {
const SparseMatrixElement &element = temp[i];
Ax[index] = element.value;
Ai[index] = element.row;
++index;
if (element.col == cur) {
++cur;
Ap[cur] = Ap[cur - 1];
}
++Ap[cur];
}
int status = UMFPACK_OK;
bool ret = true;
status = umfpack_di_symbolic(n, n, Ap, Ai, Ax, &symbolic, NULL, NULL);
if (status != UMFPACK_OK) {
ret = false;
cerr << "Bad symbolic" << endl;
}
status = umfpack_di_numeric(Ap, Ai, Ax, symbolic, &numeric, NULL, NULL);
if (status != UMFPACK_OK) {
ret = false;
cerr << "Bad numeric" << endl;
}
return ret;
}
void LinearSolver::setA(SparseMatrix &A) {
m_A = A;
m_dirty = true;
ASSERT(m_A.isSquare());
}
bool LinearSolver::solve(const double *b, double *x) {
if (m_dirty) {
if (!m_data.init(m_A))
return false;
m_dirty = false;
}
return (umfpack_di_solve(UMFPACK_A, m_data.Ap, m_data.Ai, m_data.Ax, x, b,
m_data.numeric, NULL, NULL) == UMFPACK_OK);
}