PSCF v1.1
LuSolver.cpp
1/*
2* PSCF - Polymer Self-Consistent Field Theory
3*
4* Copyright 2016 - 2022, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "LuSolver.h"
9#include <gsl/gsl_linalg.h>
10
11namespace Pscf
12{
13
15 : luPtr_(0),
16 permPtr_(0),
17 n_(0)
18 {
19 // Initialize gs_vector b_
20 b_.size = 0;
21 b_.stride = 1;
22 b_.data = 0;
23 b_.block = 0;
24 b_.owner = 0;
25
26 // Initialize gsl_vector x_
27 x_.size = 0;
28 x_.stride = 1;
29 x_.data = 0;
30 x_.block = 0;
31 x_.owner = 0;
32 }
33
35 {
36 if (n_ > 0) {
37 if (luPtr_) gsl_matrix_free(luPtr_);
38 if (gMatInverse_) gsl_matrix_free(gMatInverse_);
39 if (permPtr_) gsl_permutation_free(permPtr_);
40 }
41 }
42
43 /*
44 * Allocate memory and set n.
45 */
47 {
48 UTIL_CHECK(n > 0);
49 UTIL_CHECK(n_ == 0);
50 UTIL_CHECK(luPtr_ == 0);
51 UTIL_CHECK(permPtr_ == 0);
52 luPtr_ = gsl_matrix_alloc(n, n);
53 gMatInverse_ = gsl_matrix_alloc(n, n);
54 permPtr_ = gsl_permutation_alloc(n);
55 b_.size = n;
56 x_.size = n;
57 n_ = n;
58 }
59
60 /*
61 * Compute the LU decomposition for later use.
62 */
64 {
65 UTIL_CHECK(n_ > 0);
66 UTIL_CHECK(A.capacity1() == n_);
67 UTIL_CHECK(A.capacity2() == n_);
68
69 int i, j;
70 int k = 0;
71 for (i = 0; i < n_; ++i) {
72 for (j = 0; j < n_; ++j) {
73 luPtr_->data[k] = A(i,j);
74 ++k;
75 }
76 }
77 gsl_linalg_LU_decomp(luPtr_, permPtr_, &signum_);
78 }
79
80 /*
81 * Solve Ax = b.
82 */
84 {
85 UTIL_CHECK(n_ > 0);
86 UTIL_CHECK(b.capacity() == n_);
87 UTIL_CHECK(x.capacity() == n_);
88
89 // Associate gsl_vectors b_ and x_ with Arrays b and x
90 b_.data = b.cArray();
91 x_.data = x.cArray();
92
93 // Solve system of equations
94 gsl_linalg_LU_solve(luPtr_, permPtr_, &b_, &x_);
95
96 // Destroy temporary associations
97 b_.data = 0;
98 x_.data = 0;
99 }
100
101 /*
102 * Find inverse
103 */
105 {
106 UTIL_CHECK(n_ > 0);
107
108 gMatInverse_->data = inv.cArray();
109 gsl_linalg_LU_invert(luPtr_, permPtr_, gMatInverse_);
110
111 }
112
113}
void inverse(Matrix< double > &inv)
Compute inverse of matrix A.
Definition: LuSolver.cpp:104
~LuSolver()
Destructor.
Definition: LuSolver.cpp:34
void allocate(int n)
Allocate memory.
Definition: LuSolver.cpp:46
void solve(Array< double > &b, Array< double > &x)
Solve Ax = b for known b to compute x.
Definition: LuSolver.cpp:83
LuSolver()
Constructor.
Definition: LuSolver.cpp:14
void computeLU(const Matrix< double > &A)
Compute the LU decomposition for later use.
Definition: LuSolver.cpp:63
Array container class template.
Definition: Array.h:34
Data * cArray()
Return a pointer to the underlying C array.
Definition: Array.h:214
int capacity() const
Return allocated size.
Definition: Array.h:159
Two-dimensional array container template (abstract).
Definition: Matrix.h:32
Data * cArray()
Return pointer to underlying one-dimensional C array.
Definition: Matrix.h:178
int capacity2() const
Get number of columns (range of the second array index).
Definition: Matrix.h:143
int capacity1() const
Get number of rows (range of the first array index).
Definition: Matrix.h:136
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
C++ namespace for polymer self-consistent field theory (PSCF).