NeoPZ
TPZSpStructMatrix.cpp
Go to the documentation of this file.
1 
6 #include "TPZSpStructMatrix.h"
7 #include "pzstrmatrix.h"
8 
9 #include "pzgeoelbc.h"
10 #include "pzgmesh.h"
11 #include "pzcmesh.h"
12 
13 #include "pzanalysis.h"
14 #include "pzsolve.h"
15 #include "pzstepsolver.h"
16 
17 #include "pzdxmesh.h"
18 #include <fstream>
19 
20 #include "pzelmat.h"
21 
22 #include "pzysmp.h"
23 #include "pzmetis.h"
24 #include "pzbndcond.h"
25 #include "TPZTimer.h"
26 
27 #include "pzlog.h"
28 #ifdef LOG4CXX
29 static LoggerPtr logger(Logger::getLogger("pz.StrMatrix"));
30 #endif
31 
32 using namespace std;
33 
35  return new TPZSpStructMatrix(*this);
36 }
38  TPZAutoPointer<TPZGuiInterface> guiInterface){
39 
40 #ifdef LOG4CXX
41  if(logger->isDebugEnabled())
42  {
43  LOGPZ_DEBUG(logger,"TPZSpStructMatrix::CreateAssemble starting")
44  }
45 #endif
46 
47  int64_t neq = fMesh->NEquations();
48  if(fMesh->FatherMesh()) {
49  cout << "TPZSpStructMatrix should not be called with CreateAssemble for a substructure mesh\n";
50  return new TPZFYsmpMatrix<STATE>(0,0);
51  }
52  TPZMatrix<STATE> *stiff = Create();//new TPZFYsmpMatrix(neq,neq);
53  TPZFYsmpMatrix<STATE> *mat = dynamic_cast<TPZFYsmpMatrix<STATE> *> (stiff);
54  rhs.Redim(neq,1);
55  //stiff->Print("Stiffness TPZFYsmpMatrix :: CreateAssemble()");
56  TPZTimer before("Assembly of a sparse matrix");
57  before.start();
58 #ifdef LOG4CXX
59  if (logger->isDebugEnabled())
60  {
61  LOGPZ_DEBUG(logger,"TPZSpStructMatrix::CreateAssemble calling Assemble()");
62  }
63 #endif
64  Assemble(*stiff,rhs,guiInterface);
65  before.stop();
66  std::cout << __PRETTY_FUNCTION__ << " " << before << std::endl;
67 // mat->ComputeDiagonal();
68  // mat->ComputeDiagonal();
69  //stiff->Print("Stiffness TPZFYsmpMatrix :: CreateAssemble()");
70 #ifdef LOG4CXX
71  if(logger->isDebugEnabled()) LOGPZ_DEBUG(logger,"TPZSpStructMatrix::CreateAssemble exiting");
72 #endif
73  return stiff;
74 }
76  int64_t neq = fEquationFilter.NActiveEquations();
77  /* if(fMesh->FatherMesh()) {
78  TPZSubCompMesh *smesh = (TPZSubCompMesh *) fMesh;
79  neq = smesh->NumInternalEquations();
80  }*/
81  TPZFYsmpMatrix<STATE> * mat = new TPZFYsmpMatrix<STATE>(neq,neq);
82 
86  TPZStack<int64_t> elgraph;
87  TPZVec<int64_t> elgraphindex;
88  // int nnodes = 0;
89  fMesh->ComputeElGraph(elgraph,elgraphindex);
91  TPZMetis metis;
92  metis.SetElementsNodes(elgraphindex.NElements() -1 ,fMesh->NIndependentConnects());
93  metis.SetElementGraph(elgraph,elgraphindex);
94 
95  TPZManVector<int64_t> nodegraph;
96  TPZManVector<int64_t> nodegraphindex;
101  metis.ConvertGraph(elgraph,elgraphindex,nodegraph,nodegraphindex);
102 
103 #ifdef LOG4CXX2
104  if(logger->isDebugEnabled()){
105  std::stringstream sout;
106  sout << "Node graph \n";
107  metis.TPZRenumbering::Print(nodegraph, nodegraphindex);
108  LOGPZ_DEBUG(logger, sout.str())
109  }
110 #endif
112  int64_t nblock = nodegraphindex.NElements()-1;
113  // number of values in the sparse matrix
114  int64_t totalvar = 0;
115  // number of equations
116  int64_t totaleq = 0;
117  for(int64_t i=0;i<nblock;i++){
118  int64_t iblsize = fMesh->Block().Size(i);
119  int64_t iblpos = fMesh->Block().Position(i);
120  int64_t numactive = fEquationFilter.NumActive(iblpos, iblpos+iblsize);
121  if (!numactive) {
122  continue;
123  }
124  totaleq += iblsize;
125  int64_t icfirst = nodegraphindex[i];
126  int64_t iclast = nodegraphindex[i+1];
127  int64_t j;
128  //longhin
129  totalvar+=iblsize*iblsize;
130  for(j=icfirst;j<iclast;j++) {
131  int64_t col = nodegraph[j];
132  int64_t colsize = fMesh->Block().Size(col);
133  int64_t colpos = fMesh->Block().Position(col);
134  int64_t numactive = fEquationFilter.NumActive(colpos, colpos+colsize);
135  if (!numactive) {
136  continue;
137  }
138  totalvar += iblsize*colsize;
139  }
140  }
141 
142 #ifdef LOG4CXX
143  if (logger->isDebugEnabled()) {
144  std::stringstream sout;
145  sout << "Number of equations " << totaleq << " number of nonzero s " << totalvar;
146  LOGPZ_DEBUG(logger, sout.str())
147  }
148 #endif
149  int64_t ieq = 0;
150  // pos is the position where we will put the column value
151  int64_t pos = 0;
152 
153  nblock=fMesh->NIndependentConnects();
154 
155  TPZManVector<int64_t,400> Eq(totaleq+1);
156  TPZVec<int64_t> EqCol(totalvar);
157  TPZVec<STATE> EqValue(totalvar);
158  for(int64_t i=0;i<nblock;i++){
159  int64_t iblsize = fMesh->Block().Size(i);
160  int64_t iblpos = fMesh->Block().Position(i);
161  TPZManVector<int64_t> rowdestindices(iblsize);
162  for (int64_t ij=0; ij<iblsize; ij++) {
163  rowdestindices[ij] = iblpos+ij;
164  }
165  fEquationFilter.Filter(rowdestindices);
166 
167  int64_t ibleq;
168  // working equation by equation
169  // rowdestindices contains the equation number of each element in the block number "i"
170  for(ibleq=0; ibleq<rowdestindices.size(); ibleq++) {
171  int rowind = rowdestindices[ibleq];
172 // if (rowind != pos) {
173 // DebugStop();
174 // }
175  Eq[ieq] = pos;
176  int64_t colsize,colpos,jbleq;
177  int64_t diagonalinsert = 0;
178  int64_t icfirst = nodegraphindex[i];
179  int64_t iclast = nodegraphindex[i+1];
180  int64_t j;
181  for(j=icfirst;j<iclast;j++)
182  {
183  // col is the block linked to block "i"
184  int64_t col = nodegraph[j];
185 
186  // force the diagonal block to be inserted
187  // the nodegraph does not contain the pointer to itself
188  if(!diagonalinsert && col > i)
189  {
190  diagonalinsert = 1;
191  int64_t colsize = fMesh->Block().Size(i);
192  int64_t colpos = fMesh->Block().Position(i);
193  TPZManVector<int64_t> destindices(colsize);
194  for (int64_t i=0; i<colsize; i++) {
195  destindices[i] = colpos+i;
196  }
197  fEquationFilter.Filter(destindices);
198  int64_t jbleq;
199  for(jbleq=0; jbleq<destindices.size(); jbleq++) {
200  // if(colpos+jbleq == ieq) continue;
201  EqCol[pos] = destindices[jbleq];
202  EqValue[pos] = 0.;
203  // colpos++;
204  // pos is the position within EqCol or EqVal where we will assemble
205  pos++;
206  }
207  }
208  colsize = fMesh->Block().Size(col);
209  colpos = fMesh->Block().Position(col);
210  // optimization statement : if all equations in the range are inactive -> continue
211  if (fEquationFilter.NumActive(colpos, colpos+colsize) == 0) {
212  continue;
213  }
214  TPZManVector<int64_t> destindices(colsize);
215  for (int64_t i=0; i<colsize; i++) {
216  destindices[i] = colpos+i;
217  }
218  fEquationFilter.Filter(destindices);
219  for(jbleq=0; jbleq<destindices.size(); jbleq++) {
220  EqCol[pos] = destindices[jbleq];
221  EqValue[pos] = 0.;
222  colpos++;
223  pos++;
224  }
225  }
226  // all elements are below (last block certainly)
227  if(!diagonalinsert)
228  {
229  diagonalinsert = 1;
230  int64_t colsize = fMesh->Block().Size(i);
231  int64_t colpos = fMesh->Block().Position(i);
232  TPZManVector<int64_t> destindices(colsize);
233  for (int64_t i=0; i<colsize; i++) {
234  destindices[i] = colpos+i;
235  }
236  fEquationFilter.Filter(destindices);
237  int64_t jbleq;
238  for(jbleq=0; jbleq<destindices.size(); jbleq++) {
239  // if(colpos+jbleq == ieq) continue;
240  EqCol[pos] = destindices[jbleq];
241  EqValue[pos] = 0.;
242  // colpos++;
243  pos++;
244  }
245  }
246  ieq++;
247  }
248  }
249  Eq[ieq] = pos;
250  if(pos != totalvar)
251  {
252  DebugStop();
253  }
254  mat->SetData(Eq,EqCol,EqValue);
255  return mat;
256 }
257 
259 }
260 
262 {}
263 
265  return Hash("TPZSpStructMatrix") ^ TPZStructMatrix::ClassId() << 1;
266 }
267 
268 #ifndef STATE_COMPLEX
269 #include "pzmat2dlin.h"
270 
272 
273  int refine=5;
274  int order=5;
275 
276  TPZGeoMesh gmesh;
277  TPZCompMesh cmesh(&gmesh);
278  double coordstore[4][3] = {{0.,0.,0.},{1.,0.,0.},{1.,1.,0.},
279  {0.,1.,0.}};
280 
281  int i,j;
282  TPZVec<REAL> coord(3,0.);
283  for(i=0; i<4; i++) {
284  // initializar as coordenadas do no em um vetor
285  for (j=0; j<3; j++) coord[j] = coordstore[i][j];
286 
287  // identificar um espa� no vetor onde podemos armazenar
288  // este vetor
289  gmesh.NodeVec ().AllocateNewElement ();
290 
291  // initializar os dados do n� gmesh.NodeVec ()[nodeindex].Initialize (i,coord,gmesh);
292  }
293  int el;
294  TPZGeoEl *gel;
295  for(el=0; el<1; el++) {
296 
297  // initializar os indices dos nos
298  TPZVec<int64_t> indices(4);
299  for(i=0; i<4; i++) indices[i] = i;
300  // O proprio construtor vai inserir o elemento na malha
301  // gel = new TPZGeoElQ2d(el,indices,1,gmesh);
302  int64_t index;
303  gel = gmesh.CreateGeoElement(EQuadrilateral,indices,1,index);
304  }
305  gmesh.BuildConnectivity ();
306 
307  TPZVec<TPZGeoEl *> subel;
308 
309  cout << "Refinement ";
310  cin >> refine;
311  cout << endl;
312  DebugStop();
313 // UniformRefine(refine,gmesh);
314 
315  TPZGeoElBC gelbc(gel,4,-4);
316  TPZMat2dLin *meumat = new TPZMat2dLin(1);
317  TPZFMatrix<STATE> xk(1,1,1.),xc(1,2,0.),xf(1,1,1.);
318  meumat->SetMaterial (xk,xc,xf);
319  TPZMaterial * meumatptr(meumat);
320  cmesh.InsertMaterialObject(meumatptr);
321 
322  TPZFMatrix<STATE> val1(1,1,0.),val2(1,1,0.);
323  TPZMaterial * bnd = meumat->CreateBC (meumatptr,-4,0,val1,val2);
324  cmesh.InsertMaterialObject(bnd);
325 
326  cout << "Interpolation order ";
327  cin >> order;
328  cout << endl;
329 
330  //TPZCompEl::gOrder = order;
331  cmesh.SetDefaultOrder(order);
332 
333  cmesh.AutoBuild();
334  // cmesh.AdjustBoundaryElements();
335  cmesh.InitializeBlock();
336 
337  ofstream output("outputPar.dat");
338  TPZAnalysis an(&cmesh,true,output);
339 
340  TPZVec<int> numelconnected(cmesh.NEquations(),0);
341  TPZSpStructMatrix mat(&cmesh);
342 
343  an.SetStructuralMatrix(mat);
344 
346  sol.SetJacobi(100,1.e-5,0);
347 
348 
349  an.SetSolver(sol);
350  an.Run(output);
351  output.flush();
352  cout.flush();
353  return 0;
354 
355 }
356 #endif
int ClassId() const override
Define the class id associated with the class.
The timer class. Utility.
Definition: TPZTimer.h:46
int AllocateNewElement()
Makes more room for new elements.
Definition: pzadmchunk.h:184
Contains TPZAnalysis class which implements the sequence of actions to perform a finite element analy...
void SetElementGraph(TPZVec< int64_t > &elgraph, TPZVec< int64_t > &elgraphindex)
This method declares the element graph to the object.
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.
void SetStructuralMatrix(TPZAutoPointer< TPZStructMatrix > strmatrix)
Set structural matrix as auto pointer for analysis.
Definition: pzanalysis.cpp:82
virtual TPZGeoEl * CreateGeoElement(MElementType type, TPZVec< int64_t > &cornerindexes, int matid, int64_t &index, int reftype=1)
Generic method for creating a geometric element. Putting this method centrally facilitates the modifi...
Definition: pzgmesh.cpp:1296
Timing class. Absolutely copied from GNU time. Take a look at
Implements renumbering for elements of a mesh. Utility.
Definition: pzmetis.h:17
void ConvertGraph(TPZVec< int64_t > &elgraph, TPZVec< int64_t > &elgraphindex, TPZManVector< int64_t > &nodegraph, TPZManVector< int64_t > &nodegraphindex)
Will convert an element graph defined by elgraph and elgraphindex into a node graph defined by nodegr...
int64_t NEquations()
This computes the number of equations associated with non-restrained nodes.
Definition: pzcmesh.cpp:721
Defines step solvers class. Solver.
Definition: pzmganalysis.h:17
void SetSolver(TPZMatrixSolver< STATE > &solver)
Set solver matrix.
Implements a non symmetric sparse matrix (Yale Sparse Matrix Storage). Matrix.
Definition: pzysmp.h:45
Contains declaration of TPZGeoElBC class, it is a structure to help the construction of geometric ele...
virtual void SetData(int64_t *IA, int64_t *JA, TVar *A)
Pass the data to the class.
Definition: pzysmp.h:232
Contains the TPZStructMatrixOR class which responsible for a interface among Matrix and Finite Elemen...
virtual TPZMatrix< STATE > * Create() override
void SetDefaultOrder(int order)
Definition: pzcmesh.h:403
int ClassId() const override
Define the class id associated with the class.
Refines geometrical mesh (all the elements) num times.
Definition: pzstrmatrix.h:35
This abstract class defines the behaviour which each derived class needs to implement.
Definition: TPZMaterial.h:39
Contains declaration of TPZElementMatrix struct which associates an element matrix with the coeficien...
Contains the TPZBndCond class which implements a boundary condition for TPZMaterial objects...
void SetJacobi(const int64_t numiterations, const REAL tol, const int64_t FromCurrent)
int64_t size() const
Returns the number of elements of the vector.
Definition: pzvec.h:196
Contains the TPZSpStructMatrix class which implements sparse structural matrices. ...
Contains declaration of TPZMesh class which defines a geometrical mesh and contains a corresponding l...
Implements the sequence of actions to perform a finite element analysis. Analysis.
Definition: pzanalysis.h:32
void start()
Turns the timer on.
Definition: TPZTimer.cpp:28
Defines the behaviour of all geometric elements. GeometryTPZGeoEl is the common denominator for all g...
Definition: pzgeoel.h:43
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
virtual TPZStructMatrix * Clone() override
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
Definition: pzlog.h:87
void SetElementsNodes(int64_t NElements, int64_t NNodes)
TPZAdmChunkVector< TPZGeoNode > & NodeVec()
Definition: pzgmesh.h:140
virtual void AutoBuild(const std::set< int > *MaterialIDs)
Creates the computational elements, and the degree of freedom nodes.
Definition: pzcmesh.cpp:308
virtual TPZMatrix< STATE > * CreateAssemble(TPZFMatrix< STATE > &rhs, TPZAutoPointer< TPZGuiInterface > guiInterface) override
Contains the TPZFYsmpMatrix class which implements a non symmetric sparse matrix. ...
Contains the TPZMat2dLin class which implements a bi-dimensional linear problem.
Contains declaration of TPZCompMesh class which is a repository for computational elements...
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension a matrix and ZERO your elements.
Definition: pzfmatrix.h:616
void SetMaterial(TPZFMatrix< STATE > &xkin, TPZFMatrix< STATE > &xcin, TPZFMatrix< STATE > &xfin)
Definition: pzmat2dlin.h:44
virtual void Run(std::ostream &out=std::cout)
Calls the appropriate sequence of methods to build a solution or a time stepping sequence.
Definition: pzanalysis.cpp:920
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
Structure to help the construction of geometric elements along side of a given geometric element...
Definition: pzgeoelbc.h:21
void BuildConnectivity()
Build the connectivity of the grid.
Definition: pzgmesh.cpp:967
virtual TPZBndCond * CreateBC(TPZMaterial *reference, int id, int typ, TPZFMatrix< STATE > &val1, TPZFMatrix< STATE > &val2)
Creates an object TPZBndCond derived of TPZMaterial.
int InsertMaterialObject(TPZMaterial *mat)
Insert a material object in the datastructure.
Definition: pzcmesh.cpp:287
This class implements a geometric mesh for the pz environment. Geometry.
Definition: pzgmesh.h:48
Implements a bi-dimensional linear problem.
Definition: pzmat2dlin.h:22
Implements computational mesh. Computational Mesh.
Definition: pzcmesh.h:47
Contains TPZSolver class which defines a abstract class of solvers which will be used by matrix class...
Contains TPZStepSolver class which defines step solvers class.
Implements Sparse Structural Matrices. Structural Matrix.
void stop()
Turns the timer off, and computes the elapsed time.
Definition: TPZTimer.cpp:36
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
Contains the TPZDXGraphMesh class which implements the interface of the graphmesh to the OpenDX graph...
void InitializeBlock()
Resequence the block object, remove unconnected connect objects and reset the dimension of the soluti...
Definition: pzcmesh.cpp:391
Contains TPZMetis class which implements the renumbering for elements of a mesh to minimize the band...