NeoPZ
pzdiffmatrix.h
Go to the documentation of this file.
1 
6 #ifndef PZDIFFMATRIX_H
7 #define PZDIFFMATRIX_H
8 
9 #include <ostream>
10 #include "pzmatrix.h"
11 
12 #ifdef _AUTODIFF
13 #include "fadType.h"
14 #endif
15 
18 
26 template <class T>
28 {
29 public:
30  TPZDiffMatrix();
31  TPZDiffMatrix(const int64_t rows, const int64_t cols);
33 
35  {
36  if(fRows *fCols)
37  {
38  fStore = new T[fRows*fCols];
39  for (int64_t i=0; i<fRows*fCols; i++) {
40  fStore[i] = copy.fStore[i];
41  }
42  }
43  }
44 
45 // TPZDiffMatrix &operator=(const TPZDiffMatrix &copy)
46 // {
47 // fRows = copy.fRows;
48 // fCols = copy.fCols;
49 // fDecomposed = copy.fDecomposed;
50 // if (fStore) {
51 // delete []fStore;
52 // fStore = 0;
53 // }
54 // if (fRows*fCols) {
55 // fStore = new T[fRows*fCols];
56 // for (int i=0; i<fRows*fCols; i++) {
57 // fStore[i] = copy.fStore[i];
58 // }
59 // }
60 // }
61 
63  void Redim(const int64_t rows, const int64_t cols);
64 
66  void Multiply(TPZVec<T> & In, TPZVec<T> & Out, const T & scale = T(1.));
67 
68 
70  void Multiply(TPZDiffMatrix<T> & In, TPZDiffMatrix<T> & Out, const T & scale = T(1.));
71 
79  void MultiplyAdd(TPZDiffMatrix<T> & In, TPZDiffMatrix<T> & Out, const T & scale = T(1.));
80 
81 
84 
86  void Add(TPZDiffMatrix<T> & matrix, const T & scale = T(1.));
87 
89  T & operator()(const int64_t i, const int64_t j = 0);
90 
91  void PutVal(const int64_t row,const int64_t col,const T & value );
92 
93  const T &GetVal(const int64_t row,const int64_t col ) const;
94 
98 
113  void AddAlignDiv(TPZVec<T> & gradx,
114  const int64_t varOffset,const int64_t dim,
115  TPZVec<T> & Divergent);
116 
127  void AddDiv(T & dPhi, TPZVec<T> & U,
128  const int64_t dim, TPZVec<T> & Divergent,
129  TPZDiffMatrix<T> & dDivergent);
130 
131  int64_t Cols()const;
132 
133  int64_t Rows()const;
134 
136 
138 
139  void Reset();
140 
141 private:
142 
143  int64_t index(const int64_t i, const int64_t j)const;
144 
145  int64_t fRows, fCols;
146 
147  T * fStore;
148 
150 
151 };
152 
154 template <class T>
155 std::ostream & operator<<(std::ostream & out, TPZDiffMatrix<T> & A)
156 {
157  int64_t i, j;
158  out << "\nTPZDiffMatrix<> " << A.Rows() << " * " << A.Cols();
159  for(i=0;i<A.Rows();i++)
160  {
161  out << "\n\t";
162  for(j=0;j<A.Cols();j++)
163  {
164  out << " " << A(i,j);
165  }
166  }
167  out << std::endl;
168 
169  return out;
170 }
171 
172 template <class T>
174 {
175 }
176 
177 template <class T>
178 inline TPZDiffMatrix<T>::TPZDiffMatrix(const int64_t rows, const int64_t cols):fRows(0), fCols(0), fStore(NULL), fDecomposed(ENoDecompose)
179 {
180  Redim(rows, cols);
181 }
182 
183 template <class T>
185 {
186  if(fStore)delete[] fStore;
187 }
188 
189 
190 template <class T>
191 inline void TPZDiffMatrix<T>::Redim(const int64_t rows, const int64_t cols)
192 {
193  if(rows!=fRows || cols!=fCols){
194  if(fStore)delete[] fStore;
195  fStore = NULL;
196 
197  fRows = rows;
198  fCols = cols;
199  fStore = new T[fRows*fCols];
200  }
201 
202  int64_t i=fRows*fCols -1;
203  for(;i>=0;i--)fStore[i]=T(0.);
204 }
205 
206 template <class T>
208 {
209  matrix.Redim(fCols, fRows);
210  int64_t i, j;
211  for(i=0;i<fRows;i++)
212  for(j=0;j<fCols;j++)
213  matrix(j,i)=operator()(i,j);
214  return matrix;
215 }
216 
217 template <class T>
218 inline int64_t TPZDiffMatrix<T>::index(const int64_t i, const int64_t j)const
219 {
220 #ifdef PZDEBUG
221  if(i<0 || i>=fRows)
222  {
223  PZError << "\nTPZDiffMatrix<T>::index error: row out of bounds\n";
224  DebugStop();
225  }
226  if(j<0 || j>=fCols)
227  {
228  PZError << "\nTPZDiffMatrix<T>::index error: col out of bounds\n";
229  DebugStop();
230  }
231 #endif
232  return i*fCols+j;
233 }
234 
235 template <class T>
236 inline T & TPZDiffMatrix<T>::operator()(const int64_t i, const int64_t j)
237 {
238  return fStore[index(i,j)];
239 }
240 
241 template <class T>
242 inline void TPZDiffMatrix<T>::PutVal(const int64_t row,const int64_t col,const T & value )
243 {
244  fStore[index(row,col)] = value;
245 }
246 
247 
248 template <class T>
249 inline const T & TPZDiffMatrix<T>::GetVal(const int64_t row,const int64_t col ) const
250 {
251  return fStore[index(row,col)];
252 }
253 
254 
255 template <class T>
256 inline void TPZDiffMatrix<T>::Multiply(TPZVec<T> & In, TPZVec<T> & Out, const T & scale)
257 {
258  if(In.NElements()!=fCols)PZError << "\nTPZDiffMatrix<T>::Multiply error: 'In' vector out of bounds\n";
259  if(Out.NElements()!=fRows)Out.Resize(fRows);
260  int64_t i, j;
261  for(i=0;i<fRows;i++)
262  {
263  T buff=0.;
264  for(j=0;j<fCols;j++)
265  {
266  buff+=In[j]*operator()(i,j);
267  }
268  buff*=scale;
269  Out[i]=buff;
270  }
271 }
272 
273 template <class T>
274 inline void TPZDiffMatrix<T>::Multiply(TPZDiffMatrix<T> & In, TPZDiffMatrix<T> & Out, const T & scale)
275 {
276  if(fCols!=In.fRows)PZError << "\nTPZDiffMatrix<T>::Multiply error: 'In' matrix out of bounds\n";
277  Out.Redim(fRows,In.fCols);
278  int64_t i, j, k;
279  for(i=0;i<fRows;i++)
280  {
281  for(j=0;j<In.fCols;j++)
282  {
283  T buff=0.;
284  for(k=0;k<fCols;k++)
285  {
286  buff+=operator()(i,k) * In(k,j);
287  }
288  buff*=scale;
289  Out(i,j)=buff;
290  }
291  }
292 }
293 
294 
295 template <class T>
296 inline void TPZDiffMatrix<T>::MultiplyAdd(TPZDiffMatrix<T> & In, TPZDiffMatrix<T> & Out, const T & scale)
297 {
298  if(fCols!=In.fRows)PZError << "\nTPZDiffMatrix<T>::MultiplyAdd error: 'In' matrix out of bounds\n";
299  if(Out.fCols!=fCols)PZError << "\nTPZDiffMatrix<T>::MultiplyAdd error: 'Out' matrix out of bounds\n";Out.Redim(fRows,In.fCols);
300  if(In.fRows!=Out.fRows)PZError << "\nTPZDiffMatrix<T>::MultiplyAdd error: 'Out' matrix out of bounds\n";Out.Redim(fRows,In.fCols);
301  int64_t i, j, k;
302  for(i=0;i<fRows;i++)
303  {
304  for(j=0;j<In.fCols;j++)
305  {
306  T buff=0.;
307  for(k=0;k<fCols;k++)
308  {
309  buff+=operator()(i,k) * In(k,j);
310  }
311  buff*=scale;
312  Out(i,j)+=buff;
313  }
314  }
315 }
316 
317 template <class T>
318 inline void TPZDiffMatrix<T>::Add(TPZDiffMatrix<T> & matrix, const T & scale)
319 {
320  if(fRows!=matrix.fRows)PZError << "\nTPZDiffMatrix<T>::Add error: row out of bounds\n";
321  if(fCols!=matrix.fCols)PZError << "\nTPZDiffMatrix<T>::add error: col out of bounds\n";
322  int64_t i = fRows * fCols - 1;
323  for(;i>=0;i--)fStore[i]+=matrix.fStore[i]*scale;
324 }
325 
326 template <class T>
328 {
329  Redim(source.fRows, source.fCols);
330  int64_t i = fRows * fCols - 1;
331  for(;i>=0;i--)fStore[i]=source.fStore[i];
332  fDecomposed = source.fDecomposed;
333  return *this;
334 }
335 
336 template <class T>
337 inline void TPZDiffMatrix<T>::AddAlignDiv(TPZVec<T> & gradx, const int64_t varOffset,const int64_t dim, TPZVec<T> & Divergent)
338 {
339  int64_t nstate = dim+2;
340  if(gradx.NElements()!=(nstate)*dim)PZError << "\nTPZDiffMatrix<T>::AddAlignDiv error: gradx vector of wrong size\n";
341  if(Divergent.NElements()!=nstate )PZError << "\nTPZDiffMatrix<T>::AddAlignDiv error: Divergent vector of wrong size\n";
342  //Divergent.Resize(nstate);
343  int64_t i, j;
344  for(i=0;i<nstate;i++)
345  {
346  for(j=0;j<nstate;j++)Divergent[i]+=operator()(i,j)*gradx[j*dim+varOffset];
347  }
348 }
349 
350 template <class T>
351 inline void TPZDiffMatrix<T>::AddDiv(T & dPhi, TPZVec<T> & U, const int64_t dim, TPZVec<T> & Divergent, TPZDiffMatrix<T> & dDivergent)
352 {
353  int64_t nstate = dim+2;
354  if( U.NElements()!=nstate)PZError << "\nTPZDiffMatrix<T>::AddDiv error: U vector of wrong size\n";
355  if(Divergent.NElements()!=nstate)PZError << "\nTPZDiffMatrix<T>::AddDiv error: Divergent vector of wrong size\n";
356 
357  int64_t i, j;
358  for(i=0;i<nstate;i++)
359  {
360  for(j=0;j<nstate;j++)
361  {
362  Divergent[i]+=operator()(i,j)*dPhi*U[j];
363  dDivergent(i,j)+=operator()(i,j)*dPhi;//?
364  }
365  }
366 }
367 
368 template <class T>
369 inline int64_t TPZDiffMatrix<T>::Cols()const
370 {
371  return fCols;
372 }
373 
374 
375 template <class T>
376 inline int64_t TPZDiffMatrix<T>::Rows()const
377 {
378  return fRows;
379 }
380 
381 template <class T>
383 
384  if (fDecomposed == ELU)
385  {
386  PZError << "\npzdiffmatrix.h Attempting to decompose an already decomposed matrix\n";
387  return EOk;
388  }
389 
390  T nn, pivot;
391  int64_t min = ( Cols() < (Rows()) ) ? Cols() : Rows();
392 
393  for ( int64_t k = 0; k < min ; k++ ) {
394  if (IsZero( pivot = GetVal(k, k))) return EZeroPivot;
395  for ( int64_t i = k+1; i < Rows(); i++ ) {
396  nn = GetVal( i, k ) / pivot;
397  PutVal( i, k, nn );
398  for ( int64_t j = k+1; j < Cols(); j++ ) PutVal(i,j,GetVal(i,j)-nn*GetVal(k,j));
399  }
400  }
402  return EOk;
403 }
404 
405 template <class T>
407 
408  int64_t rowb = B->Rows();
409  int64_t colb = B->Cols();
410  if ( rowb != Rows() )
411  return EIncompDim;
412  int64_t i;
413  for ( i = 0; i < rowb; i++ ) {
414  for ( int64_t col = 0; col < colb; col++ ) {
415  for ( int64_t j = 0; j < i; j++ ) {
416  B->PutVal( i, col, B->GetVal(i, col)-GetVal(i, j) * B->GetVal(j, col) );
417  }
418  }
419  }
420  for (int64_t col=0; col<colb; col++) {
421  for ( i = rowb-1; i >= 0; i-- ) {
422  for ( int64_t j = i+1; j < rowb ; j++ ) {
423  B->PutVal( i, col, B->GetVal(i, col) -
424  GetVal(i, j) * B->GetVal(j, col) );
425  }
426  if ( IsZero( GetVal(i, i) ) ) {
427  return EZeroPivot;
428  }
429  B->PutVal( i, col, B->GetVal( i, col) / GetVal(i, i) );
430  }
431  }
432  return EOk;
433 }
434 
435 template <class T>
437 {
439 }
440 
441 
442 #endif
TPZDiffMatrix< T > & operator=(const TPZDiffMatrix< T > &source)
Copies the matrix, reallocating all coefficients.
Definition: pzdiffmatrix.h:327
void PutVal(const int64_t row, const int64_t col, const T &value)
Definition: pzdiffmatrix.h:242
void Add(TPZDiffMatrix< T > &matrix, const T &scale=T(1.))
Adds element by element.
Definition: pzdiffmatrix.h:318
int64_t index(const int64_t i, const int64_t j) const
Definition: pzdiffmatrix.h:218
bool IsZero(long double a)
Returns if the value a is close Zero as the allowable tolerance.
Definition: pzreal.h:668
void AddDiv(T &dPhi, TPZVec< T > &U, const int64_t dim, TPZVec< T > &Divergent, TPZDiffMatrix< T > &dDivergent)
Computes the divergent for diffusion purposes.
Definition: pzdiffmatrix.h:351
const T & GetVal(const int64_t row, const int64_t col) const
Definition: pzdiffmatrix.h:249
TPZDiffMatrix(const TPZDiffMatrix &copy)
Definition: pzdiffmatrix.h:34
TPZDiffMatrix< T > & Transpose(TPZDiffMatrix< T > &matrix)
Transposes the matrix onto the parameter object.
Definition: pzdiffmatrix.h:207
This class implements a simple vector storage scheme for a templated class T. Utility.
Definition: pzgeopoint.h:19
EStatus Substitution(TPZDiffMatrix< T > *B) const
Definition: pzdiffmatrix.h:406
void MultiplyAdd(TPZDiffMatrix< T > &In, TPZDiffMatrix< T > &Out, const T &scale=T(1.))
Matrix multiplication.
Definition: pzdiffmatrix.h:296
TPZSkylMatrix< REAL > matrix
Definition: numatst.cpp:255
void Redim(const int64_t rows, const int64_t cols)
Resizes and zeroes the matrix.
Definition: pzdiffmatrix.h:191
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
void AddAlignDiv(TPZVec< T > &gradx, const int64_t varOffset, const int64_t dim, TPZVec< T > &Divergent)
Performs a specific diffusive divergence operation.
Definition: pzdiffmatrix.h:337
int64_t Rows() const
Definition: pzdiffmatrix.h:376
int64_t Cols() const
Definition: pzdiffmatrix.h:369
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
EStatus
Definition: pzdiffmatrix.h:17
Definition: pzmatrix.h:52
Contains TPZMatrix<TVar>class, root matrix class.
void Multiply(TPZVec< T > &In, TPZVec< T > &Out, const T &scale=T(1.))
Multiplies the matrix by a correspondent TPZVec vector. Dimensions are checked.
Definition: pzdiffmatrix.h:256
Matrix class to hold the flux derivatives A B C and diffusive matrix coefficients. Matrix.
Definition: pzdiffmatrix.h:27
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
T & operator()(const int64_t i, const int64_t j=0)
Matrix data access.
Definition: pzdiffmatrix.h:236
EStatus Decompose_LU()
Definition: pzdiffmatrix.h:382
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15