FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/polyMesh.H>
28 #include <meshTools/cellSet.H>
29 #include <OpenFOAM/Time.H>
30 #include <OpenFOAM/IFstream.H>
31 
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 
39 defineTypeNameAndDebug(fieldToCell, 0);
40 
41 addToRunTimeSelectionTable(topoSetSource, fieldToCell, word);
42 
43 addToRunTimeSelectionTable(topoSetSource, fieldToCell, istream);
44 
45 }
46 
47 
48 Foam::topoSetSource::addToUsageTable Foam::fieldToCell::usage_
49 (
50  fieldToCell::typeName,
51  "\n Usage: fieldToCell field min max\n\n"
52  " Select all cells with field value >= min and <= max\n\n"
53 );
54 
55 
56 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
57 
58 void Foam::fieldToCell::applyToSet
59 (
60  const topoSetSource::setAction action,
61  const scalarField& field,
62  topoSet& set
63 ) const
64 {
65  Info<< " Field min:" << min(field)
66  << " max:" << max(field) << endl;
67 
68  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
69  {
70  Info<< " Adding all cells with value of field " << fieldName_
71  << " within range " << min_ << ".." << max_ << endl;
72 
73  forAll(field, cellI)
74  {
75  if (field[cellI] >= min_ && field[cellI] <= max_)
76  {
77  set.insert(cellI);
78  }
79  }
80  }
81  else if (action == topoSetSource::DELETE)
82  {
83  Info<< " Removing all cells with value of field " << fieldName_
84  << " within range " << min_ << ".." << max_ << endl;
85 
86  forAll(field, cellI)
87  {
88  if (field[cellI] >= min_ && field[cellI] <= max_)
89  {
90  set.erase(cellI);
91  }
92  }
93  }
94 }
95 
96 
97 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98 
99 // Construct from components
101 (
102  const polyMesh& mesh,
103  const word& fieldName,
104  const scalar min,
105  const scalar max
106 )
107 :
108  topoSetSource(mesh),
109  fieldName_(fieldName),
110  min_(min),
111  max_(max)
112 {}
113 
114 
115 // Construct from dictionary
117 (
118  const polyMesh& mesh,
119  const dictionary& dict
120 )
121 :
122  topoSetSource(mesh),
123  fieldName_(dict.lookup("fieldName")),
124  min_(readScalar(dict.lookup("min"))),
125  max_(readScalar(dict.lookup("max")))
126 {}
127 
128 
129 // Construct from Istream
131 (
132  const polyMesh& mesh,
133  Istream& is
134 )
135 :
136  topoSetSource(mesh),
137  fieldName_(checkIs(is)),
138  min_(readScalar(checkIs(is))),
139  max_(readScalar(checkIs(is)))
140 {}
141 
142 
143 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
144 
146 {}
147 
148 
149 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
150 
151 void Foam::fieldToCell::applyToSet
152 (
153  const topoSetSource::setAction action,
154  topoSet& set
155 ) const
156 {
157 
158 // // Construct temporary fvMesh from polyMesh
159 // fvMesh fMesh
160 // (
161 // mesh(), // IOobject
162 // mesh().points(),
163 // mesh().faces(),
164 // mesh().cells()
165 // );
166 //
167 // const polyBoundaryMesh& patches = mesh().boundaryMesh();
168 //
169 // List<polyPatch*> newPatches(patches.size());
170 // forAll(patches, patchI)
171 // {
172 // const polyPatch& pp = patches[patchI];
173 //
174 // newPatches[patchI] =
175 // patches[patchI].clone
176 // (
177 // fMesh.boundaryMesh(),
178 // patchI,
179 // pp.size(),
180 // pp.start()
181 // ).ptr();
182 // }
183 // fMesh.addFvPatches(newPatches);
184 
185  // Try to load field
187  (
188  fieldName_,
189  mesh().time().timeName(),
190  mesh(),
193  );
194 
195  if (!fieldObject.headerOk())
196  {
197  WarningIn
198  (
199  "fieldToCell::applyToSet(const topoSetSource::setAction"
200  ", topoSet& set)"
201  ) << "Cannot read field " << fieldName_
202  << " from time " << mesh().time().timeName() << endl;
203  }
204  else if (fieldObject.headerClassName() == "volScalarField")
205  {
206  IFstream str(fieldObject.filePath());
207 
208  // Read dictionary
209  dictionary fieldDict(str);
210 
211  scalarField internalVals("internalField", fieldDict, mesh().nCells());
212 
213  applyToSet(action, internalVals, set);
214  }
215  else if (fieldObject.headerClassName() == "volVectorField")
216  {
217  IFstream str(fieldObject.filePath());
218 
219  // Read dictionary
220  dictionary fieldDict(str);
221 
222  vectorField internalVals("internalField", fieldDict, mesh().nCells());
223 
224  applyToSet(action, mag(internalVals), set);
225  }
226  else
227  {
228  WarningIn
229  (
230  "fieldToCell::applyToSet(const topoSetSource::setAction"
231  ", topoSet& set)"
232  ) << "Cannot handle fields of type " << fieldObject.headerClassName()
233  << endl;
234  }
235 }
236 
237 
238 // ************************ vim: set sw=4 sts=4 et: ************************ //