Memosa-FVM  0.2
CRMatrixRect.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifndef _CRMATRIXRECT_H_
6 #define _CRMATRIXRECT_H_
7 
8 #include "Matrix.h"
9 #include "CRConnectivity.h"
10 #include "Array.h"
11 #include "StorageSite.h"
12 
13 
27 template<class T_Coeff, class X, class B>
28 class CRMatrixRect : public Matrix
29 {
30 public:
31  typedef T_Coeff Coeff;
32  typedef T_Coeff Diag;
33  typedef T_Coeff OffDiag;
36  typedef Array<X> XArray;
37  typedef Array<B> BArray;
38 
48  {
49  public:
51  const Array<Vector<int,2> >& pairToCol) :
52  _coeffs(coeffs),
53  _pairToCol(pairToCol)
54  {}
55 
56  OffDiag& getCoeff01(const int np)
57  {
58  return _coeffs[_pairToCol[np][0]];
59  }
60 
61  OffDiag& getCoeff10(const int np)
62  {
63  return _coeffs[_pairToCol[np][1]];
64  }
65 
66  void addCoeffsSymmetric(const int np, const OffDiag& c)
67  {
68  _coeffs[_pairToCol[np][0]] += c;
69  _coeffs[_pairToCol[np][1]] += c;
70  }
71 
72  void addCoeffs(const int np, const OffDiag& c01, const OffDiag& c10)
73  {
74  _coeffs[_pairToCol[np][0]] += c01;
75  _coeffs[_pairToCol[np][1]] += c10;
76  }
77 
78  void addCoeff01(const int np, const OffDiag& c01)
79  {
80  _coeffs[_pairToCol[np][0]] += c01;
81  }
82 
83  void addCoeff10(const int np, const OffDiag& c10)
84  {
85  _coeffs[_pairToCol[np][1]] += c10;
86  }
87  private:
90  };
91 
92  typedef map<const CRConnectivity*,PairWiseAssembler*> PairWiseAssemblerMap;
93 
94 
96  Matrix(),
97  _conn(conn),
98  _row(_conn.getRow()),
99  _col(_conn.getCol()),
100  _diag(_conn.getRowDim()),
101  _offDiag(_col.getLength()),
103  {
104  logCtor();
105  }
106 
107 
108  DEFINE_TYPENAME("CRMatrixRect<"
112  +">");
113 
114  virtual void initAssembly()
115  {
116  _diag.zero();
117  _offDiag.zero();
118  }
119 
125  virtual void multiply(IContainer& yB, const IContainer& xB) const
126  {
127  BArray& y = dynamic_cast<BArray&>(yB);
128  const XArray& x = dynamic_cast<const XArray&>(xB);
129 
130  const int nRows = _conn.getRowSite().getSelfCount();
131  for(int nr=0; nr<nRows; nr++)
132  {
133  y[nr] = _diag[nr]*x[nr];
134  for (int nb = _row[nr]; nb<_row[nr+1]; nb++)
135  {
136  const int j = _col[nb];
137  y[nr] += _offDiag[nb]*x[j];
138  }
139  }
140  }
141 
147  virtual void multiplyAndAdd(IContainer& yB, const IContainer& xB) const
148  {
149  BArray& y = dynamic_cast<BArray&>(yB);
150  const XArray& x = dynamic_cast<const XArray&>(xB);
151 
152  const int nRows = _conn.getRowSite().getSelfCount();
153  for(int nr=0; nr<nRows; nr++)
154  {
155  y[nr] += _diag[nr]*x[nr];
156  for (int nb = _row[nr]; nb<_row[nr+1]; nb++)
157  {
158  const int j = _col[nb];
159  y[nr] += _offDiag[nb]*x[j];
160  }
161  }
162  }
163 
164 
165  const CRConnectivity& getConnectivity() const {return _conn;}
166 
169 
170  const Array<Coeff>& getDiag() const {return _diag;}
171  const Array<Coeff>& getOffDiag() const {return _offDiag;}
172 
173  virtual ~CRMatrixRect()
174  {
175  logDtor();
176  }
177 
186  shared_ptr<CRConnectivity>
188  const CRConnectivity& coarseToFine,
189  const StorageSite& coarseRowSite,
190  const StorageSite& coarseColSite)
191  {
192  const Array<int>& coarseIndex =
193  dynamic_cast<const Array<int>& >(gCoarseIndex);
194 
195  const int nCoarseRows = coarseRowSite.getCount();
196 
197  shared_ptr<CRConnectivity> coarseCR(new CRConnectivity(coarseRowSite,coarseColSite));
198 
199 
200  coarseCR->initCount();
201 
202  Array<bool> coarseCounted(nCoarseRows);
203 
204  coarseCounted = false;
205 
206  for(int nrCoarse=0; nrCoarse<nCoarseRows; nrCoarse++)
207  {
208  const int nFine = coarseToFine.getCount(nrCoarse);
209  for(int nfr=0; nfr<nFine; nfr++)
210  {
211  const int nrFine = coarseToFine(nrCoarse,nfr);
212 
213  for (int nb = _row[nrFine]; nb<_row[nrFine+1]; nb++)
214  {
215  const int nc = _col[nb];
216  const int ncCoarse = coarseIndex[nc];
217  if (ncCoarse>=0 && nrCoarse!=ncCoarse && !coarseCounted[ncCoarse])
218  {
219  coarseCounted[ncCoarse] = true;
220  coarseCR->addCount(nrCoarse,1);
221  }
222  }
223  }
224 
225  // reset counted
226  for(int nfr=0; nfr<nFine; nfr++)
227  {
228  const int nrFine = coarseToFine(nrCoarse,nfr);
229 
230  for (int nb = _row[nrFine]; nb<_row[nrFine+1]; nb++)
231  {
232  const int nc = _col[nb];
233  const int ncCoarse = coarseIndex[nc];
234  if (ncCoarse>=0)
235  coarseCounted[ncCoarse] = false;
236  }
237  }
238  }
239 
240  coarseCR->finishCount();
241 
242  for(int nrCoarse=0; nrCoarse<nCoarseRows; nrCoarse++)
243  {
244  const int nFine = coarseToFine.getCount(nrCoarse);
245  for(int nfr=0; nfr<nFine; nfr++)
246  {
247  const int nrFine = coarseToFine(nrCoarse,nfr);
248 
249  for (int nb = _row[nrFine]; nb<_row[nrFine+1]; nb++)
250  {
251  const int nc = _col[nb];
252  const int ncCoarse = coarseIndex[nc];
253  if (ncCoarse>=0 && nrCoarse!=ncCoarse && !coarseCounted[ncCoarse])
254  {
255  coarseCounted[ncCoarse] = true;
256  coarseCR->add(nrCoarse,ncCoarse);
257  }
258  }
259  }
260 
261  // reset counted
262  for(int nfr=0; nfr<nFine; nfr++)
263  {
264  const int nrFine = coarseToFine(nrCoarse,nfr);
265 
266  for (int nb = _row[nrFine]; nb<_row[nrFine+1]; nb++)
267  {
268  const int nc = _col[nb];
269  const int ncCoarse = coarseIndex[nc];
270  if (ncCoarse>=0)
271  coarseCounted[ncCoarse] = false;
272  }
273  }
274  }
275 
276  coarseCR->finishAdd();
277  return coarseCR;
278  }
279 
286  virtual
287  shared_ptr<Matrix>
288  createCoarseMatrix(const IContainer& gCoarseIndex,
289  const CRConnectivity& coarseToFine,
290  const CRConnectivity& coarseConnectivity)
291  {
292  const Array<int>& coarseIndex =
293  dynamic_cast<const Array<int>& >(gCoarseIndex);
294 
295  const int nCoarseRows = coarseConnectivity.getRowDim();
296 
297  shared_ptr<CRMatrixRect> coarseMatrix(new CRMatrixRect(coarseConnectivity));
298 
299  Array<Diag>& coarseDiag = coarseMatrix->getDiag();
300  Array<OffDiag>& coarseOffDiag = coarseMatrix->getOffDiag();
301 
302  const Array<int>& coarseConnRow = coarseConnectivity.getRow();
303  const Array<int>& coarseConnCol = coarseConnectivity.getCol();
304 
305  coarseDiag.zero();
306  coarseOffDiag.zero();
307 
308  //used to avoid searches when inserting coeffs
309  Array<int> coarseCoeffPos(nCoarseRows);
310 
311 
312  for(int nrCoarse=0; nrCoarse<nCoarseRows; nrCoarse++)
313  {
314  // for easy indexing when inserting coefficients set col
315  // positions into the coarse connectivity
316  for(int nb=coarseConnRow[nrCoarse]; nb<coarseConnRow[nrCoarse+1]; nb++)
317  coarseCoeffPos[coarseConnCol[nb]] = nb;
318 
319  // loop over the fine rows that make up this coarse row
320  const int nFine = coarseToFine.getCount(nrCoarse);
321  for(int nfr=0; nfr<nFine; nfr++)
322  {
323  const int nrFine = coarseToFine(nrCoarse,nfr);
324 
325  coarseDiag[nrCoarse] += _diag[nrFine];
326 
327  for (int nb = _row[nrFine]; nb<_row[nrFine+1]; nb++)
328  {
329  const int nc = _col[nb];
330  const int ncCoarse = coarseIndex[nc];
331 
332  if (ncCoarse<0) continue;
333 
334  if (nrCoarse!=ncCoarse)
335  {
336  const int pos = coarseCoeffPos[ncCoarse];
337  coarseOffDiag[pos] += _offDiag[nb];
338  }
339  else
340  {
341  coarseDiag[nrCoarse] += _offDiag[nb];
342  }
343  }
344  }
345  }
346 
347  return coarseMatrix;
348  }
349 
351  {
352  if (_pairWiseAssemblers.find(&pairs) == _pairWiseAssemblers.end())
353  {
354  _pairWiseAssemblers[&pairs] =
356  _conn.getPairToColMapping(pairs));
357  }
358  return *_pairWiseAssemblers[&pairs];
359  }
360 
361 
362 private:
364  const Array<int>& _row;
365  const Array<int>& _col;
369 };
370 
371 
372 #endif
const Array< int > & getCol() const
int getCount(const int i) const
virtual void zero()
Definition: Array.h:281
const Array< int > & getRow() const
const CRConnectivity & getConnectivity() const
Definition: CRMatrixRect.h:165
int getSelfCount() const
Definition: StorageSite.h:40
virtual void multiplyAndAdd(IContainer &yB, const IContainer &xB) const
Definition: CRMatrixRect.h:147
virtual void initAssembly()
Definition: CRMatrixRect.h:114
virtual void multiply(IContainer &yB, const IContainer &xB) const
Definition: CRMatrixRect.h:125
PairWiseAssembler(Array< OffDiag > &coeffs, const Array< Vector< int, 2 > > &pairToCol)
Definition: CRMatrixRect.h:50
DEFINE_TYPENAME("CRMatrixRect<"+NumTypeTraits< Coeff >::getTypeName()+","+NumTypeTraits< X >::getTypeName()+","+NumTypeTraits< B >::getTypeName()+">")
const CRConnectivity & _conn
Definition: CRMatrixRect.h:363
#define logCtor()
Definition: RLogInterface.h:26
virtual ~CRMatrixRect()
Definition: CRMatrixRect.h:173
Array< Coeff > & getDiag()
Definition: CRMatrixRect.h:167
void addCoeff10(const int np, const OffDiag &c10)
Definition: CRMatrixRect.h:83
Array< OffDiag > & _coeffs
Definition: CRMatrixRect.h:88
OffDiag & getCoeff01(const int np)
Definition: CRMatrixRect.h:56
const Array< int > & _row
Definition: CRMatrixRect.h:364
const Array< int > & _col
Definition: CRMatrixRect.h:365
const Array< Vector< int, 2 > > & _pairToCol
Definition: CRMatrixRect.h:89
OffDiag & getCoeff10(const int np)
Definition: CRMatrixRect.h:61
T_Coeff OffDiag
Definition: CRMatrixRect.h:33
Array< X > XArray
Definition: CRMatrixRect.h:36
Array< Coeff > _diag
Definition: CRMatrixRect.h:366
PairWiseAssembler & getPairWiseAssembler(const CRConnectivity &pairs)
Definition: CRMatrixRect.h:350
Array< Coeff > & getOffDiag()
Definition: CRMatrixRect.h:168
Array< B > BArray
Definition: CRMatrixRect.h:37
Definition: Matrix.h:16
void addCoeffsSymmetric(const int np, const OffDiag &c)
Definition: CRMatrixRect.h:66
PairWiseAssemblerMap _pairWiseAssemblers
Definition: CRMatrixRect.h:368
CRMatrixRect(const CRConnectivity &conn)
Definition: CRMatrixRect.h:95
#define logDtor()
Definition: RLogInterface.h:33
T_Coeff Diag
Definition: CRMatrixRect.h:32
T_Coeff Coeff
Definition: CRMatrixRect.h:31
virtual shared_ptr< Matrix > createCoarseMatrix(const IContainer &gCoarseIndex, const CRConnectivity &coarseToFine, const CRConnectivity &coarseConnectivity)
Definition: CRMatrixRect.h:288
Array< Coeff > _offDiag
Definition: CRMatrixRect.h:367
shared_ptr< CRConnectivity > createCoarseConnectivity(const IContainer &gCoarseIndex, const CRConnectivity &coarseToFine, const StorageSite &coarseRowSite, const StorageSite &coarseColSite)
Definition: CRMatrixRect.h:187
int getCount() const
Definition: StorageSite.h:39
Array< Coeff > DiagArray
Definition: CRMatrixRect.h:35
const StorageSite & getRowSite() const
void addCoeffs(const int np, const OffDiag &c01, const OffDiag &c10)
Definition: CRMatrixRect.h:72
void addCoeff01(const int np, const OffDiag &c01)
Definition: CRMatrixRect.h:78
Array< Coeff > CoeffArray
Definition: CRMatrixRect.h:34
int getRowDim() const
map< const CRConnectivity *, PairWiseAssembler * > PairWiseAssemblerMap
Definition: CRMatrixRect.h:92
const PairToColMapping & getPairToColMapping(const CRConnectivity &pairs) const
const Array< Coeff > & getDiag() const
Definition: CRMatrixRect.h:170
const Array< Coeff > & getOffDiag() const
Definition: CRMatrixRect.h:171