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 -------------------------------------------------------------------------------
11 License
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 
39 namespace Foam
40 {
41  defineTypeNameAndDebug(fieldToCell, 0);
42  addToRunTimeSelectionTable(topoSetSource, fieldToCell, word);
43  addToRunTimeSelectionTable(topoSetSource, fieldToCell, istream);
44  addToRunTimeSelectionTable(topoSetCellSource, fieldToCell, word);
45  addToRunTimeSelectionTable(topoSetCellSource, fieldToCell, istream);
47  (
48  topoSetCellSource,
49  fieldToCell,
50  word,
51  field
52  );
54  (
55  topoSetCellSource,
56  fieldToCell,
57  istream,
58  field
59  );
60 }
61 
62 
63 Foam::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 
73 void 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 
176 void 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  // Note: should use volScalarField::typeName instead below
202  // but that would introduce linkage problems (finiteVolume needs
203  // meshTools)
204  else if ("volScalarField" == fieldObject.headerClassName())
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  IFstream str(typeFilePath<labelIOList>(fieldObject));
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 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::topoSetSource::ADD
Add elements to current set.
Definition: topoSetSource.H:103
Foam::IOobject::AUTO_WRITE
Definition: IOobject.H:194
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
fieldToCell.H
Foam::fieldDictionary
Read field as dictionary (without mesh).
Definition: fieldDictionary.H:49
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::fieldToCell::fieldToCell
fieldToCell(const polyMesh &mesh, const word &fieldName, const scalar min, const scalar max)
Construct from components.
Definition: fieldToCell.C:123
Foam::IOobject::typeHeaderOk
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.
Definition: IOobjectTemplates.C:39
Foam::topoSetSource::addToUsageTable
Class with constructor to add usage string to table.
Definition: topoSetSource.H:129
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::topoSetSource::setAction
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:100
Foam::IOobject::headerClassName
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::topoSetSource::NEW
Create a new set and ADD elements to it.
Definition: topoSetSource.H:104
polyMesh.H
Foam::fieldToCell
A topoSetCellSource to select cells based on volScalarField values, i.e. select cells with given fiel...
Definition: fieldToCell.H:160
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::topoSetSource::verbose_
bool verbose_
Output verbosity (default: true)
Definition: topoSetSource.H:159
Foam::Field< scalar >
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::addNamedToRunTimeSelectionTable
addNamedToRunTimeSelectionTable(topoSetCellSource, badQualityToCell, word, badQuality)
field
rDeltaTY field()
Foam::topoSet
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:63
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
IFstream.H
timeName
word timeName
Definition: getTimeIndex.H:3
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::topoSetSource::SUBTRACT
Subtract elements from current set.
Definition: topoSetSource.H:105
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Time.H
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::topoSetCellSource
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells.
Definition: topoSetCellSource.H:54
Foam::IOList< label >
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:280
cellSet.H
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
fieldDictionary.H
Foam::IOobject::MUST_READ
Definition: IOobject.H:185