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-2012 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  Info<< "void zone::calcLookupMap() const: "
57  << "Calculating lookup map"
58  << endl;
59  }
60 
61  if (lookupMapPtr_)
62  {
63  FatalErrorIn("void zone::calcLookupMap() const")
64  << "Lookup map already calculated" << nl
65  << abort(FatalError);
66  }
67 
68  const labelList& addr = *this;
69 
70  lookupMapPtr_ = new Map<label>(2*addr.size());
71  Map<label>& lm = *lookupMapPtr_;
72 
73  forAll(addr, i)
74  {
75  lm.insert(addr[i], i);
76  }
77 
78  if (debug)
79  {
80  Info<< "void zone::calcLookupMap() const: "
81  << "Finished calculating lookup map"
82  << endl;
83  }
84 }
85 
86 
87 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
88 
90 (
91  const word& name,
92  const labelUList& addr,
93  const label index
94 )
95 :
96  labelList(addr),
97  name_(name),
98  index_(index),
99  lookupMapPtr_(NULL)
100 {}
101 
102 
104 (
105  const word& name,
106  const Xfer<labelList>& addr,
107  const label index
108 )
109 :
110  labelList(addr),
111  name_(name),
112  index_(index),
113  lookupMapPtr_(NULL)
114 {}
115 
116 
118 (
119  const word& name,
120  const dictionary& dict,
121  const word& labelsName,
122  const label index
123 )
124 :
125  labelList(dict.lookup(labelsName)),
126  name_(name),
127  index_(index),
128  lookupMapPtr_(NULL)
129 {}
130 
131 
133 (
134  const zone& z,
135  const labelUList& addr,
136  const label index
137 )
138 :
139  labelList(addr),
140  name_(z.name()),
141  index_(index),
142  lookupMapPtr_(NULL)
143 {}
144 
145 
147 (
148  const zone& z,
149  const Xfer<labelList>& addr,
150  const label index
151 )
152 :
153  labelList(addr),
154  name_(z.name()),
155  index_(index),
156  lookupMapPtr_(NULL)
157 {}
158 
159 
160 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
161 
163 {
164  clearAddressing();
165 }
166 
167 
168 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 
170 Foam::label Foam::zone::localID(const label globalCellID) const
171 {
172  const Map<label>& lm = lookupMap();
173 
174  Map<label>::const_iterator lmIter = lm.find(globalCellID);
175 
176  if (lmIter == lm.end())
177  {
178  return -1;
179  }
180  else
181  {
182  return lmIter();
183  }
184 }
185 
186 
188 {
189  deleteDemandDrivenData(lookupMapPtr_);
190 }
191 
192 
193 bool Foam::zone::checkDefinition(const label maxSize, const bool report) const
194 {
195  const labelList& addr = *this;
196 
197  bool hasError = false;
198 
199  // To check for duplicate entries
200  labelHashSet elems(size());
201 
202  forAll(addr, i)
203  {
204  if (addr[i] < 0 || addr[i] >= maxSize)
205  {
206  hasError = true;
207 
208  if (report)
209  {
211  (
212  "bool zone::checkDefinition("
213  "const label maxSize, const bool report) const"
214  ) << "Zone " << name_
215  << " contains invalid index label " << addr[i] << nl
216  << "Valid index labels are 0.."
217  << maxSize-1 << endl;
218  }
219  else
220  {
221  // w/o report - can stop checking now
222  break;
223  }
224  }
225  else if (!elems.insert(addr[i]))
226  {
227  if (report)
228  {
229  WarningIn
230  (
231  "bool zone::checkDefinition("
232  "const label maxSize, const bool report) const"
233  ) << "Zone " << name_
234  << " contains duplicate index label " << addr[i] << endl;
235  }
236  }
237  }
238 
239  return hasError;
240 }
241 
242 
243 void Foam::zone::write(Ostream& os) const
244 {
245  os << nl << name_
246  << nl << static_cast<const labelList&>(*this);
247 }
248 
249 
250 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
251 
253 {
254  z.write(os);
255  os.check("Ostream& operator<<(Ostream& f, const zone& z");
256  return os;
257 }
258 
259 
260 // ************************************************************************* //
#define SeriousErrorIn(functionName)
Report an error message using Foam::SeriousError.
virtual void write(Ostream &) const
Write.
Definition: zone.C:243
bool insert(const label &, const T &newElmt)
Insert a new hashedEntry.
virtual ~zone()
Destructor.
Definition: zone.C:162
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49
zone(const zone &)
Disallow default bitwise copy construct.
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
void deleteDemandDrivenData(DataPtr &dataPtr)
A class for handling words, derived from string.
Definition: word.H:59
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
messageStream Info
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:133
Namespace for OpenFOAM.
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define WarningIn(functionName)
Report a warning using Foam::Warning.
Base class for zones.
Definition: zone.H:57
virtual void clearAddressing()
Clear addressing.
Definition: zone.C:187
label localID(const label globalID) const
Map storing the local index for every global index. Used to find.
Definition: zone.C:170
#define forAll(list, i)
Definition: UList.H:421
Template functions to aid in the implementation of demand driven data.
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word & name() const
Return name.
Definition: zone.H:150
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
void calcLookupMap() const
Construct the look-up map.
Definition: zone.C:52
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
error FatalError
List< label > labelList
A List of labels.
Definition: labelList.H:56
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 iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:139
defineTypeNameAndDebug(combustionModel, 0)
const Map< label > & lookupMap() const
Return a reference to the look-up map.
Definition: zone.C:41
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116