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-2021 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 {
39  defineTypeNameAndDebug(zone, 0);
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 :
55  zoneIdentifier(name, index),
56  labelList(),
57  lookupMapPtr_(nullptr)
58 {}
59 
60 
62 (
63  const word& name,
64  const labelUList& addr,
65  const label index
66 )
67 :
68  zoneIdentifier(name, index),
69  labelList(addr),
70  lookupMapPtr_(nullptr)
71 {}
72 
73 
75 (
76  const word& name,
77  labelList&& addr,
78  const label index
79 )
80 :
81  zoneIdentifier(name, index),
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 :
95  zone(name, dict.get<labelList>(labelsName), index)
96 {}
97 
98 
100 (
101  const zone& origZone,
102  const labelUList& addr,
103  const label index
104 )
105 :
106  zone(origZone.name(), addr, index)
107 {}
108 
109 
111 (
112  const zone& origZone,
113  labelList&& addr,
114  const label index
115 )
116 :
117  zone(origZone.name(), std::move(addr), index)
118 {}
119 
120 
121 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
122 
124 {
125  clearAddressing();
126 }
127 
128 
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130 
132 {
133  if (!lookupMapPtr_)
134  {
135  DebugInFunction << "Calculating lookup map" << endl;
136 
137  const labelList& addr = *this;
138 
139  lookupMapPtr_ = new Map<label>(2*addr.size());
140  auto& lm = *lookupMapPtr_;
141 
142  forAll(addr, i)
143  {
144  lm.insert(addr[i], i);
145  }
146  }
147 
148  return *lookupMapPtr_;
149 }
150 
151 
152 Foam::label Foam::zone::localID(const label globalID) const
153 {
154  return lookupMap().lookup(globalID, -1);
155 }
156 
157 
159 {
160  deleteDemandDrivenData(lookupMapPtr_);
161 }
162 
163 
164 bool Foam::zone::checkDefinition(const label maxSize, const bool report) const
165 {
166  const labelList& addr = *this;
167 
168  bool hasError = false;
169 
170  // To check for duplicate entries
171  labelHashSet elems(size());
172 
173  for (const label idx : addr)
174  {
175  if (idx < 0 || idx >= maxSize)
176  {
177  hasError = true;
178 
179  if (report)
180  {
182  << "Zone " << this->name()
183  << " contains invalid index label " << idx << nl
184  << "Valid index labels are 0.."
185  << maxSize-1 << endl;
186  }
187  else
188  {
189  // w/o report - can stop checking now
190  break;
191  }
192  }
193  else if (!elems.insert(idx))
194  {
195  if (report)
196  {
198  << "Zone " << this->name()
199  << " contains duplicate index label " << idx << endl;
200  }
201  }
202  }
203 
204  return hasError;
205 }
206 
207 
209 {
210  os << nl << this->name()
211  << nl << static_cast<const labelList&>(*this);
212 }
213 
214 
215 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
216 
218 {
219  zn.write(os);
221  return os;
222 }
223 
224 
225 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
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:158
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
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:208
Foam::zone::lookupMap
const Map< label > & lookupMap() const
Demand-driven: the look-up map from global to local id.
Definition: zone.C:131
DebugInFunction
#define DebugInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:388
Foam::zone::~zone
virtual ~zone()
Destructor.
Definition: zone.C:123
IOstream.H
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:306
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:58
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::zoneIdentifier::name
const word & name() const noexcept
The zone name.
Definition: zoneIdentifier.H:123
Foam::List< label >
Foam::UList< label >
dictionary.H
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
Foam::zone::localID
label localID(const label globalID) const
Lookup local address in zone for given global index.
Definition: zone.C:152
Foam::zoneIdentifier
Identifies a mesh zone by name and index, with optional physical type and group information.
Definition: zoneIdentifier.H:57
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::zone::zone
zone()
Default construct.
Definition: zone.C:45
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::zone::checkDefinition
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.