1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
25 Utility to refine cells next to patches.
27 Takes a patchName and number of layers to refine. Works out cells within
28 these layers and refines those in the wall-normal direction.
30 \*---------------------------------------------------------------------------*/
34 #include "polyTopoChange.H"
35 #include "polyTopoChanger.H"
36 #include "mapPolyMesh.H"
40 #include "meshCutter.H"
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 int main(int argc, char *argv[])
49 #include "addOverwriteOption.H"
50 argList::noParallel();
51 argList::validArgs.append("patchName");
52 argList::validArgs.append("edgeWeight");
58 "restrict cells to refine based on specified cellSet name"
62 # include "setRootCase.H"
63 # include "createTime.H"
64 runTime.functionObjects().off();
65 # include "createPolyMesh.H"
66 const word oldInstance = mesh.pointsInstance();
68 const word patchName = args[1];
69 const scalar weight = args.argRead<scalar>(2);
70 const bool overwrite = args.optionFound("overwrite");
72 label patchID = mesh.boundaryMesh().findPatchID(patchName);
76 FatalErrorIn(args.executable())
77 << "Cannot find patch " << patchName << endl
78 << "Valid patches are " << mesh.boundaryMesh().names()
81 const polyPatch& pp = mesh.boundaryMesh()[patchID];
86 labelHashSet cutCells(4*pp.size());
88 const labelList& meshPoints = pp.meshPoints();
90 forAll(meshPoints, pointI)
92 label meshPointI = meshPoints[pointI];
94 const labelList& pCells = mesh.pointCells()[meshPointI];
96 forAll(pCells, pCellI)
98 cutCells.insert(pCells[pCellI]);
102 Info<< "Selected " << cutCells.size()
103 << " cells connected to patch " << pp.name() << endl << endl;
106 // List of cells to refine
110 if (args.optionReadIfPresent("useSet", setName))
112 Info<< "Subsetting cells to cut based on cellSet"
113 << setName << nl << endl;
115 cellSet cells(mesh, setName);
117 Info<< "Read " << cells.size() << " cells from cellSet "
118 << cells.instance()/cells.local()/cells.name()
121 forAllConstIter(cellSet, cells, iter)
123 cutCells.erase(iter.key());
125 Info<< "Removed from cells to cut all the ones not in set "
126 << setName << nl << endl;
129 // Mark all meshpoints on patch
131 boolList vertOnPatch(mesh.nPoints(), false);
133 forAll(meshPoints, pointI)
135 const label meshPointI = meshPoints[pointI];
137 vertOnPatch[meshPointI] = true;
143 DynamicList<label> allCutEdges(pp.nEdges());
145 DynamicList<scalar> allCutEdgeWeights(pp.nEdges());
147 forAll(meshPoints, pointI)
149 label meshPointI = meshPoints[pointI];
151 const labelList& pEdges = mesh.pointEdges()[meshPointI];
153 forAll(pEdges, pEdgeI)
155 const label edgeI = pEdges[pEdgeI];
156 const edge& e = mesh.edges()[edgeI];
158 label otherPointI = e.otherVertex(meshPointI);
160 if (!vertOnPatch[otherPointI])
162 allCutEdges.append(edgeI);
164 if (e.start() == meshPointI)
166 allCutEdgeWeights.append(weight);
170 allCutEdgeWeights.append(1 - weight);
176 allCutEdges.shrink();
177 allCutEdgeWeights.shrink();
179 Info<< "Cutting:" << nl
180 << " cells:" << cutCells.size() << nl
181 << " edges:" << allCutEdges.size() << nl
184 // Transfer DynamicLists to straight ones.
185 scalarField cutEdgeWeights;
186 cutEdgeWeights.transfer(allCutEdgeWeights);
187 allCutEdgeWeights.clear();
190 // Gets cuts across cells from cuts through edges.
194 cutCells.toc(), // cells candidate for cutting
195 labelList(0), // cut vertices
196 allCutEdges, // cut edges
197 cutEdgeWeights // weight on cut edges
200 polyTopoChange meshMod(mesh);
203 meshCutter cutter(mesh);
205 // Insert mesh refinement into polyTopoChange.
206 cutter.setRefinement(cuts, meshMod);
209 Info<< "Morphing ..." << endl;
216 autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
218 if (morphMap().hasMotionPoints())
220 mesh.movePoints(morphMap().preMotionPoints());
223 // Update stored labels on meshCutter.
224 cutter.updateMesh(morphMap());
228 mesh.setInstance(oldInstance);
231 // Write resulting mesh
232 Info<< "Writing refined morphMesh to time " << runTime.timeName() << endl;
236 Info<< "End\n" << endl;
242 // ************************************************************************* //