MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
spmh.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbmath/spmh.h,v 1.32 2017/01/12 14:43:54 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 2003-2017
7  *
8  * This code is a partial merge of HmFe and MBDyn.
9  *
10  * Pierangelo Masarati <masarati@aero.polimi.it>
11  * Paolo Mantegazza <mantegazza@aero.polimi.it>
12  *
13  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
14  * via La Masa, 34 - 20156 Milano, Italy
15  * http://www.aero.polimi.it
16  *
17  * Changing this copyright notice is forbidden.
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation (version 2 of the License).
22  *
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32  */
33 
34 #ifndef SPMH_H
35 #define SPMH_H
36 
37 #include <vector>
38 
39 #include "myassert.h"
40 #include "solman.h"
41 
42 /* Sparse Matrix */
44 protected:
48 
49 #ifdef DEBUG
50  void IsValid(void) const {
51  NO_OP;
52  };
53 #endif /* DEBUG */
54 
55 public:
56 
60 
62  : iRow(0), iCol(0) { NO_OP; };
64  : iRow(iRow), iCol(iCol) { NO_OP; };
65  virtual ~SparseMatrixElement_base(void) { NO_OP; };
66  bool operator == (const SparseMatrixElement_base& op) const
67  { return iRow == op.iRow && iCol == op.iCol; };
68  bool operator != (const SparseMatrixElement_base& op) const
69  { return iRow != op.iRow || iCol != op.iCol; };
70  };
71 
72  // copy
75 
77  : dCoef(0.) {};
79  : SparseMatrixElement_base(iRow, iCol), dCoef(dCoef) { NO_OP; };
80  };
81 
82  // reference
85 
87  : SparseMatrixElement_base(iRow, iCol), dCoef(dCoef) { NO_OP; };
88 private:
89  // do not use
91  };
92 
93  // const reference
95  const doublereal& dCoef;
96 
98  : dCoef(::Zero1) {};
100  : SparseMatrixElement_base(iRow, iCol), dCoef(dCoef) { NO_OP; };
101  };
102 
103 public:
104  const integer Nz() const {
105  return NZ;
106  };
107 
108  /* FIXME: always square? */
109  SparseMatrixHandler(const integer &n, const integer &nn = 0);
110 
111  virtual ~SparseMatrixHandler(void);
112 
113  integer iGetNumRows(void) const {
114  return NRows;
115  };
116 
117  integer iGetNumCols(void) const {
118  return NCols;
119  };
120 
121  virtual
123  integer *const Ai, integer *const Ap,
124  int offset = 0) const = 0;
125 
126  virtual
127  integer MakeCompressedColumnForm(std::vector<doublereal>& Ax,
128  std::vector<integer>& Ai, std::vector<integer>& Ap,
129  int offset = 0) const = 0;
130 
131  virtual
132  integer MakeIndexForm(doublereal *const Ax,
133  integer *const Arow, integer *const Acol,
134  integer *const AcolSt,
135  int offset = 0) const = 0;
136 
137  virtual
138  integer MakeIndexForm(std::vector<doublereal>& Ax,
139  std::vector<integer>& Arow, std::vector<integer>& Acol,
140  std::vector<integer>& AcolSt,
141  int offset = 0) const = 0;
142 
143  /* Estrae una colonna da una matrice */
144  virtual VectorHandler& GetCol(integer icol,
145  VectorHandler& out) const = 0;
146 };
147 
148 /* Sparse Matrix in compact form */
150 protected:
152  std::vector<doublereal>& Ax;
153  const std::vector<integer>& Ai;
154  const std::vector<integer>& Ap;
155 
156 #ifdef DEBUG
157  void IsValid(void) const {
158  NO_OP;
159  };
160 #endif /* DEBUG */
161 
162 public:
163  CompactSparseMatrixHandler(const integer &n, const integer &nn,
164  std::vector<doublereal>& x,
165  const std::vector<integer>& i,
166  const std::vector<integer>& p);
167 
168  virtual ~CompactSparseMatrixHandler();
169 
170  /* used by MultiThreadDataManager to duplicate the storage array
171  * while preserving the CC indices */
172  virtual CompactSparseMatrixHandler *Copy(void) const = 0;
173 
174  /* used to sum CC matrices with identical indices */
176 
177  /* Restituisce un puntatore all'array di reali della matrice */
178  virtual inline const doublereal* pdGetMat(void) const {
179  return &Ax[0];
180  };
181 
182 public:
183  void Reset(void);
184 
185  virtual
187  integer *const Ai, integer *const Ap,
188  int offset = 0) const;
189 
190  virtual
191  integer MakeCompressedColumnForm(std::vector<doublereal>& Ax,
192  std::vector<integer>& Ai, std::vector<integer>& Ap,
193  int offset = 0) const;
194 
195  virtual
197  integer *const Arow, integer *const Acol,
198  integer *const AcolSt,
199  int offset = 0) const;
200 
201  virtual
202  integer MakeIndexForm(std::vector<doublereal>& Ax,
203  std::vector<integer>& Arow, std::vector<integer>& Acol,
204  std::vector<integer>& AcolSt,
205  int offset = 0) const;
206 };
207 
208 /* Sparse Matrix in compact form */
209 template <int off>
211 public:
212  CompactSparseMatrixHandler_tpl(const integer &n, const integer &nn,
213  std::vector<doublereal>& x,
214  const std::vector<integer>& i,
215  const std::vector<integer>& p);
216  virtual ~CompactSparseMatrixHandler_tpl(void);
217 
219  private:
221  mutable integer i_idx;
223 
224  protected:
225  void reset(bool is_end = false);
226 
227  public:
228  const_iterator(const CompactSparseMatrixHandler_tpl<off>& m, bool is_end = false);
229  ~const_iterator(void);
235  };
236 
237 protected:
238  /* Matrix Matrix product */
240  MatMatMul_base(void (MatrixHandler::*op)(integer iRow, integer iCol,
241  const doublereal& dCoef),
242  MatrixHandler& out, const MatrixHandler& in) const;
244  MatTMatMul_base(void (MatrixHandler::*op)(integer iRow, integer iCol,
245  const doublereal& dCoef),
246  MatrixHandler& out, const MatrixHandler& in) const;
247 
248  /* Matrix Vector product */
249  virtual VectorHandler&
250  MatVecMul_base(void (VectorHandler::*op)(integer iRow,
251  const doublereal& dCoef),
252  VectorHandler& out, const VectorHandler& in) const;
253  virtual VectorHandler&
254  MatTVecMul_base(void (VectorHandler::*op)(integer iRow,
255  const doublereal& dCoef),
256  VectorHandler& out, const VectorHandler& in) const;
257 
258 protected:
260 
261 public:
264  };
265 
267  return m_end;
268  };
269 };
270 
271 #endif /* SPMH_H */
const_iterator(const CompactSparseMatrixHandler_tpl< off > &m, bool is_end=false)
Definition: spmh.cc:299
virtual VectorHandler & MatTVecMul_base(void(VectorHandler::*op)(integer iRow, const doublereal &dCoef), VectorHandler &out, const VectorHandler &in) const
Definition: spmh.cc:253
virtual integer MakeIndexForm(doublereal *const Ax, integer *const Arow, integer *const Acol, integer *const AcolSt, int offset=0) const =0
virtual integer MakeCompressedColumnForm(doublereal *const Ax, integer *const Ai, integer *const Ap, int offset=0) const =0
virtual CompactSparseMatrixHandler * Copy(void) const =0
virtual VectorHandler & MatVecMul_base(void(VectorHandler::*op)(integer iRow, const doublereal &dCoef), VectorHandler &out, const VectorHandler &in) const
Definition: spmh.cc:228
const CompactSparseMatrixHandler_tpl< off > & m
Definition: spmh.h:220
void AddUnchecked(const CompactSparseMatrixHandler &m)
Definition: spmh.cc:72
CompactSparseMatrixHandler_tpl< off >::const_iterator m_end
Definition: spmh.h:259
const CompactSparseMatrixHandler_tpl< off >::const_iterator & operator++(void) const
Definition: spmh.cc:313
SparseMatrixElement(integer iRow, integer iCol, const doublereal &dCoef)
Definition: spmh.h:78
integer iGetNumRows(void) const
Definition: spmh.h:113
virtual integer MakeCompressedColumnForm(doublereal *const Ax, integer *const Ai, integer *const Ap, int offset=0) const
Definition: spmh.cc:112
const std::vector< integer > & Ap
Definition: spmh.h:154
SparseMatrixHandler(const integer &n, const integer &nn=0)
Definition: spmh.cc:38
const integer Nz() const
Definition: spmh.h:104
#define NO_OP
Definition: myassert.h:74
CompactSparseMatrixHandler_tpl< off >::const_iterator begin(void) const
Definition: spmh.h:262
const std::vector< integer > & Ai
Definition: spmh.h:153
virtual const doublereal * pdGetMat(void) const
Definition: spmh.h:178
integer iGetNumCols(void) const
Definition: spmh.h:117
bool operator!=(const CompactSparseMatrixHandler_tpl< off >::const_iterator &op) const
Definition: spmh.cc:352
SparseMatrixElementConstRef(integer iRow, integer iCol, const doublereal &dCoef)
Definition: spmh.h:99
SparseMatrixElement_base(integer iRow, integer iCol)
Definition: spmh.h:63
std::vector< doublereal > & Ax
Definition: spmh.h:152
virtual ~SparseMatrixHandler(void)
Definition: spmh.cc:44
integer NZ
Definition: spmh.h:47
virtual integer MakeIndexForm(doublereal *const Ax, integer *const Arow, integer *const Acol, integer *const AcolSt, int offset=0) const
Definition: spmh.cc:130
const CompactSparseMatrixHandler_tpl< off >::const_iterator & end(void) const
Definition: spmh.h:266
const SparseMatrixHandler::SparseMatrixElement * operator->(void)
Definition: spmh.cc:331
virtual ~CompactSparseMatrixHandler_tpl(void)
Definition: spmh.cc:161
const doublereal Zero1
SparseMatrixHandler::SparseMatrixElement elem
Definition: spmh.h:222
bool operator==(const CompactSparseMatrixHandler_tpl< off >::const_iterator &op) const
Definition: spmh.cc:345
SparseMatrixElementRef(integer iRow, integer iCol, doublereal &dCoef)
Definition: spmh.h:86
const SparseMatrixHandler::SparseMatrixElement & operator*(void)
Definition: spmh.cc:338
bool operator!=(const SparseMatrixElement_base &op) const
Definition: spmh.h:68
MatrixHandler & MatMatMul_base(void(MatrixHandler::*op)(integer iRow, integer iCol, const doublereal &dCoef), MatrixHandler &out, const MatrixHandler &in) const
Definition: spmh.cc:169
CompactSparseMatrixHandler(const integer &n, const integer &nn, std::vector< doublereal > &x, const std::vector< integer > &i, const std::vector< integer > &p)
Definition: spmh.cc:49
virtual VectorHandler & GetCol(integer icol, VectorHandler &out) const =0
integer NRows
Definition: spmh.h:45
CompactSparseMatrixHandler_tpl(const integer &n, const integer &nn, std::vector< doublereal > &x, const std::vector< integer > &i, const std::vector< integer > &p)
Definition: spmh.cc:148
bool operator==(const SparseMatrixElement_base &op) const
Definition: spmh.h:66
double doublereal
Definition: colamd.c:52
MatrixHandler & MatTMatMul_base(void(MatrixHandler::*op)(integer iRow, integer iCol, const doublereal &dCoef), MatrixHandler &out, const MatrixHandler &in) const
Definition: spmh.cc:199
long int integer
Definition: colamd.c:51
integer NCols
Definition: spmh.h:46
virtual ~CompactSparseMatrixHandler()
Definition: spmh.cc:63