gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
meshGFaceBamg.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 <map>
7 #include <numeric>
8 #include <iostream>
9 #include "meshGFaceBamg.h"
10 #include "GmshMessage.h"
11 #include "GFace.h"
12 #include "GModel.h"
13 #include "MVertex.h"
14 #include "MTriangle.h"
15 #include "MLine.h"
16 #include "GmshConfig.h"
17 #include "Context.h"
18 #include "BackgroundMeshTools.h"
20 #include "Options.h"
21 #include "meshGFace.h"
22 #include "MElementOctree.h"
23 #include "fullMatrix.h"
24 
25 #if defined(HAVE_BAMG)
26 
27 long verbosity = 0;
28 #include "Mesh2d.hpp"
29 #include "Mesh2.h"
30 Mesh2 *Bamg(Mesh2 *Thh, double *args, double *mm11, double *mm12, double *mm22,
31  bool);
32 
33 static void computeMeshMetricsForBamg(GFace *gf, int numV,
34  Vertex2 *bamgVertices, double *mm11,
35  double *mm12, double *mm22)
36 {
37  // char name[245];
38  // sprintf(name,"bgmBamg-%d-%d.pos",gf->tag(),iter);
39  // if (iter < 2){
40  // backgroundMesh::set(gf);
41  // backgroundMesh::current()->print(name, 0);
42  // }
43 
44  fullMatrix<double> J(2, 3), JT(3, 2), M(3, 3), R(2, 2), W(2, 3);
45  for(int i = 0; i < numV; ++i) {
46  double u = bamgVertices[i][0];
47  double v = bamgVertices[i][1];
48  GPoint gp = gf->point(SPoint2(u, v));
49  SMetric3 m = BGM_MeshMetric(gf, u, v, gp.x(), gp.y(), gp.z());
50 
51  // compute the derivatives of the parametrization
52  Pair<SVector3, SVector3> der = gf->firstDer(SPoint2(u, v));
53 
54  J(0, 0) = JT(0, 0) = der.first().x();
55  J(0, 1) = JT(1, 0) = der.first().y();
56  J(0, 2) = JT(2, 0) = der.first().z();
57  J(1, 0) = JT(0, 1) = der.second().x();
58  J(1, 1) = JT(1, 1) = der.second().y();
59  J(1, 2) = JT(2, 1) = der.second().z();
60 
61  m.getMat(M);
62  J.mult(M, W);
63  W.mult(JT, R);
64 
65  mm11[i] = R(0, 0);
66  mm12[i] = R(1, 0);
67  mm22[i] = R(1, 1);
68  }
69 }
70 
71 void meshGFaceBamg(GFace *gf)
72 {
73  std::vector<GEdge *> const &edges = gf->edges();
74  std::set<MVertex *> bcVertex;
75  for(auto it = edges.begin(); it != edges.end(); it++) {
76  for(std::size_t i = 0; i < (*it)->lines.size(); i++) {
77  bcVertex.insert((*it)->lines[i]->getVertex(0));
78  bcVertex.insert((*it)->lines[i]->getVertex(1));
79  }
80  }
81 
82  // fill mesh data fo bamg (bamgVertices, bamgTriangles, bamgBoundary)
83  std::set<MVertex *> all;
84  std::map<int, MVertex *> recover;
85  for(std::size_t i = 0; i < gf->triangles.size(); i++) {
86  for(std::size_t j = 0; j < 3; j++)
87  all.insert(gf->triangles[i]->getVertex(j));
88  }
89 
90  Vertex2 *bamgVertices = new Vertex2[all.size()];
91  int index = 0;
92  for(auto it = all.begin(); it != all.end(); ++it) {
93  if((*it)->onWhat()->dim() <= 1) {
94  SPoint2 p;
95  reparamMeshVertexOnFace(*it, gf, p);
96  bamgVertices[index][0] = p.x();
97  bamgVertices[index][1] = p.y();
98  bamgVertices[index].lab = index;
99  recover[index] = *it;
100  (*it)->setIndex(index++);
101  }
102  }
103  // exit(1);
104  int nbFixedVertices = index;
105  for(auto it = all.begin(); it != all.end(); ++it) {
106  // FIXME : SEAMS should have to be taken into account here !!!
107  if((*it)->onWhat()->dim() >= 2) {
108  SPoint2 p;
109  reparamMeshVertexOnFace(*it, gf, p);
110  bamgVertices[index][0] = p.x();
111  bamgVertices[index][1] = p.y();
112  recover[index] = *it;
113  (*it)->setIndex(index++);
114  }
115  }
116 
117  std::vector<MElement *> myParamElems;
118  std::vector<MVertex *> newVert;
119  Triangle2 *bamgTriangles = new Triangle2[gf->triangles.size()];
120  for(std::size_t i = 0; i < gf->triangles.size(); i++) {
121  int nodes[3] = {(int)gf->triangles[i]->getVertex(0)->getIndex(),
122  (int)gf->triangles[i]->getVertex(1)->getIndex(),
123  (int)gf->triangles[i]->getVertex(2)->getIndex()};
124  double u1(bamgVertices[nodes[0]][0]);
125  double u2(bamgVertices[nodes[1]][0]);
126  double u3(bamgVertices[nodes[2]][0]);
127  double v1(bamgVertices[nodes[0]][1]);
128  double v2(bamgVertices[nodes[1]][1]);
129  double v3(bamgVertices[nodes[2]][1]);
130  double sign = (u2 - u1) * (v3 - v1) - (u3 - u1) * (v2 - v1);
131  if(sign < 0) {
132  int temp = nodes[0];
133  nodes[0] = nodes[1];
134  nodes[1] = temp;
135  }
136  bamgTriangles[i].init(bamgVertices, nodes, gf->tag());
137  }
138 
139  const auto numEdges =
140  std::accumulate(begin(edges), end(edges), std::size_t(0),
141  [](std::size_t const partial_sum, const GEdge *const edge) {
142  return partial_sum + edge->lines.size();
143  });
144 
145  Seg *bamgBoundary = new Seg[numEdges];
146  int count = 0;
147  for(auto it = edges.begin(); it != edges.end(); ++it) {
148  for(std::size_t i = 0; i < (*it)->lines.size(); ++i) {
149  int nodes[2] = {(int)(*it)->lines[i]->getVertex(0)->getIndex(),
150  (int)(*it)->lines[i]->getVertex(1)->getIndex()};
151  bamgBoundary[count].init(bamgVertices, nodes, (*it)->tag());
152  bamgBoundary[count].lab = count;
153  count++;
154  }
155  }
156 
157  Mesh2 *bamgMesh = new Mesh2(all.size(), gf->triangles.size(), numEdges,
158  bamgVertices, bamgTriangles, bamgBoundary);
159 
160  MElementOctree *_octree = nullptr;
161 
162  Mesh2 *refinedBamgMesh = nullptr;
163  int iterMax = 41;
164  for(int k = 0; k < iterMax; k++) {
165  int nbVert = bamgMesh->nv;
166 
167  double *mm11 = new double[nbVert];
168  double *mm12 = new double[nbVert];
169  double *mm22 = new double[nbVert];
170  double args[256];
171  for(int i = 0; i < 256; i++) args[i] = -1.1e100;
172  args[16] = CTX::instance()->mesh.anisoMax;
173  args[7] = CTX::instance()->mesh.smoothRatio;
174  // args[ 21] = 90.0;//cutoffrad = 90 degree
175  computeMeshMetricsForBamg(gf, nbVert, bamgMesh->vertices, mm11, mm12, mm22);
176 
177  try {
178  refinedBamgMesh = Bamg(bamgMesh, args, mm11, mm12, mm22, false);
179  Msg::Info("BAMG succeeded %d vertices %d triangles", refinedBamgMesh->nv,
180  refinedBamgMesh->nt);
181  } catch(...) {
182  Msg::Error("BAMG failed");
183  return;
184  }
185  delete[] mm11;
186  delete[] mm12;
187  delete[] mm22;
188 
189  int nT = bamgMesh->nt;
190  int nTnow = refinedBamgMesh->nt;
191 
192  delete bamgMesh;
193  bamgMesh = refinedBamgMesh;
194  if(fabs((double)(nTnow - nT)) < 0.01 * nT) break;
195  }
196 
197  std::map<int, MVertex *> yetAnother;
198  for(int i = 0; i < refinedBamgMesh->nv; i++) {
199  Vertex2 &v = refinedBamgMesh->vertices[i];
200  if(i >= nbFixedVertices) {
201  GPoint gp = gf->point(SPoint2(v[0], v[1]));
202  // if (gp.x() > 2.){
203  // printf("wrong vertex index=%d %g %g %g (%g %g)\n",
204  // i, gp.x(), gp.y(), gp.z(), v[0], v[1]);
205  // }
206  // If point not found because compound edges have been remeshed and
207  // boundary triangles have changed then we call our new octree
208  MFaceVertex *x = new MFaceVertex(gp.x(), gp.y(), gp.z(), gf, v[0], v[1]);
209  yetAnother[i] = x;
210  gf->mesh_vertices.push_back(x);
211  }
212  else {
213  MVertex *v = recover[i];
214  yetAnother[i] = v;
215  }
216  }
217 
218  for(std::size_t i = 0; i < gf->triangles.size(); i++) {
219  delete gf->triangles[i];
220  }
221  gf->triangles.clear();
222  for(int i = 0; i < refinedBamgMesh->nt; i++) {
223  Triangle2 &t = refinedBamgMesh->triangles[i];
224  Vertex2 &v1 = t[0];
225  Vertex2 &v2 = t[1];
226  Vertex2 &v3 = t[2];
227  gf->triangles.push_back(new MTriangle(yetAnother[(*refinedBamgMesh)(v1)],
228  yetAnother[(*refinedBamgMesh)(v2)],
229  yetAnother[(*refinedBamgMesh)(v3)]));
230  }
231 
232  // delete pointers
233  if(refinedBamgMesh) delete refinedBamgMesh;
234  if(_octree) delete _octree;
235  for(auto it = myParamElems.begin(); it != myParamElems.end(); it++)
236  delete *it;
237  for(auto it = newVert.begin(); it != newVert.end(); it++) delete *it;
238 }
239 
240 #else
241 
243 {
244  Msg::Error("This version of Gmsh is not compiled with BAMG support");
245 }
246 
247 #endif
MTriangle.h
GFace::edges
virtual std::vector< GEdge * > const & edges() const
Definition: GFace.h:121
GFace::firstDer
virtual Pair< SVector3, SVector3 > firstDer(const SPoint2 &param) const =0
SMetric3
Definition: STensor3.h:17
GPoint::y
double y() const
Definition: GPoint.h:22
GFace.h
GFace
Definition: GFace.h:33
GFace::triangles
std::vector< MTriangle * > triangles
Definition: GFace.h:428
Msg::Info
static void Info(const char *fmt,...)
Definition: GmshMessage.cpp:587
GEdge::lines
std::vector< MLine * > lines
Definition: GEdge.h:42
SPoint2
Definition: SPoint2.h:12
MVertex
Definition: MVertex.h:24
MElementOctree.h
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
reparamMeshVertexOnFace
bool reparamMeshVertexOnFace(MVertex const *v, const GFace *gf, SPoint2 &param, bool onSurface, bool failOnSeam, int dir)
Definition: MVertex.cpp:522
GFace::point
virtual GPoint point(double par1, double par2) const =0
meshGFaceBamg
void meshGFaceBamg(GFace *gf)
Definition: meshGFaceBamg.cpp:242
GmshMessage.h
MLine.h
edges
static int edges[6][2]
Definition: meshGRegionLocalMeshMod.cpp:23
GPoint
Definition: GPoint.h:13
GPoint::z
double z() const
Definition: GPoint.h:23
CTX::instance
static CTX * instance()
Definition: Context.cpp:122
Pair::first
L first() const
Definition: Pair.h:22
fullMatrix< double >
contextMeshOptions::smoothRatio
double smoothRatio
Definition: Context.h:26
SMetric3::getMat
void getMat(fullMatrix< double > &mat) const
Definition: STensor3.cpp:51
Pair::second
R second() const
Definition: Pair.h:23
GEntity::mesh_vertices
std::vector< MVertex * > mesh_vertices
Definition: GEntity.h:56
MVertex.h
meshGFaceDelaunayInsertion.h
MElementOctree
Definition: MElementOctree.h:15
CTX::mesh
contextMeshOptions mesh
Definition: Context.h:313
GEntity::tag
int tag() const
Definition: GEntity.h:280
MFaceVertex
Definition: MVertex.h:168
SVector3::x
double x(void) const
Definition: SVector3.h:30
MTriangle
Definition: MTriangle.h:26
contextMeshOptions::anisoMax
double anisoMax
Definition: Context.h:26
meshGFace.h
SPoint2::x
double x(void) const
Definition: SPoint2.h:86
Context.h
SVector3::y
double y(void) const
Definition: SVector3.h:31
Pair
Definition: Pair.h:10
BGM_MeshMetric
SMetric3 BGM_MeshMetric(GEntity *ge, double U, double V, double X, double Y, double Z)
Definition: BackgroundMeshTools.cpp:284
GEdge
Definition: GEdge.h:26
SVector3::z
double z(void) const
Definition: SVector3.h:32
Options.h
BackgroundMeshTools.h
GModel.h
GPoint::x
double x() const
Definition: GPoint.h:21
fullMatrix.h
meshGFaceBamg.h
SPoint2::y
double y(void) const
Definition: SPoint2.h:88