NeoPZ
tpzsparseblockdiagonal.cpp
Go to the documentation of this file.
1 
8 #include "pzfmatrix.h"
9 
10 #include "pzlog.h"
11 #ifdef LOG4CXX
12 static LoggerPtr logger(Logger::getLogger("pz.StrMatrix"));
13 #endif
14 
15 using namespace std;
16 
17 template<class TVar>
19 {
20 }
21 template<class TVar>
23 {
24  int64_t numbl = blockgraphindex.NElements()-1;
25  this->fBlockSize.Resize(numbl);
26  int64_t ibl;
27  for(ibl=0; ibl<numbl; ibl++)
28  {
29  this->fBlockSize[ibl] = blockgraphindex[ibl+1]-blockgraphindex[ibl];
30  }
31  this->Initialize(this->fBlockSize);
32  this->fRow = rows;
33  this->fCol = rows;
34 }
35 
36 template<class TVar>
38 {
39  LOGPZ_DEBUG(logger, "Constructor of TPZSparseBlockDiagonal");
40  int64_t numbl = blockgraphindex.NElements()-1;
41  this->fBlockSize.Resize(numbl);
42  int64_t ibl,iblcount,graphsize = 0;
43  for(ibl=0, iblcount=0; ibl<numbl; ibl++)
44  {
45  if(colors[ibl]==color)
46  {
47  this->fBlockSize[iblcount++] = blockgraphindex[ibl+1]-blockgraphindex[ibl];
48  graphsize += this->fBlockSize[iblcount-1];
49  }
50  }
51  fBlock.Resize(graphsize);
52  fBlockIndex.Resize(iblcount+1);
53  fBlockIndex[0] = 0;
54  for(ibl=0, iblcount=0; ibl<numbl; ibl++)
55  {
56  if(colors[ibl]==color)
57  {
58  int64_t first = blockgraphindex[ibl];
59  int64_t last = blockgraphindex[ibl+1];
60  int64_t firstcp = fBlockIndex[iblcount];
61  this->fBlockIndex[iblcount+1] = firstcp+this->fBlockSize[iblcount];
62  // int lastcp = fBlockIndex[iblcount+1];
63  int64_t ieq,ieqcp;
64  for(ieq=first,ieqcp=firstcp; ieq<last; ieq++,ieqcp++)
65  {
66  fBlock[ieqcp] = blockgraph[ieq];
67  }
68  iblcount++;
69  }
70  }
71  this->fBlockSize.Resize(iblcount);
72  this->Initialize(this->fBlockSize);
73  this->fRow = rows;
74  this->fCol = rows;
75 }
76 
77 
78 template<class TVar>
80 {
81 }
82 
83 template<class TVar>
84 const TVar& TPZSparseBlockDiagonal<TVar>::Get(const int64_t row, const int64_t col) const
85 {
86  int64_t rblock,rblockindex,cblock,cblockindex;
87  FindBlockIndex(row,rblock,rblockindex);
88  if(rblock == -1) return this->gZero;
89  FindBlockIndex(col,cblock,cblockindex);
90  if(cblock != rblock) return this->gZero;
91  int64_t pos = rblockindex + cblockindex*this->fBlockSize[rblock];
92  return this->fStorage[this->fBlockPos[rblock]+pos];
93 }
94 
95 template<class TVar>
96 const TVar& TPZSparseBlockDiagonal<TVar>::GetVal(const int64_t row, const int64_t col) const
97 {
98  int64_t rblock,rblockindex,cblock,cblockindex;
99  FindBlockIndex(row,rblock,rblockindex);
100  if(rblock == -1) return this->gZero;
101  FindBlockIndex(col,cblock,cblockindex);
102  if(cblock != rblock) return this->gZero;
103  int64_t pos = rblockindex + cblockindex*this->fBlockSize[rblock];
104  return this->fStorage[this->fBlockPos[rblock]+pos];
105 }
106 
107 template <class TVar>
108 int TPZSparseBlockDiagonal<TVar>::Put(const int64_t row, const int64_t col, const TVar& value)
109 {
110  int64_t rblock,rblockindex,cblock,cblockindex;
111  FindBlockIndex(row,rblock,rblockindex);
112  if(rblock == -1) return -1;
113  FindBlockIndex(col,cblock,cblockindex);
114  if(cblock != rblock) return -1;
115  int64_t pos = rblockindex + cblockindex*this->fBlockSize[rblock];
116  this->fStorage[this->fBlockPos[rblock]+pos] = value;
117  return 0;
118 }
119 
120 template<class TVar>
121 int TPZSparseBlockDiagonal<TVar>::PutVal(const int64_t row, const int64_t col, const TVar& value)
122 {
123  int64_t rblock,rblockindex,cblock,cblockindex;
124  FindBlockIndex(row,rblock,rblockindex);
125  if(rblock == -1) return -1;
126  FindBlockIndex(col,cblock,cblockindex);
127  if(cblock != rblock) return -1;
128  int64_t pos = rblockindex + cblockindex*this->fBlockSize[rblock];
129  this->fStorage[this->fBlockPos[rblock]+pos] = value;
130  return 0;
131 }
132 template<class TVar>
133 TVar& TPZSparseBlockDiagonal<TVar>::operator ( )(const int64_t row, const int64_t col)
134 {
135  int64_t rblock,rblockindex,cblock,cblockindex;
136  FindBlockIndex(row,rblock,rblockindex);
137  if(rblock == -1) return this->gZero;
138  FindBlockIndex(col,cblock,cblockindex);
139  if(cblock != rblock) return this->gZero;
140  int64_t pos = rblockindex + cblockindex*this->fBlockSize[rblock];
141  return this->fStorage[this->fBlockPos[rblock]+pos];
142 }
143 
144 template<class TVar>
146 {
148  Gather(*B,BG);
149  int result = TPZBlockDiagonal<TVar>::Substitution(&BG);
150  B->Zero();
151  Scatter(BG,*B);
152  return result;
153 
154 }
155 
156 template<class TVar>
157 TVar& TPZSparseBlockDiagonal<TVar>::s(const int64_t row, const int64_t col)
158 {
159  int64_t rblock,rblockindex,cblock,cblockindex;
160  FindBlockIndex(row,rblock,rblockindex);
161  if(rblock == -1) return this->gZero;
162  FindBlockIndex(col,cblock,cblockindex);
163  if(cblock != rblock) return this->gZero;
164  int64_t pos = rblockindex + cblockindex*this->fBlockSize[rblock];
165  return this->fStorage[this->fBlockPos[rblock]+pos];
166 }
167 
168 template<class TVar>
169 void TPZSparseBlockDiagonal<TVar>::Print(const char* message, std::ostream& out, const MatrixOutputFormat format) const
170 {
171  TPZBlockDiagonal<TVar>::Print(message, out, format);
172  if(format == EFormatted)
173  {
174  out << "Equations for each block " << endl;
175  int64_t nbl = fBlockIndex.NElements()-1;
176  int64_t ibl;
177  for(ibl = 0; ibl<nbl ; ibl++)
178  {
179  int64_t first = fBlockIndex[ibl];
180  int64_t last = fBlockIndex[ibl+1];
181  out << "Block " << ibl << " : ";
182  int64_t i;
183  for(i=first; i<last; i++) out << fBlock[i] << " ";
184  out << endl;
185  }
186  }
187 }
188 
189 template<class TVar>
191 {
193 }
194 
195 template<class TVar>
197 {
198  LOGPZ_DEBUG(logger, "TPZSparseBlockDiagonal::BuildFromMatrix");
199  TPZManVector<int64_t> indices;
200  TPZFNMatrix<10000,TVar> submat(0,0);
201  int64_t ibl,nbl = fBlockIndex.NElements()-1;
202  for(ibl=0; ibl<nbl; ibl++)
203  {
204  int64_t nel = this->fBlockSize[ibl];
205  indices.Resize(nel);
206  submat.Resize(nel,nel);
207  int64_t iel,first = fBlockIndex[ibl];
208  for(iel=0; iel<nel; iel++) indices[iel] = fBlock[first+iel];
209  matrix.GetSub(indices,submat);
210  this->SetBlock(ibl,submat);
211  }
212 }
213 
214 template<class TVar>
216 {
218 }
219 
220 template<class TVar>
221 void TPZSparseBlockDiagonal<TVar>::MultAdd(const TPZFMatrix<TVar>& x, const TPZFMatrix<TVar>& y, TPZFMatrix<TVar>& z, const TVar alpha, const TVar beta, const int opt) const
222 {
223  LOGPZ_DEBUG(logger, "TPZSparseBlockDiagonal::MultAdd");
224  TPZFNMatrix<1000,TVar> xsc(0,0),ysc(0,0,0.),zsc(0,0);
225  xsc.Resize(this->fBlock.NElements(),x.Cols());
226  z.Zero();
227  if(abs(beta) != 0.) ysc.Resize(fBlock.NElements(),y.Cols());
228  zsc.Resize(fBlock.NElements(),z.Cols());
229  Gather(x,xsc);
230  if(abs(beta) != 0.) Scatter(y,ysc);
231  TPZBlockDiagonal<TVar>::MultAdd(xsc, ysc, zsc, alpha, beta, opt);
232  Scatter(zsc,z);
233 }
234 
238 template<class TVar>
239 void TPZSparseBlockDiagonal<TVar>::FindBlockIndex(int64_t glob, int64_t &block, int64_t &blockind) const
240 {
241  int64_t numbl = fBlockIndex.NElements()-2;
242  int64_t ieq,ibl;
243  for(ibl = 0; ibl<numbl; ibl++)
244  {
245  for(ieq = fBlockIndex[ibl];ieq<fBlockIndex[ibl+1];ieq++)
246  {
247  if(fBlock[ieq] == glob)
248  {
249  block = ibl;
250  blockind = ieq-fBlockIndex[ibl];
251  return;
252  }
253  }
254  }
255  block = -1;
256  blockind = -1;
257 }
258 
262 template<class TVar>
264 {
265  int64_t neq = fBlock.NElements();
266  int64_t nc = in.Cols();
267  int64_t ieq,ic;
268  for(ic=0; ic<nc; ic++)
269  {
270  for(ieq=0; ieq<neq; ieq++) out(fBlock[ieq],ic) += in.GetVal(ieq,ic);
271  }
272 }
273 
277 template<class TVar>
279 {
280  int64_t neq = fBlock.NElements();
281  int64_t nc = in.Cols();
282  int64_t ieq,ic;
283  for(ic=0; ic<nc; ic++)
284  {
285  for(ieq=0; ieq<neq; ieq++) out(ieq,ic) = in.GetVal(fBlock[ieq],ic);
286  }
287 }
288 
292 template<class TVar>
294 {
295  LOGPZ_DEBUG(logger, "TPZSparseBlockDiagonal::UpdateFrom");
296  if(!mat)
297  {
298  cout << __PRETTY_FUNCTION__ << " called with zero argument\n";
299  return;
300  }
301  this->fDecomposed = ENoDecompose;
302  int64_t nblock = this->fBlockSize.NElements();
303  int64_t b,bsize,pos;
305  for(b=0; b<nblock; b++) {
306  bsize = this->fBlockSize[b];
307  indices.Resize(bsize);
308  int64_t r;
309  pos = this->fBlockPos[b];
310  for(r=0; r<bsize; r++) indices[r] = fBlock[fBlockIndex[b]+r];
311  TPZFMatrix<TVar> block(bsize,bsize,&this->fStorage[pos],bsize*bsize);
312  mat->GetSub(indices,block);
313  }
314 
315 }
316 
317 template <class TVar>
319  return Hash("TPZSparseBlockDiagonal") ^ TPZBlockDiagonal<TVar>::ClassId() << 1;
320 }
321 template class TPZSparseBlockDiagonal<float>;
322 template class TPZSparseBlockDiagonal<double>;
324 
void MultAdd(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &y, TPZFMatrix< TVar > &z, const TVar alpha=1., const TVar beta=0., const int opt=0) const override
Computes z = alpha * opt(this)*x + beta * y.
Contains definitions to LOGPZ_DEBUG, LOGPZ_INFO, LOGPZ_WARN, LOGPZ_ERROR and LOGPZ_FATAL, and the implementation of the inline InitializePZLOG(string) function using log4cxx library or not. It must to be called out of "#ifdef LOG4CXX" scope.
Contains TPZSparseBlockDiagonal class which implements a block diagonal matrix where the blocks are n...
virtual int Substitution(TPZFMatrix< TVar > *B) const override
Makes the backward and forward substitutions whether the matrix was LU decomposed.
const TVar & Get(const int64_t row, const int64_t col) const override
Get value with bound checking.
virtual void UpdateFrom(TPZAutoPointer< TPZMatrix< TVar > > mat) override
Updates the values of the matrix based on the values of the matrix.
MatrixOutputFormat
Defines output format.
Definition: pzmatrix.h:55
int64_t fRow
Number of rows in matrix.
Definition: pzmatrix.h:779
Implements a block diagonal matrix where the blocks are not contiguous. Matrix.
char fDecomposed
Decomposition type used to decompose the current matrix.
Definition: pzmatrix.h:783
TinyFad< 8, T > abs(const TinyFad< 8, T > &in)
Definition: tinyfadeight.h:846
int Put(const int64_t row, const int64_t col, const TVar &value) override
Put values with bounds checking if DEBUG variable is defined.
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object.
Definition: pzmanvector.h:426
static TVar gZero
Initializing value to static variable.
Definition: pzmatrix.h:786
virtual int GetSub(const int64_t sRow, const int64_t sCol, const int64_t rowSize, const int64_t colSize, TPZFMatrix< TVar > &Target) const
Gets submatrix storing it on Target.
Definition: pzmatrix.cpp:602
int PutVal(const int64_t row, const int64_t col, const TVar &value) override
This method don&#39;t make verification if the element exist. It is fast than Put.
TPZSkylMatrix< REAL > matrix
Definition: numatst.cpp:255
virtual void Print(const char *message, std::ostream &out=std::cout, const MatrixOutputFormat format=EFormatted) const override
Prints current matrix data.
void GetBlock(int64_t i, TPZFMatrix< TVar > &block)
void Gather(const TPZFMatrix< TVar > &in, TPZFMatrix< TVar > &out) const
int Zero() override
Makes Zero all the elements.
Definition: pzfmatrix.h:651
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object reallocating the necessary storage, copying the existing objects to the new...
Definition: pzvec.h:373
Contains TPZMatrixclass which implements full matrix (using column major representation).
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
Definition: pzlog.h:87
void Scatter(const TPZFMatrix< TVar > &in, TPZFMatrix< TVar > &out) const
int ClassId() const override
Define the class id associated with the class.
TPZVec< int > fBlockSize
Stores block sizes data.
Definition: pzblockdiag.h:142
void SetBlock(int64_t i, TPZFMatrix< TVar > &block)
Sets a block in the current matrix.
Definition: pzblockdiag.cpp:39
TPZVec< int64_t > fBlockIndex
Index to first element of each block.
void AddBlock(int64_t i, TPZFMatrix< TVar > &block)
Adds a block to current matrix.
Definition: pzblockdiag.cpp:25
Full matrix class. Matrix.
Definition: pzfmatrix.h:32
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
const TVar & GetVal(const int64_t row, const int64_t col) const override
This method don&#39;t make verification if the element exist. It is fast than Get.
int64_t fCol
Number of cols in matrix.
Definition: pzmatrix.h:781
void Initialize(const TPZVec< int > &blocksize)
Initializes current matrix based on blocksize.
Definition: pzblockdiag.cpp:64
virtual int Substitution(TPZFMatrix< TVar > *B) const override
Makes the backward and forward substitutions whether the matrix was LU decomposed.
TPZVec< TVar > fStorage
Stores matrix data.
Definition: pzblockdiag.h:138
void GetBlock(int64_t i, TPZFMatrix< TVar > &block)
Gets a block from current matrix.
Definition: pzblockdiag.cpp:52
TPZVec< int64_t > fBlockPos
Stores blocks data.
Definition: pzblockdiag.h:140
void BuildFromMatrix(TPZMatrix< TVar > &matrix)
int64_t Cols() const
Returns number of cols.
Definition: pzmatrix.h:809
int Resize(const int64_t newRows, const int64_t wCols) override
Redimension a matrix, but maintain your elements.
Definition: pzfmatrix.cpp:1016
void FindBlockIndex(int64_t glob, int64_t &block, int64_t &blockind) const
TPZVec< int64_t > fBlock
Equation numbers for each block.
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
virtual void Print(const char *message, std::ostream &out, const MatrixOutputFormat=EFormatted) const override
Prints current matrix data.
void MultAdd(const TPZFMatrix< TVar > &x, const TPZFMatrix< TVar > &y, TPZFMatrix< TVar > &z, const TVar alpha, const TVar beta, const int opt) const override
Computes z = alpha * opt(this)*x + beta * y.
virtual TVar & s(const int64_t row, const int64_t col) override
The operators check on the bounds if the DEBUG variable is defined.
const TVar & GetVal(const int64_t row, const int64_t col) const override
Get values without bounds checking This method is faster than "Get" if DEBUG is defined.
Definition: pzfmatrix.h:566
Non abstract class which implements full matrices with preallocated storage with (N+1) entries...
Definition: pzfmatrix.h:716
void AddBlock(int64_t i, TPZFMatrix< TVar > &block)
Root matrix class (abstract). Matrix.
Definition: pzmatrix.h:60
This class implements a reference counter mechanism to administer a dynamically allocated object...
int ClassId() const override
Define the class id associated with the class.