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-2025 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 "addNoOverwriteOption.H"
65  #include "addNoOverwriteOption.H"
66  #include "addRegionOption.H"
68  argList::validArgs.append("patches");
69  argList::validArgs.append("edgeFraction");
70 
72  (
73  "inSet",
74  "name",
75  "Restrict cells to refine to those in specified cellSet"
76  );
77 
78  #include "setRootCase.H"
80 
81  Foam::word meshRegionName = polyMesh::defaultRegion;
82  args.optionReadIfPresent("region", meshRegionName);
83 
84  #include "createSpecifiedPolyMesh.H"
85  const word oldInstance = mesh.pointsInstance();
86 
87  // Find set of patches from the list of regular expressions provided
88  const wordReList patches((IStringStream(args[1])()));
89  const labelHashSet patchSet(mesh.boundaryMesh().patchSet(patches));
90 
91  const scalar weight = args.argRead<scalar>(2);
92  #include "setNoOverwrite.H"
93 
94  if (!patchSet.size())
95  {
97  << "Cannot find any patches in set " << patches << endl
98  << "Valid patches are " << mesh.boundaryMesh().names()
99  << exit(FatalError);
100  }
101 
102  label nPatchFaces = 0;
103  label nPatchEdges = 0;
104 
105  forAllConstIter(labelHashSet, patchSet, iter)
106  {
107  nPatchFaces += mesh.boundaryMesh()[iter.key()].size();
108  nPatchEdges += mesh.boundaryMesh()[iter.key()].nEdges();
109  }
110 
111  // Construct from estimate for the number of cells to refine
112  labelHashSet cutCells(4*nPatchFaces);
113 
114  // Construct from total patch edges in selected patches
115  DynamicList<label> allCutEdges(nPatchEdges);
116  DynamicList<scalar> allCutEdgeWeights(nPatchEdges);
117 
118  // Find cells to refine
119  forAllConstIter(labelHashSet, patchSet, iter)
120  {
121  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
122  const labelList& meshPoints = pp.meshPoints();
123 
124  forAll(meshPoints, pointi)
125  {
126  const label meshPointi = meshPoints[pointi];
127 
128  const labelList& pCells = mesh.pointCells()[meshPointi];
129 
130  forAll(pCells, pCelli)
131  {
132  cutCells.insert(pCells[pCelli]);
133  }
134  }
135  }
136 
137  word setName;
138  if (args.optionReadIfPresent("inSet", setName))
139  {
140  Info<< "Restrict cells to refine to those in cellSet "
141  << setName << endl;
142 
143  const cellSet cellsToRefine(mesh, setName);
144 
145  Info<< " Read " << cellsToRefine.size()
146  << " cells from cellSet " << cellsToRefine.relativeObjectPath()
147  << nl << endl;
148 
149  forAllIter(labelHashSet, cutCells, iter)
150  {
151  if (!cellsToRefine.found(iter.key()))
152  {
153  cutCells.erase(iter);
154  }
155  }
156  }
157 
158  // Mark all mesh points on patch
159  boolList vertOnPatch(mesh.nPoints(), false);
160 
161  forAllConstIter(labelHashSet, patchSet, iter)
162  {
163  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
164  const labelList& meshPoints = pp.meshPoints();
165 
166  forAll(meshPoints, pointi)
167  {
168  vertOnPatch[meshPoints[pointi]] = true;
169  }
170  }
171 
172  forAllConstIter(labelHashSet, patchSet, iter)
173  {
174  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
175  const labelList& meshPoints = pp.meshPoints();
176 
177  forAll(meshPoints, pointi)
178  {
179  const label meshPointi = meshPoints[pointi];
180 
181  const labelList& pEdges = mesh.pointEdges()[meshPointi];
182 
183  forAll(pEdges, pEdgeI)
184  {
185  const label edgeI = pEdges[pEdgeI];
186  const edge& e = mesh.edges()[edgeI];
187 
188  label otherPointi = e.otherVertex(meshPointi);
189 
190  if (!vertOnPatch[otherPointi])
191  {
192  allCutEdges.append(edgeI);
193 
194  if (e.start() == meshPointi)
195  {
196  allCutEdgeWeights.append(weight);
197  }
198  else
199  {
200  allCutEdgeWeights.append(1 - weight);
201  }
202  }
203  }
204  }
205  }
206 
207  allCutEdges.shrink();
208  allCutEdgeWeights.shrink();
209 
210  Info<< "Refining:" << nl
211  << " cells:" << cutCells.size() << nl
212  << " edges:" << allCutEdges.size() << endl;
213 
214  // Transfer DynamicLists to straight ones.
215  scalarField cutEdgeWeights;
216  cutEdgeWeights.transfer(allCutEdgeWeights);
217  allCutEdgeWeights.clear();
218 
219 
220  // Gets cuts across cells from cuts through edges.
221  cellCuts cuts
222  (
223  mesh,
224  cutCells.toc(), // cells candidate for cutting
225  labelList(0), // cut vertices
226  allCutEdges, // cut edges
227  cutEdgeWeights // weight on cut edges
228  );
229 
230  polyTopoChange meshMod(mesh);
231 
232  // Cutting engine
233  meshCutter cutter(mesh);
234 
235  // Insert mesh refinement into polyTopoChange.
236  cutter.setRefinement(cuts, meshMod);
237 
238  if (!overwrite)
239  {
240  runTime++;
241  }
242 
243  autoPtr<polyTopoChangeMap> map = meshMod.changeMesh(mesh);
244 
245  // Update stored labels on meshCutter.
246  cutter.topoChange(map());
247 
248  Info<< "Finished refining" << endl;
249 
250  if (overwrite)
251  {
252  mesh.setInstance(oldInstance);
253  }
254 
255  // Write resulting mesh
256  Info<< "Writing refined mesh to time " << runTime.name() << endl;
257 
258  mesh.write();
259 
260  Info<< "End\n" << endl;
261 
262  return 0;
263 }
264 
265 
266 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:458
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
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
const labelList & meshPoints() const
Return labelList of mesh points in patch. They are constructed.
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
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 optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:255
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:234
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
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1785
Cuts (splits) cells.
Definition: meshCutter.H:131
wordList toc(const word &className) const
Return the list of names of IOobjects of given class name.
labelHashSet patchSet(const UList< wordRe > &patchNames, const bool warnNotFound=true, const bool usePatchGroups=true) const
Return the patch set corresponding to the given names.
wordList names() const
Return the list of patch names.
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:270
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:988
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:401
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:91
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:70
Direct mesh changes based on v1.3 polyTopoChange syntax.
const labelListList & pointEdges() const
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
const labelListList & pointCells() const
label nPoints() const
A class for handling words, derived from string.
Definition: word.H:62
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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:106
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
messageStream Info
error FatalError
static const char nl
Definition: Ostream.H:267
const bool overwrite
Definition: setNoOverwrite.H:1
Foam::argList args(argc, argv)