Loading...
Searching...
No Matches
fieldToCell.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 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 "fieldToCell.H"
30#include "polyMesh.H"
31#include "cellSet.H"
32#include "Time.H"
33#include "IFstream.H"
37// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39namespace Foam
40{
47 (
50 word,
51 field
52 );
54 (
57 istream,
58 field
59 );
60}
61
62
63Foam::topoSetSource::addToUsageTable Foam::fieldToCell::usage_
64(
65 fieldToCell::typeName,
66 "\n Usage: fieldToCell field min max\n\n"
67 " Select all cells with field value >= min and <= max\n\n"
68);
69
70
71// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
72
73void Foam::fieldToCell::applyToSet
74(
75 const topoSetSource::setAction action,
76 const scalarField& field,
77 topoSet& set
78) const
79{
80 if (verbose_)
81 {
82 Info << " Field min:" << min(field) << " max:" << max(field) << nl;
83 }
84
85 if (action == topoSetSource::ADD || action == topoSetSource::NEW)
86 {
87 if (verbose_)
88 {
89 Info<< " Adding all cells with value of field " << fieldName_
90 << " within range " << min_ << ".." << max_ << endl;
91 }
92
93 forAll(field, celli)
94 {
95 if (field[celli] >= min_ && field[celli] <= max_)
96 {
97 set.set(celli);
98 }
99 }
100 }
101 else if (action == topoSetSource::SUBTRACT)
102 {
103 if (verbose_)
104 {
105 Info<< " Removing all cells with value of field " << fieldName_
106 << " within range " << min_ << ".." << max_ << endl;
107 }
108
109 forAll(field, celli)
110 {
111 if (field[celli] >= min_ && field[celli] <= max_)
112 {
113 set.unset(celli);
114 }
116 }
117}
118
119
120// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
121
123(
124 const polyMesh& mesh,
125 const word& fieldName,
126 const scalar min,
127 const scalar max
128)
129:
131 fieldName_(fieldName),
132 min_(min),
133 max_(max)
134{
135 if (min_ > max_)
136 {
138 << "Input min value = " << min_ << " is larger than "
139 << "input max value = " << max_ << " for field = " << fieldName_
140 << endl;
141 }
142}
143
144
146(
147 const polyMesh& mesh,
148 const dictionary& dict
149)
150:
152 fieldName_(dict.get<word>("field")),
153 min_(dict.get<scalar>("min")),
154 max_(dict.get<scalar>("max"))
155{}
156
157
159(
160 const polyMesh& mesh,
161 Istream& is
162)
163:
165 fieldName_(checkIs(is)),
166 min_(readScalar(checkIs(is))),
167 max_(readScalar(checkIs(is)))
168{}
169
170
171// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
172
173void Foam::fieldToCell::applyToSet
174(
175 const topoSetSource::setAction action,
176 topoSet& set
177) const
178{
179 // Try to load field
180 IOobject fieldObject
181 (
182 fieldName_,
183 mesh().time().timeName(),
184 mesh(),
188 );
189
190 // Note: should check for volScalarField but that introduces dependency
191 // on volMesh so just use another type with processor-local scope
192 if (!fieldObject.typeHeaderOk<labelIOList>(false))
193 {
195 << "Cannot read field " << fieldName_
196 << " from time " << mesh().time().timeName() << endl;
197 }
198 else if (fieldObject.isHeaderClass("volScalarField"))
199 {
200 // Note: cannot use volScalarField::typeName since that would
201 // introduce linkage problems (finiteVolume needs meshTools)
202
203 IFstream str(fieldObject.typeFilePath<labelIOList>());
204
205 // Read as dictionary
206 fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
207
208 scalarField internalVals("internalField", fieldDict, mesh().nCells());
209
210 applyToSet(action, internalVals, set);
211 }
212 else if (fieldObject.isHeaderClass("volVectorField"))
213 {
214 // Note: cannot use volVectorField::typeName since that would
215 // introduce linkage problems (finiteVolume needs meshTools)
216
217 IFstream str(fieldObject.typeFilePath<labelIOList>());
218
219 // Read as dictionary
220 fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
221
222 vectorField internalVals("internalField", fieldDict, mesh().nCells());
223
224 applyToSet(action, mag(internalVals), set);
225 }
226 else
227 {
229 << "Cannot handle fields of type " << fieldObject.headerClassName()
230 << endl;
231 }
232}
233
234
235// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addNamedToRunTimeSelectionTable(baseType, thisType, argNames, lookupName)
Add to construction table with 'lookupName' as the key.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Input from file stream as an ISstream, normally using std::ifstream for the actual input.
Definition IFstream.H:55
@ NO_REGISTER
Do not request registration (bool: false).
@ MUST_READ
Reading required.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
bool isHeaderClass(const word &expectedType) const
Check if headerClassName() equals the expected type. Always true if the expected type is empty.
Definition IOobjectI.H:267
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition IOobjectI.H:223
fileName typeFilePath(const bool search=true) const
Call localFilePath or globalFilePath for given type depending on its is_globalIOobject trait.
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (respects is_globalIOobject trait) and check its info. A void type suppresses trait and t...
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
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
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Read field as dictionary (without mesh).
A topoSetCellSource to select cells based on volScalarField values, i.e. select cells with given fiel...
fieldToCell(const polyMesh &mesh, const word &fieldName, const scalar min, const scalar max)
Construct from components.
const Time & time() const
Return the top-level database.
Definition fvMesh.H:360
Mesh consisting of general polyhedral cells.
Definition polyMesh.H:79
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells.
topoSetCellSource(const polyMesh &mesh)
Construct from mesh.
Class with constructor to add usage string to table.
Base class of a source for a topoSet.
setAction
Enumeration defining various actions.
@ SUBTRACT
Subtract elements from current set.
@ ADD
Add elements to current set.
@ NEW
Create a new set and ADD elements to it.
bool verbose_
Output verbosity (default: true).
const polyMesh & mesh() const noexcept
Reference to the mesh.
static Istream & checkIs(Istream &is)
Check state of stream.
General set of labels of mesh quantity (points, cells, faces).
Definition topoSet.H:63
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
rDeltaTY field()
dynamicFvMesh & mesh
word timeName
Definition getTimeIndex.H:3
#define WarningInFunction
Report a warning using Foam::Warning.
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition BitOps.C:35
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:40
IOList< label > labelIOList
IO for a List of label.
Definition labelIOList.H:32
messageStream Info
Information stream (stdout output on master, null elsewhere).
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
Field< vector > vectorField
Specialisation of Field<T> for vector.
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299