NeoPZ
pzsysmp.cpp
Go to the documentation of this file.
1 
6 #include <memory.h>
7 
8 #include "pzsysmp.h"
9 #include "pzfmatrix.h"
10 #include "pzstack.h"
11 
12 // ****************************************************************************
13 //
14 // Constructors and the destructor
15 //
16 // ****************************************************************************
17 
18 template<class TVar>
20 TPZMatrix<TVar>() {
21 #ifdef USING_MKL
22  fPardisoControl.SetMatrix(this);
23 #endif
24 
25 #ifdef CONSTRUCTOR
26  cerr << "TPZSYsmpMatrix(int rows,int cols)\n";
27 #endif
28 }
29 
30 template<class TVar>
32 TPZMatrix<TVar>(rows,cols) {
33 
34 #ifdef USING_MKL
35  fPardisoControl.SetMatrix(this);
36 #endif
37 
38 #ifdef CONSTRUCTOR
39  cerr << "TPZSYsmpMatrix(int rows,int cols)\n";
40 #endif
41 }
42 
43 template<class TVar>
45  // Deletes everything associated with a TPZSYsmpMatrix
46 #ifdef DESTRUCTOR
47  cerr << "~TPZSYsmpMatrix()\n";
48 #endif
49 }
50 
51 template<class TVar>
53 {
55  fIA =copy.fIA;
56  fJA = copy.fJA;
57  fA = copy.fA;
58  fDiag = copy.fDiag;
59 #ifdef USING_MKL
60  fPardisoControl = copy.fPardisoControl;
61  fPardisoControl.SetMatrix(this);
62 #endif
63  return *this;
64 }
65 
66 
67 // ****************************************************************************
68 //
69 // Find the element of the matrix at (row,col) in the stencil matrix
70 //
71 // ****************************************************************************
72 
73 template<class TVar>
74 const TVar &TPZSYsmpMatrix<TVar>::GetVal(const int64_t r,const int64_t c ) const {
75  // Get the matrix entry at (row,col) without bound checking
76  int64_t row(r),col(c);
77  if (r > c) {
78  int64_t temp = r;
79  row = col;
80  col = temp;
81  }
82  for(int ic=fIA[row] ; ic < fIA[row+1]; ic++ ) {
83  if ( fJA[ic] == col ) return fA[ic];
84  }
85  return this->gZero;
86 }
87 
91 template<class TVar>
92 int TPZSYsmpMatrix<TVar>::PutVal(const int64_t r,const int64_t c,const TVar & val )
93 {
94  // Get the matrix entry at (row,col) without bound checking
95  int64_t row(r),col(c);
96  if (r > c) {
97  int64_t temp = r;
98  row = col;
99  col = temp;
100  }
101  for(int ic=fIA[row] ; ic < fIA[row+1]; ic++ ) {
102  if ( fJA[ic] == col )
103  {
104  fA[ic] = val;
105  return 0;
106  }
107  }
108  if (val != (TVar(0.))) {
109  DebugStop();
110  }
111  return 0;
112 
113 }
114 
115 
116 // ****************************************************************************
117 //
118 // Multiply and Multiply-Add
119 //
120 // ****************************************************************************
121 
122 template<class TVar>
124  TPZFMatrix<TVar> &z,
125  const TVar alpha,const TVar beta,const int opt) const {
126  // computes z = beta * y + alpha * opt(this)*x
127  // z and x cannot share storage
128  int64_t ir, ic;
129  int64_t r = (opt) ? this->Cols() : this->Rows();
130 
131  // Determine how to initialize z
132  if(beta != 0) {
133  z = y*beta;
134  } else {
135  z.Zero();
136  }
137 
138  // Compute alpha * A * x
139  int64_t ncols = x.Cols();
140  for (int64_t col=0; col<ncols; col++)
141  {
142  for(int64_t ir=0; ir<this->Rows(); ir++) {
143  for(int64_t ic=fIA[ir]; ic<fIA[ir+1]; ic++) {
144  int64_t jc = fJA[ic];
145  z(ir,col) += alpha * fA[ic] * x.g(jc,col);
146  if(jc != ir)
147  {
148  z(jc,col) += alpha * fA[ic] * x.g(ir,col);
149  }
150  }
151  }
152  }
153 }
154 
155 // ****************************************************************************
156 //
157 // Print the matrix
158 //
159 // ****************************************************************************
160 
161 template<class TVar>
162 void TPZSYsmpMatrix<TVar>::Print(const char *title, std::ostream &out ,const MatrixOutputFormat form) const {
163  // Print the matrix along with a identification title
164  if(form == EInputFormat) {
165  out << "\nTSYsmpMatrix Print: " << title << '\n'
166  << "\tNon zero elements = " << fA.size() << '\n'
167  << "\tRows = " << this->Rows() << '\n'
168  << "\tColumns = " << this->Cols() << '\n';
169  int i;
170  out << "\tIA\tJA\tA\n"
171  << "\t--\t--\t-\n";
172  for(i=0; i<=this->Rows(); i++) {
173  out << i << '\t'
174  << fIA[i] << '\t'
175  << fJA[i] << '\t'
176  << fA[i] << '\n';
177  }
178  for(i=this->Rows()+1; i<fIA[this->Rows()]-1; i++) {
179  out << i << "\t\t"
180  << fJA[i] << '\t'
181  << fA[i] << '\n';
182  }
183  } else {
184  TPZMatrix<TVar>::Print(title,out,form);
185  }
186 }
187 
188 
189 // ****************************************************************************
190 //
191 // Various solvers
192 //
193 // ****************************************************************************
194 
195 template<class TVar>
197  if(!fDiag.size()) fDiag.resize(this->Rows());
198  for(int ir=0; ir<this->Rows(); ir++) {
199  fDiag[ir] = GetVal(ir,ir);
200  }
201 }
202 
205 template<class TVar>
206 void TPZSYsmpMatrix<TVar>::AutoFill(int64_t nrow, int64_t ncol, int symmetric)
207 {
208  if (!symmetric || nrow != ncol) {
209  DebugStop();
210  }
211  TPZFMatrix<TVar> orig;
212  orig.AutoFill(nrow,ncol,symmetric);
213 
214  TPZVec<int64_t> IA(nrow+1);
217  IA[0] = 0;
218  TPZVec<std::set<int64_t> > eqs(nrow);
219  for (int64_t row=0; row<nrow; row++) {
220  eqs[row].insert(row);
221  for (int64_t col = 0; col<ncol; col++) {
222  REAL test = rand()*1./RAND_MAX;
223  if (test > 0.5) {
224  eqs[row].insert(col);
225  if (symmetric) {
226  eqs[col].insert(row);
227  }
228  }
229  }
230  }
231  int64_t pos=0;
232  for (int64_t row=0; row< nrow; row++) {
233  for (std::set<int64_t>::iterator col = eqs[row].begin(); col != eqs[row].end(); col++) {
234  if(*col >= row)
235  {
236  JA.Push(*col);
237  A.Push(orig(row,*col));
238  }
239  }
240  IA[row+1] = JA.size();
241  }
242  TPZMatrix<TVar>::Resize(nrow,ncol);
243  SetData(IA, JA, A);
244 }
245 
246 #ifdef USING_MKL
247 
248 #include "TPZPardisoControl.h"
261 template<class TVar>
262 int TPZSYsmpMatrix<TVar>::Decompose_LDLt(std::list<int64_t> &singular)
263 {
264  Decompose_LDLt();
265  return 1;
266 }
268 template<class TVar>
270 {
271  if(this->IsDecomposed() == ELDLt) return 1;
272  if (this->IsDecomposed() != ENoDecompose) {
273  DebugStop();
274  }
275  fPardisoControl.SetMatrixType(TPZPardisoControl<TVar>::ESymmetric,TPZPardisoControl<TVar>::EIndefinite);
276  fPardisoControl.Decompose();
277  this->SetIsDecomposed(ELDLt);
278  return 1;
279 
280 }
281 
283 template<class TVar>
285 {
286  if(this->IsDecomposed() == ECholesky) return 1;
287  if (this->IsDecomposed() != ENoDecompose) {
288  DebugStop();
289  }
290 
291  fPardisoControl.SetMatrixType(TPZPardisoControl<TVar>::ESymmetric,TPZPardisoControl<TVar>::EPositiveDefinite);
292  fPardisoControl.Decompose();
293 
294  this->SetIsDecomposed(ECholesky);
295  return 1;
296 }
301 template<class TVar>
302 int TPZSYsmpMatrix<TVar>::Decompose_Cholesky(std::list<int64_t> &singular)
303 {
304  return Decompose_Cholesky();
305 }
306 
307 
308 
316 template<class TVar>
318 {
319  TPZFMatrix<TVar> x(*b);
320  fPardisoControl.Solve(*b,x);
321  *b = x;
322  return 1;
323 }
324 
329 template<class TVar>
331 {
332  return 1;
333 }
334 
339 template<class TVar>
341 {
342  return 1;
343 }
344 
349 template<class TVar>
351 {
352  TPZFMatrix<TVar> x(*b);
353  fPardisoControl.Solve(*b,x);
354  *b = x;
355  return 1;
356 }
357 
362 template<class TVar>
364 {
365  return 1;
366 }
367 
368 
369 #endif
370 
371 
372 template<class TVar>
374  return Hash("TPZSYsmpMatrix") ^ TPZMatrix<TVar>::ClassId() << 1;
375 }
376 template class TPZSYsmpMatrix<double>;
377 template class TPZSYsmpMatrix<float>;
378 template class TPZSYsmpMatrix<long double>;
int ClassId() const override
Define the class id associated with the class.
Definition: pzsysmp.cpp:373
Definition: pzmatrix.h:52
virtual int PutVal(const int64_t, const int64_t, const TVar &val) override
Put values without bounds checking This method is faster than "Put" if DEBUG is defined.
Definition: pzsysmp.cpp:92
void AutoFill(int64_t nrow, int64_t ncol, int symmetric)
Fill matrix storage with randomic values.
Definition: pzsysmp.cpp:206
virtual void Print(const char *title, std::ostream &out=std::cout, const MatrixOutputFormat=EFormatted) const override
Print the matrix along with a identification title.
Definition: pzsysmp.cpp:162
virtual void resize(const int64_t newsize)
Definition: pzvec.h:213
Definition: test.py:1
MatrixOutputFormat
Defines output format.
Definition: pzmatrix.h:55
virtual int Resize(const int64_t newRows, const int64_t newCols)
Redimensions a matriz keeping the previous values.
Definition: pzmatrix.h:278
void SetIsDecomposed(int val)
Sets current matrix to decomposed state.
Definition: pzmatrix.h:410
virtual int Subst_LBackward(TPZFMatrix< TVar > *b) const
Computes B = Y, where A*Y = B, A is upper triangular with A(i,i)=1.
Definition: pzmatrix.cpp:1358
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
virtual int Subst_Forward(TPZFMatrix< TVar > *b) const
Computes B = Y, where A*Y = B, A is lower triangular.
Definition: pzmatrix.cpp:1284
static TVar gZero
Initializing value to static variable.
Definition: pzmatrix.h:786
virtual int Decompose_LDLt()
Decomposes the current matrix using LDLt.
Definition: pzmatrix.cpp:1184
TPZVec< TVar > & A()
Access function for the coefficients.
Definition: pzsysmp.h:91
virtual void SetData(const TPZVec< int64_t > &IA, const TPZVec< int64_t > &JA, const TPZVec< TVar > &A)
Sets data to the class.
Definition: pzsysmp.h:198
int Zero() override
Makes Zero all the elements.
Definition: pzfmatrix.h:651
int64_t size() const
Returns the number of elements of the vector.
Definition: pzvec.h:196
virtual 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 = beta * y + alpha * opt(this)*x.
Definition: pzsysmp.cpp:123
TPZVec< int64_t > fJA
Definition: pzsysmp.h:187
TPZVec< int64_t > fIA
Definition: pzsysmp.h:186
void Push(const T object)
Pushes a copy of the object on the stack.
Definition: pzstack.h:80
int IsDecomposed() const
Checks if current matrix is already decomposed.
Definition: pzmatrix.h:405
Contains TPZMatrixclass which implements full matrix (using column major representation).
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
TPZSYsmpMatrix()
Constructor based on number of rows and columns.
Definition: pzsysmp.cpp:19
TPZSYsmpMatrix & operator=(const TPZSYsmpMatrix< TVar > &copy)
Definition: pzsysmp.cpp:52
int64_t Rows() const
Returns number of rows.
Definition: pzmatrix.h:803
virtual int Subst_Diag(TPZFMatrix< TVar > *b) const
Computes B = Y, where A*Y = B, A is diagonal matrix.
Definition: pzmatrix.cpp:1384
TPZVec< TVar > fA
Definition: pzsysmp.h:188
TPZVec< int64_t > & IA()
Definition: pzsysmp.h:96
Full matrix class. Matrix.
Definition: pzfmatrix.h:32
virtual int Subst_LForward(TPZFMatrix< TVar > *b) const
Computes B = Y, where A*Y = B, A is lower triangular with A(i,i)=1.
Definition: pzmatrix.cpp:1333
A simple stack.
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
TPZVec< TVar > fDiag
Definition: pzsysmp.h:194
virtual int Decompose_Cholesky()
Decomposes the current matrix using Cholesky method. The current matrix has to be symmetric...
Definition: pzmatrix.cpp:1252
void ComputeDiagonal()
Definition: pzsysmp.cpp:196
Implements a symmetric sparse matrix. Matrix.
Definition: pzsysmp.h:22
void AutoFill(int64_t nrow, int64_t ncol, int symmetric)
Fill matrix storage with randomic values.
Definition: pzmatrix.cpp:1960
int64_t Cols() const
Returns number of cols.
Definition: pzmatrix.h:809
virtual void Print(std::ostream &out) const
Definition: pzmatrix.h:253
virtual ~TPZSYsmpMatrix()
Destructor.
Definition: pzsysmp.cpp:44
virtual const TVar & GetVal(const int64_t row, const int64_t col) const override
Get the matrix entry at (row,col) without bound checking.
Definition: pzsysmp.cpp:74
TPZVec< int64_t > & JA()
Definition: pzsysmp.h:101
T * end() const
Returns a pointer to the last+1 element.
Definition: pzvec.h:455
TVar & g(const int64_t row, const int64_t col) const
Definition: pzfmatrix.h:594
int ClassId() const override
Define the class id associated with the class.
Definition: pzmatrix.h:957
virtual int Subst_Backward(TPZFMatrix< TVar > *b) const
Computes B = Y, where A*Y = B, A is upper triangular.
Definition: pzmatrix.cpp:1309
Root matrix class (abstract). Matrix.
Definition: pzmatrix.h:60
Contains TPZSYsmpMatrix class which implements a symmetric sparse matrix. Purpose: Defines operation...