initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / mesh / manipulation / setsToZones / setsToZones.C
bloba369a195b8c8dcf1d22b6bb60656993fe04c5417
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     Add pointZones/faceZones/cellZones to the mesh from similar named
27     pointSets/faceSets/cellSets.
29     There is one catch: for faceZones you also need to specify a flip
30     condition which basically denotes the side of the face. In this app
31     it reads a cellSet (xxxCells if 'xxx' is the name of the faceSet) which
32     is the masterCells of the zone.
33     There are lots of situations in which this will go wrong but it is the
34     best I can think of for now.
36     If one is not interested in sideNess specify the -noFlipMap
37     command line option.
39 \*---------------------------------------------------------------------------*/
41 #include "argList.H"
42 #include "Time.H"
43 #include "polyMesh.H"
44 #include "IStringStream.H"
45 #include "cellSet.H"
46 #include "faceSet.H"
47 #include "pointSet.H"
48 #include "OFstream.H"
49 #include "IFstream.H"
50 #include "IOobjectList.H"
51 #include "SortableList.H"
53 using namespace Foam;
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 // Main program:
59 int main(int argc, char *argv[])
61     argList::validOptions.insert("noFlipMap", "");
63 #   include "addTimeOptions.H"
64 #   include "setRootCase.H"
65 #   include "createTime.H"
67     bool noFlipMap = args.optionFound("noFlipMap");
69     // Get times list
70     instantList Times = runTime.times();
72     label startTime = Times.size()-1;
73     label endTime = Times.size();
75     // check -time and -latestTime options
76 #   include "checkTimeOption.H"
78     runTime.setTime(Times[startTime], startTime);
80 #   include "createPolyMesh.H"
82     // Search for list of objects for the time of the mesh
83     IOobjectList objects
84     (
85         mesh,
86         mesh.pointsInstance(),
87         polyMesh::meshSubDir/"sets"
88     );
90     Info<< "Searched : " << mesh.pointsInstance()/polyMesh::meshSubDir/"sets"
91         << nl
92         << "Found    : " << objects.names() << nl
93         << endl;
96     IOobjectList pointObjects(objects.lookupClass(pointSet::typeName));
98     //Pout<< "pointSets:" << pointObjects.names() << endl;
100     for
101     (
102         IOobjectList::const_iterator iter = pointObjects.begin();
103         iter != pointObjects.end();
104         ++iter
105     )
106     {
107         // Not in memory. Load it.
108         pointSet set(*iter());
109         SortableList<label> pointLabels(set.toc());
111         label zoneID = mesh.pointZones().findZoneID(set.name());
112         if (zoneID == -1)
113         {
114             Info<< "Adding set " << set.name() << " as a pointZone." << endl;
115             label sz = mesh.pointZones().size();
116             mesh.pointZones().setSize(sz+1);
117             mesh.pointZones().set
118             (
119                 sz,
120                 new pointZone
121                 (
122                     set.name(),             //name
123                     pointLabels,            //addressing
124                     sz,                     //index
125                     mesh.pointZones()       //pointZoneMesh
126                 )
127             );
128             mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
129             mesh.pointZones().instance() = mesh.facesInstance();
130         }
131         else
132         {
133             Info<< "Overwriting contents of existing pointZone " << zoneID
134                 << " with that of set " << set.name() << "." << endl;
135             mesh.pointZones()[zoneID] = pointLabels;
136             mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
137             mesh.pointZones().instance() = mesh.facesInstance();
138         }
139     }
143     IOobjectList faceObjects(objects.lookupClass(faceSet::typeName));
145     HashSet<word> slaveCellSets;
147     //Pout<< "faceSets:" << faceObjects.names() << endl;
149     for
150     (
151         IOobjectList::const_iterator iter = faceObjects.begin();
152         iter != faceObjects.end();
153         ++iter
154     )
155     {
156         // Not in memory. Load it.
157         faceSet set(*iter());
158         SortableList<label> faceLabels(set.toc());
160         DynamicList<label> addressing(set.size());
161         DynamicList<bool> flipMap(set.size());
163         if (!noFlipMap)
164         {
165             word setName(set.name() + "SlaveCells");
167             Info<< "Trying to load cellSet " << setName
168                 << " to find out the slave side of the zone." << nl
169                 << "If you do not care about the flipMap"
170                 << " (i.e. do not use the sideness)" << nl
171                 << "use the -noFlipMap command line option."
172                 << endl;
174             // Load corresponding cells
175             cellSet cells(mesh, setName);
177             // Store setName to exclude from cellZones further on
178             slaveCellSets.insert(setName);
180             forAll(faceLabels, i)
181             {
182                 label faceI = faceLabels[i];
184                 bool flip = false;
186                 if (mesh.isInternalFace(faceI))
187                 {
188                     if
189                     (
190                         cells.found(mesh.faceOwner()[faceI])
191                     && !cells.found(mesh.faceNeighbour()[faceI])
192                     )
193                     {
194                         flip = false;
195                     }
196                     else if
197                     (
198                        !cells.found(mesh.faceOwner()[faceI])
199                      && cells.found(mesh.faceNeighbour()[faceI])
200                     )
201                     {
202                         flip = true;
203                     }
204                     else
205                     {
206                         FatalErrorIn(args.executable())
207                             << "One of owner or neighbour of internal face "
208                             << faceI << " should be in cellSet " << cells.name()
209                             << " to be able to determine orientation." << endl
210                             << "Face:" << faceI
211                             << " own:" << mesh.faceOwner()[faceI]
212                             << " OwnInCellSet:"
213                             << cells.found(mesh.faceOwner()[faceI])
214                             << " nei:" << mesh.faceNeighbour()[faceI]
215                             << " NeiInCellSet:"
216                             << cells.found(mesh.faceNeighbour()[faceI])
217                             << abort(FatalError);
218                     }
219                 }
220                 else
221                 {
222                     if (cells.found(mesh.faceOwner()[faceI]))
223                     {
224                         flip = false;
225                     }
226                     else
227                     {
228                         flip = true;
229                     }
230                 }
232                 addressing.append(faceI);
233                 flipMap.append(flip);
234             }
235         }
236         else
237         {
238             // No flip map.
239             forAll(faceLabels, i)
240             {
241                 label faceI = faceLabels[i];
242                 addressing.append(faceI);
243                 flipMap.append(false);
244             }
245         }
247         label zoneID = mesh.faceZones().findZoneID(set.name());
248         if (zoneID == -1)
249         {
250             Info<< "Adding set " << set.name() << " as a faceZone." << endl;
251             label sz = mesh.faceZones().size();
252             mesh.faceZones().setSize(sz+1);
253             mesh.faceZones().set
254             (
255                 sz,
256                 new faceZone
257                 (
258                     set.name(),             //name
259                     addressing.shrink(),    //addressing
260                     flipMap.shrink(),       //flipmap
261                     sz,                     //index
262                     mesh.faceZones()        //pointZoneMesh
263                 )
264             );
265             mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
266             mesh.faceZones().instance() = mesh.facesInstance();
267         }
268         else
269         {
270             Info<< "Overwriting contents of existing faceZone " << zoneID
271                 << " with that of set " << set.name() << "." << endl;
272             mesh.faceZones()[zoneID].resetAddressing
273             (
274                 addressing.shrink(),
275                 flipMap.shrink()
276             );
277             mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
278             mesh.faceZones().instance() = mesh.facesInstance();
279         }
280     }
284     IOobjectList cellObjects(objects.lookupClass(cellSet::typeName));
286     //Pout<< "cellSets:" << cellObjects.names() << endl;
288     for
289     (
290         IOobjectList::const_iterator iter = cellObjects.begin();
291         iter != cellObjects.end();
292         ++iter
293     )
294     {
295         if (!slaveCellSets.found(iter.key()))
296         {
297             // Not in memory. Load it.
298             cellSet set(*iter());
299             SortableList<label> cellLabels(set.toc());
301             label zoneID = mesh.cellZones().findZoneID(set.name());
302             if (zoneID == -1)
303             {
304                 Info<< "Adding set " << set.name() << " as a cellZone." << endl;
305                 label sz = mesh.cellZones().size();
306                 mesh.cellZones().setSize(sz+1);
307                 mesh.cellZones().set
308                 (
309                     sz,
310                     new cellZone
311                     (
312                         set.name(),             //name
313                         cellLabels,             //addressing
314                         sz,                     //index
315                         mesh.cellZones()        //pointZoneMesh
316                     )
317                 );
318                 mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
319                 mesh.cellZones().instance() = mesh.facesInstance();
320             }
321             else
322             {
323                 Info<< "Overwriting contents of existing cellZone " << zoneID
324                     << " with that of set " << set.name() << "." << endl;
325                 mesh.cellZones()[zoneID] = cellLabels;
326                 mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
327                 mesh.cellZones().instance() = mesh.facesInstance();
328             }
329         }
330     }
334     Info<< "Writing mesh." << endl;
336     if (!mesh.write())
337     {
338         FatalErrorIn(args.executable())
339             << "Failed writing polyMesh."
340             << exit(FatalError);
341     }
343     Info<< nl << "End" << endl;
345     return 0;
349 // ************************************************************************* //