topoSet.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) 2016-2021 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 "topoSet.H"
30#include "mapPolyMesh.H"
31#include "polyMesh.H"
32#include "boundBox.H"
33#include "Time.H"
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
38{
43
45 (
46 debug::debugSwitch("disallowGenericSets", 0)
47 );
48}
49
50
51// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
52
55(
56 const word& setType,
57 const polyMesh& mesh,
58 const word& name,
59 readOption r,
61)
62{
63 auto* ctorPtr = wordConstructorTable(setType);
64
65 if (!ctorPtr)
66 {
68 (
69 "set",
70 setType,
71 *wordConstructorTablePtr_
72 ) << exit(FatalError);
73 }
74
75 return autoPtr<topoSet>(ctorPtr(mesh, name, r, w));
76}
77
78
81(
82 const word& setType,
83 const polyMesh& mesh,
84 const word& name,
85 const label size,
87)
88{
89 auto* ctorPtr = sizeConstructorTable(setType);
90
91 if (!ctorPtr)
92 {
94 (
95 "set",
96 setType,
97 *sizeConstructorTablePtr_
98 ) << exit(FatalError);
99 }
100
101 return autoPtr<topoSet>(ctorPtr(mesh, name, size, w));
102}
103
104
107(
108 const word& setType,
109 const polyMesh& mesh,
110 const word& name,
111 const topoSet& set,
113)
114{
115 auto* ctorPtr = setConstructorTable(setType);
116
117 if (!ctorPtr)
118 {
120 (
121 "set",
122 setType,
123 *setConstructorTablePtr_
124 ) << exit(FatalError);
125 }
126
127 return autoPtr<topoSet>(ctorPtr(mesh, name, set, w));
128}
129
130
131// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
132
134(
135 const polyMesh& mesh,
136 const word& name
137)
138{
140}
141
142
143// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
144
145// Update stored cell numbers using map.
146// Do in two passes to prevent allocation if nothing changed.
148{
149 labelHashSet& labels = *this;
150
151 // Iterate over map to see if anything changed
152
153 bool changed = false;
154
155 for (const label oldId : labels)
156 {
157 if (oldId < 0 || oldId >= map.size())
158 {
160 << "Illegal content " << oldId << " of set:" << name()
161 << " of type " << type() << nl
162 << "Value should be between [0," << map.size() << ')'
163 << endl
164 << abort(FatalError);
165 }
166
167 const label newId = map[oldId];
168
169 if (newId != oldId)
170 {
171 changed = true;
172 #ifdef FULLDEBUG
173 continue; // Check all elements in FULLDEBUG mode
174 #endif
175 break;
176 }
177 }
178
179 if (!changed)
180 {
181 return;
182 }
183
184
185 // Relabel. Use second labelHashSet to prevent overlapping.
186
187 labelHashSet newLabels(2*labels.size());
188
189 for (const label oldId : labels)
190 {
191 const label newId = map[oldId];
192
193 if (newId >= 0)
194 {
195 newLabels.set(newId);
196 }
197 }
198
199 labels.transfer(newLabels);
200}
201
202
203void Foam::topoSet::check(const label maxSize)
204{
205 const labelHashSet& labels = *this;
206
207 for (const label oldId : labels)
208 {
209 if (oldId < 0 || oldId >= maxSize)
210 {
212 << "Illegal content " << oldId << " of set:" << name()
213 << " of type " << type() << nl
214 << "Value should be between [0," << maxSize << ')'
215 << endl
216 << abort(FatalError);
217 }
218 }
219}
220
221
222// Write maxElem elements, starting at iter. Updates iter and elemI.
224(
225 Ostream& os,
226 const label maxElem,
228 label& elemI
229) const
230{
231 label n = 0;
232
233 for (; (iter != cend()) && (n < maxElem); ++iter)
234 {
235 if (n && ((n % 10) == 0))
236 {
237 os << nl;
238 }
239 os << iter.key() << ' ';
240
241 ++n;
242 ++elemI;
243 }
244}
245
246
247// Write maxElem elements, starting at iter. Updates iter and elemI.
249(
250 Ostream& os,
251 const pointField& coords,
252 const label maxElem,
254 label& elemI
255) const
256{
257 label n = 0;
258
259 for (; (iter != cend()) && (n < maxElem); ++iter)
260 {
261 if (n && ((n % 3) == 0))
262 {
263 os << nl;
264 }
265 os << iter.key() << coords[iter.key()] << ' ';
266
267 ++n;
268 ++elemI;
269 }
270}
271
272
274(
275 Ostream& os,
276 const pointField& coords,
277 const label maxLen
278) const
279{
280 // Bounding box of contents.
281 boundBox bb(pointField(coords, toc()), true);
282
283 os << "Set bounding box: min = "
284 << bb.min() << " max = " << bb.max() << " metres." << nl << endl;
285
286 label n = 0;
287
288 topoSet::const_iterator iter = this->cbegin();
289
290 if (size() <= maxLen)
291 {
292 writeDebug(os, coords, maxLen, iter, n);
293 }
294 else
295 {
296 label halfLen = maxLen/2;
297
298 os << "Size larger than " << maxLen << ". Printing first and last "
299 << halfLen << " elements:" << nl << endl;
300
301 writeDebug(os, coords, halfLen, iter, n);
302
303 os << nl << " .." << nl << endl;
304
305 for (; n < size() - halfLen; ++n)
306 {
307 ++iter;
308 }
309
310 writeDebug(os, coords, halfLen, iter, n);
311 }
312}
313
314
315// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
316
318(
319 const polyMesh& mesh,
320 const word& name,
321 readOption r,
323)
324{
326 (
327 name,
329 (
334 ),
336 mesh,
337 r,
338 w
339 );
340
341 if (!io.typeHeaderOk<topoSet>(false) && disallowGenericSets != 0)
342 {
343 DebugInfo<< "Setting no read for set " << name << endl;
345 }
346
347 return io;
348}
349
350
352(
353 const Time& runTime,
354 const word& name,
355 readOption r,
357)
358{
359 return IOobject
360 (
361 name,
363 (
368 (
370 "faces",
372 )
373 ),
375 runTime,
376 r,
377 w
378 );
379}
380
381
382// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
383
384Foam::topoSet::topoSet(const IOobject& obj, const word& wantedType)
385:
386 regIOobject(obj)
387{
388 if
389 (
393 )
394 {
395 if (readStream(wantedType).good())
396 {
397 readStream(wantedType) >> static_cast<labelHashSet&>(*this);
398
399 close();
400 }
401 }
402}
403
404
406(
407 const polyMesh& mesh,
408 const word& wantedType,
409 const word& name,
410 readOption r,
412)
413:
414 regIOobject(findIOobject(mesh, name, r, w))
415{
416 if
417 (
421 )
422 {
423 if (readStream(wantedType).good())
424 {
425 readStream(wantedType) >> static_cast<labelHashSet&>(*this);
426
427 close();
428 }
429 }
430}
431
432
434(
435 const polyMesh& mesh,
436 const word& name,
437 const label size,
439)
440:
441 regIOobject(findIOobject(mesh, name, IOobject::NO_READ, w)),
442 labelHashSet(size)
443{}
444
445
447(
448 const polyMesh& mesh,
449 const word& name,
450 const labelHashSet& labels,
452)
453:
454 regIOobject(findIOobject(mesh, name, IOobject::NO_READ, w)),
455 labelHashSet(labels)
456{}
457
458
460(
461 const polyMesh& mesh,
462 const word& name,
463 labelHashSet&& labels,
465)
466:
467 regIOobject(findIOobject(mesh, name, IOobject::NO_READ, w)),
468 labelHashSet(std::move(labels))
469{}
470
471
473(
474 const polyMesh& mesh,
475 const word& name,
476 const labelUList& labels,
478)
479:
480 regIOobject(findIOobject(mesh, name, IOobject::NO_READ, w)),
481 labelHashSet(labels)
482{}
483
484
485Foam::topoSet::topoSet(const IOobject& io, const label size)
486:
488 labelHashSet(size)
489{}
490
491
493:
495 labelHashSet(labels)
496{}
497
498
500:
502 labelHashSet(std::move(labels))
503{}
504
505
506// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
507
508bool Foam::topoSet::found(const label id) const
509{
510 return static_cast<const labelHashSet&>(*this).found(id);
511}
512
513
514bool Foam::topoSet::set(const label id)
515{
516 return static_cast<labelHashSet&>(*this).set(id);
517}
518
519
520bool Foam::topoSet::unset(const label id)
521{
522 return static_cast<labelHashSet&>(*this).unset(id);
523}
524
525
527{
528 static_cast<labelHashSet&>(*this).set(labels);
529}
530
531
533{
534 static_cast<labelHashSet&>(*this).unset(labels);
535}
536
537
538void Foam::topoSet::invert(const label maxLen)
539{
540 // Retain a copy of the original (current) set.
541 labelHashSet original
542 (
543 std::move(static_cast<labelHashSet&>(*this))
544 );
545
546 clear(); // Maybe don't trust the previous move operation
547 resize(2*max(64, (maxLen - original.size())));
548
549 for (label id=0; id < maxLen; ++id)
550 {
551 if (!original.found(id))
552 {
553 this->set(id);
554 }
555 }
556}
557
558
560{
561 // Only retain entries found in both sets
562 static_cast<labelHashSet&>(*this) &= set;
563}
564
565
567{
568 // Add entries to the set
569 static_cast<labelHashSet&>(*this) |= set;
570}
571
572
574{
575 // Subtract entries from the set
576 static_cast<labelHashSet&>(*this) -= set;
577}
578
579
581{
582 this->subtractSet(set);
583}
584
585
587{
589}
590
591
592void Foam::topoSet::writeDebug(Ostream& os, const label maxLen) const
593{
594 label n = 0;
595
596 topoSet::const_iterator iter = this->cbegin();
597
598 if (size() <= maxLen)
599 {
600 writeDebug(os, maxLen, iter, n);
601 }
602 else
603 {
604 label halfLen = maxLen/2;
605
606 os << "Size larger than " << maxLen << ". Printing first and last "
607 << halfLen << " elements:" << nl << endl;
608
609 writeDebug(os, halfLen, iter, n);
610
611 os << nl << " .." << nl << endl;
612
613 for (; n < size() - halfLen; ++n)
614 {
615 ++iter;
616 }
617
618 writeDebug(os, halfLen, iter, n);
619 }
620}
621
622
624{
625 return (os << *this).good();
626}
627
628
630{
632}
633
634
636{
638 (
639 "dummy",
641 mesh.meshSubDir/"sets",
642 mesh
643 );
644 fileName setsDir(io.path());
645
646 if (debug) DebugVar(setsDir);
647
648 if (isDir(setsDir))
649 {
650 rmDir(setsDir);
651 }
652}
653
654
655// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
656
658{
660}
661
662
663// ************************************************************************* //
bool found
label n
virtual fileName localPath() const
Output directory.
void invert()
Return the matrix inverse into itself if no elem is equal to zero.
bool unset(const Key &key)
Unset the specified key - same as erase.
Definition: HashSet.H:204
bool set(const Key &key)
Same as insert (no value to overwrite)
Definition: HashSet.H:197
void operator=(const this_type &rhs)
Copy assign.
Definition: HashSet.H:332
bool found(const Key &key) const
Return true if hashed entry is found in table.
Definition: HashTableI.H:100
void transfer(HashTable< T, Key, Hash > &rhs)
Transfer contents into this table.
Definition: HashTable.C:719
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (uses typeFilePath to find file) and check its info.
bool good() const noexcept
Did last readHeader() succeed?
Definition: IOobjectI.H:222
writeOption
Enumeration defining the write options.
Definition: IOobject.H:186
fileName path() const
The complete path.
Definition: IOobject.C:524
readOption
Enumeration defining the read options.
Definition: IOobject.H:177
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:180
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Definition: Time.C:797
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A const_iterator for iterating across on values.
Definition: bitSet.H:505
A bounding box defined in terms of min/max extrema points.
Definition: boundBox.H:64
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:91
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:97
void removeFiles() const
Remove all files from mesh instance()
Definition: faMesh.C:718
A class for handling file names.
Definition: fileName.H:76
void sync()
Do all: synchronise all IOFields and objectRegistry.
Definition: syncObjects.C:70
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:290
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:162
void updateMesh()
Update for new mesh topology.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:866
virtual const fileName & dbDir() const
Override the objectRegistry dbDir for a single-region case.
Definition: polyMesh.C:837
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:324
void writeDebug() const
Debug write.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:76
void close()
Close Istream.
Istream & readStream(const word &, const bool valid=true)
Return Istream and check object type against that given.
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:438
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:67
virtual bool writeData(Ostream &) const
Write contents.
Definition: topoSet.C:623
static IOobject findIOobject(const polyMesh &mesh, const word &name, readOption r=MUST_READ, writeOption w=NO_WRITE)
Find IOobject in the polyMesh/sets (used as constructor helper)
Definition: topoSet.C:318
void operator=(const topoSet &)
Copy labelHashSet part only.
Definition: topoSet.C:657
virtual void subtractSet(const topoSet &set)
Subtract elements present in set.
Definition: topoSet.C:573
virtual void deleteSet(const topoSet &set)
Deprecated(2018-10) subtract elements present in set.
Definition: topoSet.C:580
static int disallowGenericSets
Debug switch to disallow the use of generic sets.
Definition: topoSet.H:120
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: topoSet.C:566
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: topoSet.C:559
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition: topoSet.C:147
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoSet.C:203
static const triad unset
Definition: triad.H:97
bool set() const
Are all the vector set.
Definition: triadI.H:76
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
patchWriters resize(patchIds.size())
patchWriters clear()
dynamicFvMesh & mesh
engineTime & runTime
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:457
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
#define DebugInfo
Report an information message using Foam::Info.
#define DebugVar(var)
Report a variable name and value.
int debugSwitch(const char *name, const int deflt=0)
Lookup debug switch or add default value.
Definition: debug.C:225
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
bool rmDir(const fileName &directory, const bool silent=false)
Remove a directory and its contents (optionally silencing warnings)
Definition: MSwindows.C:1036
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
#define defineRunTimeSelectionTable(baseType, argNames)
Define run-time selection table.