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-2018 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 patches Wall1 and Wall2 in the middle
39  refineWallLayer "(Wall1 Wall2)" 0.5
40 
41  Split the near-wall cells of all patches with names beginning with wall
42  with the near-wall cells 10% of the thickness of the original cells
43  refineWallLayer '("Wall.*")' 0.1
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #include "argList.H"
48 #include "Time.H"
49 #include "polyTopoChange.H"
50 #include "cellCuts.H"
51 #include "cellSet.H"
52 #include "meshCutter.H"
53 
54 using namespace Foam;
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 int main(int argc, char *argv[])
59 {
60  #include "addOverwriteOption.H"
61  #include "addRegionOption.H"
63  argList::validArgs.append("patches");
64  argList::validArgs.append("edgeFraction");
65 
67  (
68  "useSet",
69  "name",
70  "restrict cells to refine based on specified cellSet name"
71  );
72 
73  #include "setRootCase.H"
74  #include "createTime.H"
76 
77  Foam::word meshRegionName = polyMesh::defaultRegion;
78  args.optionReadIfPresent("region", meshRegionName);
79 
80  #include "createNamedPolyMesh.H"
81  const word oldInstance = mesh.pointsInstance();
82 
83  // Find set of patches from the list of regular expressions provided
84  const wordReList patches((IStringStream(args[1])()));
85  const labelHashSet patchSet(mesh.boundaryMesh().patchSet(patches));
86 
87  const scalar weight = args.argRead<scalar>(2);
88  const bool overwrite = args.optionFound("overwrite");
89 
90  if (!patchSet.size())
91  {
93  << "Cannot find any patches in set " << patches << endl
94  << "Valid patches are " << mesh.boundaryMesh().names()
95  << exit(FatalError);
96  }
97 
98  label nPatchFaces = 0;
99  label nPatchEdges = 0;
100 
101  forAllConstIter(labelHashSet, patchSet, iter)
102  {
103  nPatchFaces += mesh.boundaryMesh()[iter.key()].size();
104  nPatchEdges += mesh.boundaryMesh()[iter.key()].nEdges();
105  }
106 
107  // Construct from estimate for the number of cells to refine
108  labelHashSet cutCells(4*nPatchFaces);
109 
110  // Construct from total patch edges in selected patches
111  DynamicList<label> allCutEdges(nPatchEdges);
112  DynamicList<scalar> allCutEdgeWeights(nPatchEdges);
113 
114  // Find cells to refine
115  forAllConstIter(labelHashSet, patchSet, iter)
116  {
117  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
118  const labelList& meshPoints = pp.meshPoints();
119 
120  forAll(meshPoints, pointi)
121  {
122  label meshPointi = meshPoints[pointi];
123 
124  const labelList& pCells = mesh.pointCells()[meshPointi];
125 
126  forAll(pCells, pCelli)
127  {
128  cutCells.insert(pCells[pCelli]);
129  }
130  }
131  }
132 
133  // Edit list of cells to refine according to specified set
134  word setName;
135  if (args.optionReadIfPresent("useSet", setName))
136  {
137  Info<< "Subsetting cells to cut based on cellSet"
138  << setName << nl << endl;
139 
140  cellSet cells(mesh, setName);
141 
142  Info<< "Read " << cells.size() << " cells from cellSet "
143  << cells.instance()/cells.local()/cells.name()
144  << nl << endl;
145 
147  {
148  cutCells.erase(iter.key());
149  }
150  Info<< "Removed from cells to cut all the ones not in set "
151  << setName << nl << endl;
152  }
153 
154  // Mark all mesh points on patch
155  boolList vertOnPatch(mesh.nPoints(), false);
156 
157  forAllConstIter(labelHashSet, patchSet, iter)
158  {
159  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
160  const labelList& meshPoints = pp.meshPoints();
161 
162  forAll(meshPoints, pointi)
163  {
164  vertOnPatch[meshPoints[pointi]] = true;
165  }
166  }
167 
168  forAllConstIter(labelHashSet, patchSet, iter)
169  {
170  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
171  const labelList& meshPoints = pp.meshPoints();
172 
173  forAll(meshPoints, pointi)
174  {
175  label meshPointi = meshPoints[pointi];
176 
177  const labelList& pEdges = mesh.pointEdges()[meshPointi];
178 
179  forAll(pEdges, pEdgeI)
180  {
181  const label edgeI = pEdges[pEdgeI];
182  const edge& e = mesh.edges()[edgeI];
183 
184  label otherPointi = e.otherVertex(meshPointi);
185 
186  if (!vertOnPatch[otherPointi])
187  {
188  allCutEdges.append(edgeI);
189 
190  if (e.start() == meshPointi)
191  {
192  allCutEdgeWeights.append(weight);
193  }
194  else
195  {
196  allCutEdgeWeights.append(1 - weight);
197  }
198  }
199  }
200  }
201  }
202 
203  allCutEdges.shrink();
204  allCutEdgeWeights.shrink();
205 
206  Info<< "Refining:" << nl
207  << " cells:" << cutCells.size() << nl
208  << " edges:" << allCutEdges.size() << endl;
209 
210  // Transfer DynamicLists to straight ones.
211  scalarField cutEdgeWeights;
212  cutEdgeWeights.transfer(allCutEdgeWeights);
213  allCutEdgeWeights.clear();
214 
215 
216  // Gets cuts across cells from cuts through edges.
217  cellCuts cuts
218  (
219  mesh,
220  cutCells.toc(), // cells candidate for cutting
221  labelList(0), // cut vertices
222  allCutEdges, // cut edges
223  cutEdgeWeights // weight on cut edges
224  );
225 
226  polyTopoChange meshMod(mesh);
227 
228  // Cutting engine
229  meshCutter cutter(mesh);
230 
231  // Insert mesh refinement into polyTopoChange.
232  cutter.setRefinement(cuts, meshMod);
233 
234  if (!overwrite)
235  {
236  runTime++;
237  }
238 
239  autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
240 
241  if (morphMap().hasMotionPoints())
242  {
243  mesh.movePoints(morphMap().preMotionPoints());
244  }
245 
246  // Update stored labels on meshCutter.
247  cutter.updateMesh(morphMap());
248 
249  Info<< "Finished refining" << endl;
250 
251  if (overwrite)
252  {
253  mesh.setInstance(oldInstance);
254  }
255 
256  // Write resulting mesh
257  Info<< "Writing refined mesh to time " << runTime.timeName() << endl;
258 
259  mesh.write();
260 
261  Info<< "End\n" << endl;
262 
263  return 0;
264 }
265 
266 
267 // ************************************************************************* //
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:434
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void off()
Switch the function objects off.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
const labelListList & pointEdges() const
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
engineTime & runTime
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:309
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
static void noParallel()
Remove the parallel options.
Definition: argList.C:174
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:626
patches[0]
label otherVertex(const label a) const
Given one vertex, return the other.
Definition: edgeI.H:140
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1035
Description of cuts across cells.
Definition: cellCuts.H:108
labelHashSet patchSet(const UList< wordRe > &patchNames, const bool warnNotFound=true, const bool usePatchGroups=true) const
Return the set of patch IDs corresponding to the given names.
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
const labelList & meshPoints() const
Return labelList of mesh points in patch. They are constructed.
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:802
dynamicFvMesh & mesh
const cellShapeList & cells
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
A class for handling words, derived from string.
Definition: word.H:59
wordList names() const
Return a list of patch names.
Cuts (splits) cells.
Definition: meshCutter.H:134
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:127
List< label > labelList
A List of labels.
Definition: labelList.H:56
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
const labelListList & pointCells() const
static const char nl
Definition: Ostream.H:265
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:32
virtual tmp< scalarField > movePoints(const pointField &)
Move points, returns volumes swept by faces in motion.
Definition: fvMesh.C:716
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:410
Input from memory buffer stream.
Definition: IStringStream.H:49
A collection of cell labels.
Definition: cellSet.H:48
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:177
Direct mesh changes based on v1.3 polyTopoChange syntax.
messageStream Info
label nPoints() const
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:202
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
Foam::argList args(argc, argv)
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
label start() const
Return start vertex label.
Definition: edgeI.H:81
Namespace for OpenFOAM.