initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / sets / cellSources / faceToCell / faceToCell.C
blob270e58f30064bb5caddf0fdfeb1714231f18dc4c
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
27 \*---------------------------------------------------------------------------*/
29 #include "faceToCell.H"
30 #include "polyMesh.H"
31 #include "faceSet.H"
33 #include "addToRunTimeSelectionTable.H"
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 namespace Foam
40 defineTypeNameAndDebug(faceToCell, 0);
42 addToRunTimeSelectionTable(topoSetSource, faceToCell, word);
44 addToRunTimeSelectionTable(topoSetSource, faceToCell, istream);
49 Foam::topoSetSource::addToUsageTable Foam::faceToCell::usage_
51     faceToCell::typeName,
52     "\n    Usage: faceToCell <faceSet> neighbour|owner|any|all\n\n"
53     "    Select cells that are the owner|neighbour|any"
54     " of the faces in the faceSet or where all faces are in the faceSet\n\n"
57 template<>
58 const char* Foam::NamedEnum<Foam::faceToCell::faceAction, 4>::names[] =
60     "neighbour",
61     "owner",
62     "any",
63     "all"
66 const Foam::NamedEnum<Foam::faceToCell::faceAction, 4>
67     Foam::faceToCell::faceActionNames_;
70 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
72 void Foam::faceToCell::combine(topoSet& set, const bool add) const
74     // Load the set
75     faceSet loadedSet(mesh_, setName_);
78     // Handle owner/neighbour/any selection
79     for
80     (
81         faceSet::const_iterator iter = loadedSet.begin();
82         iter != loadedSet.end();
83         ++iter
84     )
85     {
86         label faceI = iter.key();
88         if ((option_ == OWNER) || (option_ == ANY))
89         {
90             label cellI = mesh_.faceOwner()[faceI];
92             addOrDelete(set, cellI, add);
93         }
95         if (mesh_.isInternalFace(faceI))
96         {
97             if ((option_ == NEIGHBOUR) || (option_ == ANY))
98             {
99                 label cellI = mesh_.faceNeighbour()[faceI];
101                 addOrDelete(set, cellI, add);
102             }
103         }
104     }
106     // Handle all selection.
107     if (option_ == ALL)
108     {
109         // Count number of selected faces per cell.
111         Map<label> facesPerCell(loadedSet.size());
113         for
114         (
115             faceSet::const_iterator iter = loadedSet.begin();
116             iter != loadedSet.end();
117             ++iter
118         )
119         {
120             label faceI = iter.key();
122             label own = mesh_.faceOwner()[faceI];
124             Map<label>::iterator fndOwn = facesPerCell.find(own);
126             if (fndOwn == facesPerCell.end())
127             {
128                 facesPerCell.insert(own, 1);
129             }
130             else
131             {
132                 fndOwn()++;
133             }
135             if (mesh_.isInternalFace(faceI))
136             {
137                 label nei = mesh_.faceNeighbour()[faceI];
139                 Map<label>::iterator fndNei = facesPerCell.find(nei);
141                 if (fndNei == facesPerCell.end())
142                 {
143                     facesPerCell.insert(nei, 1);
144                 }
145                 else
146                 {
147                     fndNei()++;
148                 }
149             }
150         }
152         // Include cells that are referenced as many times as they have faces
153         // -> all faces in set.
154         for
155         (
156             Map<label>::const_iterator iter = facesPerCell.begin();
157             iter != facesPerCell.end();
158             ++iter
159         )
160         {
161             label cellI = iter.key();
163             if (iter() == mesh_.cells()[cellI].size())
164             {
165                 addOrDelete(set, cellI, add);
166             }
167         }
168     }
172 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
174 // Construct from components
175 Foam::faceToCell::faceToCell
177     const polyMesh& mesh,
178     const word& setName,
179     const faceAction option
182     topoSetSource(mesh),
183     setName_(setName),
184     option_(option)
188 // Construct from dictionary
189 Foam::faceToCell::faceToCell
191     const polyMesh& mesh,
192     const dictionary& dict          
195     topoSetSource(mesh),
196     setName_(dict.lookup("set")),
197     option_(faceActionNames_.read(dict.lookup("option")))
201 // Construct from Istream
202 Foam::faceToCell::faceToCell
204     const polyMesh& mesh,
205     Istream& is
208     topoSetSource(mesh),
209     setName_(checkIs(is)),
210     option_(faceActionNames_.read(checkIs(is)))
214 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
216 Foam::faceToCell::~faceToCell()
220 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
222 void Foam::faceToCell::applyToSet
224     const topoSetSource::setAction action,
225     topoSet& set
226 ) const
228     if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
229     {
230         Info<< "    Adding cells according to faceSet " << setName_
231             << " ..." << endl;
233         combine(set, true);
234     }
235     else if (action == topoSetSource::DELETE)
236     {
237         Info<< "    Removing cells according to faceSet " << setName_
238             << " ..." << endl;
240         combine(set, false);
241     }
245 // ************************************************************************* //