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 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 "IOstream.H"
31 #include "demandDrivenData.H"
32 #include "HashSet.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineTypeNameAndDebug(zone, 0);
39 }
40 
41 
42 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
43 
45 {
46  if (!lookupMapPtr_)
47  {
48  calcLookupMap();
49  }
50 
51  return *lookupMapPtr_;
52 }
53 
54 
56 {
57  if (debug)
58  {
59  InfoInFunction << "Calculating lookup map" << endl;
60  }
61 
62  if (lookupMapPtr_)
63  {
65  << "Lookup map already calculated" << nl
66  << abort(FatalError);
67  }
68 
69  const labelList& addr = *this;
70 
71  lookupMapPtr_ = new Map<label>(2*addr.size());
72  Map<label>& lm = *lookupMapPtr_;
73 
74  forAll(addr, i)
75  {
76  lm.insert(addr[i], i);
77  }
78 
79  if (debug)
80  {
81  InfoInFunction << "Finished calculating lookup map" << endl;
82  }
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
87 
88 Foam::zone::zone(const word& name, const label index)
89 :
90  labelList(),
91  name_(name),
92  index_(index),
93  lookupMapPtr_(nullptr)
94 {}
95 
96 
97 Foam::zone::zone
98 (
99  const word& name,
100  const labelUList& addr,
101  const label index
102 )
103 :
104  labelList(addr),
105  name_(name),
106  index_(index),
107  lookupMapPtr_(nullptr)
108 {}
109 
110 
111 Foam::zone::zone
112 (
113  const word& name,
114  labelList&& addr,
115  const label index
116 )
117 :
118  labelList(std::move(addr)),
119  name_(name),
120  index_(index),
121  lookupMapPtr_(nullptr)
122 {}
123 
124 
125 Foam::zone::zone
126 (
127  const word& name,
128  const dictionary& dict,
129  const word& labelsName,
130  const label index
131 )
132 :
133  labelList(dict.lookup(labelsName)),
134  name_(name),
135  index_(index),
136  lookupMapPtr_(nullptr)
137 {}
138 
139 
140 Foam::zone::zone
141 (
142  const zone& origZone,
143  const labelUList& addr,
144  const label index
145 )
146 :
147  labelList(addr),
148  name_(origZone.name()),
149  index_(index),
150  lookupMapPtr_(nullptr)
151 {}
152 
153 
154 Foam::zone::zone
155 (
156  const zone& origZone,
157  labelList&& addr,
158  const label index
159 )
160 :
161  labelList(std::move(addr)),
162  name_(origZone.name()),
163  index_(index),
164  lookupMapPtr_(nullptr)
165 {}
166 
167 
168 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
169 
171 {
172  clearAddressing();
173 }
174 
175 
176 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
177 
178 Foam::label Foam::zone::localID(const label globalCellID) const
179 {
180  return lookupMap().lookup(globalCellID, -1);
181 }
182 
183 
185 {
186  deleteDemandDrivenData(lookupMapPtr_);
187 }
188 
189 
190 bool Foam::zone::checkDefinition(const label maxSize, const bool report) const
191 {
192  const labelList& addr = *this;
193 
194  bool hasError = false;
195 
196  // To check for duplicate entries
197  labelHashSet elems(size());
198 
199  for (const label idx : addr)
200  {
201  if (idx < 0 || idx >= maxSize)
202  {
203  hasError = true;
204 
205  if (report)
206  {
208  << "Zone " << name_
209  << " contains invalid index label " << idx << nl
210  << "Valid index labels are 0.."
211  << maxSize-1 << endl;
212  }
213  else
214  {
215  // w/o report - can stop checking now
216  break;
217  }
218  }
219  else if (!elems.insert(idx))
220  {
221  if (report)
222  {
224  << "Zone " << name_
225  << " contains duplicate index label " << idx << endl;
226  }
227  }
228  }
229 
230  return hasError;
231 }
232 
233 
234 void Foam::zone::write(Ostream& os) const
235 {
236  os << nl << name_
237  << nl << static_cast<const labelList&>(*this);
238 }
239 
240 
241 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
242 
244 {
245  zn.write(os);
246  os.check(FUNCTION_NAME);
247  return os;
248 }
249 
250 
251 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:316
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::zone::calcLookupMap
void calcLookupMap() const
Construct the look-up map.
Definition: zone.C:55
zone.H
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
Foam::zone
Base class for mesh zones.
Definition: zone.H:63
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: HashTableFwd.H:46
Foam::zone::clearAddressing
virtual void clearAddressing()
Clear addressing.
Definition: zone.C:184
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::HashSet< label, Hash< label > >
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:42
Foam::zone::write
virtual void write(Ostream &os) const
Write.
Definition: zone.C:234
Foam::zone::lookupMap
const Map< label > & lookupMap() const
Return a reference to the look-up map.
Definition: zone.C:44
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:62
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::zone::~zone
virtual ~zone()
Destructor.
Definition: zone.C:170
IOstream.H
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:272
Foam::dictionary::lookup
ITstream & lookup(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.C:419
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:51
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::zone::name
const word & name() const
Return name.
Definition: zone.H:158
Foam::zone::lookupMapPtr_
Map< label > * lookupMapPtr_
Map of labels in zone for fast location lookup.
Definition: zone.H:87
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List< label >
Foam::UList< label >
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:182
Foam::zone::localID
label localID(const label globalID) const
Map storing the local index for every global index. Used to find.
Definition: zone.C:178
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::zone::checkDefinition
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.