initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / mesh / manipulation / cellSet / cellSet.C
blobca8225d3f2d3d0094db068b45a6a405dd2e42d4e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Selects a cell set through a dictionary.
28 \*---------------------------------------------------------------------------*/
30 #include "argList.H"
31 #include "Time.H"
32 #include "polyMesh.H"
33 #include "topoSetSource.H"
34 #include "cellSet.H"
36 using namespace Foam;
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 // Copy set
41 void backup
43     const polyMesh& mesh,
44     const word& fromName,
45     const topoSet& fromSet,
46     const word& toName
49     Info<< "Backing up " << fromName << " into " << toName << endl;
51     topoSet backupSet(mesh, toName, fromSet);
53     backupSet.write();
57 // Read and copy set
58 void backup
60     const polyMesh& mesh,
61     const word& fromName,
62     const word& toName
65     topoSet fromSet(mesh, fromName, IOobject::READ_IF_PRESENT);
67     backup(mesh, fromName, fromSet, toName);
71 // Main program:
73 int main(int argc, char *argv[])
75 #   include "setRootCase.H"
76 #   include "createTime.H"
77 #   include "createPolyMesh.H"
79     Info<< "Reading cellSetDict\n" << endl;
81     IOdictionary cellSetDict
82     (
83         IOobject
84         (
85             "cellSetDict",
86             runTime.system(),
87             mesh,
88             IOobject::MUST_READ,
89             IOobject::NO_WRITE
90         )
91     );
94     word setName(cellSetDict.lookup("name"));
96     word actionName(cellSetDict.lookup("action"));
98     topoSetSource::setAction action = topoSetSource::toAction(actionName);
101     // Create topoSetSources
102     PtrList<topoSetSource> topoSetSources
103     (
104         cellSetDict.lookup("topoSetSources"),
105         topoSetSource::iNew(mesh)
106     );
109     // Load set to work
110     autoPtr<topoSet> currentSetPtr(NULL);
111     IOobject::readOption r;
113     if ((action == topoSetSource::NEW) || (action == topoSetSource::CLEAR))
114     {
115         r = IOobject::NO_READ;
117         backup(mesh, setName, setName + "_old");
119         currentSetPtr.reset
120         (
121             new cellSet
122             (
123                 mesh,
124                 setName,
125                 mesh.nCells()/10+1  // Reasonable size estimate.
126             )
127         );
128     }
129     else
130     {
131         r = IOobject::MUST_READ;
133         currentSetPtr.reset
134         (
135             new cellSet
136             (
137                 mesh,
138                 setName,
139                 r
140             )
141         );
142     }
144     topoSet& currentSet = currentSetPtr();
146     Info<< "Set:" << currentSet.name()
147         << "  Size:" << currentSet.size()
148         << "  Action:" << actionName
149         << endl;
151     if ((r == IOobject::MUST_READ) && (action != topoSetSource::LIST))
152     {
153         // currentSet has been read so can make copy.
154         backup(mesh, setName, currentSet, setName + "_old");
155     }
157     if (action == topoSetSource::CLEAR)
158     {
159         // Already handled above by not reading
160     }
161     else if (action == topoSetSource::INVERT)
162     {
163         currentSet.invert(currentSet.maxSize(mesh));
164     }
165     else if (action == topoSetSource::LIST)
166     {
167         currentSet.writeDebug(Info, mesh, 100);
168         Info<< endl;
169     }
170     else if (action == topoSetSource::SUBSET)
171     {
172         // Apply topoSetSources to it to handle new/add/delete
173         forAll(topoSetSources, topoSetSourceI)
174         {
175             // Backup current set.
176             topoSet oldSet(mesh, currentSet.name() + "_old2", currentSet);
178             currentSet.clear();
180             topoSetSources[topoSetSourceI].applyToSet
181             (
182                 topoSetSource::NEW,
183                 currentSet
184             );
186             // Combine new value of currentSet with old one.
187             currentSet.subset(oldSet);
188         }
189     }
190     else
191     {
192         // Apply topoSetSources to it to handle new/add/delete
193         forAll(topoSetSources, topoSetSourceI)
194         {
195             topoSetSources[topoSetSourceI].applyToSet(action, currentSet);
196         }
197     }
200     if (action != topoSetSource::LIST)
201     {
202         // Set has changed.
204         // Sync across coupled patches.
205         currentSet.sync(mesh);
207         Info<< "Writing " << currentSet.name()
208             << " (size " << currentSet.size() << ") to "
209             << currentSet.instance()/currentSet.local()
210                /currentSet.name()
211             << endl << endl;
213         currentSet.write();
214     }
216     Info << nl << "End" << endl;
218     return 0;
222 // ************************************************************************* //