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-2023 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: list of zone ids per group
80  mutable std::unique_ptr<HashTable<labelList>> groupIDsPtr_;
81 
82 
83  // Private Member Functions
84 
85  //- Total of number of addressed items (all zones)
86  label totalSize() const;
87 
88  //- Create zone map
89  void calcZoneMap() const;
90 
91  //- Some zones have inGroup entries
92  bool hasGroupIDs() const;
93 
94  //- Calculate group name to zone ids lookup
95  void calcGroupIDs() const;
96 
97  //- Clear addressing at this level
98  void clearLocalAddressing();
99 
100  //- Populate/recreate from dictionary entries
101  void populate(PtrList<entry>&& entries);
102 
103  //- Return true if contents were read
104  //- (controlled by IOobject readOption flags).
105  bool readContents(const bool allowOptionalRead);
106 
107 
108 public:
109 
110  // Public Typedefs
111 
112  //- The zone type. Same as PtrList<ZoneType>::value_type
113  typedef ZoneType zone_type;
114 
115 
116  //- Debug switch to disallow the use of generic zones
117  static int disallowGenericZones;
118 
119 
120  // Generated Methods
121 
122  //- No copy construct
123  ZoneMesh(const ZoneMesh&) = delete;
124 
125  //- No copy assignment
126  void operator=(const ZoneMesh<ZoneType, MeshType>&) = delete;
127 
128 
129  // Constructors
131  //- Read construct from IOobject and mesh reference
132  //- Any reading (mandatory, optional) based on IOobject properties.
133  ZoneMesh
134  (
135  const IOobject& io,
136  const MeshType& mesh
137  );
138 
139  //- Construct empty with IOobject properties and a mesh reference.
140  //- Does not read.
141  ZoneMesh
142  (
143  const IOobject& io,
144  const MeshType& mesh,
145  Foam::zero
146  );
147 
148  //- Construct with specified size if not read.
149  //- Any reading (mandatory, optional) based on IOobject properties.
150  ZoneMesh
151  (
152  const IOobject& io,
153  const MeshType& mesh,
154  const label size
155  );
156 
157  //- Read construct (mandatory, optional) based on IOobject properties
158  //- or use the fallback PtrList (with cloning).
159  ZoneMesh
160  (
161  const IOobject& io,
162  const MeshType& mesh,
163  const PtrList<ZoneType>& list
164  );
165 
166  //- Read construct (mandatory, optional) based on IOobject properties
167  //- or use the fallback PtrList (with cloning).
168  ZoneMesh
169  (
170  const IOobject& io,
171  const MeshType& mesh,
172  PtrList<entry>&& entries
173  );
174 
175 
176  //- Destructor
177  ~ZoneMesh() = default;
178 
179 
180  // Member Functions
181 
182  //- Return the mesh reference
183  const MeshType& mesh() const noexcept { return mesh_; }
184 
185  //- Map of zones containing zone index for all zoned elements
186  // Return -1 if the object is not in the zone
187  const Map<label>& zoneMap() const;
188 
189  //- Given a global object index, return the zone it is in.
190  // If object does not belong to any zones, return -1
191  label whichZone(const label objectIndex) const;
192 
193  //- Return a list of zone types
194  wordList types() const;
195 
196  //- A list of the zone names
197  wordList names() const;
198 
199  //- A list of the zone group names (if any)
200  wordList groupNames() const;
201 
202  //- A list of zone names satisfying the input matcher
203  wordList names(const wordRe& matcher) const;
204 
205  //- A list of zone names satisfying the input matchers
206  wordList names(const wordRes& matcher) const;
207 
208  //- Sorted list of the zone names
209  wordList sortedNames() const;
210 
211  //- Sorted list of zone names satisfying the input matcher
212  wordList sortedNames(const wordRe& matcher) const;
213 
214  //- Sorted list of zone names satisfying the input matchers
215  wordList sortedNames(const wordRes& matcher) const;
216 
217 
218  //- Return (sorted) zone indices for all matches
219  // Optionally matches zone groups.
220  // A no-op (returns empty list) for an empty matcher
222  (
223  const wordRe& matcher,
224  const bool useGroups = true
225  ) const;
226 
227  //- Return (sorted) zone indices for all matches
228  // Optionally matches zone groups.
229  // A no-op (returns empty list) for an empty matcher
231  (
232  const wordRes& matcher,
233  const bool useGroups = true
234  ) const;
235 
236  //- Return (sorted) zone indices for all selected matches that
237  //- are not ignored.
238  //- The selection logic as per Foam::wordRes::filter.
239  // Optionally matches patch groups.
240  // A no-op (returns empty list) for an empty select matcher
242  (
243  const wordRes& select,
244  const wordRes& ignore,
245  const bool useGroups = true
246  ) const;
247 
248  //- Zone index for the first match, return -1 if not found
249  // A no-op (returns -1) for an empty key
250  label findIndex(const wordRe& key) const;
251 
252  //- Zone index for the first match, return -1 if not found
253  // A no-op (returns -1) for an empty matcher
254  label findIndex(const wordRes& matcher) const;
255 
256  //- Find zone index by name, return -1 if not found
257  // A no-op (returns -1) for an empty zoneName
258  label findZoneID(const word& zoneName) const;
259 
260  //- Find zone by name and return const pointer, nullptr on error
261  // A no-op (returns nullptr) for an empty zoneName
262  const ZoneType* cfindZone(const word& zoneName) const;
263 
264  //- Find zone by name and return pointer, nullptr on error
265  // A no-op (returns nullptr) for an empty zoneName
266  ZoneType* findZone(const word& zoneName);
267 
268 
269  //- Return all elements (cells, faces, points) contained in the
270  //- listed zones.
271  // The bitSet is empty (zero-size) if there are no elements matched
272  // anywhere.
273  bitSet selection(const labelUList& zoneIds) const;
274 
275  //- Return all elements (cells, faces, points) that match the zone
276  //- specification as a bitSet.
277  // The bitSet is empty (zero-size) if there are no elements matched
278  // anywhere.
279  // Optionally matches zoneGroups.
280  // A no-op (returns empty bitSet) for an empty matcher
282  (
283  const wordRe& matcher,
284  const bool useGroups = true
285  ) 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  // A no-op (returns empty bitSet) for an empty matcher
293  (
294  const wordRes& matcher,
295  const bool useGroups = true
296  ) const;
297 
298  //- The zone indices per zone group
299  const HashTable<labelList>& groupZoneIDs() const;
300 
301  //- Set/add group with zones
302  void setGroup(const word& groupName, const labelUList& zoneIDs);
303 
304  //- Check zone definition. Return true if in error.
305  bool checkDefinition(const bool report = false) const;
306 
307  //- Check whether all procs have all zones and in same order.
308  // \return True if any errors.
309  bool checkParallelSync(const bool report = false) const;
310 
311  //- Correct zone mesh after moving points
312  void movePoints(const pointField& pts);
313 
314 
315  // Storage Management
316 
317  //- Clear addressing
318  void clearAddressing();
319 
320  //- Clear primitive addressing
321  void clearPrimitives();
322 
323  //- Clear the zones
324  void clear();
325 
326  //- The zoneMap has been allocated
327  bool hasZoneMap() const noexcept { return bool(zoneMapPtr_); }
328 
329 
330  // Member Operators
331 
332  //- Return const and non-const reference to zone by index.
334 
335  //- Return const reference to zone by name.
336  // Fatal if the zone does not exist.
337  const ZoneType& operator[](const word& zoneName) const;
338 
339  //- Return reference to an existing zone by name
340  // Fatal if the zone does not exist.
341  ZoneType& operator[](const word& zoneName);
342 
343  //- Find an existing zone by name or create a new empty one
344  //- if required.
345  //
346  // To determine if the zone already existed or was newly created,
347  // it will be necessary to add additional logic in the caller.
348  // For example,
349  // \code
350  // const label nOrig = zones.size();
351  //
352  // ZoneType& zn = zones("zoneName");
353  //
354  // if (nOrig == zones.size()) { existing... } else { new... }
355  // \endcode
356  // \param zoneName the name of the zone
357  // \param verbose report if an existing zone was selected or
358  // a new zone was created.
359  // \return non-const reference to the existing or new zone
360  ZoneType& operator()(const word& zoneName, const bool verbose=false);
361 
362 
363  // IO
364 
365  //- Update internal meta-data (eg, prior to writing)
366  void updateMetaData();
367 
368  //- The writeData member function required by regIOobject
369  bool writeData(Ostream& os) const;
370 
371 
372  // Ostream Operator
373 
374  friend Ostream& operator<< <ZoneType, MeshType>
375  (
376  Ostream& os,
377  const ZoneMesh<ZoneType, MeshType>& zones
378  );
379 
380 
381  // Housekeeping
382 
383  //- Identical to the indices() method (AUG-2018)
384  FOAM_DEPRECATED_FOR(2018-08, "indices() method")
385  labelList findIndices(const wordRes& key) const
386  {
387  return indices(key);
388  }
389 };
390 
391 
392 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
393 
394 } // End namespace Foam
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398 #ifdef NoRepository
399  #include "ZoneMesh.C"
400 #endif
401 
402 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
403 
404 #endif
405 
406 // ************************************************************************* //
bool checkParallelSync(const bool report=false) const
Check whether all procs have all zones and in same order.
Definition: ZoneMesh.C:869
const labelIOList & zoneIDs
Definition: correctPhi.H:59
wordList types() const
Return a list of zone types.
Definition: ZoneMesh.C:355
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:815
ZoneType * findZone(const word &zoneName)
Find zone by name and return pointer, nullptr on error.
Definition: ZoneMesh.C:696
void clear()
Clear the zones.
Definition: ZoneMesh.C:841
label findIndex(const wordRe &key) const
Zone index for the first match, return -1 if not found.
Definition: ZoneMesh.C:601
const Map< label > & zoneMap() const
Map of zones containing zone index for all zoned elements.
Definition: ZoneMesh.C:333
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:220
void movePoints(const pointField &pts)
Correct zone mesh after moving points.
Definition: ZoneMesh.C:949
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:513
labelList indices(const wordRe &matcher, const bool useGroups=true) const
Return (sorted) zone indices for all matches.
Definition: ZoneMesh.C:435
label findZoneID(const word &zoneName) const
Find zone index by name, return -1 if not found.
Definition: ZoneMesh.C:629
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:773
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:829
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:988
label whichZone(const label objectIndex) const
Given a global object index, return the zone it is in.
Definition: ZoneMesh.C:346
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:369
bool checkDefinition(const bool report=false) const
Check zone definition. Return true if in error.
Definition: ZoneMesh.C:850
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:362
bitSet selection(const labelUList &zoneIds) const
Return all elements (cells, faces, points) contained in the listed zones.
Definition: ZoneMesh.C:706
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:130
bool hasZoneMap() const noexcept
The zoneMap has been allocated.
Definition: ZoneMesh.H:438
wordList sortedNames() const
Sorted list of the zone names.
Definition: ZoneMesh.C:397
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:1028
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
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:172
static int disallowGenericZones
Debug switch to disallow the use of generic zones.
Definition: ZoneMesh.H:136
const ZoneType * cfindZone(const word &zoneName) const
Find zone by name and return const pointer, nullptr on error.
Definition: ZoneMesh.C:661
bool writeData(Ostream &os) const
The writeData member function required by regIOobject.
Definition: ZoneMesh.C:977
Namespace for OpenFOAM.
const HashTable< labelList > & groupZoneIDs() const
The zone indices per zone group.
Definition: ZoneMesh.C:760
void updateMetaData()
Update internal meta-data (eg, prior to writing)
Definition: ZoneMesh.C:961
const pointField & pts