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-2015 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  {
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  << "Zone " << name_
212  << " contains invalid index label " << addr[i] << nl
213  << "Valid index labels are 0.."
214  << maxSize-1 << endl;
215  }
216  else
217  {
218  // w/o report - can stop checking now
219  break;
220  }
221  }
222  else if (!elems.insert(addr[i]))
223  {
224  if (report)
225  {
227  << "Zone " << name_
228  << " contains duplicate index label " << addr[i] << endl;
229  }
230  }
231  }
232 
233  return hasError;
234 }
235 
236 
237 void Foam::zone::write(Ostream& os) const
238 {
239  os << nl << name_
240  << nl << static_cast<const labelList&>(*this);
241 }
242 
243 
244 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
245 
247 {
248  z.write(os);
249  os.check("Ostream& operator<<(Ostream& f, const zone& z");
250  return os;
251 }
252 
253 
254 // ************************************************************************* //
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::labelList
List< label > labelList
A List of labels.
Definition: labelList.H:56
Foam::zone::calcLookupMap
void calcLookupMap() const
Construct the look-up map.
Definition: zone.C:52
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
zone.H
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::zone
Base class for zones.
Definition: zone.H:57
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: PrimitivePatchTemplate.H:68
Foam::zone::clearAddressing
virtual void clearAddressing()
Clear addressing.
Definition: zone.C:187
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::HashSet< label, Hash< label > >
Foam::Xfer
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:40
Foam::zone::lookupMap
const Map< label > & lookupMap() const
Return a reference to the look-up map.
Definition: zone.C:41
Foam::label
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
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::zone::~zone
virtual ~zone()
Destructor.
Definition: zone.C:162
IOstream.H
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:237
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::zone::write
virtual void write(Ostream &) const
Write.
Definition: zone.C:237
HashSet.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::zone::name
const word & name() const
Return name.
Definition: zone.H:150
Foam::zone::lookupMapPtr_
Map< label > * lookupMapPtr_
Map of labels in zone for fast location lookup.
Definition: zone.H:76
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::UList
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
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
Foam::zone::localID
label localID(const label globalID) const
Map storing the local index for every global index. Used to find.
Definition: zone.C:170
Foam::zone::zone
zone(const zone &)
Disallow default bitwise copy construct.
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::zone::checkDefinition
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.