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"
34#include "fieldDictionary.H"
36
37// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38
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 }
115 }
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 (
153 mesh,
154 dict.get<word>("field"),
155 dict.get<scalar>("min"),
156 dict.get<scalar>("max")
157 )
158{}
159
160
162(
163 const polyMesh& mesh,
164 Istream& is
165)
166:
168 fieldName_(checkIs(is)),
169 min_(readScalar(checkIs(is))),
170 max_(readScalar(checkIs(is)))
171{}
172
173
174// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
175
176void Foam::fieldToCell::applyToSet
177(
178 const topoSetSource::setAction action,
179 topoSet& set
180) const
181{
182 // Try to load field
183 IOobject fieldObject
184 (
185 fieldName_,
186 mesh().time().timeName(),
187 mesh(),
190 false
191 );
192
193 // Note: should check for volScalarField but that introduces dependency
194 // on volMesh so just use another type with processor-local scope
195 if (!fieldObject.typeHeaderOk<labelIOList>(false))
196 {
198 << "Cannot read field " << fieldName_
199 << " from time " << mesh().time().timeName() << endl;
200 }
201 else if ("volScalarField" == fieldObject.headerClassName())
202 {
203 // Note: cannot use volScalarField::typeName since that would
204 // introduce linkage problems (finiteVolume needs meshTools)
205
206 IFstream str(typeFilePath<labelIOList>(fieldObject));
207
208 // Read as dictionary
209 fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
210
211 scalarField internalVals("internalField", fieldDict, mesh().nCells());
212
213 applyToSet(action, internalVals, set);
214 }
215 else if ("volVectorField" == fieldObject.headerClassName())
216 {
217 // Note: cannot use volVectorField::typeName since that would
218 // introduce linkage problems (finiteVolume needs meshTools)
219
220 IFstream str(typeFilePath<labelIOList>(fieldObject));
221
222 // Read as dictionary
223 fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
224
225 vectorField internalVals("internalField", fieldDict, mesh().nCells());
226
227 applyToSet(action, mag(internalVals), set);
228 }
229 else
230 {
232 << "Cannot handle fields of type " << fieldObject.headerClassName()
233 << endl;
234 }
235}
236
237
238// ************************************************************************* //
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, using an ISstream.
Definition: IFstream.H:57
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (uses typeFilePath to find file) and check its info.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Read field as dictionary (without mesh).
A topoSetCellSource to select cells based on volScalarField values, i.e. select cells with given fiel...
Definition: fieldToCell.H:163
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:290
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells.
Class with constructor to add usage string to table.
Base class of a source for a topoSet.
Definition: topoSetSource.H:68
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)
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:67
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
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:38
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:47
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
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:33
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333