gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
CellComplex.h
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 #ifndef CELLCOMPLEX_H
9 #define CELLCOMPLEX_H
10 
11 #include <map>
12 #include <string.h>
13 #include <set>
14 #include <algorithm>
15 #include <queue>
16 #include <string>
17 #include "Cell.h"
18 #include "MElement.h"
19 #include "GModel.h"
20 
21 class Cell;
22 class BdInfo;
23 
24 class CellComplex {
25 private:
26  std::pair<Cell *, double> _smallestCell;
27  std::pair<Cell *, double> _biggestCell;
28 
30 
31  // sorted containers of unique cells in this cell complex
32  // one for each dimension
33  std::set<Cell *, CellPtrLessThan> _cells[4];
34 
35  // original cells of this cell complex
36  std::set<Cell *, CellPtrLessThan> _ocells[4];
37 
38  // original cells removed during reductions
39  std::vector<Cell *> _removedcells;
40 
41  // cell complex dimension
42  int _dim;
43 
44  // cell are simplexes
46 
47  // save the original unreduced complex for another reduction run
48  bool _saveorig;
49 
50  // has a relative subdomain
51  bool _relative;
52 
55 
56  // is the cell complex at reduced state
57  bool _reduced;
58 
61 
62  // for constructor
63  bool _insertCells(std::vector<MElement *> &elements, int domain);
64  bool _removeCells(std::vector<MElement *> &elements, int domain);
65 
66  bool _immunizeCells(std::vector<MElement *> &elements);
67 
68  Cell *_omitCell(Cell *cell, bool dual);
69 
70  // enqueue cells in queue if they are not there already
71  void enqueueCells(std::map<Cell *, short int, CellPtrLessThan> &cells,
72  std::queue<Cell *> &Q,
73  std::set<Cell *, CellPtrLessThan> &Qset);
74 
75  // insert/remove a cell from this cell complex
76  void removeCell(Cell *cell, bool other = true, bool del = false);
77  void insertCell(Cell *cell);
78 
79  // queued coreduction
80  int coreduction(Cell *startCell, int omit, std::vector<Cell *> &omittedCells);
81 
82  static double _patience;
83 
84 public:
85  CellComplex(GModel *model, std::vector<MElement *> &domainElements,
86  std::vector<MElement *> &subdomainElements,
87  std::vector<MElement *> &nondomainElements,
88  std::vector<MElement *> &nonsubdomainElements,
89  std::vector<MElement *> &immuneElements,
90  bool saveOriginalComplex = true);
91  ~CellComplex();
92 
93  GModel *getModel() const { return _model; }
94  int getDim() const { return _dim; }
95  bool simplicial() const { return _simplicial; }
96  bool relative() const { return _relative; }
97 
98  // get the number of certain dimensional cells
99  // if dim = -1 return the number of all cells
100  int getSize(int dim, bool orig = false);
101 
102  // get domain of a cell
103  // cell in domain relative to subdomain -> domain = 0
104  // cell in domain -> domain = 1
105  // cell in subdomain -> domain = 2
106  int getDomain(Cell *cell, std::string &str);
107 
108  // get dim-dimensional cells
109  // domain = 0: cells in domain relative to subdomain
110  // domain = 1: cells in domain
111  // domain = 2: cells in subdomain
112  void getCells(std::set<Cell *, CellPtrLessThan> &cells, int dim,
113  int domain = 0);
114  int getNumCells(int dim, int domain = 0);
115  Cell *getACell(int dim, int domain = 0);
116  // std::set<Cell*, CellPtrLessThan> getOrigCells(int dim){ return
117  // _ocells[dim]; }
118 
119  // iterator for the cells of same dimension
120  typedef std::set<Cell *, CellPtrLessThan>::iterator citer;
121 
122  // iterators to the first and last cells of certain dimension
123  citer firstCell(int dim, bool orig = false)
124  {
125  return orig ? _ocells[dim].begin() : _cells[dim].begin();
126  }
127  citer lastCell(int dim, bool orig = false)
128  {
129  return orig ? _ocells[dim].end() : _cells[dim].end();
130  }
131 
132  // true if cell complex has given cell
133  bool hasCell(Cell *cell, bool orig = false);
134 
135  // check whether two cells both belong to subdomain or if neither one does
136  bool inSameDomain(Cell *c1, Cell *c2) const
137  {
138  return (c1->getDomain() == c2->getDomain());
139  }
140 
141  // remove cells in subdomain from this cell complex
142  void removeSubdomain();
143 
144  // remove dim-dimensional cells from this cell complex
145  void removeCells(int dim);
146 
147  // (co)reduction of this cell complex
148  // removes (co)reduction pairs of cell of dimension dim and dim-1
149  int reduction(int dim, int omit, std::vector<Cell *> &omittedCells);
150  int coreduction(int dim, int omit, std::vector<Cell *> &omittedCells);
151 
152  // Cell combining for reduction and coreduction
153  int combine(int dim);
154  int cocombine(int dim);
155 
156  // check whether all boundary cells of a cell has this cell
157  // as coboundary cell and vice versa
158  // also check whether all (co)boundary cells of a cell
159  // belong to this cell complex
160  bool coherent();
161 
162  // full (co)reduction of this cell complex (all dimensions)
163  // (combine = 1 -> with combining)
164  // (omit = true -> with highest dimensional cell omitting?)
165  // (homseq = true -> homology sequence splitting possible after reduction)
166  // (heuristic = 0 -> no heuristic, let mesh indexing determine
167  // 1 -> omit 0-cell in biggest element
168  // -1 -> omit 0-cell in smallest element)
169  int reduceComplex(int combine = 1, bool omit = true, bool homseq = false);
170  int coreduceComplex(int combine = 1, bool omit = true, int heuristic = 0);
171 
172  // reduce cell complex for Betti number computation
173  void bettiReduceComplex();
174 
175  bool isReduced() const { return _reduced; }
176 
178  {
179  return getSize(0) - getSize(1) + getSize(2) - getSize(3);
180  }
181  void printEuler()
182  {
183  printf("Euler characteristic: %d. \n", eulerCharacteristic());
184  }
185 
186  // restore the cell complex to its original state before (co)reduction
187  bool restoreComplex();
188 
189  // print the vertices of cells of certain dimension
190  void printComplex(int dim);
191 
192  // experimental
193  int saveComplex(const std::string &filename);
194  int loadComplex(const std::string &filename);
195 };
196 
197 #endif
CellComplex::_saveorig
bool _saveorig
Definition: CellComplex.h:48
CellComplex::removeCells
void removeCells(int dim)
Definition: CellComplex.cpp:568
CellComplex::getCells
void getCells(std::set< Cell *, CellPtrLessThan > &cells, int dim, int domain=0)
Definition: CellComplex.cpp:880
CellComplex::citer
std::set< Cell *, CellPtrLessThan >::iterator citer
Definition: CellComplex.h:120
CellComplex::CellComplex
CellComplex(GModel *model, std::vector< MElement * > &domainElements, std::vector< MElement * > &subdomainElements, std::vector< MElement * > &nondomainElements, std::vector< MElement * > &nonsubdomainElements, std::vector< MElement * > &immuneElements, bool saveOriginalComplex=true)
Definition: CellComplex.cpp:14
CellComplex::getModel
GModel * getModel() const
Definition: CellComplex.h:93
CellComplex::printEuler
void printEuler()
Definition: CellComplex.h:181
CellComplex::getSize
int getSize(int dim, bool orig=false)
Definition: CellComplex.cpp:425
CellComplex::_removeCells
bool _removeCells(std::vector< MElement * > &elements, int domain)
Definition: CellComplex.cpp:158
CellComplex::_removedcells
std::vector< Cell * > _removedcells
Definition: CellComplex.h:39
CellComplex::_smallestCell
std::pair< Cell *, double > _smallestCell
Definition: CellComplex.h:26
CellComplex::getACell
Cell * getACell(int dim, int domain=0)
Definition: CellComplex.cpp:904
CellComplex::_numRelativeCells
int _numRelativeCells[4]
Definition: CellComplex.h:59
CellComplex::coreduction
int coreduction(Cell *startCell, int omit, std::vector< Cell * > &omittedCells)
Definition: CellComplex.cpp:301
CellComplex::_deleteCount
int _deleteCount
Definition: CellComplex.h:53
CellComplex::lastCell
citer lastCell(int dim, bool orig=false)
Definition: CellComplex.h:127
CellComplex::_cells
std::set< Cell *, CellPtrLessThan > _cells[4]
Definition: CellComplex.h:33
CellComplex::_ocells
std::set< Cell *, CellPtrLessThan > _ocells[4]
Definition: CellComplex.h:36
CellComplex::_dim
int _dim
Definition: CellComplex.h:42
CellComplex::coreduceComplex
int coreduceComplex(int combine=1, bool omit=true, int heuristic=0)
Definition: CellComplex.cpp:579
CellComplex::removeCell
void removeCell(Cell *cell, bool other=true, bool del=false)
Definition: CellComplex.cpp:256
CellComplex::~CellComplex
~CellComplex()
Definition: CellComplex.cpp:226
CellComplex::_simplicial
bool _simplicial
Definition: CellComplex.h:45
CellComplex::coherent
bool coherent()
Definition: CellComplex.cpp:822
CellComplex::_relative
bool _relative
Definition: CellComplex.h:51
CellComplex::_insertCells
bool _insertCells(std::vector< MElement * > &elements, int domain)
Definition: CellComplex.cpp:66
CellComplex::getDomain
int getDomain(Cell *cell, std::string &str)
Definition: CellComplex.cpp:441
GModel
Definition: GModel.h:44
CellComplex
Definition: CellComplex.h:24
CellComplex::_immunizeCells
bool _immunizeCells(std::vector< MElement * > &elements)
Definition: CellComplex.cpp:213
CellComplex::_model
GModel * _model
Definition: CellComplex.h:29
Cell
Definition: Cell.h:42
CellComplex::_biggestCell
std::pair< Cell *, double > _biggestCell
Definition: CellComplex.h:27
CellComplex::_omitCell
Cell * _omitCell(Cell *cell, bool dual)
Definition: CellComplex.cpp:461
CellComplex::removeSubdomain
void removeSubdomain()
Definition: CellComplex.cpp:555
CellComplex::insertCell
void insertCell(Cell *cell)
Definition: CellComplex.cpp:245
CellComplex::isReduced
bool isReduced() const
Definition: CellComplex.h:175
CellComplex::enqueueCells
void enqueueCells(std::map< Cell *, short int, CellPtrLessThan > &cells, std::queue< Cell * > &Q, std::set< Cell *, CellPtrLessThan > &Qset)
Definition: CellComplex.cpp:287
CellComplex::printComplex
void printComplex(int dim)
Definition: CellComplex.cpp:975
CellComplex::combine
int combine(int dim)
Definition: CellComplex.cpp:666
CellComplex::bettiReduceComplex
void bettiReduceComplex()
Definition: CellComplex.cpp:659
CellComplex::eulerCharacteristic
int eulerCharacteristic()
Definition: CellComplex.h:177
MElement.h
CellComplex::getDim
int getDim() const
Definition: CellComplex.h:94
CellComplex::loadComplex
int loadComplex(const std::string &filename)
Definition: CellComplex.cpp:1028
BdInfo
Definition: Cell.h:23
CellComplex::_createCount
int _createCount
Definition: CellComplex.h:54
GModel.h
CellComplex::firstCell
citer firstCell(int dim, bool orig=false)
Definition: CellComplex.h:123
CellComplex::restoreComplex
bool restoreComplex()
Definition: CellComplex.cpp:929
Cell.h
CellComplex::inSameDomain
bool inSameDomain(Cell *c1, Cell *c2) const
Definition: CellComplex.h:136
CellComplex::reduceComplex
int reduceComplex(int combine=1, bool omit=true, bool homseq=false)
Definition: CellComplex.cpp:499
CellComplex::relative
bool relative() const
Definition: CellComplex.h:96
CellComplex::cocombine
int cocombine(int dim)
Definition: CellComplex.cpp:744
CellComplex::hasCell
bool hasCell(Cell *cell, bool orig=false)
Definition: CellComplex.cpp:867
c1
const double c1
Definition: GaussQuadratureHex.cpp:16
CellComplex::_patience
static double _patience
Definition: CellComplex.h:82
CellComplex::saveComplex
int saveComplex(const std::string &filename)
Definition: CellComplex.cpp:987
Cell::getDomain
int getDomain() const
Definition: Cell.h:77
CellComplex::simplicial
bool simplicial() const
Definition: CellComplex.h:95
CellComplex::_reduced
bool _reduced
Definition: CellComplex.h:57
CellComplex::reduction
int reduction(int dim, int omit, std::vector< Cell * > &omittedCells)
Definition: CellComplex.cpp:343
CellComplex::getNumCells
int getNumCells(int dim, int domain=0)
Definition: CellComplex.cpp:893
CellComplex::_numSubdomainCells
int _numSubdomainCells[4]
Definition: CellComplex.h:60