FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

fieldToCell.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "fieldToCell.H"
00027 #include <OpenFOAM/polyMesh.H>
00028 #include <meshTools/cellSet.H>
00029 #include <OpenFOAM/Time.H>
00030 #include <OpenFOAM/IFstream.H>
00031 
00032 #include <OpenFOAM/addToRunTimeSelectionTable.H>
00033 
00034 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00035 
00036 namespace Foam
00037 {
00038 
00039 defineTypeNameAndDebug(fieldToCell, 0);
00040 
00041 addToRunTimeSelectionTable(topoSetSource, fieldToCell, word);
00042 
00043 addToRunTimeSelectionTable(topoSetSource, fieldToCell, istream);
00044 
00045 }
00046 
00047 
00048 Foam::topoSetSource::addToUsageTable Foam::fieldToCell::usage_
00049 (
00050     fieldToCell::typeName,
00051     "\n    Usage: fieldToCell field min max\n\n"
00052     "    Select all cells with field value >= min and <= max\n\n"
00053 );
00054 
00055 
00056 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00057 
00058 void Foam::fieldToCell::applyToSet
00059 (
00060     const topoSetSource::setAction action,
00061     const scalarField& field,
00062     topoSet& set
00063 ) const
00064 {
00065     Info<< "    Field min:" << min(field)
00066         << " max:" << max(field) << endl;
00067 
00068     if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
00069     {
00070         Info<< "    Adding all cells with value of field " << fieldName_
00071             << " within range " << min_ << ".." << max_ << endl;
00072 
00073         forAll(field, cellI)
00074         {
00075             if (field[cellI] >= min_ && field[cellI] <= max_)
00076             {
00077                 set.insert(cellI);
00078             }
00079         }
00080     }
00081     else if (action == topoSetSource::DELETE)
00082     {
00083         Info<< "    Removing all cells with value of field " << fieldName_
00084             << " within range " << min_ << ".." << max_ << endl;
00085 
00086         forAll(field, cellI)
00087         {
00088             if (field[cellI] >= min_ && field[cellI] <= max_)
00089             {
00090                 set.erase(cellI);
00091             }
00092         }
00093     }
00094 }
00095 
00096 
00097 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00098 
00099 // Construct from components
00100 Foam::fieldToCell::fieldToCell
00101 (
00102     const polyMesh& mesh,
00103     const word& fieldName,
00104     const scalar min,
00105     const scalar max
00106 )
00107 :
00108     topoSetSource(mesh),
00109     fieldName_(fieldName),
00110     min_(min),
00111     max_(max)
00112 {}
00113 
00114 
00115 // Construct from dictionary
00116 Foam::fieldToCell::fieldToCell
00117 (
00118     const polyMesh& mesh,
00119     const dictionary& dict
00120 )
00121 :
00122     topoSetSource(mesh),
00123     fieldName_(dict.lookup("fieldName")),
00124     min_(readScalar(dict.lookup("min"))),
00125     max_(readScalar(dict.lookup("max")))
00126 {}
00127 
00128 
00129 // Construct from Istream
00130 Foam::fieldToCell::fieldToCell
00131 (
00132     const polyMesh& mesh,
00133     Istream& is
00134 )
00135 :
00136     topoSetSource(mesh),
00137     fieldName_(checkIs(is)),
00138     min_(readScalar(checkIs(is))),
00139     max_(readScalar(checkIs(is)))
00140 {}
00141 
00142 
00143 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00144 
00145 Foam::fieldToCell::~fieldToCell()
00146 {}
00147 
00148 
00149 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00150 
00151 void Foam::fieldToCell::applyToSet
00152 (
00153     const topoSetSource::setAction action,
00154     topoSet& set
00155 ) const
00156 {
00157 
00158 //    // Construct temporary fvMesh from polyMesh
00159 //    fvMesh fMesh
00160 //    (
00161 //        mesh(), // IOobject
00162 //        mesh().points(),
00163 //        mesh().faces(),
00164 //        mesh().cells()
00165 //    );
00166 //
00167 //    const polyBoundaryMesh& patches = mesh().boundaryMesh();
00168 //
00169 //    List<polyPatch*> newPatches(patches.size());
00170 //    forAll(patches, patchI)
00171 //    {
00172 //        const polyPatch& pp = patches[patchI];
00173 //
00174 //        newPatches[patchI] =
00175 //            patches[patchI].clone
00176 //            (
00177 //                fMesh.boundaryMesh(),
00178 //                patchI,
00179 //                pp.size(),
00180 //                pp.start()
00181 //            ).ptr();
00182 //    }
00183 //    fMesh.addFvPatches(newPatches);
00184 
00185     // Try to load field
00186     IOobject fieldObject
00187     (
00188         fieldName_,
00189         mesh().time().timeName(),
00190         mesh(),
00191         IOobject::MUST_READ,
00192         IOobject::AUTO_WRITE
00193     );
00194 
00195     if (!fieldObject.headerOk())
00196     {
00197         WarningIn
00198         (
00199             "fieldToCell::applyToSet(const topoSetSource::setAction"
00200             ", topoSet& set)"
00201         )   << "Cannot read field " << fieldName_
00202             << " from time " << mesh().time().timeName() << endl;
00203     }
00204     else if (fieldObject.headerClassName() == "volScalarField")
00205     {
00206         IFstream str(fieldObject.filePath());
00207 
00208         // Read dictionary
00209         dictionary fieldDict(str);
00210 
00211         scalarField internalVals("internalField", fieldDict, mesh().nCells());
00212 
00213         applyToSet(action, internalVals, set);
00214     }
00215     else if (fieldObject.headerClassName() == "volVectorField")
00216     {
00217         IFstream str(fieldObject.filePath());
00218 
00219         // Read dictionary
00220         dictionary fieldDict(str);
00221 
00222         vectorField internalVals("internalField", fieldDict, mesh().nCells());
00223 
00224         applyToSet(action, mag(internalVals), set);
00225     }
00226     else
00227     {
00228         WarningIn
00229         (
00230             "fieldToCell::applyToSet(const topoSetSource::setAction"
00231             ", topoSet& set)"
00232         )   << "Cannot handle fields of type " << fieldObject.headerClassName()
00233             << endl;
00234     }
00235 }
00236 
00237 
00238 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines