Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / applications / utilities / mesh / manipulation / setsToZones / setsToZones.C
blob6d5e42fee73534e36a2a282426d59452d5ca74d4
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 Description
25     Add pointZones/faceZones/cellZones to the mesh from similar named
26     pointSets/faceSets/cellSets.
28     There is one catch: for faceZones you also need to specify a flip
29     condition which basically denotes the side of the face. In this app
30     it reads a cellSet (xxxCells if 'xxx' is the name of the faceSet) which
31     is the masterCells of the zone.
32     There are lots of situations in which this will go wrong but it is the
33     best I can think of for now.
35     If one is not interested in sideNess specify the -noFlipMap
36     command line option.
38 \*---------------------------------------------------------------------------*/
40 #include "argList.H"
41 #include "Time.H"
42 #include "polyMesh.H"
43 #include "IStringStream.H"
44 #include "cellSet.H"
45 #include "faceSet.H"
46 #include "pointSet.H"
47 #include "OFstream.H"
48 #include "IFstream.H"
49 #include "IOobjectList.H"
50 #include "SortableList.H"
52 using namespace Foam;
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 // Main program:
58 int main(int argc, char *argv[])
60     argList::addNote
61     (
62         "add point/face/cell Zones from similar named point/face/cell Sets"
63     );
65     argList::addBoolOption
66     (
67         "noFlipMap",
68         "ignore orientation of faceSet"
69     );
71     #include "addRegionOption.H"
72     #include "addTimeOptions.H"
73     #include "setRootCase.H"
74     #include "createTime.H"
76     const bool noFlipMap = args.optionFound("noFlipMap");
78     // Get times list
79     instantList Times = runTime.times();
81     label startTime = Times.size()-1;
82     label endTime = Times.size();
84     // check -time and -latestTime options
85     #include "checkTimeOption.H"
87     runTime.setTime(Times[startTime], startTime);
89     #include "createNamedPolyMesh.H"
91     // Search for list of objects for the time of the mesh
92     word setsInstance = runTime.findInstance
93     (
94         polyMesh::meshSubDir/"sets",
95         word::null,
96         IOobject::MUST_READ,
97         mesh.facesInstance()
98     );
100     IOobjectList objects(mesh, setsInstance, polyMesh::meshSubDir/"sets");
102     Info<< "Searched : " << setsInstance/polyMesh::meshSubDir/"sets"
103         << nl
104         << "Found    : " << objects.names() << nl
105         << endl;
108     IOobjectList pointObjects(objects.lookupClass(pointSet::typeName));
110     //Pout<< "pointSets:" << pointObjects.names() << endl;
112     forAllConstIter(IOobjectList, pointObjects, iter)
113     {
114         // Not in memory. Load it.
115         pointSet set(*iter());
116         SortableList<label> pointLabels(set.toc());
118         label zoneID = mesh.pointZones().findZoneID(set.name());
119         if (zoneID == -1)
120         {
121             Info<< "Adding set " << set.name() << " as a pointZone." << endl;
122             label sz = mesh.pointZones().size();
123             mesh.pointZones().setSize(sz+1);
124             mesh.pointZones().set
125             (
126                 sz,
127                 new pointZone
128                 (
129                     set.name(),             //name
130                     pointLabels,            //addressing
131                     sz,                     //index
132                     mesh.pointZones()       //pointZoneMesh
133                 )
134             );
135             mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
136             mesh.pointZones().instance() = mesh.facesInstance();
137         }
138         else
139         {
140             Info<< "Overwriting contents of existing pointZone " << zoneID
141                 << " with that of set " << set.name() << "." << endl;
142             mesh.pointZones()[zoneID] = pointLabels;
143             mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
144             mesh.pointZones().instance() = mesh.facesInstance();
145         }
146     }
150     IOobjectList faceObjects(objects.lookupClass(faceSet::typeName));
152     HashSet<word> slaveCellSets;
154     //Pout<< "faceSets:" << faceObjects.names() << endl;
156     forAllConstIter(IOobjectList, faceObjects, iter)
157     {
158         // Not in memory. Load it.
159         faceSet set(*iter());
160         SortableList<label> faceLabels(set.toc());
162         DynamicList<label> addressing(set.size());
163         DynamicList<bool> flipMap(set.size());
165         if (noFlipMap)
166         {
167             // No flip map.
168             forAll(faceLabels, i)
169             {
170                 label faceI = faceLabels[i];
171                 addressing.append(faceI);
172                 flipMap.append(false);
173             }
174         }
175         else
176         {
177             const word setName(set.name() + "SlaveCells");
179             Info<< "Trying to load cellSet " << setName
180                 << " to find out the slave side of the zone." << nl
181                 << "If you do not care about the flipMap"
182                 << " (i.e. do not use the sideness)" << nl
183                 << "use the -noFlipMap command line option."
184                 << endl;
186             // Load corresponding cells
187             cellSet cells(mesh, setName);
189             // Store setName to exclude from cellZones further on
190             slaveCellSets.insert(setName);
192             forAll(faceLabels, i)
193             {
194                 label faceI = faceLabels[i];
196                 bool flip = false;
198                 if (mesh.isInternalFace(faceI))
199                 {
200                     if
201                     (
202                         cells.found(mesh.faceOwner()[faceI])
203                     && !cells.found(mesh.faceNeighbour()[faceI])
204                     )
205                     {
206                         flip = false;
207                     }
208                     else if
209                     (
210                        !cells.found(mesh.faceOwner()[faceI])
211                      && cells.found(mesh.faceNeighbour()[faceI])
212                     )
213                     {
214                         flip = true;
215                     }
216                     else
217                     {
218                         FatalErrorIn(args.executable())
219                             << "One of owner or neighbour of internal face "
220                             << faceI << " should be in cellSet " << cells.name()
221                             << " to be able to determine orientation." << endl
222                             << "Face:" << faceI
223                             << " own:" << mesh.faceOwner()[faceI]
224                             << " OwnInCellSet:"
225                             << cells.found(mesh.faceOwner()[faceI])
226                             << " nei:" << mesh.faceNeighbour()[faceI]
227                             << " NeiInCellSet:"
228                             << cells.found(mesh.faceNeighbour()[faceI])
229                             << abort(FatalError);
230                     }
231                 }
232                 else
233                 {
234                     if (cells.found(mesh.faceOwner()[faceI]))
235                     {
236                         flip = false;
237                     }
238                     else
239                     {
240                         flip = true;
241                     }
242                 }
244                 addressing.append(faceI);
245                 flipMap.append(flip);
246             }
247         }
249         label zoneID = mesh.faceZones().findZoneID(set.name());
250         if (zoneID == -1)
251         {
252             Info<< "Adding set " << set.name() << " as a faceZone." << endl;
253             label sz = mesh.faceZones().size();
254             mesh.faceZones().setSize(sz+1);
255             mesh.faceZones().set
256             (
257                 sz,
258                 new faceZone
259                 (
260                     set.name(),             //name
261                     addressing.shrink(),    //addressing
262                     flipMap.shrink(),       //flipmap
263                     sz,                     //index
264                     mesh.faceZones()        //pointZoneMesh
265                 )
266             );
267             mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
268             mesh.faceZones().instance() = mesh.facesInstance();
269         }
270         else
271         {
272             Info<< "Overwriting contents of existing faceZone " << zoneID
273                 << " with that of set " << set.name() << "." << endl;
274             mesh.faceZones()[zoneID].resetAddressing
275             (
276                 addressing.shrink(),
277                 flipMap.shrink()
278             );
279             mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
280             mesh.faceZones().instance() = mesh.facesInstance();
281         }
282     }
286     IOobjectList cellObjects(objects.lookupClass(cellSet::typeName));
288     //Pout<< "cellSets:" << cellObjects.names() << endl;
290     forAllConstIter(IOobjectList, cellObjects, iter)
291     {
292         if (!slaveCellSets.found(iter.key()))
293         {
294             // Not in memory. Load it.
295             cellSet set(*iter());
296             SortableList<label> cellLabels(set.toc());
298             label zoneID = mesh.cellZones().findZoneID(set.name());
299             if (zoneID == -1)
300             {
301                 Info<< "Adding set " << set.name() << " as a cellZone." << endl;
302                 label sz = mesh.cellZones().size();
303                 mesh.cellZones().setSize(sz+1);
304                 mesh.cellZones().set
305                 (
306                     sz,
307                     new cellZone
308                     (
309                         set.name(),             //name
310                         cellLabels,             //addressing
311                         sz,                     //index
312                         mesh.cellZones()        //pointZoneMesh
313                     )
314                 );
315                 mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
316                 mesh.cellZones().instance() = mesh.facesInstance();
317             }
318             else
319             {
320                 Info<< "Overwriting contents of existing cellZone " << zoneID
321                     << " with that of set " << set.name() << "." << endl;
322                 mesh.cellZones()[zoneID] = cellLabels;
323                 mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
324                 mesh.cellZones().instance() = mesh.facesInstance();
325             }
326         }
327     }
331     Info<< "Writing mesh." << endl;
333     if (!mesh.write())
334     {
335         FatalErrorIn(args.executable())
336             << "Failed writing polyMesh."
337             << exit(FatalError);
338     }
340     Info<< "\nEnd\n" << endl;
342     return 0;
346 // ************************************************************************* //