refineWallLayer.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) 2011-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 Application
25  refineWallLayer
26 
27 Description
28  Utility to refine cells next to patches.
29 
30  Arguments:
31  1: List of patch name regular expressions
32  2: The size of the refined cells as a fraction of the edge-length.
33 
34  Examples:
35  Split the near-wall cells of patch Wall in the middle
36  refineWallLayer "(Wall)" 0.5
37 
38  Split the near-wall cells of patch Wall in the middle
39  within the cellSet box
40  refineWallLayer "(Wall)" 0.5 -inSet box
41 
42  Split the near-wall cells of patches Wall1 and Wall2 in the middle
43  refineWallLayer "(Wall1 Wall2)" 0.5
44 
45  Split the near-wall cells of all patches with names beginning with wall
46  with the near-wall cells 10% of the thickness of the original cells
47  refineWallLayer '("Wall.*")' 0.1
48 
49 \*---------------------------------------------------------------------------*/
50 
51 #include "argList.H"
52 #include "Time.H"
53 #include "polyTopoChange.H"
54 #include "cellCuts.H"
55 #include "cellSet.H"
56 #include "meshCutter.H"
57 
58 using namespace Foam;
59 
60 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 
62 int main(int argc, char *argv[])
63 {
64  #include "addOverwriteOption.H"
65  #include "addRegionOption.H"
67  argList::validArgs.append("patches");
68  argList::validArgs.append("edgeFraction");
69 
71  (
72  "inSet",
73  "name",
74  "Restrict cells to refine to those in specified cellSet"
75  );
76 
77  #include "setRootCase.H"
79 
80  Foam::word meshRegionName = polyMesh::defaultRegion;
81  args.optionReadIfPresent("region", meshRegionName);
82 
83  #include "createNamedPolyMesh.H"
84  const word oldInstance = mesh.pointsInstance();
85 
86  // Find set of patches from the list of regular expressions provided
87  const wordReList patches((IStringStream(args[1])()));
88  const labelHashSet patchSet(mesh.boundaryMesh().patchSet(patches));
89 
90  const scalar weight = args.argRead<scalar>(2);
91  const bool overwrite = args.optionFound("overwrite");
92 
93  if (!patchSet.size())
94  {
96  << "Cannot find any patches in set " << patches << endl
97  << "Valid patches are " << mesh.boundaryMesh().names()
98  << exit(FatalError);
99  }
100 
101  label nPatchFaces = 0;
102  label nPatchEdges = 0;
103 
104  forAllConstIter(labelHashSet, patchSet, iter)
105  {
106  nPatchFaces += mesh.boundaryMesh()[iter.key()].size();
107  nPatchEdges += mesh.boundaryMesh()[iter.key()].nEdges();
108  }
109 
110  // Construct from estimate for the number of cells to refine
111  labelHashSet cutCells(4*nPatchFaces);
112 
113  // Construct from total patch edges in selected patches
114  DynamicList<label> allCutEdges(nPatchEdges);
115  DynamicList<scalar> allCutEdgeWeights(nPatchEdges);
116 
117  // Find cells to refine
118  forAllConstIter(labelHashSet, patchSet, iter)
119  {
120  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
121  const labelList& meshPoints = pp.meshPoints();
122 
123  forAll(meshPoints, pointi)
124  {
125  const label meshPointi = meshPoints[pointi];
126 
127  const labelList& pCells = mesh.pointCells()[meshPointi];
128 
129  forAll(pCells, pCelli)
130  {
131  cutCells.insert(pCells[pCelli]);
132  }
133  }
134  }
135 
136  word setName;
137  if (args.optionReadIfPresent("inSet", setName))
138  {
139  Info<< "Restrict cells to refine to those in cellSet "
140  << setName << endl;
141 
142  const cellSet cellsToRefine(mesh, setName);
143 
144  Info<< " Read " << cellsToRefine.size()
145  << " cells from cellSet " << cellsToRefine.relativeObjectPath()
146  << nl << endl;
147 
148  forAllIter(labelHashSet, cutCells, iter)
149  {
150  if (!cellsToRefine.found(iter.key()))
151  {
152  cutCells.erase(iter);
153  }
154  }
155  }
156 
157  // Mark all mesh points on patch
158  boolList vertOnPatch(mesh.nPoints(), false);
159 
160  forAllConstIter(labelHashSet, patchSet, iter)
161  {
162  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
163  const labelList& meshPoints = pp.meshPoints();
164 
165  forAll(meshPoints, pointi)
166  {
167  vertOnPatch[meshPoints[pointi]] = true;
168  }
169  }
170 
171  forAllConstIter(labelHashSet, patchSet, iter)
172  {
173  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
174  const labelList& meshPoints = pp.meshPoints();
175 
176  forAll(meshPoints, pointi)
177  {
178  const label meshPointi = meshPoints[pointi];
179 
180  const labelList& pEdges = mesh.pointEdges()[meshPointi];
181 
182  forAll(pEdges, pEdgeI)
183  {
184  const label edgeI = pEdges[pEdgeI];
185  const edge& e = mesh.edges()[edgeI];
186 
187  label otherPointi = e.otherVertex(meshPointi);
188 
189  if (!vertOnPatch[otherPointi])
190  {
191  allCutEdges.append(edgeI);
192 
193  if (e.start() == meshPointi)
194  {
195  allCutEdgeWeights.append(weight);
196  }
197  else
198  {
199  allCutEdgeWeights.append(1 - weight);
200  }
201  }
202  }
203  }
204  }
205 
206  allCutEdges.shrink();
207  allCutEdgeWeights.shrink();
208 
209  Info<< "Refining:" << nl
210  << " cells:" << cutCells.size() << nl
211  << " edges:" << allCutEdges.size() << endl;
212 
213  // Transfer DynamicLists to straight ones.
214  scalarField cutEdgeWeights;
215  cutEdgeWeights.transfer(allCutEdgeWeights);
216  allCutEdgeWeights.clear();
217 
218 
219  // Gets cuts across cells from cuts through edges.
220  cellCuts cuts
221  (
222  mesh,
223  cutCells.toc(), // cells candidate for cutting
224  labelList(0), // cut vertices
225  allCutEdges, // cut edges
226  cutEdgeWeights // weight on cut edges
227  );
228 
229  polyTopoChange meshMod(mesh);
230 
231  // Cutting engine
232  meshCutter cutter(mesh);
233 
234  // Insert mesh refinement into polyTopoChange.
235  cutter.setRefinement(cuts, meshMod);
236 
237  if (!overwrite)
238  {
239  runTime++;
240  }
241 
242  autoPtr<polyTopoChangeMap> map = meshMod.changeMesh(mesh, false);
243 
244  if (map().hasMotionPoints())
245  {
246  mesh.setPoints(map().preMotionPoints());
247  }
248 
249  // Update stored labels on meshCutter.
250  cutter.topoChange(map());
251 
252  Info<< "Finished refining" << endl;
253 
254  if (overwrite)
255  {
256  mesh.setInstance(oldInstance);
257  }
258 
259  // Write resulting mesh
260  Info<< "Writing refined mesh to time " << runTime.name() << endl;
261 
262  mesh.write();
263 
264  Info<< "End\n" << endl;
265 
266  return 0;
267 }
268 
269 
270 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:459
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
Input from memory buffer stream.
Definition: IStringStream.H:52
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
virtual Ostream & write(const char)=0
Write character.
const labelList & meshPoints() const
Return labelList of mesh points in patch. They are constructed.
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
static void noParallel()
Remove the parallel options.
Definition: argList.C:175
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:183
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Description of cuts across cells.
Definition: cellCuts.H:111
A collection of cell labels.
Definition: cellSet.H:51
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:61
Cuts (splits) cells.
Definition: meshCutter.H:137
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:268
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:70
const polyBoundaryMesh & boundaryMesh() const
Return boundaryMesh reference.
Definition: polyPatch.C:270
Direct mesh changes based on v1.3 polyTopoChange syntax.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int main(int argc, char *argv[])
Definition: financialFoam.C:44
const fvPatchList & patches
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const doubleScalar e
Definition: doubleScalar.H:105
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
messageStream Info
error FatalError
static const char nl
Definition: Ostream.H:260
Foam::argList args(argc, argv)