gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
SimplePartition.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 #include "GModel.h"
7 #include "GmshConfig.h"
8 #include "SimplePartition.h"
9 #include "partitionFace.h"
10 #include "partitionEdge.h"
11 #include "MElement.h"
12 #include "MLine.h"
13 #include "MTriangle.h"
14 #include "MQuadrangle.h"
15 #include "MFace.h"
16 #include "MEdge.h"
17 #include "mathEvaluator.h"
18 #include "Context.h"
19 
20 #if defined(HAVE_MESH)
21 #include "meshPartition.h"
22 #endif
23 
25  {GMSH_FULLRC, "NumSlicesX", nullptr, 4.},
26  {GMSH_FULLRC, "NumSlicesY", nullptr, 1.},
27  {GMSH_FULLRC, "NumSlicesZ", nullptr, 1.}
28 };
29 
31  {GMSH_FULLRC, "MappingX", nullptr, "t"},
32  {GMSH_FULLRC, "MappingY", nullptr, "t"},
33  {GMSH_FULLRC, "MappingZ", nullptr, "t"}
34 };
35 
36 extern "C" {
38 {
39  return new GMSH_SimplePartitionPlugin();
40 }
41 }
42 
44 {
45  return "Plugin(SimplePartition) partitions the current mesh into "
46  "`NumSlicesX', `NumSlicesY' and `NumSlicesZ' slices along the X-, Y- "
47  "and Z-axis, respectively. The distribution of these slices is "
48  "governed by `MappingX', `MappingY' and `MappingZ', where `t' is a "
49  "normalized absissa along each direction. (Setting `MappingX' to "
50  "`t' will thus lead to equidistant slices along the X-axis.)";
51 }
52 
54 {
55  return sizeof(SimplePartitionOptions_Number) / sizeof(StringXNumber);
56 }
57 
59 {
60  return &SimplePartitionOptions_Number[iopt];
61 }
62 
64 {
65  return sizeof(SimplePartitionOptions_String) / sizeof(StringXString);
66 }
67 
69 {
70  return &SimplePartitionOptions_String[iopt];
71 }
72 
74 {
75 #if defined(HAVE_MESH)
76  int numSlicesX = (int)SimplePartitionOptions_Number[0].def;
77  int numSlicesY = (int)SimplePartitionOptions_Number[1].def;
78  int numSlicesZ = (int)SimplePartitionOptions_Number[2].def;
79  std::vector<std::string> exprX(1), exprY(1), exprZ(1);
80  exprX[0] = SimplePartitionOptions_String[0].def;
81  exprY[0] = SimplePartitionOptions_String[1].def;
82  exprZ[0] = SimplePartitionOptions_String[2].def;
83 
84  GModel *m = GModel::current();
85 
86  if(!m->getNumMeshElements()) {
87  Msg::Error("Plugin(SimplePartition) requires a mesh");
88  return 0;
89  }
90 
91  if(numSlicesX < 1 || numSlicesY < 1 || numSlicesZ < 1) {
92  Msg::Error("Number of slices should be strictly positive");
93  return 0;
94  }
95  m->unpartitionMesh();
96 
97  SBoundingBox3d bbox = m->bounds();
98  double pminX = bbox.min()[0], pmaxX = bbox.max()[0];
99  double pminY = bbox.min()[1], pmaxY = bbox.max()[1];
100  double pminZ = bbox.min()[2], pmaxZ = bbox.max()[2];
101  std::vector<double> ppX(numSlicesX + 1);
102  std::vector<double> ppY(numSlicesY + 1);
103  std::vector<double> ppZ(numSlicesZ + 1);
104  std::vector<std::string> variables(1, "t");
105  std::vector<double> values(1), res(1);
106 
107  {
108  mathEvaluator f(exprX, variables);
109  for(int p = 0; p <= numSlicesX; p++) {
110  double t = values[0] = (double)p / (double)numSlicesX;
111  if(f.eval(values, res)) t = res[0];
112  ppX[p] = pminX + t * (pmaxX - pminX);
113  }
114  }
115  bool emptyX = (ppX[0] == ppX[numSlicesX]);
116  {
117  mathEvaluator f(exprY, variables);
118  for(int p = 0; p <= numSlicesY; p++) {
119  double t = values[0] = (double)p / (double)numSlicesY;
120  if(f.eval(values, res)) t = res[0];
121  ppY[p] = pminY + t * (pmaxY - pminY);
122  }
123  }
124  bool emptyY = (ppY[0] == ppY[numSlicesY]);
125  {
126  mathEvaluator f(exprZ, variables);
127  for(int p = 0; p <= numSlicesZ; p++) {
128  double t = values[0] = (double)p / (double)numSlicesZ;
129  if(f.eval(values, res)) t = res[0];
130  ppZ[p] = pminZ + t * (pmaxZ - pminZ);
131  }
132  }
133  bool emptyZ = (ppZ[0] == ppZ[numSlicesZ]);
134 
135  std::vector<GEntity *> entities;
136  m->getEntities(entities);
137  std::vector<std::pair<MElement *, int> > elmToPartition;
138  for(std::size_t i = 0; i < entities.size(); i++) {
139  GEntity *ge = entities[i];
140  for(std::size_t j = 0; j < ge->getNumMeshElements(); j++) {
141  MElement *e = ge->getMeshElement(j);
142  SPoint3 point = e->barycenter();
143  int part = 0;
144  for(int kx = 0; kx < numSlicesX; kx++) {
145  if(part) break;
146  for(int ky = 0; ky < numSlicesY; ky++) {
147  if(part) break;
148  for(int kz = 0; kz < numSlicesZ; kz++) {
149  if(part) break;
150  if((emptyX || (kx == 0 && ppX[0] == point[0]) ||
151  (ppX[kx] < point[0] && point[0] <= ppX[kx + 1])) &&
152  (emptyY || (ky == 0 && ppY[0] == point[1]) ||
153  (ppY[ky] < point[1] && point[1] <= ppY[ky + 1])) &&
154  (emptyZ || (kz == 0 && ppZ[0] == point[2]) ||
155  (ppZ[kz] < point[2] && point[2] <= ppZ[kz + 1]))) {
156  part = kx * numSlicesY * numSlicesZ + ky * numSlicesZ + kz + 1;
157  elmToPartition.push_back(
158  std::pair<MElement *, unsigned int>(e, part));
159  e->setPartition(part); // this will be removed
160  }
161  }
162  }
163  }
164  }
165  }
166 
167  int ier = PartitionUsingThisSplit(m, elmToPartition);
168  if(!ier) {
171  }
172 
173 #else
174  Msg::Error("Gmsh must be compiled with Mesh support to partition meshes");
175 #endif
176 
177  return 0;
178 }
StringXString
Definition: Options.h:910
GMSH_SimplePartitionPlugin::getOption
StringXNumber * getOption(int iopt)
Definition: SimplePartition.cpp:58
MTriangle.h
SimplePartitionOptions_Number
StringXNumber SimplePartitionOptions_Number[]
Definition: SimplePartition.cpp:24
GMSH_SET
#define GMSH_SET
Definition: Options.h:12
mathEvaluator
Definition: mathEvaluator.h:37
opt_mesh_color_carousel
double opt_mesh_color_carousel(OPT_ARGS_NUM)
Definition: Options.cpp:6425
mathEvaluator.h
GMSH_Plugin
Definition: Plugin.h:26
MElement::setPartition
virtual void setPartition(int num)
Definition: MElement.h:93
GMSH_SimplePartitionPlugin::getNbOptions
int getNbOptions() const
Definition: SimplePartition.cpp:53
GMSH_SimplePartitionPlugin::getOptionStr
StringXString * getOptionStr(int iopt)
Definition: SimplePartition.cpp:68
SimplePartitionOptions_String
StringXString SimplePartitionOptions_String[]
Definition: SimplePartition.cpp:30
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
SPoint3
Definition: SPoint3.h:14
LegendrePolynomials::f
void f(int n, double u, double *val)
Definition: orthogonalBasis.cpp:77
StringXNumber
Definition: Options.h:918
SBoundingBox3d::min
SPoint3 min() const
Definition: SBoundingBox3d.h:90
PartitionUsingThisSplit
int PartitionUsingThisSplit(GModel *model, std::vector< std::pair< MElement *, int > > &elmToPartition)
Definition: meshPartition.cpp:2656
MLine.h
GModel::getNumMeshElements
std::size_t getNumMeshElements(int dim=-1) const
Definition: GModel.cpp:1540
GEntity
Definition: GEntity.h:31
GMSH_SimplePartitionPlugin::run
int run()
Definition: SimplePartition.cpp:73
GMSH_SimplePartitionPlugin
Definition: SimplePartition.h:15
CTX::instance
static CTX * instance()
Definition: Context.cpp:122
SimplePartition.h
GModel::bounds
SBoundingBox3d bounds(bool aroundVisible=false)
Definition: GModel.cpp:1043
MFace.h
GEntity::getNumMeshElements
virtual std::size_t getNumMeshElements() const
Definition: GEntity.h:348
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
meshPartition.h
GModel
Definition: GModel.h:44
GMSH_RegisterSimplePartitionPlugin
GMSH_Plugin * GMSH_RegisterSimplePartitionPlugin()
Definition: SimplePartition.cpp:37
GMSH_GUI
#define GMSH_GUI
Definition: Options.h:14
GMSH_SimplePartitionPlugin::getHelp
std::string getHelp() const
Definition: SimplePartition.cpp:43
CTX::mesh
contextMeshOptions mesh
Definition: Context.h:313
StringXString::def
std::string def
Definition: Options.h:914
MElement
Definition: MElement.h:30
partitionFace.h
partitionEdge.h
GModel::getEntities
void getEntities(std::vector< GEntity * > &entities, int dim=-1) const
Definition: GModel.cpp:651
GMSH_SimplePartitionPlugin::getNbOptionsStr
int getNbOptionsStr() const
Definition: SimplePartition.cpp:63
contextMeshOptions::changed
int changed
Definition: Context.h:82
MEdge.h
ENT_ALL
#define ENT_ALL
Definition: GmshDefines.h:235
Context.h
GEntity::getMeshElement
virtual MElement * getMeshElement(std::size_t index) const
Definition: GEntity.h:363
GModel::unpartitionMesh
int unpartitionMesh()
Definition: GModel.cpp:2226
MQuadrangle.h
MElement.h
MElement::barycenter
virtual SPoint3 barycenter(bool primary=false) const
Definition: MElement.cpp:520
GModel.h
SBoundingBox3d::max
SPoint3 max() const
Definition: SBoundingBox3d.h:91
SBoundingBox3d
Definition: SBoundingBox3d.h:21
GModel::current
static GModel * current(int index=-1)
Definition: GModel.cpp:136