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-2024 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  //- Return (sorted) zone indices for all matches
231  // Optionally matches zone groups.
232  // A no-op (returns empty list) for an empty matcher
234  (
235  const wordRe& matcher,
236  const bool useGroups = true
237  ) const;
238 
239  //- Return (sorted) zone indices for all matches
240  // Optionally matches zone groups.
241  // A no-op (returns empty list) for an empty matcher
243  (
244  const wordRes& matcher,
245  const bool useGroups = true
246  ) const;
247 
248  //- Return (sorted) zone indices for all selected matches that
249  //- are not ignored.
250  //- The selection logic as per Foam::wordRes::filter.
251  // Optionally matches patch groups.
252  // A no-op (returns empty list) for an empty select matcher
254  (
255  const wordRes& select,
256  const wordRes& ignore,
257  const bool useGroups = true
258  ) const;
259 
260  //- Zone index for the first match, return -1 if not found
261  // A no-op (returns -1) for an empty key
262  label findIndex(const wordRe& key) const;
263 
264  //- Zone index for the first match, return -1 if not found
265  // A no-op (returns -1) for an empty matcher
266  label findIndex(const wordRes& matcher) const;
267 
268  //- Find zone index by name, return -1 if not found
269  // A no-op (returns -1) for an empty zoneName
270  label findZoneID(const word& zoneName) const;
271 
272  //- Find zone by name and return const pointer, nullptr on error
273  // A no-op (returns nullptr) for an empty zoneName
274  const ZoneType* cfindZone(const word& zoneName) const;
275 
276  //- Find zone by name and return pointer, nullptr on error
277  // A no-op (returns nullptr) for an empty zoneName
278  ZoneType* findZone(const word& zoneName);
279 
280 
281  //- Return all elements (cells, faces, points) contained in the
282  //- listed zones.
283  // The bitSet is empty (zero-size) if there are no elements matched
284  // anywhere.
285  bitSet selection(const labelUList& zoneIds) const;
286 
287  //- Return all elements (cells, faces, points) that match the zone
288  //- specification as a bitSet.
289  // The bitSet is empty (zero-size) if there are no elements matched
290  // anywhere.
291  // Optionally matches zoneGroups.
292  // A no-op (returns empty bitSet) for an empty matcher
294  (
295  const wordRe& matcher,
296  const bool useGroups = true
297  ) const;
298 
299  //- Return all elements (cells, faces, points) that match the zone
300  //- specification as a bitSet.
301  // The bitSet is empty (zero-size) if there are no elements matched
302  // anywhere.
303  // A no-op (returns empty bitSet) for an empty matcher
305  (
306  const wordRes& matcher,
307  const bool useGroups = true
308  ) const;
309 
310  //- The zone indices per zone group
311  const HashTable<labelList>& groupZoneIDs() const;
312 
313  //- Set/add group with zones
314  void setGroup(const word& groupName, const labelUList& zoneIDs);
315 
316  //- Check zone definition. Return true if in error.
317  bool checkDefinition(const bool report = false) const;
318 
319  //- Check whether all procs have all zones and in same order.
320  // \return True if any errors.
321  bool checkParallelSync(const bool report = false) const;
322 
323  //- Correct zone mesh after moving points
324  void movePoints(const pointField& pts);
325 
326 
327  // Storage Management
328 
329  //- Clear addressing
330  void clearAddressing();
331 
332  //- Clear primitive addressing
333  void clearPrimitives();
334 
335  //- Clear the zones
336  void clear();
337 
338  //- The zoneMap has been allocated
339  bool hasZoneMap() const noexcept { return bool(zoneMapPtr_); }
340 
341 
342  // Member Operators
343 
344  //- Return const and non-const reference to zone by index.
346 
347  //- Return const reference to zone by name.
348  // Fatal if the zone does not exist.
349  const ZoneType& operator[](const word& zoneName) const;
350 
351  //- Return reference to an existing zone by name
352  // Fatal if the zone does not exist.
353  ZoneType& operator[](const word& zoneName);
354 
355  //- Find an existing zone by name or create a new empty one
356  //- if required.
357  //
358  // To determine if the zone already existed or was newly created,
359  // it will be necessary to add additional logic in the caller.
360  // For example,
361  // \code
362  // const label nOrig = zones.size();
363  //
364  // ZoneType& zn = zones("zoneName");
365  //
366  // if (nOrig == zones.size()) { existing... } else { new... }
367  // \endcode
368  // \param zoneName the name of the zone
369  // \param verbose report if an existing zone was selected or
370  // a new zone was created.
371  // \return non-const reference to the existing or new zone
372  ZoneType& operator()(const word& zoneName, const bool verbose=false);
373 
374 
375  // IO
376 
377  //- Update internal meta-data (eg, prior to writing)
378  void updateMetaData();
379 
380  //- The writeData member function required by regIOobject
381  bool writeData(Ostream& os) const;
382 
383 
384  // Ostream Operator
385 
386  friend Ostream& operator<< <ZoneType, MeshType>
387  (
388  Ostream& os,
389  const ZoneMesh<ZoneType, MeshType>& zones
390  );
391 
392 
393  // Housekeeping
394 
395  //- Identical to the indices() method (AUG-2018)
396  FOAM_DEPRECATED_FOR(2018-08, "indices() method")
397  labelList findIndices(const wordRes& key) const
398  {
399  return indices(key);
400  }
401 };
402 
403 
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 
406 } // End namespace Foam
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 
410 #ifdef NoRepository
411  #include "ZoneMesh.C"
412 #endif
413 
414 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
415 
416 #endif
417 
418 // ************************************************************************* //
bool checkParallelSync(const bool report=false) const
Check whether all procs have all zones and in same order.
Definition: ZoneMesh.C:959
const labelIOList & zoneIDs
Definition: correctPhi.H:59
wordList types() const
Return a list of zone types.
Definition: ZoneMesh.C:444
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:905
ZoneType * findZone(const word &zoneName)
Find zone by name and return pointer, nullptr on error.
Definition: ZoneMesh.C:785
void clear()
Clear the zones.
Definition: ZoneMesh.C:931
label findIndex(const wordRe &key) const
Zone index for the first match, return -1 if not found.
Definition: ZoneMesh.C:690
const Map< label > & zoneMap() const
Map of zones containing zone index for all zoned elements.
Definition: ZoneMesh.C:393
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
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:1039
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:531
labelList indices(const wordRe &matcher, const bool useGroups=true) const
Return (sorted) zone indices for all matches.
Definition: ZoneMesh.C:524
label findZoneID(const word &zoneName) const
Find zone index by name, return -1 if not found.
Definition: ZoneMesh.C:718
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:862
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:919
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: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
const ZoneType & operator[](const word &zoneName) const
Return const reference to zone by name.
Definition: ZoneMesh.C:1078
label whichZone(const label objectIndex) const
Given a global object index, return the zone it is in.
Definition: ZoneMesh.C:406
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
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
wordList groupNames() const
A list of the zone group names (if any)
Definition: ZoneMesh.C:458
bool checkDefinition(const bool report=false) const
Check zone definition. Return true if in error.
Definition: ZoneMesh.C:940
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:451
bitSet selection(const labelUList &zoneIds) const
Return all elements (cells, faces, points) contained in the listed zones.
Definition: ZoneMesh.C:795
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:456
wordList sortedNames() const
Sorted list of the zone names.
Definition: ZoneMesh.C:486
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:1118
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:416
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:750
bool writeData(Ostream &os) const
The writeData member function required by regIOobject.
Definition: ZoneMesh.C:1067
Namespace for OpenFOAM.
const HashTable< labelList > & groupZoneIDs() const
The zone indices per zone group.
Definition: ZoneMesh.C:849
void updateMetaData()
Update internal meta-data (eg, prior to writing)
Definition: ZoneMesh.C:1051
const pointField & pts