NeoPZ
TPZFrontMatrix.cpp
Go to the documentation of this file.
1 
6 #include "TPZFrontMatrix.h"
7 #include "pzsfulmat.h"
8 #include "TPZFront.h"
9 #include "pzstack.h"
10 #include "pzreal.h"
11 #include <math.h>
12 
13 #include "tpzeqnarray.h"
14 #include <iostream>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <fstream>
18 
19 #include "pzlog.h"
20 
21 #ifdef LOG4CXX
22 static LoggerPtr logger(Logger::getLogger("pz.frontal.frontmerda"));
23 static LoggerPtr loggerfw(Logger::getLogger("pz.frontal.frontmerdatotal"));
24 #endif
25 
26 using namespace std;
27 
28 template<class TVar, class store, class front>
30  return fFront.Work();
31 }
32 template<class TVar, class store, class front>
33 void TPZFrontMatrix<TVar,store, front>::EquationsToDecompose(TPZVec<int64_t> &destinationindex, int64_t &lower_eq, int64_t &upper_eq)
34 {
35  int64_t i;
36  int64_t loop_limit, global;
37  loop_limit = destinationindex.NElements();
38  for(i=0;i<loop_limit;i++){
39  global = destinationindex[i];
40  fNumElConnected[global]--;
41  }
42  upper_eq=fLastDecomposed;
43  lower_eq=fLastDecomposed+1;
44  while(upper_eq < fNumEq-1 && fNumElConnected[upper_eq+1]==0) upper_eq++;
45  fLastDecomposed=upper_eq;
46 #ifdef LOG4CXX
47  if (logger->isDebugEnabled())
48  {
49  std::stringstream sout;
50  sout << "Constructor frontmatrix lower_eq "<< lower_eq << " upper_eq " << upper_eq << " fNumElConnected " << fNumElConnected;
51  LOGPZ_DEBUG(logger,sout.str())
52  }
53 #endif
54 }
55 
57 template<class TVar, class store, class front>
59  fNumElConnected.Resize(numelconnected.NElements());
60  fNumElConnected=numelconnected;
61  fNumElConnectedBackup = fNumElConnected;
62 #ifdef LOG4CXX
63  if (logger->isDebugEnabled())
64  {
65  std::stringstream sout;
66  sout << "fNumElConnected " << fNumElConnected;
67  LOGPZ_DEBUG(logger,sout.str())
68  }
69 #endif
70 }
71 
73 template<class TVar, class store, class front>
75 
76  // message #1.3 to fFront:TPZFront
77  fFront.AddKel(elmat, destinationindex);
78 #ifdef LOG4CXX
79  if (loggerfw->isInfoEnabled())
80  {
81  std::stringstream sout;
82  sout << "Frondwidth after AddKel "<< fFront.NElements();
83  LOGPZ_INFO(loggerfw,sout.str())
84  }
85 #endif
86  int64_t mineq, maxeq;
87 
88  EquationsToDecompose(destinationindex, mineq, maxeq);
89  TPZEqnArray<TVar> AuxEqn;
90  if(maxeq >= mineq) {
91 
92  fFront.DecomposeEquations(mineq,maxeq,AuxEqn);
93  CheckCompress();
94  fStorage.AddEqnArray(&AuxEqn);
95  if(maxeq == this->Rows()-1){
96  fStorage.FinishWriting();
97  fStorage.ReOpen();
98  }
99  }
100  this->fDecomposed = fFront.GetDecomposeType();
101 }
102 
104 template<class TVar, class store, class front>
106 {
107  fFront.AddKel(elmat, sourceindex, destinationindex);
108 #ifdef LOG4CXX
109  if (loggerfw->isInfoEnabled())
110  {
111  std::stringstream sout;
112  sout << "Frondwidth after AddKel "<< fFront.FrontSize();
113  LOGPZ_INFO(loggerfw,sout.str())
114  }
115 #endif
116 
117  int64_t mineq, maxeq;
118  EquationsToDecompose(destinationindex, mineq, maxeq);
119  TPZEqnArray<TVar> AuxEqn;
120  if(maxeq >= mineq) {
121  fFront.DecomposeEquations(mineq,maxeq,AuxEqn);
122  CheckCompress();
123  fStorage.AddEqnArray(&AuxEqn);
124  if(maxeq == this->Rows()-1){
125  fFront.Reset(0);
126  fStorage.FinishWriting();
127  fStorage.ReOpen();
128  }
129  }
130  this->fDecomposed = fFront.GetDecomposeType();
131 }
132 
134 template<class TVar, class store, class front>
136 {
137  fFront.SymbolicAddKel(destinationindex);
138  int64_t mineq, maxeq;
139  EquationsToDecompose(destinationindex, mineq, maxeq);
140 
141  if(maxeq >= mineq) {
142  fFront.SymbolicDecomposeEquations(mineq,maxeq);
143  CheckCompress();
144  }
145 
146 }
147 
148 
149 template<class TVar, class store, class front>
151 }
152 
153 
154 template<class TVar, class store, class front>
157 {
158  fFront.Reset();
159  fStorage.Reset();
162  fLastDecomposed = -1;
163  fNumEq=0;
164 }
165 
166 template<class TVar, class store, class front>
168 TPZAbstractFrontMatrix<TVar>(globalsize,globalsize)
169 {
170  fFront.Reset(globalsize);
171  fStorage.Reset();
174  fLastDecomposed = -1;
175  fNumEq=globalsize;
176 }
177 
178 template<class TVar, class store, class front>
180  return Hash("TPZFrontMatrix") ^ TPZAbstractFrontMatrix<TVar>::ClassId() << 1 ^ store().ClassId() << 2 ^ front().ClassId() << 3;
181 }
182 
183 template<class TVar, class store, class front>
184 void TPZFrontMatrix<TVar,store, front>::Print(const char *name, std::ostream& out,const MatrixOutputFormat form) const
185 {
186  int64_t i;
187  out << "Frontal Matrix associated"<< endl;
188  fFront.Print(name, out);
189  out << "Stored Equations" << endl;
190  fStorage.Print(name, out);
191  out << "Number of Equations " << fNumEq << endl;
192  out << "Number of Elements connected to DF" << endl;
193  for(i=0;i<fNumElConnected.NElements();i++){
194  out << i << " " << fNumElConnected[i] << endl;
195  }
196 }
197 
198 template<class TVar, class store, class front>
200 {
201  TPZFMatrix<TVar> KEl1(2,2);
202  KEl1(0,0)=4.;
203  KEl1(0,1)=6.;
204  KEl1(1,0)=6.;
205  KEl1(1,1)=12.;
206  TPZVec<int64_t> DestInd1(2);
207  DestInd1[0]=0;
208  DestInd1[1]=1;
209 
210 
211  TPZFMatrix<TVar> KEl2(4,4);
212  KEl2(0,0)=4.;
213  KEl2(0,1)=-6.;
214  KEl2(0,2)=2.;
215  KEl2(0,3)=6.;
216 
217  KEl2(1,1)=12;
218  KEl2(1,2)=-6.;
219  KEl2(1,3)=-12.;
220 
221  KEl2(2,2)=4.;
222  KEl2(2,3)=6.;
223 
224  KEl2(3,3)=12.;
225 
226  int i, j;
227  for(i=0;i<4;i++) {
228  for(j=i;j<4;j++) KEl2(j,i)=KEl2(i,j);
229  }
230 
231  TPZVec<int64_t> DestInd2(4);
232  DestInd2[0]=0;
233  DestInd2[1]=1;
234  DestInd2[2]=2;
235  DestInd2[3]=3;
236 
237  TPZFMatrix<TVar> KEl3(2,2);
238  KEl3(0,0)=3.;
239  KEl3(0,1)=3.;
240  KEl1(1,0)=3.;
241  KEl3(1,1)=6.;
242 
243  TPZVec<int64_t> DestInd3(2);
244  DestInd3[0]=2;
245  DestInd3[1]=3;
246 
247  TPZFrontMatrix TestFront(4);
248 
249  TPZVec<int> NumConnected(4);
250  for(i=0;i<4;i++) NumConnected[i]=2;
251 
252  TestFront.SetNumElConnected(NumConnected);
253 
254  TestFront.SymbolicAddKel(DestInd1);
255  TestFront.SymbolicAddKel(DestInd2);
256  TestFront.SymbolicAddKel(DestInd3);
257 
258  TestFront.AllocData();
259  TestFront.SetNumElConnected(NumConnected);
260 
261  TestFront.AddKel(KEl1,DestInd1);
262  TestFront.AddKel(KEl2,DestInd2);
263  TestFront.AddKel(KEl3,DestInd3);
264 }
265 
266 template<class TVar, class store, class front>
268 {
269  double nfreerate = ( (double)fFront.NFree() / (double)fFront.FrontSize() ) * 100;
270  if(nfreerate>20.)
271  {
272 #ifdef LOG4CXX
273  if (logger->isDebugEnabled())
274  {
275  std::stringstream sout;
276  sout << "Compressing front nfreerate " << nfreerate << " NFree " << fFront.NFree() << " Front elements " << fFront.FrontSize();
277  LOGPZ_DEBUG(logger,sout.str())
278  }
279 #endif
280  fFront.Compress();
281 #ifdef LOG4CXX
282  if (loggerfw->isInfoEnabled())
283  {
284  std::stringstream sout;
285  sout << "Frondwidth after Compress "<< fFront.FrontSize();
286  LOGPZ_INFO(loggerfw,sout.str())
287  }
288 #endif
289  }
290 }
291 
292 template<class TVar, class store, class front>
294  if (fFront.GetDecomposeType() != dt) {
295  DebugStop();
296  }
297 #ifdef LOG4CXX2
298  if (logger->isDebugEnabled())
299  {
300 
301  std::stringstream sout;
302  B.Print("On input " , sout);
303  LOGPZ_DEBUG(logger, sout.str())
304  }
305 #endif
306  Subst_Forward(&B);
307 #ifdef LOG4CXX
308  if (logger->isDebugEnabled())
309  {
310  std::stringstream sout;
311  B.Print("After forward and diagonal " , sout);
312  LOGPZ_DEBUG(logger, sout.str())
313  }
314 #endif
315  Subst_Backward(&B);
316 #ifdef LOG4CXX
317  if (logger->isDebugEnabled())
318  {
319  std::stringstream sout;
320  B.Print("Final result " , sout);
321  LOGPZ_DEBUG(logger, sout.str())
322  }
323 #endif
324  return 1;
325 }
326 
327 
328 template<class TVar, class store, class front>
330 {
331  fFront.AllocData();
332  fLastDecomposed=-1;
333 }
334 
335 template<class TVar, class store, class front>
337  fStorage.Forward(*b, ELU);
338  fStorage.Backward(*b, ELU);
339  return 1;
340 }
341 
342 template<class TVar, class store, class front>
344  cout << "Entering Forward Substitution\n";
345  cout.flush();
346  DecomposeType dec = fFront.GetDecomposeType();
347 // if(dec != ECholesky) cout << "TPZFrontMatrix::Subst_Forward non matching decomposition\n";
348  fStorage.Forward(*b, dec);
349  return 1;
350 }
351 
352 template<class TVar, class store, class front>
354  cout << "Entering Backward Substitution\n";
355  cout.flush();
356  DecomposeType dec = fFront.GetDecomposeType();
357 // if(dec != ECholesky) cout << "TPZFrontMatrix::Subst_Forward non matching decomposition\n";
358  fStorage.Backward(*b, dec);
359  return 1;
360 }
361 
362 template<class TVar, class store, class front>
363 void TPZFrontMatrix<TVar,store, front>::SetFileName(char option, const char *name) {
364  fStorage.OpenGeneric(option, name);
365 }
366 
367 template<class TVar, class store, class front>
369  fStorage.FinishWriting();
370 }
371 template<class TVar, class store, class front>
373  fStorage.ReOpen();
374 }
375 
376 template<class TVar, class store, class front>
378  fStorage.Zero();
380  fLastDecomposed = -1;
381  fFront.Reset(fNumEq);
382  return 0;
383 }
384 
385 template<class TVar>
387 template<class TVar>
388 class TPZFileEqnStorage;
389 template<class TVar>
391 template<class TVar>
393 
394 
396 template class TPZFrontMatrix<float,TPZFileEqnStorage<float>, TPZFrontSym<float> >;
398 template class TPZFrontMatrix<float,TPZFileEqnStorage<float>, TPZFrontNonSym<float> >;
399 
401 template class TPZFrontMatrix<double,TPZFileEqnStorage<double>, TPZFrontSym<double> >;
403 template class TPZFrontMatrix<double,TPZFileEqnStorage<double>, TPZFrontNonSym<double> >;
404 
406 template class TPZFrontMatrix<long double,TPZFileEqnStorage<long double>, TPZFrontSym<long double> >;
408 template class TPZFrontMatrix<long double,TPZFileEqnStorage<long double>, TPZFrontNonSym<long double> >;
409 
411 template class TPZFrontMatrix<std::complex<float> ,TPZFileEqnStorage<std::complex<float> >, TPZFrontSym<std::complex<float> > >;
412 template class TPZFrontMatrix<std::complex<float> ,TPZStackEqnStorage<std::complex<float> >, TPZFrontNonSym<std::complex<float> > >;
413 template class TPZFrontMatrix<std::complex<float> ,TPZFileEqnStorage<std::complex<float> >, TPZFrontNonSym<std::complex<float> > >;
414 
416 template class TPZFrontMatrix<std::complex<double> ,TPZFileEqnStorage<std::complex<double> >, TPZFrontSym<std::complex<double> > >;
417 template class TPZFrontMatrix<std::complex<double> ,TPZStackEqnStorage<std::complex<double> >, TPZFrontNonSym<std::complex<double> > >;
418 template class TPZFrontMatrix<std::complex<double> ,TPZFileEqnStorage<std::complex<double> >, TPZFrontNonSym<std::complex<double> > >;
419 
421 template class TPZFrontMatrix<std::complex<long double> ,TPZFileEqnStorage<std::complex<long double> >, TPZFrontSym<std::complex<long double> > >;
422 template class TPZFrontMatrix<std::complex<long double> ,TPZStackEqnStorage<std::complex<long double> >, TPZFrontNonSym<std::complex<long double> > >;
423 template class TPZFrontMatrix<std::complex<long double> ,TPZFileEqnStorage<std::complex<long double> >, TPZFrontNonSym<std::complex<long double> > >;
Has the same porpouse of EqnStack but stores the EqnArrays in a different form (binary files)...
int64_t fNumEq
Indicates number of equations.
void CheckCompress()
Checks if FrontMatrix needs a compression, if so calls Compress method.
int Subst_Backward(TPZFMatrix< TVar > *b) const override
Backward substitution and result is on b.
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.
TPZFrontMatrix()
Simple Constructor.
Responsible for the frontal method as a whole. Frontal.
It is an equation array, generally in its decomposed form. Frontal.
Definition: tpzeqnarray.h:36
Contains the TPZFront class which implements decomposition process of the frontal matrix...
Responsible for storing arrays of equations (mostly in a decomposed form). Frontal.
Implements a matrix stored in a frontal decomposition scheme. Frontal.
store fStorage
Indicates storage schema. Assumes values TPZFileEqnStorage for binary file and TPZStackEqnStorage for...
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
Abstract class implements storage and decomposition process of the frontal matrix involving simmetry ...
TPZVec< int > fNumElConnectedBackup
Contains the number of elements which still need to contribute to a given equation.
Contains the TPZEqnArray class which implements an equation array.
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 SymbolicAddKel(TPZVec< int64_t > &destinationindex)
Add a contribution of a stiffness matrix using the indexes to compute the frontwidth. It does it symbolicaly.
#define LOGPZ_INFO(A, B)
Define log for informations.
Definition: pzlog.h:89
int Subst_Forward(TPZFMatrix< TVar > *b) const override
Forward substitution and result is on b.
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
void FinishWriting()
Finishes writing of a binary file and closes it.
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
Definition: pzlog.h:87
void SetNumElConnected(TPZVec< int > &numelconnected)
Initializes the number of elements connected to each equation.
Definition: pzmatrix.h:52
~TPZFrontMatrix()
Simple Destructor.
int64_t fLastDecomposed
Indicates last decomposed equation.
void ReOpen()
Reopen the binary file.
int ClassId() const override
Define the class id associated with the class.
front fFront
Indicates Front matrix type. Assumes values TPZFrontSym for symmetric front and TPZFrontNonSym for no...
int ClassId() const override
Define the class id associated with the class.
Full matrix class. Matrix.
Definition: pzfmatrix.h:32
int Substitution(TPZFMatrix< TVar > *) const override
Executes a substitution on a TPZFMatrix<REAL> object applies both forward and backward substitution a...
A simple stack.
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
void EquationsToDecompose(TPZVec< int64_t > &destinationindex, int64_t &lower_eq, int64_t &upper_eq)
Sends a message to decompose equations from lower_eq to upper_eq, according to destination index...
TPZVec< int > fNumElConnected
Contains the number of elements which still need to contribute to a given equation.
virtual int Zero() override
Reinitialize the structure of the frontal matrix.
virtual void AddKel(TPZFMatrix< TVar > &elmat, TPZVec< int64_t > &destinationindex) override
Add a contribution of a stiffness matrix putting it on destination indexes position.
void SetFileName(char option, const char *name)
Sets a file name for generic input or output.
Contains the TPZAbstractFrontMatrix class which implements a matrix stored in a frontal decomposition...
void Print(const char *name, std::ostream &out, const MatrixOutputFormat form=EFormatted) const override
Prints a FrontMatrix object.
virtual void Print(std::ostream &out) const
Definition: pzmatrix.h:253
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
Contains the declaration of TPZFlopCounter class and TPZCounter struct.
static void main()
void AllocData()
Allocates data for the FrontMatrix.
Abstract class implements storage and decomposition process of the frontal matrix involving non-simet...
Contains TPZSFMatrix class which implements a symmetric full matrix.
DecomposeType
Defines decomposition type for any matrix classes.
Definition: pzmatrix.h:52