Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / meshTools / sets / cellSources / fieldToCell / fieldToCell.C
blob1e9c7647e3f65eaeed7bb701829a6a27575b3ae9
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "fieldToCell.H"
27 #include "polyMesh.H"
28 #include "cellSet.H"
29 #include "Time.H"
30 #include "IFstream.H"
31 #include "fieldDictionary.H"
33 #include "addToRunTimeSelectionTable.H"
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 namespace Foam
40 defineTypeNameAndDebug(fieldToCell, 0);
42 addToRunTimeSelectionTable(topoSetSource, fieldToCell, word);
44 addToRunTimeSelectionTable(topoSetSource, fieldToCell, istream);
49 Foam::topoSetSource::addToUsageTable Foam::fieldToCell::usage_
51     fieldToCell::typeName,
52     "\n    Usage: fieldToCell field min max\n\n"
53     "    Select all cells with field value >= min and <= max\n\n"
57 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
59 void Foam::fieldToCell::applyToSet
61     const topoSetSource::setAction action,
62     const scalarField& field,
63     topoSet& set
64 ) const
66     Info<< "    Field min:" << min(field)
67         << " max:" << max(field) << endl;
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;
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;
87         forAll(field, cellI)
88         {
89             if (field[cellI] >= min_ && field[cellI] <= max_)
90             {
91                 set.erase(cellI);
92             }
93         }
94     }
98 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
100 // Construct from components
101 Foam::fieldToCell::fieldToCell
103     const polyMesh& mesh,
104     const word& fieldName,
105     const scalar min,
106     const scalar max
109     topoSetSource(mesh),
110     fieldName_(fieldName),
111     min_(min),
112     max_(max)
116 // Construct from dictionary
117 Foam::fieldToCell::fieldToCell
119     const polyMesh& mesh,
120     const dictionary& dict
123     topoSetSource(mesh),
124     fieldName_(dict.lookup("fieldName")),
125     min_(readScalar(dict.lookup("min"))),
126     max_(readScalar(dict.lookup("max")))
130 // Construct from Istream
131 Foam::fieldToCell::fieldToCell
133     const polyMesh& mesh,
134     Istream& is
137     topoSetSource(mesh),
138     fieldName_(checkIs(is)),
139     min_(readScalar(checkIs(is))),
140     max_(readScalar(checkIs(is)))
144 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
146 Foam::fieldToCell::~fieldToCell()
150 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
152 void Foam::fieldToCell::applyToSet
154     const topoSetSource::setAction action,
155     topoSet& set
156 ) const
159 //    // Construct temporary fvMesh from polyMesh
160 //    fvMesh fMesh
161 //    (
162 //        mesh(), // IOobject
163 //        mesh().points(),
164 //        mesh().faces(),
165 //        mesh().cells()
166 //    );
168 //    const polyBoundaryMesh& patches = mesh().boundaryMesh();
170 //    List<polyPatch*> newPatches(patches.size());
171 //    forAll(patches, patchI)
172 //    {
173 //        const polyPatch& pp = patches[patchI];
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);
186     // Try to load field
187     IOobject fieldObject
188     (
189         fieldName_,
190         mesh().time().timeName(),
191         mesh(),
192         IOobject::MUST_READ,
193         IOobject::AUTO_WRITE,
194         false
195     );
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());
210         // Read dictionary
211         fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
213         scalarField internalVals("internalField", fieldDict, mesh().nCells());
215         applyToSet(action, internalVals, set);
216     }
217     else if (fieldObject.headerClassName() == "volVectorField")
218     {
219         IFstream str(fieldObject.filePath());
221         // Read dictionary
222         fieldDictionary fieldDict(fieldObject, fieldObject.headerClassName());
224         vectorField internalVals("internalField", fieldDict, mesh().nCells());
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     }
240 // ************************************************************************* //