Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / applications / utilities / mesh / manipulation / faceSet / faceSet.C
blob8747cf46b1845726af276316f4c2683f1b4430a5
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Selects a face set through a dictionary.
27 \*---------------------------------------------------------------------------*/
29 #include "argList.H"
30 #include "objectRegistry.H"
31 #include "foamTime.H"
32 #include "polyMesh.H"
33 #include "topoSetSource.H"
34 #include "faceSet.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 faceSetDict\n" << endl;
81     IOdictionary faceSetDict
82     (
83         IOobject
84         (
85             "faceSetDict",
86             runTime.system(),
87             mesh,
88             IOobject::MUST_READ,
89             IOobject::NO_WRITE
90         )
91     );
94     word setName(faceSetDict.lookup("name"));
96     word actionName(faceSetDict.lookup("action"));
98     topoSetSource::setAction action = topoSetSource::toAction(actionName);
101     // Create topoSetSources
102     PtrList<topoSetSource> topoSetSources
103     (
104         faceSetDict.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 faceSet
122             (
123                 mesh,
124                 setName,
125                 mesh.nFaces()/10+1  // Reasonable size estimate.
126             )
127         );
128     }
129     else
130     {
131         r = IOobject::MUST_READ;
133         currentSetPtr.reset
134         (
135             new faceSet
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 // ************************************************************************* //