initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / preProcessing / setFields / setFields.C
blob5ba3727b39bb794d2fdeff316abc67f30589bfeb
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Selects a cell set through a dictionary.
28 \*---------------------------------------------------------------------------*/
30 #include "argList.H"
31 #include "timeSelector.H"
32 #include "Time.H"
33 #include "fvMesh.H"
34 #include "topoSetSource.H"
35 #include "cellSet.H"
36 #include "volFields.H"
38 using namespace Foam;
40 template<class GeoField>
41 void setFieldType
43     const fvMesh& mesh,
44     const labelList& selectedCells,
45     Istream& fieldValueStream
48     word fieldName(fieldValueStream);
50     IOobject fieldHeader
51     (
52         fieldName,
53         mesh.time().timeName(),
54         mesh,
55         IOobject::MUST_READ
56     );
58     // Check field exists
59     if (fieldHeader.headerOk())
60     {
61         Info<< "    Setting " << fieldHeader.headerClassName()
62             << " " << fieldName << endl;
64         GeoField field(fieldHeader, mesh);
66         typename GeoField::value_type value
67         (
68             static_cast<const typename GeoField::value_type&>
69             (
70                 pTraits<typename GeoField::value_type>(fieldValueStream)
71             )
72         );
74         if (selectedCells.size() == field.size())
75         {
76             field.internalField() = value;
77         }
78         else
79         {
80             forAll(selectedCells, celli)
81             {
82                 field[selectedCells[celli]] = value;
83             }
84         }
86         forAll(field.boundaryField(), patchi)
87         {
88             field.boundaryField()[patchi] =
89                 field.boundaryField()[patchi].patchInternalField();
90         }
92         field.write();
93     }
94     else
95     {
96         WarningIn
97         (
98             "void setFieldType"
99             "(const fvMesh& mesh, const labelList& selectedCells,"
100             "Istream& fieldValueStream)"
101         ) << "Field " << fieldName << " not found" << endl;
102     }
106 class setField
109 public:
111     setField()
112     {}
114     autoPtr<setField> clone() const
115     {
116         return autoPtr<setField>(new setField());
117     }
119     class iNew
120     {
121         const fvMesh& mesh_;
122         const labelList& selectedCells_;
124     public:
126         iNew(const fvMesh& mesh, const labelList& selectedCells)
127         :
128             mesh_(mesh),
129             selectedCells_(selectedCells)
130         {}
132         autoPtr<setField> operator()(Istream& fieldValues) const
133         {
134             word fieldType(fieldValues);
136             if (fieldType == "volScalarFieldValue")
137             {
138                 setFieldType<volScalarField>
139                     (mesh_, selectedCells_, fieldValues);
140             }
141             else if (fieldType == "volVectorFieldValue")
142             {
143                 setFieldType<volVectorField>
144                     (mesh_, selectedCells_, fieldValues);
145             }
146             else if (fieldType == "volSphericalTensorFieldValue")
147             {
148                 setFieldType<volSphericalTensorField>
149                     (mesh_, selectedCells_, fieldValues);
150             }
151             else if (fieldType == "volSymmTensorFieldValue")
152             {
153                 setFieldType<volSymmTensorField>
154                     (mesh_, selectedCells_, fieldValues);
155             }
156             else if (fieldType == "volTensorFieldValue")
157             {
158                 setFieldType<volTensorField>
159                     (mesh_, selectedCells_, fieldValues);
160             }
161             else
162             {
163                 WarningIn("setField::iNew::operator()(Istream& is)")
164                     << "field type " << fieldType << " not currently supported"
165                     << endl;
166             }
168             return autoPtr<setField>(new setField());
169         }
170     };
174 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
176 int main(int argc, char *argv[])
178     timeSelector::addOptions();
180 #   include "setRootCase.H"
181 #   include "createTime.H"
183     // Get times list
184     instantList timeDirs = timeSelector::select0(runTime, args);
186 #   include "createMesh.H"
188     Info<< "Reading setFieldsDict\n" << endl;
190     IOdictionary setFieldsDict
191     (
192         IOobject
193         (
194             "setFieldsDict",
195             runTime.system(),
196             mesh,
197             IOobject::MUST_READ,
198             IOobject::NO_WRITE
199         )
200     );
202     if (setFieldsDict.found("defaultFieldValues"))
203     {
204         Info<< "Setting field default values" << endl;
205         PtrList<setField> defaultFieldValues
206         (
207             setFieldsDict.lookup("defaultFieldValues"),
208             setField::iNew(mesh, labelList(mesh.nCells()))
209         );
210         Info<< endl;
211     }
214     Info<< "Setting field region values" << endl;
216     PtrList<entry> regions(setFieldsDict.lookup("regions"));
218     forAll(regions, regionI)
219     {
220         const entry& region = regions[regionI];
222         autoPtr<topoSetSource> cellSelector =
223             topoSetSource::New(region.keyword(), mesh, region.dict());
225         cellSet selectedCellSet
226         (
227             mesh,
228             "cellSet",
229             mesh.nCells()/10+1  // Reasonable size estimate.
230         );
232         cellSelector->applyToSet
233         (
234             topoSetSource::NEW,
235             selectedCellSet
236         );
238         PtrList<setField> fieldValues
239         (
240             region.dict().lookup("fieldValues"),
241             setField::iNew(mesh, selectedCellSet.toc())
242         );
243     }
245     Info<< "\nEnd" << endl;
247     return 0;
251 // ************************************************************************* //