Forward compatibility: flex
[foam-extend-3.2.git] / applications / utilities / preProcessing / setFields / setFields.C
blob2f016746802d41323703afcbfaebcb63890b26b9
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Selects a cell set through a dictionary.
27 \*---------------------------------------------------------------------------*/
29 #include "argList.H"
30 #include "timeSelector.H"
31 #include "objectRegistry.H"
32 #include "foamTime.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     // Read field and value together; otherwise there will be an input error
49     // when a field is not found.  HJ, 3/Aug/2011
50     word fieldName(fieldValueStream);
52     typename GeoField::value_type value
53     (
54         static_cast<const typename GeoField::value_type&>
55         (
56             pTraits<typename GeoField::value_type>(fieldValueStream)
57         )
58     );
60     IOobject fieldHeader
61     (
62         fieldName,
63         mesh.time().timeName(),
64         mesh,
65         IOobject::MUST_READ
66     );
68     // Check field exists
69     if (fieldHeader.headerOk())
70     {
71         Info<< "    Setting " << fieldHeader.headerClassName()
72             << " " << fieldName << endl;
74         GeoField field(fieldHeader, mesh);
76         if (selectedCells.size() == field.size())
77         {
78             field.internalField() = value;
79         }
80         else
81         {
82             forAll (selectedCells, celli)
83             {
84                 field[selectedCells[celli]] = value;
85             }
86         }
88         forAll (field.boundaryField(), patchi)
89         {
90             // Forced patch assignment.  HJ, 1/Aug/2010
91             field.boundaryField()[patchi] ==
92                 field.boundaryField()[patchi].patchInternalField();
93         }
95         field.write();
96     }
97     else
98     {
99         WarningIn
100         (
101             "void setFieldType"
102             "(const fvMesh& mesh, const labelList& selectedCells,"
103             "Istream& fieldValueStream)"
104         ) << "Field " << fieldName << " not found" << endl;
105     }
109 class setField
112 public:
114     setField()
115     {}
117     autoPtr<setField> clone() const
118     {
119         return autoPtr<setField>(new setField());
120     }
122     class iNew
123     {
124         const fvMesh& mesh_;
125         const labelList& selectedCells_;
127     public:
129         iNew(const fvMesh& mesh, const labelList& selectedCells)
130         :
131             mesh_(mesh),
132             selectedCells_(selectedCells)
133         {}
135         autoPtr<setField> operator()(Istream& fieldValues) const
136         {
137             word fieldType(fieldValues);
139             if (fieldType == "volScalarFieldValue")
140             {
141                 setFieldType<volScalarField>
142                     (mesh_, selectedCells_, fieldValues);
143             }
144             else if (fieldType == "volVectorFieldValue")
145             {
146                 setFieldType<volVectorField>
147                     (mesh_, selectedCells_, fieldValues);
148             }
149             else if (fieldType == "volSphericalTensorFieldValue")
150             {
151                 setFieldType<volSphericalTensorField>
152                     (mesh_, selectedCells_, fieldValues);
153             }
154             else if (fieldType == "volSymmTensorFieldValue")
155             {
156                 setFieldType<volSymmTensorField>
157                     (mesh_, selectedCells_, fieldValues);
158             }
159             else if (fieldType == "volTensorFieldValue")
160             {
161                 setFieldType<volTensorField>
162                     (mesh_, selectedCells_, fieldValues);
163             }
164             else
165             {
166                 WarningIn("setField::iNew::operator()(Istream& is)")
167                     << "field type " << fieldType << " not currently supported"
168                     << endl;
169             }
171             return autoPtr<setField>(new setField());
172         }
173     };
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 int main(int argc, char *argv[])
181 #   include "setRootCase.H"
182 #   include "createTime.H"
184     Info<< "Time = " << runTime.timeName() << endl;
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 // ************************************************************************* //