zone.C
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) 2017-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 \*---------------------------------------------------------------------------*/
28 
29 #include "zone.H"
30 #include "dictionary.H"
31 #include "HashSet.H"
32 #include "IOstream.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
39 }
40 
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
45 :
47  labelList(),
48  lookupMapPtr_(nullptr)
49 {}
50 
51 
52 Foam::zone::zone(const word& name, const label index)
53 :
55  labelList(),
56  lookupMapPtr_(nullptr)
57 {}
58 
59 
61 (
62  const word& name,
63  const labelUList& addr,
64  const label index
65 )
66 :
67  zone(name, index)
68 {
69  labelList::operator=(addr);
70 }
71 
72 
74 (
75  const word& name,
76  labelList&& addr,
77  const label index
78 )
79 :
80  zone(name, index)
81 {
82  labelList::transfer(addr);
83 }
84 
85 
87 (
88  const word& name,
89  const dictionary& dict,
90  const word& labelsName,
91  const label index
92 )
93 :
95  labelList(dict.get<labelList>(labelsName)),
96  lookupMapPtr_(nullptr)
97 {}
98 
99 
101 (
102  const zone& originalZone,
103  const labelUList& addr,
104  const label newIndex
105 )
106 :
107  zoneIdentifier(originalZone, newIndex),
108  labelList(addr),
109  lookupMapPtr_(nullptr)
110 {}
111 
112 
114 (
115  const zone& originalZone,
116  labelList&& addr,
117  const label newIndex
118 )
119 :
120  zoneIdentifier(originalZone, newIndex),
121  labelList(std::move(addr)),
122  lookupMapPtr_(nullptr)
123 {}
124 
125 
126 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127 
129 {
130  if (!lookupMapPtr_)
131  {
132  const labelList& addr = *this;
133 
134  lookupMapPtr_.reset(new Map<label>(2*addr.size()));
135  auto& map = *lookupMapPtr_;
136 
137  for (const label id : addr)
138  {
139  map.insert(id, map.size());
140  }
141  }
142 
143  return *lookupMapPtr_;
144 }
145 
147 Foam::label Foam::zone::localID(const label globalID) const
148 {
149  return lookupMap().lookup(globalID, -1);
150 }
151 
154 {
155  lookupMapPtr_.reset(nullptr);
156 }
157 
160 {
161  static_cast<labelList&>(*this).clear();
162 }
163 
164 
165 bool Foam::zone::checkDefinition(const label maxSize, const bool report) const
166 {
167  const labelList& addr = *this;
168 
169  bool hasError = false;
170 
171  // To check for duplicate entries
172  labelHashSet elems(2*size());
173 
174  for (const label id : addr)
175  {
176  if (id < 0 || id >= maxSize)
177  {
178  hasError = true;
179 
180  if (report)
181  {
183  << "Zone " << this->name()
184  << " contains invalid index label " << id << nl
185  << "Valid index labels are 0.."
186  << maxSize-1 << endl;
187  }
188  else
189  {
190  // w/o report - can stop checking now
191  break;
192  }
193  }
194  else if (!elems.insert(id))
195  {
196  if (report)
197  {
199  << "Zone " << this->name()
200  << " contains duplicate index label " << id << endl;
201  }
202  }
203  }
204 
205  return hasError;
206 }
207 
208 
209 void Foam::zone::write(Ostream& os) const
210 {
211  os << nl << this->name()
212  << nl << static_cast<const labelList&>(*this);
213 }
214 
215 
216 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
217 
218 Foam::Ostream& Foam::operator<<(Ostream& os, const zone& zn)
219 {
220  zn.write(os);
222  return os;
223 }
224 
225 
226 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
zone()
Default construct.
Definition: zone.C:37
virtual void clearAddressing()
Clear addressing (remove lookup maps and other auxiliary information)
Definition: zone.C:146
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
label localID(const label globalID) const
Lookup local address in zone for given global index.
Definition: zone.C:140
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual void clearPrimitives()
Clear primitive addressing.
Definition: zone.C:152
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
defineTypeName(manifoldCellsMeshObject)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Base class for mesh zones.
Definition: zone.H:59
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:137
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
virtual void write(Ostream &os) const
Write.
Definition: zone.C:202
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
Identifies a mesh zone by name and index, with optional physical type and group information.
#define WarningInFunction
Report a warning using Foam::Warning.
const Map< label > & lookupMap() const
Demand-driven: the look-up map from global to local id.
Definition: zone.C:121
Namespace for OpenFOAM.
A HashTable to objects of type <T> with a label key.