NeoPZ
TPZRefPatternDataBase.cpp
Go to the documentation of this file.
1 
5 /*
6  * Created by caju on 12/17/09.
7  * Copyright 2009 LabMeC. All rights reserved.
8  *
9  */
10 
11 #ifdef HAVE_CONFIG_H
12 #include "pz_config.h"
13 #endif
14 
15 #include "TPZRefPatternDataBase.h"
16 #include "pzgeoelside.h"
17 #include "pzlog.h"
18 
19 #ifdef LOG4CXX
20 static LoggerPtr logger(Logger::getLogger("pz.refpattern.TPZRefPatternDataBase"));
21 #endif
22 
23 #ifdef BORLAND
24 #include <io.h>
25 #include <fcntl.h>
26 #endif
27 
28 using namespace std;
29 
31 
32 //.........................................................................................................................................
34 {
35  this->clear();
36 }
37 
38 //.........................................................................................................................................
40 {
41  //TODO: This destructor must be implemented!!!
42 }
43 
44 //.........................................................................................................................................
46 {
47  int uniqueId = TPZRefPattern::fNonInitializedId;
48  std::set<int> Ids;
49  Ids.clear();
50 
51  std::map< MElementType , std::list< TPZAutoPointer<TPZRefPattern> > >::iterator mapIt;
52  std::list< TPZAutoPointer<TPZRefPattern> >::iterator listIt;
53  for(mapIt = fElTypeRefPatterns.begin(); mapIt != fElTypeRefPatterns.end(); mapIt++)
54  {
55  for(listIt = mapIt->second.begin(); listIt != mapIt->second.end(); listIt++)
56  {
57  Ids.insert((*listIt)->Id());
58  }
59  }
60 
61  std::set<int>::iterator it;
62 
63  int nRefPatterns = gRefDBase.NRefPatterns();
64  for(int id = 0; id <= nRefPatterns; id++)
65  {
66  it = Ids.find(id);
67  if(it == Ids.end())
68  {
69  uniqueId = id;
70  break;
71  }
72  }
73 
74  return uniqueId;
75 }
76 
77 //.........................................................................................................................................
79 {
80  std::ifstream in(filename.c_str());
81  ReadRefPatternDBase(in);
82 }
83 //.........................................................................................................................................
85 {
86  fElTypeRefPatterns.clear();
87  fIdRefPatterns.clear();
88 
89  int nRefpatterns;
90  filename >> nRefpatterns;
91  for(int i = 0; i < nRefpatterns; i++)
92  {
94  refP->ReadAndCreateRefinementPattern(filename);
95 
96  MElementType eltype = refP->Element(0)->Type();
97  fElTypeRefPatterns[eltype].push_back(refP);
98  fIdRefPatterns[refP->Id()] = refP;
99  }
100 }
101 
102 //.........................................................................................................................................
104 {
105  filename << NRefPatterns() << std::endl ;
106 
107  std::map< MElementType, std::list< TPZAutoPointer<TPZRefPattern> > >::iterator iterMap;
108  for (iterMap = fElTypeRefPatterns.begin(); iterMap != fElTypeRefPatterns.end(); iterMap++)
109  {
110  std::list<TPZAutoPointer<TPZRefPattern> > &refpList = (*iterMap).second;
111  std::list< TPZAutoPointer<TPZRefPattern> >::iterator refp;
112  for(refp = refpList.begin(); refp != refpList.end(); refp++)
113  {
114  TPZAutoPointer<TPZRefPattern> &refpattern = *refp;
115  refpattern->WritePattern(filename);
116  }
117  }
118 }
119 
120 //.........................................................................................................................................
122 {
123  std::string DefaulPath = PZSOURCEDIR;
124 
125  DefaulPath += "/Refine/RefPatterns";
126 //#define StartPathDefined 1;
127 
128  return ImportRefPatterns(DefaulPath, maxdim);
129 }
130 
131 //.........................................................................................................................................
132 int TPZRefPatternDataBase::ImportRefPatterns(std::string &Path, int maxdim)
133 {
134  std::string bar = "/";
135 
136  std::string FileTypes ("*.rpt");
137  std::string Command = std::string("ls -1 ") + Path + bar + FileTypes;
138  std::cout << "Generated command: " << Command.c_str() << std::endl;
139  FILE *fp;
140 
141 #ifdef VC
142  fp = _popen(Command.c_str(), "r");
143 #else
144  #ifndef BORLAND
145  fp = popen(Command.c_str(), "r");
146  #else
147  fp = (FILE *)open(Command.c_str(), O_RDONLY );
148  #endif
149 #endif
150 
151  if (!fp)
152  {
153  return -1;
154  }
155 
156  int count = 0;
157  char psBuffer[1024];
158 
159  while( !feof( fp ) )
160  {
161  if( fgets(psBuffer, sizeof(psBuffer), fp ) != NULL )
162  {
163  if (psBuffer[strlen(psBuffer)-1] == '\n')
164  {
165  psBuffer[strlen(psBuffer)-1] = 0;
166  }
167  std::cout << "Reading refinement patern file : " << psBuffer << std::endl;
168  std::string filref(psBuffer);
169 
170  TPZAutoPointer<TPZRefPattern> refpat = new TPZRefPattern(filref);
171 
172  if (refpat->fRefPatternMesh.Dimension() > maxdim) {
173  std::cout << "skipped\n";
174  continue;
175  }
176 
177  if(!this->FindRefPattern(refpat))
178  {
179  this->InsertRefPattern(refpat);
180  }
181  refpat->InsertPermuted();
182 
183  count++;
184  }
185  }
186 
187 #ifndef BORLAND
188 #ifdef VC
189  _pclose(fp);
190 #else
191  pclose(fp);
192 #endif
193 #else
194  close((int)fp);
195 #endif
196 
197  return count;
198 }
199 
200 //.........................................................................................................................................
202 {
203  std::list< TPZAutoPointer<TPZRefPattern> > ElTypeList = RefPatternList(type);
204  std::list< TPZAutoPointer<TPZRefPattern> >::iterator it;
205 
206  std::string unifRefName;
207  switch (type)
208  {
209  case (EOned) :
210  {
211  unifRefName = "UnifLin";
212  break;
213  }
214  case (ETriangle) :
215  {
216  unifRefName = "UnifTri";
217  break;
218  }
219  case (EQuadrilateral) :
220  {
221  unifRefName = "UnifQua";
222  break;
223  }
224  case (ETetraedro) :
225  {
226  unifRefName = "UnifTet";
227  break;
228  }
229  case (EPiramide) :
230  {
231  unifRefName = "UnifPyr";
232  break;
233  }
234  case (EPrisma) :
235  {
236  unifRefName = "UnifPri";
237  break;
238  }
239  case (ECube) :
240  {
241  unifRefName = "UnifHex";
242  break;
243  }
244  default:
245  {
246  //none
247  break;
248  }
249  }
250  TPZAutoPointer < TPZRefPattern > UnifRefPat = NULL;
251  for(it = ElTypeList.begin(); it != ElTypeList.end(); it++)
252  {
253  std::string compareRefrName = (*it)->Name();
254  if( compareRefrName == unifRefName )
255  {
256  UnifRefPat = (*it);
257  break;
258  }
259  }
260 
261 #ifdef PZDEBUG
262  if(!UnifRefPat && type != EPoint)
263  {
264  std::cout << "Uniform refpattern " << unifRefName << " was not initialized!" << std::endl;
265  std::cout << "See " << __PRETTY_FUNCTION__ << std::endl;
266  }
267 #endif
268 
269  return UnifRefPat;
270 }
271 
272 //.........................................................................................................................................
274 {
275  switch (elType)
276  {
277  case 0://EPoint
278  {
279  break;
280  }
281  case 1://EOned
282  {
283  std::cout << "inserting uniform refpattern: line\n";
284  char buf[] =
285  "3 3 "
286  "-50 UnifLin "
287  "-1. 0. 0. "
288  " 1. 0. 0. "
289  " 0. 0. 0. "
290  " 1 2 0 1 "
291  " 1 2 0 2 "
292  " 1 2 2 1 ";
293  std::istringstream str(buf);
295  TPZAutoPointer<TPZRefPattern> refpatFound = FindRefPattern(refpat);
296  if(!refpatFound)
297  {
298  InsertRefPattern(refpat);
299  }
300  else
301  {
302  refpatFound->SetName(refpat->Name());
303  }
304  refpat->InsertPermuted();
305 
306  break;
307  }
308  case 2://ETriangle
309  {
310  std::cout << "inserting uniform refpattern: triangle\n";
311  char buf[] =
312  "6 5 "
313  "-50 UnifTri "
314  "0. 0. 0. "
315  "1. 0. 0. "
316  "0. 1. 0. "
317  "0.5 0. 0. "
318  "0.5 0.5 0. "
319  "0. 0.5 0. "
320  "2 3 0 1 2 "
321  "2 3 0 3 5 "
322  "2 3 3 1 4 "
323  "2 3 5 4 2 "
324  "2 3 4 5 3 ";
325  std::istringstream str(buf);
327  TPZAutoPointer<TPZRefPattern> refpatFound = FindRefPattern(refpat);
328  if(!refpatFound)
329  {
330  InsertRefPattern(refpat);
331  }
332  else
333  {
334  refpatFound->SetName(refpat->Name());
335  }
336  refpat->InsertPermuted();
337 
338  break;
339  }
340  case 3://EQuadrilateral
341  {
342  std::cout << "inserting uniform refpattern: quadrilateral\n";
343  char buf[] =
344  "9 5 "
345  "-50 UnifQua "
346  "-1. -1. 0. "
347  " 1. -1. 0. "
348  " 1. 1. 0. "
349  "-1. 1. 0. "
350  " 0. -1. 0. "
351  " 1. 0. 0. "
352  " 0. 1. 0. "
353  "-1. 0. 0. "
354  " 0. 0. 0. "
355  " 3 4 0 1 2 3 "
356  " 3 4 0 4 8 7 "
357  " 3 4 4 1 5 8 "
358  " 3 4 8 5 2 6 "
359  " 3 4 7 8 6 3 ";
360  std::istringstream str(buf);
362  TPZAutoPointer<TPZRefPattern> refpatFound = FindRefPattern(refpat);
363  if(!refpatFound)
364  {
365  InsertRefPattern(refpat);
366  }
367  else
368  {
369  refpatFound->SetName(refpat->Name());
370  }
371  refpat->InsertPermuted();
372 #ifdef LOG4CXX
373  if (logger->isDebugEnabled()) {
374  std::stringstream sout;
375  refpat->PrintMore(sout);
376  LOGPZ_DEBUG(logger, sout.str())
377  }
378 #endif
379 
380  break;
381  }
382  case 4://ETetraedro
383  {
384  std::cout << "inserting uniform refpattern: tetrahedra\n";
385  char buf[] =
386  "10 7"
387  "-50 UnifTet "
388  "0. 0. 0. "
389  "1. 0. 0. "
390  "0. 1. 0. "
391  "0. 0. 1. "
392  "0.5 0. 0. "
393  "0.5 0.5 0. "
394  "0. 0.5 0. "
395  "0. 0. 0.5 "
396  "0.5 0. 0.5 "
397  "0. 0.5 0.5 "
398  "4 4 0 1 2 3 "
399  "4 4 0 4 6 7 "
400  "4 4 4 1 5 8 "
401  "4 4 6 5 2 9 "
402  "4 4 7 8 9 3 "
403  "5 5 4 8 9 6 7 "
404  "5 5 8 4 6 9 5 ";
405  std::istringstream str(buf);
407  TPZAutoPointer<TPZRefPattern> refpatFound = FindRefPattern(refpat);
408  if(!refpatFound)
409  {
410  InsertRefPattern(refpat);
411  }
412  else
413  {
414  refpatFound->SetName(refpat->Name());
415  }
416  refpat->InsertPermuted();
417 
418  break;
419  }
420  case 5://EPiramide
421  {
422  std::cout << "inserting uniform refpattern: pyramid\n";
423  char buf[] =
424  "14 11 "
425  "-50 UnifPyr "
426  "-1. -1. 0. "
427  " 1. -1. 0. "
428  " 1. 1. 0. "
429  "-1. 1. 0. "
430  " 0. 0. 1. "
431  " 0. -1. 0. "
432  " 1. 0. 0. "
433  " 0. 1. 0. "
434  "-1. 0. 0. "
435  "-0.5 -0.5 0.5 "
436  " 0.5 -0.5 0.5 "
437  " 0.5 0.5 0.5 "
438  "-0.5 0.5 0.5 "
439  " 0. 0. 0. "
440  " 5 5 0 1 2 3 4 "
441  " 5 5 0 5 13 8 9 "
442  " 5 5 5 1 6 13 10 "
443  " 5 5 13 6 2 7 11 "
444  " 5 5 8 13 7 3 12 "
445  " 5 5 9 10 11 12 4 "
446  " 5 5 9 12 11 10 13 "
447  " 4 4 9 5 13 10 "
448  " 4 4 10 6 13 11 "
449  " 4 4 11 7 12 13 "
450  " 4 4 8 13 12 9 ";
451  std::istringstream str(buf);
453  TPZAutoPointer<TPZRefPattern> refpatFound = FindRefPattern(refpat);
454  if(!refpatFound)
455  {
456  InsertRefPattern(refpat);
457  }
458  else
459  {
460  refpatFound->SetName(refpat->Name());
461  }
462  refpat->InsertPermuted();
463 
464  break;
465  }
466  case 6://EPrisma
467  {
468  std::cout << "inserting uniform refpattern: prism\n";
469  char buf[] =
470  "18 9 "
471  "-50 UnifPri "
472  " 0. 0. -1. "
473  " 1. 0. -1. "
474  " 0. 1. -1. "
475  " 0. 0. 1. "
476  " 1. 0. 1. "
477  " 0. 1. 1. "
478  " 0.5 0. -1. "
479  " 0.5 0.5 -1. "
480  " 0. 0.5 -1. "
481  " 0. 0. 0. "
482  " 1. 0. 0. "
483  " 0. 1. 0. "
484  " 0.5 0. 1. "
485  " 0.5 0.5 1. "
486  " 0. 0.5 1. "
487  " 0.5 0. 0. "
488  " 0.5 0.5 0. "
489  " 0. 0.5 0. "
490  " 6 6 0 1 2 3 4 5 "
491  " 6 6 0 6 8 9 15 17 "
492  " 6 6 6 1 7 15 10 16 "
493  " 6 6 8 7 2 17 16 11 "
494  " 6 6 17 16 15 8 7 6 "
495  " 6 6 9 15 17 3 12 14 "
496  " 6 6 15 10 16 12 4 13 "
497  " 6 6 17 16 11 14 13 5 "
498  " 6 6 14 13 12 17 16 15 ";
499  std::istringstream str(buf);
501  TPZAutoPointer<TPZRefPattern> refpatFound = FindRefPattern(refpat);
502  if(!refpatFound)
503  {
504  InsertRefPattern(refpat);
505  }
506  else
507  {
508  refpatFound->SetName(refpat->Name());
509  }
510  refpat->InsertPermuted();
511 
512  break;
513  }
514  case 7://ECube
515  {
516  std::cout << "inserting uniform refpattern: hexahedre\n";
517  char buf[] =
518  "27 9 "
519  "-50 UnifHex "
520  "-1. -1. -1. "
521  " 1. -1. -1. "
522  " 1. 1. -1. "
523  "-1. 1. -1. "
524  "-1. -1. 1. "
525  " 1. -1. 1. "
526  " 1. 1. 1. "
527  "-1. 1. 1. "
528  " 0. -1. -1. "
529  " 1. 0. -1. "
530  " 0. 1. -1. "
531  "-1. 0. -1. "
532  "-1. -1. 0. "
533  " 1. -1. 0. "
534  " 1. 1. 0. "
535  "-1. 1. 0. "
536  " 0. -1. 1. "
537  " 1. 0. 1. "
538  " 0. 1. 1. "
539  "-1. 0. 1. "
540  " 0. 0. -1. "
541  " 0. -1. 0. "
542  " 1. 0. 0. "
543  " 0. 1. 0. "
544  "-1. 0. 0. "
545  " 0. 0. 1. "
546  " 0. 0. 0. "
547  "7 8 0 1 2 3 4 5 6 7 "
548  "7 8 0 8 20 11 12 21 26 24 "
549  "7 8 8 1 9 20 21 13 22 26 "
550  "7 8 20 9 2 10 26 22 14 23 "
551  "7 8 11 20 10 3 24 26 23 15 "
552  "7 8 12 21 26 24 4 16 25 19 "
553  "7 8 21 13 22 26 16 5 17 25 "
554  "7 8 26 22 14 23 25 17 6 18 "
555  "7 8 24 26 23 15 19 25 18 7 ";
556  std::istringstream str(buf);
558  TPZAutoPointer<TPZRefPattern> refpatFound = FindRefPattern(refpat);
559  if(!refpatFound)
560  {
561  InsertRefPattern(refpat);
562  }
563  else
564  {
565  refpatFound->SetName(refpat->Name());
566  }
567  refpat->InsertPermuted();
568 
569  break;
570  }
571  default:
572  {
573  cout << "Cant generate uniform refpattern because MElementType was not found on " << __PRETTY_FUNCTION__ << endl;
574  DebugStop();
575  }
576  }
577 }
578 
579 //.........................................................................................................................................
581 {
582  InitializeUniformRefPattern(EOned);
583  InitializeUniformRefPattern(ETriangle);
584  InitializeUniformRefPattern(EQuadrilateral);
585  InitializeUniformRefPattern(ETetraedro);
586  InitializeUniformRefPattern(EPiramide);
587  InitializeUniformRefPattern(EPrisma);
588  InitializeUniformRefPattern(ECube);
589 }
590 
591 
593 {
594  std::string path = REFPATTERNDIR;
595  ImportRefPatterns(path, maxdim);
596 }
597 
598 //.........................................................................................................................................
600 {
601  if (!refpat)
602  {
603  PZError << "TPZGeoMesh::InsertRefPattern ERROR : NULL refinement pattern! " << std::endl;
604  return;
605  }
606 
607  MElementType eltype = refpat->Element(0)->Type();
608 
609  if(!refpat->IdInitialized())
610  {
611  int id = ReturnUniqueId();
612  refpat->SetId(id);
613  }
614 
615  else//the refpattern already have an Id initialized
616  {
617  std::map< int , TPZAutoPointer<TPZRefPattern> >::iterator itid = fIdRefPatterns.find(refpat->Id());
618  if(itid != fIdRefPatterns.end())//the Id is already in use by another refpattern
619  {
620  int id = ReturnUniqueId();
621  refpat->SetId(id);
622  }
623  }
624 
625  fElTypeRefPatterns[eltype].push_back(refpat);
626  fIdRefPatterns[refpat->Id()] = refpat;
627 }
628 
629 //.........................................................................................................................................
631 {
633  if(!refpat )
634  {
635  return NullRefPat;
636  }
637 
638  std::map< int, TPZAutoPointer<TPZRefPattern> >::iterator it;
639  for(it = fIdRefPatterns.begin(); it != fIdRefPatterns.end(); it++)
640  {
641  if( (TPZRefPattern &)refpat == it->second)
642  {
643  return it->second;
644  }
645  }
646 
647  //else
648  return NullRefPat;
649 }
650 
651 //.........................................................................................................................................
653 {
654  TPZAutoPointer<TPZRefPattern> refPatReturned;
655  std::map< int , TPZAutoPointer<TPZRefPattern> >::iterator it;
656 
657  it = fIdRefPatterns.find(id);
658 
659  if(it != fIdRefPatterns.end())
660  {
661  refPatReturned = it->second;
662  }
663 
664  return refPatReturned;
665 }
666 
667 //.........................................................................................................................................
669 {
670  TPZAutoPointer<TPZRefPattern> refp = NULL;
671 
672  std::map< int , TPZAutoPointer<TPZRefPattern> >::iterator it;
673  for(it = fIdRefPatterns.begin(); it != fIdRefPatterns.end(); it++)
674  {
675  if( (it->second)->fName == name)
676  {
677  refp = it->second;
678  break;
679  }
680  }
681 
682  return refp;
683 }
684 
685 //.........................................................................................................................................
686 const std::list< TPZAutoPointer<TPZRefPattern> > &TPZRefPatternDataBase::RefPatternList(MElementType eltype)
687 {
688  return fElTypeRefPatterns[eltype];
689 }
690 
691 //.........................................................................................................................................
693 {
694  int nRefPatterns = fIdRefPatterns.size();
695 
696  return nRefPatterns;
697 }
698 
699 //.........................................................................................................................................
700 void TPZRefPatternDataBase::Print(std::ostream &out)
701 {
702  std::map< MElementType , std::list< TPZAutoPointer<TPZRefPattern> > >::iterator it;
703 
704  for(it = fElTypeRefPatterns.begin(); it != fElTypeRefPatterns.end(); it++)
705  {
706  out << "======================================================\n";
707  out << "**************************************************\n";
708  out << "Element type: " << MElementType_Name(it->first) << std::endl;
709  out << "**************************************************";
710 
711  std::list< TPZAutoPointer<TPZRefPattern> >::iterator itt;
712 
713  for(itt = it->second.begin(); itt != it->second.end(); itt++)
714  {
715  (*itt)->Print(out);
716  }
717  }
718 }
filename
Definition: stats.py:82
Contains declaration of TPZGeoElSide class which represents an element and its side, and TPZGeoElSideIndex class which represents an TPZGeoElSide index.
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 InitializeAllUniformRefPatterns()
Initialize the uniform refinement pattern from hard coaded data for all linear geometric elements...
void ReadAndCreateRefinementPattern(std::istream &file)
const std::list< TPZAutoPointer< TPZRefPattern > > & RefPatternList(MElementType eltype)
Return the complete set of refinement patterns availabe.
static const int fNonInitializedId
Id for identifying a non initialized pattern.
Definition: TPZRefPattern.h:80
void InitializeUniformRefPattern(MElementType elType)
Initialize the uniform refinement pattern from hard coaded data for an specific geometric element...
void InsertRefPattern(TPZAutoPointer< TPZRefPattern > &refpat)
Insert the refinement pattern in the list of availabe refinement patterns assigns an Id to refPattern...
TPZGeoEl * Element(int iel)
It returns the element number iel from the stack of elements of the geometric mesh.
void SetId(int id)
Set the id of the refinement pattern.
std::string MElementType_Name(MElementType elType)
Returns the name of the element type.
Definition: pzeltype.h:127
void Print(std::ostream &out=std::cout)
void InsertPermuted()
Generate all permuted partitions and insert them in the mesh.
void InitializeRefPatterns(int maxdim=3)
TPZGeoMesh fRefPatternMesh
Geometric mesh which defines the topology of the current refinement pattern.
TPZAutoPointer< TPZRefPattern > FindRefPattern(TPZAutoPointer< TPZRefPattern > &refpat)
Check whether the refinement pattern already exists.
TPZRefPatternDataBase gRefDBase
External variable to data base of patterns.
Defines data base of patterns. Refine.
TPZAutoPointer< TPZRefPattern > GetUniformRefPattern(MElementType type)
Retrieves the uniform refinement pattern for given element type.
bool IdInitialized()
int Id() const
Return the id of the refinement pattern.
#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
virtual MElementType Type() const =0
Returns the element type acording to pzeltype.h.
int ImportRefPatterns(int maxdim=3)
Import a library of refinement patterns from the install directory.
void WriteRefPatternDBase(std::ofstream &fileout)
std::string Name()
Returns the refinement pattern identifier.
void ReadRefPatternDBase(const std::string &filename)
Read all refpatterns available in the given file.
Contains the TPZRefPatternDataBase class which defines data base of patterns.
Defines the topology of the current refinement pattern to a mesh. Refine.
Definition: TPZRefPattern.h:77
int Dimension()
Get Dimension.
Definition: pzgmesh.h:291
MElementType
Define the element types.
Definition: pzeltype.h:52
Definition: pzeltype.h:61
void SetName(std::string name)
Sets the name associated with the refinement pattern.
void PrintMore(std::ostream &out=std::cout) const
void WritePattern(std::ofstream &out) const
Definition: pzeltype.h:55
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15