gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Chain.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 //
6 // Contributed by Matti Pellikka <matti.pellikka@gmail.com>.
7 
8 #include "Chain.h"
9 #include "MLine.h"
10 #include "MTriangle.h"
11 #include "MQuadrangle.h"
12 #include "MTetrahedron.h"
13 #include "MPyramid.h"
14 #include "MHexahedron.h"
15 #include "MPrism.h"
16 
17 #if defined(HAVE_FLTK)
18 #include "FlGui.h"
19 #endif
20 
21 #if defined(HAVE_KBIPACK)
22 
23 void updateFltk()
24 {
25 #if defined(HAVE_FLTK)
26  if(FlGui::available()) FlGui::instance()->updateViews(true, true);
27 #endif
28 }
29 
30 std::string convertInt(int number)
31 {
32  std::stringstream stream;
33  stream << number;
34  return stream.str();
35 }
36 
37 std::map<GEntity *, std::set<MVertex *, MVertexPtrLessThan>, GEntityPtrLessThan>
38  ElemChain::_vertexCache;
39 
40 inline void ElemChain::_sortVertexIndices()
41 {
42  std::map<MVertex *, int, MVertexPtrLessThan> si;
43 
44  for(std::size_t i = 0; i < _v.size(); i++) si[_v[i]] = i;
45 
46  for(auto it = si.begin(); it != si.end(); it++) _si.push_back(it->second);
47 }
48 
49 void findEntitiesInPhysicalGroups(GModel *m,
50  const std::vector<int> &physicalGroups,
51  std::vector<GEntity *> &entities)
52 {
53  std::map<int, std::vector<GEntity *> > groups[4];
54  m->getPhysicalGroups(groups);
55  for(std::size_t i = 0; i < physicalGroups.size(); i++) {
56  bool found = false;
57  for(int j = 0; j < 4; j++) {
58  auto it = groups[j].find(physicalGroups.at(i));
59  if(it != groups[j].end()) {
60  found = true;
61  std::vector<GEntity *> physicalGroup = it->second;
62  for(std::size_t k = 0; k < physicalGroup.size(); k++) {
63  entities.push_back(physicalGroup.at(k));
64  }
65  }
66  }
67  if(!found) {
68  Msg::Error("Physical group %d does not exist", physicalGroups.at(i));
69  }
70  }
71 }
72 
73 bool ElemChain::_equalVertices(const std::vector<MVertex *> &v2) const
74 {
75  if(_v.size() != v2.size()) return false;
76  for(std::size_t i = 0; i < _v.size(); i++)
77  if(_v[i]->getNum() != v2[i]->getNum()) return false;
78  return true;
79 }
80 
81 ElemChain::ElemChain(MElement *e)
82 {
83  _dim = e->getDim();
84  for(std::size_t i = 0; i < e->getNumPrimaryVertices(); i++)
85  _v.push_back(e->getVertex(i));
86  _sortVertexIndices();
87 }
88 
89 ElemChain::ElemChain(int dim, std::vector<MVertex *> &v) : _dim(dim), _v(v)
90 {
91  _sortVertexIndices();
92 }
93 
94 inline int ElemChain::getSortedVertex(int i) const
95 {
96  return _v[(int)_si[i]]->getNum();
97 }
98 
99 int ElemChain::getTypeMSH(int dim, int numVertices)
100 {
101  switch(dim) {
102  case 0: return MSH_PNT;
103  case 1: return MSH_LIN_2;
104  case 2:
105  switch(numVertices) {
106  case 3: return MSH_TRI_3;
107  case 4: return MSH_QUA_4;
108  default: return 0;
109  }
110  case 3:
111  switch(numVertices) {
112  case 4: return MSH_TET_4;
113  case 5: return MSH_PYR_5;
114  case 6: return MSH_PRI_6;
115  case 8: return MSH_HEX_8;
116  default: return 0;
117  }
118  default: return 0;
119  }
120 }
121 
122 int ElemChain::getTypeMSH() const
123 {
124  return ElemChain::getTypeMSH(_dim, this->getNumVertices());
125 }
126 
127 MElement *ElemChain::createMeshElement() const
128 {
129  MElementFactory factory;
130  std::vector<MVertex *> v(_v);
131  return factory.create(this->getTypeMSH(), v);
132 }
133 
134 int ElemChain::compareOrientation(const ElemChain &c2) const
135 {
136  std::vector<MVertex *> v2;
137  c2.getMeshVertices(v2);
138 
139  int perm = 1;
140  if(this->_equalVertices(v2)) return perm;
141  while(std::next_permutation(v2.begin(), v2.end(), MVertexPtrLessThan())) {
142  perm *= -1;
143  if(this->_equalVertices(v2)) return perm;
144  }
145  c2.getMeshVertices(v2);
146  perm = 1;
147  while(std::prev_permutation(v2.begin(), v2.end(), MVertexPtrLessThan())) {
148  perm *= -1;
149  if(this->_equalVertices(v2)) return perm;
150  }
151  return 0;
152 }
153 
154 bool ElemChain::lessThan(const ElemChain &c2) const
155 {
156  if(this->getNumSortedVertices() != c2.getNumSortedVertices())
157  return (this->getNumSortedVertices() < c2.getNumSortedVertices());
158  for(int i = 0; i < this->getNumSortedVertices(); i++) {
159  if(this->getSortedVertex(i) < c2.getSortedVertex(i))
160  return true;
161  else if(this->getSortedVertex(i) > c2.getSortedVertex(i))
162  return false;
163  }
164  return false;
165 }
166 
167 int ElemChain::getNumBoundaries(int dim, int numVertices)
168 {
169  switch(dim) {
170  case 0: return 0;
171  case 1: return 2;
172  case 2:
173  switch(numVertices) {
174  case 3: return 3;
175  case 4: return 4;
176  default: return 0;
177  }
178  case 3:
179  switch(numVertices) {
180  case 4: return 4;
181  case 5: return 5;
182  case 6: return 5;
183  case 8: return 6;
184  default: return 0;
185  }
186  default: return 0;
187  }
188 }
189 
190 int ElemChain::getNumBoundaryElemChains() const
191 {
192  return ElemChain::getNumBoundaries(_dim, this->getNumVertices());
193 }
194 
195 void ElemChain::getBoundaryVertices(int i, int dim, int numVertices,
196  const std::vector<MVertex *> &v,
197  std::vector<MVertex *> &vertices)
198 {
199  vertices.clear();
200  switch(dim) {
201  case 1: vertices.push_back(v[i]); return;
202  case 2:
203  switch(numVertices) {
204  case 3:
205  for(int j = 0; j < 2; j++)
206  vertices.push_back(v[MTriangle::edges_tri(i, j)]);
207  return;
208  case 4:
209  for(int j = 0; j < 2; j++)
210  vertices.push_back(v[MQuadrangle::edges_quad(i, j)]);
211  return;
212  default: return;
213  }
214  case 3:
215  switch(numVertices) {
216  case 4:
217  for(int j = 0; j < 3; j++)
218  vertices.push_back(v[MTetrahedron::faces_tetra(i, j)]);
219  return;
220  case 5:
221  if(i < 3)
222  for(int j = 0; j < 3; j++)
223  vertices.push_back(v[MPyramid::faces_pyramid(i, j)]);
224  else
225  for(int j = 0; j < 4; j++)
226  vertices.push_back(v[MPyramid::faces_pyramid(i, j)]);
227  return;
228  case 6:
229  if(i < 2)
230  for(int j = 0; j < 3; j++)
231  vertices.push_back(v[MPrism::faces_prism(i, j)]);
232  else
233  for(int j = 0; j < 4; j++)
234  vertices.push_back(v[MPrism::faces_prism(i, j)]);
235  return;
236  case 8:
237  for(int j = 0; j < 4; j++)
238  vertices.push_back(v[MHexahedron::faces_hexa(i, j)]);
239  return;
240  default: return;
241  }
242  default: return;
243  }
244 }
245 
246 ElemChain ElemChain::getBoundaryElemChain(int i) const
247 {
248  std::vector<MVertex *> vertices;
249  ElemChain::getBoundaryVertices(i, _dim, this->getNumVertices(), _v, vertices);
250  return ElemChain(_dim - 1, vertices);
251 }
252 
253 bool ElemChain::inEntity(GEntity *e) const
254 {
255  if(_vertexCache[e].empty()) {
256  for(std::size_t i = 0; i < e->getNumMeshElements(); i++)
257  for(std::size_t j = 0; j < e->getMeshElement(i)->getNumVertices(); j++)
258  _vertexCache[e].insert(e->getMeshElement(i)->getVertex(j));
259  }
260 
261  for(int i = 0; i < this->getNumVertices(); i++)
262  if(!_vertexCache[e].count(this->getMeshVertex(i))) return false;
263  return true;
264 }
265 
266 #endif
MSH_LIN_2
#define MSH_LIN_2
Definition: GmshDefines.h:80
MSH_HEX_8
#define MSH_HEX_8
Definition: GmshDefines.h:84
MTriangle.h
MVertexPtrLessThan
Definition: MVertex.h:218
MPrism::faces_prism
static int faces_prism(const int face, const int vert)
Definition: MPrism.h:188
MSH_PNT
#define MSH_PNT
Definition: GmshDefines.h:94
MElementFactory
Definition: MElement.h:517
MTetrahedron::faces_tetra
static int faces_tetra(const int face, const int vert)
Definition: MTetrahedron.h:188
MElement::getDim
virtual int getDim() const =0
MSH_QUA_4
#define MSH_QUA_4
Definition: GmshDefines.h:82
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
MTriangle::edges_tri
static int edges_tri(const int edge, const int vert)
Definition: MTriangle.h:165
MQuadrangle::edges_quad
static int edges_quad(const int edge, const int vert)
Definition: MQuadrangle.h:174
MLine.h
GEntity
Definition: GEntity.h:31
MSH_PYR_5
#define MSH_PYR_5
Definition: GmshDefines.h:86
GEntityPtrLessThan
Definition: GEntity.h:419
MSH_TRI_3
#define MSH_TRI_3
Definition: GmshDefines.h:81
MHexahedron::faces_hexa
static int faces_hexa(const int face, const int vert)
Definition: MHexahedron.h:198
MElement::getVertex
virtual const MVertex * getVertex(int num) const =0
GModel::getPhysicalGroups
void getPhysicalGroups(std::map< int, std::vector< GEntity * > > groups[4]) const
Definition: GModel.cpp:837
MHexahedron.h
GEntity::getNumMeshElements
virtual std::size_t getNumMeshElements() const
Definition: GEntity.h:348
GModel
Definition: GModel.h:44
MPyramid::faces_pyramid
static int faces_pyramid(const int face, const int vert)
Definition: MPyramid.h:181
Chain.h
MPyramid.h
MElement
Definition: MElement.h:30
MElementFactory::create
MElement * create(int type, std::vector< MVertex * > &v, std::size_t num=0, int part=0, bool owner=false, int parent=0, MElement *parent_ptr=nullptr, MElement *d1=nullptr, MElement *d2=nullptr)
Definition: MElement.cpp:2556
GEntity::getMeshElement
virtual MElement * getMeshElement(std::size_t index) const
Definition: GEntity.h:363
MSH_TET_4
#define MSH_TET_4
Definition: GmshDefines.h:83
MTetrahedron.h
MQuadrangle.h
MElement::getNumPrimaryVertices
std::size_t getNumPrimaryVertices() const
Definition: MElement.h:160
MPrism.h
MSH_PRI_6
#define MSH_PRI_6
Definition: GmshDefines.h:85
ElementType::getNumVertices
int getNumVertices(int type)
Definition: ElementType.cpp:456