-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometry.cpp
435 lines (349 loc) · 16.8 KB
/
Geometry.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include "Geometry.h"
#include "Tools.h"
#include <gmsh.h>
namespace factory = gmsh::model::occ;
namespace model = gmsh::model;
Geometry::Geometry(Parameters * prm, PackedBed * pb)
{
createPackedBed(pb, prm, dt_beads);
createBridges(pb, prm, dt_bridges);
createContainer(pb, prm, dt_containers);
operate(prm);
// TODO: Move to Model
if (prm->meshSizeMethod == 1)
{
std::string backgroundField;
if (prm->lc_beads > prm->lc_out)
backgroundField="Max";
else
backgroundField="Min";
std::cout << "Setting Background Field: " << backgroundField << std::endl;
model::mesh::field::add(backgroundField, ++count);
model::mesh::field::setNumbers(count, "FieldsList", vCount);
model::mesh::field::setAsBackgroundMesh(count);
}
else if (prm->meshSizeMethod == 0)
{
//Set mesh size globally
std::cout << "Setting global mesh size... ";
model::getEntities(dt_dummy, 0);
model::mesh::setSize(dt_dummy, prm->lc_beads);
std:: cout << "done!" << std::endl;
}
}
Geometry::~Geometry()
{
}
// Create packed bed geometry. Basically all the beads.
// Store dimTags in last argument
void Geometry::createPackedBed(PackedBed * pb, Parameters * prm, std::vector<std::pair<int, int>> &dt_beads)
{
std::cout << "Creating beads... " << std::flush;
int tag, ctag;
for (std::vector<Bead *>::iterator iter = pb->beads.begin(); iter != pb->beads.end(); iter++ )
{
double x = (*iter)->getX();
double y = (*iter)->getY();
double z = (*iter)->getZ();
double r = (*iter)->getR();
if (prm->geomInfile.empty())
{
tag = factory::addSphere(x, y, z, r);
/* std::cout << tag << std::endl; */
ctag = factory::addPoint(x, y, z, prm->lc_beads, -1);
(*iter)->setTag(tag);
(*iter)->setCTag(ctag);
dt_beads.push_back({3, tag});
}
/*
* Distance/Threshold allows creating gradient mesh sizes within the beads
*/
//TODO: Update to new GMSH changes
model::mesh::field::add("Distance", ++count);
model::mesh::field::setNumbers(count, "NodesList", {double(ctag)});
model::mesh::field::add("Threshold", ++count);
model::mesh::field::setNumber(count, "IField", count-1);
model::mesh::field::setNumber(count, "LcMin", prm->lc_beads);
model::mesh::field::setNumber(count, "LcMax", prm->lc_out);
model::mesh::field::setNumber(count, "DistMin", prm->fieldThresholdMinFactor * r);
model::mesh::field::setNumber(count, "DistMax", prm->fieldThresholdMaxFactor * r);
vCount.push_back(count);
}
std::cout << "done!" << std::endl;
}
void Geometry::createBridges(PackedBed * pb, Parameters * prm, std::vector<std::pair<int, int>> &dt_bridges)
{
int tagBridge;
double dx, dy, dz;
double x4, y4, z4;
double x3, y3, z3;
double x2, y2, z2, r2;
double x1, y1, z1, r1;
double dx3, dy3, dz3;
double factor = prm->bridgeOffsetRatio;
double dist;
std::vector<Bead *> remBeads = pb->beads;
std::cout << "Creating Bridges... " << std::flush;
for(std::vector<Bead *>::iterator iter = pb->beads.begin(); iter != pb->beads.end(); iter++)
{
remBeads.erase(remBeads.begin());
for(std::vector<Bead *>::iterator riter = remBeads.begin(); riter != remBeads.end(); riter++)
{
x1 = (*iter)->getX();
x2 = (*riter)->getX();
y1 = (*iter)->getY();
y2 = (*riter)->getY();
z1 = (*iter)->getZ();
z2 = (*riter)->getZ();
r1 = (*iter)->getR();
r2 = (*riter)->getR();
dx = x2-x1;
dy = y2-y1;
dz = z2-z1;
dist = sqrt( pow(dx, 2) + pow(dy, 2) + pow(dz, 2) );
if (dist < r1 + r2 + prm->bridgeTol)
{
//cylinder radius
double rBeadSmallest = (r1 <= r2? r1 : r2);
double rBridge = prm->relativeBridgeRadius * rBeadSmallest;
double r1Bridge = prm->relativeBridgeRadius * r1;
double r2Bridge = prm->relativeBridgeRadius * r2;
//Cylinder start point
x3 = x1 + factor * r1 * dx/dist;
y3 = y1 + factor * r1 * dy/dist;
z3 = z1 + factor * r1 * dz/dist;
x4 = x2 - factor * r2 * dx/dist;
y4 = y2 - factor * r2 * dy/dist;
z4 = z2 - factor * r2 * dz/dist;
//cylinder length
dx3 = dx * (dist - factor * (r1+r2))/dist;
dy3 = dy * (dist - factor * (r1+r2))/dist;
dz3 = dz * (dist - factor * (r1+r2))/dist;
if (prm->geomInfile.empty())
{
if (r1 == r2)
tagBridge = factory::addCylinder(x3, y3, z3, dx3, dy3, dz3, rBridge);
else
tagBridge = factory::addCone(x3, y3, z3, dx3, dy3, dz3, r1Bridge, r2Bridge);
this->dt_bridges.push_back({3, tagBridge }) ;
/* this->bridgeTagRadiusPairs.push_back({tagBridge, rBridge}); */
}
model::mesh::field::add("Frustum", ++count);
model::mesh::field::setNumber(count, "V1_inner", prm->lc_bridge * rBeadSmallest/(prm->refBeadRadius) );
model::mesh::field::setNumber(count, "V2_inner", prm->lc_bridge * rBeadSmallest/(prm->refBeadRadius) );
/* model::mesh::field::setNumber(count, "V1_outer", prm->lc_beads * rBeadSmallest/(radius_max) ); */
/* model::mesh::field::setNumber(count, "V2_outer", prm->lc_beads * rBeadSmallest/(radius_max) ); */
model::mesh::field::setNumber(count, "V1_outer", prm->lc_out);
model::mesh::field::setNumber(count, "V2_outer", prm->lc_out);
model::mesh::field::setNumber(count, "R1_inner", 0);
model::mesh::field::setNumber(count, "R2_inner", 0);
model::mesh::field::setNumber(count, "R1_outer", prm->fieldThresholdMaxFactor * r1Bridge);
model::mesh::field::setNumber(count, "R2_outer", prm->fieldThresholdMaxFactor * r2Bridge);
model::mesh::field::setNumber(count, "X1", x3);
model::mesh::field::setNumber(count, "Y1", y3);
model::mesh::field::setNumber(count, "Z1", z3);
model::mesh::field::setNumber(count, "X2", x4);
model::mesh::field::setNumber(count, "Y2", y4);
model::mesh::field::setNumber(count, "Z2", z4);
vCount.push_back(count);
}
}
}
std::cout << "done!" << std::endl;
}
// Creates a container (cylinder or box) for the specified packed bed
// either automatically or using the specified "cyl" or "box" parameters
// Adds the dimtag of the new geometry to the last argument.
// Generating the additional container for the inlet or outlet periodic special case should be done outside
void Geometry::createContainer(PackedBed * pb, Parameters * prm, std::vector<std::pair<int,int>> &dt_containers)
{
if (prm->containerShape == 0)
{
if(prm->autoContainment == 1)
{
x = pb->xCyl; dx = 0;
y = pb->yCyl; dy = 0;
z = pb->zCylBot; dz = pb->zCylTop - pb->zCylBot; // includes inlet/outlet
R = pb->rCyl; // should include delta
}
else
{
// Strictly specified dimensions.
x = prm->x0; dx = prm->dx;
y = prm->y0; dy = prm->dy;
z = prm->z0; dz = prm->dz;
R = prm->rCyl;
}
std::cout << "Creating cylinder: " <<
x << " " << y << " " << z << " " <<
dx << " " << dy << " " << dz << " " <<
R << std::endl;;
dt_containers.push_back({3, factory::addCylinder(x, y, z, dx, dy, dz, R)});
// TODO: if periodic == "z"
}
else if (prm->containerShape == 1)
{
if(prm->autoContainment == 1)
{
x = pb->xMin; dx = pb->xMax - pb->xMin;
y = pb->yMin; dy = pb->yMax - pb->yMin;
z = pb->zCylBot; dz = pb->zCylTop - pb->zCylBot; // includes inlet/outlet?
// NOTE: Not acccounting for rcyldelta here, as opposed to cylindrical containers.
// TODO: Figure this out
}
else
{
// Strictly specified dimensions.
x = prm->x0; dx = prm->dx;
y = prm->y0; dy = prm->dy;
z = prm->z0; dz = prm->dz;
}
std::cout << std::setprecision(2);
std::cout << "Creating box: " <<
x << " " << y << " " << z << " " <<
dx << " " << dy << " " << dz << std::endl;
std::cout << std::setprecision(10);
dt_containers.push_back( {3, factory::addBox(x, y, z, dx, dy, dz)});
// NOTE: You can have periodicInlet and Outlet sections without ANY periodicity.
// PeriodicInlet and Outlet only necessitate periodic linking which will happen no matter what
//TODO: Rename these? Make the first ones a plane instead?
if (prm->periodicInlet > 0)
{
dt_containers.push_back({3, factory::addBox(x - dx, y - dy, z - prm->periodicInlet, 3*dx, 3*dy, prm->periodicInlet)});
dt_containers.push_back({3, factory::addBox(x, y, z - prm->periodicInlet, dx, dy, prm->periodicInlet)});
}
if (prm->periodicOutlet > 0)
{
dt_containers.push_back({3, factory::addBox(x - dx, y - dy, z+dz, 3*dx, 3*dy, prm->periodicOutlet)});
dt_containers.push_back({3, factory::addBox(x, y, z+dz, dx, dy, prm->periodicInlet)});
}
}
}
void Geometry::operate(Parameters * prm)
{
if (!prm->geomInfile.empty())
return;
long bool_start = gmsh::logger::getWallTime();
// dimTagsMap
std::vector<std::vector<std::pair<int, int> > > ovv;
std::cout << "Intersecting Volumes... " << std::endl;
if (dt_containers.size() == 1)
factory::intersect(dt_beads, dt_containers, dt_beadsInside, ovv, -1, true, false);
else
{
std::cout << " > Preparing Column Geometries... " << std::flush;;
factory::intersect(dt_beads, {dt_containers[0]}, dt_beadsInside, ovv, -1, false, false);
std::cout << "done!" << std::endl;;
//assuming both periodicInlet and periodicOutlet are given!!!
std::vector<std::pair<int,int>> dt_beadsInlet, dt_beadsOutlet, dt_dummy;
// NOTE:
// Possible fixes to this mess:
// 1. Intersect with plane, extract dimtags out of ovv
// 2. getEntities in Bounding Box after intersect? Not enough.. need to fragment cleanly.
// 3. Move to a fragment only based workflow. Fragment, Remove Outside bounding box.
// if so, I might not even have to do all this manual handling. All Containers X All beads.
// Still leaves the issue of ensuring xy periodicity in inlet-outlet
//
// Periodic Inlet and Outlet geometries are tricky to properly capture when dealing with full periodicity.
// Things to note:
// 1. We need to capture beads that are within the (in/out) volume, but not all of them
// 2. With XYZ periodicity, Z-direction stacking is necessary, filling the whole volume with unnecessary beads.
// 3. In case of XYZ periodicity, Inlet and Outlet will have XY periodicity. If so, finding the beads to keep in the
// volume becomes harder. The criterion is not just intersection of the bead with the z0 plane of the column; this
// doesn't account for XY projections of beads. Instead, we find intersection of beads with the full z0 plane, and then
// intersect again to keep them within the specified volume.
std::cout << " > Preparing Inlet Geometries... " << std::flush;;
factory::intersect(dt_beads, {dt_containers[1]}, dt_beadsInPeriodicInlet, ovv, -1, false, false);
filterIfSurfaceWithNormal(dt_beadsInPeriodicInlet, {0,0,1}, dt_beadsInlet);
factory::intersect(dt_beadsInlet, {dt_containers[2]}, dt_beadsInPeriodicInlet, ovv, -1, false, false);
std::cout << "done!" << std::endl;
std::cout << " > Preparing Outlet Geometries... " << std::flush;;
factory::intersect(dt_beads, {dt_containers[3]}, dt_beadsInPeriodicOutlet, ovv, -1, false, false);
filterIfSurfaceWithNormal(dt_beadsInPeriodicOutlet, {0,0,-1}, dt_beadsOutlet);
factory::intersect(dt_beadsOutlet, {dt_containers[4]}, dt_beadsInPeriodicOutlet, ovv, -1, false, false);
std::cout << "done!" << std::endl;;
// TODO: Do I need this both here and after fragmentation?
// TODO: Parallellize
std::cout << " > Cleaning Model... " << std::flush;;
factory::getEntities(dt_dummy);
subtractDimTags(dt_dummy, dt_beadsInside);
subtractDimTags(dt_dummy, dt_beadsInPeriodicInlet);
subtractDimTags(dt_dummy, dt_beadsInPeriodicOutlet);
subtractDimTags(dt_dummy, {dt_containers[0]});
subtractDimTags(dt_dummy, {dt_containers[2]});
subtractDimTags(dt_dummy, {dt_containers[4]});
factory::remove(dt_dummy, true);
std::cout << "done!" << std::endl;
}
// Synchronize gmsh model with geometry kernel.
std::cout << "Synchronizing... " << std::flush;
factory::synchronize();
std::cout << "done!" << std::endl;
/* Fuse beads together (with bridges or without) */
if (prm->booleanOperation == 1)
{
if (dt_bridges.size() == 0) dt_bridges = {3, dt_beads.back()};
std::cout << "Fusing Beads and Bridges... " << std::flush;
factory::fuse(dt_beadsInside, dt_bridges, dt_fused, ovv );
std::cout << "done!" << std::endl;
}
else if (prm->booleanOperation == 2)
{
if (dt_bridges.size() == 0)
{
std::cout << "Error: No Bridges to cut from beads" << std::endl;
exit(-1);
}
std::cout << "Capping Beads... " << std::flush;
/* factory::cut(dimTagsBeads, dimTagsBridges, ov, ovv ); */
factory::cut(dt_beadsInside, dt_bridges, dt_fused, ovv );
std::cout << "done!" << std::endl;
}
else
{
/* ov = dimTagsBeads; */
dt_fused = dt_beadsInside;
}
// Fragment cylinder w.r.t. beads
if (prm->fragment)
{
if (dt_fused.size() == 0) dt_fused = dt_beadsInside;
std::cout << "Fragmenting Volumes..." << std::endl;
/* factory::fragment(dimTagsContainers, dt_fused, dimTagsFragmented, ovv ); */
//TODO: don't use dimtagsfused here?
if (dt_containers.size() == 1)
factory::fragment(dt_containers, dt_fused, dt_fragmented, ovv );
else
{
std::cout << " > Fragmenting Column... " << std::flush;
factory::fragment({dt_containers[0]}, dt_fused, dt_fragmented, ovv );
std::cout << "done!" << std::endl;
//NOTE: assuming both periodicInlet and periodicOutlet are given!!!
std::cout << " > Fragmenting Inlet..." << std::flush;
factory::fragment({dt_containers[2]}, dt_beadsInPeriodicInlet, dt_fragmentedPeriodicInlet, ovv );
std::cout << "done!" << std::endl;
std::cout << " > Fragmenting Outlet..." << std::flush;
factory::fragment({dt_containers[4]}, dt_beadsInPeriodicOutlet, dt_fragmentedPeriodicOutlet, ovv );
std::cout << "done!" << std::endl;
// have to do this because I can't delete the beads directly during/after intersection as with simpler cases.
// But there might be simpler ways of achieving the same thing: I won't need intersect if I'm just fragmenting and deleting by bounding box
std::cout << " > Cleaning Model..." << std::flush;;
std::vector<std::pair<int,int>> dtdummy;
factory::getEntities(dtdummy);
subtractDimTags(dtdummy, dt_fragmented);
subtractDimTags(dtdummy, dt_fragmentedPeriodicInlet);
subtractDimTags(dtdummy, dt_fragmentedPeriodicOutlet);
factory::remove(dtdummy,true);
std::cout << "done!" << std::endl;
}
}
// Synchronize gmsh model with geometry kernel.
std::cout << "Synchronizing model... " << std::flush;
factory::synchronize();
std::cout << "done!" << std::endl;
std::cout << "Number of Geometrical Beads: " << dt_beadsInside.size() << std::endl;
std::cout << "Number of Geometrical Bridges: " << dt_bridges.size() << std::endl;
std::cout << "Number of Geometrical Internal Volumes: " << dt_fused.size() << std::endl;
long bool_duration = gmsh::logger::getWallTime() - bool_start;
gmsh::logger::write("Boolean time: " + std::to_string(bool_duration) + " s", "info");
}