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 
20 class SAM;
21 class LinSolParams;
22 class ProcessAdm;
23 
24 using IntVec = std::vector<int>;
25 
26 
32 {
33 public:
35  static SystemVector* create(const ProcessAdm* adm, LinAlg::MatrixType vtype);
36 
37 protected:
40 
41 public:
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 
74  SystemVector& copy(const SystemVector& x);
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 
107 protected:
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 
123 class StdVector : public SystemVector, public Vector
124 {
125 public:
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  {
155  this->Vector::resize(n,utl::RETAIN);
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 
202 protected:
204  virtual std::ostream& write(std::ostream& os) const
205  {
206  return os << static_cast<const Vector&>(*this);
207  }
208 };
209 
210 
220 {
221 public:
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 
229 protected:
231  SystemMatrix() : nonZeroEqs({false}) {}
234 
235 public:
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 flagNonZeroEqs(const IntVec& meq = {});
276  bool flagNonZeroEqs(const SystemMatrix& B);
277 
279  virtual bool endAssembly();
280 
286  virtual bool assemble(const Matrix& eM, const SAM& sam, int e) = 0;
296  virtual bool assemble(const Matrix& eM, const SAM& sam,
297  SystemVector& B, int e) = 0;
307  virtual bool assemble(const Matrix& eM, const SAM& sam,
308  SystemVector& B, const IntVec& meq) = 0;
309 
316  virtual bool assemble(const Matrix& eM, const IntVec& meq) = 0;
317 
319  virtual bool augment(const SystemMatrix&, size_t, size_t) { return false; }
320 
322  virtual bool truncate(Real = Real(1.0e-16)) { return false; }
323 
325  virtual void mult(Real alpha) = 0;
326 
328  virtual bool add(const SystemMatrix&, Real = Real(1)) { return false; }
329 
331  virtual bool add(Real, int = 0) { return false; }
332 
334  virtual bool multiply(const SystemVector&, SystemVector&) const
335  { return false; }
336 
340  virtual bool solve(SystemVector& b, Real* rc = nullptr) = 0;
341 
345  virtual bool solve(const SystemVector& b, SystemVector& x)
346  {
347  return this->solve(x.copy(b));
348  }
349 
351  virtual Real Linfnorm() const = 0;
352 
354  virtual void dump(std::ostream&, LinAlg::StorageFormat,
355  const char* = nullptr) {}
356 
358  virtual void dump(const char* fileName, std::streamsize precision = 0,
361  virtual bool load(const char*, bool = false) { return false; }
362 
364  StdVector operator*(const SystemVector& b) const;
365 
367  StdVector operator/(const SystemVector& b);
368 
369 protected:
371  virtual std::ostream& write(std::ostream& os) const { return os; }
372 
374  friend std::ostream& operator<<(std::ostream& os, const SystemMatrix& A)
375  {
376  return A.write(os);
377  }
378 
379 private:
380  std::vector<bool> nonZeroEqs;
381 };
382 
383 #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
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 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 const Vector & vec() const
Reference to underlying utl::vector.
Definition: SystemMatrix.h:171
virtual std::ostream & write(std::ostream &os) const
Writes the system vector to the given output stream.
Definition: SystemMatrix.h:204
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 SystemVector * copy() const
Creates a copy of the system vector and returns a pointer to it.
Definition: SystemMatrix.h:137
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:64
StdVector(const std::vector< Real > &vec)
Overloaded copy constructor.
Definition: SystemMatrix.h:131
virtual const Real * getRef() const
Reference through pointer.
Definition: SystemMatrix.h:169
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:121
bool isZero() const
Checks if the matrix have no non-zero contributions.
Definition: SystemMatrix.C:217
virtual void preAssemble(const std::vector< IntVec > &, size_t=0)
Initializes the element sparsity pattern based on node connections.
Definition: SystemMatrix.h:263
StdVector operator*(const SystemVector &b) const
Calculates a matrix-vector product.
Definition: SystemMatrix.C:244
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:354
friend std::ostream & operator<<(std::ostream &os, const SystemMatrix &A)
Global stream operator printing the matrix contents.
Definition: SystemMatrix.h:374
void initNonZeroEqs()
Initializes the nonZeroEqs flags.
Definition: SystemMatrix.C:176
bool flagNonZeroEqs(const IntVec &meq={})
Flags the equations meq as pivots with non-zero contributions.
Definition: SystemMatrix.C:183
StdVector operator/(const SystemVector &b)
Solves a linear equation system.
Definition: SystemMatrix.C:252
virtual bool multiply(const SystemVector &, SystemVector &) const
Performs a matrix-vector multiplication.
Definition: SystemMatrix.h:334
virtual void compressPattern()
Compresses the sparsity pattern.
Definition: SystemMatrix.h:266
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:233
virtual std::ostream & write(std::ostream &os) const
Writes the system matrix to the given output stream.
Definition: SystemMatrix.h:371
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:345
virtual SystemMatrix * copy() const =0
Creates a copy of the system matrix and returns a pointer to it.
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:322
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:319
virtual bool add(const SystemMatrix &, Real=Real(1))
Adds a matrix with similar structure to the current matrix.
Definition: SystemMatrix.h:328
virtual bool load(const char *, bool=false)
Loads the system matrix from specified file.
Definition: SystemMatrix.h:361
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
std::vector< bool > nonZeroEqs
Flags equations with non-zero contributions.
Definition: SystemMatrix.h:380
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:331
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 SystemVector * copy() const =0
Creates a copy of the system vector and returns a pointer to it.
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 void init(Real value=Real(0))=0
Initializes the vector assuming it is properly dimensioned.
virtual const Real * getRef() const =0
Reference through pointer.
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.
friend std::ostream & operator<<(std::ostream &os, const SystemVector &X)
Global stream operator printing the vector contents.
Definition: SystemMatrix.h:112
virtual std::ostream & write(std::ostream &os) const
Writes the system vector to the given output stream.
Definition: SystemMatrix.h:109
virtual Real L2norm() const =0
L2-norm of the vector.
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 * getPtr()=0
Access through pointer.
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 const Vector & vec() const =0
Reference to underlying utl::vector, if any.
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:29
SystemVector()
The default constructor is protected to allow sub-classes only.
Definition: SystemMatrix.h:39
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:1550
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:1562
T norm2(size_t off=0, int inc=1) const
Return the Euclidean norm of the vector.
Definition: matrix.h:1513
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:1494
size_t size() const
Size of the vector.
Definition: matrix.h:88
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:1526
T * ptr()
Access through pointer.
Definition: matrix.h:83
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