ZoneMesh.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "ZoneMesh.H"
27 #include "entry.H"
28 #include "demandDrivenData.H"
29 #include "stringListOps.H"
30 #include "Pstream.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class ZoneType, class MeshType>
36 {
37  // It is an error to attempt to recalculate cellEdges
38  // if the pointer is already set
39  if (zoneMapPtr_)
40  {
42  << "zone map already calculated"
43  << abort(FatalError);
44  }
45  else
46  {
47  // Count number of objects in all zones
48  label nObjects = 0;
49 
50  forAll(*this, zoneI)
51  {
52  nObjects += this->operator[](zoneI).size();
53  }
54 
55  zoneMapPtr_ = new Map<label>(2*nObjects);
56  Map<label>& zm = *zoneMapPtr_;
57 
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
60  forAll(*this, zoneI)
61  {
62  const labelList& zoneObjects = this->operator[](zoneI);
63 
64  forAll(zoneObjects, objI)
65  {
66  zm.insert(zoneObjects[objI], zoneI);
67  }
68  }
69  }
70 }
71 
72 
73 template<class ZoneType, class MeshType>
75 {
76  if
77  (
78  readOpt() == IOobject::MUST_READ
79  || readOpt() == IOobject::MUST_READ_IF_MODIFIED
80  || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
81  )
82  {
83  if (readOpt() == IOobject::MUST_READ_IF_MODIFIED)
84  {
86  << "Specified IOobject::MUST_READ_IF_MODIFIED but class"
87  << " does not support automatic rereading."
88  << endl;
89  }
90 
91  PtrList<ZoneType>& zones = *this;
92 
93  // Read zones
94  Istream& is = readStream(typeName);
95 
96  PtrList<entry> patchEntries(is);
97  zones.setSize(patchEntries.size());
98 
99  forAll(zones, zoneI)
100  {
101  zones.set
102  (
103  zoneI,
105  (
106  patchEntries[zoneI].keyword(),
107  patchEntries[zoneI].dict(),
108  zoneI,
109  *this
110  )
111  );
112  }
113 
114  // Check state of IOstream
115  is.check
116  (
117  "ZoneMesh::ZoneMesh"
118  "(const IOobject&, const MeshType&)"
119  );
120 
121  close();
122 
123  return true;
124  }
125  else
126  {
127  // Nothing read
128  return false;
129  }
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
134 
135 // Read constructor given IOobject and a MeshType reference
136 template<class ZoneType, class MeshType>
138 (
139  const IOobject& io,
140  const MeshType& mesh
141 )
142 :
143  PtrList<ZoneType>(),
144  regIOobject(io),
145  mesh_(mesh),
146  zoneMapPtr_(NULL)
147 {
148  read();
149 }
150 
151 
152 // Construct given size. Zones will be set later
153 template<class ZoneType, class MeshType>
155 (
156  const IOobject& io,
157  const MeshType& mesh,
158  const label size
159 )
160 :
161  PtrList<ZoneType>(size),
162  regIOobject(io),
163  mesh_(mesh),
164  zoneMapPtr_(NULL)
165 {
166  // Optionally read contents, otherwise keep size
167  read();
168 }
169 
170 
171 template<class ZoneType, class MeshType>
173 (
174  const IOobject& io,
175  const MeshType& mesh,
176  const PtrList<ZoneType>& pzm
177 )
178 :
179  PtrList<ZoneType>(),
180  regIOobject(io),
181  mesh_(mesh),
182  zoneMapPtr_(NULL)
183 {
184  if (!read())
185  {
186  // Nothing read. Use supplied zones
187  PtrList<ZoneType>& zones = *this;
188  zones.setSize(pzm.size());
189  forAll (zones, zoneI)
190  {
191  zones.set(zoneI, pzm[zoneI].clone(*this).ptr());
192  }
193  }
194 }
195 
196 
197 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
198 
199 template<class ZoneType, class MeshType>
201 {
202  clearAddressing();
203 }
204 
205 
206 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
207 
208 // Map of zones for quick zone lookup
209 template<class ZoneType, class MeshType>
212 {
213  if (!zoneMapPtr_)
214  {
215  calcZoneMap();
216  }
217 
218  return *zoneMapPtr_;
219 }
220 
221 
222 // Given a global object index, return the zone it is in.
223 // If object does not belong to any zones, return -1
224 template<class ZoneType, class MeshType>
226 (
227  const label objectIndex
228 ) const
229 {
230  const Map<label>& zm = zoneMap();
231  Map<label>::const_iterator zmIter = zm.find(objectIndex);
232 
233  if (zmIter == zm.end())
234  {
235  return -1;
236  }
237  else
238  {
239  return zmIter();
240  }
241 }
242 
243 
244 // Return a list of zone names
245 template<class ZoneType, class MeshType>
247 {
248  const PtrList<ZoneType>& zones = *this;
249 
250  wordList lst(zones.size());
251 
252  forAll(zones, zoneI)
253  {
254  lst[zoneI] = zones[zoneI].type();
255  }
256 
257  return lst;
258 }
259 
260 
261 // Return a list of zone names
262 template<class ZoneType, class MeshType>
264 {
265  const PtrList<ZoneType>& zones = *this;
266 
267  wordList lst(zones.size());
268 
269  forAll(zones, zoneI)
270  {
271  lst[zoneI] = zones[zoneI].name();
272  }
273 
274  return lst;
275 }
276 
277 
278 template<class ZoneType, class MeshType>
280 (
281  const keyType& key
282 ) const
283 {
284  labelList indices;
285 
286  if (!key.empty())
287  {
288  if (key.isPattern())
289  {
290  indices = findStrings(key, this->names());
291  }
292  else
293  {
294  indices.setSize(this->size());
295  label nFound = 0;
296  forAll(*this, i)
297  {
298  if (key == operator[](i).name())
299  {
300  indices[nFound++] = i;
301  }
302  }
303  indices.setSize(nFound);
304  }
305  }
306 
307  return indices;
308 }
309 
310 
311 template<class ZoneType, class MeshType>
313 (
314  const keyType& key
315 ) const
316 {
317  if (!key.empty())
318  {
319  if (key.isPattern())
320  {
321  labelList indices = this->findIndices(key);
322 
323  // return first element
324  if (!indices.empty())
325  {
326  return indices[0];
327  }
328  }
329  else
330  {
331  forAll(*this, i)
332  {
333  if (key == operator[](i).name())
334  {
335  return i;
336  }
337  }
338  }
339  }
340 
341  // not found
342  return -1;
343 }
344 
345 
346 template<class ZoneType, class MeshType>
348 (
349  const word& zoneName
350 ) const
351 {
352  const PtrList<ZoneType>& zones = *this;
353 
354  forAll(zones, zoneI)
355  {
356  if (zones[zoneI].name() == zoneName)
357  {
358  return zoneI;
359  }
360  }
361 
362  // Zone not found
363  if (debug)
364  {
365  Info<< "label ZoneMesh<ZoneType>::findZoneID(const word&) const : "
366  << "Zone named " << zoneName << " not found. "
367  << "List of available zone names: " << names() << endl;
368  }
369 
370  // not found
371  return -1;
372 }
373 
374 
375 template<class ZoneType, class MeshType>
377 (
378  const keyType& key
379 ) const
380 {
381  PackedBoolList lst;
382 
383  const labelList indices = this->findIndices(key);
384  forAll(indices, i)
385  {
386  lst |= static_cast<const labelList&>(this->operator[](indices[i]));
387  }
388 
389  return lst;
390 }
391 
392 
393 template<class ZoneType, class MeshType>
395 {
396  deleteDemandDrivenData(zoneMapPtr_);
397 
398  PtrList<ZoneType>& zones = *this;
399 
400  forAll(zones, zoneI)
401  {
402  zones[zoneI].clearAddressing();
403  }
404 }
405 
406 
407 template<class ZoneType, class MeshType>
409 {
410  clearAddressing();
412 }
413 
414 
415 // Check zone definition
416 template<class ZoneType, class MeshType>
418 (
419  const bool report
420 ) const
421 {
422  bool inError = false;
423 
424  const PtrList<ZoneType>& zones = *this;
425 
426  forAll(zones, zoneI)
427  {
428  inError |= zones[zoneI].checkDefinition(report);
429  }
430  return inError;
431 }
432 
433 
434 template<class ZoneType, class MeshType>
436 (
437  const bool report
438 ) const
439 {
440  if (!Pstream::parRun())
441  {
442  return false;
443  }
444 
445 
446  const PtrList<ZoneType>& zones = *this;
447 
448  bool hasError = false;
449 
450  // Collect all names
451  List<wordList> allNames(Pstream::nProcs());
452  allNames[Pstream::myProcNo()] = this->names();
453  Pstream::gatherList(allNames);
454  Pstream::scatterList(allNames);
455 
456  List<wordList> allTypes(Pstream::nProcs());
457  allTypes[Pstream::myProcNo()] = this->types();
458  Pstream::gatherList(allTypes);
459  Pstream::scatterList(allTypes);
460 
461  // Have every processor check but only master print error.
462 
463  for (label procI = 1; procI < allNames.size(); procI++)
464  {
465  if
466  (
467  (allNames[procI] != allNames[0])
468  || (allTypes[procI] != allTypes[0])
469  )
470  {
471  hasError = true;
472 
473  if (debug || (report && Pstream::master()))
474  {
475  Info<< " ***Inconsistent zones across processors, "
476  "processor 0 has zone names:" << allNames[0]
477  << " zone types:" << allTypes[0]
478  << " processor " << procI << " has zone names:"
479  << allNames[procI]
480  << " zone types:" << allTypes[procI]
481  << endl;
482  }
483  }
484  }
485 
486  // Check contents
487  if (!hasError)
488  {
489  forAll(zones, zoneI)
490  {
491  if (zones[zoneI].checkParallelSync(false))
492  {
493  hasError = true;
494 
495  if (debug || (report && Pstream::master()))
496  {
497  Info<< " ***Zone " << zones[zoneI].name()
498  << " of type " << zones[zoneI].type()
499  << " is not correctly synchronised"
500  << " across coupled boundaries."
501  << " (coupled faces are either not both"
502  << " present in set or have same flipmap)" << endl;
503  }
504  }
505  }
506  }
507 
508  return hasError;
509 }
510 
511 
512 // Correct zone mesh after moving points
513 template<class ZoneType, class MeshType>
515 {
516  PtrList<ZoneType>& zones = *this;
517 
518  forAll(zones, zoneI)
519  {
520  zones[zoneI].movePoints(p);
521  }
522 }
523 
524 
525 // writeData member function required by regIOobject
526 template<class ZoneType, class MeshType>
528 {
529  os << *this;
530  return os.good();
531 }
532 
533 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
534 
535 template<class ZoneType, class MeshType>
537 (
538  const word& zoneName
539 ) const
540 {
541  const label zoneI = findZoneID(zoneName);
542 
543  if (zoneI < 0)
544  {
546  << "Zone named " << zoneName << " not found." << nl
547  << "Available zone names: " << names() << endl
548  << abort(FatalError);
549  }
550 
551  return operator[](zoneI);
552 }
553 
554 
555 template<class ZoneType, class MeshType>
557 (
558  const word& zoneName
559 )
560 {
561  const label zoneI = findZoneID(zoneName);
562 
563  if (zoneI < 0)
564  {
566  << "Zone named " << zoneName << " not found." << nl
567  << "Available zone names: " << names() << endl
568  << abort(FatalError);
569  }
570 
571  return operator[](zoneI);
572 }
573 
574 
575 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
576 
577 template<class ZoneType, class MeshType>
578 Foam::Ostream& Foam::operator<<
579 (
580  Ostream& os,
581  const ZoneMesh<ZoneType, MeshType>& zones
582 )
583 {
584  os << zones.size() << nl << token::BEGIN_LIST;
585 
586  forAll(zones, zoneI)
587  {
588  zones[zoneI].writeDict(os);
589  }
590 
591  os << token::END_LIST;
592 
593  return os;
594 }
595 
596 
597 // ************************************************************************* //
Foam::ZoneMesh::zoneMap
const Map< label > & zoneMap() const
Map of zones containing zone index for all zoned elements.
Definition: ZoneMesh.C:211
Foam::ZoneMesh::checkDefinition
bool checkDefinition(const bool report=false) const
Check zone definition. Return true if in error.
Definition: ZoneMesh.C:418
Foam::ZoneMesh::clear
void clear()
Clear the zones.
Definition: ZoneMesh.C:408
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::PackedBoolList
A bit-packed bool list.
Definition: PackedBoolList.H:63
Foam::ZoneMesh::findIndex
label findIndex(const keyType &) const
Return zone index for the first match, return -1 if not found.
Definition: ZoneMesh.C:313
p
p
Definition: pEqn.H:62
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
ZoneMesh.H
Foam::compressible::New
autoPtr< BasicCompressibleTurbulenceModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const typename BasicCompressibleTurbulenceModel::transportModel &transport, const word &propertiesName)
Definition: turbulentFluidThermoModel.C:36
clear
UEqn clear()
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::keyType::isPattern
bool isPattern() const
Should be treated as a match rather than a literal string.
Definition: keyTypeI.H:76
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::Map< label >
Foam::ZoneMesh::read
bool read()
Read if IOobject flags set. Return true if read.
Definition: ZoneMesh.C:74
Foam::ZoneMesh::ZoneMesh
ZoneMesh(const ZoneMesh &)
Disallow construct as copy.
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
entry.H
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:40
Foam::ZoneMesh::~ZoneMesh
~ZoneMesh()
Destructor.
Definition: ZoneMesh.C:200
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:56
Foam::PtrList::set
bool set(const label) const
Is element set.
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::ZoneMesh::movePoints
void movePoints(const pointField &)
Correct zone mesh after moving points.
Definition: ZoneMesh.C:514
Foam::ZoneMesh
A list of mesh zones.
Definition: cellZoneMeshFwd.H:39
Foam::PtrList
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
Foam::ZoneMesh::whichZone
label whichZone(const label objectIndex) const
Given a global object index, return the zone it is in.
Definition: ZoneMesh.C:226
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Foam::FatalError
error FatalError
Foam::ZoneMesh::findMatching
PackedBoolList findMatching(const keyType &) const
Mark cells that match the zone specification.
Definition: ZoneMesh.C:377
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::ZoneMesh::calcZoneMap
void calcZoneMap() const
Create zone map.
Definition: ZoneMesh.C:35
Pstream.H
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::ZoneMesh::findZoneID
label findZoneID(const word &zoneName) const
Find zone index given a name.
Definition: ZoneMesh.C:348
Foam::ZoneMesh::names
wordList names() const
Return a list of zone names.
Definition: ZoneMesh.C:263
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
Foam::findStrings
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::ZoneMesh::findIndices
labelList findIndices(const keyType &) const
Return zone indices for all matches.
Definition: ZoneMesh.C:280
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::PtrList::size
label size() const
Return the number of elements in the PtrList.
Definition: PtrListI.H:32
Foam::PtrList::setSize
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:142
Foam::ZoneMesh::writeData
bool writeData(Ostream &) const
writeData member function required by regIOobject
Definition: ZoneMesh.C:527
stringListOps.H
Operations on lists of strings.
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::ZoneMesh::clearAddressing
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:394
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::findIndices
labelList findIndices(const ListType &, typename ListType::const_reference, const label start=0)
Find all occurences of given element. Linear search.
Foam::ZoneMesh::checkParallelSync
bool checkParallelSync(const bool report=false) const
Check whether all procs have all zones and in same order. Return.
Definition: ZoneMesh.C:436
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::ZoneMesh::types
wordList types() const
Return a list of zone types.
Definition: ZoneMesh.C:246