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 
136 
138 (
139  const polyMesh& mesh,
140  const dictionary& dict
141 )
142 :
144  (
145  mesh,
146  dict.get<word>("field"),
147  dict.get<scalar>("min"),
148  dict.get<scalar>("max")
149  )
150 {}
151 
152 
154 (
155  const polyMesh& mesh,
156  Istream& is
157 )
158 :
160  fieldName_(checkIs(is)),
161  min_(readScalar(checkIs(is))),
162  max_(readScalar(checkIs(is)))
163 {}
164 
165 
166 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
167 
168 void Foam::fieldToCell::applyToSet
169 (
170  const topoSetSource::setAction action,
171  topoSet& set
172 ) const
173 {
174  // Try to load field
175  IOobject fieldObject
176  (
177  fieldName_,
178  mesh().time().timeName(),
179  mesh(),
182  false
183  );
184 
185  // Note: should check for volScalarField but that introduces dependency
186  // on volMesh so just use another type with processor-local scope
187  if (!fieldObject.typeHeaderOk<labelIOList>(false))
188  {
190  << "Cannot read field " << fieldName_
191  << " from time " << mesh().time().timeName() << endl;
192  }
193  // Note: should use volScalarField::typeName instead below
194  // but that would introduce linkage problems (finiteVolume needs
195  // meshTools)
196  else if ("volScalarField" == fieldObject.headerClassName())
197  {
198  IFstream str(typeFilePath<labelIOList>(fieldObject));
199 
200  // Read as dictionary
201  fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
202 
203  scalarField internalVals("internalField", fieldDict, mesh().nCells());
204 
205  applyToSet(action, internalVals, set);
206  }
207  else if ("volVectorField" == fieldObject.headerClassName())
208  {
209  IFstream str(typeFilePath<labelIOList>(fieldObject));
210 
211  // Read as dictionary
212  fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
213 
214  vectorField internalVals("internalField", fieldDict, mesh().nCells());
215 
216  applyToSet(action, mag(internalVals), set);
217  }
218  else
219  {
221  << "Cannot handle fields of type " << fieldObject.headerClassName()
222  << endl;
223  }
224 }
225 
226 
227 // ************************************************************************* //
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:104
Foam::topoSetSource::ADD
Add elements to the set.
Definition: topoSetSource.H:101
Foam::IOobject::AUTO_WRITE
Definition: IOobject.H:129
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
fieldToCell.H
Foam::fieldDictionary
Read field as dictionary (without mesh).
Definition: fieldDictionary.H:48
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:97
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:124
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:764
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::topoSetSource::setAction
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:99
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
Foam::topoSetSource::NEW
Create a new set and ADD elements to it.
Definition: topoSetSource.H:106
polyMesh.H
Foam::fieldToCell
A topoSetCellSource to select cells based on field values.
Definition: fieldToCell.H:80
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:290
Foam::topoSetSource::verbose_
bool verbose_
Verbosity (default: true)
Definition: topoSetSource.H:154
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 (uses stdout - output is on the master only)
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:66
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:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::topoSetSource::SUBTRACT
Subtract elements from the set.
Definition: topoSetSource.H:102
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOobject::headerClassName
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobjectI.H:64
Time.H
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::topoSetCellSource
Base class of a topoSet source for selecting cells.
Definition: topoSetCellSource.H:50
Foam::IOList< label >
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:246
cellSet.H
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
fieldDictionary.H
Foam::IOobject::MUST_READ
Definition: IOobject.H:120