jacobi.template
getmatrix.h
Go to the documentation of this file.
1 #ifndef GETMATRIX_FILE
2 #define GETMATRIX_FILE
3 
4 #include "geom.h"
5 #include <cassert>
6 #include <vector>
7 // #####################################################################
11 class Matrix
12 {
13 public:
22  Matrix(int nrows, int ncols);
23  //Matrix();
24 
25  Matrix(Matrix const &) = default;
31  virtual ~Matrix();
32 
38  bool isSquare() const
39  {
40  return _nrows == _ncols;
41  }
42 
47  int Nrows() const
48  {
49  return _nrows;
50  }
51 
56  int Ncols() const
57  {
58  return _ncols;
59  }
60 
64  virtual void Debug() const = 0;
65 
71  virtual void GetDiag(std::vector<double> &d) const = 0;
72 
78  std::vector<double> const &GetDiag() const
79  {
80  if ( 0 == _dd.size() ) // GH: better? Nrows()>static_cast<int>(_dd.size())
81  {
82  _dd.resize(Nrows());
83  this->GetDiag(_dd);
84  }
85  assert( Nrows() == static_cast<int>(_dd.size()) );
86  return _dd;
87  }
88 
95  virtual void Mult(std::vector<double> &w, std::vector<double> const &u) const = 0;
96 
104  virtual void Defect(
105  std::vector<double> &w,
106  std::vector<double> const &f, std::vector<double> const &u) const = 0;
107 
117  virtual int fetch(int row, int col) const = 0;
118 
119 protected:
120  int _nrows;
121  int _ncols;
122  mutable std::vector<double> _dd;
123 };
124 
125 // #####################################################################
126 class BisectInterpolation; // class forward declaration
131 class CRS_Matrix: public Matrix
132 {
133 public:
138  CRS_Matrix();
139 
140  CRS_Matrix(CRS_Matrix const &) = default;
141 
142 
146  virtual ~CRS_Matrix() override;
152  void GetDiag(std::vector<double> &d) const override;
154  //* Extracts the diagonal elements of the sparse matrix.
155  //*
156  //* @return d vector of diagonal elements
157  //*/
158  //std::vector<double> const & GetDiag() const override;
159 
166  void Mult(std::vector<double> &w, std::vector<double> const &u) const override;
167 
175  void Defect(std::vector<double> &w,
176  std::vector<double> const &f, std::vector<double> const &u) const override;
177 
181  void Debug() const override;
182 
192  int fetch(int row, int col) const override;
193 
206  bool Compare2Old(int nnode, int const id[], int const ik[], double const sk[]) const;
207 
218  friend void DefectRestrict(CRS_Matrix const &SK, BisectInterpolation const &P,
219  std::vector<double> &fc, std::vector<double> &ff, std::vector<double> &uf);
220 
221 protected:
222  //int _nrows; //!< number of rows in matrix
223  //int _ncols; //!< number of columns in matrix
224  int _nnz;
225  std::vector<int> _id;
226  std::vector<int> _ik;
227  std::vector<double> _sk;
228 };
229 
230 
235 class FEM_Matrix: public CRS_Matrix
236 {
237 public:
251  explicit FEM_Matrix(Mesh const &mesh);
252 
253  FEM_Matrix(FEM_Matrix const &) = default;
254 
258  ~FEM_Matrix() override;
259 
266  {
267  //Derive_Matrix_Pattern_slow();
269  }
272 
273 
280  void CalculateLaplace(std::vector<double> &f);
281 
290  void ApplyDirichletBC(std::vector<double> const &u, std::vector<double> &f);
291 
293  //* Extracts the diagonal elements of the sparse matrix.
294  //*
295  //* @param[in,out] d (prellocated) vector of diagonal elements
296  //*/
297  //void GetDiag(std::vector<double> &d) const; // override in MPI parallel
298 
299 
312  void AddElem_3(int const ial[3], double const ske[3][3], double const fe[3], std::vector<double> &f);
313 
314 
315 private:
316  Mesh const &_mesh;
317 
318 };
319 
320 
322 //* Prolongation matrix in CRS format (compressed row storage; also named CSR),
323 //* see an <a href="https://en.wikipedia.org/wiki/Sparse_matrix">introduction</a>.
324 //*
325 //* The prolongation is applied for each node from the coarse mesh to the fine mesh and
326 //* is derived only geometrically (no operator weighted prolongation).
327 //*/
328 //class Prolongation: public CRS_Matrix
329 //{
330 //public:
332 //* Intializes the CRS matrix structure from the given discetization in @p mesh.
333 //*
334 //* The sparse matrix pattern is generated but the values are 0.
335 //*
336 //* @param[in] cmesh coarse mesh
337 //* @param[in] fmesh fine mesh
338 //*
339 //* @warning A reference to the discretizations @p fmesh @p cmesh are stored inside this class.
340 //* Therefore, changing these meshes outside requires also
341 //* to call method @p Derive_Matrix_Pattern explicitely.
342 //*
343 //* @see Derive_Matrix_Pattern
344 //*/
345 //Prolongation(Mesh const & cmesh, Mesh const & fmesh);
346 
348 //* Destructor.
349 //*/
350 //~Prolongation() override
351 //{}
352 
354 //* Generates the sparse matrix pattern and overwrites the existing pattern.
355 //*
356 //* The sparse matrix pattern is generated but the values are 0.
357 //*/
358 //void Derive_Matrix_Pattern() override;
359 
360 //private:
361 //Mesh const & _cmesh; //!< reference to coarse discretization
362 //Mesh const & _fmesh; //!< reference to fine discretization
363 //};
364 
365 // *********************************************************************
366 
367 
375 {
376 public:
386  explicit BisectInterpolation(std::vector<int> const &fathers);
388 
389  BisectInterpolation(BisectInterpolation const &) = default;
390 
394  ~BisectInterpolation() override;
395 
401  void GetDiag(std::vector<double> &d) const override;
403  //* Extracts the diagonal elements of the sparse matrix.
404  //*
405  //* @return d vector of diagonal elements
406  //*/
407  //std::vector<double> const & GetDiag() const override;
408 
415  void Mult(std::vector<double> &wf, std::vector<double> const &uc) const override;
416 
423  void MultT(std::vector<double> const &wf, std::vector<double> &uc) const;
424 
432  void Defect(std::vector<double> &w,
433  std::vector<double> const &f, std::vector<double> const &u) const override;
434 
438  void Debug() const override;
439 
449  int fetch(int row, int col) const override;
450 
461  friend void DefectRestrict(CRS_Matrix const &SK, BisectInterpolation const &P,
462  std::vector<double> &fc, std::vector<double> &ff, std::vector<double> &uf);
463 
464 protected:
465  std::vector<int> _iv;
466  std::vector<double> _vv;
467 };
468 
483 {
484 public:
490  {}
491 
499  BisectIntDirichlet(std::vector<int> const &fathers, std::vector<int> const &idxc_dir);
500 
501  BisectIntDirichlet(BisectIntDirichlet const &) = default;
502 
503 
507  ~BisectIntDirichlet() override;
508 };
509 
510 
511 
512 // *********************************************************************
513 
522 void CalcElem(int const ial[3], double const xc[], double ske[3][3], double fe[3]);
523 
540 void AddElem(int const ial[3], double const ske[3][3], double const fe[3],
541  int const id[], int const ik[], double sk[], double f[]);
542 
543 
544 
545 #endif
Matrix::_nrows
int _nrows
number of rows in matrix
Definition: getmatrix.h:120
BisectIntDirichlet
Definition: getmatrix.h:482
xc
xc
Definition: ascii_read_meshvector.m:30
BisectInterpolation::MultT
void MultT(std::vector< double > const &wf, std::vector< double > &uc) const
Definition: getmatrix.cpp:598
Matrix::~Matrix
virtual ~Matrix()
Definition: getmatrix.cpp:26
FEM_Matrix::Derive_Matrix_Pattern
void Derive_Matrix_Pattern()
Definition: getmatrix.h:265
FEM_Matrix::CalculateLaplace
void CalculateLaplace(std::vector< double > &f)
Definition: getmatrix.cpp:308
AddElem
void AddElem(int const ial[3], double const ske[3][3], double const fe[3], int const id[], int const ik[], double sk[], double f[])
Definition: getmatrix.cpp:496
BisectInterpolation
‍**
Definition: getmatrix.h:374
CRS_Matrix::fetch
int fetch(int row, int col) const override
Definition: getmatrix.cpp:103
BisectInterpolation::fetch
int fetch(int row, int col) const override
Definition: getmatrix.cpp:649
CRS_Matrix::DefectRestrict
friend void DefectRestrict(CRS_Matrix const &SK, BisectInterpolation const &P, std::vector< double > &fc, std::vector< double > &ff, std::vector< double > &uf)
CRS_Matrix::Compare2Old
bool Compare2Old(int nnode, int const id[], int const ik[], double const sk[]) const
Definition: getmatrix.cpp:146
Matrix::_dd
std::vector< double > _dd
diagonal matrix elements
Definition: getmatrix.h:122
FEM_Matrix::AddElem_3
void AddElem_3(int const ial[3], double const ske[3][3], double const fe[3], std::vector< double > &f)
‍**
Definition: getmatrix.cpp:398
CRS_Matrix::_ik
std::vector< int > _ik
column indices
Definition: getmatrix.h:226
CRS_Matrix::Mult
void Mult(std::vector< double > &w, std::vector< double > const &u) const override
‍**
Definition: getmatrix.cpp:49
BisectInterpolation::Mult
void Mult(std::vector< double > &wf, std::vector< double > const &uc) const override
‍**
Definition: getmatrix.cpp:567
BisectInterpolation::_iv
std::vector< int > _iv
fathers[nnode][2] of fine grid nodes, double entries denote injection points
Definition: getmatrix.h:465
Mesh
Definition: geom.h:13
CRS_Matrix::_sk
std::vector< double > _sk
non-zero values
Definition: getmatrix.h:227
BisectInterpolation::Defect
void Defect(std::vector< double > &w, std::vector< double > const &f, std::vector< double > const &u) const override
Definition: getmatrix.cpp:624
CRS_Matrix::Defect
void Defect(std::vector< double > &w, std::vector< double > const &f, std::vector< double > const &u) const override
Definition: getmatrix.cpp:66
FEM_Matrix::Derive_Matrix_Pattern_slow
void Derive_Matrix_Pattern_slow()
Definition: getmatrix.cpp:244
Matrix::Nrows
int Nrows() const
Definition: getmatrix.h:47
u
u
Definition: laplacian.m:3
CRS_Matrix::Debug
void Debug() const override
Definition: getmatrix.cpp:123
Matrix::isSquare
bool isSquare() const
Definition: getmatrix.h:38
BisectInterpolation::Debug
void Debug() const override
Definition: getmatrix.cpp:638
BisectInterpolation::~BisectInterpolation
~BisectInterpolation() override
Definition: getmatrix.cpp:546
BisectInterpolation::BisectInterpolation
BisectInterpolation()
Definition: getmatrix.cpp:535
FEM_Matrix::ApplyDirichletBC
void ApplyDirichletBC(std::vector< double > const &u, std::vector< double > &f)
Definition: getmatrix.cpp:366
Matrix::Defect
virtual void Defect(std::vector< double > &w, std::vector< double > const &f, std::vector< double > const &u) const =0
BisectInterpolation::_vv
std::vector< double > _vv
weights[nnode][2] of fathers for grid nodes
Definition: getmatrix.h:466
Matrix::Debug
virtual void Debug() const =0
CRS_Matrix::~CRS_Matrix
virtual ~CRS_Matrix() override
Definition: getmatrix.cpp:46
Matrix::Matrix
Matrix(int nrows, int ncols)
Definition: getmatrix.cpp:17
BisectIntDirichlet::BisectIntDirichlet
BisectIntDirichlet()
Definition: getmatrix.h:488
FEM_Matrix::_mesh
const Mesh & _mesh
reference to discretization
Definition: getmatrix.h:316
CRS_Matrix::CRS_Matrix
CRS_Matrix()
Definition: getmatrix.cpp:42
Matrix::Mult
virtual void Mult(std::vector< double > &w, std::vector< double > const &u) const =0
geom.h
CRS_Matrix::_nnz
int _nnz
number of non-zero entries
Definition: getmatrix.h:224
BisectIntDirichlet::~BisectIntDirichlet
~BisectIntDirichlet() override
Definition: getmatrix.cpp:677
FEM_Matrix::~FEM_Matrix
~FEM_Matrix() override
Definition: getmatrix.cpp:191
Matrix::GetDiag
const std::vector< double > & GetDiag() const
Definition: getmatrix.h:78
Matrix::Ncols
int Ncols() const
Definition: getmatrix.h:56
FEM_Matrix::FEM_Matrix
FEM_Matrix(Mesh const &mesh)
Definition: getmatrix.cpp:184
Matrix::_ncols
int _ncols
number of columns in matrix
Definition: getmatrix.h:121
FEM_Matrix::Derive_Matrix_Pattern_fast
void Derive_Matrix_Pattern_fast()
Definition: getmatrix.cpp:194
nnode
function vertex minimal boundary edge info in an ASCII file Matlab indexing is stored(starts with 1). % % The output file format is compatible with Mesh_2d_3_matlab nnode
Definition: ascii_write_mesh.m:23
CRS_Matrix
Definition: getmatrix.h:131
CalcElem
void CalcElem(int const ial[3], double const xc[], double ske[3][3], double fe[3])
Definition: getmatrix.cpp:445
Matrix
Definition: getmatrix.h:11
BisectInterpolation::DefectRestrict
friend void DefectRestrict(CRS_Matrix const &SK, BisectInterpolation const &P, std::vector< double > &fc, std::vector< double > &ff, std::vector< double > &uf)
FEM_Matrix
Definition: getmatrix.h:235
CRS_Matrix::_id
std::vector< int > _id
start indices of matrix rows
Definition: getmatrix.h:225
Matrix::fetch
virtual int fetch(int row, int col) const =0