IFEM 90A354
SystemMatrix.h
Go to the documentation of this file.
1// $Id$
2//==============================================================================
12//==============================================================================
13
14#ifndef _SYSTEM_MATRIX_H
15#define _SYSTEM_MATRIX_H
16
17#include "MatVec.h"
18#include "LinAlgenums.h"
19
20class SAM;
21class LinSolParams;
22class ProcessAdm;
23
24using IntVec = std::vector<int>;
25
26
32{
33public:
35 static SystemVector* create(const ProcessAdm* adm, LinAlg::MatrixType vtype);
36
37protected:
40
41public:
43 virtual ~SystemVector() {}
44
46 virtual LinAlg::MatrixType getType() const = 0;
47
49 virtual SystemVector* copy() const = 0;
50
52 virtual size_t dim() const = 0;
53
55 virtual void redim(size_t n) = 0;
56
58 virtual void resize(size_t n, bool = false) { this->redim(n); }
59
61 bool empty() const { return this->dim() == 0; }
62
64 virtual Real* getPtr() = 0;
66 virtual const Real* getRef() const = 0;
68 virtual const Vector& vec() const = 0;
69
71 virtual void init(Real value = Real(0)) = 0;
72
75
82 virtual void assemble(const Vectors& vecs,
83 const IntVec& meqn, int neq) = 0;
84
86 virtual bool endAssembly() { return true; }
87
89 virtual void mult(Real alpha) = 0;
90
92 virtual void add(const SystemVector& vec, Real scale = Real(1)) = 0;
93
95 virtual Real L1norm() const = 0;
96
98 virtual Real L2norm() const = 0;
99
101 virtual Real Linfnorm() const = 0;
102
104 virtual void dump(std::ostream&, LinAlg::StorageFormat,
105 const char* = nullptr) const {}
106
107protected:
109 virtual std::ostream& write(std::ostream& os) const { return os; }
110
112 friend std::ostream& operator<<(std::ostream& os, const SystemVector& X)
113 {
114 return X.write(os);
115 }
116};
117
118
123class StdVector : public SystemVector, public Vector
124{
125public:
127 explicit StdVector(size_t n = 0) : Vector(n) {}
129 StdVector(const Real* values, size_t n) : Vector(values,n) {}
131 explicit StdVector(const std::vector<Real>& vec) : Vector(vec) {}
132
134 virtual LinAlg::MatrixType getType() const { return LinAlg::DENSE; }
135
137 virtual SystemVector* copy() const { return new StdVector(*this); }
138
145 virtual void assemble(const Vectors& vecs,
146 const IntVec& meqn,
147 int neq);
148
150 virtual size_t dim() const { return this->size(); }
151
153 virtual void redim(size_t n)
154 {
156 }
157
161 virtual void resize(size_t n, bool forceClear = false)
162 {
163 this->Vector::resize(n,forceClear);
164 }
165
167 virtual Real* getPtr() { return this->ptr(); }
169 virtual const Real* getRef() const { return this->ptr(); }
171 virtual const Vector& vec() const { return *this; }
172
174 virtual void init(Real value = Real(0)) { this->fill(value); }
175
177 virtual void mult(Real alpha) { this->operator*=(alpha); }
178
180 virtual void add(const SystemVector& vec, Real scale)
181 {
182 this->Vector::add(static_cast<const StdVector&>(vec),scale);
183 }
184
186 virtual Real L1norm() const { return this->asum(); }
187
189 virtual Real L2norm() const { return this->norm2(); }
190
192 virtual Real Linfnorm() const { size_t off = 0; return this->normInf(off); }
193
195 virtual void dump(std::ostream& os, LinAlg::StorageFormat format,
196 const char* label) const { dump(*this,label,format,os); }
197
199 static void dump(const Vector& x, const char* label,
200 LinAlg::StorageFormat format, std::ostream& os);
201
202protected:
204 virtual std::ostream& write(std::ostream& os) const
205 {
206 return os << static_cast<const Vector&>(*this);
207 }
208};
209
210
220{
221public:
223 static SystemMatrix* create(const ProcessAdm* adm, LinAlg::MatrixType mType,
224 int num_thread_SLU = 1);
226 static SystemMatrix* create(const ProcessAdm* adm, LinAlg::MatrixType mType,
227 const LinSolParams& spar);
228
229protected:
231 SystemMatrix() : nonZeroEqs({false}) {}
234
235public:
237 virtual ~SystemMatrix() {}
238
240 virtual LinAlg::MatrixType getType() const = 0;
241
243 virtual SystemMatrix* copy() const = 0;
244
246 virtual bool lockPattern(bool) { return false; }
247
249 virtual bool empty() const { return this->dim(0) == 0; }
251 bool isZero() const;
252
254 virtual size_t dim(int idim = 1) const = 0;
255
260 virtual void initAssembly(const SAM& sam, char = 0) = 0;
261
263 virtual void preAssemble(const std::vector<IntVec>&, size_t = 0) {}
264
266 virtual void compressPattern() {}
267
269 virtual void init() = 0;
270
272 void initNonZeroEqs();
274 bool flagNonZeroEq(int ieq = 0);
276 bool flagNonZeroEqs(const SAM& sam, const IntVec& meq);
278 bool flagNonZeroEqs(const SystemMatrix& B);
279
281 virtual bool endAssembly();
282
288 virtual bool assemble(const Matrix& eM, const SAM& sam, int e) = 0;
298 virtual bool assemble(const Matrix& eM, const SAM& sam,
299 SystemVector& B, int e) = 0;
309 virtual bool assemble(const Matrix& eM, const SAM& sam,
310 SystemVector& B, const IntVec& meq) = 0;
311
318 virtual bool assemble(const Matrix& eM, const IntVec& meq) = 0;
319
321 virtual bool augment(const SystemMatrix&, size_t, size_t) { return false; }
322
324 virtual bool truncate(Real = Real(1.0e-16)) { return false; }
325
327 virtual void mult(Real alpha) = 0;
328
330 virtual bool add(const SystemMatrix&, Real = Real(1)) { return false; }
331
333 virtual bool add(Real, int = 0) { return false; }
334
336 virtual bool multiply(const SystemVector&, SystemVector&) const
337 { return false; }
338
342 virtual bool solve(SystemVector& b, Real* rc = nullptr) = 0;
343
347 virtual bool solve(const SystemVector& b, SystemVector& x)
348 {
349 return this->solve(x.copy(b));
350 }
351
355 virtual bool solve(SystemVector& b, Matrix& x);
356
358 virtual Real Linfnorm() const = 0;
359
361 virtual void dump(std::ostream&, LinAlg::StorageFormat,
362 const char* = nullptr) {}
363
365 virtual void dump(const char* fileName, std::streamsize precision = 0,
368 virtual bool load(const char*, bool = false) { return false; }
369
371 StdVector operator*(const SystemVector& b) const;
372
375
376protected:
378 virtual std::ostream& write(std::ostream& os) const { return os; }
379
381 friend std::ostream& operator<<(std::ostream& os, const SystemMatrix& A)
382 {
383 return A.write(os);
384 }
385
386private:
387 std::vector<bool> nonZeroEqs;
388};
389
390#endif
std::vector< int > IntVec
General integer vector.
Definition ASMbase.h:25
#define Real
The floating point type to use.
Definition ImmersedBoundaries.h:18
Various enums for linear algebra scope.
Global algebraic operations on index 1-based matrices and vectors.
std::vector< Vector > Vectors
An array of real-valued vectors with algebraic operations.
Definition MatVec.h:37
std::vector< int > IntVec
General integer vector.
Definition SystemMatrix.h:24
Class for linear solver parameters.
Definition LinSolParams.h:67
Class for administration of MPI processes in IFEM library.
Definition ProcessAdm.h:33
This class contains data and functions for the assembly of FE matrices.
Definition SAM.h:39
Standard system vector stored as a single continuous array.
Definition SystemMatrix.h:124
virtual std::ostream & write(std::ostream &os) const
Writes the system vector to the given output stream.
Definition SystemMatrix.h:204
virtual void resize(size_t n, bool forceClear=false)
Resizes the vector to length n.
Definition SystemMatrix.h:161
virtual size_t dim() const
Returns the dimension of the system vector.
Definition SystemMatrix.h:150
virtual void dump(std::ostream &os, LinAlg::StorageFormat format, const char *label) const
Dumps the system vector on a specified format.
Definition SystemMatrix.h:195
virtual SystemVector * copy() const
Creates a copy of the system vector and returns a pointer to it.
Definition SystemMatrix.h:137
virtual Real Linfnorm() const
Linfinity-norm of the vector.
Definition SystemMatrix.h:192
virtual Real L2norm() const
L2-norm of the vector.
Definition SystemMatrix.h:189
virtual const Real * getRef() const
Reference through pointer.
Definition SystemMatrix.h:169
virtual void init(Real value=Real(0))
Initializes the vector to a given scalar value.
Definition SystemMatrix.h:174
virtual void assemble(const Vectors &vecs, const IntVec &meqn, int neq)
Adds element vectors into the system vector.
Definition SystemMatrix.C:65
StdVector(const std::vector< Real > &vec)
Overloaded copy constructor.
Definition SystemMatrix.h:131
virtual const Vector & vec() const
Reference to underlying utl::vector.
Definition SystemMatrix.h:171
StdVector(const Real *values, size_t n)
Constructor creating a vector from an array.
Definition SystemMatrix.h:129
virtual Real * getPtr()
Access through pointer.
Definition SystemMatrix.h:167
virtual Real L1norm() const
L1-norm of the vector.
Definition SystemMatrix.h:186
virtual LinAlg::MatrixType getType() const
Returns the vector type.
Definition SystemMatrix.h:134
virtual void redim(size_t n)
Sets the dimension of the system vector while retaining content.
Definition SystemMatrix.h:153
virtual void mult(Real alpha)
Multiplication with a scalar.
Definition SystemMatrix.h:177
virtual void add(const SystemVector &vec, Real scale)
Addition of another system vector to this one.
Definition SystemMatrix.h:180
StdVector(size_t n=0)
Default constructor creating a vector of length n.
Definition SystemMatrix.h:127
Base class for representing a system matrix on different formats.
Definition SystemMatrix.h:220
virtual bool assemble(const Matrix &eM, const IntVec &meq)=0
Adds an element matrix into the associated system matrix.
static SystemMatrix * create(const ProcessAdm *adm, LinAlg::MatrixType mType, int num_thread_SLU=1)
Static method creating a matrix of the given type.
Definition SystemMatrix.C:122
bool isZero() const
Checks if the matrix have no non-zero contributions.
Definition SystemMatrix.C:241
virtual void preAssemble(const std::vector< IntVec > &, size_t=0)
Initializes the element sparsity pattern based on node connections.
Definition SystemMatrix.h:263
bool flagNonZeroEq(int ieq=0)
Flags equation ieq as pivot element with non-zero contributions.
Definition SystemMatrix.C:184
virtual std::ostream & write(std::ostream &os) const
Writes the system matrix to the given output stream.
Definition SystemMatrix.h:378
StdVector operator*(const SystemVector &b) const
Calculates a matrix-vector product.
Definition SystemMatrix.C:268
virtual ~SystemMatrix()
Empty destructor.
Definition SystemMatrix.h:237
virtual void dump(std::ostream &, LinAlg::StorageFormat, const char *=nullptr)
Dumps the system matrix on a specified format.
Definition SystemMatrix.h:361
void initNonZeroEqs()
Initializes the nonZeroEqs flags.
Definition SystemMatrix.C:177
StdVector operator/(const SystemVector &b)
Solves a linear equation system.
Definition SystemMatrix.C:276
virtual bool multiply(const SystemVector &, SystemVector &) const
Performs a matrix-vector multiplication.
Definition SystemMatrix.h:336
virtual void compressPattern()
Compresses the sparsity pattern.
Definition SystemMatrix.h:266
bool flagNonZeroEqs(const SAM &sam, const IntVec &meq)
Flags the equations meq as pivots with non-zero contributions.
Definition SystemMatrix.C:199
virtual void init()=0
Initializes the matrix to zero assuming it is properly dimensioned.
SystemMatrix()
Default constructor.
Definition SystemMatrix.h:231
virtual bool endAssembly()
Finalizes the system matrix assembly.
Definition SystemMatrix.C:257
virtual bool empty() const
Checks if the matrix is empty.
Definition SystemMatrix.h:249
virtual bool solve(const SystemVector &b, SystemVector &x)
Solves the linear system of equations for a given right-hand-side.
Definition SystemMatrix.h:347
virtual void initAssembly(const SAM &sam, char=0)=0
Initializes the element assembly process.
virtual bool assemble(const Matrix &eM, const SAM &sam, SystemVector &B, int e)=0
Adds an element matrix into the associated system matrix.
virtual LinAlg::MatrixType getType() const =0
Returns the matrix type.
virtual bool solve(SystemVector &b, Real *rc=nullptr)=0
Solves the linear system of equations for a given right-hand-side.
virtual bool truncate(Real=Real(1.0e-16))
Truncates all small matrix elements to zero.
Definition SystemMatrix.h:324
virtual bool assemble(const Matrix &eM, const SAM &sam, SystemVector &B, const IntVec &meq)=0
Adds an element matrix into the associated system matrix.
virtual bool augment(const SystemMatrix &, size_t, size_t)
Augments a similar matrix symmetrically to the current matrix.
Definition SystemMatrix.h:321
virtual bool add(const SystemMatrix &, Real=Real(1))
Adds a matrix with similar structure to the current matrix.
Definition SystemMatrix.h:330
virtual bool load(const char *, bool=false)
Loads the system matrix from specified file.
Definition SystemMatrix.h:368
virtual Real Linfnorm() const =0
Returns the L-infinity norm of the matrix.
virtual void mult(Real alpha)=0
Multiplication with a scalar.
SystemMatrix(const SystemMatrix &a)
Copy constructor.
Definition SystemMatrix.h:233
virtual SystemMatrix * copy() const =0
Creates a copy of the system matrix and returns a pointer to it.
std::vector< bool > nonZeroEqs
Flags equations with non-zero contributions.
Definition SystemMatrix.h:387
friend std::ostream & operator<<(std::ostream &os, const SystemMatrix &A)
Global stream operator printing the matrix contents.
Definition SystemMatrix.h:381
virtual size_t dim(int idim=1) const =0
Returns the dimension of the system matrix.
virtual bool lockPattern(bool)
Locks or unlocks the sparsity pattern.
Definition SystemMatrix.h:246
virtual bool assemble(const Matrix &eM, const SAM &sam, int e)=0
Adds an element matrix into the associated system matrix.
virtual bool add(Real, int=0)
Adds a constant to the diagonal of current matrix.
Definition SystemMatrix.h:333
Base class for representing a system vector on different formats.
Definition SystemMatrix.h:32
virtual void add(const SystemVector &vec, Real scale=Real(1))=0
Addition of another system vector to this one.
virtual size_t dim() const =0
Returns the dimension/size of the system vector.
virtual void assemble(const Vectors &vecs, const IntVec &meqn, int neq)=0
Adds element vectors into the system vector.
virtual std::ostream & write(std::ostream &os) const
Writes the system vector to the given output stream.
Definition SystemMatrix.h:109
virtual SystemVector * copy() const =0
Creates a copy of the system vector and returns a pointer to it.
virtual Real * getPtr()=0
Access through pointer.
virtual const Real * getRef() const =0
Reference through pointer.
virtual void init(Real value=Real(0))=0
Initializes the vector assuming it is properly dimensioned.
virtual Real Linfnorm() const =0
Linfinity-norm of the vector.
virtual void mult(Real alpha)=0
Multiplication with a scalar.
virtual LinAlg::MatrixType getType() const =0
Returns the vector type.
virtual Real L2norm() const =0
L2-norm of the vector.
virtual const Vector & vec() const =0
Reference to underlying utl::vector, if any.
bool empty() const
Checks if the vector is empty.
Definition SystemMatrix.h:61
virtual ~SystemVector()
Empty destructor.
Definition SystemMatrix.h:43
virtual void resize(size_t n, bool=false)
Resizes the vector to length n.
Definition SystemMatrix.h:58
virtual Real L1norm() const =0
L1-norm of the vector.
virtual void dump(std::ostream &, LinAlg::StorageFormat, const char *=nullptr) const
Dumps the system vector on a specified format.
Definition SystemMatrix.h:104
virtual bool endAssembly()
Finalizes the system vector assembly.
Definition SystemMatrix.h:86
static SystemVector * create(const ProcessAdm *adm, LinAlg::MatrixType vtype)
Static method creating a vector of the given type.
Definition SystemMatrix.C:30
SystemVector()
The default constructor is protected to allow sub-classes only.
Definition SystemMatrix.h:39
friend std::ostream & operator<<(std::ostream &os, const SystemVector &X)
Global stream operator printing the vector contents.
Definition SystemMatrix.h:112
virtual void redim(size_t n)=0
Sets the dimension of the system vector.
A vector class with some added algebraic operations.
Definition matrix.h:64
void fill(T s)
Fill the vector with a scalar value.
Definition matrix.h:137
T asum(size_t off=0, int inc=1) const
Return the sum of the absolute value of the vector elements.
Definition matrix.h:1607
vector< T > & add(const std::vector< T > &X, const T &alfa=T(1), unsigned int ofsx=0, int stridex=1, unsigned int ofsy=0, int stridey=1)
Add the given vector X scaled by alfa to *this.
Definition matrix.h:1619
T norm2(size_t off=0, int inc=1) const
Return the Euclidean norm of the vector.
Definition matrix.h:1570
bool resize(size_t n, char forceClear=0)
Resize the vector to length n.
Definition matrix.h:277
vector< T > & operator*=(T c)
Multiplication with a scalar.
Definition matrix.h:1551
size_t size() const
Size of the vector.
Definition matrix.h:88
T * ptr()
Access through pointer.
Definition matrix.h:83
T normInf(size_t &off, int inc=1, bool sign=false) const
Return the infinite norm of the vector, or signed max value.
Definition matrix.h:1583
MatrixType
The available system matrix formats and associated solvers.
Definition LinAlgenums.h:22
@ DENSE
Dense matrices / LAPack solver.
Definition LinAlgenums.h:23
StorageFormat
Enumeration of matrix storage formats.
Definition LinAlgenums.h:43
@ FLAT
Flat format.
Definition LinAlgenums.h:45
const char RETAIN
Flag for vector::resize() method telling it to retain its content.
Definition matrix.h:55