jacobi.template
Loading...
Searching...
No Matches
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// #####################################################################
11class Matrix
12{
13public:
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
119protected:
120 int _nrows;
121 int _ncols;
122 mutable std::vector<double> _dd;
123};
124
125// #####################################################################
126class BisectInterpolation; // class forward declaration
131class CRS_Matrix: public Matrix
132{
133public:
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
221protected:
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
236{
237public:
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
315private:
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{
376public:
386 explicit BisectInterpolation(std::vector<int> const &fathers);
388
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
464protected:
465 std::vector<int> _iv;
466 std::vector<double> _vv;
467};
468
483{
484public:
491
499 BisectIntDirichlet(std::vector<int> const &fathers, std::vector<int> const &idxc_dir);
500
502
503
507 ~BisectIntDirichlet() override;
508};
509
510
511
512// *********************************************************************
513
522void CalcElem(int const ial[3], double const xc[], double ske[3][3], double fe[3]);
523
540void 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
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
~BisectIntDirichlet() override
BisectIntDirichlet(BisectIntDirichlet const &)=default
std::vector< int > _iv
fathers[nnode][2] of fine grid nodes, double entries denote injection points
Definition getmatrix.h:465
void Debug() const override
std::vector< double > _vv
weights[nnode][2] of fathers for grid nodes
Definition getmatrix.h:466
void Mult(std::vector< double > &wf, std::vector< double > const &uc) const override
‍**
~BisectInterpolation() override
friend void DefectRestrict(CRS_Matrix const &SK, BisectInterpolation const &P, std::vector< double > &fc, std::vector< double > &ff, std::vector< double > &uf)
BisectInterpolation(BisectInterpolation const &)=default
void MultT(std::vector< double > const &wf, std::vector< double > &uc) const
int fetch(int row, int col) const override
void Defect(std::vector< double > &w, std::vector< double > const &f, std::vector< double > const &u) const override
std::vector< int > _id
start indices of matrix rows
Definition getmatrix.h:225
void Mult(std::vector< double > &w, std::vector< double > const &u) const override
‍**
Definition getmatrix.cpp:49
std::vector< double > _sk
non-zero values
Definition getmatrix.h:227
int fetch(int row, int col) const override
virtual ~CRS_Matrix() override
Definition getmatrix.cpp:46
void Defect(std::vector< double > &w, std::vector< double > const &f, std::vector< double > const &u) const override
Definition getmatrix.cpp:66
CRS_Matrix(CRS_Matrix const &)=default
std::vector< int > _ik
column indices
Definition getmatrix.h:226
friend void DefectRestrict(CRS_Matrix const &SK, BisectInterpolation const &P, std::vector< double > &fc, std::vector< double > &ff, std::vector< double > &uf)
int _nnz
number of non-zero entries
Definition getmatrix.h:224
void Debug() const override
bool Compare2Old(int nnode, int const id[], int const ik[], double const sk[]) const
void CalculateLaplace(std::vector< double > &f)
FEM_Matrix(FEM_Matrix const &)=default
void ApplyDirichletBC(std::vector< double > const &u, std::vector< double > &f)
Mesh const & _mesh
reference to discretization
Definition getmatrix.h:316
~FEM_Matrix() override
void Derive_Matrix_Pattern_fast()
void Derive_Matrix_Pattern_slow()
void Derive_Matrix_Pattern()
Definition getmatrix.h:265
void AddElem_3(int const ial[3], double const ske[3][3], double const fe[3], std::vector< double > &f)
‍**
int Ncols() const
Definition getmatrix.h:56
virtual void Mult(std::vector< double > &w, std::vector< double > const &u) const =0
bool isSquare() const
Definition getmatrix.h:38
virtual void GetDiag(std::vector< double > &d) const =0
std::vector< double > const & GetDiag() const
Definition getmatrix.h:78
virtual ~Matrix()
Definition getmatrix.cpp:26
int _nrows
number of rows in matrix
Definition getmatrix.h:120
virtual int fetch(int row, int col) const =0
virtual void Debug() const =0
Matrix(Matrix const &)=default
virtual void Defect(std::vector< double > &w, std::vector< double > const &f, std::vector< double > const &u) const =0
int _ncols
number of columns in matrix
Definition getmatrix.h:121
std::vector< double > _dd
diagonal matrix elements
Definition getmatrix.h:122
int Nrows() const
Definition getmatrix.h:47
Definition geom.h:14
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[])
void CalcElem(int const ial[3], double const xc[], double ske[3][3], double fe[3])
u
Definition laplacian.m:3