Feellgood
solver.h
Go to the documentation of this file.
1 #ifndef solver_h
2 #define solver_h
3 
11 #include <eigen3/Eigen/Dense>
12 #include "mesh.h"
13 #include "algebra/algebra.h"
14 
20 template <int DIM_PROBLEM>
21 class solver
22  {
23  public:
25  explicit solver(Mesh::mesh & _msh ,
26  const std::vector<Tetra::prm> & _pTetra ,
28  const std::vector<Triangle::prm> & _pTri ,
30  const std::string& name ,
31  const double _tol ,
32  const bool v ,
33  const int max_iter ,
34  const std::function<bool(Mesh::Edge)> &edge_filter = [](Mesh::Edge)
35  { return true; }):
37  msh(&_msh), NOD(_msh.getNbNodes()), paramTet(_pTetra),
38  paramTri(_pTri), verbose(v), iter(name, _tol, v, max_iter),
39  K(build_shape(edge_filter)), L_rhs(DIM_PROBLEM*NOD) {}
40 
42  virtual void checkBoundaryConditions(void) const = 0;
43 
44  protected:
46  static const int DIM_PB = DIM_PROBLEM;
47 
50 
52  const int NOD;
53 
56  const std::vector<Tetra::prm> &paramTet;
57 
60  const std::vector<Triangle::prm> &paramTri;
61 
63  const bool verbose;
64 
67 
70 
72  std::vector<double> L_rhs;
73 
75  algebra::MatrixShape build_shape(const std::function<bool(Mesh::Edge)>& edge_filter) const
76  {
77  algebra::MatrixShape shape(DIM_PROBLEM * NOD);
78 
79  // Add a DIM_PROBLEM × DIM_PROBLEM block connecting nodes i and j.
80  auto add_block = [this, &shape](const int i, const int j)
81  {
82  for (int k = 0; k < DIM_PROBLEM; ++k)
83  {
84  for (int l = 0; l < DIM_PROBLEM; ++l)
85  { shape[DIM_PROBLEM * i + k].insert(DIM_PROBLEM * j + l); }
86  }
87  };
88 
89  // Add a diagonal block for each node.
90  for (int i = 0; i < NOD; ++i)
91  { add_block(i, i); }
92 
93  // Add two off-diagonal blocks for each edge relevant to the current problem.
94  for (auto edge: msh->edges)
95  {
96  if (edge_filter(edge))
97  {
98  add_block(edge.first, edge.second);
99  add_block(edge.second, edge.first);
100  }
101  }
102 
103  return shape;
104  }
105 
109  template <int N>
110  void buildMat(std::array<int,N> &ind,
111  Eigen::Matrix<double,DIM_PROBLEM*N,DIM_PROBLEM*N> &Ke)
112  {
113  for (int ie = 0; ie < N; ie++)
114  {
115  int i_ = ind[ie];
116  for (int je = 0; je < N; je++)
117  {
118  int j_ = ind[je];
119  for (int di = 0; di < DIM_PROBLEM; di++)
120  {
121  for (int dj = 0; dj < DIM_PROBLEM; dj++)
122  {
123  K.add(DIM_PROBLEM * i_ + di, DIM_PROBLEM * j_ + dj,
124  Ke(di * N + ie,dj * N + je));
125  }
126  }
127  }
128  }
129  }
130 
134  template <int N>
135  void buildVect(std::array<int,N> &ind, std::vector<double> &Le)
136  {
137  for (int ie = 0; ie < N; ie++)
138  {
139  int i_ = ind[ie];
140  for (int di = 0; di < DIM_PROBLEM; di++)
141  { L_rhs[DIM_PROBLEM*i_+di] += Le[di*N+ie]; }
142  }
143  }
144  }; // end template class solver
145 
146 #endif
set of class to handle sparse matrix operations for gradient conjugate algorithms a sparse vector cla...
Definition: mesh.h:34
std::vector< Edge > edges
Definition: mesh.h:353
int getNbNodes(void) const
Definition: mesh.h:144
Square sparse matrix.
Definition: sparseMat.h:46
void add(const int i, const int j, const double val)
Definition: sparseMat.h:114
template class for the different solvers template parameter DIM_PROBLEM: dimensionnality of the probl...
Definition: solver.h:22
Mesh::mesh * msh
Definition: solver.h:49
virtual void checkBoundaryConditions(void) const =0
void buildMat(std::array< int, N > &ind, Eigen::Matrix< double, DIM_PROBLEM *N, DIM_PROBLEM *N > &Ke)
Definition: solver.h:110
algebra::SparseMatrix K
Definition: solver.h:69
void buildVect(std::array< int, N > &ind, std::vector< double > &Le)
Definition: solver.h:135
const int NOD
Definition: solver.h:52
const bool verbose
Definition: solver.h:63
algebra::iteration< double > iter
Definition: solver.h:66
const std::vector< Tetra::prm > & paramTet
Definition: solver.h:56
const std::vector< Triangle::prm > & paramTri
Definition: solver.h:60
algebra::MatrixShape build_shape(const std::function< bool(Mesh::Edge)> &edge_filter) const
Definition: solver.h:75
static const int DIM_PB
Definition: solver.h:46
solver(Mesh::mesh &_msh, const std::vector< Tetra::prm > &_pTetra, const std::vector< Triangle::prm > &_pTri, const std::string &name, const double _tol, const bool v, const int max_iter, const std::function< bool(Mesh::Edge)> &edge_filter=[](Mesh::Edge) { return true;})
Definition: solver.h:25
std::vector< double > L_rhs
Definition: solver.h:72
class mesh, readMesh is expecting a mesh file in gmsh format either text or binary,...
std::pair< int, int > Edge
Definition: mesh.h:27
const int N
Definition: tetra.h:25
constexpr double v[NPI]
Definition: tetra.h:58
std::vector< std::set< int > > MatrixShape
Definition: sparseMat.h:38