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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "refinementIterator.H"
29 #include "refineCell.H"
30 #include "undoableMeshCutter.H"
31 #include "polyTopoChange.H"
32 #include "mapPolyMesh.H"
35 #include "meshTools.H"
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 defineTypeNameAndDebug(Foam::refinementIterator, 0);
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 // Construct from components
45 Foam::refinementIterator::refinementIterator
48 undoableMeshCutter& meshRefiner,
49 const cellLooper& cellWalker,
55 meshRefiner_(meshRefiner),
56 cellWalker_(cellWalker),
61 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
63 Foam::refinementIterator::~refinementIterator()
67 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69 Foam::Map<Foam::label> Foam::refinementIterator::setRefinement
71 const List<refineCell>& refCells
74 Map<label> addedCells(2*refCells.size());
76 Time& runTime = const_cast<Time&>(mesh_.time());
78 label nRefCells = refCells.size();
80 label oldRefCells = -1;
83 List<refineCell> currentRefCells(refCells);
91 // Need different times to write meshes.
95 polyTopoChange meshMod(mesh_);
99 Pout<< "refinementIterator : refining "
100 << currentRefCells.size() << " cells." << endl;
103 // Determine cut pattern.
104 cellCuts cuts(mesh_, cellWalker_, currentRefCells);
106 label nCuts = cuts.nLoops();
107 reduce(nCuts, sumOp<label>());
113 Pout<< "refinementIterator : exiting iteration since no valid"
114 << " loops found for " << currentRefCells.size()
118 fileName cutsFile("failedCuts_" + runTime.timeName() + ".obj");
120 Pout<< "Writing cuts for time " << runTime.timeName()
121 << " to " << cutsFile << endl;
123 OFstream cutsStream(cutsFile);
126 labelList refCells(currentRefCells.size());
127 forAll(currentRefCells, i)
129 refCells[i] = currentRefCells[i].cellNo();
146 fileName cutsFile("cuts_" + runTime.timeName() + ".obj");
148 Pout<< "Writing cuts for time " << runTime.timeName()
149 << " to " << cutsFile << endl;
151 OFstream cutsStream(cutsFile);
152 cuts.writeOBJ(cutsStream);
156 // Insert mesh refinement into polyTopoChange.
157 meshRefiner_.setRefinement(cuts, meshMod);
164 autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh
170 // Move mesh (since morphing does not do this)
171 if (morphMap().hasMotionPoints())
173 mesh_.movePoints(morphMap().preMotionPoints());
176 // Update stored refinement pattern
177 meshRefiner_.updateMesh(morphMap());
179 // Write resulting mesh
184 Pout<< "Writing refined polyMesh to time "
185 << runTime.timeName() << endl;
191 // Update currentRefCells for new cell numbers. Use helper function
192 // in meshCutter class.
195 morphMap->reverseCellMap(),
199 // Update addedCells for new cell numbers
202 morphMap->reverseCellMap(),
206 // Get all added cells from cellCutter (already in new numbering
207 // from meshRefiner.updateMesh call) and add to global list of added
208 const Map<label>& addedNow = meshRefiner_.addedCells();
210 forAllConstIter(Map<label>, addedNow, iter)
212 if (!addedCells.insert(iter.key(), iter()))
214 FatalErrorIn("refinementIterator")
215 << "Master cell " << iter.key()
216 << " already has been refined" << endl
217 << "Added cell:" << iter() << abort(FatalError);
222 // Get failed refinement in new cell numbering and reconstruct input
223 // to the meshRefiner. Is done by removing all refined cells from
224 // current list of cells to refine.
226 // Update refCells for new cell numbers.
229 morphMap->reverseCellMap(),
233 // Pack refCells acc. to refined status
236 forAll(currentRefCells, refI)
238 const refineCell& refCell = currentRefCells[refI];
240 if (!addedNow.found(refCell.cellNo()))
242 if (nRefCells != refI)
244 currentRefCells[nRefCells++] =
254 oldRefCells = currentRefCells.size();
256 currentRefCells.setSize(nRefCells);
263 // Stop only if all finished or all can't refine any further.
264 stop = (nRefCells == 0) || (nRefCells == oldRefCells);
265 reduce(stop, andOp<bool>());
270 if (nRefCells == oldRefCells)
272 WarningIn("refinementIterator")
273 << "stopped refining."
274 << "Did not manage to refine a single cell" << endl
275 << "Wanted :" << oldRefCells << endl;
283 // ************************************************************************* //