fvMeshSubsetProxy.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) 2016-2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "fvMeshSubsetProxy.H"
29 #include "cellSet.H"
30 #include "cellZone.H"
31 #include "Time.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 void Foam::fvMeshSubsetProxy::clearOut()
36 {
37  subsetter_.clear();
38  type_ = subsetType::NONE;
39  name_.clear();
40  names_.clear();
41  selectedCells_.clearStorage();
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
47 Foam::fvMeshSubsetProxy::fvMeshSubsetProxy(fvMesh& baseMesh)
48 :
49  baseMesh_(baseMesh),
50  subsetter_(baseMesh),
51  exposedPatchId_(-1),
52  type_(subsetType::NONE),
53  name_(),
54  names_(),
55  selectedCells_()
56 {}
57 
58 
59 Foam::fvMeshSubsetProxy::fvMeshSubsetProxy
60 (
61  fvMesh& baseMesh,
62  const subsetType type,
63  const word& selectionName,
64  label exposedPatchId
65 )
66 :
67  baseMesh_(baseMesh),
68  subsetter_(baseMesh),
69  exposedPatchId_(exposedPatchId),
70  type_(selectionName.empty() ? subsetType::NONE : type),
71  name_(),
72  names_(),
73  selectedCells_()
74 {
75  if (type_ == subsetType::ZONES)
76  {
77  // Populate wordRes for ZONES
78  names_.resize(1);
79  names_.first() = selectionName;
80  }
81  else if (type_ == subsetType::SET || type_ == subsetType::ZONE)
82  {
83  name_ = selectionName;
84  }
85 
86  correct();
87 }
88 
89 
90 Foam::fvMeshSubsetProxy::fvMeshSubsetProxy
91 (
92  fvMesh& baseMesh,
93  const wordRes& zoneNames,
94  label exposedPatchId
95 )
96 :
97  baseMesh_(baseMesh),
98  subsetter_(baseMesh),
99  exposedPatchId_(exposedPatchId),
100  type_(subsetType::ZONES),
101  name_(),
102  names_(zoneNames),
103  selectedCells_()
104 {
105  correct();
106 }
107 
108 
109 Foam::fvMeshSubsetProxy::fvMeshSubsetProxy
110 (
111  fvMesh& baseMesh,
112  wordRes&& zoneNames,
113  label exposedPatchId
114 )
115 :
116  baseMesh_(baseMesh),
117  subsetter_(baseMesh),
118  exposedPatchId_(exposedPatchId),
119  type_(subsetType::ZONES),
120  name_(),
121  names_(std::move(zoneNames)),
122  selectedCells_()
123 {
124  correct();
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
131 {
132  fvMeshSubsetProxy::clearOut();
133 
134  if (!zoneNames.empty())
135  {
136  type_ = subsetType::ZONES;
137  names_ = zoneNames;
138  correct();
139  }
140 }
141 
142 
144 {
145  if (type_ == subsetType::NONE)
146  {
147  subsetter_.clear();
148  selectedCells_.clearStorage();
149  return false;
150  }
151 
152  const label nCells = baseMesh_.nCells();
153 
154  bitSet selectedCells;
155 
156  if (type_ == subsetType::SET)
157  {
158  if (verbose)
159  {
160  Info<< "Subsetting mesh based on cellSet " << name_ << endl;
161  }
162 
163  cellSet cset(baseMesh_, name_);
164 
165  selectedCells.resize(nCells);
166  for (const label idx : cset)
167  {
168  selectedCells.set(idx);
169  }
170  }
171  else if (type_ == subsetType::ZONE)
172  {
173  if (verbose)
174  {
175  Info<< "Subsetting mesh based on cellZone " << name_ << endl;
176  }
177 
178  selectedCells.resize(nCells);
179  selectedCells.set(baseMesh_.cellZones()[name_]);
180  }
181  else if (type_ == subsetType::ZONES)
182  {
183  if (verbose)
184  {
185  Info<< "Subsetting mesh based on cellZones "
186  << flatOutput(names_) << endl;
187  }
188 
189  selectedCells = baseMesh_.cellZones().selection(names_);
190  }
191 
192 
193  const bool changed = (selectedCells_ != selectedCells);
194 
195  // Use as a cached value for next time
196  selectedCells_.transfer(selectedCells);
197 
198  if (changed || selectedCells_.empty())
199  {
200  subsetter_.setCellSubset(selectedCells_, exposedPatchId_);
201  }
202 
203  return returnReduce(changed, orOp<bool>());
204 }
205 
206 
208 {
209  const polyMesh::readUpdateState meshState = baseMesh_.readUpdate();
210 
211  if (meshState == polyMesh::POINTS_MOVED)
212  {
213  if (correct(true))
214  {
215  // The cellSet/cellZone changed on POINTS_MOVED,
216  // treat like TOPO_CHANGE
217  return polyMesh::TOPO_CHANGE;
218  }
219  }
220  else if
221  (
222  meshState == polyMesh::TOPO_CHANGE
223  || meshState == polyMesh::TOPO_PATCH_CHANGE
224  )
225  {
226  correct(true);
227  }
228 
229  return meshState;
230 }
231 
232 
233 // ************************************************************************* //
Foam::PackedList::resize
void resize(const label numElem, const unsigned int val=0u)
Reset addressable list size, does not shrink the allocated size.
Definition: PackedListI.H:409
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::polyMesh::POINTS_MOVED
Definition: polyMesh.H:93
Foam::bitSet::set
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:574
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::fvMeshSubsetProxy::subsetType
subsetType
Internal bookkeeping for subset type.
Definition: fvMeshSubsetProxy.H:62
Foam::fvMeshSubset::clear
void clear()
Reset maps and subsetting.
Definition: fvMeshSubset.C:545
Foam::polyMesh::TOPO_PATCH_CHANGE
Definition: polyMesh.H:95
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
correct
fvOptions correct(rho)
Foam::polyMesh::TOPO_CHANGE
Definition: polyMesh.H:94
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
fvMeshSubsetProxy.H
Foam::cellSet
A collection of cell labels.
Definition: cellSet.H:51
Foam::polyMesh::readUpdateState
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:90
Time.H
Foam::fvMeshSubsetProxy::resetZones
void resetZones(const wordRes &zoneNames)
Define the zones selection, subset the mesh accordingly.
Definition: fvMeshSubsetProxy.C:130
cellZone.H
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::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Foam::PackedList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: PackedListI.H:520
cellSet.H
Foam::fvMeshSubsetProxy::correct
bool correct(bool verbose=false)
Update of mesh subset.
Definition: fvMeshSubsetProxy.C:143
Foam::orOp
Definition: ops.H:234
Foam::fvMeshSubsetProxy::readUpdate
polyMesh::readUpdateState readUpdate()
Read mesh. Correct on topo-change.
Definition: fvMeshSubsetProxy.C:207
Foam::bitSet::transfer
void transfer(bitSet &bitset)
Definition: bitSetI.H:544