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 | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "fieldToCell.H"
27 #include "polyMesh.H"
28 #include "cellSet.H"
29 #include "Time.H"
30 #include "IFstream.H"
31 #include "fieldDictionary.H"
32 
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 
40 defineTypeNameAndDebug(fieldToCell, 0);
41 
42 addToRunTimeSelectionTable(topoSetSource, fieldToCell, word);
43 
44 addToRunTimeSelectionTable(topoSetSource, fieldToCell, istream);
45 
46 }
47 
48 
49 Foam::topoSetSource::addToUsageTable Foam::fieldToCell::usage_
50 (
51  fieldToCell::typeName,
52  "\n Usage: fieldToCell field min max\n\n"
53  " Select all cells with field value >= min and <= max\n\n"
54 );
55 
56 
57 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
58 
59 void Foam::fieldToCell::applyToSet
60 (
61  const topoSetSource::setAction action,
62  const scalarField& field,
63  topoSet& set
64 ) const
65 {
66  Info<< " Field min:" << min(field)
67  << " max:" << max(field) << endl;
68 
69  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
70  {
71  Info<< " Adding all cells with value of field " << fieldName_
72  << " within range " << min_ << ".." << max_ << endl;
73 
74  forAll(field, cellI)
75  {
76  if (field[cellI] >= min_ && field[cellI] <= max_)
77  {
78  set.insert(cellI);
79  }
80  }
81  }
82  else if (action == topoSetSource::DELETE)
83  {
84  Info<< " Removing all cells with value of field " << fieldName_
85  << " within range " << min_ << ".." << max_ << endl;
86 
87  forAll(field, cellI)
88  {
89  if (field[cellI] >= min_ && field[cellI] <= max_)
90  {
91  set.erase(cellI);
92  }
93  }
94  }
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
100 // Construct from components
102 (
103  const polyMesh& mesh,
104  const word& fieldName,
105  const scalar min,
106  const scalar max
107 )
108 :
109  topoSetSource(mesh),
110  fieldName_(fieldName),
111  min_(min),
112  max_(max)
113 {}
114 
115 
116 // Construct from dictionary
118 (
119  const polyMesh& mesh,
120  const dictionary& dict
121 )
122 :
123  topoSetSource(mesh),
124  fieldName_(dict.lookup("fieldName")),
125  min_(readScalar(dict.lookup("min"))),
126  max_(readScalar(dict.lookup("max")))
127 {}
128 
129 
130 // Construct from Istream
132 (
133  const polyMesh& mesh,
134  Istream& is
135 )
136 :
137  topoSetSource(mesh),
138  fieldName_(checkIs(is)),
139  min_(readScalar(checkIs(is))),
140  max_(readScalar(checkIs(is)))
141 {}
142 
143 
144 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
145 
147 {}
148 
149 
150 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
151 
152 void Foam::fieldToCell::applyToSet
153 (
154  const topoSetSource::setAction action,
155  topoSet& set
156 ) const
157 {
158 
159 // // Construct temporary fvMesh from polyMesh
160 // fvMesh fMesh
161 // (
162 // mesh(), // IOobject
163 // mesh().points(),
164 // mesh().faces(),
165 // mesh().cells()
166 // );
167 //
168 // const polyBoundaryMesh& patches = mesh().boundaryMesh();
169 //
170 // List<polyPatch*> newPatches(patches.size());
171 // forAll(patches, patchI)
172 // {
173 // const polyPatch& pp = patches[patchI];
174 //
175 // newPatches[patchI] =
176 // patches[patchI].clone
177 // (
178 // fMesh.boundaryMesh(),
179 // patchI,
180 // pp.size(),
181 // pp.start()
182 // ).ptr();
183 // }
184 // fMesh.addFvPatches(newPatches);
185 
186  // Try to load field
188  (
189  fieldName_,
190  mesh().time().timeName(),
191  mesh(),
194  false
195  );
196 
197  if (!fieldObject.headerOk())
198  {
199  WarningIn
200  (
201  "fieldToCell::applyToSet(const topoSetSource::setAction"
202  ", topoSet& set)"
203  ) << "Cannot read field " << fieldName_
204  << " from time " << mesh().time().timeName() << endl;
205  }
206  else if (fieldObject.headerClassName() == "volScalarField")
207  {
208  IFstream str(fieldObject.filePath());
209 
210  // Read dictionary
211  fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
212 
213  scalarField internalVals("internalField", fieldDict, mesh().nCells());
214 
215  applyToSet(action, internalVals, set);
216  }
217  else if (fieldObject.headerClassName() == "volVectorField")
218  {
219  IFstream str(fieldObject.filePath());
220 
221  // Read dictionary
222  fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
223 
224  vectorField internalVals("internalField", fieldDict, mesh().nCells());
225 
226  applyToSet(action, mag(internalVals), set);
227  }
228  else
229  {
230  WarningIn
231  (
232  "fieldToCell::applyToSet(const topoSetSource::setAction"
233  ", topoSet& set)"
234  ) << "Cannot handle fields of type " << fieldObject.headerClassName()
235  << endl;
236  }
237 }
238 
239 
240 // ************************************************************************* //
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:266
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:323
virtual ~fieldToCell()
Destructor.
Definition: fieldToCell.C:146
IOobject fieldObject(fieldNames[var2field[nVar]], runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::NO_WRITE)
dimensioned< scalar > mag(const dimensioned< Type > &)
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:61
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A class for handling words, derived from string.
Definition: word.H:59
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:741
messageStream Info
Read field as dictionary (without mesh).
dynamicFvMesh & mesh
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
Input from file stream.
Definition: IFstream.H:81
Namespace for OpenFOAM.
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:243
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
#define WarningIn(functionName)
Report a warning using Foam::Warning.
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define forAll(list, i)
Definition: UList.H:421
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:82
Macros for easy insertion into run-time selection tables.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
Base class of a source for a topoSet.
Definition: topoSetSource.H:63
bool headerOk()
Read and check header info.
Definition: IOobject.C:424
Class with constructor to add usage string to table.
fieldToCell(const polyMesh &mesh, const word &fieldName, const scalar min, const scalar max)
Construct from components.
Definition: fieldToCell.C:102
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
word timeName
Definition: getTimeIndex.H:3