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

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-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     Selects a cell set through a dictionary.
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "argList.H"
00031 #include "timeSelector.H"
00032 #include "Time.H"
00033 #include "fvMesh.H"
00034 #include "topoSetSource.H"
00035 #include "cellSet.H"
00036 #include "volFields.H"
00037 
00038 using namespace Foam;
00039 
00040 template<class GeoField>
00041 void setFieldType
00042 (
00043     const fvMesh& mesh,
00044     const labelList& selectedCells,
00045     Istream& fieldValueStream
00046 )
00047 {
00048     word fieldName(fieldValueStream);
00049 
00050     IOobject fieldHeader
00051     (
00052         fieldName,
00053         mesh.time().timeName(),
00054         mesh,
00055         IOobject::MUST_READ
00056     );
00057 
00058     // Check field exists
00059     if (fieldHeader.headerOk())
00060     {
00061         Info<< "    Setting " << fieldHeader.headerClassName()
00062             << " " << fieldName << endl;
00063 
00064         GeoField field(fieldHeader, mesh);
00065 
00066         typename GeoField::value_type value
00067         (
00068             static_cast<const typename GeoField::value_type&>
00069             (
00070                 pTraits<typename GeoField::value_type>(fieldValueStream)
00071             )
00072         );
00073 
00074         if (selectedCells.size() == field.size())
00075         {
00076             field.internalField() = value;
00077         }
00078         else
00079         {
00080             forAll(selectedCells, celli)
00081             {
00082                 field[selectedCells[celli]] = value;
00083             }
00084         }
00085 
00086         forAll(field.boundaryField(), patchi)
00087         {
00088             field.boundaryField()[patchi] =
00089                 field.boundaryField()[patchi].patchInternalField();
00090         }
00091 
00092         field.write();
00093     }
00094     else
00095     {
00096         WarningIn
00097         (
00098             "void setFieldType"
00099             "(const fvMesh& mesh, const labelList& selectedCells,"
00100             "Istream& fieldValueStream)"
00101         ) << "Field " << fieldName << " not found" << endl;
00102     }
00103 }
00104 
00105 
00106 class setField
00107 {
00108 
00109 public:
00110 
00111     setField()
00112     {}
00113 
00114     autoPtr<setField> clone() const
00115     {
00116         return autoPtr<setField>(new setField());
00117     }
00118 
00119     class iNew
00120     {
00121         const fvMesh& mesh_;
00122         const labelList& selectedCells_;
00123 
00124     public:
00125 
00126         iNew(const fvMesh& mesh, const labelList& selectedCells)
00127         :
00128             mesh_(mesh),
00129             selectedCells_(selectedCells)
00130         {}
00131 
00132         autoPtr<setField> operator()(Istream& fieldValues) const
00133         {
00134             word fieldType(fieldValues);
00135 
00136             if (fieldType == "volScalarFieldValue")
00137             {
00138                 setFieldType<volScalarField>
00139                     (mesh_, selectedCells_, fieldValues);
00140             }
00141             else if (fieldType == "volVectorFieldValue")
00142             {
00143                 setFieldType<volVectorField>
00144                     (mesh_, selectedCells_, fieldValues);
00145             }
00146             else if (fieldType == "volSphericalTensorFieldValue")
00147             {
00148                 setFieldType<volSphericalTensorField>
00149                     (mesh_, selectedCells_, fieldValues);
00150             }
00151             else if (fieldType == "volSymmTensorFieldValue")
00152             {
00153                 setFieldType<volSymmTensorField>
00154                     (mesh_, selectedCells_, fieldValues);
00155             }
00156             else if (fieldType == "volTensorFieldValue")
00157             {
00158                 setFieldType<volTensorField>
00159                     (mesh_, selectedCells_, fieldValues);
00160             }
00161             else
00162             {
00163                 WarningIn("setField::iNew::operator()(Istream& is)")
00164                     << "field type " << fieldType << " not currently supported"
00165                     << endl;
00166             }
00167 
00168             return autoPtr<setField>(new setField());
00169         }
00170     };
00171 };
00172 
00173 
00174 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00175 
00176 int main(int argc, char *argv[])
00177 {
00178     timeSelector::addOptions();
00179 
00180 #   include "setRootCase.H"
00181 #   include "createTime.H"
00182 
00183     // Get times list
00184     instantList timeDirs = timeSelector::select0(runTime, args);
00185 
00186 #   include "createMesh.H"
00187 
00188     Info<< "Reading setFieldsDict\n" << endl;
00189 
00190     IOdictionary setFieldsDict
00191     (
00192         IOobject
00193         (
00194             "setFieldsDict",
00195             runTime.system(),
00196             mesh,
00197             IOobject::MUST_READ,
00198             IOobject::NO_WRITE
00199         )
00200     );
00201 
00202     if (setFieldsDict.found("defaultFieldValues"))
00203     {
00204         Info<< "Setting field default values" << endl;
00205         PtrList<setField> defaultFieldValues
00206         (
00207             setFieldsDict.lookup("defaultFieldValues"),
00208             setField::iNew(mesh, labelList(mesh.nCells()))
00209         );
00210         Info<< endl;
00211     }
00212 
00213 
00214     Info<< "Setting field region values" << endl;
00215 
00216     PtrList<entry> regions(setFieldsDict.lookup("regions"));
00217 
00218     forAll(regions, regionI)
00219     {
00220         const entry& region = regions[regionI];
00221 
00222         autoPtr<topoSetSource> cellSelector =
00223             topoSetSource::New(region.keyword(), mesh, region.dict());
00224 
00225         cellSet selectedCellSet
00226         (
00227             mesh,
00228             "cellSet",
00229             mesh.nCells()/10+1  // Reasonable size estimate.
00230         );
00231 
00232         cellSelector->applyToSet
00233         (
00234             topoSetSource::NEW,
00235             selectedCellSet
00236         );
00237 
00238         PtrList<setField> fieldValues
00239         (
00240             region.dict().lookup("fieldValues"),
00241             setField::iNew(mesh, selectedCellSet.toc())
00242         );
00243     }
00244 
00245     Info<< "\nEnd" << endl;
00246 
00247     return 0;
00248 }
00249 
00250 
00251 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd