gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
linearSystemGmm.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef LINEAR_SYSTEM_GMM_H
7 #define LINEAR_SYSTEM_GMM_H
8 
9 #include <string>
10 #include "GmshConfig.h"
11 #include "GmshMessage.h"
12 #include "linearSystem.h"
13 
14 #if defined(HAVE_GMM)
15 
16 #undef BB // can be defined by FlGui.h, and clashes with gmm arg name
17 #include <gmm.h>
18 
19 // Consider using linearSystemCSRGmm instead: assembly is much faster
20 template <class scalar> class linearSystemGmm : public linearSystem<scalar> {
21 protected:
22  std::vector<scalar> *_x; // nonLinearSystemGmm has to access to this vector
23  std::vector<scalar> *_b; // idem
24  gmm::row_matrix<gmm::wsvector<scalar> > *_a;
25 
26 private:
27  std::string _method;
28  double _tol;
29  int _noisy;
30 
31 public:
32  linearSystemGmm(const std::string &method = "gmres", double tol = 1e-8,
33  int noisy = 0)
34  : _x(0), _b(0), _a(0), _method(method), _tol(tol), _noisy(noisy) {}
35  virtual bool isAllocated() const { return _a != 0; }
36  virtual void allocate(int nbRows)
37  {
38  clear();
39  _a = new gmm::row_matrix<gmm::wsvector<scalar> >(nbRows, nbRows);
40  _b = new std::vector<scalar>(nbRows);
41  _x = new std::vector<scalar>(nbRows);
42  }
43  virtual ~linearSystemGmm() { clear(); }
44  virtual void clear()
45  {
46  if(_a) {
47  delete _a;
48  delete _b;
49  delete _x;
50  }
51  _a = 0;
52  }
53  virtual void addToMatrix(int row, int col, const scalar &val)
54  {
55  if(val != 0.0) (*_a)(row, col) += val;
56  }
57  virtual void getFromMatrix(int row, int col, scalar &val) const
58  {
59  val = (*_a)(row, col);
60  }
61  virtual void addToRightHandSide(int row, const scalar &val, int ith = 0)
62  {
63  if(val != 0.0) (*_b)[row] += val;
64  }
65  virtual void getFromRightHandSide(int row, scalar &val) const
66  {
67  val = (*_b)[row];
68  }
69  virtual void addToSolution(int row, const scalar &val)
70  {
71  if(val != 0.0) (*_x)[row] += val;
72  }
73  virtual void getFromSolution(int row, scalar &val) const { val = (*_x)[row]; }
74  virtual void zeroMatrix() { gmm::clear(*_a); }
75  virtual void zeroRightHandSide()
76  {
77  for(std::size_t i = 0; i < _b->size(); i++) (*_b)[i] = 0.;
78  }
79  virtual void zeroSolution()
80  {
81  for(std::size_t i = 0; i < _x->size(); i++) (*_x)[i] = 0.;
82  }
83  virtual double normInfRightHandSide() const
84  {
85  double nor = 0.;
86  double temp;
87  for(std::size_t i = 0; i < _b->size(); i++) {
88  temp = abs((*_b)[i]); // this is valid also for complex
89  // if(temp<0) temp = -temp;
90  if(nor < temp) nor = temp;
91  }
92  return nor;
93  }
94  void setPrec(double p) { _tol = p; }
95  void setNoisy(int n) { _noisy = n; }
96  void setGmres(int n) { _method = (n ? "gmres" : "cg"); }
97  virtual int systemSolve()
98  {
99 #if defined(HAVE_MUMPS)
100  if(_method == "mumps"){
101  gmm::MUMPS_solve(*_a, *_x, *_b);
102  return 1;
103  }
104 #else
105  //gmm::ilutp_precond<gmm::row_matrix<gmm::wsvector<scalar> > > P(*_a, 25, 0.);
106  gmm::ildltt_precond<gmm::row_matrix<gmm::wsvector<scalar> > > P(*_a, 30, 1.e-10);
107  //gmm::ilu_precond<gmm::row_matrix<gmm::wsvector<scalar> > > P(*_a);
108  gmm::iteration iter(_tol);
109  iter.set_noisy(_noisy);
110  if(_method == "gmres")
111  gmm::gmres(*_a, *_x, *_b, P, 100, iter);
112  else
113  gmm::cg(*_a, *_x, *_b, P, iter);
114  if(!iter.converged())
115  Msg::Warning("Iterative linear solver has not converged (res = %g)",
116  iter.get_res());
117 #endif
118  return 1;
119  }
120 };
121 
122 #else
123 
124 template <class scalar> class linearSystemGmm : public linearSystem<scalar> {
125 public:
126  linearSystemGmm(const std::string &method = "gmres", double tol = 1e-8,
127  int noisy = 0)
128  {
129  Msg::Error("Gmm++ is not available in this version of Gmsh");
130  }
131  virtual bool isAllocated() const { return false; }
132  virtual void allocate(int nbRows) {}
133  virtual void addToMatrix(int row, int col, const scalar &val) {}
134  virtual void getFromMatrix(int row, int col, scalar &val) const {}
135  virtual void addToRightHandSide(int row, const scalar &val, int ith = 0) {}
136  virtual void getFromRightHandSide(int row, scalar &val) const {}
137  virtual void addToSolution(int row, const scalar &val) {}
138  virtual void getFromSolution(int row, scalar &val) const {}
139  virtual void zeroMatrix() {}
140  virtual void zeroRightHandSide() {}
141  virtual void zeroSolution() {}
142  virtual int systemSolve() { return 0; }
143  virtual double normInfRightHandSide() const { return 0.; }
144  void setPrec(double p) {}
145  virtual void clear() {}
146  void setNoisy(int n) {}
147  void setGmres(int n) {}
148 };
149 
150 #endif
151 
152 #endif
linearSystemGmm::getFromSolution
virtual void getFromSolution(int row, scalar &val) const
Definition: linearSystemGmm.h:138
linearSystemGmm::zeroRightHandSide
virtual void zeroRightHandSide()
Definition: linearSystemGmm.h:140
linearSystemGmm::setPrec
void setPrec(double p)
Definition: linearSystemGmm.h:144
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
linearSystemGmm::getFromRightHandSide
virtual void getFromRightHandSide(int row, scalar &val) const
Definition: linearSystemGmm.h:136
linearSystemGmm::setNoisy
void setNoisy(int n)
Definition: linearSystemGmm.h:146
linearSystemGmm::addToMatrix
virtual void addToMatrix(int row, int col, const scalar &val)
Definition: linearSystemGmm.h:133
linearSystemGmm::systemSolve
virtual int systemSolve()
Definition: linearSystemGmm.h:142
GmshMessage.h
linearSystemGmm
Definition: linearSystemGmm.h:124
linearSystemGmm::linearSystemGmm
linearSystemGmm(const std::string &method="gmres", double tol=1e-8, int noisy=0)
Definition: linearSystemGmm.h:126
linearSystemGmm::allocate
virtual void allocate(int nbRows)
Definition: linearSystemGmm.h:132
linearSystemGmm::addToRightHandSide
virtual void addToRightHandSide(int row, const scalar &val, int ith=0)
Definition: linearSystemGmm.h:135
linearSystemGmm::zeroSolution
virtual void zeroSolution()
Definition: linearSystemGmm.h:141
linearSystemGmm::zeroMatrix
virtual void zeroMatrix()
Definition: linearSystemGmm.h:139
linearSystemGmm::isAllocated
virtual bool isAllocated() const
Definition: linearSystemGmm.h:131
linearSystemGmm::addToSolution
virtual void addToSolution(int row, const scalar &val)
Definition: linearSystemGmm.h:137
linearSystemGmm::clear
virtual void clear()
Definition: linearSystemGmm.h:145
linearSystemGmm::setGmres
void setGmres(int n)
Definition: linearSystemGmm.h:147
linearSystemGmm::normInfRightHandSide
virtual double normInfRightHandSide() const
Definition: linearSystemGmm.h:143
linearSystem
Definition: linearSystem.h:38
linearSystemGmm::getFromMatrix
virtual void getFromMatrix(int row, int col, scalar &val) const
Definition: linearSystemGmm.h:134
linearSystem.h