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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2018 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  refineWallLayer
29 
30 Group
31  grpMeshAdvancedUtilities
32 
33 Description
34  Refine cells next to specified patches.
35 
36  Arguments:
37  1: List of patch names or regular expressions
38  2: The size of the refined cells as a fraction of the edge-length.
39 
40  Examples:
41  Split the near-wall cells of patch Wall in the middle
42  refineWallLayer "(Wall)" 0.5
43 
44  Split the near-wall cells of patches Wall1 and Wall2 in the middle
45  refineWallLayer "(Wall1 Wall2)" 0.5
46 
47  Split the near-wall cells of all patches with names beginning with wall
48  with the near-wall cells 10% of the thickness of the original cells
49  refineWallLayer '("Wall.*")' 0.1
50 
51 \*---------------------------------------------------------------------------*/
52 
53 #include "argList.H"
54 #include "Time.H"
55 #include "polyTopoChange.H"
56 #include "cellCuts.H"
57 #include "cellSet.H"
58 #include "meshCutter.H"
59 #include "processorMeshes.H"
60 
61 using namespace Foam;
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 int main(int argc, char *argv[])
66 {
68  (
69  "Refine cells next to specified patches."
70  );
71 
72  #include "addOverwriteOption.H"
74  (
75  "patches",
76  "The list of patch names or regex - Eg, '(top \"Wall.\")'"
77  );
79  (
80  "edgeFraction",
81  "The size of the refined cells as a fraction of the edge-length"
82  " on a (0,1) interval"
83  );
84 
86  (
87  "useSet",
88  "name",
89  "Restrict cells to refine based on specified cellSet name"
90  );
91 
92  argList::noFunctionObjects(); // Never use function objects
93 
94  #include "setRootCase.H"
95  #include "createTime.H"
96  #include "createPolyMesh.H"
97 
98  const word oldInstance = mesh.pointsInstance();
99 
100  // Find set of patches from the list of regular expressions provided
101  const wordRes patches(args.getList<wordRe>(1));
102  const scalar weight = args.get<scalar>(2);
103  const bool overwrite = args.found("overwrite");
104 
105  const labelHashSet patchSet(mesh.boundaryMesh().patchSet(patches));
106  if (!patchSet.size())
107  {
109  << "Cannot find any patches in set " << patches << endl
110  << "Valid patches are " << mesh.boundaryMesh().names()
111  << exit(FatalError);
112  }
113 
114  label nPatchFaces = 0;
115  label nPatchEdges = 0;
116 
117  for (const label patchi : patchSet)
118  {
119  nPatchFaces += mesh.boundaryMesh()[patchi].size();
120  nPatchEdges += mesh.boundaryMesh()[patchi].nEdges();
121  }
122 
123  // Construct from estimate for the number of cells to refine
124  labelHashSet cutCells(4*nPatchFaces);
125 
126  // Construct from total patch edges in selected patches
127  DynamicList<label> allCutEdges(nPatchEdges);
128  DynamicList<scalar> allCutEdgeWeights(nPatchEdges);
129 
130  // Find cells to refine
131  for (const label patchi : patchSet)
132  {
133  const polyPatch& pp = mesh.boundaryMesh()[patchi];
134  const labelList& meshPoints = pp.meshPoints();
135 
136  for (const label meshPointi : meshPoints)
137  {
138  const labelList& pCells = mesh.pointCells()[meshPointi];
139 
140  cutCells.insert(pCells);
141  }
142  }
143 
144  // Edit list of cells to refine according to specified set
145  word setName;
146  if (args.readIfPresent("useSet", setName))
147  {
148  Info<< "Subsetting cells to cut based on cellSet"
149  << setName << nl << endl;
150 
151  cellSet cells(mesh, setName);
152 
153  Info<< "Read " << cells.size() << " cells from cellSet "
154  << cells.instance()/cells.local()/cells.name()
155  << nl << endl;
156 
157 
158  cutCells.retain(cells);
159 
160  Info<< "Removed from cells to cut all the ones not in set "
161  << setName << nl << endl;
162  }
163 
164  // Mark all mesh points on patch
165  bitSet vertOnPatch(mesh.nPoints());
166 
167  for (const label patchi : patchSet)
168  {
169  const polyPatch& pp = mesh.boundaryMesh()[patchi];
170  const labelList& meshPoints = pp.meshPoints();
171 
172  vertOnPatch.set(meshPoints);
173  }
174 
175  for (const label patchi : patchSet)
176  {
177  const polyPatch& pp = mesh.boundaryMesh()[patchi];
178  const labelList& meshPoints = pp.meshPoints();
179 
180  for (const label meshPointi : meshPoints)
181  {
182  const labelList& pEdges = mesh.pointEdges()[meshPointi];
183 
184  for (const label edgei : pEdges)
185  {
186  const edge& e = mesh.edges()[edgei];
187 
188  label otherPointi = e.otherVertex(meshPointi);
189 
190  if (!vertOnPatch.test(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(), // 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();
266 
267  Info<< "End\n" << endl;
268 
269  return 0;
270 }
271 
272 
273 // ************************************************************************* //
static void noFunctionObjects(bool addWithOption=false)
Remove &#39;-noFunctionObjects&#39; option and ignore any occurrences.
Definition: argList.C:547
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
Required Variables.
static void removeFiles(const polyMesh &mesh)
Helper: remove all procAddressing files from mesh instance.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
label nPoints() const noexcept
Number of mesh points.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
const labelListList & pointEdges() const
labelHashSet patchSet(const UList< wordRe > &select, const bool warnNotFound=true, const bool useGroups=true) const
Return the set of patch IDs corresponding to the given names.
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition: List.H:493
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual void movePoints(const pointField &)
Move points, returns volumes swept by faces in motion.
Definition: fvMesh.C:929
List< T > getList(const label index) const
Get a List of values from the argument at index.
Description of cuts across cells.
Definition: cellCuts.H:106
const labelList & meshPoints() const
Return labelList of mesh points in patch.
static void removeFiles(const polyMesh &)
Helper: remove all sets files from mesh instance.
Definition: topoSet.C:693
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:853
dynamicFvMesh & mesh
const cellShapeList & cells
An edge is a list of two vertex labels. This can correspond to a directed graph edge or an edge on a ...
Definition: edge.H:59
const polyBoundaryMesh & boundaryMesh() const noexcept
Return boundary mesh.
Definition: polyMesh.H:609
A class for handling words, derived from Foam::string.
Definition: word.H:63
wordList names() const
Return a list of patch names.
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
Cuts (splits) cells.
Definition: meshCutter.H:134
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:385
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
const labelListList & pointCells() const
virtual bool write(const bool writeOnProc=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1113
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
void setInstance(const fileName &instance, const IOobjectOption::writeOption wOpt=IOobject::AUTO_WRITE)
Set the instance for mesh files.
Definition: polyMeshIO.C:29
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
A collection of cell labels.
Definition: cellSet.H:47
Direct mesh changes based on v1.3 polyTopoChange syntax.
const polyBoundaryMesh & patches
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:351
messageStream Info
Information stream (stdout output on master, null elsewhere)
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:316
List< Key > toc() const
The table of contents (the keys) in unsorted order.
Definition: HashTable.C:148
List< label > labelList
A List of labels.
Definition: List.H:62
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:69
Foam::argList args(argc, argv)
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
Namespace for OpenFOAM.