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-2015 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 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
meshCutter.H
Foam::meshCutter
Cuts (splits) cells.
Definition: meshCutter.H:134
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::HashTable::toc
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:201
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::DynamicList< label >
addOverwriteOption.H
polyTopoChange.H
Foam::polyTopoChange
Direct mesh changes based on v1.3 polyTopoChange syntax.
Definition: polyTopoChange.H:97
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
Foam::fvMesh::movePoints
virtual tmp< scalarField > movePoints(const pointField &)
Move points, returns volumes swept by faces in motion.
Definition: fvMesh.C:726
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::primitiveMesh::edges
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
Definition: primitiveMeshEdges.C:353
Foam::primitiveMesh::pointEdges
const labelListList & pointEdges() const
Definition: primitiveMeshPointEdges.C:96
Foam::HashSet< label, Hash< label > >
Foam::polyBoundaryMesh::names
wordList names() const
Return a list of patch names.
Definition: polyBoundaryMesh.C:527
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::fvMesh::write
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:873
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:772
Foam::primitiveMesh::nPoints
label nPoints() const
Definition: primitiveMeshI.H:35
Foam::label
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
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
argList.H
main
int main(int argc, char *argv[])
Definition: postCalc.C:54
cellCuts.H
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::cellSet
A collection of cell labels.
Definition: cellSet.H:48
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::polyMesh::setInstance
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:32
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::PtrList::size
label size() const
Return the number of elements in the PtrList.
Definition: PtrListI.H:32
Foam::primitiveMesh::pointCells
const labelListList & pointCells() const
Definition: primitiveMeshPointCells.C:108
createTime.H
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::argList::argRead
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:177
patches
patches[0]
Definition: createSingleCellMesh.H:36
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
cellSet.H
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
Foam::polyBoundaryMesh::patchSet
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.
Definition: polyBoundaryMesh.C:750
Foam::PrimitivePatch::meshPoints
const labelList & meshPoints() const
Return labelList of mesh points in patch.
Definition: PrimitivePatchTemplate.C:392
args
Foam::argList args(argc, argv)
Foam::argList::optionReadIfPresent
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
createPolyMesh.H
Foam::cellCuts
Description of cuts across cells.
Definition: cellCuts.H:108