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-2020 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"
78  #include "createTime.H"
80 
81  Foam::word meshRegionName = polyMesh::defaultRegion;
82  args.optionReadIfPresent("region", meshRegionName);
83 
84  #include "createNamedPolyMesh.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  const bool overwrite = args.optionFound("overwrite");
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.localObjectPath()
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<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
244 
245  if (morphMap().hasMotionPoints())
246  {
247  mesh.movePoints(morphMap().preMotionPoints());
248  }
249 
250  // Update stored labels on meshCutter.
251  cutter.updateMesh(morphMap());
252 
253  Info<< "Finished refining" << endl;
254 
255  if (overwrite)
256  {
257  mesh.setInstance(oldInstance);
258  }
259 
260  // Write resulting mesh
261  Info<< "Writing refined mesh to time " << runTime.timeName() << endl;
262 
263  mesh.write();
264 
265  Info<< "End\n" << endl;
266 
267  return 0;
268 }
269 
270 
271 // ************************************************************************* //
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
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:459
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:251
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
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
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:622
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:204
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
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:128
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:260
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:409
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:183
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.