PSCF v1.5.0
LuSolver.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "LuSolver.h"
9#include <gsl/gsl_linalg.h>
10#include <gsl/gsl_blas.h>
11#include <gsl/gsl_machine.h>
12#include <gsl/gsl_version.h>
13#include <cmath>
14#include <vector>
15
16// Since GSL 2.6, gsl_linalg_LU_decomp is recursive (see computeLU)
17#if defined(GSL_MAJOR_VERSION) && defined(GSL_MINOR_VERSION)
18#if GSL_MAJOR_VERSION > 2 || (GSL_MAJOR_VERSION == 2 && GSL_MINOR_VERSION >= 6)
19#define PSCF_LU_RECURSIVE
20#endif
21#endif
22
23namespace Pscf
24{
25
26#ifdef PSCF_LU_RECURSIVE
27 namespace {
28
29 /*
30 * LU decomposition with partial pivoting of an M x N matrix A,
31 * M >= N, P A = L U, where rows j and ipiv[j] were swapped in
32 * step j (as in LAPACK getrf).
33 *
34 * This is the recursive algorithm of gsl_linalg_LU_decomp of
35 * GSL 2.8, from the functions LU_decomp_L3 and LU_decomp_L2 of
36 * the GSL file linalg/lu.c,
37 *
38 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007, 2009
39 * Gerard Jungman, Brian Gough
40 * Copyright (C) 2019, 2021 Patrick Alken
41 *
42 * distributed under the GNU General Public License, version 3 or
43 * later, like PSCF. It does the same floating point operations,
44 * except that the decomposition is completed past a zero pivot:
45 * GSL 2.8 returns from each level of the recursion as soon as a
46 * block reports a zero pivot, leaving the later columns
47 * unfactored and their row swaps unset or unshifted (GSL 2.6 and
48 * 2.7 divide by the zero pivot, which fills the remaining
49 * submatrix with NaN).
50 */
51 void decomposeLU(gsl_matrix* A, size_t* ipiv)
52 {
53 const size_t M = A->size1;
54 const size_t N = A->size2;
55 size_t i, j;
56 if (N <= 24) {
57 // Unblocked algorithm (LAPACK getf2)
58 for (j = 0; j < N; ++j) {
59 gsl_vector_view c = gsl_matrix_subcolumn(A, j, j, M - j);
60 ipiv[j] = j + gsl_blas_idamax(&c.vector);
61 if (gsl_matrix_get(A, ipiv[j], j) != 0.0) {
62 if (ipiv[j] != j) gsl_matrix_swap_rows(A, j, ipiv[j]);
63 double ajj = gsl_matrix_get(A, j, j);
64 if (j < M - 1 && std::fabs(ajj) >= GSL_DBL_MIN) {
65 c = gsl_matrix_subcolumn(A, j, j + 1, M - j - 1);
66 gsl_blas_dscal(1.0 / ajj, &c.vector);
67 } else {
68 for (i = j + 1; i < M; ++i) {
69 *gsl_matrix_ptr(A, i, j) /= ajj;
70 }
71 }
72 }
73 if (j < N - 1) {
74 gsl_vector_view l, u;
75 l = gsl_matrix_subcolumn(A, j, j + 1, M - j - 1);
76 u = gsl_matrix_subrow(A, j, j + 1, N - j - 1);
77 gsl_matrix_view A22;
78 A22 = gsl_matrix_submatrix(A, j+1, j+1, M-j-1, N-j-1);
79 gsl_blas_dger(-1.0, &l.vector, &u.vector, &A22.matrix);
80 }
81 }
82 return;
83 }
84
85 // Recursion: columns 0 to N1 - 1, then the submatrix A22
86 const size_t N1 = ((N + 8) / 16) * 8;
87 const size_t N2 = N - N1;
88 gsl_matrix_view A11 = gsl_matrix_submatrix(A, 0, 0, N1, N1);
89 gsl_matrix_view A12 = gsl_matrix_submatrix(A, 0, N1, N1, N2);
90 gsl_matrix_view A21 = gsl_matrix_submatrix(A, N1, 0, M - N1, N1);
91 gsl_matrix_view A22 = gsl_matrix_submatrix(A, N1, N1, M - N1, N2);
92 gsl_matrix_view AL = gsl_matrix_submatrix(A, 0, 0, M, N1);
93 gsl_matrix_view AR = gsl_matrix_submatrix(A, 0, N1, M, N2);
94 decomposeLU(&AL.matrix, ipiv);
95 for (i = 0; i < N1; ++i) {
96 if (ipiv[i] != i) gsl_matrix_swap_rows(&AR.matrix, i, ipiv[i]);
97 }
98 gsl_blas_dtrsm(CblasLeft, CblasLower, CblasNoTrans, CblasUnit,
99 1.0, &A11.matrix, &A12.matrix);
100 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, -1.0, &A21.matrix,
101 &A12.matrix, 1.0, &A22.matrix);
102 decomposeLU(&A22.matrix, ipiv + N1);
103 for (i = N1; i < N; ++i) {
104 if (ipiv[i] != i - N1) {
105 gsl_matrix_swap_rows(&A21.matrix, i - N1, ipiv[i]);
106 }
107 ipiv[i] += N1;
108 }
109 }
110
111 }
112#endif
113
115 : luPtr_(0),
116 permPtr_(0),
117 n_(0)
118 {
119 // Initialize gs_vector b_
120 b_.size = 0;
121 b_.stride = 1;
122 b_.data = 0;
123 b_.block = 0;
124 b_.owner = 0;
125
126 // Initialize gsl_vector x_
127 x_.size = 0;
128 x_.stride = 1;
129 x_.data = 0;
130 x_.block = 0;
131 x_.owner = 0;
132 }
133
135 {
136 if (n_ > 0) {
137 if (luPtr_) gsl_matrix_free(luPtr_);
138 if (gMatInverse_) gsl_matrix_free(gMatInverse_);
139 if (permPtr_) gsl_permutation_free(permPtr_);
140 }
141 }
142
143 /*
144 * Allocate memory and set n.
145 */
147 {
148 UTIL_CHECK(n > 0);
149 UTIL_CHECK(n_ == 0);
150 UTIL_CHECK(luPtr_ == 0);
151 UTIL_CHECK(permPtr_ == 0);
152 luPtr_ = gsl_matrix_alloc(n, n);
153 gMatInverse_ = gsl_matrix_alloc(n, n);
154 permPtr_ = gsl_permutation_alloc(n);
155 b_.size = n;
156 x_.size = n;
157 n_ = n;
158 }
159
160 /*
161 * Compute the LU decomposition for later use.
162 */
164 {
165 UTIL_CHECK(n_ > 0);
166 UTIL_CHECK(A.capacity1() == n_);
167 UTIL_CHECK(A.capacity2() == n_);
168
169 int i, j;
170 int k = 0;
171 for (i = 0; i < n_; ++i) {
172 for (j = 0; j < n_; ++j) {
173 luPtr_->data[k] = A(i,j);
174 ++k;
175 }
176 }
177
178 // LU decomposition with partial pivoting, P A = L U, in the
179 // format of gsl_linalg_LU_decomp, completed also for a singular
180 // matrix, with a zero pivot that isSingular() reports. Since GSL
181 // 2.6, gsl_linalg_LU_decomp decomposes matrices with more than
182 // 24 columns recursively and does not complete the decomposition
183 // of a singular matrix, so its algorithm is used here (function
184 // decomposeLU above); for a nonsingular matrix, the result is that
185 // of gsl_linalg_LU_decomp, bit for bit. GSL 2.5 and earlier use
186 // an unblocked algorithm, which continues past a zero pivot.
187#ifdef PSCF_LU_RECURSIVE
188 std::vector<size_t> ipiv(n_);
189 decomposeLU(luPtr_, ipiv.data());
190 gsl_permutation_init(permPtr_);
191 signum_ = 1;
192 for (i = 0; i < n_; ++i) {
193 if (ipiv[i] != (size_t) i) {
194 gsl_permutation_swap(permPtr_, i, ipiv[i]);
195 signum_ = -signum_;
196 }
197 }
198#else
199 gsl_linalg_LU_decomp(luPtr_, permPtr_, &signum_);
200#endif
201 }
202
203 /*
204 * Solve Ax = b.
205 */
207 {
208 UTIL_CHECK(n_ > 0);
209 UTIL_CHECK(b.capacity() == n_);
210 UTIL_CHECK(x.capacity() == n_);
211
212 // Associate gsl_vectors b_ and x_ with Arrays b and x
213 b_.data = b.cArray();
214 x_.data = x.cArray();
215
216 // Solve system of equations
217 gsl_linalg_LU_solve(luPtr_, permPtr_, &b_, &x_);
218
219 // Destroy temporary associations
220 b_.data = 0;
221 x_.data = 0;
222 }
223
224 /*
225 * Does the LU decomposition have a zero pivot (GSL's singular test)?
226 */
228 {
229 UTIL_CHECK(n_ > 0);
230 for (int i = 0; i < n_; ++i) {
231 if (gsl_matrix_get(luPtr_, i, i) == 0.0) return true;
232 }
233 return false;
234 }
235
236 /*
237 * Solve Ax = b by a basic solution, with zero pivots allowed.
238 */
240 const
241 {
242 UTIL_CHECK(n_ > 0);
243 UTIL_CHECK(b.capacity() == n_);
244 UTIL_CHECK(x.capacity() == n_);
245 UTIL_CHECK(b.cArray() != x.cArray());
246
247 int i, j;
248 double sum, pivot;
249
250 // Row permutation, x = P b
251 for (i = 0; i < n_; ++i) {
252 x[i] = b[gsl_permutation_get(permPtr_, i)];
253 }
254
255 // Forward substitution, L y = P b (L has a unit diagonal)
256 for (i = 1; i < n_; ++i) {
257 sum = x[i];
258 for (j = 0; j < i; ++j) {
259 sum -= gsl_matrix_get(luPtr_, i, j)*x[j];
260 }
261 x[i] = sum;
262 }
263
264 // Back substitution, U x = y, with x[i] = 0 for a zero pivot
265 for (i = n_ - 1; i >= 0; --i) {
266 pivot = gsl_matrix_get(luPtr_, i, i);
267 if (pivot == 0.0) {
268 x[i] = 0.0;
269 } else {
270 sum = x[i];
271 for (j = i + 1; j < n_; ++j) {
272 sum -= gsl_matrix_get(luPtr_, i, j)*x[j];
273 }
274 x[i] = sum/pivot;
275 }
276 }
277 }
278
279 /*
280 * Find inverse
281 */
283 {
284 UTIL_CHECK(n_ > 0);
285
286 gMatInverse_->data = inv.cArray();
287 gsl_linalg_LU_invert(luPtr_, permPtr_, gMatInverse_);
288
289 }
290
291}
void inverse(Matrix< double > &inv)
Compute inverse of matrix A.
Definition LuSolver.cpp:282
~LuSolver()
Destructor.
Definition LuSolver.cpp:134
void allocate(int n)
Allocate memory.
Definition LuSolver.cpp:146
bool isSingular() const
Does the last LU decomposition have a zero pivot?
Definition LuSolver.cpp:227
void solveBasic(Array< double > const &b, Array< double > &x) const
Solve Ax = b by a basic solution, also for a singular matrix A.
Definition LuSolver.cpp:239
void solve(Array< double > &b, Array< double > &x)
Solve Ax = b for known b to compute x.
Definition LuSolver.cpp:206
LuSolver()
Constructor.
Definition LuSolver.cpp:114
void computeLU(const Matrix< double > &A)
Compute the LU decomposition for later use.
Definition LuSolver.cpp:163
Array container class template.
Definition Array.h:41
Data * cArray()
Return a pointer to the underlying C array (non-const data).
Definition Array.h:222
int capacity() const
Return allocated size.
Definition Array.h:250
Two-dimensional array container template (interface).
Definition Matrix.h:32
Data * cArray()
Return pointer to underlying one-dimensional C array.
Definition Matrix.h:174
int capacity2() const
Get number of columns (range of the second array index).
Definition Matrix.h:139
int capacity1() const
Get number of rows (range of the first array index).
Definition Matrix.h:132
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.