polyCellSet.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2022-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "polyCellSet.H"
27 #include "polyMesh.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  template<> const char* NamedEnum
34  <
36  4
37  >::names[] =
38  {
39  "points",
40  "cellSet",
41  "cellZone",
42  "all"
43  };
44 
47 }
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 void Foam::polyCellSet::setCells()
53 {
54  Info<< incrIndent;
55 
56  switch (selectionType_)
57  {
59  {
60  Info<< indent << "- selecting cells using points" << endl;
61 
62  labelHashSet selectedCells;
63 
64  forAll(points_, i)
65  {
66  label celli = mesh_.findCell(points_[i]);
67  if (celli >= 0)
68  {
69  selectedCells.insert(celli);
70  }
71 
72  const label globalCelli = returnReduce(celli, maxOp<label>());
73  if (globalCelli < 0)
74  {
76  << "Unable to find owner cell for point " << points_[i]
77  << endl;
78  }
79 
80  }
81 
82  cells_ = selectedCells.toc();
83 
84  break;
85  }
87  {
88  Info<< indent
89  << "- selecting cells using cellSet " << cellSetName_ << endl;
90 
91  cells_ = cellSet(mesh_, cellSetName_).toc();
92 
93  break;
94  }
96  {
97  Info<< indent
98  << "- selecting cells using cellZone " << cellSetName_ << endl;
99 
100  const label zoneID = mesh_.cellZones().findIndex(cellSetName_);
101  if (zoneID == -1)
102  {
104  << "Cannot find cellZone " << cellSetName_ << endl
105  << "Valid cellZones are " << mesh_.cellZones().toc()
106  << exit(FatalError);
107  }
108 
109  // cells_ not required for cellZone
110 
111  break;
112  }
113  case selectionTypes::all:
114  {
115  Info<< indent << "- selecting all cells" << endl;
116 
117  // cells_ not required for cellZone
118 
119  break;
120  }
121  }
122 
123  Info<< decrIndent;
124 }
125 
126 
127 Foam::labelUList Foam::polyCellSet::identityMap(const label len) const
128 {
129  // Static identity list, resized as required
130  static labelList map;
131 
132  if (len > map.size())
133  {
134  map.resize(len);
135 
136  forAll(map, i)
137  {
138  map[i] = i;
139  }
140  }
141 
142  return SubList<label>(map, len);
143 }
144 
145 
146 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
147 
149 :
150  mesh_(mesh),
151  selectionType_(selectionTypes::all),
152  cellSetName_(word::null)
153 {}
154 
155 
157 :
158  mesh_(mesh),
159  selectionType_(selectionTypes::all),
160  cellSetName_(word::null)
161 {
162  read(dict);
163 }
164 
165 
166 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
167 
169 {}
170 
171 
172 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
173 
175 {
176  if (selectionType_ == selectionTypes::points)
177  {
178  setCells();
179  }
180 }
181 
182 
184 {
185  setCells();
186 }
187 
188 
190 {
191  setCells();
192 }
193 
194 
196 {
197  setCells();
198 }
199 
200 
202 {
203  if (dict.found("select") || dict.found("selectionMode"))
204  {
205  selectionType_ = selectionTypeNames.read
206  (
207  dict.lookupBackwardsCompatible({"select", "selectionMode"})
208  );
209  }
210  else if (dict.found("points"))
211  {
212  selectionType_ = selectionTypes::points;
213  }
214  else if (dict.found("cellSet"))
215  {
216  selectionType_ = selectionTypes::cellSet;
217  }
218  else if (dict.found("cellZone"))
219  {
220  selectionType_ = selectionTypes::cellZone;
221  }
222  else
223  {
224  dict.lookup("select");
225  }
226 
227  switch (selectionType_)
228  {
230  {
231  dict.lookup("points") >> points_;
232  break;
233  }
234  case selectionTypes::cellSet:
235  {
236  dict.lookup("cellSet") >> cellSetName_;
237  break;
238  }
239  case selectionTypes::cellZone:
240  {
241  dict.lookup("cellZone") >> cellSetName_;
242  break;
243  }
244  case selectionTypes::all:
245  {
246  break;
247  }
248  default:
249  {
251  << "Unknown selection type "
252  << selectionTypeNames[selectionType_]
253  << nl << "Valid selection type:"
254  << selectionTypeNames.toc()
255  << exit(FatalError);
256  }
257  }
258 
259  setCells();
260 
261  return true;
262 }
263 
264 
265 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
wordList toc() const
Return the table of contents.
void resize(const label)
Alias for setSize(const label)
Definition: ListI.H:138
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:54
label findIndex(const word &key) const
Return the index of the given the key or -1 if not found.
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:710
ITstream & lookupBackwardsCompatible(const wordList &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream, trying a list of keywords.
Definition: dictionary.C:721
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:509
polyCellSet(const polyMesh &mesh)
Construct from mesh. Will select all.
Definition: polyCellSet.C:148
label celli(const label i) const
Return the cell index corresponding to the cell set index.
Definition: polyCellSetI.H:79
~polyCellSet()
Destructor.
Definition: polyCellSet.C:168
void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: polyCellSet.C:183
void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: polyCellSet.C:195
void movePoints()
Update for mesh motion.
Definition: polyCellSet.C:174
bool read(const dictionary &dict)
Read coefficients dictionary.
Definition: polyCellSet.C:201
void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: polyCellSet.C:189
static const NamedEnum< selectionTypes, 4 > selectionTypeNames
Word list of selection type names.
Definition: polyCellSet.H:97
selectionTypes
Enumeration for selection mode types.
Definition: polyCellSet.H:89
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const cellZoneList & cellZones() const
Return cell zones.
Definition: polyMesh.H:452
label findCell(const point &p, const cellDecomposition=CELL_TETS) const
Find cell enclosing this location and return index.
Definition: polyMesh.C:1727
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
const pointField & points
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:241
List< label > labelList
A List of labels.
Definition: labelList.H:56
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:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
messageStream Info
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:234
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
HashSet< label, Hash< label > > labelHashSet
A HashSet with label keys.
Definition: HashSet.H:213
error FatalError
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:227
static const char nl
Definition: Ostream.H:266
dictionary dict