snappyHexMeshConfiguration.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 "Tuple3.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 void Foam::snappyHexMeshConfiguration::writeSnappySwitches()
32 {
33  dictionary dict("switches");
34 
35  dict.add("castellatedMesh", "on", true);
36  dict.add("snap", "on", true);
37  dict.add
38  (
39  "addLayers",
40  layers_ == 0 ? "off" : "on",
41  true
42  );
43 
44  dict.write(os_, false);
45  os_ << endl;
46 }
47 
48 
49 void Foam::snappyHexMeshConfiguration::writeGeometrySurface(const label surfID)
50 {
51  beginDict(os_, surfaces_[surfID].name());
52 
53  os_ << indent << "type triSurfaceMesh;" << nl
54  << indent << "file " << surfaces_[surfID].file() << ";" << endl;
55 
56  const wordList& inletRegions = surfaces_[surfID].inletRegions();
57  const wordList& outletRegions = surfaces_[surfID].outletRegions();
58 
59  if (!inletRegions.empty() || !outletRegions.empty())
60  {
61  beginDict(os_, "regions");
62 
63  forAll(inletRegions, i)
64  {
65  const word& region = inletRegions[i];
66  const word patch =
67  (
68  region == "<inletRegion>" || inletRegions.size() == 1
69  ? "inlet"
70  : region
71  );
72 
73  os_ << indent << region << " { name " << patch << "; }" << endl;
74  }
75 
76  forAll(outletRegions, i)
77  {
78  const word& region = outletRegions[i];
79  const word patch =
80  (
81  region == "<outletRegion>" || outletRegions.size() == 1
82  ? "outlet"
83  : region
84  );
85 
86  os_ << indent << region << " { name " << patch << "; }" << endl;
87  }
88 
89  endDict(os_, false);
90  }
91 
92  endDict(os_, false);
93 }
94 
95 
96 void Foam::snappyHexMeshConfiguration::writeSearchableBox(const label i)
97 {
98  beginDict(os_, "box" + std::to_string(i));
99 
100  os_ << indent << "type searchableBox;" << nl
101  << indent << "min " << refinementBoxes_[i].first() << ";" << nl
102  << indent << "max " << refinementBoxes_[i].second() << ";" << endl;
103 
104  endDict(os_);
105 }
106 
107 
108 void Foam::snappyHexMeshConfiguration::writeSnappyGeometry()
109 {
110  beginDict(os_, "geometry");
111 
112  forAll(surfaces_, i)
113  {
114  if (i != 0)
115  {
116  os_ << endl;
117  }
118 
119  writeGeometrySurface(i);
120  }
121 
122  forAll(refinementBoxes_, i)
123  {
124  os_ << endl;
125  writeSearchableBox(i);
126  }
127 
128  endDict(os_);
129 }
130 
131 
132 void Foam::snappyHexMeshConfiguration::writeFeatures()
133 {
134  beginList(os_, "features");
135 
136  if (!implicitFeatures_)
137  {
138  forAll(surfaces_, i)
139  {
140  fileName eMeshFile(surfaces_[i].name() + ".eMesh");
141  os_ << indent << "{ file " << eMeshFile
142  << "; level 1; }" << endl;
143  }
144  }
145 
146  endList(os_);
147 }
148 
149 
150 void Foam::snappyHexMeshConfiguration::writeRefinementSurfacesLevel
151 (
152  const label rl
153 )
154 {
155  os_ << indent << "level (" << rl << " " << rl << ");" << endl;
156 }
157 
158 
159 void Foam::snappyHexMeshConfiguration::writeRefinementSurfacesLevel()
160 {
161  writeRefinementSurfacesLevel(refinementLevel_);
162 }
163 
164 
165 void Foam::snappyHexMeshConfiguration::writeRefinementSurfacesLevel
166 (
167  const word& name
168 )
169 {
170  label rl(refinementLevel_);
171 
172  forAll(surfaceLevels_, i)
173  {
174  if (surfaceLevels_[i].first() == name)
175  {
176  rl = surfaceLevels_[i].second();
177  break;
178  }
179  }
180 
181  writeRefinementSurfacesLevel(rl);
182 }
183 
184 
185 void Foam::snappyHexMeshConfiguration::writePatchInfo
186 (
187  const word& type,
188  const word& group
189 )
190 {
191  if (group.empty())
192  {
193  os_ << indent << "patchInfo { type " << type << "; }" << endl;
194  }
195  else
196  {
197  beginDict(os_, "patchInfo");
198  os_ << indent << "type "<< type << ";" << endl;
199  os_ << indent << "inGroups (" << group << ");" << endl;
200  endDict(os_, false);
201  }
202 }
203 
204 
205 void Foam::snappyHexMeshConfiguration::writeRefinementSurfacesRegion
206 (
207  const word regionName,
208  const List<word>& regions
209 )
210 {
211  switch (regions.size())
212  {
213  case 0:
214  {
215  return;
216  }
217  case 1:
218  {
219  os_ << indent << regions[0] << endl;
220  break;
221  }
222  default:
223  {
224  os_ << indent << "\"" << regionName << ".*\"" << endl;
225  }
226  }
227 
228  beginDict(os_);
229  writeRefinementSurfacesLevel();
230 
231  word group(regionName);
232  if (regions.size() == 1 && regions[0] == regionName)
233  {
234  group = "";
235  }
236 
237  writePatchInfo("patch", group);
238 
239  endDict(os_, false);
240 }
241 
242 
243 void Foam::snappyHexMeshConfiguration::writeRefinementSurfacesRegions
244 (
245  const wordList& inletRegions,
246  const wordList& outletRegions
247 )
248 {
249  if (inletRegions.empty() && outletRegions.empty())
250  {
251  return;
252  }
253 
254  os_ << endl;
255  beginDict(os_, "regions");
256  writeRefinementSurfacesRegion("inlet", inletRegions);
257  writeRefinementSurfacesRegion("outlet", outletRegions);
258  endDict(os_, false);
259 }
260 
261 
262 void Foam::snappyHexMeshConfiguration::writeRefinementSurfaces()
263 {
264  beginDict(os_, "refinementSurfaces");
265 
266  forAll(surfaces_, i)
267  {
268  if (i != 0)
269  {
270  os_ << endl;
271  }
272 
273  const word& name = surfaces_[i].name();
274 
275  beginDict(os_, name);
276 
277  writeRefinementSurfacesLevel(name);
278 
279  const wordList& inletRegions = surfaces_[i].inletRegions();
280  const wordList& outletRegions = surfaces_[i].outletRegions();
281 
282  switch (surfaces_[i].type())
283  {
284  case surfaceType::wall:
285  {
286  writePatchInfo("wall");
287  writeRefinementSurfacesRegions(inletRegions, outletRegions);
288  break;
289  }
290 
291  case surfaceType::external:
292  {
293  writePatchInfo("wall", "externalWall");
294  writeRefinementSurfacesRegions(inletRegions, outletRegions);
295  break;
296  }
297 
298  case surfaceType::cellZone:
299  case surfaceType::rotatingZone:
300  {
301  os_ << indent << "faceZone "
302  << surfaces_[i].name() << ";" << nl
303  << indent << "cellZone "
304  << surfaces_[i].name() << ";" << nl
305  << indent << "mode inside;" << endl;
306 
307  break;
308  }
309 
310  case surfaceType::baffle:
311  {
312  writePatchInfo("wall");
313 
314  os_ << indent << "faceZone "
315  << surfaces_[i].name() << ";" << nl
316  << indent << "faceType boundary;" << endl;
317 
318  break;
319  }
320  }
321 
322  endDict(os_, false);
323  }
324 
325  endDict(os_);
326 }
327 
328 
329 void Foam::snappyHexMeshConfiguration::writeRefinementRegion
330 (
331  const word& name,
332  const label level
333 )
334 {
335  beginDict(os_, name);
336 
337  os_ << indent << "mode inside;" << nl
338  << indent << "level " << level << ";" << endl;
339 
340  endDict(os_, false);
341 }
342 
343 
344 void Foam::snappyHexMeshConfiguration::writeRefinementRegions()
345 {
346  if
347  (
348  refinementRegions_.empty()
349  && refinementBoxes_.empty()
350  && refinementDists_.empty()
351  )
352  {
353  os_ << indent
354  << "// delete \"-disabled\" below to enable refinementRegions"
355  << endl;
356 
357  beginDict(os_, "refinementRegions-disabled");
358  writeRefinementRegion("<surface>", refinementLevel_);
359  endDict(os_);
360  }
361  else
362  {
363  beginDict(os_, "refinementRegions");
364 
365  forAll(refinementRegions_, i)
366  {
367  writeRefinementRegion
368  (
369  refinementRegions_[i].first(),
370  refinementRegions_[i].second()
371  );
372  }
373 
374  forAll(refinementBoxes_, i)
375  {
376  writeRefinementRegion
377  (
378  "box" + std::to_string(i),
379  refinementBoxes_[i].third()
380  );
381  }
382 
383  forAll(refinementDists_, i)
384  {
385  beginDict(os_, refinementDists_[i].first());
386 
387  os_ << indent << "mode distance;" << nl
388  << indent << "levels (("
389  << refinementDists_[i].second() << " "
390  << refinementDists_[i].third() << "));" << endl;
391 
392  endDict(os_, false);
393  }
394 
395  endDict(os_);
396  }
397 }
398 
399 
400 void Foam::snappyHexMeshConfiguration::writeCastellatedMeshControls()
401 {
402  beginDict(os_, "castellatedMeshControls");
403 
404  writeFeatures();
405  writeRefinementSurfaces();
406  writeRefinementRegions();
407 
408  // Needs customising
409  os_ << indent << "insidePoint "
410  << insidePoint_ << ";" << endl;
411 
412  os_ << indent << "nCellsBetweenLevels "
413  << nCellsBetweenLevels_
414  << ";" << endl;
415 
416  endDict(os_);
417 }
418 
419 
420 void Foam::snappyHexMeshConfiguration::writeSnapControls()
421 {
422  beginDict(os_, "snapControls");
423 
424  os_ << indent << "explicitFeatureSnap "
425  << (implicitFeatures_ ? "off" : "on") << ";" << endl;
426  os_ << indent << "implicitFeatureSnap "
427  << (implicitFeatures_ ? "on" : "off") << ";" << endl;
428 
429  endDict(os_);
430 }
431 
432 
433 void Foam::snappyHexMeshConfiguration::writeAddLayersControls()
434 {
435  if (layers_ == 0)
436  {
437  return;
438  }
439 
440  beginDict(os_, "addLayersControls");
441 
442  beginDict(os_, "layers");
443 
444  forAll(surfaces_, i)
445  {
446  switch (surfaces_[i].type())
447  {
448  case surfaceType::wall:
449  case surfaceType::external:
450  case surfaceType::baffle:
451  {
452  os_ << indent << "\"" << surfaces_[i].name()
453  << ".*\" { nSurfaceLayers "
454  << layers_ << "; }" << endl;
455  break;
456  }
457 
458  default: break;
459  }
460  }
461 
462  endDict(os_);
463 
464  os_ << indent << "relativeSizes on; "
465  << "// off, usually with firstLayerThickness" << nl
466  << indent << "expansionRatio 1.2;" << nl
467  << indent << "finalLayerThickness 0.5;" << nl
468  << indent << "minThickness 1e-3;" << nl
469  << indent << "firstLayerThickness-disabled 0.01;" << nl << nl
470  << indent << "maxThicknessToMedialRatio-disabled 0.3;" << endl;
471 
472  endDict(os_);
473 }
474 
475 
476 void Foam::snappyHexMeshConfiguration::writeWriteFlags()
477 {
478  os_ << "// delete \"-disabled\" to output mesh data, e.g. for layers"
479  << endl;
480 
481  beginList(os_, "writeFlags-disabled");
482  os_ << indent << "scalarLevels" << endl;
483  os_ << indent << "layerSets" << endl;
484  os_ << indent << "layerFields" << endl;
485  endList(os_);
486 }
487 
488 
489 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
490 
492 (
493  const fileName& name,
494  const fileName& dir,
495  const Time& time,
496  const meshingSurfaceList& surfaces,
497  const label refinementLevel,
498  const List<Tuple2<word, label>>& surfaceLevels,
499  const List<Tuple2<word, label>>& refinementRegions,
500  const List<Tuple3<vector, vector, label>>& refinementBoxes,
501  const List<Tuple3<word, scalar, label>>& refinementDists,
502  const bool implicitFeatures,
503  const label layers,
504  const point& insidePoint,
505  const label nCellsBetweenLevels
506 )
507 :
508  caseFileConfiguration(name, dir, time),
509  surfaces_(surfaces),
510  refinementLevel_(refinementLevel),
511  surfaceLevels_(surfaceLevels),
512  refinementRegions_(refinementRegions),
513  refinementBoxes_(refinementBoxes),
514  refinementDists_(refinementDists),
515  implicitFeatures_(implicitFeatures),
516  layers_(layers),
517  insidePoint_(insidePoint),
518  nCellsBetweenLevels_(nCellsBetweenLevels)
519 {}
520 
521 
522 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
523 
525 {}
526 
527 
528 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
529 
531 {
532  dict_.writeHeader(os_, word("dictionary"));
533  os_ << "#includeEtc \"caseDicts/mesh/generation/snappyHexMeshDict.cfg\""
534  << nl << endl;
535  writeSnappySwitches();
536  writeSnappyGeometry();
537  writeCastellatedMeshControls();
538  writeSnapControls();
539  writeAddLayersControls();
540  writeWriteFlags();
541 
542  os_ << "mergeTolerance 1e-6;";
543 
544  dict_.writeEndDivider(os_);
545 }
546 
547 
548 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
OFstream os_
Output file stream to write file content.
snappyHexMeshConfiguration(const fileName &name, const fileName &dir, const Time &time, const meshingSurfaceList &surfaces, const label refinementLevel, const List< Tuple2< word, label >> &surfaceLevels, const List< Tuple2< word, label >> &refinementRegions, const List< Tuple3< vector, vector, label >> &refinementBoxes, const List< Tuple3< word, scalar, label >> &refinementDists, const bool implicitFeatures, const label layers, const point &insidePoint, const label nCellsBetweenLevels)
Construct from components.
Foam::word regionName
const char *const group
Group name for atomic constants.
List< word > wordList
A List of words.
Definition: fileName.H:54
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
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:49
vector point
Point is a vector.
Definition: point.H:41
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
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