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 | Copyright (C) 2011-2016 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 {
35  defineTypeNameAndDebug(zone, 0);
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_(NULL)
96 {}
97 
98 
100 (
101  const word& name,
102  const Xfer<labelList>& addr,
103  const label index
104 )
105 :
106  labelList(addr),
107  name_(name),
108  index_(index),
109  lookupMapPtr_(NULL)
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_(NULL)
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_(NULL)
139 {}
140 
141 
143 (
144  const zone& z,
145  const Xfer<labelList>& addr,
146  const label index
147 )
148 :
149  labelList(addr),
150  name_(z.name()),
151  index_(index),
152  lookupMapPtr_(NULL)
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 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
241 
243 {
244  z.write(os);
245  os.check("Ostream& operator<<(Ostream& f, const zone& z");
246  return os;
247 }
248 
249 
250 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
virtual void clearAddressing()
Clear addressing.
Definition: zone.C:183
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
zone(const zone &)
Disallow default bitwise copy construct.
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
void calcLookupMap() const
Construct the look-up map.
Definition: zone.C:52
virtual void write(Ostream &) const
Write.
Definition: zone.C:233
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
bool insert(const label &, const T &newElmt)
Insert a new hashedEntry.
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:138
virtual ~zone()
Destructor.
Definition: zone.C:158
const word & name() const
Return name.
Definition: zone.H:150
Base class for zones.
Definition: zone.H:57
A class for handling words, derived from string.
Definition: word.H:59
label localID(const label globalID) const
Map storing the local index for every global index. Used to find.
Definition: zone.C:166
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.
List< label > labelList
A List of labels.
Definition: labelList.H:56
errorManip< error > abort(error &err)
Definition: errorManip.H:131
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:262
defineTypeNameAndDebug(combustionModel, 0)
Template functions to aid in the implementation of demand driven data.
#define WarningInFunction
Report a warning using Foam::Warning.
Ostream & operator<<(Ostream &, const ensightPart &)
const Map< label > & lookupMap() const
Return a reference to the look-up map.
Definition: zone.C:41
void deleteDemandDrivenData(DataPtr &dataPtr)
Namespace for OpenFOAM.
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451
#define InfoInFunction
Report an information message using Foam::Info.