1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
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
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 \*---------------------------------------------------------------------------*/
28 #include "demandDrivenData.H"
29 #include "stringListOps.H"
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 template<class ZoneType, class MeshType>
35 void Foam::ZoneMesh<ZoneType, MeshType>::calcZoneMap() const
37 // It is an error to attempt to recalculate cellEdges
38 // if the pointer is already set
41 FatalErrorIn("void ZoneMesh<ZoneType>::calcZoneMap() const")
42 << "zone map already calculated"
47 // Count number of objects in all zones
52 nObjects += this->operator[](zoneI).size();
55 zoneMapPtr_ = new Map<label>(2*nObjects);
56 Map<label>& zm = *zoneMapPtr_;
58 // Fill in objects of all zones into the map. The key is the global
59 // object index and the result is the zone index
62 const labelList& zoneObjects = this->operator[](zoneI);
64 forAll(zoneObjects, objI)
66 zm.insert(zoneObjects[objI], zoneI);
73 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
75 // Read constructor given IOobject and a MeshType reference
76 template<class ZoneType, class MeshType>
77 Foam::ZoneMesh<ZoneType, MeshType>::ZoneMesh
90 readOpt() == IOobject::MUST_READ
91 || readOpt() == IOobject::MUST_READ_IF_MODIFIED
92 || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
95 if (readOpt() == IOobject::MUST_READ_IF_MODIFIED)
99 "ZoneMesh::ZoneMesh\n"
101 " const IOobject&,\n"
104 ) << "Specified IOobject::MUST_READ_IF_MODIFIED but class"
105 << " does not support automatic rereading."
109 PtrList<ZoneType>& zones = *this;
112 Istream& is = readStream(typeName);
114 PtrList<entry> patchEntries(is);
115 zones.setSize(patchEntries.size());
124 patchEntries[zoneI].keyword(),
125 patchEntries[zoneI].dict(),
132 // Check state of IOstream
136 "(const IOobject&, const MeshType&)"
143 // No files found. Force a write of zero-sized zones
149 // Construct given size. Zones will be set later
150 template<class ZoneType, class MeshType>
151 Foam::ZoneMesh<ZoneType, MeshType>::ZoneMesh
154 const MeshType& mesh,
158 PtrList<ZoneType>(size),
165 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
167 template<class ZoneType, class MeshType>
168 Foam::ZoneMesh<ZoneType, MeshType>::~ZoneMesh()
174 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
176 // Map of zones for quick zone lookup
177 template<class ZoneType, class MeshType>
178 const Foam::Map<Foam::label>&
179 Foam::ZoneMesh<ZoneType, MeshType>::zoneMap() const
190 // Given a global object index, return the zone it is in.
191 // If object does not belong to any zones, return -1
192 template<class ZoneType, class MeshType>
193 Foam::label Foam::ZoneMesh<ZoneType, MeshType>::whichZone
195 const label objectIndex
198 const Map<label>& zm = zoneMap();
199 Map<label>::const_iterator zmIter = zm.find(objectIndex);
201 if (zmIter == zm.end())
212 // Return a list of zone names
213 template<class ZoneType, class MeshType>
214 Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::types() const
216 const PtrList<ZoneType>& zones = *this;
218 wordList lst(zones.size());
222 lst[zoneI] = zones[zoneI].type();
229 // Return a list of zone names
230 template<class ZoneType, class MeshType>
231 Foam::wordList Foam::ZoneMesh<ZoneType, MeshType>::names() const
233 const PtrList<ZoneType>& zones = *this;
235 wordList lst(zones.size());
239 lst[zoneI] = zones[zoneI].name();
246 template<class ZoneType, class MeshType>
247 Foam::labelList Foam::ZoneMesh<ZoneType, MeshType>::findIndices
258 indices = findStrings(key, this->names());
262 indices.setSize(this->size());
266 if (key == operator[](i).name())
268 indices[nFound++] = i;
271 indices.setSize(nFound);
279 template<class ZoneType, class MeshType>
280 Foam::label Foam::ZoneMesh<ZoneType, MeshType>::findIndex
289 labelList indices = this->findIndices(key);
291 // return first element
292 if (!indices.empty())
301 if (key == operator[](i).name())
314 template<class ZoneType, class MeshType>
315 Foam::label Foam::ZoneMesh<ZoneType, MeshType>::findZoneID
320 const PtrList<ZoneType>& zones = *this;
324 if (zones[zoneI].name() == zoneName)
333 Info<< "label ZoneMesh<ZoneType>::findZoneID(const word&) const : "
334 << "Zone named " << zoneName << " not found. "
335 << "List of available zone names: " << names() << endl;
343 template<class ZoneType, class MeshType>
344 Foam::PackedBoolList Foam::ZoneMesh<ZoneType, MeshType>::findMatching
351 const labelList indices = this->findIndices(key);
354 lst |= static_cast<const labelList&>(this->operator[](indices[i]));
361 template<class ZoneType, class MeshType>
362 void Foam::ZoneMesh<ZoneType, MeshType>::clearAddressing()
364 deleteDemandDrivenData(zoneMapPtr_);
366 PtrList<ZoneType>& zones = *this;
370 zones[zoneI].clearAddressing();
375 template<class ZoneType, class MeshType>
376 void Foam::ZoneMesh<ZoneType, MeshType>::clear()
379 PtrList<ZoneType>::clear();
383 // Check zone definition
384 template<class ZoneType, class MeshType>
385 bool Foam::ZoneMesh<ZoneType, MeshType>::checkDefinition
390 bool inError = false;
392 const PtrList<ZoneType>& zones = *this;
396 inError |= zones[zoneI].checkDefinition(report);
402 template<class ZoneType, class MeshType>
403 bool Foam::ZoneMesh<ZoneType, MeshType>::checkParallelSync
408 if (!Pstream::parRun())
414 const PtrList<ZoneType>& zones = *this;
416 bool hasError = false;
419 List<wordList> allNames(Pstream::nProcs());
420 allNames[Pstream::myProcNo()] = this->names();
421 Pstream::gatherList(allNames);
422 Pstream::scatterList(allNames);
424 List<wordList> allTypes(Pstream::nProcs());
425 allTypes[Pstream::myProcNo()] = this->types();
426 Pstream::gatherList(allTypes);
427 Pstream::scatterList(allTypes);
429 // Have every processor check but only master print error.
431 for (label procI = 1; procI < allNames.size(); procI++)
435 (allNames[procI] != allNames[0])
436 || (allTypes[procI] != allTypes[0])
441 if (debug || (report && Pstream::master()))
443 Info<< " ***Inconsistent zones across processors, "
444 "processor 0 has zone names:" << allNames[0]
445 << " zone types:" << allTypes[0]
446 << " processor " << procI << " has zone names:"
448 << " zone types:" << allTypes[procI]
459 if (zones[zoneI].checkParallelSync(false))
463 if (debug || (report && Pstream::master()))
465 Info<< " ***Zone " << zones[zoneI].name()
466 << " of type " << zones[zoneI].type()
467 << " is not correctly synchronised"
468 << " across coupled boundaries."
469 << " (coupled faces are either not both "
470 << " present in set or have same flipmap)" << endl;
480 // Correct zone mesh after moving points
481 template<class ZoneType, class MeshType>
482 void Foam::ZoneMesh<ZoneType, MeshType>::movePoints(const pointField& p)
484 PtrList<ZoneType>& zones = *this;
488 zones[zoneI].movePoints(p);
493 // writeData member function required by regIOobject
494 template<class ZoneType, class MeshType>
495 bool Foam::ZoneMesh<ZoneType, MeshType>::writeData(Ostream& os) const
501 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
503 template<class ZoneType, class MeshType>
504 const ZoneType& Foam::ZoneMesh<ZoneType, MeshType>::operator[]
509 const label zoneI = findZoneID(zoneName);
515 "ZoneMesh<ZoneType>::operator[](const word&) const"
516 ) << "Zone named " << zoneName << " not found." << nl
517 << "Available zone names: " << names() << endl
518 << abort(FatalError);
521 return operator[](zoneI);
525 template<class ZoneType, class MeshType>
526 ZoneType& Foam::ZoneMesh<ZoneType, MeshType>::operator[]
531 const label zoneI = findZoneID(zoneName);
537 "ZoneMesh<ZoneType>::operator[](const word&)"
538 ) << "Zone named " << zoneName << " not found." << nl
539 << "Available zone names: " << names() << endl
540 << abort(FatalError);
543 return operator[](zoneI);
547 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
549 template<class ZoneType, class MeshType>
550 Foam::Ostream& Foam::operator<<
553 const ZoneMesh<ZoneType, MeshType>& zones
556 os << zones.size() << nl << token::BEGIN_LIST;
560 zones[zoneI].writeDict(os);
563 os << token::END_LIST;
569 // ************************************************************************* //