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

setFields.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 Application
00025     setFields
00026 
00027 Description
00028     Selects a cell set through a dictionary.
00029 
00030 Usage
00031 
00032     - setFields [OPTIONS]
00033 
00034     @param -noZero \n
00035     Ignore timestep 0.
00036 
00037     @param -constant \n
00038     Include the constant directory.
00039 
00040     @param -time <time>\n
00041     Apply only to specific time.
00042 
00043     @param -latestTime \n
00044     Only apply to latest time step.
00045 
00046     @param -case <dir>\n
00047     Case directory.
00048 
00049     @param -parallel \n
00050     Run in parallel.
00051 
00052     @param -help \n
00053     Display help message.
00054 
00055     @param -doc \n
00056     Display Doxygen API documentation page for this application.
00057 
00058     @param -srcDoc \n
00059     Display Doxygen source documentation page for this application.
00060 
00061 \*---------------------------------------------------------------------------*/
00062 
00063 #include <OpenFOAM/argList.H>
00064 #include <OpenFOAM/timeSelector.H>
00065 #include <OpenFOAM/Time.H>
00066 #include <finiteVolume/fvMesh.H>
00067 #include <meshTools/topoSetSource.H>
00068 #include <meshTools/cellSet.H>
00069 #include <finiteVolume/volFields.H>
00070 
00071 using namespace Foam;
00072 
00073 template<class GeoField>
00074 void setFieldType
00075 (
00076     const fvMesh& mesh,
00077     const labelList& selectedCells,
00078     Istream& fieldValueStream
00079 )
00080 {
00081     word fieldName(fieldValueStream);
00082 
00083     IOobject fieldHeader
00084     (
00085         fieldName,
00086         mesh.time().timeName(),
00087         mesh,
00088         IOobject::MUST_READ
00089     );
00090 
00091     // Check field exists
00092     if (fieldHeader.headerOk())
00093     {
00094         Info<< "    Setting " << fieldHeader.headerClassName()
00095             << " " << fieldName << endl;
00096 
00097         GeoField field(fieldHeader, mesh);
00098 
00099         typename GeoField::value_type value
00100         (
00101             static_cast<const typename GeoField::value_type&>
00102             (
00103                 pTraits<typename GeoField::value_type>(fieldValueStream)
00104             )
00105         );
00106 
00107         if (selectedCells.size() == field.size())
00108         {
00109             field.internalField() = value;
00110         }
00111         else
00112         {
00113             forAll(selectedCells, celli)
00114             {
00115                 field[selectedCells[celli]] = value;
00116             }
00117         }
00118 
00119         forAll(field.boundaryField(), patchi)
00120         {
00121             field.boundaryField()[patchi] =
00122                 field.boundaryField()[patchi].patchInternalField();
00123         }
00124 
00125         field.write();
00126     }
00127     else
00128     {
00129         WarningIn
00130         (
00131             "void setFieldType"
00132             "(const fvMesh& mesh, const labelList& selectedCells,"
00133             "Istream& fieldValueStream)"
00134         ) << "Field " << fieldName << " not found" << endl;
00135     }
00136 }
00137 
00138 
00139 class setField
00140 {
00141 
00142 public:
00143 
00144     setField()
00145     {}
00146 
00147     autoPtr<setField> clone() const
00148     {
00149         return autoPtr<setField>(new setField());
00150     }
00151 
00152     class iNew
00153     {
00154         const fvMesh& mesh_;
00155         const labelList& selectedCells_;
00156 
00157     public:
00158 
00159         iNew(const fvMesh& mesh, const labelList& selectedCells)
00160         :
00161             mesh_(mesh),
00162             selectedCells_(selectedCells)
00163         {}
00164 
00165         autoPtr<setField> operator()(Istream& fieldValues) const
00166         {
00167             word fieldType(fieldValues);
00168 
00169             if (fieldType == "volScalarFieldValue")
00170             {
00171                 setFieldType<volScalarField>
00172                     (mesh_, selectedCells_, fieldValues);
00173             }
00174             else if (fieldType == "volVectorFieldValue")
00175             {
00176                 setFieldType<volVectorField>
00177                     (mesh_, selectedCells_, fieldValues);
00178             }
00179             else if (fieldType == "volSphericalTensorFieldValue")
00180             {
00181                 setFieldType<volSphericalTensorField>
00182                     (mesh_, selectedCells_, fieldValues);
00183             }
00184             else if (fieldType == "volSymmTensorFieldValue")
00185             {
00186                 setFieldType<volSymmTensorField>
00187                     (mesh_, selectedCells_, fieldValues);
00188             }
00189             else if (fieldType == "volTensorFieldValue")
00190             {
00191                 setFieldType<volTensorField>
00192                     (mesh_, selectedCells_, fieldValues);
00193             }
00194             else
00195             {
00196                 WarningIn("setField::iNew::operator()(Istream& is)")
00197                     << "field type " << fieldType << " not currently supported"
00198                     << endl;
00199             }
00200 
00201             return autoPtr<setField>(new setField());
00202         }
00203     };
00204 };
00205 
00206 
00207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00208 
00209 int main(int argc, char *argv[])
00210 {
00211     timeSelector::addOptions();
00212 
00213 #   include <OpenFOAM/setRootCase.H>
00214 #   include <OpenFOAM/createTime.H>
00215 
00216     // Get times list
00217     instantList timeDirs = timeSelector::select0(runTime, args);
00218 
00219 #   include <OpenFOAM/createMesh.H>
00220 
00221     Info<< "Reading setFieldsDict\n" << endl;
00222 
00223     IOdictionary setFieldsDict
00224     (
00225         IOobject
00226         (
00227             "setFieldsDict",
00228             runTime.system(),
00229             mesh,
00230             IOobject::MUST_READ,
00231             IOobject::NO_WRITE
00232         )
00233     );
00234 
00235     if (setFieldsDict.found("defaultFieldValues"))
00236     {
00237         Info<< "Setting field default values" << endl;
00238         PtrList<setField> defaultFieldValues
00239         (
00240             setFieldsDict.lookup("defaultFieldValues"),
00241             setField::iNew(mesh, labelList(mesh.nCells()))
00242         );
00243         Info<< endl;
00244     }
00245 
00246 
00247     Info<< "Setting field region values" << endl;
00248 
00249     PtrList<entry> regions(setFieldsDict.lookup("regions"));
00250 
00251     forAll(regions, regionI)
00252     {
00253         const entry& region = regions[regionI];
00254 
00255         autoPtr<topoSetSource> cellSelector =
00256             topoSetSource::New(region.keyword(), mesh, region.dict());
00257 
00258         cellSet selectedCellSet
00259         (
00260             mesh,
00261             "cellSet",
00262             mesh.nCells()/10+1  // Reasonable size estimate.
00263         );
00264 
00265         cellSelector->applyToSet
00266         (
00267             topoSetSource::NEW,
00268             selectedCellSet
00269         );
00270 
00271         PtrList<setField> fieldValues
00272         (
00273             region.dict().lookup("fieldValues"),
00274             setField::iNew(mesh, selectedCellSet.toc())
00275         );
00276     }
00277 
00278     Info<< "\nEnd" << endl;
00279 
00280     return 0;
00281 }
00282 
00283 
00284 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines