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

errorDrivenRefinement.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 "errorDrivenRefinement.H"
00028 #include "polyTopoChanger.H"
00029 #include "polyMesh.H"
00030 #include "primitiveMesh.H"
00031 #include "polyTopoChange.H"
00032 #include "addToRunTimeSelectionTable.H"
00033 #include "volFields.H"
00034 #include "surfaceFields.H"
00035 #include "evaluateError.H"
00036 #include "fvc.H"
00037 #include "mapPolyMesh.H"
00038 #include "topoCellLooper.H"
00039 #include "cellCuts.H"
00040 
00041 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00042 
00043 namespace Foam
00044 {
00045     defineTypeNameAndDebug(errorDrivenRefinement, 0);
00046     addToRunTimeSelectionTable
00047     (
00048         polyMeshModifier,
00049         errorDrivenRefinement,
00050         dictionary
00051     );
00052 }
00053 
00054 
00055 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00056 
00057 
00058 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00059 
00060 // Construct from dictionary
00061 Foam::errorDrivenRefinement::errorDrivenRefinement
00062 (
00063     const word& name,
00064     const dictionary& dict,
00065     const label index,
00066     const polyTopoChanger& mme
00067 )
00068 :
00069     polyMeshModifier(name, index, mme, false),
00070     refinementEngine_(topoChanger().mesh(), true),
00071     errorField_(dict.lookup("errorField"))
00072 {}
00073 
00074 
00075 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00076 
00077 Foam::errorDrivenRefinement::~errorDrivenRefinement()
00078 {}
00079 
00080 
00081 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00082 
00083 
00084 bool Foam::errorDrivenRefinement::changeTopology() const
00085 {
00086     const Time& runTime = topoChanger().mesh().time();
00087 
00088     if (runTime.foundObject<volVectorField>(errorField_))
00089     {
00090         if (debug)
00091         {
00092             Info<< "errorDrivenRefinement::changeTopology() : triggering topo"
00093                 << " change since found errorField "
00094                 << errorField_ << endl;
00095         }
00096 
00097         return true;
00098     }
00099     else
00100     {
00101         if (debug)
00102         {
00103             Info<< "errorDrivenRefinement::changeTopology() : no topo"
00104                 << " change request from me since no errorField "
00105                 << errorField_ << endl;
00106         }
00107 
00108         return false;
00109     }
00110 }
00111 
00112 
00113 void Foam::errorDrivenRefinement::setRefinement(polyTopoChange& ref) const
00114 {
00115     // Insert the coarsen/refinement instructions into the topological change
00116 
00117     if (debug)
00118     {
00119         Info<< "errorDrivenRefinement::setRefinement(polyTopoChange& ref)"
00120             << endl;
00121     }
00122 
00123     const polyMesh& mesh = topoChanger().mesh();
00124 
00125     const Time& runTime = mesh.time();
00126 
00127     if (debug)
00128     {
00129         Info<< "Looking up vector field with name " << errorField_ << endl;
00130     }
00131     const volVectorField& resError =
00132         runTime.lookupObject<volVectorField>(errorField_);
00133 
00134     const volScalarField magResError = Foam::mag(resError);
00135 
00136     scalar min = Foam::min(magResError).value();
00137     scalar max = Foam::max(magResError).value();
00138     scalar avg = Foam::average(magResError).value();
00139 
00140     if (debug) 
00141     {
00142         Info<< "Writing magResError" << endl;
00143         magResError.write();
00144 
00145         Info<< "min:" << min << " max:" << max << " avg:" << avg << endl;
00146     }
00147 
00148     // Get faces to remove and cells to refine based on error
00149     evaluateError refPattern
00150     (
00151         magResError,                        // Error on cells
00152         resError,                           // Error vector on cells
00153         fvc::interpolate(magResError),      // Error on faces
00154         refinementEngine_.getSplitFaces()   // Current live split faces
00155     );
00156 
00157 
00158     // Insert mesh refinement into polyTopoChange:
00159     // - remove split faces
00160     // - refine cells
00161 
00162     // Give 'hint' of faces to remove to cell splitter.
00163     const labelList& candidates = refPattern.unsplitFaces();
00165     //labelList candidates;
00166 
00167     labelList removedFaces(refinementEngine_.removeSplitFaces(candidates, ref));
00168 
00169     // Now success will be for every candidates whether face has been removed.
00170     // Protect cells using face from refinement.
00171 
00172     // List of protected cells
00173     boolList markedCell(mesh.nCells(), false);
00174 
00175     forAll(removedFaces, i)
00176     {
00177         label faceI = removedFaces[i];
00178 
00179         markedCell[mesh.faceOwner()[faceI]] = true;
00180 
00181         if (mesh.isInternalFace(faceI))
00182         {
00183             markedCell[mesh.faceNeighbour()[faceI]] = true;
00184         }
00185     }
00186     
00187     // Repack list of cells to refine.
00188     List<refineCell> refCells = refPattern.refCells();
00189 
00190     label newRefCellI = 0;
00191 
00192     forAll(refCells, refCellI)
00193     {
00194         label cellI = refCells[refCellI].cellNo();
00195 
00196         if (!markedCell[cellI] && (newRefCellI != refCellI))
00197         {
00198             refCells[newRefCellI++] = refCells[refCellI];
00199         }
00200     }
00201 
00202     if (debug)
00203     {
00204         Info<< "errorDrivenRefinement : shrinking refCells from "
00205             << refCells.size()
00206             << " to " << newRefCellI << endl;
00207     }
00208 
00209     refCells.setSize(newRefCellI);
00210 
00211     // Determine cut pattern using topological cell walker
00212     topoCellLooper cellWalker(mesh);
00213 
00214     cellCuts cuts(mesh, cellWalker, refCells);
00215 
00216     // Do actual splitting
00217     refinementEngine_.setRefinement(cuts, ref);
00218 }
00219 
00220 
00221 // Has the responsability of moving my newly introduced points onto the right
00222 // place. This is since the whole mesh might e.g. have been moved by another
00223 // meshmodifier. So using preMotionPoints is hack for if I am only meshModifier.
00224 // Good solution:
00225 // - remember new point label of introduced point and vertices
00226 // of edge it is created from (in setRefinement)
00227 // - in here reposition point at correct position between current vertex
00228 // position of edge endpoints.
00229 void Foam::errorDrivenRefinement::modifyMotionPoints
00230 (
00231     pointField& motionPoints
00232 ) const
00233 {
00234     if (debug)
00235     {
00236         Info<< "errorDrivenRefinement::modifyMotionPoints(*pointField&)" << endl;
00237     }
00238 }
00239 
00240 
00241 void Foam::errorDrivenRefinement::updateMesh(const mapPolyMesh& morphMap)
00242 {
00243     // Mesh has changed topologically. Update local topological data
00244     if (debug)
00245     {
00246         Info<< "errorDrivenRefinement::updateMesh"
00247             << "(const mapPolyMesh& morphMap)" << endl;
00248     }
00249     refinementEngine_.updateMesh(morphMap);
00250 }
00251 
00252 
00253 void Foam::errorDrivenRefinement::write(Ostream& os) const
00254 {
00255     os  << nl << type() << nl;
00256 }
00257 
00258 
00259 void Foam::errorDrivenRefinement::writeDict(Ostream& os) const
00260 {
00261     os  << nl << name() << nl << token::BEGIN_BLOCK << nl
00262         << "    type " << type()
00263         << token::END_STATEMENT << nl
00264         << token::END_BLOCK << endl;
00265 }
00266 
00267 
00268 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00269 
00270 
00271 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00272 
00273 
00274 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00275 
00276 
00277 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd