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-------------------------------------------------------------------------------
11License
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
37namespace Foam
38{
40}
41
42
43// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44
46:
48 labelList(),
49 lookupMapPtr_(nullptr)
50{}
51
52
53Foam::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 zoneIdentifier(name, dict, index),
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
153
154Foam::label Foam::zone::localID(const label globalID) const
155{
156 return lookupMap().lookup(globalID, -1);
157}
158
159
161{
162 deleteDemandDrivenData(lookupMapPtr_);
163}
164
165
166bool 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
211{
212 os << nl << this->name()
213 << nl << static_cast<const labelList&>(*this);
214}
215
216
217// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
218
220{
221 zn.write(os);
223 return os;
224}
225
226
227// ************************************************************************* //
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
A HashTable to objects of type <T> with a label key.
Definition: Map.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
virtual bool write()
Write the output fields.
A class for handling words, derived from Foam::string.
Definition: word.H:68
Identifies a mesh zone by name and index, with optional physical type and group information.
Base class for mesh zones.
Definition: zone.H:67
const Map< label > & lookupMap() const
Demand-driven: the look-up map from global to local id.
Definition: zone.C:133
virtual ~zone()
Destructor.
Definition: zone.C:125
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.
label localID(const label globalID) const
Lookup local address in zone for given global index.
Definition: zone.C:154
virtual void write(Ostream &os) const
Write.
Definition: zone.C:210
zone()
Default construct.
Definition: zone.C:45
virtual void clearAddressing()
Clear addressing.
Definition: zone.C:160
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
Template functions to aid in the implementation of demand driven data.
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
#define FUNCTION_NAME
#define DebugInFunction
Report an information message using Foam::Info.
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
void deleteDemandDrivenData(DataPtr &dataPtr)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333