Forward compatibility: flex
[foam-extend-3.2.git] / src / meshTools / sets / cellSources / fieldToCell / fieldToCell.C
blobb1f9ad8365ec9c7cdd9bb8310abb1a9e28c36a9e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "fieldToCell.H"
27 #include "polyMesh.H"
28 #include "cellSet.H"
29 #include "foamTime.H"
30 #include "IFstream.H"
32 #include "addToRunTimeSelectionTable.H"
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 namespace Foam
39 defineTypeNameAndDebug(fieldToCell, 0);
41 addToRunTimeSelectionTable(topoSetSource, fieldToCell, word);
43 addToRunTimeSelectionTable(topoSetSource, fieldToCell, istream);
48 Foam::topoSetSource::addToUsageTable Foam::fieldToCell::usage_
50     fieldToCell::typeName,
51     "\n    Usage: fieldToCell field min max\n\n"
52     "    Select all cells with field value >= min and <= max\n\n"
56 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
58 void Foam::fieldToCell::applyToSet
60     const topoSetSource::setAction action,
61     const scalarField& field,
62     topoSet& set
63 ) const
65     Info<< "    Field min:" << min(field)
66         << " max:" << max(field) << endl;
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;
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;
86         forAll(field, cellI)
87         {
88             if (field[cellI] >= min_ && field[cellI] <= max_)
89             {
90                 set.erase(cellI);
91             }
92         }
93     }
97 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
99 // Construct from components
100 Foam::fieldToCell::fieldToCell
102     const polyMesh& mesh,
103     const word& fieldName,
104     const scalar min,
105     const scalar max
108     topoSetSource(mesh),
109     fieldName_(fieldName),
110     min_(min),
111     max_(max)
115 // Construct from dictionary
116 Foam::fieldToCell::fieldToCell
118     const polyMesh& mesh,
119     const dictionary& dict
122     topoSetSource(mesh),
123     fieldName_(dict.lookup("fieldName")),
124     min_(readScalar(dict.lookup("min"))),
125     max_(readScalar(dict.lookup("max")))
129 // Construct from Istream
130 Foam::fieldToCell::fieldToCell
132     const polyMesh& mesh,
133     Istream& is
136     topoSetSource(mesh),
137     fieldName_(checkIs(is)),
138     min_(readScalar(checkIs(is))),
139     max_(readScalar(checkIs(is)))
143 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
145 Foam::fieldToCell::~fieldToCell()
149 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
151 void Foam::fieldToCell::applyToSet
153     const topoSetSource::setAction action,
154     topoSet& set
155 ) const
158 //    // Construct temporary fvMesh from polyMesh
159 //    fvMesh fMesh
160 //    (
161 //        mesh(), // IOobject
162 //        mesh().points(),
163 //        mesh().faces(),
164 //        mesh().cells()
165 //    );
167 //    const polyBoundaryMesh& patches = mesh().boundaryMesh();
169 //    List<polyPatch*> newPatches(patches.size());
170 //    forAll(patches, patchI)
171 //    {
172 //        const polyPatch& pp = patches[patchI];
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);
185     // Try to load field
186     IOobject fieldObject
187     (
188         fieldName_,
189         mesh().time().timeName(),
190         mesh(),
191         IOobject::MUST_READ,
192         IOobject::AUTO_WRITE
193     );
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());
208         // Read dictionary
209         dictionary fieldDict(str);
211         scalarField internalVals("internalField", fieldDict, mesh().nCells());
213         applyToSet(action, internalVals, set);
214     }
215     else if (fieldObject.headerClassName() == "volVectorField")
216     {
217         IFstream str(fieldObject.filePath());
219         // Read dictionary
220         dictionary fieldDict(str);
222         vectorField internalVals("internalField", fieldDict, mesh().nCells());
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     }
238 // ************************************************************************* //