refineWallLayer.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 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"
62  argList::validArgs.append("patches");
63  argList::validArgs.append("edgeFraction");
64 
66  (
67  "useSet",
68  "name",
69  "restrict cells to refine based on specified cellSet name"
70  );
71 
72 
73  #include "setRootCase.H"
74  #include "createTime.H"
75  runTime.functionObjects().off();
76 
77  #include "createPolyMesh.H"
78  const word oldInstance = mesh.pointsInstance();
79 
80  // Find set of patches from the list of regular expressions provided
81  const wordReList patches((IStringStream(args[1])()));
82  const labelHashSet patchSet(mesh.boundaryMesh().patchSet(patches));
83 
84  const scalar weight = args.argRead<scalar>(2);
85  const bool overwrite = args.optionFound("overwrite");
86 
87  if (!patchSet.size())
88  {
90  << "Cannot find any patches in set " << patches << endl
91  << "Valid patches are " << mesh.boundaryMesh().names()
92  << exit(FatalError);
93  }
94 
95  label nPatchFaces = 0;
96  label nPatchEdges = 0;
97 
98  forAllConstIter(labelHashSet, patchSet, iter)
99  {
100  nPatchFaces += mesh.boundaryMesh()[iter.key()].size();
101  nPatchEdges += mesh.boundaryMesh()[iter.key()].nEdges();
102  }
103 
104  // Construct from estimate for the number of cells to refine
105  labelHashSet cutCells(4*nPatchFaces);
106 
107  // Construct from total patch edges in selected patches
108  DynamicList<label> allCutEdges(nPatchEdges);
109  DynamicList<scalar> allCutEdgeWeights(nPatchEdges);
110 
111  // Find cells to refine
112  forAllConstIter(labelHashSet, patchSet, iter)
113  {
114  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
115  const labelList& meshPoints = pp.meshPoints();
116 
117  forAll(meshPoints, pointi)
118  {
119  label meshPointi = meshPoints[pointi];
120 
121  const labelList& pCells = mesh.pointCells()[meshPointi];
122 
123  forAll(pCells, pCelli)
124  {
125  cutCells.insert(pCells[pCelli]);
126  }
127  }
128  }
129 
130  // Edit list of cells to refine according to specified set
131  word setName;
132  if (args.optionReadIfPresent("useSet", setName))
133  {
134  Info<< "Subsetting cells to cut based on cellSet"
135  << setName << nl << endl;
136 
137  cellSet cells(mesh, setName);
138 
139  Info<< "Read " << cells.size() << " cells from cellSet "
140  << cells.instance()/cells.local()/cells.name()
141  << nl << endl;
142 
144  {
145  cutCells.erase(iter.key());
146  }
147  Info<< "Removed from cells to cut all the ones not in set "
148  << setName << nl << endl;
149  }
150 
151  // Mark all mesh points on patch
152  boolList vertOnPatch(mesh.nPoints(), false);
153 
154  forAllConstIter(labelHashSet, patchSet, iter)
155  {
156  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
157  const labelList& meshPoints = pp.meshPoints();
158 
159  forAll(meshPoints, pointi)
160  {
161  vertOnPatch[meshPoints[pointi]] = true;
162  }
163  }
164 
165  forAllConstIter(labelHashSet, patchSet, iter)
166  {
167  const polyPatch& pp = mesh.boundaryMesh()[iter.key()];
168  const labelList& meshPoints = pp.meshPoints();
169 
170  forAll(meshPoints, pointi)
171  {
172  label meshPointi = meshPoints[pointi];
173 
174  const labelList& pEdges = mesh.pointEdges()[meshPointi];
175 
176  forAll(pEdges, pEdgeI)
177  {
178  const label edgeI = pEdges[pEdgeI];
179  const edge& e = mesh.edges()[edgeI];
180 
181  label otherPointi = e.otherVertex(meshPointi);
182 
183  if (!vertOnPatch[otherPointi])
184  {
185  allCutEdges.append(edgeI);
186 
187  if (e.start() == meshPointi)
188  {
189  allCutEdgeWeights.append(weight);
190  }
191  else
192  {
193  allCutEdgeWeights.append(1 - weight);
194  }
195  }
196  }
197  }
198  }
199 
200  allCutEdges.shrink();
201  allCutEdgeWeights.shrink();
202 
203  Info<< "Refining:" << nl
204  << " cells:" << cutCells.size() << nl
205  << " edges:" << allCutEdges.size() << endl;
206 
207  // Transfer DynamicLists to straight ones.
208  scalarField cutEdgeWeights;
209  cutEdgeWeights.transfer(allCutEdgeWeights);
210  allCutEdgeWeights.clear();
211 
212 
213  // Gets cuts across cells from cuts through edges.
214  cellCuts cuts
215  (
216  mesh,
217  cutCells.toc(), // cells candidate for cutting
218  labelList(0), // cut vertices
219  allCutEdges, // cut edges
220  cutEdgeWeights // weight on cut edges
221  );
222 
223  polyTopoChange meshMod(mesh);
224 
225  // Cutting engine
226  meshCutter cutter(mesh);
227 
228  // Insert mesh refinement into polyTopoChange.
229  cutter.setRefinement(cuts, meshMod);
230 
231  if (!overwrite)
232  {
233  runTime++;
234  }
235 
236  autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
237 
238  if (morphMap().hasMotionPoints())
239  {
240  mesh.movePoints(morphMap().preMotionPoints());
241  }
242 
243  // Update stored labels on meshCutter.
244  cutter.updateMesh(morphMap());
245 
246  Info<< "Finished refining" << endl;
247 
248  if (overwrite)
249  {
250  mesh.setInstance(oldInstance);
251  }
252 
253  // Write resulting mesh
254  Info<< "Writing refined mesh to time " << runTime.timeName() << endl;
255 
256  mesh.write();
257 
258  Info<< "End\n" << endl;
259 
260  return 0;
261 }
262 
263 
264 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
wordList names() const
Return a list of patch names.
const labelList & meshPoints() const
Return labelList of mesh points in patch. They are constructed.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
label otherVertex(const label a) const
Given one vertex, return the other.
Definition: edgeI.H:103
patches[0]
Description of cuts across cells.
Definition: cellCuts.H:108
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
dynamicFvMesh & mesh
const labelListList & pointCells() const
const cellShapeList & cells
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
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
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:93
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.
List< label > labelList
A List of labels.
Definition: labelList.H:56
const labelListList & pointEdges() const
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:198
label start() const
Return start vertex label.
Definition: edgeI.H:81
static const char nl
Definition: Ostream.H:262
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:177
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:721
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:772
Input from memory buffer stream.
Definition: IStringStream.H:49
A collection of cell labels.
Definition: cellSet.H:48
Direct mesh changes based on v1.3 polyTopoChange syntax.
label nPoints() const
messageStream Info
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
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:365
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:870
Namespace for OpenFOAM.
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29