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-2020 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  DebugInFunction << "Calculating lookup map" << endl;
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  DebugInfo << "Finished calculating lookup map" << endl;
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
81 
82 Foam::zone::zone(const word& name, const label index)
83 :
84  labelList(),
85  name_(name),
86  index_(index),
87  lookupMapPtr_(nullptr)
88 {}
89 
90 
91 Foam::zone::zone
92 (
93  const word& name,
94  const labelUList& addr,
95  const label index
96 )
97 :
98  labelList(addr),
99  name_(name),
100  index_(index),
101  lookupMapPtr_(nullptr)
102 {}
103 
104 
105 Foam::zone::zone
106 (
107  const word& name,
108  labelList&& addr,
109  const label index
110 )
111 :
112  labelList(std::move(addr)),
113  name_(name),
114  index_(index),
115  lookupMapPtr_(nullptr)
116 {}
117 
118 
119 Foam::zone::zone
120 (
121  const word& name,
122  const dictionary& dict,
123  const word& labelsName,
124  const label index
125 )
126 :
127  labelList(dict.lookup(labelsName)),
128  name_(name),
129  index_(index),
130  lookupMapPtr_(nullptr)
131 {}
132 
133 
134 Foam::zone::zone
135 (
136  const zone& origZone,
137  const labelUList& addr,
138  const label index
139 )
140 :
141  labelList(addr),
142  name_(origZone.name()),
143  index_(index),
144  lookupMapPtr_(nullptr)
145 {}
146 
147 
148 Foam::zone::zone
149 (
150  const zone& origZone,
151  labelList&& addr,
152  const label index
153 )
154 :
155  labelList(std::move(addr)),
156  name_(origZone.name()),
157  index_(index),
158  lookupMapPtr_(nullptr)
159 {}
160 
161 
162 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
163 
165 {
166  clearAddressing();
167 }
168 
169 
170 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
171 
172 Foam::label Foam::zone::localID(const label globalCellID) const
173 {
174  return lookupMap().lookup(globalCellID, -1);
175 }
176 
177 
179 {
180  deleteDemandDrivenData(lookupMapPtr_);
181 }
182 
183 
184 bool Foam::zone::checkDefinition(const label maxSize, const bool report) const
185 {
186  const labelList& addr = *this;
187 
188  bool hasError = false;
189 
190  // To check for duplicate entries
191  labelHashSet elems(size());
192 
193  for (const label idx : addr)
194  {
195  if (idx < 0 || idx >= maxSize)
196  {
197  hasError = true;
198 
199  if (report)
200  {
202  << "Zone " << name_
203  << " contains invalid index label " << idx << nl
204  << "Valid index labels are 0.."
205  << maxSize-1 << endl;
206  }
207  else
208  {
209  // w/o report - can stop checking now
210  break;
211  }
212  }
213  else if (!elems.insert(idx))
214  {
215  if (report)
216  {
218  << "Zone " << name_
219  << " contains duplicate index label " << idx << endl;
220  }
221  }
222  }
223 
224  return hasError;
225 }
226 
227 
228 void Foam::zone::write(Ostream& os) const
229 {
230  os << nl << name_
231  << nl << static_cast<const labelList&>(*this);
232 }
233 
234 
235 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
236 
238 {
239  zn.write(os);
240  os.check(FUNCTION_NAME);
241  return os;
242 }
243 
244 
245 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
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: lumpedPointController.H:69
Foam::zone::clearAddressing
virtual void clearAddressing()
Clear addressing.
Definition: zone.C:178
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::HashSet< label, Hash< label > >
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:42
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::zone::write
virtual void write(Ostream &os) const
Write.
Definition: zone.C:228
Foam::zone::lookupMap
const Map< label > & lookupMap() const
Return a reference to the look-up map.
Definition: zone.C:44
DebugInFunction
#define DebugInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:365
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:164
IOstream.H
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:281
Foam::dictionary::lookup
ITstream & lookup(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.C:424
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:144
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:381
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:359
Foam::nl
constexpr char nl
Definition: Ostream.H:385
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:181
Foam::zone::localID
label localID(const label globalID) const
Map storing the local index for every global index. Used to find.
Definition: zone.C:172
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
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:303
Foam::zone::checkDefinition
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.