FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

cellSet.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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Application
00025     cellSet
00026 
00027 Description
00028     Selects a cell set through a dictionary.
00029 
00030 Usage
00031 
00032     - cellSet [OPTIONS]
00033 
00034     @param -case <dir>\n
00035     Case directory.
00036 
00037     @param -parallel \n
00038     Run in parallel.
00039 
00040     @param -help \n
00041     Display help message.
00042 
00043     @param -doc \n
00044     Display Doxygen API documentation page for this application.
00045 
00046     @param -srcDoc \n
00047     Display Doxygen source documentation page for this application.
00048 
00049 \*---------------------------------------------------------------------------*/
00050 
00051 #include <OpenFOAM/argList.H>
00052 #include <OpenFOAM/Time.H>
00053 #include <OpenFOAM/polyMesh.H>
00054 #include <meshTools/topoSetSource.H>
00055 #include <meshTools/cellSet.H>
00056 
00057 using namespace Foam;
00058 
00059 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00060 
00061 // Copy set
00062 void backup
00063 (
00064     const polyMesh& mesh,
00065     const word& fromName,
00066     const topoSet& fromSet,
00067     const word& toName
00068 )
00069 {
00070     Info<< "Backing up " << fromName << " into " << toName << endl;
00071 
00072     topoSet backupSet(mesh, toName, fromSet);
00073 
00074     backupSet.write();
00075 }
00076 
00077 
00078 // Read and copy set
00079 void backup
00080 (
00081     const polyMesh& mesh,
00082     const word& fromName,
00083     const word& toName
00084 )
00085 {
00086     topoSet fromSet(mesh, fromName, IOobject::READ_IF_PRESENT);
00087 
00088     backup(mesh, fromName, fromSet, toName);
00089 }
00090 
00091 
00092 // Main program:
00093 
00094 int main(int argc, char *argv[])
00095 {
00096 #   include <OpenFOAM/setRootCase.H>
00097 #   include <OpenFOAM/createTime.H>
00098 #   include <OpenFOAM/createPolyMesh.H>
00099 
00100     Info<< "Reading cellSetDict\n" << endl;
00101 
00102     IOdictionary cellSetDict
00103     (
00104         IOobject
00105         (
00106             "cellSetDict",
00107             runTime.system(),
00108             mesh,
00109             IOobject::MUST_READ,
00110             IOobject::NO_WRITE
00111         )
00112     );
00113 
00114 
00115     word setName(cellSetDict.lookup("name"));
00116 
00117     word actionName(cellSetDict.lookup("action"));
00118 
00119     topoSetSource::setAction action = topoSetSource::toAction(actionName);
00120 
00121 
00122     // Create topoSetSources
00123     PtrList<topoSetSource> topoSetSources
00124     (
00125         cellSetDict.lookup("topoSetSources"),
00126         topoSetSource::iNew(mesh)
00127     );
00128 
00129 
00130     // Load set to work
00131     autoPtr<topoSet> currentSetPtr(NULL);
00132     IOobject::readOption r;
00133 
00134     if ((action == topoSetSource::NEW) || (action == topoSetSource::CLEAR))
00135     {
00136         r = IOobject::NO_READ;
00137 
00138         backup(mesh, setName, setName + "_old");
00139 
00140         currentSetPtr.reset
00141         (
00142             new cellSet
00143             (
00144                 mesh,
00145                 setName,
00146                 mesh.nCells()/10+1  // Reasonable size estimate.
00147             )
00148         );
00149     }
00150     else
00151     {
00152         r = IOobject::MUST_READ;
00153 
00154         currentSetPtr.reset
00155         (
00156             new cellSet
00157             (
00158                 mesh,
00159                 setName,
00160                 r
00161             )
00162         );
00163     }
00164 
00165     topoSet& currentSet = currentSetPtr();
00166 
00167     Info<< "Set:" << currentSet.name()
00168         << "  Size:" << currentSet.size()
00169         << "  Action:" << actionName
00170         << endl;
00171 
00172     if ((r == IOobject::MUST_READ) && (action != topoSetSource::LIST))
00173     {
00174         // currentSet has been read so can make copy.
00175         backup(mesh, setName, currentSet, setName + "_old");
00176     }
00177 
00178     if (action == topoSetSource::CLEAR)
00179     {
00180         // Already handled above by not reading
00181     }
00182     else if (action == topoSetSource::INVERT)
00183     {
00184         currentSet.invert(currentSet.maxSize(mesh));
00185     }
00186     else if (action == topoSetSource::LIST)
00187     {
00188         currentSet.writeDebug(Info, mesh, 100);
00189         Info<< endl;
00190     }
00191     else if (action == topoSetSource::SUBSET)
00192     {
00193         // Apply topoSetSources to it to handle new/add/delete
00194         forAll(topoSetSources, topoSetSourceI)
00195         {
00196             // Backup current set.
00197             topoSet oldSet(mesh, currentSet.name() + "_old2", currentSet);
00198 
00199             currentSet.clear();
00200 
00201             topoSetSources[topoSetSourceI].applyToSet
00202             (
00203                 topoSetSource::NEW,
00204                 currentSet
00205             );
00206 
00207             // Combine new value of currentSet with old one.
00208             currentSet.subset(oldSet);
00209         }
00210     }
00211     else
00212     {
00213         // Apply topoSetSources to it to handle new/add/delete
00214         forAll(topoSetSources, topoSetSourceI)
00215         {
00216             topoSetSources[topoSetSourceI].applyToSet(action, currentSet);
00217         }
00218     }
00219 
00220 
00221     if (action != topoSetSource::LIST)
00222     {
00223         // Set has changed.
00224 
00225         // Sync across coupled patches.
00226         currentSet.sync(mesh);
00227 
00228         Info<< "Writing " << currentSet.name()
00229             << " (size " << currentSet.size() << ") to "
00230             << currentSet.instance()/currentSet.local()
00231                /currentSet.name()
00232             << endl << endl;
00233 
00234         currentSet.write();
00235     }
00236 
00237     Info << nl << "End" << endl;
00238 
00239     return 0;
00240 }
00241 
00242 
00243 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines