1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 Set values on a selected set of cells/patchfaces through a dictionary.
27 \*---------------------------------------------------------------------------*/
30 #include "timeSelector.H"
33 #include "topoSetSource.H"
36 #include "volFields.H"
43 const word& fieldTypeDesc,
45 const labelList& selectedCells,
46 Istream& fieldValueStream
49 typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
51 if (fieldTypeDesc != fieldType::typeName + "Value")
56 word fieldName(fieldValueStream);
61 mesh.time().timeName(),
67 if (fieldHeader.headerOk())
69 Info<< " Setting internal values of "
70 << fieldHeader.headerClassName()
71 << " " << fieldName << endl;
73 fieldType field(fieldHeader, mesh);
75 const Type& value = pTraits<Type>(fieldValueStream);
77 if (selectedCells.size() == field.size())
79 field.internalField() = value;
83 forAll(selectedCells, celli)
85 field[selectedCells[celli]] = value;
89 forAll(field.boundaryField(), patchi)
91 field.boundaryField()[patchi] =
92 field.boundaryField()[patchi].patchInternalField();
101 "void setCellFieldType"
102 "(const fvMesh& mesh, const labelList& selectedCells,"
103 "Istream& fieldValueStream)"
104 ) << "Field " << fieldName << " not found" << endl;
119 autoPtr<setCellField> clone() const
121 return autoPtr<setCellField>(new setCellField());
127 const labelList& selectedCells_;
131 iNew(const fvMesh& mesh, const labelList& selectedCells)
134 selectedCells_(selectedCells)
137 autoPtr<setCellField> operator()(Istream& fieldValues) const
139 word fieldType(fieldValues);
144 setCellFieldType<scalar>
145 (fieldType, mesh_, selectedCells_, fieldValues)
146 || setCellFieldType<vector>
147 (fieldType, mesh_, selectedCells_, fieldValues)
148 || setCellFieldType<sphericalTensor>
149 (fieldType, mesh_, selectedCells_, fieldValues)
150 || setCellFieldType<symmTensor>
151 (fieldType, mesh_, selectedCells_, fieldValues)
152 || setCellFieldType<tensor>
153 (fieldType, mesh_, selectedCells_, fieldValues)
157 WarningIn("setCellField::iNew::operator()(Istream& is)")
158 << "field type " << fieldType << " not currently supported"
162 return autoPtr<setCellField>(new setCellField());
169 bool setFaceFieldType
171 const word& fieldTypeDesc,
173 const labelList& selectedFaces,
174 Istream& fieldValueStream
177 typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
179 if (fieldTypeDesc != fieldType::typeName + "Value")
184 word fieldName(fieldValueStream);
189 mesh.time().timeName(),
194 // Check field exists
195 if (fieldHeader.headerOk())
197 Info<< " Setting patchField values of "
198 << fieldHeader.headerClassName()
199 << " " << fieldName << endl;
201 fieldType field(fieldHeader, mesh);
203 const Type& value = pTraits<Type>(fieldValueStream);
205 // Create flat list of selected faces and their value.
206 Field<Type> allBoundaryValues(mesh.nFaces()-mesh.nInternalFaces());
207 forAll(field.boundaryField(), patchi)
212 field.boundaryField()[patchi].size(),
213 field.boundaryField()[patchi].patch().start()
214 - mesh.nInternalFaces()
215 ).assign(field.boundaryField()[patchi]);
219 labelList nChanged(field.boundaryField().size(), 0);
220 forAll(selectedFaces, i)
222 label facei = selectedFaces[i];
223 if (mesh.isInternalFace(facei))
225 WarningIn("setFaceFieldType(..)")
226 << "Ignoring internal face " << facei << endl;
230 label bFaceI = facei-mesh.nInternalFaces();
231 allBoundaryValues[bFaceI] = value;
232 nChanged[mesh.boundaryMesh().patchID()[bFaceI]]++;
236 Pstream::listCombineGather(nChanged, plusEqOp<label>());
237 Pstream::listCombineScatter(nChanged);
240 forAll(field.boundaryField(), patchi)
242 if (nChanged[patchi] > 0)
245 << field.boundaryField()[patchi].patch().name()
246 << " set " << nChanged[patchi] << " values" << endl;
247 field.boundaryField()[patchi] == SubField<Type>
250 field.boundaryField()[patchi].size(),
251 field.boundaryField()[patchi].patch().start()
252 - mesh.nInternalFaces()
263 "void setFaceFieldType"
264 "(const fvMesh& mesh, const labelList& selectedFaces,"
265 "Istream& fieldValueStream)"
266 ) << "Field " << fieldName << " not found" << endl;
281 autoPtr<setFaceField> clone() const
283 return autoPtr<setFaceField>(new setFaceField());
289 const labelList& selectedFaces_;
293 iNew(const fvMesh& mesh, const labelList& selectedFaces)
296 selectedFaces_(selectedFaces)
299 autoPtr<setFaceField> operator()(Istream& fieldValues) const
301 word fieldType(fieldValues);
306 setFaceFieldType<scalar>
307 (fieldType, mesh_, selectedFaces_, fieldValues)
308 || setFaceFieldType<vector>
309 (fieldType, mesh_, selectedFaces_, fieldValues)
310 || setFaceFieldType<sphericalTensor>
311 (fieldType, mesh_, selectedFaces_, fieldValues)
312 || setFaceFieldType<symmTensor>
313 (fieldType, mesh_, selectedFaces_, fieldValues)
314 || setFaceFieldType<tensor>
315 (fieldType, mesh_, selectedFaces_, fieldValues)
319 WarningIn("setFaceField::iNew::operator()(Istream& is)")
320 << "field type " << fieldType << " not currently supported"
324 return autoPtr<setFaceField>(new setFaceField());
331 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333 int main(int argc, char *argv[])
335 timeSelector::addOptions();
337 # include "setRootCase.H"
338 # include "createTime.H"
341 instantList timeDirs = timeSelector::select0(runTime, args);
343 # include "createMesh.H"
345 Info<< "Reading setFieldsDict\n" << endl;
347 IOdictionary setFieldsDict
354 IOobject::MUST_READ_IF_MODIFIED,
359 if (setFieldsDict.found("defaultFieldValues"))
361 Info<< "Setting field default values" << endl;
362 PtrList<setCellField> defaultFieldValues
364 setFieldsDict.lookup("defaultFieldValues"),
365 setCellField::iNew(mesh, labelList(mesh.nCells()))
371 Info<< "Setting field region values" << endl;
373 PtrList<entry> regions(setFieldsDict.lookup("regions"));
375 forAll(regions, regionI)
377 const entry& region = regions[regionI];
379 autoPtr<topoSetSource> source =
380 topoSetSource::New(region.keyword(), mesh, region.dict());
382 if (source().setType() == topoSetSource::CELLSETSOURCE)
384 cellSet selectedCellSet
388 mesh.nCells()/10+1 // Reasonable size estimate.
397 PtrList<setCellField> fieldValues
399 region.dict().lookup("fieldValues"),
400 setCellField::iNew(mesh, selectedCellSet.toc())
403 else if (source().setType() == topoSetSource::FACESETSOURCE)
405 faceSet selectedFaceSet
409 (mesh.nFaces()-mesh.nInternalFaces())/10+1
418 PtrList<setFaceField> fieldValues
420 region.dict().lookup("fieldValues"),
421 setFaceField::iNew(mesh, selectedFaceSet.toc())
427 Info<< "\nEnd\n" << endl;
433 // ************************************************************************* //