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