zone.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "zone.H"
27 #include "IOstream.H"
28 #include "demandDrivenData.H"
29 #include "HashSet.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
36 }
37 
38 
39 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
40 
42 {
43  if (!lookupMapPtr_)
44  {
45  calcLookupMap();
46  }
47 
48  return *lookupMapPtr_;
49 }
50 
51 
53 {
54  if (debug)
55  {
56  InfoInFunction << "Calculating lookup map" << endl;
57  }
58 
59  if (lookupMapPtr_)
60  {
62  << "Lookup map already calculated" << nl
63  << abort(FatalError);
64  }
65 
66  const labelList& addr = *this;
67 
68  lookupMapPtr_ = new Map<label>(2*addr.size());
69  Map<label>& lm = *lookupMapPtr_;
70 
71  forAll(addr, i)
72  {
73  lm.insert(addr[i], i);
74  }
75 
76  if (debug)
77  {
78  InfoInFunction << "Finished calculating lookup map" << endl;
79  }
80 }
81 
82 
83 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
84 
86 (
87  const word& name,
88  const labelUList& addr,
89  const label index
90 )
91 :
92  labelList(addr),
93  name_(name),
94  index_(index),
95  lookupMapPtr_(nullptr)
96 {}
97 
98 
100 (
101  const word& name,
102  labelList&& addr,
103  const label index
104 )
105 :
106  labelList(move(addr)),
107  name_(name),
108  index_(index),
109  lookupMapPtr_(nullptr)
110 {}
111 
112 
114 (
115  const word& name,
116  const dictionary& dict,
117  const word& labelsName,
118  const label index
119 )
120 :
121  labelList(dict.lookup(labelsName)),
122  name_(name),
123  index_(index),
124  lookupMapPtr_(nullptr)
125 {}
126 
127 
129 (
130  const zone& z,
131  const labelUList& addr,
132  const label index
133 )
134 :
135  labelList(addr),
136  name_(z.name()),
137  index_(index),
138  lookupMapPtr_(nullptr)
139 {}
140 
141 
143 (
144  const zone& z,
145  labelList&& addr,
146  const label index
147 )
148 :
149  labelList(move(addr)),
150  name_(z.name()),
151  index_(index),
152  lookupMapPtr_(nullptr)
153 {}
154 
155 
156 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
157 
159 {
160  clearAddressing();
161 }
162 
163 
164 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
165 
166 Foam::label Foam::zone::localID(const label globalCellID) const
167 {
168  const Map<label>& lm = lookupMap();
169 
170  Map<label>::const_iterator lmIter = lm.find(globalCellID);
171 
172  if (lmIter == lm.end())
173  {
174  return -1;
175  }
176  else
177  {
178  return lmIter();
179  }
180 }
181 
182 
184 {
185  deleteDemandDrivenData(lookupMapPtr_);
186 }
187 
188 
189 bool Foam::zone::checkDefinition(const label maxSize, const bool report) const
190 {
191  const labelList& addr = *this;
192 
193  bool hasError = false;
194 
195  // To check for duplicate entries
196  labelHashSet elems(size());
197 
198  forAll(addr, i)
199  {
200  if (addr[i] < 0 || addr[i] >= maxSize)
201  {
202  hasError = true;
203 
204  if (report)
205  {
207  << "Zone " << name_
208  << " contains invalid index label " << addr[i] << nl
209  << "Valid index labels are 0.."
210  << maxSize-1 << endl;
211  }
212  else
213  {
214  // w/o report - can stop checking now
215  break;
216  }
217  }
218  else if (!elems.insert(addr[i]))
219  {
220  if (report)
221  {
223  << "Zone " << name_
224  << " contains duplicate index label " << addr[i] << endl;
225  }
226  }
227  }
228 
229  return hasError;
230 }
231 
232 
233 void Foam::zone::write(Ostream& os) const
234 {
235  os << nl << name_
236  << nl << static_cast<const labelList&>(*this);
237 }
238 
239 
240 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
241 
243 {
245 }
246 
247 
249 {
250  labelList::operator=(move(zn));
251 }
252 
253 
255 {
256  labelList::operator=(addr);
257 }
258 
259 
261 {
262  labelList::operator=(move(addr));
263 }
264 
265 
266 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
267 
269 {
270  z.write(os);
271  os.check("Ostream& operator<<(Ostream& f, const zone& z");
272  return os;
273 }
274 
275 
276 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:111
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:142
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void operator=(const UList< label > &)
Assignment to UList operator. Takes linear time.
Definition: List.C:376
A HashTable to objects of type <T> with a label key.
Definition: Map.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
A class for handling words, derived from string.
Definition: word.H:62
Base class for zones.
Definition: zone.H:60
const Map< label > & lookupMap() const
Return a reference to the look-up map.
Definition: zone.C:41
virtual ~zone()
Destructor.
Definition: zone.C:158
virtual void write(Ostream &) const
Write.
Definition: zone.C:233
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.
void calcLookupMap() const
Construct the look-up map.
Definition: zone.C:52
Map< label > * lookupMapPtr_
Map of labels in zone for fast location lookup.
Definition: zone.H:76
label localID(const label globalID) const
Map storing the local index for every global index. Used to find.
Definition: zone.C:166
zone(const word &name, const labelUList &addr, const label index)
Construct from components.
Definition: zone.C:86
virtual void clearAddressing()
Clear addressing.
Definition: zone.C:183
void operator=(const zone &)
Assignment operator.
Definition: zone.C:242
Template functions to aid in the implementation of demand driven data.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
#define WarningInFunction
Report a warning using Foam::Warning.
#define InfoInFunction
Report an information message using Foam::Info.
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void deleteDemandDrivenData(DataPtr &dataPtr)
defineTypeNameAndDebug(combustionModel, 0)
error FatalError
Ostream & operator<<(Ostream &, const ensightPart &)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const char nl
Definition: Ostream.H:260
dictionary dict
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112