NeoPZ
TPZSSpStructMatrix.cpp
Go to the documentation of this file.
1 
6 #include "TPZSSpStructMatrix.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 "pzsysmp.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 TPZSymetricSpStructMatrix(*this);
36 }
38  TPZAutoPointer<TPZGuiInterface> guiInterface){
39 
40 #ifdef LOG4CXX
41  if (logger->isDebugEnabled())
42  {
43  LOGPZ_DEBUG(logger,"TPZSymetricSpStructMatrix::CreateAssemble starting");
44  }
45 #endif
46 
47  int64_t neq = fMesh->NEquations();
48  if(fMesh->FatherMesh()) {
49  cout << "TPZSymetricSpStructMatrix should not be called with CreateAssemble for a substructure mesh\n";
50  return new TPZSYsmpMatrix<STATE>(0,0);
51  }
52 // std::cout << "Creating\n";
53  TPZMatrix<STATE> *stiff = Create();//new TPZFYsmpMatrix(neq,neq);
54  TPZSYsmpMatrix<STATE> *mat = dynamic_cast<TPZSYsmpMatrix<STATE> *> (stiff);
55  rhs.Redim(neq,1);
56  //stiff->Print("Stiffness TPZFYsmpMatrix :: CreateAssemble()");
57  TPZTimer before("Assembly of a sparse matrix");
58 // std::cout << "Assembling\n";
59  before.start();
60 #ifdef LOG4CXX
61  if(logger->isDebugEnabled()) LOGPZ_DEBUG(logger,"TPZSymetricSpStructMatrix::CreateAssemble calling Assemble()");
62 #endif
63  Assemble(*stiff,rhs,guiInterface);
64  mat->ComputeDiagonal();
65 
66 // std::cout << "Rhs norm " << Norm(rhs) << std::endl;
67 
68  before.stop();
69  //std::cout << __PRETTY_FUNCTION__ << " " << before << std::endl;
70  // mat->ComputeDiagonal();
71  //stiff->Print("Stiffness TPZFYsmpMatrix :: CreateAssemble()");
72 #ifdef LOG4CXX
73  if(logger->isDebugEnabled()) LOGPZ_DEBUG(logger,"TPZSymetricSpStructMatrix::CreateAssemble exiting");
74 #endif
75  return stiff;
76 }
78 
82  TPZStack<int64_t> elgraph;
83  TPZVec<int64_t> elgraphindex;
84  // int nnodes = 0;
85  fMesh->ComputeElGraph(elgraph,elgraphindex);
86  TPZMatrix<STATE> * mat = SetupMatrixData(elgraph, elgraphindex);
87  return mat;
88 }
89 
91 
92  int64_t neq = fEquationFilter.NActiveEquations();
93  TPZSYsmpMatrix<STATE> * mat = new TPZSYsmpMatrix<STATE>(neq,neq);
94 
96  TPZMetis metis;
97  metis.SetElementsNodes(elgraphindex.NElements() -1 ,fMesh->NIndependentConnects());
98  metis.SetElementGraph(elgraph,elgraphindex);
99 
100  TPZManVector<int64_t> nodegraph;
101  TPZManVector<int64_t> nodegraphindex;
106  metis.ConvertGraph(elgraph,elgraphindex,nodegraph,nodegraphindex);
108  int64_t i;
109  int64_t nblock = nodegraphindex.NElements()-1;
110  // number of values in the sparse matrix
111  int64_t totalvar = 0;
112  // number of equations
113  int64_t totaleq = 0;
114  for(i=0;i<nblock;i++){
115  int64_t iblsize = fMesh->Block().Size(i);
116  int64_t iblpos = fMesh->Block().Position(i);
117  int64_t numactive = fEquationFilter.NumActive(iblpos, iblpos+iblsize);
118  if (!numactive) {
119  continue;
120  }
121  totaleq += iblsize;
122  int64_t icfirst = nodegraphindex[i];
123  int64_t iclast = nodegraphindex[i+1];
124  int64_t j;
125  //longhin
126  totalvar+=(iblsize*(iblsize+1))/2;
127  for(j=icfirst;j<iclast;j++) {
128  int64_t col = nodegraph[j];
129  if (col < i) {
130  continue;
131  }
132 
133  if (col == i) {
134  DebugStop();
135  }
136 
137  int64_t colsize = fMesh->Block().Size(col);
138  int64_t colpos = fMesh->Block().Position(col);
139  int64_t numactive = fEquationFilter.NumActive(colpos, colpos+colsize);
140  if (!numactive) {
141  continue;
142  }
143  totalvar += iblsize*colsize;
144  }
145  }
146 
147  int64_t ieq = 0;
148  // pos is the position where we will put the column value
149  int64_t pos = 0;
150 
151  nblock=fMesh->NIndependentConnects();
152 
153  TPZVec<int64_t> Eq(totaleq+1);
154  TPZVec<int64_t> EqCol(totalvar);
155  TPZVec<STATE> EqValue(totalvar,0.);
156  for(i=0;i<nblock;i++){
157  int64_t iblsize = fMesh->Block().Size(i);
158  int64_t iblpos = fMesh->Block().Position(i);
159  TPZManVector<int64_t> rowdestindices(iblsize);
160  for (int64_t i=0; i<iblsize; i++) {
161  rowdestindices[i] = iblpos+i;
162  }
163  fEquationFilter.Filter(rowdestindices);
164 
165  int64_t ibleq;
166  // working equation by equation
167  for(ibleq=0; ibleq<rowdestindices.size(); ibleq++) {
168  if (rowdestindices[ibleq] != ieq) {
169  DebugStop();
170  }
171  Eq[ieq] = pos;
172  int64_t colsize,colpos,jbleq;
173  int64_t diagonalinsert = 0;
174  int64_t icfirst = nodegraphindex[i];
175  int64_t iclast = nodegraphindex[i+1];
176  int64_t j;
177  for(j=icfirst;j<iclast;j++)
178  {
179  int64_t col = nodegraph[j];
180  if (col < i) {
181  continue;
182  }
183  // force the diagonal block to be inserted
184  // the nodegraph does not contain the pointer to itself
185  if(!diagonalinsert && col > i)
186  {
187  diagonalinsert = 1;
188  int64_t colsize = fMesh->Block().Size(i);
189  int64_t colpos = fMesh->Block().Position(i);
190  TPZManVector<int64_t> destindices(colsize);
191  for (int64_t i=0; i<colsize; i++) {
192  destindices[i] = colpos+i;
193  }
194  fEquationFilter.Filter(destindices);
195  int64_t jbleq;
196  for(jbleq=0; jbleq<destindices.size(); jbleq++) {
197  // if(colpos+jbleq == ieq) continue;
198  int64_t jeq = destindices[jbleq];
199  if (jeq < ieq) {
200  continue;
201  }
202  EqCol[pos] = destindices[jbleq];
203  EqValue[pos] = 0.;
204  // colpos++;
205  pos++;
206  }
207  }
208  colsize = fMesh->Block().Size(col);
209  colpos = fMesh->Block().Position(col);
210  if (fEquationFilter.NumActive(colpos, colpos+colsize) == 0) {
211  continue;
212  }
213  TPZManVector<int64_t> destindices(colsize);
214  for (int64_t i=0; i<colsize; i++) {
215  destindices[i] = colpos+i;
216  }
217  fEquationFilter.Filter(destindices);
218  for(jbleq=0; jbleq<destindices.size(); jbleq++) {
219  int64_t jeq = destindices[jbleq];
220  if (jeq < ieq) {
221  continue;
222  }
223  EqCol[pos] = jeq;
224  EqValue[pos] = 0.;
225  colpos++;
226  pos++;
227  }
228  }
229  // all elements are below (last block certainly)
230  if(!diagonalinsert)
231  {
232  diagonalinsert = 1;
233  int64_t colsize = fMesh->Block().Size(i);
234  int64_t colpos = fMesh->Block().Position(i);
235  TPZManVector<int64_t> destindices(colsize);
236  for (int64_t i=0; i<colsize; i++) {
237  destindices[i] = colpos+i;
238  }
239  fEquationFilter.Filter(destindices);
240  int64_t jbleq;
241  for(jbleq=0; jbleq<destindices.size(); jbleq++) {
242  // if(colpos+jbleq == ieq) continue;
243  int64_t jeq = destindices[jbleq];
244  if (jeq < ieq) {
245  continue;
246  }
247  EqCol[pos] = jeq;
248  EqValue[pos] = 0.;
249  // colpos++;
250  pos++;
251  }
252  }
253  ieq++;
254  }
255  }
256 
257  Eq[ieq] = pos;
258  mat->SetData(Eq,EqCol,EqValue);
259  return mat;
260 }
261 
263 
264 }
265 
267 {}
268 
269 #ifndef STATE_COMPLEX
270 #include "pzmat2dlin.h"
271 
273 
274  int refine=5;
275  int order=5;
276 
277  TPZGeoMesh gmesh;
278  TPZCompMesh cmesh(&gmesh);
279  double coordstore[4][3] = {{0.,0.,0.},{1.,0.,0.},{1.,1.,0.},
280  {0.,1.,0.}};
281 
282  int i,j;
283  TPZVec<REAL> coord(3,0.);
284  for(i=0; i<4; i++) {
285  // initializar as coordenadas do no em um vetor
286  for (j=0; j<3; j++) coord[j] = coordstore[i][j];
287 
288  // identificar um espa� no vetor onde podemos armazenar
289  // este vetor
290  gmesh.NodeVec ().AllocateNewElement ();
291 
292  // initializar os dados do n� gmesh.NodeVec ()[nodeindex].Initialize (i,coord,gmesh);
293  }
294  int el;
295  TPZGeoEl *gel;
296  for(el=0; el<1; el++) {
297 
298  // initializar os indices dos nos
299  TPZVec<int64_t> indices(4);
300  for(i=0; i<4; i++) indices[i] = i;
301  // O proprio construtor vai inserir o elemento na malha
302  // gel = new TPZGeoElQ2d(el,indices,1,gmesh);
303  int64_t index;
304  gel = gmesh.CreateGeoElement(EQuadrilateral,indices,1,index);
305  }
306  gmesh.BuildConnectivity ();
307 
308  TPZVec<TPZGeoEl *> subel;
309 
310  cout << "Refinement ";
311  cin >> refine;
312  cout << endl;
313  DebugStop();
314 // UniformRefine(refine,gmesh);
315 
316  TPZGeoElBC gelbc(gel,4,-4);
317  TPZMat2dLin *meumat = new TPZMat2dLin(1);
318  TPZFMatrix<STATE> xk(1,1,1.),xc(1,2,0.),xf(1,1,1.);
319  meumat->SetMaterial (xk,xc,xf);
320  TPZMaterial * meumatptr(meumat);
321  cmesh.InsertMaterialObject(meumatptr);
322 
323  TPZFMatrix<STATE> val1(1,1,0.),val2(1,1,0.);
324  TPZMaterial * bnd = meumat->CreateBC (meumatptr,-4,0,val1,val2);
325  cmesh.InsertMaterialObject(bnd);
326 
327  cout << "Interpolation order ";
328  cin >> order;
329  cout << endl;
330 
331  //TPZCompEl::gOrder = order;
332  cmesh.SetDefaultOrder(order);
333 
334  cmesh.AutoBuild();
335  // cmesh.AdjustBoundaryElements();
336  cmesh.InitializeBlock();
337 
338  ofstream output("outputPar.dat");
339  TPZAnalysis an(&cmesh,true,output);
340 
341  //TPZVec<int> numelconnected(cmesh.NEquations(),0);
342  TPZSymetricSpStructMatrix mat(&cmesh);
343 
344  an.SetStructuralMatrix(mat);
345 
347  sol.SetJacobi(100,1.e-5,0);
348 
349 
350  an.SetSolver(sol);
351  an.Run(output);
352  output.flush();
353  cout.flush();
354  return 0;
355 
356 }
357 #endif
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...
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
virtual TPZMatrix< STATE > * Create()
Defines step solvers class. Solver.
Definition: pzmganalysis.h:17
void SetSolver(TPZMatrixSolver< STATE > &solver)
Set solver matrix.
virtual TPZMatrix< STATE > * SetupMatrixData(TPZStack< int64_t > &elgraph, TPZVec< int64_t > &elgraphindex)
Contains declaration of TPZGeoElBC class, it is a structure to help the construction of geometric ele...
Contains the TPZStructMatrixOR class which responsible for a interface among Matrix and Finite Elemen...
Contains the TPZSymetricSpStructMatrix class which implements sparse structural matrices.
virtual TPZStructMatrix * Clone()
void SetDefaultOrder(int order)
Definition: pzcmesh.h:403
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
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 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
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
Definition: pzlog.h:87
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)
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
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
void ComputeDiagonal()
Definition: pzsysmp.cpp:196
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 a symmetric sparse matrix. Matrix.
Definition: pzsysmp.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.
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
Implements Sparse Structural Matrices. Structural Matrix.
Contains TPZMetis class which implements the renumbering for elements of a mesh to minimize the band...
Contains TPZSYsmpMatrix class which implements a symmetric sparse matrix. Purpose: Defines operation...