Loading...
Searching...
No Matches
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-2025 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
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
36namespace Foam
39}
40
41
42// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
47{}
48
50Foam::zone::zone(const word& name, const label index)
51:
53{}
54
55
57(
58 const word& name,
59 const labelUList& addr,
60 const label index
61)
64{
66}
67
68
70(
71 const word& name,
72 labelList&& addr,
73 const label index
74)
77{
79}
80
81
83(
84 const word& name,
85 const dictionary& dict,
86 const word& labelsName,
87 const label index
88)
89:
90 zoneIdentifier(name, dict, index)
91{
92 if (!labelsName.empty())
93 {
94 dict.readEntry<labelList>(labelsName, *this, keyType::LITERAL);
95 }
96}
97
98
100(
101 const zone& originalZone,
102 const label newIndex
104:
105 zoneIdentifier(originalZone, newIndex),
106 labelList(originalZone)
107{}
108
109
111(
112 const zone& originalZone,
113 const labelUList& addr,
114 const label newIndex
116:
117 zoneIdentifier(originalZone, newIndex),
118 labelList(addr)
119{}
120
121
123(
124 const zone& originalZone,
125 labelList&& addr,
126 const label newIndex
127)
128:
129 zoneIdentifier(originalZone, newIndex),
130 labelList(std::move(addr))
131{}
132
133
134// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135
137{
138 if (!lookupMapPtr_)
139 {
140 const labelList& addr = *this;
141
142 lookupMapPtr_.reset(new Map<label>());
143 auto& map = *lookupMapPtr_;
144 map.reserve(addr.size());
145
146 for (const label id : addr)
147 {
148 map.insert(id, map.size());
150 }
151
152 return *lookupMapPtr_;
153}
154
155
156Foam::label Foam::zone::localID(const label globalID) const
157{
158 return lookupMap().lookup(globalID, -1);
159}
160
161
162// void Foam::zone::sort()
163// {
164// clearAddressing();
165// Foam::sort(*this);
166// }
167
170{
171 lookupMapPtr_.reset(nullptr);
172}
173
176{
177 static_cast<labelList&>(*this).clear();
178}
179
180
181bool Foam::zone::checkDefinition(const label maxSize, const bool report) const
182{
183 const labelList& addr = *this;
184
185 bool hasError = false;
186
187 // To check for duplicate entries
188 labelHashSet elems;
189 elems.reserve(addr.size());
190
191 for (const label id : addr)
192 {
193 if (id < 0 || id >= maxSize)
194 {
195 hasError = true;
196
197 if (report)
198 {
200 << "Zone " << this->name()
201 << " contains invalid index label " << id << nl
202 << "Valid index labels are 0.." << (maxSize-1) << endl;
203 }
204 else
205 {
206 // w/o report - can stop checking now
207 break;
208 }
209 }
210 else if (!elems.insert(id))
211 {
212 if (report)
213 {
215 << "Zone " << this->name()
216 << " contains duplicate index label " << id << endl;
217 }
219 }
220
221 return hasError;
222}
223
224
225void Foam::zone::write(Ostream& os) const
226{
227 os.writeEntry("type", type());
229}
230
231
232// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
233
234Foam::Ostream& Foam::operator<<(Ostream& os, const zone& zn)
235{
236 os << nl << zn.name()
238
239 os.check(FUNCTION_NAME);
240 return os;
241}
242
243
244// ************************************************************************* //
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
const T & lookup(const Key &key, const T &deflt) const
Return hashed entry if it exists, or return the given default.
Definition HashTableI.H:222
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
Definition HashTable.C:729
void transfer(List< label > &list)
void operator=(const UList< label > &list)
A HashTable to objects of type <T> with a label key.
Definition Map.H:54
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class for handling words, derived from Foam::string.
Definition word.H:66
Identifies a mesh zone by name and index, with optional physical type and group information.
zoneIdentifier(const zoneIdentifier &)=default
Copy construct.
void write(Ostream &os) const
Write (physicalType, inGroups) dictionary entries (without surrounding braces).
label index() const noexcept
The index of this zone in the zone list.
const word & name() const noexcept
The zone name.
Base class for mesh zones.
Definition zone.H:63
const Map< label > & lookupMap() const
Demand-driven: the look-up map from global to local id.
Definition zone.C:129
virtual bool checkDefinition(const bool report=false) const =0
Check zone definition. Return true if in error.
virtual void clearPrimitives()
Clear primitive addressing.
Definition zone.C:168
label localID(const label globalID) const
Lookup local address in zone for given global index.
Definition zone.C:149
virtual void write(Ostream &os) const
Write (dictionary entries).
Definition zone.C:218
zone()
Default construct: empty zone with name="", index=0.
Definition zone.C:37
virtual void clearAddressing()
Clear addressing (remove lookup maps and other auxiliary information).
Definition zone.C:162
#define defineTypeName(Type)
Define the typeName.
Definition className.H:113
OBJstream os(runTime.globalPath()/outputName)
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
#define FUNCTION_NAME
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition List.H:62
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition HashSet.H:85
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
UList< label > labelUList
A UList of labels.
Definition UList.H:75
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict