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-2022 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 "dictionary.H"
31 #include "HashSet.H"
32 #include "IOstream.H"
33 #include "demandDrivenData.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
46 :
48  labelList(),
49  lookupMapPtr_(nullptr)
50 {}
51 
52 
53 Foam::zone::zone(const word& name, const label index)
54 :
56  labelList(),
57  lookupMapPtr_(nullptr)
58 {}
59 
60 
62 (
63  const word& name,
64  const labelUList& addr,
65  const label index
66 )
67 :
69  labelList(addr),
70  lookupMapPtr_(nullptr)
71 {}
72 
73 
75 (
76  const word& name,
77  labelList&& addr,
78  const label index
79 )
80 :
82  labelList(std::move(addr)),
83  lookupMapPtr_(nullptr)
84 {}
85 
86 
88 (
89  const word& name,
90  const dictionary& dict,
91  const word& labelsName,
92  const label index
93 )
94 :
96  labelList(dict.get<labelList>(labelsName)),
97  lookupMapPtr_(nullptr)
98 {}
99 
100 
102 (
103  const zone& origZone,
104  const labelUList& addr,
105  const label index
106 )
107 :
108  zone(origZone.name(), addr, index)
109 {}
110 
111 
113 (
114  const zone& origZone,
115  labelList&& addr,
116  const label index
117 )
118 :
119  zone(origZone.name(), std::move(addr), index)
120 {}
121 
122 
123 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
124 
126 {
127  clearAddressing();
128 }
129 
130 
131 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
132 
134 {
135  if (!lookupMapPtr_)
136  {
137  DebugInFunction << "Calculating lookup map" << endl;
138 
139  const labelList& addr = *this;
140 
141  lookupMapPtr_ = new Map<label>(2*addr.size());
142  auto& lm = *lookupMapPtr_;
143 
144  forAll(addr, i)
145  {
146  lm.insert(addr[i], i);
147  }
148  }
149 
150  return *lookupMapPtr_;
151 }
152 
154 Foam::label Foam::zone::localID(const label globalID) const
155 {
156  return lookupMap().lookup(globalID, -1);
157 }
158 
161 {
162  deleteDemandDrivenData(lookupMapPtr_);
163 }
164 
165 
166 bool Foam::zone::checkDefinition(const label maxSize, const bool report) const
167 {
168  const labelList& addr = *this;
169 
170  bool hasError = false;
171 
172  // To check for duplicate entries
173  labelHashSet elems(size());
174 
175  for (const label idx : addr)
176  {
177  if (idx < 0 || idx >= maxSize)
178  {
179  hasError = true;
180 
181  if (report)
182  {
184  << "Zone " << this->name()
185  << " contains invalid index label " << idx << nl
186  << "Valid index labels are 0.."
187  << maxSize-1 << endl;
188  }
189  else
190  {
191  // w/o report - can stop checking now
192  break;
193  }
194  }
195  else if (!elems.insert(idx))
196  {
197  if (report)
198  {
200  << "Zone " << this->name()
201  << " contains duplicate index label " << idx << endl;
202  }
203  }
204  }
205 
206  return hasError;
207 }
208 
209 
210 void Foam::zone::write(Ostream& os) const
211 {
212  os << nl << this->name()
213  << nl << static_cast<const labelList&>(*this);
214 }
215 
216 
217 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
218 
219 Foam::Ostream& Foam::operator<<(Ostream& os, const zone& zn)
220 {
221  zn.write(os);
223  return os;
224 }
225 
226 
227 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
zone()
Default construct.
Definition: zone.C:38
virtual void clearAddressing()
Clear addressing.
Definition: zone.C:153
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
label localID(const label globalID) const
Lookup local address in zone for given global index.
Definition: zone.C:147
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:414
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:152
virtual ~zone()
Destructor.
Definition: zone.C:118
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
Base class for mesh zones.
Definition: zone.H:59
A class for handling words, derived from Foam::string.
Definition: word.H:63
#define DebugInFunction
Report an information message using Foam::Info.
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
defineTypeNameAndDebug(combustionModel, 0)
virtual void write(Ostream &os) const
Write.
Definition: zone.C:203
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
Template functions to aid in the implementation of demand driven data.
Identifies a mesh zone by name and index, with optional physical type and group information.
#define WarningInFunction
Report a warning using Foam::Warning.
const Map< label > & lookupMap() const
Demand-driven: the look-up map from global to local id.
Definition: zone.C:126
void deleteDemandDrivenData(DataPtr &dataPtr)
Namespace for OpenFOAM.
A HashTable to objects of type <T> with a label key.