faceZoneSet.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-2017 OpenFOAM Foundation
9  Copyright (C) 2018-2019 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 "faceZoneSet.H"
30 #include "mapPolyMesh.H"
31 #include "polyMesh.H"
32 #include "setToFaceZone.H"
33 #include "setsToFaceZone.H"
34 #include "syncTools.H"
35 
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42  defineTypeNameAndDebug(faceZoneSet, 0);
43  addToRunTimeSelectionTable(topoSet, faceZoneSet, word);
44  addToRunTimeSelectionTable(topoSet, faceZoneSet, size);
45  addToRunTimeSelectionTable(topoSet, faceZoneSet, set);
46 }
47 
48 
49 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
50 
52 {
53  labelList order(sortedOrder(addressing_));
54  addressing_ = labelUIndList(addressing_, order)();
55  flipMap_ = boolUIndList(flipMap_, order)();
56 
58  faceSet::resize(2*addressing_.size());
59  faceSet::set(addressing_);
60 }
61 
62 
63 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
64 
66 (
67  const polyMesh& mesh,
68  const word& name,
69  readOption r,
70  writeOption w
71 )
72 :
73  faceSet(mesh, name, 1024), // do not read faceSet
74  mesh_(mesh),
75  addressing_(),
76  flipMap_()
77 {
78  const faceZoneMesh& faceZones = mesh.faceZones();
79  label zoneID = faceZones.findZoneID(name);
80 
81  if
82  (
83  (r == IOobject::MUST_READ)
85  || (r == IOobject::READ_IF_PRESENT && zoneID != -1)
86  )
87  {
88  const faceZone& fz = faceZones[zoneID];
89  addressing_ = fz;
90  flipMap_ = fz.flipMap();
91  }
92 
93  updateSet();
94 
95  check(mesh.nFaces());
96 }
97 
98 
100 (
101  const polyMesh& mesh,
102  const word& name,
103  const label size,
104  writeOption w
105 )
106 :
107  faceSet(mesh, name, size, w),
108  mesh_(mesh),
109  addressing_(),
110  flipMap_()
111 {
112  updateSet();
113 }
114 
115 
117 (
118  const polyMesh& mesh,
119  const word& name,
120  const topoSet& set,
121  writeOption w
122 )
123 :
124  faceSet(mesh, name, set.size(), w),
125  mesh_(mesh),
126  addressing_(refCast<const faceZoneSet>(set).addressing()),
127  flipMap_(refCast<const faceZoneSet>(set).flipMap())
128 {
129  updateSet();
130 }
131 
132 
133 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
134 
136 {
137  // Count
138  label n = 0;
139 
140  for (label facei = 0; facei < maxLen; ++facei)
141  {
142  if (!found(facei))
143  {
144  ++n;
145  }
146  }
147 
148  // Fill
149  addressing_.setSize(n);
150  flipMap_.setSize(n);
151  n = 0;
152 
153  for (label facei = 0; facei < maxLen; ++facei)
154  {
155  if (!found(facei))
156  {
157  addressing_[n] = facei;
158  flipMap_[n] = false; //? or true?
159  ++n;
160  }
161  }
162  updateSet();
163 }
164 
165 
167 {
168  label nConflict = 0;
169 
170  DynamicList<label> newAddressing(addressing_.size());
171  DynamicList<bool> newFlipMap(flipMap_.size());
172 
173  Map<label> faceToIndex(addressing_.size());
174  forAll(addressing_, i)
175  {
176  faceToIndex.insert(addressing_[i], i);
177  }
178 
179  const faceZoneSet& zoneSet = refCast<const faceZoneSet>(set);
180 
181  forAll(zoneSet.addressing(), i)
182  {
183  const label facei = zoneSet.addressing()[i];
184 
185  const auto iter = faceToIndex.cfind(facei);
186 
187  if (iter.found())
188  {
189  const label index = *iter;
190 
191  if (zoneSet.flipMap()[i] != flipMap_[index])
192  {
193  ++nConflict;
194  }
195  newAddressing.append(facei);
196  newFlipMap.append(flipMap_[index]);
197  }
198  }
199 
200  if (nConflict > 0)
201  {
203  << "subset : there are " << nConflict
204  << " faces with different orientation in faceZonesSets "
205  << name() << " and " << set.name() << endl;
206  }
207 
208  addressing_.transfer(newAddressing);
209  flipMap_.transfer(newFlipMap);
210  updateSet();
211 }
212 
213 
215 {
216  label nConflict = 0;
217 
218  DynamicList<label> newAddressing(addressing_);
219  DynamicList<bool> newFlipMap(flipMap_);
220 
221  Map<label> faceToIndex(addressing_.size());
222  forAll(addressing_, i)
223  {
224  faceToIndex.insert(addressing_[i], i);
225  }
226 
227  const faceZoneSet& zoneSet = refCast<const faceZoneSet>(set);
228 
229  forAll(zoneSet.addressing(), i)
230  {
231  label facei = zoneSet.addressing()[i];
232 
233  const auto iter = faceToIndex.cfind(facei);
234 
235  if (iter.found())
236  {
237  const label index = *iter;
238 
239  if (zoneSet.flipMap()[i] != flipMap_[index])
240  {
241  ++nConflict;
242  }
243  }
244  else
245  {
246  newAddressing.append(facei);
247  newFlipMap.append(zoneSet.flipMap()[i]);
248  }
249  }
250 
251  if (nConflict > 0)
252  {
254  << "addSet : there are " << nConflict
255  << " faces with different orientation in faceZonesSets "
256  << name() << " and " << set.name() << endl;
257  }
258 
259  addressing_.transfer(newAddressing);
260  flipMap_.transfer(newFlipMap);
261  updateSet();
262 }
263 
264 
266 {
267  label nConflict = 0;
268 
269  DynamicList<label> newAddressing(addressing_.size());
270  DynamicList<bool> newFlipMap(flipMap_.size());
271 
272  const faceZoneSet& zoneSet = refCast<const faceZoneSet>(set);
273 
274  Map<label> faceToIndex(zoneSet.addressing().size());
275  forAll(zoneSet.addressing(), i)
276  {
277  faceToIndex.insert(zoneSet.addressing()[i], i);
278  }
279 
280  forAll(addressing_, i)
281  {
282  const label facei = addressing_[i];
283 
284  const auto iter = faceToIndex.cfind(facei);
285 
286  if (iter.found())
287  {
288  const label index = *iter;
289 
290  if (zoneSet.flipMap()[index] != flipMap_[i])
291  {
292  ++nConflict;
293  }
294  }
295  else
296  {
297  // Not found in zoneSet so add
298  newAddressing.append(facei);
299  newFlipMap.append(zoneSet.flipMap()[i]);
300  }
301  }
302 
303  if (nConflict > 0)
304  {
306  << "subtractSet : there are " << nConflict
307  << " faces with different orientation in faceZonesSets "
308  << name() << " and " << set.name() << endl;
309  }
310 
311  addressing_.transfer(newAddressing);
312  flipMap_.transfer(newFlipMap);
313  updateSet();
314 }
315 
316 
318 {
319  // Make sure that the faceZone is consistent with the faceSet
320  {
321  const labelHashSet zoneSet(addressing_);
322 
323  // Get elements that are in zone but not faceSet
324  labelHashSet badSet(zoneSet);
325  badSet -= *this;
326 
327  // Add elements that are in faceSet but not in zone
328  labelHashSet fSet(*this);
329  fSet -= zoneSet;
330 
331  badSet += fSet;
332 
333  label nBad = returnReduce(badSet.size(), sumOp<label>());
334 
335  if (nBad)
336  {
337  WarningInFunction << "Detected " << nBad
338  << " faces that are in the faceZone but not"
339  << " in the faceSet or vice versa."
340  << " The faceZoneSet should only be manipulated"
341  << " using " << setsToFaceZone::typeName
342  << " or " << setToFaceZone::typeName << endl;
343  }
344  }
345 
346 
347  // Make sure that on coupled faces orientation is opposite. Pushes
348  // master orientation to slave in case of conflict.
349 
350 
351  // 0 : not in faceZone
352  // 1 : in faceZone and unflipped
353  //-1 : in faceZone and flipped
354  const label UNFLIPPED = 1;
355  const label FLIPPED = -1;
356  labelList myZoneFace(mesh.nBoundaryFaces(), Zero);
357 
358  forAll(addressing_, i)
359  {
360  const label bFacei = addressing_[i]-mesh.nInternalFaces();
361 
362  if (bFacei >= 0)
363  {
364  if (flipMap_[i])
365  {
366  myZoneFace[bFacei] = FLIPPED;
367  }
368  else
369  {
370  myZoneFace[bFacei] = UNFLIPPED;
371  }
372  }
373  }
374 
375  labelList neiZoneFace(myZoneFace);
377 
378 
379  const bitSet isMasterFace(syncTools::getMasterFaces(mesh));
380 
381 
382  // Rebuild faceZone addressing and flipMap
383  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
384 
385  DynamicList<label> newAddressing(addressing_.size());
386  DynamicList<bool> newFlipMap(flipMap_.size());
387 
388  forAll(addressing_, i)
389  {
390  const label facei = addressing_[i];
391  if (facei < mesh.nInternalFaces())
392  {
393  newAddressing.append(facei);
394  newFlipMap.append(flipMap_[i]);
395  }
396  }
397 
398  for (label facei = mesh.nInternalFaces(); facei < mesh.nFaces(); facei++)
399  {
400  label myStat = myZoneFace[facei-mesh.nInternalFaces()];
401  label neiStat = neiZoneFace[facei-mesh.nInternalFaces()];
402 
403  if (myStat == 0)
404  {
405  if (neiStat == UNFLIPPED)
406  {
407  // Neighbour is unflipped so I am flipped
408  newAddressing.append(facei);
409  newFlipMap.append(true);
410  }
411  else if (neiStat == FLIPPED)
412  {
413  newAddressing.append(facei);
414  newFlipMap.append(false);
415  }
416  }
417  else
418  {
419  if (myStat == neiStat)
420  {
421  // Conflict. masterFace wins
422  newAddressing.append(facei);
423  if (isMasterFace[facei])
424  {
425  newFlipMap.append(myStat == FLIPPED);
426  }
427  else
428  {
429  newFlipMap.append(neiStat == UNFLIPPED);
430  }
431  }
432  else
433  {
434  newAddressing.append(facei);
435  newFlipMap.append(myStat == FLIPPED);
436  }
437  }
438  }
439 
440  addressing_.transfer(newAddressing);
441  flipMap_.transfer(newFlipMap);
442  updateSet();
443 }
444 
445 
447 {
448  return mesh.nFaces();
449 }
450 
451 
453 (
457  const bool valid
458 ) const
459 {
460  // Write shadow faceSet
461  word oldTypeName = typeName;
462  const_cast<word&>(type()) = faceSet::typeName;
463  bool ok = faceSet::writeObject(s, v, c, valid);
464  const_cast<word&>(type()) = oldTypeName;
465 
466  // Modify faceZone
467  faceZoneMesh& faceZones = const_cast<polyMesh&>(mesh_).faceZones();
468  label zoneID = faceZones.findZoneID(name());
469 
470  if (zoneID == -1)
471  {
472  zoneID = faceZones.size();
473 
474  faceZones.setSize(zoneID+1);
475  faceZones.set
476  (
477  zoneID,
478  new faceZone
479  (
480  name(),
481  addressing_,
482  flipMap_,
483  zoneID,
484  faceZones
485  )
486  );
487  }
488  else
489  {
490  faceZones[zoneID].resetAddressing(addressing_, flipMap_);
491  }
492  faceZones.clearAddressing();
493 
494  return ok && faceZones.write(valid);
495 }
496 
497 
499 {
500  // faceZone
501  labelList newAddressing(addressing_.size());
502  boolList newFlipMap(flipMap_.size(), false);
503 
504  label n = 0;
505  forAll(addressing_, i)
506  {
507  label facei = addressing_[i];
508  label newFacei = morphMap.reverseFaceMap()[facei];
509  if (newFacei >= 0)
510  {
511  newAddressing[n] = newFacei;
512  newFlipMap[n] = flipMap_[i];
513  n++;
514  }
515  }
516  newAddressing.setSize(n);
517  newFlipMap.setSize(n);
518 
519  addressing_.transfer(newAddressing);
520  flipMap_.transfer(newFlipMap);
521 
522  updateSet();
523 }
524 
525 
527 (
528  Ostream& os,
529  const primitiveMesh& mesh,
530  const label maxLen
531 ) const
532 {
533  faceSet::writeDebug(os, mesh, maxLen);
534 }
535 
536 
537 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::faceZoneSet::addressing
const labelList & addressing() const
Definition: faceZoneSet.H:107
Foam::regIOobject::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
Definition: regIOobjectWrite.C:39
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:46
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:64
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::DynamicList< label >
Foam::primitiveMesh::nInternalFaces
label nInternalFaces() const
Number of internal faces.
Definition: primitiveMeshI.H:78
faceZoneSet.H
Foam::faceZoneSet::updateSet
void updateSet()
Sort addressing and make faceSet part consistent with addressing.
Definition: faceZoneSet.C:51
Foam::primitiveMesh::nFaces
label nFaces() const
Number of mesh faces.
Definition: primitiveMeshI.H:90
mapPolyMesh.H
Foam::faceZoneSet::maxSize
virtual label maxSize(const polyMesh &mesh) const
Return max index+1.
Definition: faceZoneSet.C:446
Foam::Map< label >
Foam::faceZoneSet::updateMesh
virtual void updateMesh(const mapPolyMesh &morphMap)
Update any stored data for new labels.
Definition: faceZoneSet.C:498
Foam::syncTools::swapBoundaryFaceList
static void swapBoundaryFaceList(const polyMesh &mesh, UList< T > &faceValues)
Swap coupled boundary face values. Uses eqOp.
Definition: syncTools.H:439
Foam::faceSet
A list of face labels.
Definition: faceSet.H:51
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:182
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::syncTools::getMasterFaces
static bitSet getMasterFaces(const polyMesh &mesh)
Definition: syncTools.C:126
polyMesh.H
Foam::HashSet< label, Hash< label > >
syncTools.H
Foam::faceZoneSet::flipMap
const boolList & flipMap() const
Definition: faceZoneSet.H:118
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::sumOp
Definition: ops.H:213
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::polyMesh::faceZones
const faceZoneMesh & faceZones() const
Return face zone mesh.
Definition: polyMesh.H:477
Foam::IOobject::writeOption
writeOption
Enumeration defining the write options.
Definition: IOobject.H:127
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::faceSet::writeDebug
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: faceSet.C:215
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::boolUIndList
UIndirectList< bool > boolUIndList
UIndirectList of bools.
Definition: UIndirectList.H:55
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::faceZone
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:65
Foam::faceZoneSet::invert
virtual void invert(const label maxLen)
Invert contents.
Definition: faceZoneSet.C:135
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:472
Foam::topoSet::set
virtual bool set(const label id)
Set an index.
Definition: topoSet.C:517
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
Foam::HashTable< zero::null, label, Hash< label > >::resize
void resize(const label sz)
Resize the hash table for efficiency.
Definition: HashTable.C:553
Foam::ZoneMesh< faceZone, polyMesh >
Foam::faceZoneSet
Like faceSet but -reads data from faceZone -updates faceZone when writing.
Definition: faceZoneSet.H:52
Foam::faceZoneSet::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write faceZone.
Definition: faceZoneSet.C:453
Foam::faceZoneSet::writeDebug
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: faceZoneSet.C:527
Foam::topoSet
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:66
Foam::faceZoneSet::faceZoneSet
faceZoneSet(const polyMesh &mesh, const word &name, readOption r=MUST_READ, writeOption w=NO_WRITE)
Construct from objectRegistry and name.
Definition: faceZoneSet.C:66
Foam::faceZoneSet::subtractSet
virtual void subtractSet(const topoSet &set)
Subtract elements present in set.
Definition: faceZoneSet.C:265
Foam::faceZoneSet::sync
virtual void sync(const polyMesh &mesh)
Sync faceZoneSet across coupled patches.
Definition: faceZoneSet.C:317
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::primitiveMesh::nBoundaryFaces
label nBoundaryFaces() const
Number of boundary faces (== nFaces - nInternalFaces)
Definition: primitiveMeshI.H:84
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
zoneID
const labelIOList & zoneID
Definition: interpolatedFaces.H:22
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::ZoneMesh::findZoneID
label findZoneID(const word &zoneName) const
Find zone index given a name, return -1 if not found.
Definition: ZoneMesh.C:484
Foam::mapPolyMesh::reverseFaceMap
const labelList & reverseFaceMap() const
Reverse face map.
Definition: mapPolyMesh.H:500
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::faceZoneSet::addSet
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: faceZoneSet.C:214
setToFaceZone.H
Foam::faceZoneSet::subset
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: faceZoneSet.C:166
Foam::HashTable< zero::null, label, Hash< label > >::clearStorage
void clearStorage()
Clear the table entries and the table itself.
Definition: HashTable.C:649
Foam::List< label >
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:121
setsToFaceZone.H
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:160
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:118
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::ZoneMesh::clearAddressing
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:621
Foam::faceZone::flipMap
const boolList & flipMap() const
Return face flip map.
Definition: faceZone.H:271
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::IOobject::MUST_READ
Definition: IOobject.H:120
Foam::labelUIndList
UIndirectList< label > labelUIndList
UIndirectList of labels.
Definition: UIndirectList.H:59
Foam::primitiveMesh
Cell-face mesh analysis engine.
Definition: primitiveMesh.H:78