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-2024 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 << nl
105  << "convertToMeters 1;" << nl
106  << endl;
107 }
108 
109 
111 {
112  Pair<word> defaultPatch;
113 
114  word opt = "defaultPatch";
115  if (patchOpts_.found(opt))
116  {
117  defaultPatch = readPatchOption(opt);
118  }
119  else
120  {
121  defaultPatch = {"background", "internal"};
122  }
123 
124  beginDict(os_, "defaultPatch");
125 
126  os_ << indent << "name " << defaultPatch.first() << ";" << nl
127  << indent << "type " << defaultPatch.second() << ";" << endl;
128 
129  endDict(os_);
130 
131  Info<< "\nAdding defaultPatch '" << defaultPatch.first()
132  << "' of type '" << defaultPatch.second() << "'" << endl;
133 }
134 
135 
137 (
138  const word& name,
139  const word& type,
140  const string& face
141 )
142 {
143  os_ << indent << name
144  << " { type " << type
145  << "; faces ( " << face.c_str()
146  << " ); }" << endl;
147 
148  Info<< "Adding patch '" << name
149  << "' of type '" << type << "'" << endl;
150 }
151 
152 
154 {
155  // Enable boundary section if a patch option or clearBoundary is selected
156  bool enableBoundary = clearBoundary_;
157  forAll(patches, i)
158  {
159  if (enableBoundary)
160  {
161  break;
162  }
163 
164  enableBoundary = patchOpts_.found(patches[i] + "Patch");
165  }
166 
167  if (!enableBoundary)
168  {
169  os_ << "// delete \"-disabled\" to enable boundary settings" << endl;
170 
171  Info<< "\nNote: The boundary list in blockMeshDict is disabled" << nl
172  << "To enable, open the file and edit line number "
173  << os_.lineNumber() << nl << endl;
174  }
175 
176  beginList
177  (
178  os_,
179  enableBoundary ? "boundary" : "boundary-disabled"
180  );
181 
182  const List<word> faces
183  {
184  "(0 3 7 4)",
185  "(1 5 6 2)",
186  "(0 4 5 1)",
187  "(3 2 6 7)",
188  "(0 1 2 3)",
189  "(4 7 6 5)"
190  };
191 
192  forAll(patches, i)
193  {
194  const bool optFound(patchOpts_.found(patches[i] + "Patch"));
195 
196  // Do not write patch entry if clearBoundary option is selected
197  // and the respective patch option is not selected
198  if (clearBoundary_ && !optFound)
199  {
200  continue;
201  }
202 
203  Pair<word> patch(patches[i], "patch");
204 
205  if (optFound)
206  {
207  patch = readPatchOption(patch.first() + "Patch");
208  }
209 
210  writePatch(patch.first(), patch.second(), faces[i]);
211  }
212 
213  endList(os_);
214 }
215 
216 
218 {
219  beginList(os_, "vertices");
220 
221  writeVertex("xMin", "yMin", "zMin");
222  writeVertex("xMax", "yMin", "zMin");
223  writeVertex("xMax", "yMax", "zMin");
224  writeVertex("xMin", "yMax", "zMin");
225  writeVertex("xMin", "yMin", "zMax");
226  writeVertex("xMax", "yMin", "zMax");
227  writeVertex("xMax", "yMax", "zMax");
228  writeVertex("xMin", "yMax", "zMax");
229 
230  endList(os_);
231 }
232 
233 
235 {
236  beginList(os_, "blocks");
237 
238  os_ << indent << "hex (0 1 2 3 4 5 6 7)" << nl
239  << indent << "(" << incrIndent << nl
240  << indent << "$!backgroundMesh/xCells" << nl
241  << indent << "$!backgroundMesh/yCells" << nl
242  << indent << "$!backgroundMesh/zCells" << decrIndent << nl
243  << indent << ")" << nl
244  << indent << "simpleGrading (1 1 1)" << endl;
245 
246  endList(os_);
247 }
248 
249 
251 {
252  beginList(os_, "edges");
253  endList(os_);
254 }
255 
256 
258 {
259  beginList(os_, "mergePatchPairs");
260  endList(os_, false);
261 }
262 
263 
264 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
265 
267 (
268  const fileName& name,
269  const fileName& dir,
270  const Time& time,
271  const meshingSurfaceList& surfaces,
272  const bool& boundsOpt,
273  const Vector<label>& nCells,
274  const label minDimCells,
275  const label refineFactor,
276  const HashTable<Pair<word>>& patchOpts,
277  const bool clearBoundary
278 )
279 :
280  blockMeshConfigurationBase(name, dir, time, surfaces, patchOpts),
281  nCells_(nCells),
282  minDimCells_(minDimCells),
283  refineFactor_(refineFactor),
284  clearBoundary_(clearBoundary)
285 {
286  calcBlockMeshDict(boundsOpt);
287 }
288 
289 
290 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
291 
293 {}
294 
295 
296 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
297 
299 {
300  dict_.writeHeader(os_, word("dictionary"));
301 
302  writeBackgroundMesh();
303  writeDefaultPatch();
304  writeBoundary();
305  writeVertices();
306  writeBlocks();
307  writeEdges();
308  writeMergePatchPairs();
309 
310  dict_.writeEndDivider(os_);
311 }
312 
313 
314 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
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:108
vector span() const
The bounding box span (from minimum to maximum)
Definition: boundBoxI.H:90
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:1020
const fvPatchList & patches
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:242
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:258
Vector< label > setMinDimCells(const vector &v, const scalar s)
void cmptMin(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
messageStream Info
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:235
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:228
static const char nl
Definition: Ostream.H:267
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