FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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  Takes a patchName and number of layers to refine. Works out cells within
31  these layers and refines those in the wall-normal direction.
32 
33 Usage
34 
35  - refineWallLayer [OPTIONS] <patchName> <edgeWeight>
36 
37  @param <patchName> \n
38  @todo Detailed description of argument.
39 
40  @param <edgeWeight> \n
41  @todo Detailed description of argument.
42 
43  @param -useSet <cellSet name>\n
44  Refine named cell set.
45 
46  @param -overwrite \n
47  Overwrite existing data.
48 
49  @param -case <dir>\n
50  Case directory.
51 
52  @param -help \n
53  Display help message.
54 
55  @param -doc \n
56  Display Doxygen API documentation page for this application.
57 
58  @param -srcDoc \n
59  Display Doxygen source documentation page for this application.
60 
61 \*---------------------------------------------------------------------------*/
62 
63 #include <OpenFOAM/argList.H>
64 #include <OpenFOAM/Time.H>
67 #include <OpenFOAM/mapPolyMesh.H>
68 #include <OpenFOAM/polyMesh.H>
69 #include <dynamicMesh/cellCuts.H>
70 #include <meshTools/cellSet.H>
71 #include <dynamicMesh/meshCutter.H>
72 
73 using namespace Foam;
74 
75 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76 // Main program:
77 
78 int main(int argc, char *argv[])
79 {
81  Foam::argList::validArgs.append("patchName");
82  Foam::argList::validArgs.append("edgeWeight");
83  Foam::argList::validOptions.insert("useSet", "cellSet");
84  Foam::argList::validOptions.insert("overwrite", "");
85 
86 # include <OpenFOAM/setRootCase.H>
87 # include <OpenFOAM/createTime.H>
88  runTime.functionObjects().off();
89 # include <OpenFOAM/createPolyMesh.H>
90  const word oldInstance = mesh.pointsInstance();
91 
92  word patchName(args.additionalArgs()[0]);
93 
94  scalar weight(readScalar(IStringStream(args.additionalArgs()[1])()));
95  bool overwrite = args.optionFound("overwrite");
96 
97 
98  label patchID = mesh.boundaryMesh().findPatchID(patchName);
99 
100  if (patchID == -1)
101  {
103  << "Cannot find patch " << patchName << endl
104  << "Valid patches are " << mesh.boundaryMesh().names()
105  << exit(FatalError);
106  }
107  const polyPatch& pp = mesh.boundaryMesh()[patchID];
108 
109 
110  // Cells cut
111 
112  labelHashSet cutCells(4*pp.size());
113 
114  const labelList& meshPoints = pp.meshPoints();
115 
116  forAll(meshPoints, pointI)
117  {
118  label meshPointI = meshPoints[pointI];
119 
120  const labelList& pCells = mesh.pointCells()[meshPointI];
121 
122  forAll(pCells, pCellI)
123  {
124  cutCells.insert(pCells[pCellI]);
125  }
126  }
127 
128  Info<< "Selected " << cutCells.size()
129  << " cells connected to patch " << pp.name() << endl << endl;
130 
131  //
132  // List of cells to refine
133  //
134 
135  bool useSet = args.optionFound("useSet");
136 
137  if (useSet)
138  {
139  word setName(args.option("useSet"));
140 
141  Info<< "Subsetting cells to cut based on cellSet" << setName << endl
142  << endl;
143 
144  cellSet cells(mesh, setName);
145 
146  Info<< "Read " << cells.size() << " cells from cellSet "
147  << cells.instance()/cells.local()/cells.name()
148  << endl << endl;
149 
150  for
151  (
153  iter != cells.end();
154  ++iter
155  )
156  {
157  cutCells.erase(iter.key());
158  }
159  Info<< "Removed from cells to cut all the ones not in set " << setName
160  << endl << endl;
161  }
162 
163  // Mark all meshpoints on patch
164 
165  boolList vertOnPatch(mesh.nPoints(), false);
166 
167  forAll(meshPoints, pointI)
168  {
169  label meshPointI = meshPoints[pointI];
170 
171  vertOnPatch[meshPointI] = true;
172  }
173 
174 
175  // Mark cut edges.
176 
177  DynamicList<label> allCutEdges(pp.nEdges());
178 
179  DynamicList<scalar> allCutEdgeWeights(pp.nEdges());
180 
181  forAll(meshPoints, pointI)
182  {
183  label meshPointI = meshPoints[pointI];
184 
185  const labelList& pEdges = mesh.pointEdges()[meshPointI];
186 
187  forAll(pEdges, pEdgeI)
188  {
189  label edgeI = pEdges[pEdgeI];
190 
191  const edge& e = mesh.edges()[edgeI];
192 
193  label otherPointI = e.otherVertex(meshPointI);
194 
195  if (!vertOnPatch[otherPointI])
196  {
197  allCutEdges.append(edgeI);
198 
199  if (e.start() == meshPointI)
200  {
201  allCutEdgeWeights.append(weight);
202  }
203  else
204  {
205  allCutEdgeWeights.append(1 - weight);
206  }
207  }
208  }
209  }
210 
211  allCutEdges.shrink();
212  allCutEdgeWeights.shrink();
213 
214  Info<< "Cutting:" << endl
215  << " cells:" << cutCells.size() << endl
216  << " edges:" << allCutEdges.size() << endl
217  << endl;
218 
219  // Transfer DynamicLists to straight ones.
220  scalarField cutEdgeWeights;
221  cutEdgeWeights.transfer(allCutEdgeWeights);
222  allCutEdgeWeights.clear();
223 
224 
225  // Gets cuts across cells from cuts through edges.
226  cellCuts cuts
227  (
228  mesh,
229  cutCells.toc(), // cells candidate for cutting
230  labelList(0), // cut vertices
231  allCutEdges, // cut edges
232  cutEdgeWeights // weight on cut edges
233  );
234 
235  polyTopoChange meshMod(mesh);
236 
237  // Cutting engine
238  meshCutter cutter(mesh);
239 
240  // Insert mesh refinement into polyTopoChange.
241  cutter.setRefinement(cuts, meshMod);
242 
243  // Do all changes
244  Info<< "Morphing ..." << endl;
245 
246  if (!overwrite)
247  {
248  runTime++;
249  }
250 
251  autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
252 
253  if (morphMap().hasMotionPoints())
254  {
255  mesh.movePoints(morphMap().preMotionPoints());
256  }
257 
258  // Update stored labels on meshCutter.
259  cutter.updateMesh(morphMap());
260 
261  if (overwrite)
262  {
263  mesh.setInstance(oldInstance);
264  }
265 
266  // Write resulting mesh
267  Info << "Writing refined morphMesh to time " << runTime.timeName() << endl;
268 
269  mesh.write();
270 
271  Info << "End\n" << endl;
272 
273  return 0;
274 }
275 
276 
277 // ************************ vim: set sw=4 sts=4 et: ************************ //