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 zone type. Same as PtrList<ZoneType>::value_type
117  typedef ZoneType zone_type;
118 
119 
120  //- Debug switch to disallow the use of generic zones
121  static int disallowGenericZones;
122 
123 
124  // Generated Methods
125 
126  //- No copy construct
127  ZoneMesh(const ZoneMesh&) = delete;
128 
129  //- No copy assignment
130  void operator=(const ZoneMesh<ZoneType, MeshType>&) = delete;
131 
132 
133  // Constructors
134 
135  //- Read construct from IOobject and mesh reference
136  //- Any reading (mandatory, optional) based on IOobject properties.
138  (
139  const IOobject& io,
140  const MeshType& mesh
141  );
142 
143  //- Construct empty with IOobject properties and a mesh reference.
144  //- Does not read.
145  ZoneMesh
146  (
147  const IOobject& io,
148  const MeshType& mesh,
149  Foam::zero
150  );
151 
152  //- Construct with specified size if not read.
153  //- Any reading (mandatory, optional) based on IOobject properties.
154  ZoneMesh
155  (
156  const IOobject& io,
157  const MeshType& mesh,
158  const label size
159  );
160 
161  //- Read construct (mandatory, optional) based on IOobject properties
162  //- or use the fallback PtrList (with cloning).
163  ZoneMesh
164  (
165  const IOobject& io,
166  const MeshType& mesh,
167  const PtrList<ZoneType>& list
168  );
169 
170  //- Read construct (mandatory, optional) based on IOobject properties
171  //- or use the fallback PtrList (with cloning).
172  ZoneMesh
173  (
174  const IOobject& io,
175  const MeshType& mesh,
176  PtrList<entry>&& entries
177  );
178 
179 
180  //- Destructor
181  ~ZoneMesh() = default;
182 
183 
184  // Member Functions
185 
186  //- Return the mesh reference
187  const MeshType& mesh() const noexcept { return mesh_; }
188 
189  //- Map of zones containing zone index for all zoned elements
190  // Return -1 if the object is not in the zone
191  const Map<label>& zoneMap() const;
192 
193  //- Given a global object index, return the zone it is in.
194  // If object does not belong to any zones, return -1
195  label whichZone(const label objectIndex) const;
196 
197  //- Given a global object index, return (in argument) its zones.
198  // Returns number of zones (0 if object does not belong to any zones)
199  label whichZones
200  (
201  const label objectIndex,
202  DynamicList<label>& zones
203  ) const;
204 
205  //- Return a list of zone types
206  wordList types() const;
207 
208  //- A list of the zone names
209  wordList names() const;
210 
211  //- A list of the zone group names (if any)
212  wordList groupNames() const;
213 
214  //- A list of zone names satisfying the input matcher
215  wordList names(const wordRe& matcher) const;
216 
217  //- A list of zone names satisfying the input matchers
218  wordList names(const wordRes& matcher) const;
219 
220  //- Sorted list of the zone names
221  wordList sortedNames() const;
222 
223  //- Sorted list of zone names satisfying the input matcher
224  wordList sortedNames(const wordRe& matcher) const;
225 
226  //- Sorted list of zone names satisfying the input matchers
227  wordList sortedNames(const wordRes& matcher) const;
228 
229 
230  //- The (sorted) patch indices for all matches,
231  //- optionally matching zone groups.
232  // \returns an empty list for an empty matcher
234  (
235  const wordRe& matcher,
236  const bool useGroups = true
237  ) const;
238 
239  //- The (sorted) patch indices for all matches,
240  //- optionally matching zone groups.
241  // \returns an empty list for an empty matcher
243  (
244  const wordRes& matcher,
245  const bool useGroups = true
246  ) const;
247 
248  //- The (sorted) patch indices: logic as per Foam::wordRes::filter,
249  //- optionally matching zone groups.
250  //
251  // An empty \em allow accepts everything not in \em deny.
252  // A literal \em allow match has higher priority than any \em deny.
253  // A regex \em allow match has lower priority than any \em deny.
254  //
255  // \returns identity list when allow/deny are both empty.
257  (
258  const wordRes& allow,
259  const wordRes& deny,
260  const bool useGroups = true
261  ) const;
262 
263  //- Zone index for the first match, return -1 if not found
264  // A no-op (returns -1) for an empty key
265  label findIndex(const wordRe& key) const;
266 
267  //- Zone index for the first match, return -1 if not found
268  // A no-op (returns -1) for an empty matcher
269  label findIndex(const wordRes& matcher) const;
270 
271  //- Find zone index by name, return -1 if not found
272  // A no-op (returns -1) for an empty zoneName
273  label findZoneID(const word& zoneName) const;
274 
275  //- Find zone by name and return const pointer, nullptr on error
276  // A no-op (returns nullptr) for an empty zoneName
277  const ZoneType* cfindZone(const word& zoneName) const;
278 
279  //- Find zone by name and return pointer, nullptr on error
280  // A no-op (returns nullptr) for an empty zoneName
281  ZoneType* findZone(const word& zoneName);
282 
283 
284  //- Return all elements (cells, faces, points) contained in the
285  //- listed zones.
286  // The bitSet is empty (zero-size) if there are no elements matched
287  // anywhere.
288  bitSet selection(const labelUList& zoneIds) const;
289 
290  //- Return all elements (cells, faces, points) that match the zone
291  //- specification as a bitSet.
292  // The bitSet is empty (zero-size) if there are no elements matched
293  // anywhere.
294  // Optionally matches zoneGroups.
295  // A no-op (returns empty bitSet) for an empty matcher
297  (
298  const wordRe& matcher,
299  const bool useGroups = true
300  ) const;
301 
302  //- Return all elements (cells, faces, points) that match the zone
303  //- specification as a bitSet.
304  // The bitSet is empty (zero-size) if there are no elements matched
305  // anywhere.
306  // A no-op (returns empty bitSet) for an empty matcher
308  (
309  const wordRes& matcher,
310  const bool useGroups = true
311  ) const;
312 
313  //- The zone indices per zone group
314  const HashTable<labelList>& groupZoneIDs() const;
315 
316  //- Set/add group with zones
317  void setGroup(const word& groupName, const labelUList& zoneIDs);
318 
319  //- Check zone definition. Return true if in error.
320  bool checkDefinition(const bool report = false) const;
321 
322  //- Check whether all procs have all zones and in same order.
323  // \return True if any errors.
324  bool checkParallelSync(const bool report = false) const;
325 
326  //- Correct zone mesh after moving points
327  void movePoints(const pointField& pts);
328 
329 
330  // Storage Management
331 
332  //- Clear addressing
333  void clearAddressing();
334 
335  //- Clear primitive addressing
336  void clearPrimitives();
337 
338  //- Clear the zones
339  void clear();
340 
341  //- The zoneMap has been allocated
342  bool hasZoneMap() const noexcept { return bool(zoneMapPtr_); }
343 
344 
345  // Member Operators
346 
347  //- Return const and non-const reference to zone by index.
349 
350  //- Return const reference to zone by name.
351  // Fatal if the zone does not exist.
352  const ZoneType& operator[](const word& zoneName) const;
353 
354  //- Return reference to an existing zone by name
355  // Fatal if the zone does not exist.
356  ZoneType& operator[](const word& zoneName);
357 
358  //- Find an existing zone by name or create a new empty one
359  //- if required.
360  //
361  // To determine if the zone already existed or was newly created,
362  // it will be necessary to add additional logic in the caller.
363  // For example,
364  // \code
365  // const label nOrig = zones.size();
366  //
367  // ZoneType& zn = zones("zoneName");
368  //
369  // if (nOrig == zones.size()) { existing... } else { new... }
370  // \endcode
371  // \param zoneName the name of the zone
372  // \param verbose report if an existing zone was selected or
373  // a new zone was created.
374  // \return non-const reference to the existing or new zone
375  ZoneType& operator()(const word& zoneName, const bool verbose=false);
376 
377 
378  // IO
379 
380  //- Update internal meta-data (eg, prior to writing)
381  void updateMetaData();
382 
383  //- The writeData member function required by regIOobject
384  bool writeData(Ostream& os) const;
385 
386 
387  // Ostream Operator
388 
389  friend Ostream& operator<< <ZoneType, MeshType>
390  (
391  Ostream& os,
392  const ZoneMesh<ZoneType, MeshType>& zones
393  );
394 
395 
396  // Housekeeping
397 
398  //- Identical to the indices() method (AUG-2018)
399  FOAM_DEPRECATED_FOR(2018-08, "indices() method")
400  labelList findIndices(const wordRes& key) const
401  {
402  return indices(key);
403  }
404 };
405 
406 
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408 
409 } // End namespace Foam
410 
411 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
412 
413 #ifdef NoRepository
414  #include "ZoneMesh.C"
415 #endif
416 
417 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
418 
419 #endif
420 
421 // ************************************************************************* //
bool checkParallelSync(const bool report=false) const
Check whether all procs have all zones and in same order.
Definition: ZoneMesh.C:964
const labelIOList & zoneIDs
Definition: correctPhi.H:59
wordList types() const
Return a list of zone types.
Definition: ZoneMesh.C:448
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:910
ZoneType * findZone(const word &zoneName)
Find zone by name and return pointer, nullptr on error.
Definition: ZoneMesh.C:790
void clear()
Clear the zones.
Definition: ZoneMesh.C:936
label findIndex(const wordRe &key) const
Zone index for the first match, return -1 if not found.
Definition: ZoneMesh.C:695
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:492
A list of mesh zones.
const MeshType & mesh() const noexcept
Return the mesh reference.
Definition: ZoneMesh.H:227
void movePoints(const pointField &pts)
Correct zone mesh after moving points.
Definition: ZoneMesh.C:1044
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:534
labelList indices(const wordRe &matcher, const bool useGroups=true) const
The (sorted) patch indices for all matches, optionally matching zone groups.
Definition: ZoneMesh.C:528
label findZoneID(const word &zoneName) const
Find zone index by name, return -1 if not found.
Definition: ZoneMesh.C:723
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:867
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
~ZoneMesh()=default
Destructor.
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
void clearPrimitives()
Clear primitive addressing.
Definition: ZoneMesh.C:924
auto key(const Type &t) -> std::enable_if_t< std::is_enum_v< Type >, std::underlying_type_t< Type > >
Definition: foamGltfBase.H:103
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:255
OBJstream os(runTime.globalPath()/outputName)
const ZoneType & operator[](const word &zoneName) const
Return const reference to zone by name.
Definition: ZoneMesh.C:1083
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:59
wordList groupNames() const
A list of the zone group names (if any)
Definition: ZoneMesh.C:462
bool checkDefinition(const bool report=false) const
Check zone definition. Return true if in error.
Definition: ZoneMesh.C:945
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:455
bitSet selection(const labelUList &zoneIds) const
Return all elements (cells, faces, points) contained in the listed zones.
Definition: ZoneMesh.C:800
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:137
bool hasZoneMap() const noexcept
The zoneMap has been allocated.
Definition: ZoneMesh.H:459
wordList sortedNames() const
Sorted list of the zone names.
Definition: ZoneMesh.C:490
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:1123
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
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:180
static int disallowGenericZones
Debug switch to disallow the use of generic zones.
Definition: ZoneMesh.H:143
const ZoneType * cfindZone(const word &zoneName) const
Find zone by name and return const pointer, nullptr on error.
Definition: ZoneMesh.C:755
bool writeData(Ostream &os) const
The writeData member function required by regIOobject.
Definition: ZoneMesh.C:1072
Namespace for OpenFOAM.
const HashTable< labelList > & groupZoneIDs() const
The zone indices per zone group.
Definition: ZoneMesh.C:854
void updateMetaData()
Update internal meta-data (eg, prior to writing)
Definition: ZoneMesh.C:1056
const pointField & pts