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

polyTopoChanger.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 "polyTopoChanger.H"
00028 #include "polyMesh.H"
00029 #include "polyTopoChange.H"
00030 #include "Time.H"
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036     defineTypeNameAndDebug(polyTopoChanger, 0);
00037 }
00038 
00039 
00040 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00041 
00042 void Foam::polyTopoChanger::readModifiers()
00043 {
00044     if
00045     (
00046         readOpt() == IOobject::MUST_READ
00047      || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
00048     )
00049     {
00050         PtrList<polyMeshModifier>& modifiers = *this;
00051 
00052         // Read modifiers
00053         Istream& is = readStream(typeName);
00054 
00055         PtrList<entry> patchEntries(is);
00056         modifiers.setSize(patchEntries.size());
00057 
00058         forAll(modifiers, modifierI)
00059         {
00060             modifiers.set
00061             (
00062                 modifierI,
00063                 polyMeshModifier::New
00064                 (
00065                     patchEntries[modifierI].keyword(),
00066                     patchEntries[modifierI].dict(),
00067                     modifierI,
00068                     *this
00069                 )
00070             );
00071         }
00072 
00073         // Check state of IOstream
00074         is.check
00075         (
00076             "polyTopoChanger::polyTopoChanger"
00077             "(const IOobject&, const polyMesh&)"
00078         );
00079 
00080         close();
00081     }
00082 }
00083 
00084 
00085 // Read constructor given IOobject and a polyMesh reference
00086 Foam::polyTopoChanger::polyTopoChanger
00087 (
00088     const IOobject& io,
00089     polyMesh& mesh
00090 )
00091 :
00092     PtrList<polyMeshModifier>(),
00093     regIOobject(io),
00094     mesh_(mesh)
00095 {
00096     readModifiers();
00097 }
00098 
00099 
00100 // Read constructor given IOobject and a polyMesh reference
00101 Foam::polyTopoChanger::polyTopoChanger(polyMesh& mesh)
00102 :
00103     PtrList<polyMeshModifier>(),
00104     regIOobject
00105     (
00106         IOobject
00107         (
00108             "meshModifiers",
00109             mesh.time().findInstance
00110             (
00111                 mesh.meshDir(),
00112                 "meshModifiers",
00113                 IOobject::READ_IF_PRESENT
00114             ),
00115             mesh.meshSubDir,
00116             mesh,
00117             IOobject::READ_IF_PRESENT,
00118             IOobject::NO_WRITE
00119         )
00120     ),
00121     mesh_(mesh)
00122 {
00123     readModifiers();
00124 }
00125 
00126 
00127 // Return a list of modifier types
00128 Foam::wordList Foam::polyTopoChanger::types() const
00129 {
00130     const PtrList<polyMeshModifier>& modifiers = *this;
00131 
00132     wordList t(modifiers.size());
00133 
00134     forAll (modifiers, modifierI)
00135     {
00136         t[modifierI] = modifiers[modifierI].type();
00137     }
00138 
00139     return t;
00140 }
00141 
00142 
00143 // Return a list of modifier names
00144 Foam::wordList Foam::polyTopoChanger::names() const
00145 {
00146     const PtrList<polyMeshModifier>& modifiers = *this;
00147 
00148     wordList t(modifiers.size());
00149 
00150     forAll (modifiers, modifierI)
00151     {
00152         t[modifierI] = modifiers[modifierI].name();
00153     }
00154 
00155     return t;
00156 }
00157 
00158 
00159 // Is topology change required
00160 bool Foam::polyTopoChanger::changeTopology() const
00161 {
00162     // Go through all mesh modifiers and accumulate the morphing information
00163     const PtrList<polyMeshModifier>& topoChanges = *this;
00164 
00165     bool triggerChange = false;
00166 
00167     forAll (topoChanges, morphI)
00168     {
00169         if (topoChanges[morphI].active())
00170         {
00171             bool curTriggerChange = topoChanges[morphI].changeTopology();
00172 
00173             if (debug)
00174             {
00175                 Info<< "Modifier " << morphI << " named "
00176                     << topoChanges[morphI].name();
00177                 
00178                 if (curTriggerChange)
00179                 {
00180                     Info << " morphing" << endl;
00181                 }
00182                 else
00183                 {
00184                     Info << " unchanged" << endl;
00185                 }
00186             }
00187 
00188             triggerChange = triggerChange || curTriggerChange;
00189         }
00190         else
00191         {
00192             if (debug)
00193             {
00194                 Info<< "Modifier " << morphI  << " named "
00195                     << topoChanges[morphI].name() << " inactive" << endl;
00196             }
00197         }
00198             
00199     }
00200 
00201     return triggerChange;
00202 }
00203 
00204 
00205 // Return topology change request
00206 Foam::autoPtr<Foam::polyTopoChange>
00207 Foam::polyTopoChanger::topoChangeRequest() const
00208 {
00209     // Collect changes from all modifiers
00210     const PtrList<polyMeshModifier>& topoChanges = *this;
00211 
00212     polyTopoChange* refPtr(new polyTopoChange(mesh()));
00213     polyTopoChange& ref = *refPtr;
00214 
00215     forAll (topoChanges, morphI)
00216     {
00217         if (topoChanges[morphI].active())
00218         {
00219             topoChanges[morphI].setRefinement(ref);
00220         }
00221     }
00222 
00223     return autoPtr<polyTopoChange>(refPtr);
00224 }
00225 
00226 
00227 // Correct polyTopoChanger after moving points
00228 void Foam::polyTopoChanger::modifyMotionPoints(pointField& p) const
00229 {
00230     const PtrList<polyMeshModifier>& topoChanges = *this;
00231 
00232     forAll (topoChanges, morphI)
00233     {
00234         if (topoChanges[morphI].active())
00235         {
00236             topoChanges[morphI].modifyMotionPoints(p);
00237         }
00238     }
00239 }
00240 
00241 
00242 // Force recalculation of locally stored data on topological change
00243 void Foam::polyTopoChanger::update(const mapPolyMesh& m)
00244 {
00245     // Go through all mesh modifiers and accumulate the morphing information
00246     PtrList<polyMeshModifier>& topoChanges = *this;
00247 
00248     forAll (topoChanges, morphI)
00249     {
00250         topoChanges[morphI].updateMesh(m);
00251     }
00252 
00253     // Force the mesh modifiers to auto-write.  This allows us to
00254     // preserve the current state of modifiers corresponding with
00255     // the mesh.  
00256     writeOpt() = IOobject::AUTO_WRITE;
00257     instance() = mesh_.time().timeName();
00258 }
00259 
00260 
00261 Foam::autoPtr<Foam::mapPolyMesh> Foam::polyTopoChanger::changeMesh
00262 (
00263     const bool inflate,
00264     const bool syncParallel,
00265     const bool orderCells,
00266     const bool orderPoints
00267 )
00268 {
00269     if (changeTopology())
00270     {
00271         autoPtr<polyTopoChange> ref = topoChangeRequest();
00272 
00273         autoPtr<mapPolyMesh> topoChangeMap = ref().changeMesh
00274         (
00275             mesh_,
00276             inflate,
00277             syncParallel,
00278             orderCells,
00279             orderPoints
00280         );
00281 
00282         update(topoChangeMap());
00283         mesh_.updateMesh(topoChangeMap());
00284         return topoChangeMap;
00285     }
00286     else
00287     {
00288         return autoPtr<mapPolyMesh>(NULL);
00289     }
00290 }
00291 
00292 
00293 // Add mesh modifiers to the morph engine
00294 void Foam::polyTopoChanger::addTopologyModifiers
00295 (
00296     const List<polyMeshModifier*>& tm
00297 )
00298 {
00299     setSize(tm.size());
00300 
00301     // Copy the patch pointers
00302     forAll (tm, tmI)
00303     {
00304         if (tm[tmI]->topoChanger() != *this)
00305         {
00306             FatalErrorIn
00307             (
00308                 "void polyTopoChanger::addTopologyModifiers("
00309                 "const List<polyMeshModifier*>& tm)"
00310             )   << "Mesh modifier created with different mesh reference."
00311                 << abort(FatalError);
00312         }
00313         set(tmI, tm[tmI]);
00314     }
00315 
00316     writeOpt() = IOobject::AUTO_WRITE;
00317 }
00318 
00319 
00320 Foam::label Foam::polyTopoChanger::findModifierID
00321 (
00322     const word& modName
00323 ) const
00324 {
00325     const PtrList<polyMeshModifier>& topoChanges = *this;
00326 
00327     forAll (topoChanges, morphI)
00328     {
00329         if (topoChanges[morphI].name() == modName)
00330         {
00331             return morphI;
00332         }
00333     }
00334 
00335     // Modifier not found
00336     if (debug)
00337     {
00338         Info<< "label polyTopoChanger::::findModifierID(const word& "
00339             << "modName) const"
00340             << "Modifier named " << modName << " not found.  "
00341             << "List of available modifier names: " << names() << endl;
00342     }
00343 
00344     // Not found, return -1
00345     return -1;
00346 }
00347 
00348 
00349 // writeData member function required by regIOobject
00350 bool Foam::polyTopoChanger::writeData(Ostream& os) const
00351 {
00352     os << *this;
00353     return os.good();
00354 }
00355 
00356 
00357 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00358 
00359 bool Foam::polyTopoChanger::operator!=(const polyTopoChanger& me) const
00360 {
00361     return &me != this;
00362 }
00363 
00364 
00365 bool Foam::polyTopoChanger::operator==(const polyTopoChanger& me) const
00366 {
00367     return &me == this;
00368 }
00369 
00370 
00371 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00372 
00373 Foam::Ostream& Foam::operator<<(Ostream& os, const polyTopoChanger& mme)
00374 {
00375     os  << mme.size() << nl << token::BEGIN_LIST;
00376 
00377     forAll(mme, mmeI)
00378     {
00379         mme[mmeI].writeDict(os);
00380     }
00381 
00382     os  << token::END_LIST;
00383 
00384     return os;
00385 }
00386 
00387 
00388 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd