ZoneList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 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 "ZoneList.H"
27 #include "Pstream.H"
28 #include "demandDrivenData.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class ZoneType, class ZonesType, class MeshType>
34 {
35  if
36  (
37  readOpt() == IOobject::MUST_READ
38  || readOpt() == IOobject::MUST_READ_IF_MODIFIED
39  || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
40  )
41  {
42  if (readOpt() == IOobject::MUST_READ_IF_MODIFIED)
43  {
45  << "Specified IOobject::MUST_READ_IF_MODIFIED but class"
46  << " does not support automatic rereading."
47  << endl;
48  }
49 
50  PtrListDictionary<ZoneType>& zones = *this;
51 
52  // Read zones
53  Istream& is = readStream(ZonesType::typeName);
54 
55  PtrList<entry> patchEntries(is);
56  zones.setSize(patchEntries.size());
57 
58  forAll(zones, zi)
59  {
60  zones.set
61  (
62  zi,
63  patchEntries[zi].keyword(),
65  (
66  patchEntries[zi].keyword(),
67  patchEntries[zi].dict(),
68  static_cast<const ZonesType&>(*this)
69  )
70  );
71  }
72 
73  // Check state of IOstream
74  is.check
75  (
76  "ZoneList::ZoneList"
77  "(const IOobject&, const MeshType&)"
78  );
79 
80  close();
81 
82  return true;
83  }
84  else
85  {
86  // Nothing read
87  return false;
88  }
89 }
90 
91 
92 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
93 
94 template<class ZoneType, class ZonesType, class MeshType>
96 (
97  const IOobject& io,
98  const MeshType& mesh
99 )
100 :
101  regIOobject(io),
102  PtrListDictionary<ZoneType>(0),
103  mesh_(mesh)
104 {
105  read();
106 }
107 
108 
109 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
110 
111 template<class ZoneType, class ZonesType, class MeshType>
113 {
114  clearAddressing();
115 }
116 
117 
118 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
119 
120 template<class ZoneType, class ZonesType, class MeshType>
122 (
123  const label objectIndex
124 ) const
125 {
126  forAll(*this, zi)
127  {
128  if (this->operator[](zi).localIndex(objectIndex) != -1)
129  {
130  return true;
131  }
132  }
133 
134  return false;
135 }
136 
137 
138 template<class ZoneType, class ZonesType, class MeshType>
140 (
141  const label objectIndex
142 ) const
143 {
144  labelList zones;
145 
146  forAll(*this, zi)
147  {
148  if (this->operator[](zi).localIndex(objectIndex) != -1)
149  {
150  zones.append(zi);
151  }
152  }
153 
154  return zones;
155 }
156 
157 
158 template<class ZoneType, class ZonesType, class MeshType>
160 {
161  const PtrListDictionary<ZoneType>& zones = *this;
162 
163  wordList lst(zones.size());
164 
165  forAll(zones, zi)
166  {
167  lst[zi] = zones[zi].type();
168  }
169 
170  return lst;
171 }
172 
173 
174 template<class ZoneType, class ZonesType, class MeshType>
176 (
177  ZoneType* zonePtr
178 ) const
179 {
181  const_cast<ZoneList<ZoneType, ZonesType, MeshType>&>(*this);
182 
183  if (found(zonePtr->name()))
184  {
185  zones[zonePtr->name()] = *zonePtr;
186  delete zonePtr;
187  }
188  else
189  {
190  zones.PtrListDictionary<ZoneType>::append
191  (
192  zonePtr->name(),
193  zonePtr
194  );
195  }
196 }
197 
198 
199 template<class ZoneType, class ZonesType, class MeshType>
201 (
202  const ZoneType& zone
203 ) const
204 {
206  const_cast<ZoneList<ZoneType, ZonesType, MeshType>&>(*this);
207 
208  if (found(zone.name()))
209  {
210  zones[zone.name()] = zone;
211  }
212  else
213  {
214  zones.PtrListDictionary<ZoneType>::append
215  (
216  zone.name(),
217  zone.clone(*this)
218  );
219  }
220 }
221 
222 
223 template<class ZoneType, class ZonesType, class MeshType>
225 {
226  PtrListDictionary<ZoneType>& zones = *this;
227 
228  forAll(zones, zi)
229  {
230  zones[zi].clearAddressing();
231  }
232 }
233 
234 
235 template<class ZoneType, class ZonesType, class MeshType>
237 {
238  clearAddressing();
240 }
241 
242 
243 template<class ZoneType, class ZonesType, class MeshType>
245 (
246  const bool report
247 ) const
248 {
249  bool inError = false;
250 
251  const PtrListDictionary<ZoneType>& zones = *this;
252 
253  forAll(zones, zi)
254  {
255  inError |= zones[zi].checkDefinition(report);
256  }
257  return inError;
258 }
259 
260 
261 template<class ZoneType, class ZonesType, class MeshType>
263 (
264  const bool report
265 ) const
266 {
267  if (!Pstream::parRun())
268  {
269  return false;
270  }
271 
272 
273  const PtrListDictionary<ZoneType>& zones = *this;
274 
275  bool hasError = false;
276 
277  // Collect all names
278  List<wordList> allNames(Pstream::nProcs());
279  allNames[Pstream::myProcNo()] = this->toc();
280  Pstream::gatherList(allNames);
281  Pstream::scatterList(allNames);
282 
283  List<wordList> allTypes(Pstream::nProcs());
284  allTypes[Pstream::myProcNo()] = this->types();
285  Pstream::gatherList(allTypes);
286  Pstream::scatterList(allTypes);
287 
288  // Have every processor check but only master print error.
289 
290  for (label proci = 1; proci < allNames.size(); proci++)
291  {
292  if
293  (
294  (allNames[proci] != allNames[0])
295  || (allTypes[proci] != allTypes[0])
296  )
297  {
298  hasError = true;
299 
300  if (debug || (report && Pstream::master()))
301  {
302  Info<< " ***Inconsistent zones across processors, "
303  "processor 0 has zone names:" << allNames[0]
304  << " zone types:" << allTypes[0]
305  << " processor " << proci << " has zone names:"
306  << allNames[proci]
307  << " zone types:" << allTypes[proci]
308  << endl;
309  }
310  }
311  }
312 
313  // Check contents
314  if (!hasError)
315  {
316  forAll(zones, zi)
317  {
318  if (zones[zi].checkParallelSync(false))
319  {
320  hasError = true;
321 
322  if (debug || (report && Pstream::master()))
323  {
324  Info<< " ***Zone " << zones[zi].name()
325  << " of type " << zones[zi].type()
326  << " is not correctly synchronised"
327  << " across coupled boundaries."
328  << " (coupled faces are either not both"
329  << " present in set or have same flipmap)" << endl;
330  }
331  }
332  }
333  }
334 
335  return hasError;
336 }
337 
338 
339 template<class ZoneType, class ZonesType, class MeshType>
341 (
342  const List<labelHashSet>& zonesIndices
343 )
344 {
345  PtrListDictionary<ZoneType>& zones = *this;
346 
347  if (zonesIndices.size() != zones.size())
348  {
350  << "zonesIndices.size() " << zonesIndices.size()
351  << " != number of zones " << zones.size()
352  << exit(FatalError);
353  }
354 
355  forAll(zonesIndices, zonei)
356  {
357  zones[zonei].insert(zonesIndices[zonei]);
358  }
359 }
360 
361 
362 template<class ZoneType, class ZonesType, class MeshType>
364 (
365  const pointField& p
366 )
367 {
368  PtrListDictionary<ZoneType>& zones = *this;
369 
370  forAll(zones, zi)
371  {
372  zones[zi].movePoints(p);
373  }
374 }
375 
376 
377 template<class ZoneType, class ZonesType, class MeshType>
379 (
380  const polyTopoChangeMap& map
381 )
382 {
383  PtrListDictionary<ZoneType>& zones = *this;
384 
385  forAll(zones, zi)
386  {
387  zones[zi].topoChange(map);
388  }
389 }
390 
391 
392 template<class ZoneType, class ZonesType, class MeshType>
394 (
395  const polyMeshMap& map
396 )
397 {
398  PtrListDictionary<ZoneType>& zones = *this;
399 
400  forAll(zones, zi)
401  {
402  zones[zi].mapMesh(map);
403  }
404 }
405 
406 
407 template<class ZoneType, class ZonesType, class MeshType>
409 (
410  const polyDistributionMap& map
411 )
412 {
413  PtrListDictionary<ZoneType>& zones = *this;
414 
415  forAll(zones, zi)
416  {
417  zones[zi].distribute(map);
418  }
419 }
420 
421 
422 template<class ZoneType, class ZonesType, class MeshType>
424 {
425  clearAddressing();
426  otherZones.clearAddressing();
427 
428  PtrListDictionary<ZoneType>& zones = *this;
429 
430  DynamicList<label> toOtherZone;
431 
432  forAll(zones, zi)
433  {
434  const label ozi = otherZones.findIndex(zones[zi].name());
435 
436  if (ozi < 0)
437  {
438  toOtherZone.append(zi);
439  }
440  }
441 
442  forAll(otherZones, ozi)
443  {
444  const label zi = this->findIndex(otherZones[ozi].name());
445 
446  if (zi < 0)
447  {
448  zones.append
449  (
450  otherZones[ozi].name(),
451  otherZones[ozi].clone
452  (
453  static_cast<const ZonesType&>(*this)
454  )
455  );
456  otherZones.set(ozi, otherZones[ozi].name(), nullptr);
457  }
458  else
459  {
460  zones[zi].swap(otherZones[ozi]);
461  }
462  }
463 
464  forAll(toOtherZone, i)
465  {
466  otherZones.PtrListDictionary<ZoneType>::append
467  (
468  zones[toOtherZone[i]].name(),
469  zones[toOtherZone[i]].clone(otherZones)
470  );
471  zones.set(toOtherZone[i], zones[toOtherZone[i]].name(), nullptr);
472  }
473 
474  zones.shrink();
475  otherZones.shrink();
476 }
477 
478 
479 template<class ZoneType, class ZonesType, class MeshType>
481 {
482  readOpt() = IOobject::READ_IF_PRESENT;
483  return read();
484 }
485 
486 
487 template<class ZoneType, class ZonesType, class MeshType>
489 {
490  os << *this;
491  return os.good();
492 }
493 
494 
495 template<class ZoneType, class ZonesType, class MeshType>
497 (
501  const bool write
502 ) const
503 {
504  if (this->size())
505  {
506  return regIOobject::writeObject(fmt, ver, cmp, write);
507  }
508  else
509  {
510  return true;
511  }
512 }
513 
514 
515 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
516 
517 template<class ZoneType, class ZonesType, class MeshType>
518 Foam::Ostream& Foam::operator<<
519 (
520  Ostream& os,
522 )
523 {
524  os << zones.size() << nl << token::BEGIN_LIST;
525 
526  forAll(zones, zi)
527  {
528  zones[zi].writeDict(os);
529  }
530 
531  os << token::END_LIST;
532 
533  return os;
534 }
535 
536 
537 // ************************************************************************* //
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void insert(const word &, T *)
Add at head of dictionary.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
Version number type.
Definition: IOstream.H:97
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Template dictionary class which manages the storage associated with it.
void append(const word &key, T *)
Append an element at the end of the list.
autoPtr< T > set(const label, const word &key, T *)
Set element to pointer provided and return old element.
A list of mesh zones.
Definition: ZoneList.H:71
void append(ZoneType *) const
Append or update a zone.
Definition: ZoneList.C:176
bool checkDefinition(const bool report=false) const
Check zone definition. Return true if in error.
Definition: ZoneList.C:245
~ZoneList()
Destructor.
Definition: ZoneList.C:112
void insert(const List< labelHashSet > &zonesIndices)
Insert given indices into zones.
Definition: ZoneList.C:341
virtual bool writeData(Ostream &) const
writeData member function required by regIOobject
Definition: ZoneList.C:488
bool found(const label objectIndex) const
Return true if objectIndex is in any zone.
Definition: ZoneList.C:122
ZoneList(const IOobject &, const MeshType &)
Read constructor given IOobject and a MeshType reference.
Definition: ZoneList.C:96
wordList types() const
Return a list of zone types.
Definition: ZoneList.C:159
bool readIfPresent()
Read zones if the zones file is present.
Definition: ZoneList.C:480
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: ZoneList.C:497
labelList whichZones(const label objectIndex) const
Given a global object index, return the list of zones it is in.
Definition: ZoneList.C:140
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: ZoneList.C:409
void swap(ZonesType &)
Swap zones.
Definition: ZoneList.C:423
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: ZoneList.C:394
void clearAddressing()
Clear addressing.
Definition: ZoneList.C:224
virtual void movePoints(const pointField &)
Correct zones after moving points.
Definition: ZoneList.C:364
void clear()
Clear the zones.
Definition: ZoneList.C:236
bool checkParallelSync(const bool report=false) const
Check whether all procs have all zones and in same order. Return.
Definition: ZoneList.C:263
virtual void topoChange(const polyTopoChangeMap &map)
Update topology using the given map.
Definition: ZoneList.C:379
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
Template functions to aid in the implementation of demand driven data.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
tUEqn clear()
#define WarningInFunction
Report a warning using Foam::Warning.
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
autoPtr< CompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const viscosity &viscosity)
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
T clone(const T &t)
Definition: List.H:55
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
error FatalError
static const char nl
Definition: Ostream.H:266
dictionary dict
volScalarField & p