Loading...
Searching...
No Matches
cellSetOption.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-2023 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 "cellSetOption.H"
30#include "cellSet.H"
31#include "cellBitSet.H"
32#include "volFields.H"
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
38{
39 namespace fv
40 {
42 }
43}
44
45
46const Foam::Enum
47<
49>
51({
52 { selectionModeType::smAll, "all" },
53 { selectionModeType::smGeometric, "geometric" },
54 { selectionModeType::smPoints, "points" },
55 { selectionModeType::smMovingPoints, "movingPoints" },
56 { selectionModeType::smCellSet, "cellSet" },
58 { selectionModeType::smCellType, "cellType" }
59});
60
61
62// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
63
65{
67
68 switch (selectionMode_)
69 {
70 case smAll:
71 {
72 break;
73 }
74 case smGeometric:
75 {
76 geometricSelection_ = dict.subDict("selection");
77 break;
78 }
79 case smPoints:
80 {
81 dict.readEntry("points", points_);
82 break;
83 }
84 case smMovingPoints:
85 {
86 const dictionary& mpsDict = dict.subDict("movingPoints");
87
88 movingPoints_.resize_null(mpsDict.size());
89
90 label pointi = 0;
91 for (const entry& dEntry : mpsDict)
92 {
93 const word& key = dEntry.keyword();
94
96 (
97 pointi,
98 Function1<point>::New
99 (
100 key,
101 mpsDict,
102 &mesh_
103 )
104 );
105 ++pointi;
106 }
107 break;
108 }
109 case smCellSet:
110 {
111 selectionNames_.resize(1);
112 dict.readEntry("cellSet", selectionNames_.first());
113 break;
114 }
115 case smCellZone:
116 {
117 if
118 (
119 !dict.readIfPresent("cellZones", selectionNames_)
120 || selectionNames_.empty()
121 )
122 {
123 selectionNames_.resize(1);
124 dict.readEntry("cellZone", selectionNames_.first());
125 }
126 break;
127 }
128 case smCellType:
129 {
130 break;
131 }
132 default:
133 {
135 << "Unknown selectionMode "
137 << ". Valid selectionMode types : "
139 << exit(FatalError);
140 }
141 }
142}
143
144
146{
147 // Set volume information
148
149 scalar sumVol = 0;
150 for (const label celli : cells_)
151 {
152 sumVol += mesh_.V()[celli];
153 }
154 reduce(sumVol, sumOp<scalar>());
155
156 const scalar old(V_);
157 V_ = sumVol;
158
159 // Compare volume values, stringified using current write precision
160 if
161 (
164 )
165 {
167 << "- selected " << returnReduce(cells_.size(), sumOp<label>())
168 << " cell(s) with volume " << V_ << endl;
169 }
170}
171
172
174{
175 switch (selectionMode_)
176 {
177 case smAll:
178 {
179 Info<< indent << "- selecting all cells" << endl;
180
181 cells_ = identity(mesh_.nCells());
182 break;
183 }
184 case smGeometric:
185 {
186 Info<< indent << "- selecting cells geometrically" << endl;
187
188 bitSet selectedCells
189 (
190 // verbosity = true
191 cellBitSet::select(mesh_, geometricSelection_, true)
192 );
193
194 // From bitSet -> labels
195 cells_ = selectedCells.sortedToc();
196 break;
197 }
198 case smPoints:
199 {
200 Info<< indent << "- selecting cells using points" << endl;
201
202 labelHashSet selectedCells;
203
204 for (const point& p : points_)
205 {
206 const label celli = mesh_.findCell(p);
207
208 const bool found = (celli >= 0);
209
210 if (found)
211 {
212 selectedCells.insert(celli);
213 }
214
215 if (!returnReduceOr(found))
216 {
218 << "No owner cell found for point " << p << endl;
219 }
220 }
221
222 cells_ = selectedCells.sortedToc();
223 break;
224 }
225 case smMovingPoints:
226 {
227 Info<< indent << "- selecting cells using moving points" << endl;
228
229 const scalar t = mesh_.time().timeOutputValue();
230
231 labelHashSet selectedCells;
232
233 forAll(movingPoints_, i)
234 {
235 if (!movingPoints_.set(i))
236 {
237 continue;
238 }
239
240 const point p(movingPoints_[i].value(t));
241
242 const label celli = mesh_.findCell(p);
243
244 const bool found = (celli >= 0);
245
246 // Ensure that only one processor inserts this cell
247 label proci = -1;
248 if (found)
249 {
250 proci = Pstream::myProcNo();
251 }
252 reduce(proci, maxOp<label>());
253
254 if (found && (proci == Pstream::myProcNo()))
255 {
256 selectedCells.insert(celli);
257 }
258
259 if (!returnReduceOr(found))
260 {
262 << "No owner cell found for point " << p << endl;
263 }
264 }
265
266 cells_ = selectedCells.sortedToc();
267 break;
268 }
269 case smCellSet:
270 {
271 Info<< indent
272 << "- selecting cells using cellSet "
273 << zoneName() << endl;
274
275 cells_ = cellSet(mesh_, zoneName()).sortedToc();
276 break;
277 }
278 case smCellZone:
279 {
280 Info<< indent
281 << "- selecting cells using cellZones "
282 << flatOutput(selectionNames_) << nl;
283
284 const auto& zones = mesh_.cellZones();
285
286 // Also handles groups, multiple zones etc ...
287 labelList zoneIDs = zones.indices(selectionNames_);
288
289 if (zoneIDs.empty())
290 {
292 << "No matching cellZones: "
293 << flatOutput(selectionNames_) << nl
294 << "Valid zones : "
295 << flatOutput(zones.names()) << nl
296 << "Valid groups: "
297 << flatOutput(zones.groupNames())
298 << nl
299 << exit(FatalError);
300 }
301
302 if (zoneIDs.size() == 1)
303 {
304 cells_ = zones[zoneIDs.first()];
305 // TBD: Foam::sort(cells_);
306 }
307 else
308 {
309 cells_ = zones.selection(zoneIDs).sortedToc();
310 }
311 break;
312 }
313 case smCellType:
314 {
315 labelHashSet selectedCells;
316 const cellCellStencilObject& overlap = Stencil::New(mesh_);
317 const labelUList& cellTypes = overlap.cellTypes();
318 forAll(cellTypes, celli)
319 {
321 {
322 selectedCells.insert(celli);
323 }
324 cells_ = selectedCells.sortedToc();
325 }
326 break;
327 }
328 default:
329 {
331 << "Unknown selectionMode "
332 << selectionModeTypeNames_[selectionMode_]
333 << ". Valid selectionMode types are "
334 << selectionModeTypeNames_
335 << exit(FatalError);
336 }
337 }
338
339 if
340 (
341 !(smAll == selectionMode_ || smMovingPoints == selectionMode_)
342 && returnReduceAnd(cells_.empty())
343 )
344 {
346 << "No cells selected!" << endl;
347 }
348}
349
350
351// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
352
354(
355 const word& name,
356 const word& modelType,
357 const dictionary& dict,
358 const fvMesh& mesh
359)
360:
361 fv::option(name, modelType, dict, mesh),
362 selectionMode_(selectionModeTypeNames_.get("selectionMode", coeffs_)),
363 updateSelection_(false),
364 timeStart_(-1),
365 duration_(0),
366 selectionNames_(),
367 points_(),
368 movingPoints_(),
369 geometricSelection_(),
370 V_(0)
371{
373 read(dict);
378}
379
380
381// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
382
384{
385 if (fv::option::isActive() && inTimeLimits(mesh_.time().value()))
386 {
387 // Update the cell set if the mesh is changing
388 if (mesh_.changing())
389 {
390 if (mesh_.topoChanging())
391 {
392 setCellSelection();
393 // Force printing of new set volume
394 V_ = -GREAT;
395 }
396 else if
397 (
398 selectionMode_ == smGeometric
399 || selectionMode_ == smPoints
400 || selectionMode_ == smCellType
401 || selectionMode_ == smMovingPoints
402 )
403 {
404 // Geometric selection mode(s)
405 setCellSelection();
406 }
407
408 // Report new volume (if changed)
409 setVol();
410 }
411 else if
412 (
413 selectionMode_ == smMovingPoints
414 || selectionMode_ == smGeometric
415 )
416 {
417 // Update the cell selection even if mesh is not moving
418 setCellSelection();
419 setVol();
420 }
421
422 return true;
423 }
424
425 return false;
426}
427
428
430{
432 {
433 return false;
434 }
435
436 timeStart_ = -1;
437
438 if (coeffs_.readIfPresent("timeStart", timeStart_))
439 {
440 coeffs_.readEntry("duration", duration_);
441 }
442
443 // Do not read and set selections unless users request
444 updateSelection_ = coeffs_.getOrDefault("updateSelection", false);
445
446 if (updateSelection_)
447 {
448 setSelection(coeffs_);
449 setCellSelection();
450 setVol();
451 }
452
453 return true;
454}
455
456
457// ************************************************************************* //
bool found
label size() const noexcept
The number of elements in list.
Definition DLListBase.H:194
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition HashTable.C:156
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition IOstream.H:437
void clear()
Clear the list, i.e. set size to zero.
Definition ListI.H:133
static FOAM_NO_DANGLING_REFERENCE const cellCellStencilObject & New(const fvMesh &mesh, Args &&... args)
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition Time.C:714
const word & timeName() const noexcept
The current time name.
Definition TimeStateI.H:30
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Negative if the process is not a...
Definition UPstream.H:1706
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
labelList sortedToc() const
The indices of the on bits as a sorted labelList.
Definition bitSetI.H:441
static bitSet select(const polyMesh &mesh, const dictionary &dict, const bool verbosity=false)
Return a cell selection according to the dictionary specification of actions.
Definition cellBitSet.C:84
A collection of cell labels.
Definition cellSet.H:50
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
Intermediate abstract class for handling cell-set options for the derived fvOptions.
dictionary geometricSelection_
Dictionary entries for "geometric" (topoSetCellSource) selection.
List< point > points_
List of points for "points" selectionMode.
wordRes selectionNames_
Face selection names (for set or zone selections).
void setSelection(const dictionary &dict)
Set cell selection name or points selection from dictionary input.
labelList cells_
Set of cells to apply source to.
virtual bool read(const dictionary &dict)
Read source dictionary.
cellSetOption(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
scalar V_
Sum of cell volumes.
scalar duration_
Duration of fvOption execution starting from timeStart.
scalar timeStart_
Start time of fvOption.
const wordRe & zoneName() const
Return const access to the first set/zone name.
virtual bool isActive()
Is the source active?
selectionModeType selectionMode_
Cell selection mode.
static const Enum< selectionModeType > selectionModeTypeNames_
List of selection mode type names.
void setCellSelection()
Set the cell selection based on user input selection mode.
bool updateSelection_
Flag to enable dictionary-based updates of selections.
selectionModeType
Enumeration for selection mode types.
@ smCellType
"overset type cells"
@ smMovingPoints
"movingPoints"
bool inTimeLimits(const scalar timeValue) const
True if within time limits.
PtrList< Function1< point > > movingPoints_
List of points for "movingPoints" selectionMode.
void setVol()
Recalculate the volume.
const word & name() const noexcept
Return const access to the source name.
Definition fvOptionI.H:24
const fvMesh & mesh_
Reference to the mesh database.
Definition fvOption.H:142
option(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
Definition fvOption.C:51
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition fvOptionIO.C:48
dictionary coeffs_
Dictionary containing source coefficients.
Definition fvOption.H:152
virtual bool isActive()
Is the source active?
Definition fvOption.C:115
const fvMesh & mesh() const noexcept
Return const access to the mesh database.
Definition fvOptionI.H:30
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
volScalarField & p
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
const cellCellStencilObject & overlap
Definition correctPhi.H:57
const labelIOList & zoneIDs
Definition correctPhi.H:59
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for finite-volume.
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
Namespace for OpenFOAM.
bool returnReduceOr(const bool value, const int communicator=UPstream::worldComm)
Perform logical (or) MPI Allreduce on a copy. Uses UPstream::reduceOr.
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
messageStream Info
Information stream (stdout output on master, null elsewhere).
T returnReduce(const T &value, BinaryOp bop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition Ostream.H:490
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Ostream & indent(Ostream &os)
Indent stream.
Definition Ostream.H:481
void reduce(T &value, BinaryOp bop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce).
bool returnReduceAnd(const bool value, const int communicator=UPstream::worldComm)
Perform logical (and) MPI Allreduce on a copy. Uses UPstream::reduceAnd.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
vector point
Point is a vector.
Definition point.H:37
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
UList< label > labelUList
A UList of labels.
Definition UList.H:75
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition Ostream.H:499
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
const labelUList & cellTypes
Definition setCellMask.H:27
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299