-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshSimple.cc
475 lines (392 loc) · 14.9 KB
/
MeshSimple.cc
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
Copyright 2010-201x held jointly by LANL, ORNL, LBNL, and PNNL.
Amanzi is released under the three-clause BSD License.
The terms of use and "as is" disclaimer for this license are
provided in the top-level COPYRIGHT file.
Authors:
*/
#include <algorithm>
#include "errors.hh"
#include "MeshSimple.hh"
namespace Amanzi {
namespace AmanziMesh {
//---------------------------------------------------------
// Constructor
//---------------------------------------------------------
MeshSimple::MeshSimple(double x0, double y0, double z0,
double x1, double y1, double z1,
int nx, int ny, int nz)
: MeshFramework(),
nx_(nx), ny_(ny), nz_(nz),
x0_(x0), x1_(x1),
y0_(y0), y1_(y1),
z0_(z0), z1_(z1)
{
setSpaceDimension(3);
setManifoldDimension(3);
edges_requested_ = false;
CreateCache_();
}
//---------------------------------------------------------
// Update
//---------------------------------------------------------
void MeshSimple::CreateCache_()
{
// clear old cache
coordinates_.clear();
cell_to_face_.clear();
face_to_node_.clear();
edge_to_node_.clear();
// build new cache
num_cells_ = nx_ * ny_ * nz_;
num_nodes_ = (nx_ + 1) * (ny_ + 1) * (nz_ + 1);
num_faces_ = (nx_ + 1) * ny_ * nz_ + nx_ * (ny_ + 1) * nz_ + nx_ * ny_ * (nz_ + 1);
num_edges_ = nx_ * (ny_ + 1) * (nz_ + 1) + (nx_ + 1) * ny_ * (nz_ + 1) + (nx_ + 1) * (ny_ + 1) * nz_;
// -- node coordinates
coordinates_.resize(3 * num_nodes_);
double hx = (x1_ - x0_) / nx_;
double hy = (y1_ - y0_) / ny_;
double hz = (z1_ - z0_) / nz_;
for (int iz = 0; iz <= nz_; iz++) {
for (int iy = 0; iy <= ny_; iy++) {
for (int ix = 0; ix <= nx_; ix++) {
int istart = 3 * node_index_(ix, iy, iz);
coordinates_[istart] = x0_ + ix * hx;
coordinates_[istart + 1] = y0_ + iy * hy;
coordinates_[istart + 2] = z0_ + iz * hz;
}
}
}
// -- connectivity arrays
cell_to_face_.resize(6 * num_cells_);
cell_to_face_dirs_.resize(6 * num_cells_);
face_to_cell_.assign(2 * num_faces_, -1);
face_to_node_.resize(4 * num_faces_);
node_to_face_.resize(13 * num_nodes_); // 1 extra for num faces
if (edges_requested_) {
face_to_edge_.resize(4 * num_faces_);
face_to_edge_dirs_.resize(4 * num_faces_);
edge_to_node_.resize(2 * num_edges_);
}
// loop over cells and initialize cell <-> face
for (int iz = 0; iz < nz_; iz++) {
for (int iy = 0; iy < ny_; iy++) {
for (int ix = 0; ix < nx_; ix++) {
int istart = 6 * cell_index_(ix,iy,iz);
int jstart = 0;
cell_to_face_[istart] = xzface_index_(ix, iy, iz);
cell_to_face_[istart + 1] = yzface_index_(ix+1,iy, iz);
cell_to_face_[istart + 2] = xzface_index_(ix, iy+1,iz);
cell_to_face_[istart + 3] = yzface_index_(ix, iy, iz);
cell_to_face_[istart + 4] = xyface_index_(ix, iy, iz);
cell_to_face_[istart + 5] = xyface_index_(ix, iy, iz+1);
cell_to_face_dirs_[istart] = 1;
cell_to_face_dirs_[istart + 1] = 1;
cell_to_face_dirs_[istart + 2] = -1;
cell_to_face_dirs_[istart + 3] = -1;
cell_to_face_dirs_[istart + 4] = -1;
cell_to_face_dirs_[istart + 5] = 1;
jstart = 2 * xzface_index_(ix, iy, iz);
face_to_cell_[jstart + 1] = cell_index_(ix, iy, iz);
jstart = 2 * yzface_index_(ix+1, iy, iz);
face_to_cell_[jstart + 1] = cell_index_(ix, iy, iz);
jstart = 2 * xzface_index_(ix, iy+1, iz);
face_to_cell_[jstart] = cell_index_(ix, iy, iz);
jstart = 2 * yzface_index_(ix, iy, iz);
face_to_cell_[jstart] = cell_index_(ix, iy, iz);
jstart = 2 * xyface_index_(ix, iy, iz);
face_to_cell_[jstart] = cell_index_(ix, iy, iz);
jstart = 2 * xyface_index_(ix, iy, iz+1);
face_to_cell_[jstart + 1] = cell_index_(ix, iy, iz);
}
}
}
// loop over faces and initialize face <-> node
// -- xy faces
for (int iz = 0; iz <= nz_; iz++) {
for (int iy = 0; iy < ny_; iy++) {
for (int ix = 0; ix < nx_; ix++) {
int istart = 4 * xyface_index_(ix, iy, iz);
int jstart = 0;
int nfaces = 0;
face_to_node_[istart] = node_index_(ix, iy, iz);
face_to_node_[istart + 1] = node_index_(ix+1,iy, iz);
face_to_node_[istart + 2] = node_index_(ix+1,iy+1,iz);
face_to_node_[istart + 3] = node_index_(ix, iy+1,iz);
jstart = 13 * node_index_(ix,iy,iz);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix+1, iy, iz);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix+1, iy+1, iz);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix, iy+1, iz);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
}
}
}
// -- xz faces
for (int iz = 0; iz < nz_; iz++) {
for (int iy = 0; iy <= ny_; iy++) {
for (int ix=0; ix < nx_; ix++) {
int istart = 4 * xzface_index_(ix, iy, iz);
int jstart = 0;
int nfaces = 0;
face_to_node_[istart] = node_index_(ix, iy, iz);
face_to_node_[istart + 1] = node_index_(ix+1,iy, iz);
face_to_node_[istart + 2] = node_index_(ix+1,iy, iz+1);
face_to_node_[istart + 3] = node_index_(ix, iy, iz+1);
jstart = 13 * node_index_(ix, iy, iz);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix+1, iy, iz);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix+1, iy, iz+1);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix, iy, iz+1);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
}
}
}
// -- yz faces
for (int iz = 0; iz < nz_; iz++) {
for (int iy = 0; iy < ny_; iy++) {
for (int ix = 0; ix <= nx_; ix++) {
int istart = 4 * yzface_index_(ix, iy, iz);
int jstart = 0;
int nfaces = 0;
face_to_node_[istart] = node_index_(ix, iy, iz);
face_to_node_[istart + 1] = node_index_(ix, iy+1,iz);
face_to_node_[istart + 2] = node_index_(ix, iy+1,iz+1);
face_to_node_[istart + 3] = node_index_(ix, iy, iz+1);
jstart = 13 * node_index_(ix, iy, iz);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix, iy+1, iz);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix, iy+1, iz+1);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
jstart = 13 * node_index_(ix, iy, iz+1);
nfaces = node_to_face_[jstart];
node_to_face_[jstart + 1 + nfaces] = xyface_index_(ix, iy, iz);
(node_to_face_[jstart])++;
}
}
}
if (edges_requested_) {
// loop over faces and initialize face -> edge
// -- xy faces
for (int iz = 0; iz <= nz_; iz++) {
for (int iy = 0; iy < ny_; iy++) {
for (int ix = 0; ix < nx_; ix++) {
int istart = 4 * xyface_index_(ix, iy, iz);
face_to_edge_[istart] = xedge_index_(ix, iy, iz);
face_to_edge_[istart + 1] = yedge_index_(ix+1,iy, iz);
face_to_edge_[istart + 2] = xedge_index_(ix, iy+1,iz);
face_to_edge_[istart + 3] = yedge_index_(ix, iy, iz);
face_to_edge_dirs_[istart] = 1;
face_to_edge_dirs_[istart + 1] = 1;
face_to_edge_dirs_[istart + 2] = -1;
face_to_edge_dirs_[istart + 3] = -1;
}
}
}
// -- xz faces
for (int iz = 0; iz < nz_; iz++) {
for (int iy = 0; iy <= ny_; iy++) {
for (int ix=0; ix < nx_; ix++) {
int istart = 4 * xzface_index_(ix, iy, iz);
face_to_edge_[istart] = xedge_index_(ix, iy, iz);
face_to_edge_[istart + 1] = zedge_index_(ix+1,iy, iz);
face_to_edge_[istart + 2] = xedge_index_(ix, iy, iz+1);
face_to_edge_[istart + 3] = zedge_index_(ix, iy, iz);
face_to_edge_dirs_[istart] = 1;
face_to_edge_dirs_[istart + 1] = 1;
face_to_edge_dirs_[istart + 2] = -1;
face_to_edge_dirs_[istart + 3] = -1;
}
}
}
// -- yz faces
for (int iz = 0; iz < nz_; iz++) {
for (int iy = 0; iy < ny_; iy++) {
for (int ix = 0; ix <= nx_; ix++) {
int istart = 4 * yzface_index_(ix, iy, iz);
face_to_edge_[istart] = yedge_index_(ix, iy, iz);
face_to_edge_[istart + 1] = zedge_index_(ix, iy+1,iz);
face_to_edge_[istart + 2] = yedge_index_(ix, iy, iz+1);
face_to_edge_[istart + 3] = zedge_index_(ix, iy, iz);
face_to_edge_dirs_[istart] = 1;
face_to_edge_dirs_[istart + 1] = 1;
face_to_edge_dirs_[istart + 2] = -1;
face_to_edge_dirs_[istart + 3] = -1;
}
}
}
// loop over edges and initialize edge -> nodes
// -- x edges
for (int iz = 0; iz <= nz_; iz++) {
for (int iy = 0; iy <= ny_; iy++) {
for (int ix = 0; ix < nx_; ix++) {
int istart = 2 * xedge_index_(ix, iy, iz);
edge_to_node_[istart] = node_index_(ix, iy, iz);
edge_to_node_[istart + 1] = node_index_(ix+1,iy, iz);
}
}
}
// -- y edges
for (int iz = 0; iz <= nz_; iz++) {
for (int iy = 0; iy < ny_; iy++) {
for (int ix = 0; ix <= nx_; ix++) {
int istart = 2 * yedge_index_(ix, iy, iz);
edge_to_node_[istart] = node_index_(ix, iy, iz);
edge_to_node_[istart + 1] = node_index_(ix, iy+1, iz);
}
}
}
// -- z edges
for (int iz = 0; iz < nz_; iz++) {
for (int iy = 0; iy <= ny_; iy++) {
for (int ix = 0; ix <= nx_; ix++) {
int istart = 2 * zedge_index_(ix, iy, iz);
edge_to_node_[istart] = node_index_(ix, iy, iz);
edge_to_node_[istart + 1] = node_index_(ix, iy, iz+1);
}
}
}
}
}
//---------------------------------------------------------
// TBW
//---------------------------------------------------------
std::size_t MeshSimple::getNumEntities(AmanziMesh::Entity_kind kind,
AmanziMesh::Parallel_type ptype) const
{
switch (kind) {
case Entity_kind::FACE:
return (ptype != AmanziMesh::Parallel_type::GHOST) ? num_faces_ : 0;
break;
case Entity_kind::BOUNDARY_FACE:
// Same problem as the constructor, cannot be -1 for std::size_t
return 0;
break;
case Entity_kind::NODE:
return (ptype != AmanziMesh::Parallel_type::GHOST) ? num_nodes_ : 0;
break;
case Entity_kind::BOUNDARY_NODE:
return 0;
break;
case Entity_kind::CELL:
return (ptype != AmanziMesh::Parallel_type::GHOST) ? num_cells_ : 0;
break;
default:
throw std::exception();
break;
}
}
//---------------------------------------------------------
// Connectivity: cell -> faces
//---------------------------------------------------------
void MeshSimple::getCellFacesAndDirs(const AmanziMesh::Entity_ID cellid,
Entity_ID_List& faceids,
Entity_Direction_List *cfacedirs) const
{
unsigned int offset = (unsigned int) 6*cellid;
faceids.clear();
auto it = cell_to_face_.begin() + offset;
faceids.insert(faceids.end(), it, it + 6);
if (cfacedirs) {
cfacedirs->clear();
auto jt = cell_to_face_dirs_.begin() + offset;
cfacedirs->insert(cfacedirs->begin(), jt, jt + 6);
}
}
//---------------------------------------------------------
// Connectivity: face -> nodes
//---------------------------------------------------------
void MeshSimple::getFaceNodes(AmanziMesh::Entity_ID face,
AmanziMesh::Entity_ID_List& nodeids) const
{
unsigned int offset = (unsigned int) 4*face;
nodeids.clear();
for (int i = 0; i < 4; i++) {
nodeids.push_back(*(face_to_node_.begin()+offset));
offset++;
}
}
//---------------------------------------------------------
// Connectivity: face -> edges
//---------------------------------------------------------
void MeshSimple::getFaceEdgesAndDirs(const Entity_ID faceid,
Entity_ID_List& edgeids,
Entity_Direction_List *fedgedirs) const
{
unsigned int offset = (unsigned int) 4*faceid;
edgeids.clear();
auto it = face_to_edge_.begin() + offset;
edgeids.insert(edgeids.begin(), it, it + 4);
if (fedgedirs) {
fedgedirs->clear();
auto jt = face_to_edge_dirs_.begin();
fedgedirs->insert(fedgedirs->begin(), jt, jt + 4);
}
}
//---------------------------------------------------------
// Cooordinates of a node
//---------------------------------------------------------
AmanziGeometry::Point
MeshSimple::getNodeCoordinate(const AmanziMesh::Entity_ID local_node_id) const
{
unsigned int offset = (unsigned int) 3*local_node_id;
AmanziGeometry::Point ncoord;
ncoord.set(3, &(coordinates_[offset]));
return ncoord;
}
//---------------------------------------------------------
// Faces of type 'ptype' connected to a node
//---------------------------------------------------------
void MeshSimple::getNodeFaces(const AmanziMesh::Entity_ID nodeid,
const AmanziMesh::Parallel_type ptype,
AmanziMesh::Entity_ID_List& faceids) const
{
unsigned int offset = (unsigned int) 13*nodeid;
unsigned int nfaces = node_to_face_[offset];
faceids.clear();
for (int i = 0; i < nfaces; i++)
faceids.push_back(node_to_face_[offset+i+1]);
}
//---------------------------------------------------------
// Cells connected to a face
//---------------------------------------------------------
void MeshSimple::getFaceCells(const AmanziMesh::Entity_ID faceid,
const AmanziMesh::Parallel_type ptype,
AmanziMesh::Entity_ID_List& cellids) const
{
unsigned int offset = (unsigned int) 2*faceid;
cellids.clear();
if (face_to_cell_[offset] != -1)
cellids.push_back(face_to_cell_[offset]);
if (face_to_cell_[offset+1] != -1)
cellids.push_back(face_to_cell_[offset+1]);
}
} // namespace AmanziMesh
} // namespace Amanzi