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-2022 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 autoPtr<Map<label>> zoneMapPtr_;
78 
79  //- Demand-driven: list of zone ids per group
80  mutable autoPtr<HashTable<labelList>> groupIDsPtr_;
81 
82 
83  // Private Member Functions
84 
85  //- Read if IOobject flags set. Return true if read.
86  bool readContents();
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  //- No copy construct
98  ZoneMesh(const ZoneMesh&) = delete;
99 
100  //- No copy assignment
101  void operator=(const ZoneMesh<ZoneType, MeshType>&) = delete;
102 
103 
104 public:
105 
106  //- Debug switch to disallow the use of generic zones
107  static int disallowGenericZones;
108 
109 
110  // Constructors
111 
112  //- Read constructor given IOobject and a MeshType reference
113  ZoneMesh
114  (
115  const IOobject& io,
116  const MeshType& mesh
117  );
118 
119  //- Construct given size
120  ZoneMesh
121  (
122  const IOobject& io,
123  const MeshType& mesh,
124  const label size
125  );
126 
127  //- Construct given a PtrList
128  ZoneMesh
129  (
130  const IOobject& io,
131  const MeshType& mesh,
132  const PtrList<ZoneType>& pzm
133  );
134 
135 
136  //- Destructor
137  ~ZoneMesh();
138 
139 
140  // Member Functions
141 
142  //- Return the mesh reference
143  const MeshType& mesh() const noexcept
144  {
145  return mesh_;
146  }
147 
148  //- Map of zones containing zone index for all zoned elements
149  // Return -1 if the object is not in the zone
150  const Map<label>& zoneMap() const;
151 
152  //- Given a global object index, return the zone it is in.
153  // If object does not belong to any zones, return -1
154  label whichZone(const label objectIndex) const;
155 
156  //- Return a list of zone types
157  wordList types() const;
158 
159  //- A list of the zone names
160  wordList names() const;
161 
162  //- A list of the zone group names (if any)
163  wordList groupNames() const;
164 
165  //- A list of zone names satisfying the input matcher
166  wordList names(const wordRe& matcher) const;
167 
168  //- A list of zone names satisfying the input matchers
169  wordList names(const wordRes& matcher) const;
170 
171  //- Sorted list of the zone names
172  wordList sortedNames() const;
173 
174  //- Sorted list of zone names satisfying the input matcher
175  wordList sortedNames(const wordRe& matcher) const;
176 
177  //- Sorted list of zone names satisfying the input matchers
178  wordList sortedNames(const wordRes& matcher) const;
179 
180 
181  //- Return (sorted) zone indices for all matches
182  // Optionally matches zone groups.
183  // A no-op (returns empty list) for an empty matcher
185  (
186  const wordRe& matcher,
187  const bool useGroups = true
188  ) const;
189 
190  //- Return (sorted) zone indices for all matches
191  // Optionally matches zone groups.
192  // A no-op (returns empty list) for an empty matcher
194  (
195  const wordRes& matcher,
196  const bool useGroups = true
197  ) const;
198 
199  //- Zone index for the first match, return -1 if not found
200  // A no-op (returns -1) for an empty key
201  label findIndex(const wordRe& key) const;
202 
203  //- Zone index for the first match, return -1 if not found
204  // A no-op (returns -1) for an empty matcher
205  label findIndex(const wordRes& matcher) const;
206 
207  //- Find zone index by name, return -1 if not found
208  // A no-op (returns -1) for an empty zoneName
209  label findZoneID(const word& zoneName) const;
210 
211  //- Find zone by name and return const pointer, nullptr on error
212  // A no-op (returns nullptr) for an empty zoneName
213  const ZoneType* cfindZone(const word& zoneName) const;
214 
215  //- Find zone by name and return pointer, nullptr on error
216  // A no-op (returns nullptr) for an empty zoneName
217  ZoneType* findZone(const word& zoneName);
218 
219 
220  //- Return all elements (cells, faces, points) contained in the
221  //- listed zones.
222  // The bitSet is empty (zero-size) if there are no elements matched
223  // anywhere.
224  bitSet selection(const labelUList& zoneIds) const;
225 
226  //- Return all elements (cells, faces, points) that match the zone
227  //- specification as a bitSet.
228  // The bitSet is empty (zero-size) if there are no elements matched
229  // anywhere.
230  // Optionally matches zoneGroups.
231  // A no-op (returns empty bitSet) for an empty matcher
233  (
234  const wordRe& matcher,
235  const bool useGroups = true
236  ) const;
237 
238  //- Return all elements (cells, faces, points) that match the zone
239  //- specification as a bitSet.
240  // The bitSet is empty (zero-size) if there are no elements matched
241  // anywhere.
242  // A no-op (returns empty bitSet) for an empty matcher
244  (
245  const wordRes& matcher,
246  const bool useGroups = true
247  ) const;
248 
249  //- The zone indices per zone group
250  const HashTable<labelList>& groupZoneIDs() const;
251 
252  //- Set/add group with zones
253  void setGroup(const word& groupName, const labelUList& zoneIDs);
254 
255  //- Check zone definition. Return true if in error.
256  bool checkDefinition(const bool report = false) const;
257 
258  //- Check whether all procs have all zones and in same order.
259  // \return True if any errors.
260  bool checkParallelSync(const bool report = false) const;
261 
262  //- Correct zone mesh after moving points
263  void movePoints(const pointField& pts);
264 
265 
266  // Storage Management
267 
268  //- Clear addressing
269  void clearAddressing();
270 
271  //- Clear the zones
272  void clear();
273 
274  //- The zoneMap has been allocated
275  bool hasZoneMap() const noexcept { return bool(zoneMapPtr_); }
276 
277 
278  // Member Operators
279 
280  //- Return const and non-const reference to zone by index.
282 
283  //- Return const reference to zone by name.
284  // Fatal if the zone does not exist.
285  const ZoneType& operator[](const word& zoneName) const;
286 
287  //- Return reference to an existing zone by name
288  // Fatal if the zone does not exist.
289  ZoneType& operator[](const word& zoneName);
290 
291  //- Find an existing zone by name or create a new empty one
292  //- if required.
293  //
294  // To determine if the zone already existed or was newly created,
295  // it will be necessary to add additional logic in the caller.
296  // For example,
297  // \code
298  // const label nOrig = zones.size();
299  //
300  // ZoneType& zn = zones("zoneName");
301  //
302  // if (nOrig == zones.size()) { existing... } else { new... }
303  // \endcode
304  // \param zoneName the name of the zone
305  // \param verbose report if an existing zone was selected or
306  // a new zone was created.
307  // \return non-const reference to the existing or new zone
308  ZoneType& operator()(const word& zoneName, const bool verbose=false);
309 
310 
311  // IO
312 
313  //- Update internal meta-data (eg, prior to writing)
314  void updateMetaData();
315 
316  //- The writeData member function required by regIOobject
317  bool writeData(Ostream& os) const;
318 
319 
320  // Ostream Operator
321 
322  friend Ostream& operator<< <ZoneType, MeshType>
323  (
324  Ostream& os,
325  const ZoneMesh<ZoneType, MeshType>& zones
326  );
327 
328 
329  // Housekeeping
330 
331  //- Identical to the indices() method (AUG-2018)
332  FOAM_DEPRECATED_FOR(2018-08, "indices() method")
333  labelList findIndices(const wordRes& key) const
334  {
335  return indices(key);
336  }
337 };
338 
339 
340 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
341 
342 } // End namespace Foam
343 
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
345 
346 #ifdef NoRepository
347  #include "ZoneMesh.C"
348 #endif
349 
350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
351 
352 #endif
353 
354 // ************************************************************************* //
bool checkParallelSync(const bool report=false) const
Check whether all procs have all zones and in same order.
Definition: ZoneMesh.C:743
const labelIOList & zoneIDs
Definition: correctPhi.H:59
wordList types() const
Return a list of zone types.
Definition: ZoneMesh.C:285
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:700
ZoneType * findZone(const word &zoneName)
Find zone by name and return pointer, nullptr on error.
Definition: ZoneMesh.C:580
void clear()
Clear the zones.
Definition: ZoneMesh.C:715
label findIndex(const wordRe &key) const
Zone index for the first match, return -1 if not found.
Definition: ZoneMesh.C:485
const Map< label > & zoneMap() const
Map of zones containing zone index for all zoned elements.
Definition: ZoneMesh.C:263
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:482
A list of mesh zones.
~ZoneMesh()
Destructor.
Definition: ZoneMesh.C:253
const MeshType & mesh() const noexcept
Return the mesh reference.
Definition: ZoneMesh.H:168
void movePoints(const pointField &pts)
Correct zone mesh after moving points.
Definition: ZoneMesh.C:823
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:444
labelList indices(const wordRe &matcher, const bool useGroups=true) const
Return (sorted) zone indices for all matches.
Definition: ZoneMesh.C:365
label findZoneID(const word &zoneName) const
Find zone index by name, return -1 if not found.
Definition: ZoneMesh.C:513
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:657
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:113
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
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:55
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:862
label whichZone(const label objectIndex) const
Given a global object index, return the zone it is in.
Definition: ZoneMesh.C:276
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:299
bool checkDefinition(const bool report=false) const
Check zone definition. Return true if in error.
Definition: ZoneMesh.C:724
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:292
bitSet selection(const labelUList &zoneIds) const
Return all elements (cells, faces, points) contained in the listed zones.
Definition: ZoneMesh.C:590
bool hasZoneMap() const noexcept
The zoneMap has been allocated.
Definition: ZoneMesh.H:369
wordList sortedNames() const
Sorted list of the zone names.
Definition: ZoneMesh.C:327
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:902
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:171
static int disallowGenericZones
Debug switch to disallow the use of generic zones.
Definition: ZoneMesh.H:122
const ZoneType * cfindZone(const word &zoneName) const
Find zone by name and return const pointer, nullptr on error.
Definition: ZoneMesh.C:545
bool writeData(Ostream &os) const
The writeData member function required by regIOobject.
Definition: ZoneMesh.C:851
Namespace for OpenFOAM.
const HashTable< labelList > & groupZoneIDs() const
The zone indices per zone group.
Definition: ZoneMesh.C:644
void updateMetaData()
Update internal meta-data (eg, prior to writing)
Definition: ZoneMesh.C:835
const pointField & pts