MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
spmapmh.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbmath/spmapmh.h,v 1.52 2017/01/12 14:43:54 masarati Exp $ */
2 /*
3  * HmFe (C) is a FEM analysis code.
4  *
5  * Copyright (C) 1996-2017
6  *
7  * Marco Morandini <morandini@aero.polimi.it>
8  *
9  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
10  * via La Masa, 34 - 20156 Milano, Italy
11  * http://www.aero.polimi.it
12  *
13  * Changing this copyright notice is forbidden.
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  *
18  */
19 /* November 2001
20  * Modified to add methods
21  * to be used in the parallel MBDyn Solver.
22  *
23  * Copyright (C) 2003-2017
24  *
25  * Giuseppe Quaranta <quaranta@aero.polimi.it>
26  *
27  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
28  * via La Masa, 34 - 20156 Milano, Italy
29  * http://www.aero.polimi.it
30  *
31  */
32 /*
33  * MBDyn (C) is a multibody analysis code.
34  * http://www.mbdyn.org
35  *
36  * Copyright (C) 1996-2017
37  *
38  * This code is a partial merge of HmFe and MBDyn.
39  *
40  * Pierangelo Masarati <masarati@aero.polimi.it>
41  * Paolo Mantegazza <mantegazza@aero.polimi.it>
42  *
43  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
44  * via La Masa, 34 - 20156 Milano, Italy
45  * http://www.aero.polimi.it
46  *
47  * Changing this copyright notice is forbidden.
48  *
49  * This program is free software; you can redistribute it and/or modify
50  * it under the terms of the GNU General Public License as published by
51  * the Free Software Foundation (version 2 of the License).
52  *
53  *
54  * This program is distributed in the hope that it will be useful,
55  * but WITHOUT ANY WARRANTY; without even the implied warranty of
56  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57  * GNU General Public License for more details.
58  *
59  * You should have received a copy of the GNU General Public License
60  * along with this program; if not, write to the Free Software
61  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
62  */
63 
64 #ifndef SpMapMatrixHandler_hh
65 #define SpMapMatrixHandler_hh
66 
67 #include <map>
68 #include <vector>
69 #include "myassert.h"
70 #include "solman.h"
71 #include "spmh.h"
72 
73 /* #define MBDYN_X_KEEP_SPARSITY */
74 
75 /* Sparse Matrix in columns form */
77 private:
78  typedef std::map<integer, doublereal> row_cont_type;
79  mutable std::vector<row_cont_type> col_indices;
80 
81  // don't allow copy constructor!
83 
84 #ifdef DEBUG
85  void IsValid(void) const {
86  NO_OP;
87  };
88 #endif /* DEBUG */
89 
90 public:
92  friend class SpMapMatrixHandler;
93 
94  private:
96  mutable row_cont_type::const_iterator i;
98 
99  protected:
100  void reset(bool is_end = false);
101 
102  public:
103  const_iterator(const SpMapMatrixHandler& m, bool is_end = false);
104  ~const_iterator(void);
108  bool operator == (const SpMapMatrixHandler::const_iterator& op) const;
109  bool operator != (const SpMapMatrixHandler::const_iterator& op) const;
110  };
111 
112 private:
114 
115 public:
117  return const_iterator(*this);
118  };
119 
121  return m_end;
122  };
123 
124 public:
125  SpMapMatrixHandler(const integer &n = 0,const integer &nn = 0);
126 
127  virtual ~SpMapMatrixHandler(void);
128 
130  ASSERTMSGBREAK(i_row > 0 && i_row <= NRows,
131  "Error in SpMapMatrixHandler::operator(), "
132  "row index out of range");
133  ASSERTMSGBREAK(i_col > 0 && i_col <= NCols,
134  "Error in SpMapMatrixHandler::operator(), "
135  "col index out of range");
136  i_row--;
137  i_col--;
138  row_cont_type::iterator i;
139  row_cont_type& row = col_indices[i_col];
140  i = row.find(i_row);
141  if (i == row.end()) {
142  NZ++;
143 
144  row[i_row] = 0.;
145 
146  return row[i_row];
147 
148  } else {
149  return i->second;
150  }
151  };
152 
153  void IncCoef(integer ix, integer iy, const doublereal& inc) {
154  ASSERTMSGBREAK(ix > 0 && ix <= NRows,
155  "Error in SpMapMatrixHandler::IncCoef(), "
156  "row index out of range");
157  ASSERTMSGBREAK(iy > 0 && iy <= NCols,
158  "Error in SpMapMatrixHandler::IncCoef(), "
159  "col index out of range");
160 #ifdef MBDYN_X_KEEP_SPARSITY
161  /* try to keep sparsity */
162  if (inc != 0.) {
163 #endif /* MBDYN_X_KEEP_SPARSITY */
164  operator()(ix,iy) += inc;
165 #ifdef MBDYN_X_KEEP_SPARSITY
166  }
167 #endif /* MBDYN_X_KEEP_SPARSITY */
168  };
169 
170  void DecCoef(integer ix, integer iy, const doublereal& inc) {
171  ASSERTMSGBREAK(ix > 0 && ix <= NRows,
172  "Error in SpMapMatrixHandler::DecCoef(), "
173  "row index out of range");
174  ASSERTMSGBREAK(iy > 0 && iy <= NCols,
175  "Error in SpMapMatrixHandler::DecCoef(), "
176  "col index out of range");
177 #ifdef MBDYN_X_KEEP_SPARSITY
178  /* try to keep sparsity */
179  if (inc != 0.) {
180 #endif /* MBDYN_X_KEEP_SPARSITY */
181  operator()(ix,iy) -= inc;
182 #ifdef MBDYN_X_KEEP_SPARSITY
183  }
184 #endif /* MBDYN_X_KEEP_SPARSITY */
185  };
186 
187  void PutCoef(integer ix, integer iy, const doublereal& val) {
188  ASSERTMSGBREAK(ix - 1 < NRows,
189  "Error in SpMapMatrixHandler::PutCoef(), "
190  "row index out of range");
191  ASSERTMSGBREAK(iy - 1 < NCols,
192  "Error in SpMapMatrixHandler::PutCoef(), "
193  "col index out of range");
194 #ifdef MBDYN_X_KEEP_SPARSITY
195  /* try to keep sparsity */
196  if (val != 0.) {
197 #endif /* MBDYN_X_KEEP_SPARSITY */
198  operator()(ix,iy) = val;
199 #ifdef MBDYN_X_KEEP_SPARSITY
200  } else {
201  row_cont_type::iterator i;
202  row_cont_type& row = col_indices[iy-1];
203  i = row.find(ix - 1);
204  if (i != row.end()) {
205  i->second = val;
206  }
207  }
208 #endif /* MBDYN_X_KEEP_SPARSITY */
209  };
210 
211  const doublereal& dGetCoef(integer ix, integer iy) const {
212  ASSERTMSGBREAK(ix > 0 && ix <= NRows,
213  "Error in SpMapMatrixHandler::dGetCoef(), "
214  "row index out of range");
215  ASSERTMSGBREAK(iy > 0 && iy <= NCols,
216  "Error in SpMapMatrixHandler::dGetCoef(), "
217  "col index out of range");
218  row_cont_type::iterator i;
219  row_cont_type& row = col_indices[iy - 1];
220  i = row.find(ix - 1);
221  if (i == row.end()) {
223 
224  } else {
225  return i->second;
226  }
227  };
228 
229  const doublereal& operator () (integer ix, integer iy) const {
230  ASSERTMSGBREAK(ix > 0 && ix <= NRows,
231  "Error in SpMapMatrixHandler::operator(), "
232  "row index out of range");
233  ASSERTMSGBREAK(iy > 0 && iy <= NCols,
234  "Error in SpMapMatrixHandler::operator(), "
235  "col index out of range");
236  row_cont_type::iterator i;
237  row_cont_type& row = col_indices[iy - 1];
238  i = row.find(ix - 1);
239  if (i == row.end()) {
241 
242  } else {
243  return i->second;
244  }
245  };
246 
248  integer *const Ai, integer *const Ap,
249  int offset = 0) const;
250 
251  integer MakeCompressedColumnForm(std::vector<doublereal>& Ax,
252  std::vector<integer>& Ai, std::vector<integer>& Ap,
253  int offset = 0) const;
254 
255  integer MakeIndexForm(doublereal *const Ax,
256  integer *const Arow, integer *const Acol,
257  integer *const AcolSt,
258  int offset = 0) const;
259 
260  integer MakeIndexForm(std::vector<doublereal>& Ax,
261  std::vector<integer>& Arow, std::vector<integer>& Acol,
262  std::vector<integer>& AcolSt,
263  int offset = 0) const;
264 
265  void Reset(void);
266 
267  void Resize(integer ir, integer ic);
268 
269  /* Estrae una colonna da una matrice */
270  VectorHandler& GetCol(integer icol, VectorHandler& out) const;
271 
272  /* Matrix Matrix product */
273 protected:
275  MatMatMul_base(void (MatrixHandler::*op)(integer iRow, integer iCol,
276  const doublereal& dCoef),
277  MatrixHandler& out, const MatrixHandler& in) const;
279  MatTMatMul_base(void (MatrixHandler::*op)(integer iRow, integer iCol,
280  const doublereal& dCoef),
281  MatrixHandler& out, const MatrixHandler& in) const;
282 public:
283 
284  /* Moltiplica per uno scalare e somma a una matrice */
286  doublereal s = 1.,
287  integer drow = 0, integer dcol = 0) const;
288 
290  std::vector<bool> b, doublereal s = 1.,
291  integer drow = 0, integer dcol = 0) const;
292 
293  /* Matrix Vector product */
294 protected:
295  virtual VectorHandler&
296  MatVecMul_base(void (VectorHandler::*op)(integer iRow,
297  const doublereal& dCoef),
298  VectorHandler& out, const VectorHandler& in) const;
299  virtual VectorHandler&
300  MatTVecMul_base(void (VectorHandler::*op)(integer iRow,
301  const doublereal& dCoef),
302  VectorHandler& out, const VectorHandler& in) const;
303 
304 public:
305 };
306 
307 #endif /* SpMapMatrixHandler_hh */
308 
std::vector< row_cont_type > col_indices
Definition: spmapmh.h:79
#define ASSERTMSGBREAK(expr, msg)
Definition: myassert.h:222
const SparseMatrixHandler::SparseMatrixElement * operator->(void) const
Definition: spmapmh.cc:421
void DecCoef(integer ix, integer iy, const doublereal &inc)
Definition: spmapmh.h:170
const SpMapMatrixHandler::const_iterator & operator++(void) const
Definition: spmapmh.cc:402
const SparseMatrixHandler::SparseMatrixElement & operator*(void) const
Definition: spmapmh.cc:427
const_iterator(const SpMapMatrixHandler &m, bool is_end=false)
Definition: spmapmh.cc:390
integer MakeCompressedColumnForm(doublereal *const Ax, integer *const Ai, integer *const Ap, int offset=0) const
Definition: spmapmh.cc:80
SparseMatrixHandler::SparseMatrixElement elem
Definition: spmapmh.h:97
#define NO_OP
Definition: myassert.h:74
SpMapMatrixHandler(const SpMapMatrixHandler &)
std::map< integer, doublereal > row_cont_type
Definition: spmapmh.h:78
const SpMapMatrixHandler::const_iterator & end(void) const
Definition: spmapmh.h:120
row_cont_type::const_iterator i
Definition: spmapmh.h:96
void Reset(void)
Definition: spmapmh.cc:161
integer MakeIndexForm(doublereal *const Ax, integer *const Arow, integer *const Acol, integer *const AcolSt, int offset=0) const
Definition: spmapmh.cc:119
MatrixHandler & MulAndSumWithShift(MatrixHandler &out, doublereal s=1., integer drow=0, integer dcol=0) const
Definition: spmapmh.cc:269
integer NZ
Definition: spmh.h:47
void Resize(integer ir, integer ic)
Definition: spmapmh.cc:174
SpMapMatrixHandler::const_iterator begin(void) const
Definition: spmapmh.h:116
const doublereal Zero1
MatrixHandler & FakeThirdOrderMulAndSumWithShift(MatrixHandler &out, std::vector< bool > b, doublereal s=1., integer drow=0, integer dcol=0) const
Definition: spmapmh.cc:296
MatrixHandler & MatTMatMul_base(void(MatrixHandler::*op)(integer iRow, integer iCol, const doublereal &dCoef), MatrixHandler &out, const MatrixHandler &in) const
Definition: spmapmh.cc:239
doublereal & operator()(integer i_row, integer i_col)
Definition: spmapmh.h:129
const doublereal & dGetCoef(integer ix, integer iy) const
Definition: spmapmh.h:211
void PutCoef(integer ix, integer iy, const doublereal &val)
Definition: spmapmh.h:187
const SpMapMatrixHandler & m
Definition: spmapmh.h:95
MatrixHandler & MatMatMul_base(void(MatrixHandler::*op)(integer iRow, integer iCol, const doublereal &dCoef), MatrixHandler &out, const MatrixHandler &in) const
Definition: spmapmh.cc:210
VectorHandler & GetCol(integer icol, VectorHandler &out) const
Definition: spmapmh.cc:194
void reset(bool is_end=false)
Definition: spmapmh.cc:376
const_iterator m_end
Definition: spmapmh.h:113
integer NRows
Definition: spmh.h:45
virtual VectorHandler & MatVecMul_base(void(VectorHandler::*op)(integer iRow, const doublereal &dCoef), VectorHandler &out, const VectorHandler &in) const
Definition: spmapmh.cc:327
virtual ~SpMapMatrixHandler(void)
Definition: spmapmh.cc:74
double doublereal
Definition: colamd.c:52
virtual VectorHandler & MatTVecMul_base(void(VectorHandler::*op)(integer iRow, const doublereal &dCoef), VectorHandler &out, const VectorHandler &in) const
Definition: spmapmh.cc:351
long int integer
Definition: colamd.c:51
integer NCols
Definition: spmh.h:46
bool operator==(const SpMapMatrixHandler::const_iterator &op) const
Definition: spmapmh.cc:433
bool operator!=(const SpMapMatrixHandler::const_iterator &op) const
Definition: spmapmh.cc:439
void IncCoef(integer ix, integer iy, const doublereal &inc)
Definition: spmapmh.h:153