ZoneMesh.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::ZoneMesh
29 
30 Description
31  A list of mesh zones.
32 
33 SourceFiles
34  ZoneMesh.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_ZoneMesh_H
39 #define Foam_ZoneMesh_H
40 
41 #include "regIOobject.H"
42 #include "pointField.H"
43 #include "Map.H"
44 #include "HashSet.H"
45 #include "PtrList.H"
46 #include "bitSet.H"
47 #include "wordRes.H"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 template<class ZoneType, class MeshType> class ZoneMesh;
56 
57 template<class ZoneType, class MeshType>
58 Ostream& operator<<(Ostream& os, const ZoneMesh<ZoneType, MeshType>& zones);
59 
60 
61 /*---------------------------------------------------------------------------*\
62  Class ZoneMesh Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class ZoneType, class MeshType>
66 class ZoneMesh
67 :
68  public PtrList<ZoneType>,
69  public regIOobject
70 {
71  // Private Data
72 
73  //- Reference to mesh
74  const MeshType& mesh_;
75 
76  //- Demand-driven: map of zone labels for given element
77  mutable std::unique_ptr<Map<label>> zoneMapPtr_;
78 
79  //- Demand-driven: map of additional zone labels for given element.
80  // Only populated if overlapping zones
81  mutable std::unique_ptr<labelListList> additionalMapPtr_;
82 
83  //- Demand-driven: list of zone ids per group
84  mutable std::unique_ptr<HashTable<labelList>> groupIDsPtr_;
85 
86 
87  // Private Member Functions
88 
89  //- Total of number of addressed items (all zones)
90  label totalSize() const;
91 
92  //- Create zone map
93  void calcZoneMap() const;
94 
95  //- Some zones have inGroup entries
96  bool hasGroupIDs() const;
97 
98  //- Calculate group name to zone ids lookup
99  void calcGroupIDs() const;
100 
101  //- Clear addressing at this level
102  void clearLocalAddressing();
103 
104  //- Populate/recreate from dictionary entries
105  void populate(PtrList<entry>&& entries);
106 
107  //- Return true if contents were read
108  //- (controlled by IOobject readOption flags).
109  bool readIOcontents(const bool allowOptionalRead);
110 
111 
112 public:
113 
114  // Public Typedefs
115 
116  //- The referenced mesh type
117  typedef MeshType mesh_type;
118 
119  //- The zone type. Same as PtrList<ZoneType>::value_type
120  typedef ZoneType zone_type;
121 
122 
123  //- Debug switch to disallow the use of generic zones
124  static int disallowGenericZones;
125 
126 
127  // Generated Methods
128 
129  //- No copy construct
130  ZoneMesh(const ZoneMesh&) = delete;
131 
132  //- No copy assignment
133  void operator=(const ZoneMesh<ZoneType, MeshType>&) = delete;
134 
135 
136  // Constructors
138  //- Read construct from IOobject and mesh reference
139  //- Any reading (mandatory, optional) based on IOobject properties.
140  ZoneMesh
141  (
142  const IOobject& io,
143  const MeshType& mesh
144  );
145 
146  //- Construct empty with IOobject properties and a mesh reference.
147  //- Does not read.
149  (
150  const IOobject& io,
151  const MeshType& mesh,
152  Foam::zero
153  );
154 
155  //- Construct with specified size if not read.
156  //- Any reading (mandatory, optional) based on IOobject properties.
157  ZoneMesh
158  (
159  const IOobject& io,
160  const MeshType& mesh,
161  const label size
162  );
163 
164  //- Read construct (mandatory, optional) based on IOobject properties
165  //- or use the fallback PtrList (with cloning).
166  ZoneMesh
167  (
168  const IOobject& io,
169  const MeshType& mesh,
170  const PtrList<ZoneType>& list
171  );
172 
173  //- Read construct (mandatory, optional) based on IOobject properties
174  //- or use the fallback PtrList (with cloning).
175  ZoneMesh
176  (
177  const IOobject& io,
178  const MeshType& mesh,
179  PtrList<entry>&& entries
180  );
181 
182 
183  //- Destructor
184  ~ZoneMesh() = default;
185 
186 
187  // Member Functions
188 
189  //- Return the mesh reference
190  const MeshType& mesh() const noexcept { return mesh_; }
191 
192  //- Map of zones containing zone index for all zoned elements
193  // Return -1 if the object is not in the zone
194  const Map<label>& zoneMap() const;
195 
196  //- Given a global object index, return the zone it is in.
197  // If object does not belong to any zones, return -1
198  label whichZone(const label objectIndex) const;
199 
200  //- Given a global object index, return (in argument) its zones.
201  // Returns number of zones (0 if object does not belong to any zones)
202  label whichZones
203  (
204  const label objectIndex,
205  DynamicList<label>& zones
206  ) const;
207 
208  //- Return a list of zone sizes
209  labelList zoneSizes() const;
210 
211  //- Return a list of zone types
212  wordList types() const;
213 
214  //- A list of the zone names
215  wordList names() const;
216 
217  //- A list of the zone group names (if any)
218  wordList groupNames() const;
219 
220  //- A list of zone names satisfying the input matcher
221  wordList names(const wordRe& matcher) const;
222 
223  //- A list of zone names satisfying the input matchers
224  wordList names(const wordRes& matcher) const;
225 
226  //- Sorted list of the zone names
227  wordList sortedNames() const;
228 
229  //- Sorted list of zone names satisfying the input matcher
230  wordList sortedNames(const wordRe& matcher) const;
231 
232  //- Sorted list of zone names satisfying the input matchers
233  wordList sortedNames(const wordRes& matcher) const;
234 
235  //- Adjust the index of zone entries to be consistent with their
236  //- position in the list.
237  // If the indexing changed, invalidates any mapping.
238  // \returns true if the indexing changed.
239  bool reindex();
240 
241  //- The (sorted) patch indices for all matches,
242  //- optionally matching zone groups.
243  // \returns an empty list for an empty matcher
245  (
246  const wordRe& matcher,
247  const bool useGroups = true
248  ) const;
249 
250  //- The (sorted) patch indices for all matches,
251  //- optionally matching zone groups.
252  // \returns an empty list for an empty matcher
254  (
255  const wordRes& matcher,
256  const bool useGroups = true
257  ) const;
258 
259  //- The (sorted) patch indices: logic as per Foam::wordRes::filter,
260  //- optionally matching zone groups.
261  //
262  // An empty \em allow accepts everything not in \em deny.
263  // A literal \em allow match has higher priority than any \em deny.
264  // A regex \em allow match has lower priority than any \em deny.
265  //
266  // \returns identity list when allow/deny are both empty.
268  (
269  const wordRes& allow,
270  const wordRes& deny,
271  const bool useGroups = true
272  ) const;
273 
274  //- Zone index for the first match, return -1 if not found
275  // A no-op (returns -1) for an empty key
276  label findIndex(const wordRe& key) const;
277 
278  //- Zone index for the first match, return -1 if not found
279  // A no-op (returns -1) for an empty matcher
280  label findIndex(const wordRes& matcher) const;
281 
282  //- Find zone index by name, return -1 if not found
283  // A no-op (returns -1) for an empty zoneName
284  label findZoneID(const word& zoneName) const;
285 
286  //- Find zone by name and return const pointer, nullptr on error
287  // A no-op (returns nullptr) for an empty zoneName
288  const ZoneType* cfindZone(const word& zoneName) const;
289 
290  //- Find zone by name and return pointer, nullptr on error
291  // A no-op (returns nullptr) for an empty zoneName
292  ZoneType* findZone(const word& zoneName);
293 
294 
295  //- Return all elements (cells, faces, points) contained in the
296  //- listed zones.
297  // The bitSet is empty (zero-size) if there are no elements matched
298  // anywhere.
299  bitSet selection(const labelUList& zoneIds) const;
300 
301  //- Return all elements (cells, faces, points) that match the zone
302  //- specification as a bitSet.
303  // The bitSet is empty (zero-size) if there are no elements matched
304  // anywhere.
305  // Optionally matches zoneGroups.
306  // A no-op (returns empty bitSet) for an empty matcher
308  (
309  const wordRe& matcher,
310  const bool useGroups = true
311  ) const;
312 
313  //- Return all elements (cells, faces, points) that match the zone
314  //- specification as a bitSet.
315  // The bitSet is empty (zero-size) if there are no elements matched
316  // anywhere.
317  // A no-op (returns empty bitSet) for an empty matcher
319  (
320  const wordRes& matcher,
321  const bool useGroups = true
322  ) const;
323 
324  //- The zone indices per zone group
325  const HashTable<labelList>& groupZoneIDs() const;
326 
327  //- Set/add group with zones
328  void setGroup(const word& groupName, const labelUList& zoneIDs);
329 
330  //- Check zone definition. Return true if in error.
331  bool checkDefinition(const bool report = false) const;
332 
333  //- Check whether all procs have all zones and in same order.
334  // \return True if any errors.
335  bool checkParallelSync(const bool report = false) const;
336 
337  //- Correct zone mesh after moving points
338  void movePoints(const pointField& pts);
339 
340 
341  // Storage Management
342 
343  //- Clear addressing
344  void clearAddressing();
345 
346  //- Clear primitive addressing
347  void clearPrimitives();
348 
349  //- Clear the zones
350  void clear();
351 
352  //- The zoneMap has been allocated
353  bool hasZoneMap() const noexcept { return bool(zoneMapPtr_); }
354 
355 
356  // Member Operators
357 
358  //- Return const and non-const reference to zone by index.
360 
361  //- Return const reference to zone by name.
362  // Fatal if the zone does not exist.
363  const ZoneType& operator[](const word& zoneName) const;
364 
365  //- Return reference to an existing zone by name
366  // Fatal if the zone does not exist.
367  ZoneType& operator[](const word& zoneName);
368 
369  //- Find an existing zone by name or create a new empty one
370  //- if required.
371  //
372  // To determine if the zone already existed or was newly created,
373  // it will be necessary to add additional logic in the caller.
374  // For example,
375  // \code
376  // const label nOrig = zones.size();
377  //
378  // ZoneType& zn = zones("zoneName");
379  //
380  // if (nOrig == zones.size()) { existing... } else { new... }
381  // \endcode
382  // \param zoneName the name of the zone
383  // \param verbose report if an existing zone was selected or
384  // a new zone was created.
385  // \return non-const reference to the existing or new zone
386  ZoneType& operator()(const word& zoneName, const bool verbose=false);
387 
388 
389  // IO
390 
391  //- Update internal meta-data (eg, prior to writing)
392  void updateMetaData();
393 
394  //- Write as a plain list of entries
395  void writeEntry(Ostream& os) const;
396 
397  //- Write as a primitive entry with given name.
398  //- If the keyword is empty, revert to a plain list.
399  void writeEntry(const word& keyword, Ostream& os) const;
400 
401  //- The writeData member function required by regIOobject
402  virtual bool writeData(Ostream& os) const;
403 
404 
405  // Ostream Operator
406 
407  friend Ostream& operator<< <ZoneType, MeshType>
408  (
409  Ostream& os,
410  const ZoneMesh<ZoneType, MeshType>& zones
411  );
412 
413 
414  // Housekeeping
415 
416  //- Identical to the indices() method (AUG-2018)
417  FOAM_DEPRECATED_FOR(2018-08, "indices() method")
418  labelList findIndices(const wordRes& key) const
419  {
420  return indices(key);
421  }
422 };
423 
424 
425 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
426 
427 } // End namespace Foam
428 
429 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
430 
431 #ifdef NoRepository
432  #include "ZoneMesh.C"
433 #endif
434 
435 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
436 
437 #endif
438 
439 // ************************************************************************* //
bool checkParallelSync(const bool report=false) const
Check whether all procs have all zones and in same order.
Definition: ZoneMesh.C:998
const labelIOList & zoneIDs
Definition: correctPhi.H:59
wordList types() const
Return a list of zone types.
Definition: ZoneMesh.C:456
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:944
ZoneType * findZone(const word &zoneName)
Find zone by name and return pointer, nullptr on error.
Definition: ZoneMesh.C:824
void clear()
Clear the zones.
Definition: ZoneMesh.C:970
label findIndex(const wordRe &key) const
Zone index for the first match, return -1 if not found.
Definition: ZoneMesh.C:729
const Map< label > & zoneMap() const
Map of zones containing zone index for all zoned elements.
Definition: ZoneMesh.C:397
FOAM_DEPRECATED_FOR(2022-09, "get(), set() or test() methods") const T *operator()(const label i) const
Deprecated(2022-09) - same as get()
Definition: UPtrList.H:485
labelList zoneSizes() const
Return a list of zone sizes.
Definition: ZoneMesh.C:448
A list of mesh zones.
const MeshType & mesh() const noexcept
Return the mesh reference.
Definition: ZoneMesh.H:232
void movePoints(const pointField &pts)
Correct zone mesh after moving points.
Definition: ZoneMesh.C:1078
static word groupName(StringType base, const word &group)
Create dot-delimited name.group string.
labelList findIndices(const wordRes &key) const
Identical to the indices() method (AUG-2018)
Definition: ZoneMesh.H:563
labelList indices(const wordRe &matcher, const bool useGroups=true) const
The (sorted) patch indices for all matches, optionally matching zone groups.
Definition: ZoneMesh.C:562
label findZoneID(const word &zoneName) const
Find zone index by name, return -1 if not found.
Definition: ZoneMesh.C:757
A class for handling words, derived from Foam::string.
Definition: word.H:63
void setGroup(const word &groupName, const labelUList &zoneIDs)
Set/add group with zones.
Definition: ZoneMesh.C:901
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
const auto & io
~ZoneMesh()=default
Destructor.
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
void clearPrimitives()
Clear primitive addressing.
Definition: ZoneMesh.C:958
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:265
MeshType mesh_type
The referenced mesh type.
Definition: ZoneMesh.H:137
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
Definition: foamGltfBase.H:105
const ZoneType & operator[](const word &zoneName) const
Return const reference to zone by name.
Definition: ZoneMesh.C:1177
label whichZone(const label objectIndex) const
Given a global object index, return the zone it is in.
Definition: ZoneMesh.C:410
ZoneMesh(const ZoneMesh &)=delete
No copy construct.
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:58
wordList groupNames() const
A list of the zone group names (if any)
Definition: ZoneMesh.C:470
bool checkDefinition(const bool report=false) const
Check zone definition. Return true if in error.
Definition: ZoneMesh.C:979
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:463
bitSet selection(const labelUList &zoneIds) const
Return all elements (cells, faces, points) contained in the listed zones.
Definition: ZoneMesh.C:834
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
ZoneType zone_type
The zone type. Same as PtrList<ZoneType>::value_type.
Definition: ZoneMesh.H:142
bool hasZoneMap() const noexcept
The zoneMap has been allocated.
Definition: ZoneMesh.H:477
wordList sortedNames() const
Sorted list of the zone names.
Definition: ZoneMesh.C:498
ZoneType & operator()(const word &zoneName, const bool verbose=false)
Find an existing zone by name or create a new empty one if required.
Definition: ZoneMesh.C:1217
label whichZones(const label objectIndex, DynamicList< label > &zones) const
Given a global object index, return (in argument) its zones.
Definition: ZoneMesh.C:420
void operator=(const ZoneMesh< ZoneType, MeshType > &)=delete
No copy assignment.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:188
static int disallowGenericZones
Debug switch to disallow the use of generic zones.
Definition: ZoneMesh.H:148
bool reindex()
Adjust the index of zone entries to be consistent with their position in the list.
Definition: ZoneMesh.C:535
const ZoneType * cfindZone(const word &zoneName) const
Find zone by name and return const pointer, nullptr on error.
Definition: ZoneMesh.C:789
virtual bool writeData(Ostream &os) const
The writeData member function required by regIOobject.
Definition: ZoneMesh.C:1166
Namespace for OpenFOAM.
const HashTable< labelList > & groupZoneIDs() const
The zone indices per zone group.
Definition: ZoneMesh.C:888
void updateMetaData()
Update internal meta-data (eg, prior to writing)
Definition: ZoneMesh.C:1090
const pointField & pts
void writeEntry(Ostream &os) const
Write as a plain list of entries.
Definition: ZoneMesh.C:1115