OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "fieldToCell.H"
00030 #include "polyMesh.H"
00031 #include "cellSet.H"
00032 #include "Time.H"
00033 #include "IFstream.H"
00034 
00035 #include "addToRunTimeSelectionTable.H"
00036 
00037 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00038 
00039 namespace Foam
00040 {
00041 
00042 defineTypeNameAndDebug(fieldToCell, 0);
00043 
00044 addToRunTimeSelectionTable(topoSetSource, fieldToCell, word);
00045 
00046 addToRunTimeSelectionTable(topoSetSource, fieldToCell, istream);
00047 
00048 }
00049 
00050 
00051 Foam::topoSetSource::addToUsageTable Foam::fieldToCell::usage_
00052 (
00053     fieldToCell::typeName,
00054     "\n    Usage: fieldToCell field min max\n\n"
00055     "    Select all cells with field value >= min and <= max\n\n"
00056 );
00057 
00058 
00059 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00060 
00061 void Foam::fieldToCell::applyToSet
00062 (
00063     const topoSetSource::setAction action,
00064     const scalarField& field,
00065     topoSet& set
00066 ) const
00067 {
00068     Info<< "    Field min:" << min(field)
00069         << " max:" << max(field) << endl;
00070 
00071     if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
00072     {
00073         Info<< "    Adding all cells with value of field " << fieldName_
00074             << " within range " << min_ << ".." << max_ << endl;
00075 
00076         forAll(field, cellI)
00077         {
00078             if (field[cellI] >= min_ && field[cellI] <= max_)
00079             {
00080                 set.insert(cellI);
00081             }
00082         }
00083     }
00084     else if (action == topoSetSource::DELETE)
00085     {
00086         Info<< "    Removing all cells with value of field " << fieldName_
00087             << " within range " << min_ << ".." << max_ << endl;
00088 
00089         forAll(field, cellI)
00090         {
00091             if (field[cellI] >= min_ && field[cellI] <= max_)
00092             {
00093                 set.erase(cellI);
00094             }
00095         }
00096     }
00097 }
00098 
00099 
00100 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00101 
00102 // Construct from components
00103 Foam::fieldToCell::fieldToCell
00104 (
00105     const polyMesh& mesh,
00106     const word& fieldName,
00107     const scalar min,
00108     const scalar max
00109 )
00110 :
00111     topoSetSource(mesh),
00112     fieldName_(fieldName),
00113     min_(min),
00114     max_(max)
00115 {}
00116 
00117 
00118 // Construct from dictionary
00119 Foam::fieldToCell::fieldToCell
00120 (
00121     const polyMesh& mesh,
00122     const dictionary& dict
00123 )
00124 :
00125     topoSetSource(mesh),
00126     fieldName_(dict.lookup("fieldName")),
00127     min_(readScalar(dict.lookup("min"))),
00128     max_(readScalar(dict.lookup("max")))
00129 {}
00130 
00131 
00132 // Construct from Istream
00133 Foam::fieldToCell::fieldToCell
00134 (
00135     const polyMesh& mesh,
00136     Istream& is
00137 )
00138 :
00139     topoSetSource(mesh),
00140     fieldName_(checkIs(is)),
00141     min_(readScalar(checkIs(is))),
00142     max_(readScalar(checkIs(is)))
00143 {}
00144 
00145 
00146 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00147 
00148 Foam::fieldToCell::~fieldToCell()
00149 {}
00150 
00151 
00152 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00153 
00154 void Foam::fieldToCell::applyToSet
00155 (
00156     const topoSetSource::setAction action,
00157     topoSet& set
00158 ) const
00159 {
00160 
00161 //    // Construct temporary fvMesh from polyMesh
00162 //    fvMesh fMesh
00163 //    (
00164 //        mesh(), // IOobject
00165 //        mesh().points(),
00166 //        mesh().faces(),
00167 //        mesh().cells()
00168 //    );
00169 //
00170 //    const polyBoundaryMesh& patches = mesh().boundaryMesh();
00171 //
00172 //    List<polyPatch*> newPatches(patches.size());
00173 //    forAll(patches, patchI)
00174 //    {
00175 //        const polyPatch& pp = patches[patchI];
00176 //
00177 //        newPatches[patchI] =
00178 //            patches[patchI].clone
00179 //            (
00180 //                fMesh.boundaryMesh(),
00181 //                patchI,
00182 //                pp.size(),
00183 //                pp.start()
00184 //            ).ptr();
00185 //    }
00186 //    fMesh.addFvPatches(newPatches);
00187 
00188     // Try to load field
00189     IOobject fieldObject
00190     (
00191         fieldName_,
00192         mesh().time().timeName(),
00193         mesh(),
00194         IOobject::MUST_READ,
00195         IOobject::AUTO_WRITE
00196     );
00197 
00198     if (!fieldObject.headerOk())
00199     {
00200         WarningIn
00201         (
00202             "fieldToCell::applyToSet(const topoSetSource::setAction"
00203             ", topoSet& set)"
00204         )   << "Cannot read field " << fieldName_
00205             << " from time " << mesh().time().timeName() << endl;
00206     }
00207     else if (fieldObject.headerClassName() == "volScalarField")
00208     {
00209         IFstream str(fieldObject.filePath());
00210 
00211         // Read dictionary
00212         dictionary fieldDict(str);
00213 
00214         scalarField internalVals("internalField", fieldDict, mesh().nCells());
00215 
00216         applyToSet(action, internalVals, set);
00217     }
00218     else if (fieldObject.headerClassName() == "volVectorField")
00219     {
00220         IFstream str(fieldObject.filePath());
00221 
00222         // Read dictionary
00223         dictionary fieldDict(str);
00224 
00225         vectorField internalVals("internalField", fieldDict, mesh().nCells());
00226 
00227         applyToSet(action, mag(internalVals), set);
00228     }
00229     else
00230     {
00231         WarningIn
00232         (
00233             "fieldToCell::applyToSet(const topoSetSource::setAction"
00234             ", topoSet& set)"
00235         )   << "Cannot handle fields of type " << fieldObject.headerClassName()
00236             << endl;
00237     }
00238 }
00239 
00240 
00241 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd