MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
spmapmh.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbmath/spmapmh.cc,v 1.42 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 /*
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) 2003-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 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
65 
66 #include "spmapmh.h"
67 
69 : SparseMatrixHandler(n, nn), m_end(*this, true)
70 {
71  col_indices.resize(NCols);
72 }
73 
75 {
76  NO_OP;
77 }
78 
79 integer
81  integer *const Ai, integer *const Ap, int offset) const
82 {
83  integer x_ptr = 0;
84 
85  row_cont_type::iterator ri;
86  row_cont_type::const_iterator re;
87 
88  for (integer col = 0; col < NCols; col++) {
89  Ap[col] = x_ptr + offset;
90  re = col_indices[col].end();
91  for (ri = col_indices[col].begin(); ri != re; ++ri) {
92  Ax[x_ptr] = ri->second;
93  Ai[x_ptr] = ri->first + offset;
94  x_ptr++;
95  }
96  }
97 
98  ASSERTMSGBREAK(x_ptr == NZ, "Error in "
99  "SpMapMatrixHandler::MakeCompressedColumnForm");
100 
101  Ap[NCols] = x_ptr + offset;
102 
103  return Nz();
104 }
105 
106 integer
108  std::vector<integer>& Ai, std::vector<integer>& Ap,
109  int offset) const
110 {
111  Ax.resize(Nz());
112  Ai.resize(Nz());
113  Ap.resize(iGetNumCols() + 1);
114 
115  return MakeCompressedColumnForm(&Ax[0], &Ai[0], &Ap[0], offset);
116 }
117 
118 integer
120  integer *const Arow, integer *const Acol,
121  integer *const Ap, int offset) const
122 {
123  integer x_ptr = 0;
124 
125  row_cont_type::iterator ri;
126  row_cont_type::const_iterator re;
127 
128  for (integer col = 0; col < NCols; col++) {
129  Ap[col] = x_ptr + offset;
130  re = col_indices[col].end();
131  for (ri = col_indices[col].begin(); ri != re; ++ri) {
132  Ax[x_ptr] = ri->second;
133  Arow[x_ptr] = ri->first + offset;
134  Acol[x_ptr] = col + offset;
135  x_ptr++;
136  }
137  }
138 
139  ASSERTMSGBREAK(x_ptr == NZ, "Error in "
140  "SpMapMatrixHandler::MakeIndexForm");
141 
142  Ap[NCols] = x_ptr + offset;
143 
144  return Nz();
145 }
146 
147 integer
148 SpMapMatrixHandler::MakeIndexForm(std::vector<doublereal>& Ax,
149  std::vector<integer>& Arow, std::vector<integer>& Acol,
150  std::vector<integer>& Ap, int offset) const
151 {
152  Ax.resize(Nz());
153  Arow.resize(Nz());
154  Acol.resize(Nz());
155  Ap.resize(iGetNumCols() + 1);
156 
157  return MakeIndexForm(&Ax[0], &Arow[0], &Acol[0], &Ap[0], offset);
158 }
159 
160 void
162 {
163  row_cont_type::const_iterator re;
164  row_cont_type::iterator ri;
165  for (integer col = 0; col < NCols; col++) {
166  re = col_indices[col].end();
167  for (ri = col_indices[col].begin(); ri != re; ++ri) {
168  ri->second = 0.;
169  }
170  }
171 }
172 
173 void
175 {
176  if (nn == 0) {
177  nn = n;
178  }
179 
180  for (integer col = 0; col < NCols; col++) {
181  col_indices[col].clear();
182  }
183 
184  col_indices.resize(nn);
185  NRows = n;
186  NCols = nn;
187  NZ = 0;
188 
189  m_end.reset();
190 }
191 
192 /* Estrae una colonna da una matrice */
195 {
196  if (icol > iGetNumCols()) {
198  }
199 
200  row_cont_type::const_iterator ri, re;
201  re = col_indices[icol].end();
202  for (ri = col_indices[icol].begin(); ri != re; ++ri) {
203  out(ri->first + 1) = ri->second;
204  }
205  return out;
206 }
207 
208 /* Prodotto Matrice per Matrice */
211  integer iCol, const doublereal& dCoef),
212  MatrixHandler& out, const MatrixHandler& in) const
213 {
214  if ((in.iGetNumRows() != iGetNumCols())
215  || (in.iGetNumCols() != out.iGetNumCols())
216  || (out.iGetNumRows() != iGetNumRows()))
217  {
218  silent_cerr("Assertion fault "
219  "in SpMapMatrixHandler::MatMatIncMul" << std::endl);
221  }
222 
223  integer ncols_in = in.iGetNumCols();
224  for (integer row_in = 0; row_in < NCols; row_in++) {
225  row_cont_type::const_iterator ri, re;
226  re = col_indices[row_in].end();
227  for (ri = col_indices[row_in].begin(); ri != re; ++ri) {
228  for (integer col_in = 1; col_in <= ncols_in; col_in++) {
229  (out.*op)(ri->first + 1, col_in,
230  ri->second*in(row_in + 1, col_in));
231  }
232  }
233  }
234 
235  return out;
236 }
237 
240  integer iCol, const doublereal& dCoef),
241  MatrixHandler& out, const MatrixHandler& in) const
242 {
243  if ((in.iGetNumRows() != iGetNumCols())
244  || (in.iGetNumCols() != out.iGetNumCols())
245  || (out.iGetNumRows() != iGetNumRows()))
246  {
247  silent_cerr("Assertion fault "
248  "in SpMapMatrixHandler::MatTMatMul_base" << std::endl);
250  }
251 
252  integer ncols_in = in.iGetNumCols();
253  for (integer row_out = 0; row_out < NCols; row_out++) {
254  row_cont_type::const_iterator ri, re;
255  re = col_indices[row_out].end();
256  for (ri = col_indices[row_out].begin(); ri != re; ++ri) {
257  for (integer col_in = 1; col_in <= ncols_in; col_in++) {
258  (out.*op)(row_out + 1, col_in,
259  ri->second*in(ri->first + 1, col_in));
260  }
261  }
262  }
263 
264  return out;
265 }
266 
267 /* Moltiplica per uno scalare e somma a una matrice */
270  integer drow, integer dcol) const
271 {
272  if ((out.iGetNumCols() < iGetNumCols() + dcol)
273  || (out.iGetNumRows() < iGetNumRows() + drow))
274  {
275  silent_cerr("Assertion fault "
276  "in SpMapMatrixHandler::MulAndSumWithShift"
277  << std::endl);
279  }
280 
281  drow = drow + 1;
282 
283  for (integer col = 0; col < NCols; col++) {
284  row_cont_type::const_iterator ri, re;
285  re = col_indices[col].end();
286  integer newcol = col + dcol + 1;
287  for (ri = col_indices[col].begin(); ri != re; ++ri) {
288  out.IncCoef(ri->first + drow, newcol, ri->second*s);
289  }
290  }
291 
292  return out;
293 }
294 
297  std::vector<bool> b, doublereal s, integer drow,
298  integer dcol) const
299 {
300  if ((out.iGetNumCols() < iGetNumCols() + dcol)
301  || (out.iGetNumRows() < iGetNumRows() + drow))
302  {
303  silent_cerr("Assertion fault "
304  "in SpMapMatrixHandler::MulAndSumWithShift"
305  << std::endl);
307  }
308 
309  drow = drow + 1;
310 
311  for (integer col = 0; col < NCols; col++) {
312  row_cont_type::const_iterator ri, re;
313  re = col_indices[col].end();
314  integer newcol = col + dcol + 1;
315  for (ri = col_indices[col].begin(); ri != re; ++ri) {
316  if (b[ri->first]) {
317  out.IncCoef(ri->first + drow,
318  newcol, ri->second*s);
319  }
320  }
321  }
322 
323  return out;
324 }
325 
328  const doublereal &dCoef),
329  VectorHandler& out, const VectorHandler& in) const
330 {
331  if (in.iGetSize() != iGetNumCols()
332  || out.iGetSize() != iGetNumRows())
333  {
335  }
336 
337  row_cont_type::const_iterator ri, re;
338 
339  for (integer col = 0; col < NCols; col++) {
340  re = col_indices[col].end();
341  for (ri = col_indices[col].begin(); ri != re; ++ri) {
342  doublereal d = ri->second*in(col + 1);
343  (out.*op)(ri->first + 1, d);
344  }
345  }
346 
347  return out;
348 }
349 
352  const doublereal &dCoef),
353  VectorHandler& out, const VectorHandler& in) const
354 {
355  if (out.iGetSize() != iGetNumCols()
356  || in.iGetSize() != iGetNumRows())
357  {
359  }
360 
361  row_cont_type::const_iterator ri, re;
362 
363  for (integer col = 0; col < NCols; col++) {
364  doublereal d = 0.;
365  re = col_indices[col].end();
366  for (ri = col_indices[col].begin(); ri != re; ++ri) {
367  d += ri->second*in(ri->first + 1);
368  }
369  (out.*op)(col+1, d);
370  }
371 
372  return out;
373 }
374 
375 void
377 {
378  if (is_end) {
379  elem.iRow = m.NRows;
380  elem.iCol = m.NCols;
381 
382  } else {
383  i = m.col_indices[0].begin();
384  elem.iRow = i->first;
385  elem.iCol = 0;
386  elem.dCoef = i->second;
387  }
388 }
389 
391 : m(m)
392 {
393  reset(is_end);
394 }
395 
397 {
398  NO_OP;
399 }
400 
403 {
404  ++i;
405  while (i == m.col_indices[elem.iCol].end()) {
406  if (++elem.iCol == m.NCols) {
407  elem.iRow = m.NRows;
408  return *this;
409  }
410 
411  i = m.col_indices[elem.iCol].begin();
412  }
413 
414  elem.iRow = i->first;
415  elem.dCoef = i->second;
416 
417  return *this;
418 }
419 
422 {
423  return &elem;
424 }
425 
428 {
429  return elem;
430 }
431 
432 bool
434 {
435  return elem == op.elem;
436 }
437 
438 bool
440 {
441  return elem != op.elem;
442 }
std::vector< row_cont_type > col_indices
Definition: spmapmh.h:79
virtual integer iGetNumCols(void) const =0
#define ASSERTMSGBREAK(expr, msg)
Definition: myassert.h:222
const SparseMatrixHandler::SparseMatrixElement * operator->(void) const
Definition: spmapmh.cc:421
virtual void IncCoef(integer iRow, integer iCol, const doublereal &dCoef)
Definition: mh.cc:374
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
const SpMapMatrixHandler::const_iterator & operator++(void) const
Definition: spmapmh.cc:402
const SparseMatrixHandler::SparseMatrixElement & operator*(void) const
Definition: spmapmh.cc:427
integer iGetNumRows(void) const
Definition: spmh.h:113
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
const integer Nz() const
Definition: spmh.h:104
#define NO_OP
Definition: myassert.h:74
SpMapMatrixHandler(const SpMapMatrixHandler &)
virtual integer iGetSize(void) const =0
row_cont_type::const_iterator i
Definition: spmapmh.h:96
integer iGetNumCols(void) const
Definition: spmh.h:117
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
Definition: mbdyn.h:77
SpMapMatrixHandler::const_iterator begin(void) const
Definition: spmapmh.h:116
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
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
virtual integer iGetNumRows(void) const =0