OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

refinementIterator.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "refinementIterator.H"
00028 #include "polyMesh.H"
00029 #include "Time.H"
00030 #include "refineCell.H"
00031 #include "undoableMeshCutter.H"
00032 #include "polyTopoChange.H"
00033 #include "mapPolyMesh.H"
00034 #include "cellCuts.H"
00035 #include "OFstream.H"
00036 #include "meshTools.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042 
00043 defineTypeNameAndDebug(refinementIterator, 0);
00044 
00045 }
00046 
00047 
00048 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00049 
00050 // Construct from components
00051 Foam::refinementIterator::refinementIterator
00052 (
00053     polyMesh& mesh,
00054     undoableMeshCutter& meshRefiner,
00055     const cellLooper& cellWalker,
00056     const bool writeMesh
00057 )
00058 :
00059     edgeVertex(mesh),
00060     mesh_(mesh),
00061     meshRefiner_(meshRefiner),
00062     cellWalker_(cellWalker),
00063     writeMesh_(writeMesh)
00064 {}
00065 
00066 
00067 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00068 
00069 Foam::refinementIterator::~refinementIterator()
00070 {}
00071 
00072 
00073 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00074 
00075 Foam::Map<Foam::label> Foam::refinementIterator::setRefinement
00076 (
00077     const List<refineCell>& refCells
00078 )
00079 {
00080     Map<label> addedCells(2*refCells.size());
00081 
00082     Time& runTime = const_cast<Time&>(mesh_.time());
00083 
00084     label nRefCells = refCells.size();
00085 
00086     label oldRefCells = -1;
00087 
00088     // Operate on copy.
00089     List<refineCell> currentRefCells(refCells);
00090 
00091     bool stop = false;
00092 
00093     do
00094     {
00095         if (writeMesh_)
00096         {
00097             // Need different times to write meshes.
00098             runTime++;
00099         }
00100 
00101         polyTopoChange meshMod(mesh_);
00102 
00103         if (debug)
00104         {
00105             Pout<< "refinementIterator : refining "
00106                 << currentRefCells.size() << " cells." << endl;
00107         }
00108 
00109         // Determine cut pattern.
00110         cellCuts cuts(mesh_, cellWalker_, currentRefCells);
00111 
00112         label nCuts = cuts.nLoops();
00113         reduce(nCuts, sumOp<label>());
00114 
00115         if (nCuts == 0)
00116         {
00117             if (debug)
00118             {
00119                 Pout<< "refinementIterator : exiting iteration since no valid"
00120                     << " loops found for " << currentRefCells.size() 
00121                     << " cells" << endl;
00122 
00123 
00124                 fileName cutsFile("failedCuts_" + runTime.timeName() + ".obj");
00125 
00126                 Pout<< "Writing cuts for time " <<  runTime.timeName()
00127                     << " to " << cutsFile << endl;
00128 
00129                 OFstream cutsStream(cutsFile);
00130 
00131 
00132                 labelList refCells(currentRefCells.size());
00133                 forAll(currentRefCells, i)
00134                 {
00135                     refCells[i] = currentRefCells[i].cellNo();
00136                 }
00137                 meshTools::writeOBJ
00138                 (
00139                     cutsStream,
00140                     mesh().cells(),
00141                     mesh().faces(),
00142                     mesh().points(),
00143                     refCells
00144                 );
00145             }
00146 
00147             break;
00148         }
00149             
00150         if (debug)
00151         {
00152             fileName cutsFile("cuts_" + runTime.timeName() + ".obj");
00153 
00154             Pout<< "Writing cuts for time " <<  runTime.timeName()
00155                 << " to " << cutsFile << endl;
00156 
00157             OFstream cutsStream(cutsFile);
00158             cuts.writeOBJ(cutsStream);
00159         }
00160 
00161 
00162         // Insert mesh refinement into polyTopoChange.
00163         meshRefiner_.setRefinement(cuts, meshMod);
00164 
00165 
00166         //
00167         // Do all changes
00168         //
00169 
00170         autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh
00171         (
00172             mesh_,
00173             false
00174         );
00175 
00176         // Move mesh (since morphing does not do this)
00177         if (morphMap().hasMotionPoints())
00178         {
00179             mesh_.movePoints(morphMap().preMotionPoints());
00180         }
00181 
00182         // Update stored refinement pattern
00183         meshRefiner_.updateMesh(morphMap());
00184 
00185         // Write resulting mesh
00186         if (writeMesh_)
00187         {
00188             if (debug)
00189             {
00190                 Pout<< "Writing refined polyMesh to time "
00191                     << runTime.timeName() << endl;
00192             }
00193 
00194             mesh_.write();
00195         }
00196 
00197         // Update currentRefCells for new cell numbers. Use helper function
00198         // in meshCutter class.
00199         updateLabels
00200         (
00201             morphMap->reverseCellMap(),
00202             currentRefCells
00203         );
00204 
00205         // Update addedCells for new cell numbers
00206         updateLabels
00207         (
00208             morphMap->reverseCellMap(),
00209             addedCells
00210         );
00211 
00212         // Get all added cells from cellCutter (already in new numbering
00213         // from meshRefiner.updateMesh call) and add to global list of added
00214         const Map<label>& addedNow = meshRefiner_.addedCells();
00215 
00216         for
00217         (
00218             Map<label>::const_iterator iter = addedNow.begin();
00219             iter != addedNow.end();
00220             ++iter
00221         )
00222         {
00223             if (!addedCells.insert(iter.key(), iter()))
00224             {
00225                 FatalErrorIn("refinementIterator")
00226                     << "Master cell " << iter.key()
00227                     << " already has been refined" << endl
00228                     << "Added cell:" << iter() << abort(FatalError);
00229             }
00230         }
00231 
00232 
00233         // Get failed refinement in new cell numbering and reconstruct input
00234         // to the meshRefiner. Is done by removing all refined cells from
00235         // current list of cells to refine.
00236 
00237         // Update refCells for new cell numbers.
00238         updateLabels
00239         (
00240             morphMap->reverseCellMap(),
00241             currentRefCells
00242         );
00243 
00244         // Pack refCells acc. to refined status
00245         nRefCells = 0;
00246 
00247         forAll(currentRefCells, refI)
00248         {
00249             const refineCell& refCell = currentRefCells[refI];
00250 
00251             if (!addedNow.found(refCell.cellNo()))
00252             {
00253                 if (nRefCells != refI)
00254                 {
00255                     currentRefCells[nRefCells++] =
00256                         refineCell
00257                         (
00258                             refCell.cellNo(),
00259                             refCell.direction()
00260                         );
00261                 }
00262             }
00263         }
00264 
00265         oldRefCells = currentRefCells.size();
00266 
00267         currentRefCells.setSize(nRefCells);
00268 
00269         if (debug)
00270         {
00271             Pout<< endl;
00272         }
00273 
00274         // Stop only if all finished or all can't refine any further.
00275         stop = (nRefCells == 0) || (nRefCells == oldRefCells);
00276         reduce(stop, andOp<bool>());
00277     }
00278     while (!stop);
00279 
00280 
00281     if (nRefCells == oldRefCells)
00282     {
00283         WarningIn("refinementIterator")
00284             << "stopped refining."
00285             << "Did not manage to refine a single cell" << endl
00286             << "Wanted :" << oldRefCells << endl;
00287     }
00288 
00289     return addedCells;
00290 }
00291 
00292 
00293 
00294 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd