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-------------------------------------------------------------------------------
11License
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
27Application
28 refineWallLayer
29
30Group
31 grpMeshAdvancedUtilities
32
33Description
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
61using namespace Foam;
62
63// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64
65int main(int argc, char *argv[])
66{
67 argList::addNote
68 (
69 "Refine cells next to specified patches."
70 );
71
72 #include "addOverwriteOption.H"
73 argList::addArgument
74 (
75 "patches",
76 "The list of patch names or regex - Eg, '(top \"Wall.\")'"
77 );
78 argList::addArgument
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
85 argList::addOption
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();
264 topoSet::removeFiles(mesh);
265 processorMeshes::removeFiles(mesh);
266
267 Info<< "End\n" << endl;
268
269 return 0;
270}
271
272
273// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
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:330
void transfer(List< T > &list)
Definition: List.C:447
const labelList & meshPoints() const
Return labelList of mesh points in patch.
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:323
List< T > getList(const label index) const
Get a List of values from the argument at index.
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
Description of cuts across cells.
Definition: cellCuts.H:113
A collection of cell labels.
Definition: cellSet.H:54
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:66
Cuts (splits) cells.
Definition: meshCutter.H:141
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:75
Direct mesh changes based on v1.3 polyTopoChange syntax.
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:54
A class for handling words, derived from Foam::string.
Definition: word.H:68
const polyBoundaryMesh & patches
dynamicFvMesh & mesh
engineTime & runTime
Required Variables.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
const cellShapeList & cells
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Foam::argList args(argc, argv)
volScalarField & e
Definition: createFields.H:11