IFEM 90A354
SparseMatrix.h
Go to the documentation of this file.
1// $Id$
2//==============================================================================
12//==============================================================================
13
14#ifndef _SPARSE_MATRIX_H
15#define _SPARSE_MATRIX_H
16
17#include "SystemMatrix.h"
18#include <map>
19#include <set>
20#include <array>
21
22typedef std::pair<size_t,size_t> IJPair;
23typedef std::map<IJPair,Real> ValueMap;
24typedef ValueMap::const_iterator ValueIter;
25
26struct SuperLUdata;
27
28
38{
39public:
41 enum SparseSolver { NONE, SUPERLU, S_A_M_G, UMFPACK };
42
44 SparseMatrix(SparseSolver eqSolver = NONE, int nt = 1);
46 explicit SparseMatrix(size_t m, size_t n = 0, SparseSolver eqsolver = NONE);
48 SparseMatrix(const SparseMatrix& B);
50 virtual ~SparseMatrix();
51
53 virtual LinAlg::MatrixType getType() const;
54
56 virtual SystemMatrix* copy() const { return new SparseMatrix(*this); }
57
61 virtual bool lockPattern(bool doLock);
62
69 void resize(size_t r, size_t c = 0, bool forceEditable = false);
70
73 bool redim(size_t r, size_t c);
74
76 size_t rows() const { return nrow; }
78 size_t cols() const { return ncol; }
80 size_t size() const { return editable ? elem.size() : A.size(); }
81
84 virtual size_t dim(int idim = 1) const;
85
87 Real& operator()(size_t r, size_t c);
89 const Real& operator()(size_t r, size_t c) const;
90
92 const ValueMap& getValues() const { return elem; }
93
95 void printSparsity(std::ostream& os) const;
96
99 void printFull(std::ostream& os) const;
100
102 virtual void dump(std::ostream& os, LinAlg::StorageFormat format,
103 const char* label);
104
113 virtual void initAssembly(const SAM& sam, char preAssembly);
114
118 void preAssemble(const SAM& sam, bool delayLocking);
119
123 virtual void preAssemble(const std::vector<IntVec>& MMNPC, size_t nel = 0);
124
126 virtual void compressPattern();
127
129 virtual void init();
130
136 virtual bool assemble(const Matrix& eM, const SAM& sam, int e);
146 virtual bool assemble(const Matrix& eM, const SAM& sam,
147 SystemVector& B, int e);
157 virtual bool assemble(const Matrix& eM, const SAM& sam,
158 SystemVector& B, const IntVec& meq);
159
164 virtual bool assemble(const Matrix& eM, const IntVec& meq);
165
175 bool assembleCol(const RealArray& V, const SAM& sam, int n, size_t col);
176
186 bool assembleCol(Real val, const SAM& sam, int n, size_t col)
187 {
188 return this->assembleCol(RealArray(1,val),sam,n,col);
189 }
190
195 virtual bool augment(const SystemMatrix& B, size_t r0, size_t c0);
196
199 virtual bool truncate(Real threshold = Real(1.0e-16));
200
202 virtual void mult(Real alpha);
203
207 virtual bool add(const SystemMatrix& B, Real alpha);
208
210 virtual bool add(Real sigma, int ieq);
211
213 virtual bool multiply(const SystemVector& B, SystemVector& C) const;
214
219 virtual bool solve(SystemVector& B, Real* rc = nullptr);
220
224 void calcCSR(IntVec& iA, IntVec& jA) const;
225
229 bool split(std::array<SparseMatrix,4>& Asub, const IntVec& meqn2) const;
230
234 bool getColumn(size_t c, Vector& col) const;
235
237 virtual Real Linfnorm() const;
238
240 bool isFactored() const { return factored; }
241
242protected:
244 bool optimiseRows(bool transposed = false);
245
247 bool optimiseCols();
248
251 bool optimiseCols(const std::vector< std::set<int> >& dofc);
252
255 bool solveSAMG(Vector& B);
256
259 bool solveSLU(Vector& B);
260
264 bool solveSLUx(Vector& B, Real* rcond);
265
269 bool solveUMF(Vector& B, Real* rcond);
270
272 virtual std::ostream& write(std::ostream& os) const;
273
274public:
275 static bool printSLUstat;
276
277private:
283
284 size_t nrow;
285 size_t ncol;
286
292
293protected:
294 bool factored;
295
299};
300
301#endif
std::vector< int > IntVec
General integer vector.
Definition ASMbase.h:25
#define Real
The floating point type to use.
Definition ImmersedBoundaries.h:18
std::vector< Real > RealArray
A real-valued array without algebraic operations.
Definition ImmersedBoundaries.h:32
ValueMap::const_iterator ValueIter
Iterator over matrix elements.
Definition SparseMatrix.h:24
std::pair< size_t, size_t > IJPair
1-based matrix indices
Definition SparseMatrix.h:22
std::map< IJPair, Real > ValueMap
Index to matrix value mapping.
Definition SparseMatrix.h:23
General representation of system matrices and vectors.
This class contains data and functions for the assembly of FE matrices.
Definition SAM.h:39
Class for representing a system matrix on an unstructured sparse form.
Definition SparseMatrix.h:38
virtual bool truncate(Real threshold=Real(1.0e-16))
Truncates all small matrix elements to zero.
Definition SparseMatrix.C:483
virtual size_t dim(int idim=1) const
Returns the dimension of the system matrix.
Definition SparseMatrix.C:256
void printFull(std::ostream &os) const
Print the matrix in full rectangular form.
Definition SparseMatrix.C:450
virtual LinAlg::MatrixType getType() const
Returns the matrix type.
Definition SparseMatrix.C:190
bool optimiseRows(bool transposed=false)
Converts the matrix to an optimized row-oriented format.
Definition SparseMatrix.C:876
size_t ncol
Number of matrix columns.
Definition SparseMatrix.h:285
char editable
Flag for the editability of the matrix elements.
Definition SparseMatrix.h:282
size_t nrow
Number of matrix rows.
Definition SparseMatrix.h:284
size_t size() const
Query total matrix size in terms of number of non-zero elements.
Definition SparseMatrix.h:80
SparseSolver solver
Which equation solver to use.
Definition SparseMatrix.h:288
void calcCSR(IntVec &iA, IntVec &jA) const
Calculates compressed-sparse-row arrays from the element map.
Definition SparseMatrix.C:1427
Vector A
Stores the nonzero matrix elements.
Definition SparseMatrix.h:298
void printSparsity(std::ostream &os) const
Print sparsity pattern - for inspection purposes.
Definition SparseMatrix.C:417
virtual bool solve(SystemVector &B, Real *rc=nullptr)
Solves the linear system of equations for a given right-hand-side.
Definition SparseMatrix.C:1026
void preAssemble(const SAM &sam, bool delayLocking)
Initializes the element sparsity pattern based on node connections.
Definition SparseMatrix.C:731
virtual std::ostream & write(std::ostream &os) const
Writes the system matrix to the given output stream.
Definition SparseMatrix.C:398
bool solveSLU(Vector &B)
Invokes the SuperLU equation solver for a given right-hand-side.
Definition SparseMatrix.C:1050
virtual ~SparseMatrix()
The destructor frees the dynamically allocated arrays.
Definition SparseMatrix.C:180
virtual void initAssembly(const SAM &sam, char preAssembly)
Initializes the element assembly process.
Definition SparseMatrix.C:717
virtual SystemMatrix * copy() const
Creates a copy of the system matrix and returns a pointer to it.
Definition SparseMatrix.h:56
virtual void compressPattern()
Compresses the sparsity pattern.
Definition SparseMatrix.C:789
virtual Real Linfnorm() const
Returns the L-infinity norm of the matrix.
Definition SparseMatrix.C:1405
bool solveSAMG(Vector &B)
Invokes the SAMG equation solver for a given right-hand-side.
Definition SparseMatrix.C:1339
virtual bool augment(const SystemMatrix &B, size_t r0, size_t c0)
Augments a similar matrix symmetrically to the current matrix.
Definition SparseMatrix.C:460
static bool printSLUstat
Print solution statistics for SuperLU?
Definition SparseMatrix.h:275
bool solveUMF(Vector &B, Real *rcond)
Invokes the UMFPACK equation solver for a given right-hand-side.
Definition SparseMatrix.C:1299
Real & operator()(size_t r, size_t c)
Index-1 based element access.
Definition SparseMatrix.C:267
void * umfSymbolic
Symbolically factored matrix for UMFPACK.
Definition SparseMatrix.h:291
void resize(size_t r, size_t c=0, bool forceEditable=false)
Resizes the matrix to dimension .
Definition SparseMatrix.C:204
bool split(std::array< SparseMatrix, 4 > &Asub, const IntVec &meqn2) const
Splits the matrix into four sub-matrices, A11, A12, A21, A22.
Definition SparseMatrix.C:1444
virtual void dump(std::ostream &os, LinAlg::StorageFormat format, const char *label)
Dumps the system matrix on a specified format.
Definition SparseMatrix.C:341
virtual bool multiply(const SystemVector &B, SystemVector &C) const
Performs the matrix-vector multiplication C = *this * B.
Definition SparseMatrix.C:578
bool factored
Set to true when the matrix is factorized.
Definition SparseMatrix.h:294
size_t cols() const
Query number of matrix columns.
Definition SparseMatrix.h:78
ValueMap elem
Stores nonzero matrix elements with index pairs.
Definition SparseMatrix.h:287
const ValueMap & getValues() const
For traversal of the non-zero elements of an editable matrix.
Definition SparseMatrix.h:92
bool optimiseCols()
Converts the matrix to an optimized column-oriented format.
Definition SparseMatrix.C:936
bool assembleCol(const RealArray &V, const SAM &sam, int n, size_t col)
Adds a nodal vector into columns of a non-symmetric sparse matrix.
Definition SparseMatrix.C:862
IntVec JA
Specifies column/row index of each nonzero element.
Definition SparseMatrix.h:297
size_t rows() const
Query number of matrix rows.
Definition SparseMatrix.h:76
SparseSolver
Available equation solvers for this matrix type.
Definition SparseMatrix.h:41
int numThreads
Number of threads to use for the SuperLU_MT solver.
Definition SparseMatrix.h:290
bool redim(size_t r, size_t c)
Resizes the matrix to dimension .
Definition SparseMatrix.C:237
virtual bool lockPattern(bool doLock)
Locks or unlocks the sparsity pattern.
Definition SparseMatrix.C:196
bool isFactored() const
Returns whether the matrix has been factored or not.
Definition SparseMatrix.h:240
IntVec IA
Identifies the beginning of each row or column.
Definition SparseMatrix.h:296
SuperLUdata * slu
Matrix data for the SuperLU equation solver.
Definition SparseMatrix.h:289
bool getColumn(size_t c, Vector &col) const
Extracts a specified column from the matrix.
Definition SparseMatrix.C:1500
virtual bool add(const SystemMatrix &B, Real alpha)
Adds a matrix with similar sparsity pattern to the current matrix.
Definition SparseMatrix.C:525
bool assembleCol(Real val, const SAM &sam, int n, size_t col)
Adds a scalar value into columns of a non-symmetric sparse matrix.
Definition SparseMatrix.h:186
bool solveSLUx(Vector &B, Real *rcond)
Invokes the SuperLU equation solver for a given right-hand-side.
Definition SparseMatrix.C:1147
virtual void init()
Initializes the matrix to zero assuming it is properly dimensioned.
Definition SparseMatrix.C:798
virtual void mult(Real alpha)
Multiplication with a scalar.
Definition SparseMatrix.C:515
virtual bool assemble(const Matrix &eM, const SAM &sam, int e)
Adds an element matrix into the associated system matrix.
Definition SparseMatrix.C:804
Base class for representing a system matrix on different formats.
Definition SystemMatrix.h:220
virtual bool solve(SystemVector &b, Real *rc=nullptr)=0
Solves the linear system of equations for a given right-hand-side.
Base class for representing a system vector on different formats.
Definition SystemMatrix.h:32
A vector class with some added algebraic operations.
Definition matrix.h:64
size_t size() const
Size of the vector.
Definition matrix.h:88
MatrixType
The available system matrix formats and associated solvers.
Definition LinAlgenums.h:22
StorageFormat
Enumeration of matrix storage formats.
Definition LinAlgenums.h:43
Data structures for the SuperLU equation solver.
Definition SparseMatrix.C:45