blockMeshCartesianConfiguration.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) 2023-2026 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 
27 #include "dictionary.H"
28 #include "polyPatch.H"
29 #include "wallPolyPatch.H"
30 #include "blockMeshFunctions.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
35  {"xMin", "xMax", "yMin", "yMax", "zMin", "zMax"};
36 
37 
38 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 
41 (
42  const bool& boundsOpt
43 )
44 {
45  Info<< "Surface bounding box is " << bb_ << endl;
46 
47  // Round the bounding box if it is not specified with '-bounds' option
48  const scalar roundFactor = roundingScale(bb_.minDim());
49  if (!boundsOpt)
50  {
51  roundBoundingBox(bb_, roundFactor);
52  }
53 
54  // Set nCells with the lowest number of cells within the range 10-20
56  {
57  nCells_ = Vector<label>(bb_.span()/roundFactor);
58 
59  if (!isEven(nCells_) && cmptMin(nCells_) > 20 && !boundsOpt)
60  {
61  roundBoundingBox(bb_, 2*roundFactor);
62  nCells_ = Vector<label>(bb_.span()/roundFactor);
63  }
64 
65  if (cmptMin(nCells_) < 10)
66  {
67  nCells_ *= 2;
68  }
69  else if (cmptMin(nCells_) > 20)
70  {
71  nCells_ /= 2;
72  }
73  }
74 
75  Info<< "Bounding box is now " << bb_ << endl;
76 
77  if (minDimCells_ > 0)
78  {
80  }
81 
82  // Scale nCells_ by refine factor
84 
85  Info<< "Using background mesh nCells " << nCells_ << endl;
86 }
87 
88 
90 {
91  dictionary dict("backgroundMesh");
92 
93  dict.add("xMin", bb_.min().x(), true);
94  dict.add("xMax", bb_.max().x(), true);
95  dict.add("yMin", bb_.min().y(), true);
96  dict.add("yMax", bb_.max().y(), true);
97  dict.add("zMin", bb_.min().z(), true);
98  dict.add("zMax", bb_.max().z(), true);
99  dict.add("xCells", nCells_.x(), true);
100  dict.add("yCells", nCells_.y(), true);
101  dict.add("zCells", nCells_.z(), true);
102 
103  os_ << dict.name().c_str()
104  << dict << endl;
105 }
106 
107 
109 {
110  Pair<word> defaultPatch;
111 
112  word opt = "defaultPatch";
113  if (patchOpts_.found(opt))
114  {
115  defaultPatch = readPatchOption(opt);
116  }
117  else
118  {
119  defaultPatch = {"background", "internal"};
120  }
121 
122  beginDict(os_, "defaultPatch");
123 
124  os_ << indent << "name " << defaultPatch.first() << ";" << nl
125  << indent << "type " << defaultPatch.second() << ";" << endl;
126 
127  endDict(os_);
128 
129  Info<< "\nAdding defaultPatch '" << defaultPatch.first()
130  << "' of type '" << defaultPatch.second() << "'" << endl;
131 }
132 
133 
135 (
136  const word& name,
137  const word& type,
138  const string& face
139 )
140 {
141  os_ << indent << name
142  << " { type " << type
143  << "; faces ( " << face.c_str()
144  << " ); }" << endl;
145 
146  Info<< "Adding patch '" << name
147  << "' of type '" << type << "'" << endl;
148 }
149 
150 
152 {
153  // Enable boundary section if a patch option or clearBoundary is selected
154  bool enableBoundary = clearBoundary_;
155  forAll(patches, i)
156  {
157  if (enableBoundary)
158  {
159  break;
160  }
161 
162  enableBoundary = patchOpts_.found(patches[i] + "Patch");
163  }
164 
165  if (!enableBoundary)
166  {
167  os_ << "// delete \"-disabled\" to enable boundary settings" << endl;
168 
169  Info<< "\nNote: The boundary list in blockMeshDict is disabled" << nl
170  << "To enable, open the file and edit line number "
171  << os_.lineNumber() << nl << endl;
172  }
173 
174  beginList
175  (
176  os_,
177  enableBoundary ? "boundary" : "boundary-disabled"
178  );
179 
180  const List<word> faces
181  {
182  "(0 3 7 4)",
183  "(1 5 6 2)",
184  "(0 4 5 1)",
185  "(3 2 6 7)",
186  "(0 1 2 3)",
187  "(4 7 6 5)"
188  };
189 
190  forAll(patches, i)
191  {
192  const bool optFound(patchOpts_.found(patches[i] + "Patch"));
193 
194  // Do not write patch entry if clearBoundary option is selected
195  // and the respective patch option is not selected
196  if (clearBoundary_ && !optFound)
197  {
198  continue;
199  }
200 
201  Pair<word> patch(patches[i], "patch");
202 
203  if (optFound)
204  {
205  patch = readPatchOption(patch.first() + "Patch");
206  }
207 
208  writePatch(patch.first(), patch.second(), faces[i]);
209  }
210 
211  endList(os_);
212 }
213 
214 
216 {
217  beginList(os_, "vertices");
218 
219  writeVertex("xMin", "yMin", "zMin");
220  writeVertex("xMax", "yMin", "zMin");
221  writeVertex("xMax", "yMax", "zMin");
222  writeVertex("xMin", "yMax", "zMin");
223  writeVertex("xMin", "yMin", "zMax");
224  writeVertex("xMax", "yMin", "zMax");
225  writeVertex("xMax", "yMax", "zMax");
226  writeVertex("xMin", "yMax", "zMax");
227 
228  endList(os_);
229 }
230 
231 
233 {
234  beginList(os_, "blocks");
235 
236  os_ << indent << "hex (0 1 2 3 4 5 6 7)" << nl
237  << indent << "(" << incrIndent << nl
238  << indent << "$!backgroundMesh/xCells" << nl
239  << indent << "$!backgroundMesh/yCells" << nl
240  << indent << "$!backgroundMesh/zCells" << decrIndent << nl
241  << indent << ")" << nl
242  << indent << "simpleGrading (1 1 1)" << endl;
243 
244  endList(os_);
245 }
246 
247 
249 {
250  beginList(os_, "edges");
251  endList(os_);
252 }
253 
254 
256 {
257  beginList(os_, "mergePatchPairs");
258  endList(os_, false);
259 }
260 
261 
262 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
263 
265 (
266  const fileName& name,
267  const fileName& dir,
268  const Time& time,
269  const meshingSurfaceList& surfaces,
270  const bool& boundsOpt,
271  const Vector<label>& nCells,
272  const label minDimCells,
273  const label refineFactor,
274  const HashTable<Pair<word>>& patchOpts,
275  const bool clearBoundary
276 )
277 :
278  blockMeshConfigurationBase(name, dir, time, surfaces, patchOpts),
279  nCells_(nCells),
280  minDimCells_(minDimCells),
281  refineFactor_(refineFactor),
282  clearBoundary_(clearBoundary)
283 {
284  calcBlockMeshDict(boundsOpt);
285 }
286 
287 
288 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
289 
291 {}
292 
293 
294 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
295 
297 {
298  dict_.writeHeader(os_, word("dictionary"));
299 
300  writeBackgroundMesh();
301  writeDefaultPatch();
302  writeBoundary();
303  writeVertices();
304  writeBlocks();
305  writeEdges();
306  writeMergePatchPairs();
307 
308  dict_.writeEndDivider(os_);
309 }
310 
311 
312 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Functions for calculating the bounds and number of cells of a background mesh configured within a blo...
static const Form zero
Definition: VectorSpace.H:118
void writeEdges()
Write edges list.
static const List< word > patches
Default patch names for the background mesh.
void writeBackgroundMesh()
Write backgroundMesh sub-dictionary.
const label minDimCells_
Number of cells in background mesh shortest direction.
const label refineFactor_
Refinement factor used to scale nCells.
Vector< label > nCells_
Number of cells in background mesh block.
void writeBoundary()
Write the boundary sub-dictionary.
void calcBlockMeshDict(const bool &boundsOpt)
Calculate the parameters for the blockMeshDict file.
void writeMergePatchPairs()
Write mergePatchPairs.
void writeDefaultPatch()
Write the defaultPatch entry.
void writeBlocks()
Write blocks sub-dictionary.
void write()
Write the blockMeshDict.
void writeVertices()
Write vertices list.
blockMeshCartesianConfiguration(const fileName &name, const fileName &dir, const Time &time, const meshingSurfaceList &surfaces, const bool &boundsOpt, const Vector< label > &nCells, const label minDimCells, const label refineFactor, const HashTable< Pair< word >> &patchOpts, const bool clearBoundary)
Construct from components.
void writePatch(const word &name, const word &type, const string &face)
Write a patch in the boundary sub-dictionary.
void roundBoundingBox(boundBox &bb, const scalar s)
Round a bounding box by the rounding scale.
boundBox bb_
Bounding box for the background mesh block.
scalar minDim() const
Smallest length/height/width dimension.
Definition: boundBoxI.H:136
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:118
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:111
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1019
Motion of the mesh specified as a list of pointMeshMovers.
const fvPatchList & patches
const dimensionSet time
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:272
bool isEven(const label l)
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
Vector< label > setMinDimCells(const vector &v, const scalar s)
messageStream Info
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:265
void cmptMin(Field< typename Field< Type >::cmptType > &res, const UList< Type > &f)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
scalar roundingScale(const scalar s)
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
static const char nl
Definition: Ostream.H:297
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict