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