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

layerAdditionRemoval.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 Description
00026     Cell layer addition/removal mesh modifier
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "layerAdditionRemoval.H"
00031 #include "polyTopoChanger.H"
00032 #include "polyMesh.H"
00033 #include "Time.H"
00034 #include "primitiveMesh.H"
00035 #include "polyTopoChange.H"
00036 #include "addToRunTimeSelectionTable.H"
00037 
00038 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042     defineTypeNameAndDebug(layerAdditionRemoval, 0);
00043     addToRunTimeSelectionTable
00044     (
00045         polyMeshModifier,
00046         layerAdditionRemoval,
00047         dictionary
00048     );
00049 }
00050 
00051 
00052 const Foam::scalar Foam::layerAdditionRemoval::addDelta_ = 0.3;
00053 const Foam::scalar Foam::layerAdditionRemoval::removeDelta_ = 0.1;
00054 
00055 
00056 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00057 
00058 void Foam::layerAdditionRemoval::checkDefinition()
00059 {
00060     if (!faceZoneID_.active())
00061     {
00062         FatalErrorIn
00063         (
00064             "void Foam::layerAdditionRemoval::checkDefinition()"
00065         )   << "Master face zone named " << faceZoneID_.name()
00066             << " cannot be found."
00067             << abort(FatalError);
00068     }
00069 
00070     if
00071     (
00072         minLayerThickness_ < VSMALL
00073      || maxLayerThickness_ < minLayerThickness_
00074     )
00075     {
00076         FatalErrorIn
00077         (
00078             "void Foam::layerAdditionRemoval::checkDefinition()"
00079         )   << "Incorrect layer thickness definition."
00080             << abort(FatalError);
00081     }
00082 
00083     if (topoChanger().mesh().faceZones()[faceZoneID_.index()].empty())
00084     {
00085         FatalErrorIn
00086         (
00087             "void Foam::layerAdditionRemoval::checkDefinition()"
00088         )   << "Face extrusion zone contains no faces. "
00089             << " Please check your mesh definition."
00090             << abort(FatalError);
00091     }
00092 
00093     if (debug)
00094     {
00095         Pout<< "Cell layer addition/removal object " << name() << " :" << nl
00096             << "    faceZoneID: " << faceZoneID_ << endl;
00097     }
00098 }
00099 
00100 Foam::scalar Foam::layerAdditionRemoval::readOldThickness
00101 (
00102     const dictionary& dict
00103 )
00104 {
00105     return dict.lookupOrDefault("oldLayerThickness", -1.0);
00106 }
00107 
00108 
00109 void Foam::layerAdditionRemoval::clearAddressing() const
00110 {
00111     // Layer removal data
00112     deleteDemandDrivenData(pointsPairingPtr_);
00113     deleteDemandDrivenData(facesPairingPtr_);
00114 }
00115 
00116 
00117 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00118 
00119 // Construct from components
00120 Foam::layerAdditionRemoval::layerAdditionRemoval
00121 (
00122     const word& name,
00123     const label index,
00124     const polyTopoChanger& mme,
00125     const word& zoneName,
00126     const scalar minThickness,
00127     const scalar maxThickness
00128 )
00129 :
00130     polyMeshModifier(name, index, mme, true),
00131     faceZoneID_(zoneName, mme.mesh().faceZones()),
00132     minLayerThickness_(minThickness),
00133     maxLayerThickness_(maxThickness),
00134     oldLayerThickness_(-1.0),
00135     pointsPairingPtr_(NULL),
00136     facesPairingPtr_(NULL),
00137     triggerRemoval_(-1),
00138     triggerAddition_(-1)
00139 {
00140     checkDefinition();
00141 }
00142 
00143 
00144 // Construct from dictionary
00145 Foam::layerAdditionRemoval::layerAdditionRemoval
00146 (
00147     const word& name,
00148     const dictionary& dict,
00149     const label index,
00150     const polyTopoChanger& mme
00151 )
00152 :
00153     polyMeshModifier(name, index, mme, Switch(dict.lookup("active"))),
00154     faceZoneID_(dict.lookup("faceZoneName"), mme.mesh().faceZones()),
00155     minLayerThickness_(readScalar(dict.lookup("minLayerThickness"))),
00156     maxLayerThickness_(readScalar(dict.lookup("maxLayerThickness"))),
00157     oldLayerThickness_(readOldThickness(dict)),
00158     pointsPairingPtr_(NULL),
00159     facesPairingPtr_(NULL),
00160     triggerRemoval_(-1),
00161     triggerAddition_(-1)
00162 {
00163     checkDefinition();
00164 }
00165 
00166 
00167 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00168 
00169 Foam::layerAdditionRemoval::~layerAdditionRemoval()
00170 {
00171     clearAddressing();
00172 }
00173 
00174 
00175 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00176 
00177 bool Foam::layerAdditionRemoval::changeTopology() const
00178 {
00179     // Protect from multiple calculation in the same time-step
00180     if (triggerRemoval_ > -1 || triggerAddition_ > -1)
00181     {
00182         return true;
00183     }
00184 
00185     // Go through all the cells in the master layer and calculate
00186     // approximate layer thickness as the ratio of the cell volume and
00187     // face area in the face zone.
00188     // Layer addition:
00189     //     When the max thickness exceeds the threshold, trigger refinement.
00190     // Layer removal:
00191     //     When the min thickness falls below the threshold, trigger removal.
00192 
00193     const faceZone& fz = topoChanger().mesh().faceZones()[faceZoneID_.index()];
00194     const labelList& mc = fz.masterCells();
00195 
00196     const scalarField& V = topoChanger().mesh().cellVolumes();
00197     const vectorField& S = topoChanger().mesh().faceAreas();
00198 
00199     if (min(V) < -VSMALL)
00200     {
00201         FatalErrorIn("bool layerAdditionRemoval::changeTopology() const")
00202             << "negative cell volume. Error in mesh motion before "
00203             << "topological change.\n V: " << V
00204             << abort(FatalError);
00205     }
00206 
00207     scalar avgDelta = 0;
00208     scalar minDelta = GREAT;
00209     scalar maxDelta = 0;
00210 
00211     forAll (fz, faceI)
00212     {
00213         scalar curDelta = V[mc[faceI]]/mag(S[fz[faceI]]);
00214         avgDelta += curDelta;
00215         minDelta = min(minDelta, curDelta);
00216         maxDelta = max(maxDelta, curDelta);
00217     }
00218 
00219     avgDelta /= fz.size();
00220 
00222     //{
00223     //    // Edges on layer.
00224     //    const Map<label>& zoneMeshPointMap = fz().meshPointMap();
00225     //
00226     //    label nDelta = 0;
00227     //
00228     //    // Edges with only one point on zone
00229     //    const polyMesh& mesh = topoChanger().mesh();
00230     //
00231     //    forAll(mc, faceI)
00232     //    {
00233     //        const cell& cFaces = mesh.cells()[mc[faceI]];
00234     //        const edgeList cellEdges(cFaces.edges(mesh.faces()));
00235     //
00236     //        forAll(cellEdges, i)
00237     //        {
00238     //            const edge& e = cellEdges[i];
00239     //
00240     //            if (zoneMeshPointMap.found(e[0]))
00241     //            {
00242     //                if (!zoneMeshPointMap.found(e[1]))
00243     //                {
00244     //                    scalar curDelta = e.mag(mesh.points());
00245     //                    avgDelta += curDelta;
00246     //                    nDelta++;
00247     //                    minDelta = min(minDelta, curDelta);
00248     //                    maxDelta = max(maxDelta, curDelta);
00249     //                }
00250     //            }
00251     //            else
00252     //            {
00253     //                if (zoneMeshPointMap.found(e[1]))
00254     //                {
00255     //                    scalar curDelta = e.mag(mesh.points());
00256     //                    avgDelta += curDelta;
00257     //                    nDelta++;
00258     //                    minDelta = min(minDelta, curDelta);
00259     //                    maxDelta = max(maxDelta, curDelta);
00260     //                }
00261     //            }
00262     //        }
00263     //    }
00264     //
00265     //    avgDelta /= nDelta;
00266     //}
00267 
00268     if (debug)
00269     {
00270         Pout<< "bool layerAdditionRemoval::changeTopology() const "
00271             << " for object " << name() << " : " << nl
00272             << "Layer thickness: min: " << minDelta
00273             << " max: " << maxDelta << " avg: " << avgDelta
00274             << " old thickness: " << oldLayerThickness_ << nl
00275             << "Removal threshold: " << minLayerThickness_
00276             << " addition threshold: " << maxLayerThickness_ << endl;
00277     }
00278 
00279     bool topologicalChange = false;
00280 
00281     // If the thickness is decreasing and crosses the min thickness,
00282     // trigger removal
00283     if (oldLayerThickness_ < 0)
00284     {
00285         if (debug)
00286         {
00287             Pout << "First step. No addition/removal" << endl;
00288         }
00289 
00290         // No topological changes allowed before first mesh motion
00291         //
00292         oldLayerThickness_ = avgDelta;
00293 
00294         topologicalChange = false;
00295     }
00296     else if (avgDelta < oldLayerThickness_)
00297     {
00298         // Layers moving towards removal
00299         if (minDelta < minLayerThickness_)
00300         {
00301             // Check layer pairing
00302             if (setLayerPairing())
00303             {
00304                 // A mesh layer detected.  Check that collapse is valid
00305                 if (validCollapse())
00306                 {
00307                     // At this point, info about moving the old mesh
00308                     // in a way to collapse the cells in the removed
00309                     // layer is available.  Not sure what to do with
00310                     // it.
00311 
00312                     if (debug)
00313                     {
00314                         Pout<< "bool layerAdditionRemoval::changeTopology() "
00315                             << " const for object " << name() << " : "
00316                             << "Triggering layer removal" << endl;
00317                     }
00318 
00319                     triggerRemoval_ = topoChanger().mesh().time().timeIndex();
00320 
00321                     // Old thickness looses meaning.
00322                     // Set it up to indicate layer removal
00323                     oldLayerThickness_ = GREAT;
00324 
00325                     topologicalChange = true;
00326                 }
00327                 else
00328                 {
00329                     // No removal, clear addressing
00330                     clearAddressing();
00331                 }
00332             }
00333         }
00334         else
00335         {
00336             oldLayerThickness_ = avgDelta;
00337         }
00338     }
00339     else
00340     {
00341         // Layers moving towards addition
00342         if (maxDelta > maxLayerThickness_)
00343         {
00344             if (debug)
00345             {
00346                 Pout<< "bool layerAdditionRemoval::changeTopology() const "
00347                     << " for object " << name() << " : "
00348                     << "Triggering layer addition" << endl;
00349             }
00350 
00351             triggerAddition_ = topoChanger().mesh().time().timeIndex();
00352 
00353             // Old thickness looses meaning.
00354             // Set it up to indicate layer removal
00355             oldLayerThickness_ = 0;
00356 
00357             topologicalChange = true;
00358         }
00359         else
00360         {
00361             oldLayerThickness_ = avgDelta;
00362         }
00363     }
00364 
00365     return topologicalChange;
00366 }
00367 
00368 
00369 void Foam::layerAdditionRemoval::setRefinement(polyTopoChange& ref) const
00370 {
00371     // Insert the layer addition/removal instructions
00372     // into the topological change
00373 
00374     if (triggerRemoval_ == topoChanger().mesh().time().timeIndex())
00375     {
00376         removeCellLayer(ref);
00377 
00378         // Clear addressing.  This also resets the addition/removal data
00379         if (debug)
00380         {
00381             Pout<< "layerAdditionRemoval::setRefinement(polyTopoChange& ref) "
00382                 << " for object " << name() << " : "
00383                 << "Clearing addressing after layer removal. " << endl;
00384         }
00385 
00386         triggerRemoval_ = -1;
00387         clearAddressing();
00388     }
00389 
00390     if (triggerAddition_ == topoChanger().mesh().time().timeIndex())
00391     {
00392         addCellLayer(ref);
00393 
00394         // Clear addressing.  This also resets the addition/removal data
00395         if (debug)
00396         {
00397             Pout<< "layerAdditionRemoval::setRefinement(polyTopoChange& ref) "
00398                 << " for object " << name() << " : "
00399                 << "Clearing addressing after layer addition. " << endl;
00400         }
00401 
00402         triggerAddition_ = -1;
00403         clearAddressing();
00404     }
00405 }
00406 
00407 
00408 void Foam::layerAdditionRemoval::updateMesh(const mapPolyMesh&)
00409 {
00410     if (debug)
00411     {
00412         Pout<< "layerAdditionRemoval::updateMesh(const mapPolyMesh&) "
00413             << " for object " << name() << " : "
00414             << "Clearing addressing on external request. ";
00415 
00416         if (pointsPairingPtr_ || facesPairingPtr_)
00417         {
00418             Pout << "Pointers set." << endl;
00419         }
00420         else
00421         {
00422             Pout << "Pointers not set." << endl;
00423         }
00424     }
00425 
00426     // Mesh has changed topologically.  Update local topological data
00427     faceZoneID_.update(topoChanger().mesh().faceZones());
00428 
00429     clearAddressing();
00430 }
00431 
00432 
00433 void Foam::layerAdditionRemoval::setMinLayerThickness(const scalar t) const
00434 {
00435     if
00436     (
00437         t < VSMALL
00438      || maxLayerThickness_ < t
00439     )
00440     {
00441         FatalErrorIn
00442         (
00443             "void layerAdditionRemoval::setMinLayerThickness("
00444             "const scalar t) const"
00445         )   << "Incorrect layer thickness definition."
00446             << abort(FatalError);
00447     }
00448 
00449     minLayerThickness_ = t;
00450 }
00451 
00452 
00453 void Foam::layerAdditionRemoval::setMaxLayerThickness(const scalar t) const
00454 {
00455     if
00456     (
00457         t < minLayerThickness_
00458     )
00459     {
00460         FatalErrorIn
00461         (
00462             "void layerAdditionRemoval::setMaxLayerThickness("
00463             "const scalar t) const"
00464         )   << "Incorrect layer thickness definition."
00465             << abort(FatalError);
00466     }
00467 
00468     maxLayerThickness_ = t;
00469 }
00470 
00471 
00472 void Foam::layerAdditionRemoval::write(Ostream& os) const
00473 {
00474     os  << nl << type() << nl
00475         << name()<< nl
00476         << faceZoneID_ << nl
00477         << minLayerThickness_ << nl
00478         << oldLayerThickness_ << nl
00479         << maxLayerThickness_ << endl;
00480 }
00481 
00482 
00483 void Foam::layerAdditionRemoval::writeDict(Ostream& os) const
00484 {
00485     os  << nl << name() << nl << token::BEGIN_BLOCK << nl
00486         << "    type " << type()
00487         << token::END_STATEMENT << nl
00488         << "    faceZoneName " << faceZoneID_.name()
00489         << token::END_STATEMENT << nl
00490         << "    minLayerThickness " << minLayerThickness_
00491         << token::END_STATEMENT << nl
00492         << "    maxLayerThickness " << maxLayerThickness_
00493         << token::END_STATEMENT << nl
00494         << "    oldLayerThickness " << oldLayerThickness_
00495         << token::END_STATEMENT << nl
00496         << "    active " << active()
00497         << token::END_STATEMENT << nl
00498         << token::END_BLOCK << endl;
00499 }
00500 
00501 
00502 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd