gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
VoroMetal.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 // Contributor(s):
6 // Tristan Carrier
7 // Maxime Melchior
8 
9 #include <vector>
10 #include <fstream>
11 #include <algorithm>
12 #include "GmshConfig.h"
13 #include "GModel.h"
14 #include "GmshMessage.h"
15 #include "MElement.h"
16 #include "VoroMetal.h"
17 
19  {GMSH_FULLRC, "ComputeBestSeeds", nullptr, 0.},
20  {GMSH_FULLRC, "ComputeMicrostructure", nullptr, 1.}};
21 
23  {GMSH_FULLRC, "SeedsFile", nullptr, "seeds.txt"},
24 };
25 
26 extern "C" {
28 {
29  return new GMSH_VoroMetalPlugin();
30 }
31 }
32 
33 std::string GMSH_VoroMetalPlugin::getHelp() const
34 {
35  return "Plugin(VoroMetal) creates microstructures using Voronoi "
36  "diagrams.\n\n";
37 }
38 
40 {
41  return sizeof(VoroMetalOptions_Number) / sizeof(StringXNumber);
42 }
43 
45 {
46  return sizeof(VoroMetalOptions_String) / sizeof(StringXString);
47 }
48 
50 {
51  return &VoroMetalOptions_Number[iopt];
52 }
53 
55 {
56  return &VoroMetalOptions_String[iopt];
57 }
58 
59 #if defined(HAVE_MESH) && defined(HAVE_VOROPP)
60 
61 #include "meshGRegion.h"
62 #include "voro++.hh"
63 
64 using namespace voro;
65 
66 void voroMetal3D::execute(double h)
67 {
68  GRegion *gr;
69  GModel *model = GModel::current();
70  GModel::riter it;
71  for(it = model->firstRegion(); it != model->lastRegion(); it++) {
72  gr = *it;
73  if(gr->getNumMeshElements() > 0) { execute(gr, h); }
74  }
75 }
76 
77 void voroMetal3D::execute(GRegion *gr, double h)
78 {
79  std::set<MVertex *> vertices;
80  std::set<MVertex *>::iterator it;
81 
82  for(std::size_t i = 0; i < gr->getNumMeshElements(); i++) {
84  for(std::size_t j = 0; j < element->getNumVertices(); j++) {
85  MVertex *vertex = element->getVertex(j);
86  vertices.insert(vertex);
87  }
88  }
89 
90  std::vector<SPoint3> vertices2;
91  vertices2.reserve(vertices.size());
92 
93  std::vector<double> radii(vertices.size(), 1.0);
94 
95  for(it = vertices.begin(); it != vertices.end(); it++) {
96  vertices2.push_back(SPoint3((*it)->x(), (*it)->y(), (*it)->z()));
97  }
98 
99  double xMax = 1.0;
100  double yMax = 1.0;
101  double zMax = 1.0;
102  execute(vertices2, radii, 0, h, xMax, yMax, zMax);
103 }
104 
105 void voroMetal3D::execute(std::vector<double> &properties, int radical,
106  double h, double xMax, double yMax, double zMax)
107 {
108  std::size_t i;
109  std::vector<SPoint3> vertices;
110  std::vector<double> radii;
111  for(i = 0; i < properties.size() / 4; i++) {
112  vertices.push_back(
113  SPoint3(properties[4 * i], properties[4 * i + 1], properties[4 * i + 2]));
114  radii.push_back(properties[4 * i + 3]);
115  }
116  execute(vertices, radii, radical, h, xMax, yMax, zMax);
117 }
118 
119 void voroMetal3D::execute(std::vector<SPoint3> &vertices,
120  std::vector<double> &radii, int radical, double h,
121  double xMax, double yMax, double zMax)
122 {
123  std::size_t i;
124  std::size_t j;
125  std::size_t k;
126  int start;
127  std::size_t end;
128  int index1;
129  int index2;
130  int val;
131  int face_number;
132  int last;
133  int mem;
134  int number;
135  double x, y, z;
136  double x1, y1, z1;
137  double x2, y2, z2;
138  double delta;
139  double min_x, max_x;
140  double min_y, max_y;
141  double min_z, max_z;
142  double min_area;
143  voronoicell_neighbor *pointer;
144  voronoicell_neighbor cell;
145  std::vector<int> faces;
146  std::vector<double> voronoi_vertices;
147  std::vector<voronoicell_neighbor *> pointers;
148  std::vector<SPoint3> generators;
149  std::vector<int> temp;
150  std::vector<int> temp2;
151  std::vector<double> areas;
152  std::map<int, int> table;
153  geo_cell obj;
154 
155  min_x = 1000000000.0;
156  max_x = -1000000000.0;
157  min_y = 1000000000.0;
158  max_y = -1000000000.0;
159  min_z = 1000000000.0;
160  max_z = -1000000000.0;
161  for(i = 0; i < vertices.size(); i++) {
162  min_x = std::min(vertices[i].x(), min_x);
163  max_x = std::max(vertices[i].x(), max_x);
164  min_y = std::min(vertices[i].y(), min_y);
165  max_y = std::max(vertices[i].y(), max_y);
166  min_z = std::min(vertices[i].z(), min_z);
167  max_z = std::max(vertices[i].z(), max_z);
168  }
169 
170  delta = 0.2 * (max_x - min_x);
171  min_x = min_y = min_z = 0;
172  // max_x=max_y=max_z = 1;
173  max_x = xMax;
174  max_y = yMax;
175  max_z = zMax;
176  delta = 0;
177 
178  container contA(min_x - delta, max_x + delta, min_y - delta, max_y + delta,
179  min_z - delta, max_z + delta, 6, 6, 6, true, true, true,
180  vertices.size());
181  container_poly contB(min_x - delta, max_x + delta, min_y - delta,
182  max_y + delta, min_z - delta, max_z + delta, 6, 6, 6,
183  true, true, true, vertices.size());
184 
185  for(i = 0; i < vertices.size(); i++) {
186  if(radical == 0) {
187  contA.put(i, vertices[i].x(), vertices[i].y(), vertices[i].z());
188  }
189  else {
190  contB.put(i, vertices[i].x(), vertices[i].y(), vertices[i].z(), radii[i]);
191  }
192  }
193 
194  number = 0;
195 
196  c_loop_all loopA(contA);
197  c_loop_all loopB(contB);
198 
199  if(radical == 0) {
200  loopA.start();
201  do {
202  contA.compute_cell(cell, loopA);
203  loopA.pos(x, y, z);
204  pointer = new voronoicell_neighbor();
205  *pointer = cell;
206  pointers.push_back(pointer);
207  generators.push_back(SPoint3(x, y, z));
208  table.insert(std::make_pair(loopA.pid(), number));
209  number++;
210  } while(loopA.inc());
211  }
212  else {
213  loopB.start();
214  do {
215  contB.compute_cell(cell, loopB);
216  loopB.pos(x, y, z);
217  pointer = new voronoicell_neighbor();
218  *pointer = cell;
219  pointers.push_back(pointer);
220  generators.push_back(SPoint3(x, y, z));
221  table.insert(std::make_pair(loopB.pid(), number));
222  number++;
223  } while(loopB.inc());
224  }
225 
226  std::ofstream file6("table.txt");
227  if(!file6.is_open()) {
228  Msg::Error("Could not open file 'table.txt'");
229  return;
230  }
231 
232  for(i = 0; i < vertices.size(); i++) {
233  file6 << i + 1 << " " << table[i] + 1 << "\n";
234  }
235 
236  initialize_counter();
237 
238  min_area = 1000000000.0;
239 
240  for(i = 0; i < pointers.size(); i++) {
241  areas.clear();
242  pointers[i]->face_areas(areas);
243  for(j = 0; j < areas.size(); j++) {
244  if(areas[j] < min_area) { min_area = areas[j]; }
245  }
246  }
247 
248  std::ofstream file("MicrostructurePolycrystal3D.pos");
249  if(!file.is_open()) {
250  Msg::Error("Could not open file 'MicrostructurePolycrystal3D.pos'");
251  return;
252  }
253  file << "View \"test\" {\n";
254 
255  std::ofstream file2("MicrostructurePolycrystal3D.geo");
256  if(!file2.is_open()) {
257  Msg::Error("Could not open file 'MicrostructurePolycrystal3D.geo'");
258  return;
259  }
260  std::ofstream file5("SET.map");
261  if(!file5.is_open()) {
262  Msg::Error("Could not open file 'SET.map'");
263  return;
264  }
265  file2 << "c=" << h << ";\n";
266  int countPeriodSurf = 0;
267  int countVolume = 0;
268  for(i = 0; i < pointers.size(); i++) {
269  obj = geo_cell();
270  faces.clear();
271  voronoi_vertices.clear();
272  pointers[i]->face_vertices(faces);
273  pointers[i]->vertices(generators[i].x(), generators[i].y(),
274  generators[i].z(), voronoi_vertices);
275  obj.line_loops.resize(pointers[i]->number_of_faces());
276  obj.orientations.resize(pointers[i]->number_of_faces());
277  face_number = 0;
278  end = 0;
279  while(end < faces.size()) {
280  start = end + 1;
281  end = start + faces[end];
282  for(j = start; j < end; j++) {
283  if(j < end - 1) {
284  index1 = faces[j];
285  index2 = faces[j + 1];
286  }
287  else {
288  index1 = faces[end - 1];
289  index2 = faces[start];
290  }
291  x1 = voronoi_vertices[3 * index1];
292  y1 = voronoi_vertices[3 * index1 + 1];
293  z1 = voronoi_vertices[3 * index1 + 2];
294  x2 = voronoi_vertices[3 * index2];
295  y2 = voronoi_vertices[3 * index2 + 1];
296  z2 = voronoi_vertices[3 * index2 + 2];
297  print_segment(SPoint3(x1, y1, z1), SPoint3(x2, y2, z2), file);
298 
299  val = obj.search_line(std::make_pair(index1, index2));
300  if(val == -1) {
301  obj.lines.push_back(std::make_pair(index1, index2));
302  obj.line_loops[face_number].push_back(obj.lines.size() - 1);
303  val = obj.lines.size() - 1;
304  }
305  else {
306  obj.line_loops[face_number].push_back(val);
307  }
308 
309  last = obj.line_loops[face_number].size() - 1;
310  if(last == 0) { obj.orientations[face_number].push_back(0); }
311  else if(obj.lines[obj.line_loops[face_number][last - 1]].second ==
312  obj.lines[val].first) {
313  obj.orientations[face_number][last - 1] = 0;
314  obj.orientations[face_number].push_back(0);
315  }
316  else if(obj.lines[obj.line_loops[face_number][last - 1]].first ==
317  obj.lines[val].first) {
318  obj.orientations[face_number][last - 1] = 1;
319  obj.orientations[face_number].push_back(0);
320  }
321  else if(obj.lines[obj.line_loops[face_number][last - 1]].second ==
322  obj.lines[val].second) {
323  obj.orientations[face_number][last - 1] = 0;
324  obj.orientations[face_number].push_back(1);
325  }
326  else {
327  obj.orientations[face_number][last - 1] = 1;
328  obj.orientations[face_number].push_back(1);
329  }
330  }
331 
332  face_number++;
333  }
334 
335  for(j = 0; j < voronoi_vertices.size() / 3; j++) {
336  print_geo_point(get_counter(), voronoi_vertices[3 * j],
337  voronoi_vertices[3 * j + 1], voronoi_vertices[3 * j + 2],
338  file2);
339  obj.points2.push_back(get_counter());
340  increase_counter();
341  }
342 
343  for(j = 0; j < obj.lines.size(); j++) {
344  print_geo_line(get_counter(), obj.points2[obj.lines[j].first],
345  obj.points2[obj.lines[j].second], file2);
346  obj.lines2.push_back(get_counter());
347  increase_counter();
348  }
349 
350  for(j = 0; j < obj.line_loops.size(); j++) {
351  temp.clear();
352  temp2.clear();
353  for(k = 0; k < obj.line_loops[j].size(); k++) {
354  temp.push_back(obj.lines2[obj.line_loops[j][k]]);
355  temp2.push_back(obj.orientations[j][k]);
356  }
357  print_geo_line_loop(get_counter(), temp, temp2, file2);
358  obj.line_loops2.push_back(get_counter());
359  increase_counter();
360  }
361 
362  for(j = 0; j < obj.line_loops2.size(); j++) {
363  print_geo_face(get_counter(), obj.line_loops2[j], file2);
364  countPeriodSurf++;
365  file5 << get_counter() << "\t"
366  << "SURFACE" << get_counter() << "\t"
367  << "NSET\n";
368  obj.faces2.push_back(get_counter());
369  increase_counter();
370  }
371 
372  print_geo_face_loop(get_counter(), obj.faces2, file2);
373  obj.face_loops2 = get_counter();
374  increase_counter();
375 
376  print_geo_volume(get_counter(), obj.face_loops2, file2);
377  mem = get_counter();
378  countVolume++;
379  file5 << get_counter() << "\t"
380  << "GRAIN" << countVolume << "\t"
381  << "ELSET\n";
382  increase_counter();
383  print_geo_physical_volume(get_counter(), mem, file2);
384  increase_counter();
385  }
386 
387  file2 << "Coherence;\n";
388  file << "};\n";
389 
390  for(i = 0; i < pointers.size(); i++) delete pointers[i];
391 }
392 
393 void voroMetal3D::print_segment(SPoint3 p1, SPoint3 p2, std::ofstream &file)
394 {
395  file << "SL (" << p1.x() << ", " << p1.y() << ", " << p1.z() << ", " << p2.x()
396  << ", " << p2.y() << ", " << p2.z() << "){10, 20};\n";
397 }
398 
399 void voroMetal3D::initialize_counter() { counter = 12; }
400 
401 void voroMetal3D::increase_counter() { counter = counter + 1; }
402 
403 int voroMetal3D::get_counter() { return counter; }
404 
405 void voroMetal3D::print_geo_point(int index, double x, double y, double z,
406  std::ofstream &file)
407 {
408  file.precision(17);
409 
410  file << "Point(" << index << ")={" << x << "," << y << "," << z << ",c};\n";
411 }
412 
413 void voroMetal3D::print_geo_line(int index1, int index2, int index3,
414  std::ofstream &file)
415 {
416  file << "Line(" << index1 << ")={" << index2 << "," << index3 << "};\n";
417 }
418 
419 void voroMetal3D::print_geo_face(int index1, int index2, std::ofstream &file)
420 {
421  file << "Plane Surface(" << index1 << ")={" << index2 << "};\n";
422 }
423 
424 void voroMetal3D::print_geo_physical_face(int index1, int index2,
425  std::ofstream &file)
426 {
427  file << "Physical Surface(" << index1 << ")={" << index2 << "};\n";
428 }
429 
430 void voroMetal3D::print_geo_volume(int index1, int index2, std::ofstream &file)
431 {
432  file << "Volume(" << index1 << ")={" << index2 << "};\n";
433 }
434 
435 void voroMetal3D::print_geo_physical_volume(int index1, int index2,
436  std::ofstream &file)
437 {
438  file << "Physical Volume(" << index1 << ")={" << index2 << "};\n";
439 }
440 
441 void voroMetal3D::print_geo_line_loop(int index, std::vector<int> &indices,
442  std::vector<int> &orientations,
443  std::ofstream &file)
444 {
445  std::size_t i;
446  file << "Curve Loop(" << index << ")={";
447  for(i = 0; i < indices.size(); i++) {
448  if(orientations[i] == 1) file << "-";
449  file << indices[i];
450  if(i < indices.size() - 1) file << ",";
451  }
452  file << "};\n";
453 }
454 
455 void voroMetal3D::print_geo_face_loop(int index, std::vector<int> &indices,
456  std::ofstream &file)
457 {
458  std::size_t i;
459  file << "Surface Loop(" << index << ")={";
460  for(i = 0; i < indices.size(); i++) {
461  file << indices[i];
462  if(i < indices.size() - 1) file << ",";
463  }
464  file << "};\n";
465 }
466 
467 void voroMetal3D::correspondence(double e, double xMax, double yMax,
468  double zMax)
469 {
470  std::size_t i;
471  std::size_t j;
472  int count;
473  int val;
474  int phase;
475  bool flag;
476  bool flag1;
477  bool flag2;
478  bool flag3;
479  bool flag4;
480  double x, y, z;
481  double delta_x;
482  double delta_y;
483  double delta_z;
484  SPoint3 p1;
485  SPoint3 p2;
486  GFace *gf;
487  GFace *gf1;
488  GFace *gf2;
489  GVertex *v1;
490  GVertex *v2;
491  GVertex *v3;
492  GVertex *v4;
493  GModel *model = GModel::current();
494  GModel::fiter it;
495  std::vector<GFace *> faces;
496  std::vector<std::pair<GFace *, GFace *> > pairs;
497  std::vector<int> categories;
498  std::vector<int> indices1;
499  std::vector<int> indices2;
500  std::vector<int> indices3;
501  std::vector<GVertex *> vertices;
502  std::vector<GEdge *> edges1;
503  std::vector<GEdge *> edges2;
504  std::vector<int> orientations1;
505  std::vector<int> orientations2;
506  std::map<GFace *, SPoint3> centers;
507  std::map<GFace *, bool> markings;
508  std::vector<GVertex *>::iterator it2;
509  std::map<GFace *, SPoint3>::iterator it3;
510  std::map<GFace *, SPoint3>::iterator it4;
511  std::map<GFace *, bool>::iterator it5;
512  std::map<GFace *, bool>::iterator it6;
513  std::vector<GEdge *>::iterator it7;
514  std::vector<GEdge *>::iterator it8;
515  std::vector<int>::iterator it9;
516  std::vector<int>::iterator it10;
517  std::vector<GEdge *>::iterator mem;
518 
519  faces.clear();
520 
521  for(it = model->firstFace(); it != model->lastFace(); it++) {
522  gf = *it;
523  if(gf->numRegions() == 1) { faces.push_back(gf); }
524  }
525 
526  centers.clear();
527  markings.clear();
528  pairs.clear();
529  categories.clear();
530 
531  for(i = 0; i < faces.size(); i++) {
532  x = 0.0;
533  y = 0.0;
534  z = 0.0;
535  vertices.clear();
536  vertices = faces[i]->vertices();
537  for(it2 = vertices.begin(); it2 != vertices.end(); it2++) {
538  x = x + (*it2)->x();
539  y = y + (*it2)->y();
540  z = z + (*it2)->z();
541  }
542  x = x / vertices.size();
543  y = y / vertices.size();
544  z = z / vertices.size();
545  centers.insert(std::make_pair(faces[i], SPoint3(x, y, z)));
546  }
547 
548  for(i = 0; i < faces.size(); i++) {
549  markings.insert(std::make_pair(faces[i], false));
550  }
551 
552  count = 0;
553  std::ofstream file("MicrostructurePolycrystal3D.pos");
554  if(!file.is_open()) {
555  Msg::Error("Could not open file 'MicrostructurePolycrystal3D.pos'");
556  return;
557  }
558  file << "View \"test\" {\n";
559 
560  std::ofstream file2("PERIODIC.map");
561  if(!file2.is_open()) {
562  Msg::Error("Could not open file 'PERIODIC.map'");
563  return;
564  }
565 
566  for(i = 0; i < faces.size(); i++) {
567  for(j = 0; j < faces.size(); j++) {
568  it3 = centers.find(faces[i]);
569  it4 = centers.find(faces[j]);
570  p1 = it3->second;
571  p2 = it4->second;
572  delta_x = std::abs(p2.x() - p1.x());
573  delta_y = std::abs(p2.y() - p1.y());
574  delta_z = std::abs(p2.z() - p1.z());
575  flag =
576  correspondence(delta_x, delta_y, delta_z, e, val, xMax, yMax, zMax);
577  if(flag) {
578  it5 = markings.find(faces[i]);
579  it6 = markings.find(faces[j]);
580  if(it5->second == 0 && it6->second == 0) {
581  it5->second = 1;
582  it6->second = 1;
583  pairs.push_back(std::make_pair(faces[i], faces[j]));
584  categories.push_back(val);
585  print_segment(p1, p2, file);
586  if(std::abs((p2.x() - p1.x() - 1.0)) < 0.0001) {
587  if(std::abs((p2.y() - p1.y())) < 0.0001) {
588  if(std::abs((p2.z() - p1.z())) < 0.0001) {
589  file2 << "NSET\tFRONT = FRONT + SURFACE" << faces[j]->tag()
590  << "\n";
591  file2 << "NSET\tBACK = BACK + SURFACE" << faces[i]->tag()
592  << "\n";
593  }
594  else if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
595  file2 << "NSET\tFRONTTOP = FRONTTOP + SURFACE"
596  << faces[j]->tag() << "\n";
597  file2 << "NSET\tBACKBOTTOM = BACKBOTTOM + SURFACE"
598  << faces[i]->tag() << "\n";
599  }
600  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
601  file2 << "NSET\tFRONTBOTTOM = FRONTBOTTOM + SURFACE"
602  << faces[j]->tag() << "\n";
603  file2 << "NSET\tBACKTOP = BACKTOP + SURFACE" << faces[i]->tag()
604  << "\n";
605  }
606  }
607  else if(std::abs((p2.y() - p1.y() - 1.0)) < 0.0001) {
608  if(std::abs((p2.z() - p1.z())) < 0.0001) {
609  file2 << "NSET\tFRONTRIGHT = FRONTRIGHT + SURFACE"
610  << faces[j]->tag() << "\n";
611  file2 << "NSET\tBACKLEFT = BACKLEFT + SURFACE"
612  << faces[i]->tag() << "\n";
613  }
614  else if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
615  file2 << "NSET\tFRONTRIGHTTOP = FRONTRIGHTTOP + SURFACE"
616  << faces[j]->tag() << "\n";
617  file2 << "NSET\tBACKLEFTBOTTOM = BACKLEFTBOTTOM + SURFACE"
618  << faces[i]->tag() << "\n";
619  }
620  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
621  file2 << "NSET\tFRONTRIGHTBOTTOM = FRONTRIGHTBOTTOM + SURFACE"
622  << faces[j]->tag() << "\n";
623  file2 << "NSET\tBACKLEFTTOP = BACKLEFTTOP + SURFACE"
624  << faces[i]->tag() << "\n";
625  }
626  }
627  else if(std::abs((p1.y() - p2.y() - 1.0)) < 0.0001) {
628  if(std::abs((p2.z() - p1.z())) < 0.0001) {
629  file2 << "NSET\tFRONTLEFT = FRONTLEFT + SURFACE"
630  << faces[j]->tag() << "\n";
631  file2 << "NSET\tBACKRIGHT = BACKRIGHT + SURFACE"
632  << faces[i]->tag() << "\n";
633  }
634  else if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
635  file2 << "NSET\tFRONTLEFTTOP = FRONTLEFTTOP + SURFACE"
636  << faces[j]->tag() << "\n";
637  file2 << "NSET\tBACKRIGHTBOTTOM = BACKRIGHTBOTTOM + SURFACE"
638  << faces[i]->tag() << "\n";
639  }
640  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
641  file2 << "NSET\tFRONTLEFTBOTTOM = FRONTLEFTBOTTOM + SURFACE"
642  << faces[j]->tag() << "\n";
643  file2 << "NSET\tBACKRIGHTTOP = BACKRIGHTTOP + SURFACE"
644  << faces[i]->tag() << "\n";
645  }
646  }
647  }
648  else if(std::abs((p1.x() - p2.x() - 1.0)) < 0.0001) {
649  if(std::abs((p2.y() - p1.y())) < 0.0001) {
650  if(std::abs((p2.z() - p1.z())) < 0.0001) {
651  file2 << "NSET\tFRONT = FRONT + SURFACE" << faces[i]->tag()
652  << "\n";
653  file2 << "NSET\tBACK = BACK + SURFACE" << faces[j]->tag()
654  << "\n";
655  }
656  else if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
657  file2 << "NSET\tFRONTBOTTOM = FRONTBOTTOM + SURFACE"
658  << faces[i]->tag() << "\n";
659  file2 << "NSET\tBACKTOP = BACKTOP + SURFACE" << faces[j]->tag()
660  << "\n";
661  }
662  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
663  file2 << "NSET\tFRONTTOP = FRONTTOP + SURFACE"
664  << faces[i]->tag() << "\n";
665  file2 << "NSET\tBACKBOTTOM = BACKBOTTOM + SURFACE"
666  << faces[j]->tag() << "\n";
667  }
668  }
669  else if(std::abs((p2.y() - p1.y() - 1.0)) < 0.0001) {
670  if(std::abs((p2.z() - p1.z())) < 0.0001) {
671  file2 << "NSET\tFRONTLEFT = FRONTLEFT + SURFACE"
672  << faces[i]->tag() << "\n";
673  file2 << "NSET\tBACKRIGHT = BACKRIGHT + SURFACE"
674  << faces[j]->tag() << "\n";
675  }
676  else if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
677  file2 << "NSET\tFRONTLEFTBOTTOM = FRONTLEFTBOTTOM + SURFACE"
678  << faces[i]->tag() << "\n";
679  file2 << "NSET\tBACKRIGHTTOP = BACKRIGHTTOP + SURFACE"
680  << faces[j]->tag() << "\n";
681  }
682  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
683  file2 << "NSET\tFRONTLEFTTOP = FRONTLEFTTOP + SURFACE"
684  << faces[i]->tag() << "\n";
685  file2 << "NSET\tBACKRIGHTBOTTOM = BACKRIGHTBOTTOM + SURFACE"
686  << faces[j]->tag() << "\n";
687  }
688  }
689  else if(std::abs((p1.y() - p2.y() - 1.0)) < 0.0001) {
690  if(std::abs((p2.z() - p1.z())) < 0.0001) {
691  file2 << "NSET\tFRONTRIGHT = FRONTRIGHT + SURFACE"
692  << faces[i]->tag() << "\n";
693  file2 << "NSET\tBACKLEFT = BACKLEFT + SURFACE"
694  << faces[j]->tag() << "\n";
695  }
696  else if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
697  file2 << "NSET\tFRONTRIGHTBOTTOM = FRONTRIGHTBOTTOM + SURFACE"
698  << faces[i]->tag() << "\n";
699  file2 << "NSET\tBACKLEFTTOP = BACKLEFTTOP + SURFACE"
700  << faces[j]->tag() << "\n";
701  }
702  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
703  file2 << "NSET\tFRONTRIGHTTOP = FRONTRIGHTTOP + SURFACE"
704  << faces[i]->tag() << "\n";
705  file2 << "NSET\tBACKLEFTBOTTOM = BACKLEFTBOTTOM + SURFACE"
706  << faces[j]->tag() << "\n";
707  }
708  }
709  }
710  else if(std::abs((p1.x() - p2.x())) < 0.0001) {
711  if(std::abs((p2.y() - p1.y() - 1.0)) < 0.0001) {
712  if(std::abs((p2.z() - p1.z())) < 0.0001) {
713  file2 << "NSET\tRIGHT = RIGHT + SURFACE" << faces[j]->tag()
714  << "\n";
715  file2 << "NSET\tLEFT = LEFT + SURFACE" << faces[i]->tag()
716  << "\n";
717  }
718  else if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
719  file2 << "NSET\tRIGHTTOP = RIGHTTOP + SURFACE"
720  << faces[j]->tag() << "\n";
721  file2 << "NSET\tLEFTBOTTOM = LEFTBOTTOM + SURFACE"
722  << faces[i]->tag() << "\n";
723  }
724  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
725  file2 << "NSET\tRIGHTBOTTOM = RIGHTBOTTOM + SURFACE"
726  << faces[j]->tag() << "\n";
727  file2 << "NSET\tLEFTTOP = LEFTTOP + SURFACE" << faces[i]->tag()
728  << "\n";
729  }
730  }
731  else if(std::abs((p1.y() - p2.y() - 1.0)) < 0.0001) {
732  if(std::abs((p2.z() - p1.z())) < 0.0001) {
733  file2 << "NSET\tRIGHT = RIGHT + SURFACE" << faces[i]->tag()
734  << "\n";
735  file2 << "NSET\tLEFT = LEFT + SURFACE" << faces[j]->tag()
736  << "\n";
737  }
738  else if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
739  file2 << "NSET\tRIGHTBOTTOM = RIGHTBOTTOM + SURFACE"
740  << faces[i]->tag() << "\n";
741  file2 << "NSET\tLEFTTOP = LEFTTOP + SURFACE" << faces[j]->tag()
742  << "\n";
743  }
744  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
745  file2 << "NSET\tRIGHTTOP = RIGHTTOP + SURFACE"
746  << faces[i]->tag() << "\n";
747  file2 << "NSET\tLEFTBOTTOM = LEFTBOTTOM + SURFACE"
748  << faces[j]->tag() << "\n";
749  }
750  }
751  else if(std::abs((p1.y() - p2.y())) < 0.0001) {
752  if(std::abs((p2.z() - p1.z() - 1.0)) < 0.0001) {
753  file2 << "NSET\tTOP = TOP + SURFACE" << faces[j]->tag() << "\n";
754  file2 << "NSET\tBOTTOM = BOTTOM + SURFACE" << faces[i]->tag()
755  << "\n";
756  }
757  else if(std::abs((p1.z() - p2.z() - 1.0)) < 0.0001) {
758  file2 << "NSET\tTOP = TOP + SURFACE" << faces[i]->tag() << "\n";
759  file2 << "NSET\tBOTTOM = BOTTOM + SURFACE" << faces[j]->tag()
760  << "\n";
761  }
762  }
763  }
764  count++;
765  }
766  }
767  }
768  }
769 
770  file << "};\n";
771 
772  std::ofstream file3;
773  file3.open("MicrostructurePolycrystal3D.geo", std::ios::out | std::ios::app);
774  if(!file3.is_open()) {
775  Msg::Error("Could not open file 'MicrostructurePolycrystal3D.geo'");
776  return;
777  }
778  file3.precision(17);
779 
780  std::ofstream file4("MicrostructurePolycrystal3D2.pos");
781  if(!file4.is_open()) {
782  Msg::Error("Could not open file 'MicrostructurePolycrystal3D2.pos'");
783  return;
784  }
785  file4 << "View \"test\" {\n";
786 
787  file3 << "Physical Surface(11)={";
788 
789  count = 0;
790  for(it = model->firstFace(); it != model->lastFace(); it++) {
791  gf = *it;
792  if(count > 0) file3 << ",";
793  file3 << gf->tag();
794  count++;
795  }
796 
797  file3 << "};\n";
798 
799  for(i = 0; i < pairs.size(); i++) {
800  gf1 = pairs[i].first;
801  gf2 = pairs[i].second;
802  std::vector<GVertex *> gv1 = gf1->vertices();
803  std::vector<GVertex *> gv2 = gf2->vertices();
804  auto it1 = gv1.begin();
805  auto it2 = gv2.begin();
806  SPoint3 cg1(0, 0, 0);
807  SPoint3 cg2(0, 0, 0);
808  for(; it1 != gv1.end(); it1++, it2++) {
809  cg1 += SPoint3((*it1)->x(), (*it1)->y(), (*it1)->z());
810  cg2 += SPoint3((*it2)->x(), (*it2)->y(), (*it2)->z());
811  }
812  SVector3 dx = (cg2 - cg1) * (1. / gv1.size());
813  edges1 = gf1->edges();
814  edges2 = gf2->edges();
815  orientations1 = gf1->edgeOrientations();
816  orientations2 = gf2->edgeOrientations();
817  indices1.clear();
818  indices2.clear();
819  indices3.clear();
820  phase = 1;
821 
822  it9 = orientations1.begin();
823  it10 = orientations2.begin();
824  for(it8 = edges2.begin(); it8 != edges2.end(); it8++, it10++) {
825  if(*it10 == 1)
826  indices3.push_back((*it8)->tag());
827  else
828  indices3.push_back(-(*it8)->tag());
829  }
830  int countReverseEdge = 0;
831  for(it7 = edges1.begin(); it7 != edges1.end(); it7++, it9++) {
832  v1 = (*it7)->getBeginVertex();
833  v2 = (*it7)->getEndVertex();
834  if(*it9 == 1)
835  indices1.push_back((*it7)->tag());
836  else
837  indices1.push_back(-(*it7)->tag());
838  flag1 = 0;
839  flag2 = 0;
840  flag3 = 0;
841  flag4 = 0;
842  it10 = orientations2.begin();
843  for(it8 = edges2.begin(); it8 != edges2.end(); it8++, it10++) {
844  v3 = (*it8)->getBeginVertex();
845  v4 = (*it8)->getEndVertex();
846  correspondence(fabs(v3->x() - v1->x()), fabs(v3->y() - v1->y()),
847  fabs(v3->z() - v1->z()), e, categories[i], flag1, xMax,
848  yMax, zMax);
849  correspondence(fabs(v4->x() - v2->x()), fabs(v4->y() - v2->y()),
850  fabs(v4->z() - v2->z()), e, categories[i], flag2, xMax,
851  yMax, zMax);
852 
853  correspondence(fabs(v4->x() - v1->x()), fabs(v4->y() - v1->y()),
854  fabs(v4->z() - v1->z()), e, categories[i], flag3, xMax,
855  yMax, zMax);
856  correspondence(fabs(v3->x() - v2->x()), fabs(v3->y() - v2->y()),
857  fabs(v3->z() - v2->z()), e, categories[i], flag4, xMax,
858  yMax, zMax);
859  if(flag1 && flag2) {
860  if(phase == 1) {
861  mem = it8;
862  phase = 2;
863  }
864  else if(phase == 2) {
865  mem++;
866  /*if(it8==mem){
867  normal = 1;
868  }
869  else{
870  normal = -1;
871  }*/
872  phase = 3;
873  }
874  if(*it9 == 1)
875  indices2.push_back((*it8)->tag());
876  else
877  indices2.push_back(-(*it8)->tag());
878  if(*it9 != *it10) countReverseEdge++;
879  print_segment(SPoint3(v3->x(), v3->y(), v3->z()),
880  SPoint3(v1->x(), v1->y(), v1->z()), file4);
881  print_segment(SPoint3(v4->x(), v4->y(), v4->z()),
882  SPoint3(v2->x(), v2->y(), v2->z()), file4);
883  }
884  else if(flag3 && flag4) {
885  if(phase == 1) {
886  mem = it8;
887  phase = 2;
888  }
889  else if(phase == 2) {
890  mem++;
891  /*if(it8==mem){
892  normal = 1;
893  }
894  else{
895  normal = -1;
896  }*/
897  phase = 3;
898  }
899  if(*it9 == 1)
900  indices2.push_back(-(*it8)->tag());
901  else
902  indices2.push_back((*it8)->tag());
903  if(*it9 != *it10) countReverseEdge++;
904  print_segment(SPoint3(v4->x(), v4->y(), v4->z()),
905  SPoint3(v1->x(), v1->y(), v1->z()), file4);
906  print_segment(SPoint3(v3->x(), v3->y(), v3->z()),
907  SPoint3(v2->x(), v2->y(), v2->z()), file4);
908  }
909  }
910  }
911 
912  if(indices1.size() != indices2.size()) { printf("Error\n\n"); }
913  file3 << "Periodic Surface {" << gf1->tag() << " }={ " << gf2->tag()
914  << " } Translate { " << -dx.x() << "," << -dx.y() << "," << -dx.z()
915  << "};\n";
916  }
917  file4 << "};\n";
918 }
919 
920 bool voroMetal3D::correspondence(double delta_x, double delta_y, double delta_z,
921  double e, int &val, double xMax, double yMax,
922  double zMax)
923 {
924  bool flag;
925 
926  flag = 0;
927  val = 1000;
928 
929  if(equal(delta_x, xMax, e) && equal(delta_y, 0.0, e) &&
930  equal(delta_z, 0.0, e)) {
931  flag = 1;
932  val = 1;
933  }
934  if(equal(delta_x, 0.0, e) && equal(delta_y, yMax, e) &&
935  equal(delta_z, 0.0, e)) {
936  flag = 1;
937  val = 2;
938  }
939  if(equal(delta_x, 0.0, e) && equal(delta_y, 0.0, e) &&
940  equal(delta_z, zMax, e)) {
941  flag = 1;
942  val = 3;
943  }
944 
945  if(equal(delta_x, xMax, e) && equal(delta_y, yMax, e) &&
946  equal(delta_z, 0.0, e)) {
947  flag = 1;
948  val = 4;
949  }
950  if(equal(delta_x, 0.0, e) && equal(delta_y, yMax, e) &&
951  equal(delta_z, zMax, e)) {
952  flag = 1;
953  val = 5;
954  }
955  if(equal(delta_x, xMax, e) && equal(delta_y, 0.0, e) &&
956  equal(delta_z, zMax, e)) {
957  flag = 1;
958  val = 6;
959  }
960 
961  if(equal(delta_x, xMax, e) && equal(delta_y, yMax, e) &&
962  equal(delta_z, zMax, e)) {
963  flag = 1;
964  val = 7;
965  }
966  return flag;
967 }
968 
969 void voroMetal3D::correspondence(double delta_x, double delta_y, double delta_z,
970  double e, int val, bool &flag, double xMax,
971  double yMax, double zMax)
972 {
973  flag = 0;
974 
975  if(val == 1 && equal(delta_x, xMax, e) && equal(delta_y, 0.0, e) &&
976  equal(delta_z, 0.0, e)) {
977  flag = 1;
978  }
979  if(val == 2 && equal(delta_x, 0.0, e) && equal(delta_y, yMax, e) &&
980  equal(delta_z, 0.0, e)) {
981  flag = 1;
982  }
983  if(val == 3 && equal(delta_x, 0.0, e) && equal(delta_y, 0.0, e) &&
984  equal(delta_z, zMax, e)) {
985  flag = 1;
986  }
987 
988  if(val == 4 && equal(delta_x, xMax, e) && equal(delta_y, yMax, e) &&
989  equal(delta_z, 0.0, e)) {
990  flag = 1;
991  }
992  if(val == 5 && equal(delta_x, 0.0, e) && equal(delta_y, yMax, e) &&
993  equal(delta_z, zMax, e)) {
994  flag = 1;
995  }
996  if(val == 6 && equal(delta_x, xMax, e) && equal(delta_y, 0.0, e) &&
997  equal(delta_z, zMax, e)) {
998  flag = 1;
999  }
1000 
1001  if(val == 7 && equal(delta_x, xMax, e) && equal(delta_y, yMax, e) &&
1002  equal(delta_z, zMax, e)) {
1003  flag = 1;
1004  }
1005 }
1006 
1007 bool voroMetal3D::equal(double x, double y, double e)
1008 {
1009  bool flag;
1010  if(x > y - e && x < y + e) { flag = 1; }
1011  else {
1012  flag = 0;
1013  }
1014  return flag;
1015 }
1016 
1017 static void microstructure(const char *filename)
1018 {
1019  int j;
1020  int radical;
1021  double max;
1022  double xMax;
1023  double yMax;
1024  double zMax;
1025  std::vector<double> properties;
1026  if(filename) {
1027  std::ifstream file(filename);
1028  if(!file.is_open()) {
1029  Msg::Error("Could not open file '%s'", filename);
1030  return;
1031  }
1032  file >> max;
1033  file >> radical;
1034  file >> xMax;
1035  file >> yMax;
1036  file >> zMax;
1037  properties.clear();
1038  properties.resize(4 * max);
1039  for(j = 0; j < max; j++) {
1040  file >> properties[4 * j];
1041  file >> properties[4 * j + 1];
1042  file >> properties[4 * j + 2];
1043  file >> properties[4 * j + 3];
1044  }
1045  voroMetal3D vm1;
1046  vm1.execute(properties, radical, 0.1, xMax, yMax, zMax);
1047  GModel::current()->load("MicrostructurePolycrystal3D.geo");
1048  voroMetal3D vm2;
1049  vm2.correspondence(0.00001, xMax, yMax, zMax);
1050  }
1051 }
1052 
1053 static void computeBestSeeds(const char *filename)
1054 {
1055  int j;
1056  int radical;
1057  double max;
1058  double xMax;
1059  double yMax;
1060  double zMax;
1061  std::vector<double> properties;
1062  std::cout << "entree dans computeBestSeeds" << std::endl;
1063  if(filename) {
1064  std::ifstream file(filename);
1065  if(!file.is_open()) {
1066  Msg::Error("Could not open file '%s'", filename);
1067  return;
1068  }
1069  file >> max;
1070  file >> radical;
1071  file >> xMax;
1072  file >> yMax;
1073  file >> zMax;
1074  properties.clear();
1075  properties.resize(4 * max);
1076  for(j = 0; j < max; j++) {
1077  file >> properties[4 * j];
1078  file >> properties[4 * j + 1];
1079  file >> properties[4 * j + 2];
1080  file >> properties[4 * j + 3];
1081  }
1082  std::cout << "Before count" << std::endl;
1083  std::vector<double> listDistances;
1084  listDistances.clear();
1085  int nbOfCount = 17;
1086  listDistances.resize(nbOfCount);
1087  for(int Count = 0; Count < nbOfCount; Count++) {
1088  std::cout << "Count" << Count << std::endl;
1089  double distMinGlobal = 0.0;
1090  int jMinGlobal = 0;
1091  int xORyORz = 0;
1092  int posORneg = 0;
1093  for(j = 0; j < max; j++) {
1094  std::cout << "j " << j << std::endl;
1095  std::vector<double> propertiesModified;
1096  propertiesModified.clear();
1097  propertiesModified.resize(4 * max);
1098  std::cout << "before assign propModif" << std::endl;
1099  for(std::size_t k = 0; k < properties.size(); k++) {
1100  propertiesModified[k] = properties[k];
1101  }
1102  std::cout << "after assign propModif" << std::endl;
1103  propertiesModified[4 * j] += 0.01;
1104  voroMetal3D vm1;
1105  std::cout << "before execute" << std::endl;
1106  // std::remove("MicrostructurePolycrystal3D.geo");
1107  vm1.execute(propertiesModified, radical, 0.1, xMax, yMax, zMax);
1108  // GModel::current()->destroy();
1109  GModel *m = new GModel();
1110  // GModel::current()->load("MicrostructurePolycrystal3D.geo");
1111  m->load("MicrostructurePolycrystal3D.geo");
1112  double distMinTmp = 1000.0;
1113  // GModel *m = GModel::current();
1114  for(auto ite = m->firstEdge(); ite != m->lastEdge(); ite++) {
1115  GEdge *eTmp = (*ite);
1116  GVertex *vTmp1 = eTmp->getBeginVertex();
1117  GVertex *vTmp2 = eTmp->getEndVertex();
1118  double distTmp =
1119  sqrt((vTmp1->x() - vTmp2->x()) * (vTmp1->x() - vTmp2->x()) +
1120  (vTmp1->y() - vTmp2->y()) * (vTmp1->y() - vTmp2->y()) +
1121  (vTmp1->z() - vTmp2->z()) * (vTmp1->z() - vTmp2->z()));
1122  if(distTmp < distMinTmp) { distMinTmp = distTmp; }
1123  }
1124  if(distMinTmp > distMinGlobal) {
1125  distMinGlobal = distMinTmp;
1126  jMinGlobal = j;
1127  xORyORz = 1;
1128  posORneg = 1;
1129  }
1130  delete m;
1131  }
1132  for(j = 0; j < max; j++) {
1133  std::cout << "j " << j << std::endl;
1134  std::vector<double> propertiesModified;
1135  propertiesModified.clear();
1136  propertiesModified.resize(4 * max);
1137  std::cout << "before assign propModif" << std::endl;
1138  for(std::size_t k = 0; k < properties.size(); k++) {
1139  propertiesModified[k] = properties[k];
1140  }
1141  std::cout << "after assign propModif" << std::endl;
1142  propertiesModified[4 * j + 1] += 0.01;
1143  voroMetal3D vm1;
1144  std::cout << "before execute" << std::endl;
1145  // std::remove("MicrostructurePolycrystal3D.geo");
1146  vm1.execute(propertiesModified, radical, 0.1, xMax, yMax, zMax);
1147  // GModel::current()->destroy();
1148  GModel *m = new GModel();
1149  // GModel::current()->load("MicrostructurePolycrystal3D.geo");
1150  m->load("MicrostructurePolycrystal3D.geo");
1151  double distMinTmp = 1000.0;
1152  // GModel *m = GModel::current();
1153  for(auto ite = m->firstEdge(); ite != m->lastEdge(); ite++) {
1154  GEdge *eTmp = (*ite);
1155  GVertex *vTmp1 = eTmp->getBeginVertex();
1156  GVertex *vTmp2 = eTmp->getEndVertex();
1157  double distTmp =
1158  sqrt((vTmp1->x() - vTmp2->x()) * (vTmp1->x() - vTmp2->x()) +
1159  (vTmp1->y() - vTmp2->y()) * (vTmp1->y() - vTmp2->y()) +
1160  (vTmp1->z() - vTmp2->z()) * (vTmp1->z() - vTmp2->z()));
1161  if(distTmp < distMinTmp) { distMinTmp = distTmp; }
1162  }
1163  if(distMinTmp > distMinGlobal) {
1164  distMinGlobal = distMinTmp;
1165  jMinGlobal = j;
1166  xORyORz = 2;
1167  posORneg = 1;
1168  }
1169  delete m;
1170  }
1171  for(j = 0; j < max; j++) {
1172  std::cout << "j " << j << std::endl;
1173  std::vector<double> propertiesModified;
1174  propertiesModified.clear();
1175  propertiesModified.resize(4 * max);
1176  std::cout << "before assign propModif" << std::endl;
1177  for(std::size_t k = 0; k < properties.size(); k++) {
1178  propertiesModified[k] = properties[k];
1179  }
1180  std::cout << "after assign propModif" << std::endl;
1181  propertiesModified[4 * j + 2] += 0.01;
1182  voroMetal3D vm1;
1183  std::cout << "before execute" << std::endl;
1184  // std::remove("MicrostructurePolycrystal3D.geo");
1185  vm1.execute(propertiesModified, radical, 0.1, xMax, yMax, zMax);
1186  // GModel::current()->destroy();
1187  GModel *m = new GModel();
1188  // GModel::current()->load("MicrostructurePolycrystal3D.geo");
1189  m->load("MicrostructurePolycrystal3D.geo");
1190  double distMinTmp = 1000.0;
1191  // GModel *m = GModel::current();
1192  for(auto ite = m->firstEdge(); ite != m->lastEdge(); ite++) {
1193  GEdge *eTmp = (*ite);
1194  GVertex *vTmp1 = eTmp->getBeginVertex();
1195  GVertex *vTmp2 = eTmp->getEndVertex();
1196  double distTmp =
1197  sqrt((vTmp1->x() - vTmp2->x()) * (vTmp1->x() - vTmp2->x()) +
1198  (vTmp1->y() - vTmp2->y()) * (vTmp1->y() - vTmp2->y()) +
1199  (vTmp1->z() - vTmp2->z()) * (vTmp1->z() - vTmp2->z()));
1200  if(distTmp < distMinTmp) { distMinTmp = distTmp; }
1201  }
1202  if(distMinTmp > distMinGlobal) {
1203  distMinGlobal = distMinTmp;
1204  jMinGlobal = j;
1205  xORyORz = 3;
1206  posORneg = 1;
1207  }
1208  delete m;
1209  }
1210  for(j = 0; j < max; j++) {
1211  std::cout << "j " << j << std::endl;
1212  std::vector<double> propertiesModified;
1213  propertiesModified.clear();
1214  propertiesModified.resize(4 * max);
1215  std::cout << "before assign propModif" << std::endl;
1216  for(std::size_t k = 0; k < properties.size(); k++) {
1217  propertiesModified[k] = properties[k];
1218  }
1219  std::cout << "after assign propModif" << std::endl;
1220  propertiesModified[4 * j] -= 0.01;
1221  voroMetal3D vm1;
1222  std::cout << "before execute" << std::endl;
1223  // std::remove("MicrostructurePolycrystal3D.geo");
1224  vm1.execute(propertiesModified, radical, 0.1, xMax, yMax, zMax);
1225  // GModel::current()->destroy();
1226  GModel *m = new GModel();
1227  // GModel::current()->load("MicrostructurePolycrystal3D.geo");
1228  m->load("MicrostructurePolycrystal3D.geo");
1229  double distMinTmp = 1000.0;
1230  // GModel *m = GModel::current();
1231  for(auto ite = m->firstEdge(); ite != m->lastEdge(); ite++) {
1232  GEdge *eTmp = (*ite);
1233  GVertex *vTmp1 = eTmp->getBeginVertex();
1234  GVertex *vTmp2 = eTmp->getEndVertex();
1235  double distTmp =
1236  sqrt((vTmp1->x() - vTmp2->x()) * (vTmp1->x() - vTmp2->x()) +
1237  (vTmp1->y() - vTmp2->y()) * (vTmp1->y() - vTmp2->y()) +
1238  (vTmp1->z() - vTmp2->z()) * (vTmp1->z() - vTmp2->z()));
1239  if(distTmp < distMinTmp) { distMinTmp = distTmp; }
1240  }
1241  if(distMinTmp > distMinGlobal) {
1242  distMinGlobal = distMinTmp;
1243  jMinGlobal = j;
1244  xORyORz = 1;
1245  posORneg = 2;
1246  }
1247  delete m;
1248  }
1249  for(j = 0; j < max; j++) {
1250  std::cout << "j " << j << std::endl;
1251  std::vector<double> propertiesModified;
1252  propertiesModified.clear();
1253  propertiesModified.resize(4 * max);
1254  std::cout << "before assign propModif" << std::endl;
1255  for(std::size_t k = 0; k < properties.size(); k++) {
1256  propertiesModified[k] = properties[k];
1257  }
1258  std::cout << "after assign propModif" << std::endl;
1259  propertiesModified[4 * j + 1] -= 0.01;
1260  voroMetal3D vm1;
1261  std::cout << "before execute" << std::endl;
1262  // std::remove("MicrostructurePolycrystal3D.geo");
1263  vm1.execute(propertiesModified, radical, 0.1, xMax, yMax, zMax);
1264  // GModel::current()->destroy();
1265  GModel *m = new GModel();
1266  // GModel::current()->load("MicrostructurePolycrystal3D.geo");
1267  m->load("MicrostructurePolycrystal3D.geo");
1268  double distMinTmp = 1000.0;
1269  // GModel *m = GModel::current();
1270  for(auto ite = m->firstEdge(); ite != m->lastEdge(); ite++) {
1271  GEdge *eTmp = (*ite);
1272  GVertex *vTmp1 = eTmp->getBeginVertex();
1273  GVertex *vTmp2 = eTmp->getEndVertex();
1274  double distTmp =
1275  sqrt((vTmp1->x() - vTmp2->x()) * (vTmp1->x() - vTmp2->x()) +
1276  (vTmp1->y() - vTmp2->y()) * (vTmp1->y() - vTmp2->y()) +
1277  (vTmp1->z() - vTmp2->z()) * (vTmp1->z() - vTmp2->z()));
1278  if(distTmp < distMinTmp) { distMinTmp = distTmp; }
1279  }
1280  if(distMinTmp > distMinGlobal) {
1281  distMinGlobal = distMinTmp;
1282  jMinGlobal = j;
1283  xORyORz = 2;
1284  posORneg = 2;
1285  }
1286  delete m;
1287  }
1288  for(j = 0; j < max; j++) {
1289  std::cout << "j " << j << std::endl;
1290  std::vector<double> propertiesModified;
1291  propertiesModified.clear();
1292  propertiesModified.resize(4 * max);
1293  std::cout << "before assign propModif" << std::endl;
1294  for(std::size_t k = 0; k < properties.size(); k++) {
1295  propertiesModified[k] = properties[k];
1296  }
1297  std::cout << "after assign propModif" << std::endl;
1298  propertiesModified[4 * j + 2] -= 0.01;
1299  voroMetal3D vm1;
1300  std::cout << "before execute" << std::endl;
1301  // std::remove("MicrostructurePolycrystal3D.geo");
1302  vm1.execute(propertiesModified, radical, 0.1, xMax, yMax, zMax);
1303  // GModel::current()->destroy();
1304  GModel *m = new GModel();
1305  // GModel::current()->load("MicrostructurePolycrystal3D.geo");
1306  m->load("MicrostructurePolycrystal3D.geo");
1307  double distMinTmp = 1000.0;
1308  // GModel *m = GModel::current();
1309  for(auto ite = m->firstEdge(); ite != m->lastEdge(); ite++) {
1310  GEdge *eTmp = (*ite);
1311  GVertex *vTmp1 = eTmp->getBeginVertex();
1312  GVertex *vTmp2 = eTmp->getEndVertex();
1313  double distTmp =
1314  sqrt((vTmp1->x() - vTmp2->x()) * (vTmp1->x() - vTmp2->x()) +
1315  (vTmp1->y() - vTmp2->y()) * (vTmp1->y() - vTmp2->y()) +
1316  (vTmp1->z() - vTmp2->z()) * (vTmp1->z() - vTmp2->z()));
1317  if(distTmp < distMinTmp) { distMinTmp = distTmp; }
1318  }
1319  if(distMinTmp > distMinGlobal) {
1320  distMinGlobal = distMinTmp;
1321  jMinGlobal = j;
1322  xORyORz = 3;
1323  posORneg = 2;
1324  }
1325  delete m;
1326  }
1327  std::cout << "distance minimale de " << distMinGlobal << std::endl;
1328  listDistances[Count] = distMinGlobal;
1329  if(xORyORz == 1) {
1330  if(posORneg == 1) { properties[4 * jMinGlobal] += 0.01; }
1331  else if(posORneg == 2) {
1332  properties[4 * jMinGlobal] -= 0.01;
1333  }
1334  }
1335  else if(xORyORz == 2) {
1336  if(posORneg == 1) { properties[4 * jMinGlobal + 1] += 0.01; }
1337  else if(posORneg == 2) {
1338  properties[4 * jMinGlobal + 1] -= 0.01;
1339  }
1340  }
1341  else if(xORyORz == 3) {
1342  if(posORneg == 1) { properties[4 * jMinGlobal + 2] += 0.01; }
1343  else if(posORneg == 2) {
1344  properties[4 * jMinGlobal + 2] -= 0.01;
1345  }
1346  }
1347  }
1348  voroMetal3D vm1;
1349  vm1.execute(properties, radical, 0.1, xMax, yMax, zMax);
1350  GModel::current()->load("MicrostructurePolycrystal3D.geo");
1351  voroMetal3D vm2;
1352  vm2.correspondence(0.00001, xMax, yMax, zMax);
1353  for(std::size_t iTmp = 0; iTmp < listDistances.size(); iTmp++) {
1354  std::cout << "distMinGlobal " << iTmp << " egale a "
1355  << listDistances[iTmp] << std::endl;
1356  }
1357  std::cout << "liste des nouveaux seeds :" << std::endl;
1358  for(std::size_t iTmp = 0; iTmp < max; iTmp++) {
1359  std::cout << properties[4 * iTmp] << " " << properties[4 * iTmp + 1]
1360  << " " << properties[4 * iTmp + 2] << " "
1361  << properties[4 * iTmp + 3] << std::endl;
1362  }
1363  }
1364 }
1365 
1367 {
1368  int runBestSeeds = (int)VoroMetalOptions_Number[0].def;
1369  int runMicrostructure = (int)VoroMetalOptions_Number[1].def;
1370  std::string seedsFile = VoroMetalOptions_String[0].def;
1371  if(runBestSeeds) computeBestSeeds(seedsFile.c_str());
1372  if(runMicrostructure) microstructure(seedsFile.c_str());
1373  return v;
1374 }
1375 
1376 #else
1377 
1379 {
1380  Msg::Error("Plugin(VoroMetal) requires mesh module and voro++");
1381  return v;
1382 }
1383 
1384 #endif
StringXString
Definition: Options.h:910
GModel::fiter
std::set< GFace *, GEntityPtrLessThan >::iterator fiter
Definition: GModel.h:184
GModel::firstEdge
eiter firstEdge()
Definition: GModel.h:356
GFace::edges
virtual std::vector< GEdge * > const & edges() const
Definition: GFace.h:121
PView
Definition: PView.h:27
voroMetal3D::print_geo_physical_face
void print_geo_physical_face(int, int, std::ofstream &)
geo_cell::lines
std::vector< std::pair< int, int > > lines
Definition: VoroMetal.h:14
GVertex::z
virtual double z() const =0
GFace::edgeOrientations
virtual std::vector< int > const & edgeOrientations() const
Definition: GFace.h:139
geo_cell::line_loops
std::vector< std::vector< int > > line_loops
Definition: VoroMetal.h:15
GFace
Definition: GFace.h:33
GMSH_Plugin
Definition: Plugin.h:26
geo_cell::search_line
int search_line(std::pair< int, int > line)
Definition: VoroMetal.h:24
geo_cell::lines2
std::vector< int > lines2
Definition: VoroMetal.h:18
delta
int delta(int i, int j)
Definition: terms.h:32
GModel::load
void load(const std::string &fileName)
Definition: GModel.cpp:3424
MVertex
Definition: MVertex.h:24
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
GMSH_VoroMetalPlugin::getOptionStr
StringXString * getOptionStr(int iopt)
Definition: VoroMetal.cpp:54
GMSH_VoroMetalPlugin
Definition: VoroMetal.h:74
SPoint3
Definition: SPoint3.h:14
StringXNumber
Definition: Options.h:918
GMSH_VoroMetalPlugin::getNbOptionsStr
int getNbOptionsStr() const
Definition: VoroMetal.cpp:44
SVector3
Definition: SVector3.h:16
GRegion::getNumMeshElements
std::size_t getNumMeshElements() const
Definition: GRegion.cpp:60
GmshMessage.h
geo_cell::orientations
std::vector< std::vector< int > > orientations
Definition: VoroMetal.h:16
voroMetal3D::correspondence
void correspondence(double, double, double, double)
GFace::vertices
virtual std::vector< GVertex * > vertices() const
Definition: GFace.cpp:390
voroMetal3D::print_geo_physical_volume
void print_geo_physical_volume(int, int, std::ofstream &)
SPoint3::x
double x(void) const
Definition: SPoint3.h:125
GModel::lastFace
fiter lastFace()
Definition: GModel.h:359
voroMetal3D::print_geo_point
void print_geo_point(int, double, double, double, std::ofstream &)
VoroMetal.h
GMSH_RegisterVoroMetalPlugin
GMSH_Plugin * GMSH_RegisterVoroMetalPlugin()
Definition: VoroMetal.cpp:27
GVertex
Definition: GVertex.h:23
voroMetal3D::execute
void execute(double)
GMSH_VoroMetalPlugin::getNbOptions
int getNbOptions() const
Definition: VoroMetal.cpp:39
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
GModel
Definition: GModel.h:44
geo_cell::line_loops2
std::vector< int > line_loops2
Definition: VoroMetal.h:19
GMSH_VoroMetalPlugin::execute
PView * execute(PView *)
Definition: VoroMetal.cpp:1378
voroMetal3D
Definition: VoroMetal.h:37
GEdge::getBeginVertex
virtual GVertex * getBeginVertex() const
Definition: GEdge.h:63
VoroMetalOptions_Number
StringXNumber VoroMetalOptions_Number[]
Definition: VoroMetal.cpp:18
GModel::firstFace
fiter firstFace()
Definition: GModel.h:355
SPoint3::y
double y(void) const
Definition: SPoint3.h:127
StringXString::def
std::string def
Definition: Options.h:914
GMSH_VoroMetalPlugin::getOption
StringXNumber * getOption(int iopt)
Definition: VoroMetal.cpp:49
MElement
Definition: MElement.h:30
GEntity::tag
int tag() const
Definition: GEntity.h:280
geo_cell::faces2
std::vector< int > faces2
Definition: VoroMetal.h:20
SVector3::x
double x(void) const
Definition: SVector3.h:30
element
Definition: shapeFunctions.h:12
GRegion
Definition: GRegion.h:28
GFace::numRegions
std::size_t numRegions() const
Definition: GFace.h:87
GVertex::x
virtual double x() const =0
voroMetal3D::print_geo_volume
void print_geo_volume(int, int, std::ofstream &)
GModel::firstRegion
riter firstRegion()
Definition: GModel.h:354
GVertex::y
virtual double y() const =0
GModel::lastRegion
riter lastRegion()
Definition: GModel.h:358
SVector3::y
double y(void) const
Definition: SVector3.h:31
z
const double z
Definition: GaussQuadratureQuad.cpp:56
voroMetal3D::increase_counter
void increase_counter()
geo_cell
Definition: VoroMetal.h:12
MElement.h
voroMetal3D::get_counter
int get_counter()
voroMetal3D::print_segment
void print_segment(SPoint3, SPoint3, std::ofstream &)
GEdge
Definition: GEdge.h:26
SVector3::z
double z(void) const
Definition: SVector3.h:32
GModel::riter
std::set< GRegion *, GEntityPtrLessThan >::iterator riter
Definition: GModel.h:183
SPoint3::z
double z(void) const
Definition: SPoint3.h:129
meshGRegion.h
GMSH_VoroMetalPlugin::getHelp
std::string getHelp() const
Definition: VoroMetal.cpp:33
GModel::lastEdge
eiter lastEdge()
Definition: GModel.h:360
geo_cell::face_loops2
int face_loops2
Definition: VoroMetal.h:21
GModel.h
GEdge::getEndVertex
virtual GVertex * getEndVertex() const
Definition: GEdge.h:64
voroMetal3D::print_geo_line_loop
void print_geo_line_loop(int, std::vector< int > &, std::vector< int > &, std::ofstream &)
voroMetal3D::print_geo_face_loop
void print_geo_face_loop(int, std::vector< int > &, std::ofstream &)
voroMetal3D::print_geo_face
void print_geo_face(int, int, std::ofstream &)
voroMetal3D::print_geo_line
void print_geo_line(int, int, int, std::ofstream &)
voroMetal3D::equal
bool equal(double, double, double)
geo_cell::points2
std::vector< int > points2
Definition: VoroMetal.h:17
GRegion::getMeshElement
MElement * getMeshElement(std::size_t index) const
Definition: GRegion.cpp:127
GModel::current
static GModel * current(int index=-1)
Definition: GModel.cpp:136
voroMetal3D::initialize_counter
void initialize_counter()
VoroMetalOptions_String
StringXString VoroMetalOptions_String[]
Definition: VoroMetal.cpp:22
faces
static int faces[4][3]
Definition: meshGRegionDelaunayInsertion.cpp:165