initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / meshes / polyMesh / zones / faceZone / faceZone.C
blob0ce9de38a8bdd49a5203d82e31893d6dab5f202b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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     A subset of mesh faces.
28 \*---------------------------------------------------------------------------*/
30 #include "faceZone.H"
31 #include "addToRunTimeSelectionTable.H"
32 #include "faceZoneMesh.H"
33 #include "polyMesh.H"
34 #include "primitiveMesh.H"
35 #include "demandDrivenData.H"
36 #include "mapPolyMesh.H"
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 namespace Foam
42     defineTypeNameAndDebug(faceZone, 0);
43     defineRunTimeSelectionTable(faceZone, dictionary);
44     addToRunTimeSelectionTable(faceZone, faceZone, dictionary);
47 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
49 void Foam::faceZone::calcFaceZonePatch() const
51     if (debug)
52     {
53         Info<< "void faceZone::calcFaceZonePatch() const : "
54             << "Calculating primitive patch"
55             << endl;
56     }
58     if (patchPtr_)
59     {
60         FatalErrorIn
61         (
62             "void faceZone::calcFaceZonePatch() const"
63         )   << "primitive face zone patch already calculated"
64             << abort(FatalError);
65     }
67     patchPtr_ =
68         new primitiveFacePatch
69         (
70             faceList(size()),
71             zoneMesh().mesh().points()
72         );
74     primitiveFacePatch& patch = *patchPtr_;
76     const faceList& f = zoneMesh().mesh().faces();
78     const labelList& addr = *this;
79     const boolList& flip = flipMap();
81     forAll (addr, faceI)
82     {
83         if (flip[faceI])
84         {
85             patch[faceI] = f[addr[faceI]].reverseFace();
86         }
87         else
88         {
89             patch[faceI] = f[addr[faceI]];
90         }
91     }
93     if (debug)
94     {
95         Info<< "void faceZone::calcFaceZonePatch() const : "
96             << "Finished calculating primitive patch"
97             << endl;
98     }
102 const Foam::Map<Foam::label>& Foam::faceZone::faceLookupMap() const
104     if (!faceLookupMapPtr_)
105     {
106         calcFaceLookupMap();
107     }
109     return *faceLookupMapPtr_;
113 void Foam::faceZone::calcFaceLookupMap() const
115     if (debug)
116     {
117         Info<< "void faceZone::calcFaceLookupMap() const : "
118             << "Calculating face lookup map"
119             << endl;
120     }
122     if (faceLookupMapPtr_)
123     {
124         FatalErrorIn
125         (
126             "void faceZone::calcFaceLookupMap() const"
127         )   << "face lookup map already calculated"
128             << abort(FatalError);
129     }
131     const labelList& addr = *this;
133     faceLookupMapPtr_ = new Map<label>(2*addr.size());
134     Map<label>& flm = *faceLookupMapPtr_;
136     forAll (addr, faceI)
137     {
138         flm.insert(addr[faceI], faceI);
139     }
141     if (debug)
142     {
143         Info<< "void faceZone::calcFaceLookupMap() const : "
144             << "Finished calculating face lookup map"
145             << endl;
146     }
150 void Foam::faceZone::calcCellLayers() const
152     if (debug)
153     {
154         Info<< "void Foam::faceZone::calcCellLayers() const : "
155             << "calculating master cells"
156             << endl;
157     }
159     // It is an error to attempt to recalculate edgeCells
160     // if the pointer is already set
161     if (masterCellsPtr_ || slaveCellsPtr_)
162     {
163         FatalErrorIn("void faceZone::calcCellLayers() const")
164             << "cell layers already calculated"
165             << abort(FatalError);
166     }
167     else
168     {
169         // Go through all the faces in the master zone.  Choose the
170         // master or slave cell based on the face flip
172         const labelList& own = zoneMesh().mesh().faceOwner();
173         const labelList& nei = zoneMesh().mesh().faceNeighbour();
175         const labelList& mf = *this;
177         const boolList& faceFlip = flipMap();
179         masterCellsPtr_ = new labelList(mf.size());
180         labelList& mc = *masterCellsPtr_;
182         slaveCellsPtr_ = new labelList(mf.size());
183         labelList& sc = *slaveCellsPtr_;
185         forAll (mf, faceI)
186         {
187             if (!faceFlip[faceI])
188             {
189                 // Face is oriented correctly, no flip needed
190                 mc[faceI] = nei[mf[faceI]];
191                 sc[faceI] = own[mf[faceI]];
192             }
193             else
194             {
195                 mc[faceI] = own[mf[faceI]];
196                 sc[faceI] = nei[mf[faceI]];
197             }
198         }
199         //Info << "masterCells: " << mc << endl;
200         //Info << "slaveCells: " << sc << endl;
201     }
205 void Foam::faceZone::checkAddressing() const
207     if (size() != flipMap_.size())
208     {
209         FatalErrorIn("void Foam::faceZone::checkAddressing() const")
210             << "Different sizes of the addressing and flipMap arrays.  "
211             << "Size of addressing: " << size()
212             << " size of flip map: " << flipMap_.size()
213             << abort(FatalError);
214     }
218 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
220 // Construct from components
221 Foam::faceZone::faceZone
223     const word& name,
224     const labelList& addr,
225     const boolList& fm,
226     const label index,
227     const faceZoneMesh& zm
230     labelList(addr),
231     name_(name),
232     flipMap_(fm),
233     index_(index),
234     zoneMesh_(zm),
235     patchPtr_(NULL),
236     masterCellsPtr_(NULL),
237     slaveCellsPtr_(NULL),
238     mePtr_(NULL),
239     faceLookupMapPtr_(NULL)
241     checkAddressing();
245 // Construct from dictionary
246 Foam::faceZone::faceZone
248     const word& name,
249     const dictionary& dict,
250     const label index,
251     const faceZoneMesh& zm
254     labelList(dict.lookup("faceLabels")),
255     name_(name),
256     flipMap_(dict.lookup("flipMap")),
257     index_(index),
258     zoneMesh_(zm),
259     patchPtr_(NULL),
260     masterCellsPtr_(NULL),
261     slaveCellsPtr_(NULL),
262     mePtr_(NULL),
263     faceLookupMapPtr_(NULL)
265     checkAddressing();
269 // Construct given the original zone and resetting the
270 // face list and zone mesh information
271 Foam::faceZone::faceZone
273     const faceZone& fz,
274     const labelList& addr,
275     const boolList& fm,
276     const label index,
277     const faceZoneMesh& zm
280     labelList(addr),
281     name_(fz.name()),
282     flipMap_(fm),
283     index_(index),
284     zoneMesh_(zm),
285     patchPtr_(NULL),
286     masterCellsPtr_(NULL),
287     slaveCellsPtr_(NULL),
288     mePtr_(NULL),
289     faceLookupMapPtr_(NULL)
291     checkAddressing();
295 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
297 Foam::faceZone::~faceZone()
299     clearAddressing();
303 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
305 Foam::label Foam::faceZone::whichFace(const label globalFaceID) const
307     const Map<label>& flm = faceLookupMap();
309     Map<label>::const_iterator flmIter = flm.find(globalFaceID);
311     if (flmIter == flm.end())
312     {
313         return -1;
314     }
315     else
316     {
317         return flmIter();
318     }
322 const Foam::faceZoneMesh& Foam::faceZone::zoneMesh() const
324     return zoneMesh_;
328 const Foam::primitiveFacePatch& Foam::faceZone::operator()() const
330     if (!patchPtr_)
331     {
332         calcFaceZonePatch();
333     }
335     return *patchPtr_;
339 const Foam::labelList& Foam::faceZone::masterCells() const
341     if (!masterCellsPtr_)
342     {
343         calcCellLayers();
344     }
346     return *masterCellsPtr_;
350 const Foam::labelList& Foam::faceZone::slaveCells() const
352     if (!slaveCellsPtr_)
353     {
354         calcCellLayers();
355     }
357     return *slaveCellsPtr_;
361 const Foam::labelList& Foam::faceZone::meshEdges() const
363     if (!mePtr_)
364     {
365         labelList faceCells(size());
367         const labelList& own = zoneMesh().mesh().faceOwner();
369         const labelList& faceLabels = *this;
371         forAll (faceCells, faceI)
372         {
373             faceCells[faceI] = own[faceLabels[faceI]];
374         }
376         mePtr_ =
377             new labelList
378             (
379                 operator()().meshEdges
380                 (
381                     zoneMesh().mesh().edges(),
382                     zoneMesh().mesh().cellEdges(),
383                     faceCells
384                 )
385             );
386     }
388     return *mePtr_;
392 void Foam::faceZone::clearAddressing()
394     deleteDemandDrivenData(patchPtr_);
396     deleteDemandDrivenData(masterCellsPtr_);
397     deleteDemandDrivenData(slaveCellsPtr_);
399     deleteDemandDrivenData(mePtr_);
400     deleteDemandDrivenData(faceLookupMapPtr_);
404 void Foam::faceZone::resetAddressing
406     const labelList& addr,
407     const boolList& flipMap
410     clearAddressing();
411     labelList::operator=(addr);
412     flipMap_ = flipMap;
416 void Foam::faceZone::updateMesh(const mapPolyMesh& mpm)
418     clearAddressing();
420     labelList newAddressing(size());
421     boolList newFlipMap(flipMap_.size());
422     label nFaces = 0;
424     const labelList& faceMap = mpm.reverseFaceMap();
426     forAll(*this, i)
427     {
428         label faceI = operator[](i);
430         if (faceMap[faceI] >= 0)
431         {
432             newAddressing[nFaces] = faceMap[faceI];
433             newFlipMap[nFaces] = flipMap_[i];       // Keep flip map.
434             nFaces++;
435         }
436     }
438     newAddressing.setSize(nFaces);
439     newFlipMap.setSize(nFaces);
441     transfer(newAddressing);
442     flipMap_.transfer(newFlipMap);
446 bool Foam::faceZone::checkDefinition(const bool report) const
448     const labelList& addr = *this;
450     bool boundaryError = false;
452     forAll(addr, i)
453     {
454         if (addr[i] < 0 || addr[i] >= zoneMesh().mesh().faces().size())
455         {
456             boundaryError = true;
458             if (report)
459             {
460                 SeriousErrorIn
461                 (
462                     "bool faceZone::checkDefinition("
463                     "const bool report) const"
464                 )   << "Zone " << name()
465                     << " contains invalid face label " << addr[i] << nl
466                     << "Valid face labels are 0.."
467                     << zoneMesh().mesh().faces().size()-1 << endl;
468             }
469         }
470     }
471     return boundaryError;
475 void Foam::faceZone::movePoints(const pointField& p)
477     if (patchPtr_)
478     {
479         patchPtr_->movePoints(p);
480     }
483 void Foam::faceZone::write(Ostream& os) const
485     os  << nl << name()
486         << nl << static_cast<const labelList&>(*this)
487         << nl << flipMap();
491 void Foam::faceZone::writeDict(Ostream& os) const
493     os  << nl << name() << nl << token::BEGIN_BLOCK << nl
494         << "    type " << type() << token::END_STATEMENT << nl;
496     writeEntry("faceLabels", os);
497     flipMap().writeEntry("flipMap", os);
499     os  << token::END_BLOCK << endl;
503 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
505 Foam::Ostream& Foam::operator<<(Ostream& os, const faceZone& p)
507     p.write(os);
508     os.check("Ostream& operator<<(Ostream& f, const faceZone& p");
509     return os;
513 // ************************************************************************* //