-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrefinePart.cxx
More file actions
339 lines (297 loc) · 12.5 KB
/
refinePart.cxx
File metadata and controls
339 lines (297 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2019 by Carl Ollivier-Gooch. The University of British
// Columbia disclaims all copyright interest in the software ExaMesh.//
//
// This file is part of ExaMesh.
//
// ExaMesh is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// ExaMesh is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with ExaMesh. If not, see <https://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////////
//
// Refine a mesh (a coarse mesh on a single part in a parallel mesh)
// by smooth subdivision, and write that mesh to a file.
//
//////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <locale.h>
#include <unistd.h>
#include <time.h>
#include <cstdio>
#include "ExaMesh.h"
#include "HexDivider.h"
#include "PrismDivider.h"
#include "PyrDivider.h"
#include "TetDivider.h"
#include "BdryTriDivider.h"
#include "BdryQuadDivider.h"
#include <stdio.h>
#include <inttypes.h>
#include <locale.h>
void printLocalVerts(const exa_set<TriFaceVerts> tris, const emInt nDivs) {
for (auto itr = tris.begin(); itr != tris.end(); itr++) {
std::cout << "For this tri: " << itr->getCorner(0) << " "
<< itr->getCorner(1) << " " << itr->getCorner(2) << std::endl;
for (emInt jj = 1; jj <= nDivs - 2; jj++) {
for (emInt ii = 1; ii <= nDivs - 1 - jj; ii++) {
std::cout << itr->getIntVertInd(ii, jj) << " ";
}
}
std::cout << std::endl;
}
}
emInt subdividePartMesh(const ExaMesh *const pVM_input, UMesh *const pVM_output,
const int nDivs, const emInt partID) {
assert(nDivs >= 1);
// Assumption: the mesh is already ordered in a way that seems sensible
// to the caller, both cells and vertices. As a result, we can create new
// verts and cells on the fly, with the expectation that the new ones will
// remain about as well-ordered as the old ones.
// Base of the tet is tri 012.
// Pt 0 is at indices (i,j,k) = (0,0,nDivs).
// Pt 1 is at indices (i,j,k) = (nDivs,0,nDivs).
// Pt 2 is at indices (i,j,k) = (0,nDivs,nDivs).
// Pt 3 is at indices (i,j,k) = (0,0,0).
// Other created points are laid into this framework.
// The i,j,k system isn't topologically right-handed. It's set up so that k
// corresponds to layers in the divided tet, with each layer having
// progressively more points / tris. Nevertheless, the tets produces should
// be geometrically right-handed.
// TODO: Potentially, identify in advance how many times each edge is used,
// so that when all of them have appeared, the edge can be removed from the
// map.
exa_map<Edge, EdgeVerts> vertsOnEdges;
exa_set<TriFaceVerts> vertsOnTris;
exa_set<QuadFaceVerts> vertsOnQuads;
// Copy vertex data into the new mesh.
for (emInt iV = 0; iV < pVM_input->numVertsToCopy(); iV++) {
double coords[3];
pVM_input->getCoords(iV, coords);
pVM_output->addVert(coords);
// double len = pVM_input->getLengthScale(iV);
// printf("Vert: %4d Coords: (%8f %8f %8f) Len: %8f\n", iV, coords[0],
// coords[1], coords[2], len);
}
assert(pVM_input->numVertsToCopy() == pVM_output->numVerts());
// Need to explicitly specify the type of mapping here.
TetDivider TD(pVM_output, pVM_input, nDivs);
for (emInt iT = 0; iT < pVM_input->numTets(); iT++) {
// Divide all the edges, including storing info about which new verts
// are on which edges
const emInt *const thisTet = pVM_input->getTetConn(iT);
TD.setupCoordMapping(thisTet);
TD.createDivisionVerts(vertsOnEdges, vertsOnTris, vertsOnQuads);
// And now the moment of truth: create a flock of new tets.
TD.createNewCells();
if ((iT + 1) % 100000 == 0)
fprintf(
stderr, "Refined %'12d tets. Tree sizes: %'12lu %'12lu %'12lu\r",
iT + 1, vertsOnEdges.size(), vertsOnTris.size(),
vertsOnQuads.size());
} // Done looping over all tets
#ifndef NDEBUG
fprintf(stderr, "\nDone with tets\n");
#endif
PyrDivider PD(pVM_output, pVM_input, nDivs);
for (emInt iP = 0; iP < pVM_input->numPyramids(); iP++) {
// Divide all the edges, including storing info about which new verts
// are on which edges
const emInt *const thisPyr = pVM_input->getPyrConn(iP);
PD.setupCoordMapping(thisPyr);
PD.createDivisionVerts(vertsOnEdges, vertsOnTris, vertsOnQuads);
// And now the moment of truth: create a flock of new pyramids.
PD.createNewCells();
if ((iP + 1) % 100000 == 0)
fprintf(
stderr, "Refined %'12d pyrs. Tree sizes: %'12lu %'12lu %'12lu\r",
iP + 1, vertsOnEdges.size(), vertsOnTris.size(),
vertsOnQuads.size());
} // Done looping over all pyramids
#ifndef NDEBUG
fprintf(stderr, "\nDone with pyramids\n");
#endif
PrismDivider PrismD(pVM_output, pVM_input, nDivs);
for (emInt iP = 0; iP < pVM_input->numPrisms(); iP++) {
// Divide all the edges, including storing info about which new verts
// are on which edges
const emInt *const thisPrism = pVM_input->getPrismConn(iP);
PrismD.setupCoordMapping(thisPrism);
PrismD.createDivisionVerts(vertsOnEdges, vertsOnTris, vertsOnQuads);
// And now the moment of truth: create a flock of new prisms.
PrismD.createNewCells();
if ((iP + 1) % 100000 == 0)
fprintf(
stderr, "Refined %'12d prisms. Tree sizes: %'12lu %'12lu %'12lu\r",
iP + 1, vertsOnEdges.size(), vertsOnTris.size(),
vertsOnQuads.size());
} // Done looping over all prisms
#ifndef NDEBUG
fprintf(stderr, "\nDone with prisms\n");
#endif
HexDivider HD(pVM_output, pVM_input, nDivs);
for (emInt iH = 0; iH < pVM_input->numHexes(); iH++) {
// Divide all the edges, including storing info about which new verts
// are on which edges
const emInt *const thisHex = pVM_input->getHexConn(iH);
HD.setupCoordMapping(thisHex);
HD.createDivisionVerts(vertsOnEdges, vertsOnTris, vertsOnQuads);
// And now the moment of truth: create a flock of new hexes.
HD.createNewCells();
if ((iH + 1) % 100000 == 0)
fprintf(
stderr, "Refined %'12d hexes. Tree sizes: %'12lu %'12lu %'12lu\r",
iH + 1, vertsOnEdges.size(), vertsOnTris.size(),
vertsOnQuads.size());
} // Done looping over all hexes
#ifndef NDEBUG
fprintf(stderr, "\nDone with hexes\n");
#endif
exa_set<TriFaceVerts> tris = pVM_input->getTempTriPart();
BdryTriDivider BTD(pVM_output, nDivs);
for (emInt iBT = 0; iBT < pVM_input->numBdryTris(); iBT++) {
const emInt *const thisBdryTri = pVM_input->getBdryTriConn(iBT);
TriFaceVerts TFV(nDivs, thisBdryTri[0], thisBdryTri[1], thisBdryTri[2]);
TFV.setCompare(false);
BTD.setupCoordMapping(thisBdryTri);
// Shouldn't need to divide anything at all here, but these function
// copy the vertices into the CellDivider internal data structure.
BTD.createDivisionVerts(vertsOnEdges, vertsOnTris, vertsOnQuads);
BTD.createNewCells();
if ((iBT + 1) % 100000 == 0)
fprintf(
stderr,
"Refined %'12d bdry tris. Tree sizes: %'12lu %'12lu %'12lu\r",
iBT + 1, vertsOnEdges.size(), vertsOnTris.size(),
vertsOnQuads.size());
//BTD.getRefinedVerts(pVM_input);
auto it = tris.find(TFV);
if (it != tris.end()) {
TFV.setPartID(it->getPartid());
TFV.setRemotePartID(it->getRemoteId());
emInt remoteIndices[3] = { it->getRemoteIndices(0),
it->getRemoteIndices(1), it->getRemoteIndices(2) };
emInt global[3] = { it->getGlobalCorner(0), it->getGlobalCorner(1),
it->getGlobalCorner(2)
};
emInt local[3] = { thisBdryTri[0], thisBdryTri[1], thisBdryTri[2] };
TriFaceVerts TF(nDivs, local, global, remoteIndices,
it->getPartid(), it->getRemoteId());
TF.setCompare(true);
BTD.setRefinedVerts(TF);
pVM_output->addRefinedPartTritoSet(TF);
}
}
#ifndef NDEBUG
fprintf(stderr, "\nDone with bdry tris\n");
#endif
exa_set<QuadFaceVerts> quads = pVM_input->getTempQuadPart();
BdryQuadDivider BQD(pVM_output, nDivs);
for (emInt iBQ = 0; iBQ < pVM_input->numBdryQuads(); iBQ++) {
const emInt *const thisBdryQuad = pVM_input->getBdryQuadConn(iBQ);
QuadFaceVerts QFV(nDivs, thisBdryQuad[0], thisBdryQuad[1],
thisBdryQuad[2], thisBdryQuad[3]);
QFV.setCompare(false);
BQD.setupCoordMapping(thisBdryQuad);
// Shouldn't need to divide anything at all here, but this function
// copies the triangle vertices into the CellDivider internal data structure.
BQD.createDivisionVerts(vertsOnEdges, vertsOnTris, vertsOnQuads);
BQD.createNewCells();
if ((iBQ + 1) % 100000 == 0)
fprintf(
stderr,
"Refined %'12d bdry quads. Tree sizes: %'12lu %'12lu %'12lu\r",
iBQ + 1, vertsOnEdges.size(), vertsOnTris.size(),
vertsOnQuads.size());
auto it = quads.find(QFV);
if (it != quads.end()) {
QFV.setPartID(it->getPartid());
QFV.setRemotePartID(it->getRemoteId());
emInt remoteIndices[4] = { it->getRemoteIndices(0),
it->getRemoteIndices(1), it->getRemoteIndices(2),
it->getRemoteIndices(3) };
emInt global[4] = { it->getGlobalCorner(0), it->getGlobalCorner(1),
it->getGlobalCorner(2), it->getGlobalCorner(3)
};
emInt local[4] = { thisBdryQuad[0], thisBdryQuad[1],
thisBdryQuad[2], thisBdryQuad[3] };
QuadFaceVerts QF(nDivs, local, global, remoteIndices,
it->getPartid(), it->getRemoteId());
QF.setCompare(true);
BQD.setRefinedVerts(QF);
pVM_output->addRefinedPartQuadtoSet(QF);
}
}
#ifndef NDEBUG
fprintf(stderr, "\nDone with bdry quads\n");
#endif
// assert(vertsOnTris.empty());
// assert(vertsOnQuads.empty());
//
#ifndef NDEBUG
fprintf(stderr, "Final size of edge list: %'lu\n", vertsOnEdges.size());
fprintf(stderr, "Final size of tri list: %'lu\n", vertsOnTris.size());
fprintf(stderr, "Final size of quad list: %'lu\n", vertsOnQuads.size());
#endif
return pVM_output->numCells();
}
bool computeMeshSize(const struct MeshSize &MSIn, const emInt nDivs,
struct MeshSize &MSOut) {
// It's relatively easy to compute some of these quantities:
const emInt surfFactor = nDivs * nDivs;
const emInt volFactor = surfFactor * nDivs;
const emInt maxFaces = EMINT_MAX / surfFactor;
const emInt maxCells = EMINT_MAX / volFactor;
if (MSIn.nBdryTris > maxFaces || MSIn.nBdryQuads > maxFaces
|| MSIn.nTets > maxCells || MSIn.nPyrs > maxCells
|| MSIn.nPrisms > maxCells || MSIn.nHexes > maxCells) {
fprintf(stderr, "Output mesh will exceed max index size!\n");
return false;
}
MSOut.nBdryTris = MSIn.nBdryTris * surfFactor;
MSOut.nBdryQuads = MSIn.nBdryQuads * surfFactor;
MSOut.nTets = MSIn.nTets * volFactor
+ MSIn.nPyrs * (volFactor - nDivs) * 2 / 3;
MSOut.nPyrs = MSIn.nPyrs * (2 * volFactor + nDivs) / 3;
MSOut.nPrisms = MSIn.nPrisms * volFactor;
MSOut.nHexes = MSIn.nHexes * volFactor;
//setlocale(LC_ALL, "");
//ssize_t totalCells = MSOut.nPyrs+MSOut.nPrisms+MSOut.nHexes+MSOut.nTets;
//fprintf(stderr, "Total expected cells: %'zd \n", totalCells);
// Use signed 64-bit ints for these calculations. It's possible someone will ask for
// something that blows out 32-bit unsigned ints, and will need to be stopped.
ssize_t inputTriCount = (MSIn.nBdryTris + MSIn.nTets * 4 + MSIn.nPyrs * 4
+ MSIn.nPrisms * 2) / 2;
ssize_t inputQuadCount = (MSIn.nBdryQuads + MSIn.nPyrs + MSIn.nPrisms * 3
+ MSIn.nHexes * 6) / 2;
ssize_t inputFaceCount = inputTriCount + inputQuadCount;
ssize_t inputCellCount = MSIn.nTets + MSIn.nPyrs + MSIn.nPrisms
+ MSIn.nHexes;
ssize_t inputBdryEdgeCount = (MSIn.nBdryTris * 3 + MSIn.nBdryQuads * 4) / 2;
// Upcast the first arg explicitly, and the rest should follow.
int inputGenus = (ssize_t(MSIn.nBdryVerts) - inputBdryEdgeCount
+ MSIn.nBdryTris + MSIn.nBdryQuads - 2) / 2;
ssize_t inputEdges = (ssize_t(MSIn.nVerts) + inputFaceCount - inputCellCount
- 1 - inputGenus);
ssize_t outputFaceVerts = inputTriCount * (nDivs - 2) * (nDivs - 1) / 2
+ inputQuadCount * (nDivs - 1) * (nDivs - 1);
ssize_t outputCellVerts = MSIn.nTets * (nDivs - 3) * (nDivs - 2)
* (nDivs - 1) / 6
+ MSIn.nPyrs * (2 * nDivs - 3) * (nDivs - 2) * (nDivs - 1) / 6
+ MSIn.nPrisms * (nDivs - 1) * (nDivs - 2) * (nDivs - 1) / 2
+ MSIn.nHexes * (nDivs - 1) * (nDivs - 1) * (nDivs - 1);
ssize_t outputEdgeVerts = inputEdges * (nDivs - 1);
ssize_t outputVerts = outputFaceVerts + outputEdgeVerts + outputCellVerts
+ MSIn.nVerts;
// ssize_t outputEdges = outputVerts + inputFaceCount * surfFactor
// - inputCellCount * volFactor - 1 - inputGenus;
MSOut.nVerts = outputVerts;
return true;
}