blockDescriptor.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "blockDescriptor.H"
27 #include "blockMesh.H"
28 #include "blockMeshTools.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 void Foam::blockDescriptor::check(const Istream& is)
33 {
34  forAll(blockShape_, pi)
35  {
36  if (blockShape_[pi] < 0)
37  {
39  << "Negative point label " << blockShape_[pi]
40  << " in block " << *this
41  << exit(FatalIOError);
42  }
43  else if (blockShape_[pi] >= vertices_.size())
44  {
46  << "Point label " << blockShape_[pi]
47  << " out of range 0.." << vertices_.size() - 1
48  << " in block " << *this
49  << exit(FatalIOError);
50  }
51  }
52 
53  const faceList faces(blockShape_.faces());
54 
55  // Check each face is outward-pointing with respect to the block centre
56  label outwardFaceCount = 0;
57  boolList correctFaces(faces.size(), true);
58 
59  forAll(faces, i)
60  {
61  const point faceCentre(faces[i].centre(vertices_));
62  const vector faceArea(faces[i].area(vertices_));
63 
64  if (mag(faceArea) > small)
65  {
66  bool outwardFace = false;
67  forAll(faces, j)
68  {
69  if (j != i)
70  {
71  if
72  (
73  ((faceCentre - faces[j].centre(vertices_)) & faceArea)
74  > 0
75  )
76  {
77  outwardFace = true;
78  }
79  }
80  }
81 
82  if (outwardFace)
83  {
84  outwardFaceCount++;
85  }
86  else
87  {
88  correctFaces[i] = false;
89  }
90  }
91  else
92  {
93  outwardFaceCount++;
94  }
95  }
96 
97  // If all faces are inward-pointing the block is inside-out
98  if (outwardFaceCount == 0)
99  {
101  << "Block " << *this << " is inside-out"
102  << exit(FatalIOError);
103  }
104  else if (outwardFaceCount != faces.size())
105  {
107  << "Block " << *this << " has inward-pointing faces"
108  << nl << " ";
109 
110  forAll(correctFaces, i)
111  {
112  if (!correctFaces[i])
113  {
115  }
116  }
117 
119  }
120 }
121 
122 
123 void Foam::blockDescriptor::findCurvedFaces()
124 {
125  const faceList blockFaces(blockShape().faces());
126 
127  forAll(blockFaces, blockFacei)
128  {
129  forAll(faces_, facei)
130  {
131  if
132  (
134  (
135  faces_[facei].vertices(),
136  blockFaces[blockFacei]
137  )
138  )
139  {
140  curvedFaces_[blockFacei] = facei;
141  nCurvedFaces_++;
142  break;
143  }
144  }
145  }
146 }
147 
148 
149 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
150 
152 (
153  const cellShape& bshape,
154  const pointField& vertices,
155  const blockEdgeList& edges,
156  const blockFaceList& faces,
157  const Vector<label>& density,
159  const word& zoneName
160 )
161 :
162  vertices_(vertices),
163  edges_(edges),
164  faces_(faces),
165  blockShape_(bshape),
166  density_(density),
167  expand_(expand),
168  zoneName_(zoneName),
169  curvedFaces_(-1),
170  nCurvedFaces_(0)
171 {
172  if (expand_.size() != 12)
173  {
175  << "Unknown definition of expansion ratios"
176  << exit(FatalError);
177  }
178 
179  findCurvedFaces();
180 }
181 
182 
184 (
185  const dictionary& dict,
186  const label index,
187  const pointField& vertices,
188  const blockEdgeList& edges,
189  const blockFaceList& faces,
190  Istream& is
191 )
192 :
193  vertices_(vertices),
194  edges_(edges),
195  faces_(faces),
196  density_(),
197  expand_(12, gradingDescriptors()),
198  zoneName_(),
199  curvedFaces_(-1),
200  nCurvedFaces_(0)
201 {
202  // Read cell model and list of vertices (potentially with variables)
203  word modelName(is);
204  const cellModel* modelPtr = cellModeller::lookup(modelName);
205  if (!modelPtr)
206  {
208  << "Block type " << modelName << " not recognised"
209  << exit(FatalIOError);
210  }
211  blockShape_ = cellShape
212  (
213  *modelPtr,
214  blockMeshTools::read<label>
215  (
216  is,
217  dict.subOrEmptyDict("namedVertices")
218  )
219  );
220  if (blockShape_.size() != blockShape_.model().nPoints())
221  {
223  << "Block of type " << modelName << " has "
224  << blockShape_.size() << " points rather than "
225  << blockShape_.model().nPoints()
226  << exit(FatalIOError);
227  }
228 
229  // Examine next token
230  token t(is);
231 
232  // Optional zone name
233  if (t.isWord())
234  {
235  zoneName_ = t.wordToken();
236 
237  // Examine next token
238  is >> t;
239  }
240  is.putBack(t);
241 
242  if (t.isPunctuation())
243  {
244  // New-style: read a list of 3 values
245  if (t.pToken() == token::BEGIN_LIST)
246  {
247  is >> density_;
248  }
249  else
250  {
252  (
253  is
254  ) << "incorrect token while reading n, expected '(', found "
255  << t.info()
256  << exit(FatalIOError);
257  }
258  }
259  else
260  {
261  // Old-style: read three labels
262  is >> density_.x()
263  >> density_.y()
264  >> density_.z();
265  }
266 
267  is >> t;
268  if (!t.isWord())
269  {
270  is.putBack(t);
271  }
272 
273  List<gradingDescriptors> expRatios(is);
274 
275  if (expRatios.size() == 1)
276  {
277  // Identical in x/y/z-directions
278  expand_ = expRatios[0];
279  }
280  else if (expRatios.size() == 3)
281  {
282  // x-direction
283  expand_[0] = expRatios[0];
284  expand_[1] = expRatios[0];
285  expand_[2] = expRatios[0];
286  expand_[3] = expRatios[0];
287 
288  // y-direction
289  expand_[4] = expRatios[1];
290  expand_[5] = expRatios[1];
291  expand_[6] = expRatios[1];
292  expand_[7] = expRatios[1];
293 
294  // z-direction
295  expand_[8] = expRatios[2];
296  expand_[9] = expRatios[2];
297  expand_[10] = expRatios[2];
298  expand_[11] = expRatios[2];
299  }
300  else if (expRatios.size() == 12)
301  {
302  expand_ = expRatios;
303  }
304  else
305  {
307  << "Unknown definition of expansion ratios: " << expRatios
308  << exit(FatalIOError);
309  }
310 
312  {
313  check(is);
314  }
315 
316  findCurvedFaces();
317 }
318 
319 
320 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
321 
324 {
325  // Caches points for curvature correction
326  FixedList<pointField, 6> facePoints;
327 
328  // Set local variables for mesh specification
329  const label ni = density_.x();
330  const label nj = density_.y();
331  const label nk = density_.z();
332 
333  facePoints[0].setSize((nj + 1)*(nk + 1));
334  facePoints[1].setSize((nj + 1)*(nk + 1));
335 
336  for (label j=0; j<=nj; j++)
337  {
338  for (label k=0; k<=nk; k++)
339  {
340  facePoints[0][facePointLabel(0, j, k)] =
341  points[pointLabel(0, j, k)];
342  facePoints[1][facePointLabel(1, j, k)] =
343  points[pointLabel(ni, j, k)];
344  }
345  }
346 
347  facePoints[2].setSize((ni + 1)*(nk + 1));
348  facePoints[3].setSize((ni + 1)*(nk + 1));
349 
350  for (label i=0; i<=ni; i++)
351  {
352  for (label k=0; k<=nk; k++)
353  {
354  facePoints[2][facePointLabel(2, i, k)] =
355  points[pointLabel(i, 0, k)];
356  facePoints[3][facePointLabel(3, i, k)] =
357  points[pointLabel(i, nj, k)];
358  }
359  }
360 
361  facePoints[4].setSize((ni + 1)*(nj + 1));
362  facePoints[5].setSize((ni + 1)*(nj + 1));
363 
364  for (label i=0; i<=ni; i++)
365  {
366  for (label j=0; j<=nj; j++)
367  {
368  facePoints[4][facePointLabel(4, i, j)] =
369  points[pointLabel(i, j, 0)];
370  facePoints[5][facePointLabel(5, i, j)] =
371  points[pointLabel(i, j, nk)];
372  }
373  }
374 
375  return facePoints;
376 }
377 
378 
380 (
381  FixedList<pointField, 6>& facePoints
382 ) const
383 {
384  forAll(curvedFaces_, blockFacei)
385  {
386  if (curvedFaces_[blockFacei] != -1)
387  {
388  faces_[curvedFaces_[blockFacei]].project
389  (
390  *this,
391  blockFacei,
392  facePoints[blockFacei]
393  );
394  }
395  }
396 }
397 
398 
400 (
401  Ostream& os,
402  const label val,
403  const dictionary& d
404 )
405 {
406  const dictionary* varDictPtr = d.subDictPtr("namedBlocks");
407  if (varDictPtr)
408  {
409  blockMeshTools::write(os, val, *varDictPtr);
410  }
411  else
412  {
413  os << val;
414  }
415 }
416 
417 
418 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
419 
421 {
422  const cellShape& bshape = bd.blockShape();
423  const labelList& blockLabels = bshape;
424 
425  os << bshape.model().name() << " (";
426 
427  forAll(blockLabels, labelI)
428  {
429  if (labelI)
430  {
431  os << ' ';
432  }
433  os << blockLabels[labelI];
434  }
435  os << ')';
436 
437  if (bd.zoneName().size())
438  {
439  os << ' ' << bd.zoneName();
440  }
441 
442  os << ' ' << bd.density()
443  << " simpleGrading (";
444 
445  const List<gradingDescriptors>& expand = bd.expand_;
446 
447  // Can we use a compact notation?
448  if
449  (
450  // x-direction
451  (
452  expand[0] == expand[1]
453  && expand[0] == expand[2]
454  && expand[0] == expand[3]
455  )
456  && // y-direction
457  (
458  expand[4] == expand[5]
459  && expand[4] == expand[6]
460  && expand[4] == expand[7]
461  )
462  && // z-direction
463  (
464  expand[8] == expand[9]
465  && expand[8] == expand[10]
466  && expand[8] == expand[11]
467  )
468  )
469  {
470  os << expand[0] << ' ' << expand[4] << ' ' << expand[8];
471  }
472  else
473  {
474  forAll(expand, edgei)
475  {
476  if (edgei)
477  {
478  os << ' ';
479  }
480  os << expand[edgei];
481  }
482  }
483 
484  os << ")";
485 
486  return os;
487 }
488 
489 
490 // ************************************************************************* //
label k
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:183
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
void putBack(const token &)
Put back token.
Definition: Istream.C:30
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
const Cmpt & z() const
Definition: VectorI.H:87
const Cmpt & y() const
Definition: VectorI.H:81
const Cmpt & x() const
Definition: VectorI.H:75
Takes the description of the block and the list of curved edges and creates a list of points on edges...
const cellShape & blockShape() const
Return the block shape.
blockDescriptor(const cellShape &, const pointField &vertices, const blockEdgeList &, const blockFaceList &, const Vector< label > &density, const UList< gradingDescriptors > &expand, const word &zoneName="")
Construct from components. Optional cellSet/zone name.
const word & zoneName() const
Return the (optional) zone name.
const blockFaceList & faces() const
Return reference to the list of curved faces.
static void write(Ostream &, const label blocki, const dictionary &)
Write block index with dictionary lookup.
const Vector< label > & density() const
Return 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.
static Switch checkBlockFaceOrientation
Switch checking block face orientation.
Definition: blockMesh.H:216
Maps a geometry to a set of cell primitives, which enables geometric cell data to be calculated witho...
Definition: cellModel.H:65
label nPoints() const
Return number of points.
Definition: cellModelI.H:50
const word & name() const
Return model name.
Definition: cellModelI.H:38
static const cellModel * lookup(const word &)
Look up a model by name and return a pointer to the model or nullptr.
Definition: cellModeller.C:100
An analytical geometric cellShape.
Definition: cellShape.H:72
const cellModel & model() const
Model reference.
Definition: cellShapeI.H:106
faceList faces() const
Faces of this cell.
Definition: cellShapeI.H:180
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
const dictionary * subDictPtr(const word &) const
Find and return a sub-dictionary pointer if present.
Definition: dictionary.C:748
static bool sameVertices(const face &, const face &)
Return true if the faces have the same vertices.
Definition: face.C:150
List of gradingDescriptor for the sections of a block with additional IO functionality.
A token holds items read from Istream.
Definition: token.H:74
bool isPunctuation() const
Definition: tokenI.H:324
@ BEGIN_LIST
Definition: token.H:110
punctuationToken pToken() const
Definition: tokenI.H:329
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:426
bool isWord() const
Definition: tokenI.H:342
const word & wordToken() const
Definition: tokenI.H:347
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
const pointField & points
void write(Ostream &, const label, const dictionary &)
Write with dictionary lookup.
vector faceArea(const face &f, const point &fPAvg, const pointField &ps)
Compute the face-area.
const dimensionSet density
const dimensionSet area
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
pointField vertices(const blockVertexList &bvl)
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
static const labelSphericalTensor labelI(1)
Identity labelTensor.
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
vector point
Point is a vector.
Definition: point.H:41
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
IOerror FatalIOError
tmp< DimensionedField< scalar, GeoMesh, Field > > mag(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
error FatalError
List< face > faceList
Definition: faceListFwd.H:41
static const char nl
Definition: Ostream.H:297
string expand(const string &s, string::size_type &index, const dictionary &dict, const bool allowEnvVars, const bool allowEmpty)
Definition: stringOps.C:146
dictionary dict