Loading...
Searching...
No Matches
blockCreate.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2019-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27\*---------------------------------------------------------------------------*/
29#include "block.H"
30
31// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33#define w0 w[0][i]
34#define w1 w[1][i]
35#define w2 w[2][i]
36#define w3 w[3][i]
38#define w4 w[4][j]
39#define w5 w[5][j]
40#define w6 w[6][j]
41#define w7 w[7][j]
42
43#define w8 w[8][k]
44#define w9 w[9][k]
45#define w10 w[10][k]
46#define w11 w[11][k]
47
48void Foam::block::createPoints()
49{
50 // Set local variables for mesh specification
51 const label ni = density().x();
52 const label nj = density().y();
53 const label nk = density().z();
54
55 const point& p000 = blockPoint(0);
56 const point& p100 = blockPoint(1);
57 const point& p110 = blockPoint(2);
58 const point& p010 = blockPoint(3);
59
60 const point& p001 = blockPoint(4);
61 const point& p101 = blockPoint(5);
62 const point& p111 = blockPoint(6);
63 const point& p011 = blockPoint(7);
64
65 // List of edge point and weighting factors
66 pointField p[12];
67 scalarList w[12];
68 const int nCurvedEdges = edgesPointsWeights(p, w);
69
70 points_.resize(nPoints());
71
72 points_[pointLabel(0, 0, 0)] = p000;
73 points_[pointLabel(ni, 0, 0)] = p100;
74 points_[pointLabel(ni, nj, 0)] = p110;
75 points_[pointLabel(0, nj, 0)] = p010;
76 points_[pointLabel(0, 0, nk)] = p001;
77 points_[pointLabel(ni, 0, nk)] = p101;
78 points_[pointLabel(ni, nj, nk)] = p111;
79 points_[pointLabel(0, nj, nk)] = p011;
80
81 for (label k=0; k<=nk; k++)
82 {
83 for (label j=0; j<=nj; j++)
84 {
85 for (label i=0; i<=ni; i++)
86 {
87 // Skip block vertices
88 if (vertex(i, j, k)) continue;
89
90 const label vijk = pointLabel(i, j, k);
91
92 // Calculate the weighting factors for all edges
93
94 // x-direction
95 scalar wx1 = (1 - w0)*(1 - w4)*(1 - w8) + w0*(1 - w5)*(1 - w9);
96 scalar wx2 = (1 - w1)*w4*(1 - w11) + w1*w5*(1 - w10);
97 scalar wx3 = (1 - w2)*w7*w11 + w2*w6*w10;
98 scalar wx4 = (1 - w3)*(1 - w7)*w8 + w3*(1 - w6)*w9;
99 {
100 const scalar sum(wx1 + wx2 + wx3 + wx4);
101 wx1 /= sum;
102 wx2 /= sum;
103 wx3 /= sum;
104 wx4 /= sum;
105 }
106
107 // y-direction
108 scalar wy1 = (1 - w4)*(1 - w0)*(1 - w8) + w4*(1 - w1)*(1 - w11);
109 scalar wy2 = (1 - w5)*w0*(1 - w9) + w5*w1*(1 - w10);
110 scalar wy3 = (1 - w6)*w3*w9 + w6*w2*w10;
111 scalar wy4 = (1 - w7)*(1 - w3)*w8 + w7*(1 - w2)*w11;
112 {
113 const scalar sum(wy1 + wy2 + wy3 + wy4);
114 wy1 /= sum;
115 wy2 /= sum;
116 wy3 /= sum;
117 wy4 /= sum;
118 }
119
120 // z-direction
121 scalar wz1 = (1 - w8)*(1 - w0)*(1 - w4) + w8*(1 - w3)*(1 - w7);
122 scalar wz2 = (1 - w9)*w0*(1 - w5) + w9*w3*(1 - w6);
123 scalar wz3 = (1 - w10)*w1*w5 + w10*w2*w6;
124 scalar wz4 = (1 - w11)*(1 - w1)*w4 + w11*(1 - w2)*w7;
125 {
126 const scalar sum(wz1 + wz2 + wz3 + wz4);
127 wz1 /= sum;
128 wz2 /= sum;
129 wz3 /= sum;
130 wz4 /= sum;
131 }
132
133 // Points on straight edges
134 const vector edgex1 = p000 + (p100 - p000)*w0;
135 const vector edgex2 = p010 + (p110 - p010)*w1;
136 const vector edgex3 = p011 + (p111 - p011)*w2;
137 const vector edgex4 = p001 + (p101 - p001)*w3;
138
139 const vector edgey1 = p000 + (p010 - p000)*w4;
140 const vector edgey2 = p100 + (p110 - p100)*w5;
141 const vector edgey3 = p101 + (p111 - p101)*w6;
142 const vector edgey4 = p001 + (p011 - p001)*w7;
143
144 const vector edgez1 = p000 + (p001 - p000)*w8;
145 const vector edgez2 = p100 + (p101 - p100)*w9;
146 const vector edgez3 = p110 + (p111 - p110)*w10;
147 const vector edgez4 = p010 + (p011 - p010)*w11;
148
149 // Add the contributions
150 // Note: use double precision to avoid overflows when summing
151
152 doubleVector sumVector(Zero);
153
154 sumVector += wx1*edgex1;
155 sumVector += wx2*edgex2;
156 sumVector += wx3*edgex3;
157 sumVector += wx4*edgex4;
158
159 sumVector += wy1*edgey1;
160 sumVector += wy2*edgey2;
161 sumVector += wy3*edgey3;
162 sumVector += wy4*edgey4;
163
164 sumVector += wz1*edgez1;
165 sumVector += wz2*edgez2;
166 sumVector += wz3*edgez3;
167 sumVector += wz4*edgez4;
168 sumVector /= 3;
169
170 // Apply curved-edge correction if block has curved edges
171 if (nCurvedEdges)
172 {
173 // Calculate the correction vectors
174 sumVector += wx1*(p[0][i] - edgex1); // corx1
175 sumVector += wx2*(p[1][i] - edgex2); // corx2
176 sumVector += wx3*(p[2][i] - edgex3); // corx3
177 sumVector += wx4*(p[3][i] - edgex4); // corx4
178
179 sumVector += wy1*(p[4][j] - edgey1); // cory1
180 sumVector += wy2*(p[5][j] - edgey2); // cory2
181 sumVector += wy3*(p[6][j] - edgey3); // cory3
182 sumVector += wy4*(p[7][j] - edgey4); // cory4
183
184 sumVector += wz1*(p[8][k] - edgez1); // corz1
185 sumVector += wz2*(p[9][k] - edgez2); // corz2
186 sumVector += wz3*(p[10][k] - edgez3); // corz3
187 sumVector += wz4*(p[11][k] - edgez4); // corz4
188 }
189
190 points_[vijk] = sumVector;
191 }
192 }
193 }
194
195 if (!nCurvedFaces()) return;
196
197 // Apply curvature correction to face points
198 FixedList<pointField, 6> facePoints(this->facePoints(points_));
200
201 // Loop over points and apply the correction from the i-faces
202 for (label ii=0; ii<=ni; ii++)
203 {
204 // Process the points on the curved faces last
205 const label i = (ii + 1)%(ni + 1);
206
207 for (label j=0; j<=nj; j++)
208 {
209 for (label k=0; k<=nk; k++)
210 {
211 // Skip non-curved faces and edges
212 if (flatFaceOrEdge(i, j, k)) continue;
213
214 const label vijk = pointLabel(i, j, k);
215
216 scalar wf0 =
217 (
218 (1 - w0)*(1 - w4)*(1 - w8)
219 + (1 - w1)*w4*(1 - w11)
220 + (1 - w2)*w7*w11
221 + (1 - w3)*(1 - w7)*w8
222 );
223
224 scalar wf1 =
225 (
226 w0*(1 - w5)*(1 - w9)
227 + w1*w5*(1 - w10)
228 + w2*w5*w10
229 + w3*(1 - w6)*w9
230 );
231
232 const scalar sumWf = wf0 + wf1;
233 wf0 /= sumWf;
234 wf1 /= sumWf;
235
236 points_[vijk] +=
237 (
238 wf0
239 *(
240 facePoints[0][facePointLabel(0, j, k)]
241 - points_[pointLabel(0, j, k)]
242 )
243 + wf1
244 *(
245 facePoints[1][facePointLabel(1, j, k)]
246 - points_[pointLabel(ni, j, k)]
247 )
248 );
249 }
250 }
251 }
252
253 // Loop over points and apply the correction from the j-faces
254 for (label jj=0; jj<=nj; jj++)
255 {
256 // Process the points on the curved faces last
257 const label j = (jj + 1)%(nj + 1);
258
259 for (label i=0; i<=ni; i++)
260 {
261 for (label k=0; k<=nk; k++)
262 {
263 // Skip flat faces and edges
264 if (flatFaceOrEdge(i, j, k)) continue;
265
266 const label vijk = pointLabel(i, j, k);
267
268 scalar wf2 =
269 (
270 (1 - w4)*(1 - w1)*(1 - w8)
271 + (1 - w5)*w0*(1 - w9)
272 + (1 - w6)*w3*w9
273 + (1 - w7)*(1 - w3)*w8
274 );
275
276 scalar wf3 =
277 (
278 w4*(1 - w1)*(1 - w11)
279 + w5*w1*(1 - w10)
280 + w6*w2*w10
281 + w7*(1 - w2)*w11
282 );
283
284 const scalar sumWf = wf2 + wf3;
285 wf2 /= sumWf;
286 wf3 /= sumWf;
287
288 points_[vijk] +=
289 (
290 wf2
291 *(
292 facePoints[2][facePointLabel(2, i, k)]
293 - points_[pointLabel(i, 0, k)]
294 )
295 + wf3
296 *(
297 facePoints[3][facePointLabel(3, i, k)]
298 - points_[pointLabel(i, nj, k)]
299 )
300 );
301 }
302 }
303 }
304
305 // Loop over points and apply the correction from the k-faces
306 for (label kk=0; kk<=nk; kk++)
307 {
308 // Process the points on the curved faces last
309 const label k = (kk + 1)%(nk + 1);
310
311 for (label i=0; i<=ni; i++)
312 {
313 for (label j=0; j<=nj; j++)
314 {
315 // Skip flat faces and edges
316 if (flatFaceOrEdge(i, j, k)) continue;
317
318 const label vijk = pointLabel(i, j, k);
319
320 scalar wf4 =
321 (
322 (1 - w8)*(1 - w0)*(1 - w4)
323 + (1 - w9)*w0*(1 - w5)
324 + (1 - w10)*w1*w5
325 + (1 - w11)*(1 - w1)*w4
326 );
327
328 scalar wf5 =
329 (
330 w8*(1 - w3)*(1 - w7)
331 + w9*w3*(1 - w6)
332 + w10*w2*w6
333 + w11*(1 - w2)*w7
334 );
335
336 {
337 const scalar sum(wf4 + wf5);
338 wf4 /= sum;
339 wf5 /= sum;
340 }
341
342 points_[vijk] +=
343 (
344 wf4
345 *(
346 facePoints[4][facePointLabel(4, i, j)]
347 - points_[pointLabel(i, j, 0)]
348 )
349 + wf5
350 *(
351 facePoints[5][facePointLabel(5, i, j)]
352 - points_[pointLabel(i, j, nk)]
353 )
354 );
355 }
356 }
357 }
358}
359
360
361void Foam::block::createCells()
362{
363 const label ni = density().x();
364 const label nj = density().y();
365 const label nk = density().z();
366
367 blockCells_.resize(nCells()); // (ni*nj*nk)
368
369 label celli = 0;
370
371 for (label k=0; k<nk; ++k)
372 {
373 for (label j=0; j<nj; ++j)
374 {
375 for (label i=0; i<ni; ++i)
376 {
377 blockCells_[celli] = vertLabels(i, j, k);
378 ++celli;
379 }
380 }
381 }
382}
383
384
385template<class OutputIterator>
386OutputIterator Foam::block::addBoundaryFaces
387(
388 const direction shapeFacei,
389 OutputIterator iter
390) const
391{
392 const label ni = density().x();
393 const label nj = density().y();
394 const label nk = density().z();
395
396 switch (shapeFacei)
397 {
398 // Face 0 == x-min
399 case 0:
400 {
401 for (label k=0; k<nk; ++k)
402 {
403 for (label j=0; j<nj; ++j)
404 {
405 auto& f = *iter;
406 ++iter;
407 f.resize(4);
408
409 f[0] = pointLabel(0, j, k);
410 f[1] = pointLabel(0, j, k+1);
411 f[2] = pointLabel(0, j+1, k+1);
412 f[3] = pointLabel(0, j+1, k);
413 }
414 }
415 }
416 break;
417
418 // Face 1 == x-max
419 case 1:
420 {
421 for (label k=0; k<nk; ++k)
422 {
423 for (label j=0; j<nj; ++j)
424 {
425 auto& f = *iter;
426 ++iter;
427 f.resize(4);
428
429 f[0] = pointLabel(ni, j, k);
430 f[1] = pointLabel(ni, j+1, k);
431 f[2] = pointLabel(ni, j+1, k+1);
432 f[3] = pointLabel(ni, j, k+1);
433 }
434 }
435 }
436 break;
437
438 // Face 2 == y-min
439 case 2:
440 {
441 for (label i=0; i<ni; ++i)
442 {
443 for (label k=0; k<nk; ++k)
444 {
445 auto& f = *iter;
446 ++iter;
447 f.resize(4);
448
449 f[0] = pointLabel(i, 0, k);
450 f[1] = pointLabel(i+1, 0, k);
451 f[2] = pointLabel(i+1, 0, k+1);
452 f[3] = pointLabel(i, 0, k+1);
453 }
454 }
455 }
456 break;
457
458 // Face 3 == y-max
459 case 3:
460 {
461 for (label i=0; i<ni; ++i)
462 {
463 for (label k=0; k<nk; ++k)
464 {
465 auto& f = *iter;
466 ++iter;
467 f.resize(4);
468
469 f[0] = pointLabel(i, nj, k);
470 f[1] = pointLabel(i, nj, k+1);
471 f[2] = pointLabel(i+1, nj, k+1);
472 f[3] = pointLabel(i+1, nj, k);
473 }
474 }
475 }
476 break;
477
478 // Face 4 == z-min
479 case 4:
480 {
481 for (label i=0; i<ni; ++i)
482 {
483 for (label j=0; j<nj; ++j)
484 {
485 auto& f = *iter;
486 ++iter;
487 f.resize(4);
488
489 f[0] = pointLabel(i, j, 0);
490 f[1] = pointLabel(i, j+1, 0);
491 f[2] = pointLabel(i+1, j+1, 0);
492 f[3] = pointLabel(i+1, j, 0);
493 }
494 }
495 }
496 break;
497
498 // Face 5 == z-max
499 case 5:
500 {
501 for (label i=0; i<ni; ++i)
502 {
503 for (label j=0; j<nj; ++j)
504 {
505 auto& f = *iter;
506 ++iter;
507 f.resize(4);
508
509 f[0] = pointLabel(i, j, nk);
510 f[1] = pointLabel(i+1, j, nk);
511 f[2] = pointLabel(i+1, j+1, nk);
512 f[3] = pointLabel(i, j+1, nk);
513 }
514 }
515 }
516 break;
517 }
518
519 return iter;
520}
521
522
523void Foam::block::createBoundary()
524{
525 const label countx = (density().y() * density().z());
526 const label county = (density().z() * density().x());
527 const label countz = (density().x() * density().y());
528
529 direction patchi = 0;
530
531 // 0 == x-min
532 blockPatches_[patchi].resize(countx);
533 addBoundaryFaces(patchi, blockPatches_[patchi].begin());
534 ++patchi;
535
536 // 1 == x-max
537 blockPatches_[patchi].resize(countx);
538 addBoundaryFaces(patchi, blockPatches_[patchi].begin());
539 ++patchi;
540
541 // 2 == y-min
542 blockPatches_[patchi].resize(county);
543 addBoundaryFaces(patchi, blockPatches_[patchi].begin());
544 ++patchi;
545
546 // 3 == y-max
547 blockPatches_[patchi].resize(county);
548 addBoundaryFaces(patchi, blockPatches_[patchi].begin());
549 ++patchi;
550
551 // 4 == z-min
552 blockPatches_[patchi].resize(countz);
553 addBoundaryFaces(patchi, blockPatches_[patchi].begin());
554 ++patchi;
555
556 // 5 == z-max
557 blockPatches_[patchi].resize(countz);
558 addBoundaryFaces(patchi, blockPatches_[patchi].begin());
559 ++patchi;
560}
561
562
563// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
564
566{
567 const label ni = density().x();
568 const label nj = density().y();
569 const label nk = density().z();
570
571 cellShapeList theCells(nCells()); // (ni*nj*nk)
572
573 label celli = 0;
574
575 for (label k=0; k<nk; ++k)
576 {
577 for (label j=0; j<nj; ++j)
578 {
579 for (label i=0; i<ni; ++i)
580 {
581 theCells[celli] = vertLabels(i, j, k).shape();
582 ++celli;
583 }
584 }
585 }
586
587 return theCells;
588}
589
590
591// ************************************************************************* //
label k
#define w2
Definition blockCreate.C:28
#define w11
Definition blockCreate.C:39
#define w9
Definition blockCreate.C:37
#define w8
Definition blockCreate.C:36
#define w7
Definition blockCreate.C:34
#define w1
Definition blockCreate.C:27
#define w4
Definition blockCreate.C:31
#define w10
Definition blockCreate.C:38
#define w3
Definition blockCreate.C:29
#define w0
Definition blockCreate.C:26
#define w5
Definition blockCreate.C:32
#define w6
Definition blockCreate.C:33
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
const Cmpt & x() const noexcept
Access to the vector x component.
Definition Vector.H:135
const Cmpt & z() const noexcept
Access to the vector z component.
Definition Vector.H:145
const Cmpt & y() const noexcept
Access to the vector y component.
Definition Vector.H:140
label nCurvedFaces() const noexcept
Number of curved faces in this block.
bool flatFaceOrEdge(const label i, const label j, const label k) const
Return true if point i,j,k addresses a block flat face or edge.
const point & blockPoint(const label i) const
Return block point for local label i.
bool vertex(const label i, const label j, const label k) const
True if point i,j,k addresses a block vertex.
int edgesPointsWeights(pointField(&edgesPoints)[12], scalarList(&edgesWeights)[12]) const
Calculate the points and weights for all edges.
label facePointLabel(const direction facei, const label i, const label j) const
Face vertex label offset for a particular i,j,k position on hex face (0-5).
const labelVector & density() const noexcept
The mesh density (number of cells) in the i,j,k directions.
void correctFacePoints(FixedList< pointField, 6 > &) const
Correct the location of the given face-points.
FixedList< pointField, 6 > facePoints(const pointField &points) const
Return the list of face-points for all of the faces of the block.
cellShapeList shapes() const
The (hex) cell shapes for filling the block.
A Vector of values with double precision.
label pointLabel(const label i, const label j, const label k) const
The linear point index for an i-j-k position.
Definition ijkMeshI.H:176
label nCells() const
The number of mesh cells (nx*ny*nz) in the i-j-k mesh.
Definition ijkMeshI.H:61
label nPoints() const
The number of mesh points (nx+1)*(ny+1)*(nz+1) in the i-j-k mesh.
Definition ijkMeshI.H:48
hexCell vertLabels(const label i, const label j, const label k) const
The hex cell vertices for an i-j-k position.
Definition ijkMesh.C:26
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
volScalarField & p
uint8_t direction
Definition direction.H:49
vector point
Point is a vector.
Definition point.H:37
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
vectorField pointField
pointField is a vectorField.
List< scalar > scalarList
List of scalar.
Definition scalarList.H:32
List< cellShape > cellShapeList
List of cellShape.
labelList f(nPoints)