gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
GModelIO_P3D.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 "OS.h"
8 #include "MQuadrangle.h"
9 #include "MHexahedron.h"
10 #include "discreteFace.h"
11 #include "discreteRegion.h"
12 
13 int GModel::readP3D(const std::string &name)
14 {
15  FILE *fp = Fopen(name.c_str(), "r");
16  if(!fp) {
17  Msg::Error("Unable to open file '%s'", name.c_str());
18  return 0;
19  }
20 
21  int numBlocks = 0;
22  if(fscanf(fp, "%d", &numBlocks) != 1 || numBlocks <= 0) {
23  fclose(fp);
24  return 0;
25  }
26 
27  std::vector<int> Ni(numBlocks), Nj(numBlocks), Nk(numBlocks);
28  for(int n = 0; n < numBlocks; n++)
29  if(fscanf(fp, "%d %d %d", &Ni[n], &Nj[n], &Nk[n]) != 3) {
30  fclose(fp);
31  return 0;
32  }
33 
34  for(int n = 0; n < numBlocks; n++) {
35  if(Nk[n] == 1) {
36  GFace *gf = new discreteFace(this, getMaxElementaryNumber(2) + 1);
37  add(gf);
38  gf->transfinite_vertices.resize(Ni[n]);
39  for(int i = 0; i < Ni[n]; i++) gf->transfinite_vertices[i].resize(Nj[n]);
40  for(int coord = 0; coord < 3; coord++) {
41  for(int j = 0; j < Nj[n]; j++) {
42  for(int i = 0; i < Ni[n]; i++) {
43  double d;
44  if(fscanf(fp, "%lf", &d) != 1) {
45  fclose(fp);
46  return 0;
47  }
48  if(coord == 0) {
49  MVertex *v = new MVertex(d, 0., 0., gf);
50  gf->transfinite_vertices[i][j] = v;
51  gf->mesh_vertices.push_back(v);
52  }
53  else if(coord == 1) {
54  gf->transfinite_vertices[i][j]->y() = d;
55  }
56  else if(coord == 2) {
57  gf->transfinite_vertices[i][j]->z() = d;
58  }
59  }
60  }
61  }
62  for(std::size_t i = 0; i < gf->transfinite_vertices.size() - 1; i++)
63  for(std::size_t j = 0; j < gf->transfinite_vertices[0].size() - 1; j++)
64  gf->quadrangles.push_back(new MQuadrangle(
65  gf->transfinite_vertices[i][j], gf->transfinite_vertices[i + 1][j],
66  gf->transfinite_vertices[i + 1][j + 1],
67  gf->transfinite_vertices[i][j + 1]));
68  }
69  else {
70  GRegion *gr = new discreteRegion(this, getMaxElementaryNumber(3) + 1);
71  add(gr);
72  gr->transfinite_vertices.resize(Ni[n]);
73  for(int i = 0; i < Ni[n]; i++) {
74  gr->transfinite_vertices[i].resize(Nj[n]);
75  for(int j = 0; j < Nj[n]; j++) {
76  gr->transfinite_vertices[i][j].resize(Nk[n]);
77  }
78  }
79  for(int coord = 0; coord < 3; coord++) {
80  for(int k = 0; k < Nk[n]; k++) {
81  for(int j = 0; j < Nj[n]; j++) {
82  for(int i = 0; i < Ni[n]; i++) {
83  double d;
84  if(fscanf(fp, "%lf", &d) != 1) {
85  fclose(fp);
86  return 0;
87  }
88  if(coord == 0) {
89  MVertex *v = new MVertex(d, 0., 0., gr);
90  gr->transfinite_vertices[i][j][k] = v;
91  gr->mesh_vertices.push_back(v);
92  }
93  else if(coord == 1) {
94  gr->transfinite_vertices[i][j][k]->y() = d;
95  }
96  else if(coord == 2) {
97  gr->transfinite_vertices[i][j][k]->z() = d;
98  }
99  }
100  }
101  }
102  }
103  for(std::size_t i = 0; i < gr->transfinite_vertices.size() - 1; i++)
104  for(std::size_t j = 0; j < gr->transfinite_vertices[0].size() - 1; j++)
105  for(std::size_t k = 0; k < gr->transfinite_vertices[0][0].size() - 1;
106  k++)
107  gr->hexahedra.push_back(
108  new MHexahedron(gr->transfinite_vertices[i][j][k],
109  gr->transfinite_vertices[i + 1][j][k],
110  gr->transfinite_vertices[i + 1][j + 1][k],
111  gr->transfinite_vertices[i][j + 1][k],
112  gr->transfinite_vertices[i][j][k + 1],
113  gr->transfinite_vertices[i + 1][j][k + 1],
114  gr->transfinite_vertices[i + 1][j + 1][k + 1],
115  gr->transfinite_vertices[i][j + 1][k + 1]));
116  }
117  }
118 
119  fclose(fp);
120  return 1;
121 }
122 
123 int GModel::writeP3D(const std::string &name, bool saveAll,
124  double scalingFactor)
125 {
126  FILE *fp = Fopen(name.c_str(), "w");
127  if(!fp) {
128  Msg::Error("Unable to open file '%s'", name.c_str());
129  return 0;
130  }
131 
132  if(noPhysicalGroups()) saveAll = true;
133 
134  std::vector<GFace *> faces;
135  for(auto it = firstFace(); it != lastFace(); ++it)
136  if((*it)->transfinite_vertices.size() &&
137  (*it)->transfinite_vertices[0].size() &&
138  ((*it)->physicals.size() || saveAll))
139  faces.push_back(*it);
140 
141  std::vector<GRegion *> regions;
142  for(auto it = firstRegion(); it != lastRegion(); ++it)
143  if((*it)->transfinite_vertices.size() &&
144  (*it)->transfinite_vertices[0].size() &&
145  (*it)->transfinite_vertices[0][0].size() &&
146  ((*it)->physicals.size() || saveAll))
147  regions.push_back(*it);
148 
149  if(faces.empty() && regions.empty()) {
150  Msg::Warning("No structured grids to save");
151  fclose(fp);
152  return 0;
153  }
154 
155  fprintf(fp, "%d\n", (int)(faces.size() + regions.size()));
156 
157  for(std::size_t i = 0; i < faces.size(); i++)
158  fprintf(fp, "%d %d 1\n", (int)faces[i]->transfinite_vertices.size(),
159  (int)faces[i]->transfinite_vertices[0].size());
160 
161  for(std::size_t i = 0; i < regions.size(); i++)
162  fprintf(fp, "%d %d %d\n", (int)regions[i]->transfinite_vertices.size(),
163  (int)regions[i]->transfinite_vertices[0].size(),
164  (int)regions[i]->transfinite_vertices[0][0].size());
165 
166  for(std::size_t i = 0; i < faces.size(); i++) {
167  GFace *gf = faces[i];
168  for(int coord = 0; coord < 3; coord++) {
169  for(std::size_t k = 0; k < gf->transfinite_vertices[0].size(); k++) {
170  for(std::size_t j = 0; j < gf->transfinite_vertices.size(); j++) {
171  MVertex *v = gf->transfinite_vertices[j][k];
172  double d = (coord == 0) ? v->x() : (coord == 1) ? v->y() : v->z();
173  fprintf(fp, "%.16g ", d * scalingFactor);
174  }
175  fprintf(fp, "\n");
176  }
177  }
178  }
179 
180  for(std::size_t i = 0; i < regions.size(); i++) {
181  GRegion *gr = regions[i];
182  for(int coord = 0; coord < 3; coord++) {
183  for(std::size_t l = 0; l < gr->transfinite_vertices[0][0].size(); l++) {
184  for(std::size_t k = 0; k < gr->transfinite_vertices[0].size(); k++) {
185  for(std::size_t j = 0; j < gr->transfinite_vertices.size(); j++) {
186  MVertex *v = gr->transfinite_vertices[j][k][l];
187  double d = (coord == 0) ? v->x() : (coord == 1) ? v->y() : v->z();
188  fprintf(fp, "%.16g ", d * scalingFactor);
189  }
190  fprintf(fp, "\n");
191  }
192  }
193  }
194  }
195 
196  fclose(fp);
197  return 1;
198 }
GModel::getMaxElementaryNumber
int getMaxElementaryNumber(int dim)
Definition: GModel.cpp:817
GFace
Definition: GFace.h:33
discreteRegion.h
OS.h
GModel::writeP3D
int writeP3D(const std::string &name, bool saveAll=false, double scalingFactor=1.0)
Definition: GModelIO_P3D.cpp:123
MVertex
Definition: MVertex.h:24
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
MVertex::z
double z() const
Definition: MVertex.h:62
GRegion::transfinite_vertices
std::vector< std::vector< std::vector< MVertex * > > > transfinite_vertices
Definition: GRegion.h:161
GFace::quadrangles
std::vector< MQuadrangle * > quadrangles
Definition: GFace.h:429
discreteRegion
Definition: discreteRegion.h:13
Fopen
FILE * Fopen(const char *f, const char *mode)
Definition: OS.cpp:273
GRegion::hexahedra
std::vector< MHexahedron * > hexahedra
Definition: GRegion.h:164
GModel::lastFace
fiter lastFace()
Definition: GModel.h:359
GEntity::mesh_vertices
std::vector< MVertex * > mesh_vertices
Definition: GEntity.h:56
MHexahedron.h
GModel::readP3D
int readP3D(const std::string &name)
Definition: GModelIO_P3D.cpp:13
GModel::firstFace
fiter firstFace()
Definition: GModel.h:355
MHexahedron
Definition: MHexahedron.h:28
GModel::add
bool add(GRegion *r)
Definition: GModel.h:394
discreteFace.h
GRegion
Definition: GRegion.h:28
GModel::firstRegion
riter firstRegion()
Definition: GModel.h:354
GModel::lastRegion
riter lastRegion()
Definition: GModel.h:358
MQuadrangle.h
GModel::faces
std::set< GFace *, GEntityPtrLessThan > faces
Definition: GModel.h:144
GModel::noPhysicalGroups
bool noPhysicalGroups()
Definition: GModel.cpp:828
GModel::regions
std::set< GRegion *, GEntityPtrLessThan > regions
Definition: GModel.h:143
GFace::transfinite_vertices
std::vector< std::vector< MVertex * > > transfinite_vertices
Definition: GFace.h:420
GModel.h
MVertex::y
double y() const
Definition: MVertex.h:61
MQuadrangle
Definition: MQuadrangle.h:26
MVertex::x
double x() const
Definition: MVertex.h:60
discreteFace
Definition: discreteFace.h:18