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

functionObjectList.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 "functionObjectList.H"
00028 #include "Time.H"
00029 
00030 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00031 
00032 Foam::functionObject*
00033 Foam::functionObjectList::remove(const word& key, label& oldIndex)
00034 {
00035     functionObject* ptr = 0;
00036 
00037     // Find index of existing functionObject
00038     HashTable<label>::iterator fnd = indices_.find(key);
00039 
00040     if (fnd != indices_.end())
00041     {
00042         oldIndex = fnd();
00043 
00044         // retrieve the pointer and remove it from the old list
00045         ptr = this->set(oldIndex, 0).ptr();
00046         indices_.erase(fnd);
00047     }
00048     else
00049     {
00050         oldIndex = -1;
00051     }
00052 
00053     return ptr;
00054 }
00055 
00056 
00057 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00058 
00059 Foam::functionObjectList::functionObjectList
00060 (
00061     const Time& t,
00062     const bool execution
00063 )
00064 :
00065     PtrList<functionObject>(),
00066     digests_(),
00067     indices_(),
00068     time_(t),
00069     parentDict_(t.controlDict()),
00070     execution_(execution),
00071     updated_(false)
00072 {}
00073 
00074 
00075 Foam::functionObjectList::functionObjectList
00076 (
00077     const Time& t,
00078     const dictionary& parentDict,
00079     const bool execution
00080 )
00081 :
00082     PtrList<functionObject>(),
00083     digests_(),
00084     indices_(),
00085     time_(t),
00086     parentDict_(parentDict),
00087     execution_(execution),
00088     updated_(false)
00089 {}
00090 
00091 
00092 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00093 
00094 Foam::functionObjectList::~functionObjectList()
00095 {}
00096 
00097 
00098 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00099 
00100 void Foam::functionObjectList::clear()
00101 {
00102     PtrList<functionObject>::clear();
00103     digests_.clear();
00104     indices_.clear();
00105     updated_ = false;
00106 }
00107 
00108 
00109 void Foam::functionObjectList::on()
00110 {
00111     execution_ = true;
00112 }
00113 
00114 
00115 void Foam::functionObjectList::off()
00116 {
00117     // for safety, also force a read() when execution is turned back on
00118     updated_ = execution_ = false;
00119 }
00120 
00121 
00122 bool Foam::functionObjectList::status() const
00123 {
00124     return execution_;
00125 }
00126 
00127 
00128 bool Foam::functionObjectList::start()
00129 {
00130     return read();
00131 }
00132 
00133 
00134 bool Foam::functionObjectList::execute()
00135 {
00136     bool ok = true;
00137 
00138     if (execution_)
00139     {
00140         if (!updated_)
00141         {
00142             read();
00143         }
00144 
00145         forAllIter
00146         (
00147             PtrList<functionObject>,
00148             static_cast<PtrList<functionObject>&>(*this),
00149             iter
00150         )
00151         {
00152             ok = iter().execute() && ok;
00153         }
00154     }
00155 
00156     return ok;
00157 }
00158 
00159 
00160 bool Foam::functionObjectList::end()
00161 {
00162     bool ok = true;
00163 
00164     if (execution_)
00165     {
00166         if (!updated_)
00167         {
00168             read();
00169         }
00170 
00171         forAllIter
00172         (
00173             PtrList<functionObject>,
00174             static_cast<PtrList<functionObject>&>(*this),
00175             iter
00176         )
00177         {
00178             ok = iter().end() && ok;
00179         }
00180     }
00181 
00182     return ok;
00183 }
00184 
00185 
00186 bool Foam::functionObjectList::read()
00187 {
00188     bool ok = true;
00189     updated_ = execution_;
00190 
00191     // avoid reading/initializing if execution is off
00192     if (!execution_)
00193     {
00194         return ok;
00195     }
00196 
00197     // Update existing and add new functionObjects
00198     const entry* entryPtr = parentDict_.lookupEntryPtr("functions",false,false);
00199     if (entryPtr)
00200     {
00201         PtrList<functionObject> newPtrs;
00202         List<SHA1Digest> newDigs;
00203         HashTable<label> newIndices;
00204 
00205         label nFunc = 0;
00206 
00207         if (entryPtr->isDict())
00208         {
00209             // a dictionary of functionObjects
00210             const dictionary& functionDicts = entryPtr->dict();
00211 
00212             newPtrs.setSize(functionDicts.size());
00213             newDigs.setSize(functionDicts.size());
00214 
00215             forAllConstIter(dictionary, functionDicts, iter)
00216             {
00217                 // safety:
00218                 if (!iter().isDict())
00219                 {
00220                     continue;
00221                 }
00222                 const word& key = iter().keyword();
00223                 const dictionary& dict = iter().dict();
00224 
00225                 newDigs[nFunc] = dict.digest();
00226 
00227                 label oldIndex;
00228                 functionObject* objPtr = remove(key, oldIndex);
00229                 if (objPtr)
00230                 {
00231                     // an existing functionObject, and dictionary changed
00232                     if (newDigs[nFunc] != digests_[oldIndex])
00233                     {
00234                         ok = objPtr->read(dict) && ok;
00235                     }
00236                 }
00237                 else
00238                 {
00239                     // new functionObject
00240                     objPtr = functionObject::New(key, time_, dict).ptr();
00241                     ok = objPtr->start() && ok;
00242                 }
00243 
00244                 newPtrs.set(nFunc, objPtr);
00245                 newIndices.insert(key, nFunc);
00246                 nFunc++;
00247             }
00248         }
00249         else
00250         {
00251             // a list of functionObjects
00252             PtrList<entry> functionDicts(entryPtr->stream());
00253 
00254             newPtrs.setSize(functionDicts.size());
00255             newDigs.setSize(functionDicts.size());
00256 
00257             forAllIter(PtrList<entry>, functionDicts, iter)
00258             {
00259                 // safety:
00260                 if (!iter().isDict())
00261                 {
00262                     continue;
00263                 }
00264                 const word& key = iter().keyword();
00265                 const dictionary& dict = iter().dict();
00266 
00267                 newDigs[nFunc] = dict.digest();
00268 
00269                 label oldIndex;
00270                 functionObject* objPtr = remove(key, oldIndex);
00271                 if (objPtr)
00272                 {
00273                     // an existing functionObject, and dictionary changed
00274                     if (newDigs[nFunc] != digests_[oldIndex])
00275                     {
00276                         ok = objPtr->read(dict) && ok;
00277                     }
00278                 }
00279                 else
00280                 {
00281                     // new functionObject
00282                     objPtr = functionObject::New(key, time_, dict).ptr();
00283                     ok = objPtr->start() && ok;
00284                 }
00285 
00286                 newPtrs.set(nFunc, objPtr);
00287                 newIndices.insert(key, nFunc);
00288                 nFunc++;
00289             }
00290         }
00291 
00292         // safety:
00293         newPtrs.setSize(nFunc);
00294         newDigs.setSize(nFunc);
00295 
00296         // updating the PtrList of functionObjects also deletes any existing,
00297         // but unused functionObjects
00298         PtrList<functionObject>::transfer(newPtrs);
00299         digests_.transfer(newDigs);
00300         indices_.transfer(newIndices);
00301     }
00302     else
00303     {
00304         PtrList<functionObject>::clear();
00305         digests_.clear();
00306         indices_.clear();
00307     }
00308 
00309     return ok;
00310 }
00311 
00312 
00313 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd