setFields.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Description
25  Initialises fields with default and zone values
26 
27  The default and zone values are read from a dictionary which defaults to
28  system/setFieldsDict, cellZones are used to specify the internal field
29  values and faceZone patch field values.
30 
31 Usage
32  Any number of fields can be initialised on any number of zones, for
33  example the 1D shock tube is initialised by
34  \verbatim
35  defaultValues
36  {
37  U (0 0 0);
38  T 348.432;
39  p 100000;
40  }
41 
42  zones
43  {
44  lowPressure
45  {
46  type box;
47  zoneType cell;
48 
49  box (0 -1 -1) (5 1 1);
50 
51  values
52  {
53  T 278.746;
54  p 10000;
55  }
56  }
57  }
58  \endverbatim
59  and the water in the tank of the rotatingCube VoF case is initialised by
60  \verbatim
61  defaultValues
62  {
63  alpha.water 0;
64  }
65 
66  zones
67  {
68  cells
69  {
70  type box;
71  zoneType cell;
72 
73  box (-1e300 -1e300 -1e300) (1e300 0 1e300);
74 
75  values
76  {
77  alpha.water 1;
78  }
79  }
80 
81  patchFaces
82  {
83  type box;
84  zoneType face;
85 
86  box (-1e300 -1e300 -1e300) (1e300 0 1e300);
87 
88  values
89  {
90  alpha.water 1;
91  }
92  }
93  }
94  \endverbatim
95  which sets both the internal and inlet values of the patch phase-fraction
96  field.
97 
98 \*---------------------------------------------------------------------------*/
99 
100 #include "argList.H"
101 #include "timeSelector.H"
102 #include "volFields.H"
103 #include "zoneGenerator.H"
104 #include "systemDict.H"
105 
106 // Backward compatibility
107 #include "topoSetSource.H"
108 #include "cellSet.H"
109 #include "faceSet.H"
110 #include "setCellField.H"
111 
112 using namespace Foam;
113 
114 void setVolFields
115 (
116  const fvMesh& mesh,
117  const dictionary& fieldsDict,
118  const labelList& selectedCells
119 );
120 
121 void setPatchFields
122 (
123  const fvMesh& mesh,
124  const dictionary& fieldsDict,
125  const labelList& selectedCells
126 );
127 
128 
129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 
131 int main(int argc, char *argv[])
132 {
134  #include "addDictOption.H"
135  #include "addRegionOption.H"
136  #include "setRootCase.H"
137  #include "createTime.H"
138  timeSelector::select0(runTime, args);
140 
141  const dictionary setFieldsDict(systemDict("setFieldsDict", args, mesh));
142 
143  if (setFieldsDict.found("defaultValues"))
144  {
145  Info<< "Setting field default values" << endl;
146  setVolFields
147  (
148  mesh,
149  setFieldsDict.subDict("defaultValues"),
151  );
152  Info<< endl;
153  }
154  else if (setFieldsDict.found("defaultFieldValues"))
155  {
156  Info<< "Setting field default values" << nl
157  << " The 'defaultFieldValues' entry is deprecated, "
158  "please use 'default'" << endl;
159 
160  PtrList<setCellField> defaultFieldValues
161  (
162  setFieldsDict.lookup("defaultFieldValues"),
164  );
165  Info<< endl;
166  }
167 
168  if (setFieldsDict.found("zones"))
169  {
170  Info<< "Setting field zone values" << endl;
171 
172  const dictionary& zonesDict = setFieldsDict.subDict("zones");
173 
174  forAllConstIter(dictionary, zonesDict, iter)
175  {
176  Info<< "Zone: " << iter().keyword() << endl;
177 
178  const dictionary& zoneDict = iter().dict();
179 
181  (
182  zoneGenerator::New(iter().keyword(), mesh, zoneDict)
183  );
184 
185  const zoneSet zs(zg->generate());
186 
187  if (zs.cValid())
188  {
189  setVolFields(mesh, zoneDict.subDict("values"), zs.cZone());
190  }
191 
192  if (zs.fValid())
193  {
194  setPatchFields(mesh, zoneDict.subDict("values"), zs.fZone());
195  }
196  }
197  }
198 
199  if (setFieldsDict.found("regions"))
200  {
201  PtrList<entry> regions(setFieldsDict.lookup("regions"));
202 
203  Info<< "Setting field region values" << nl
204  << " The 'regions' entry is deprecated, "
205  "please use 'zones'" << endl;
206 
207  forAll(regions, ri)
208  {
209  const entry& region = regions[ri];
210 
211  autoPtr<topoSetSource> source =
212  topoSetSource::New(region.keyword(), mesh, region.dict());
213 
214  if (source().setType() == topoSetSource::CELLSETSOURCE)
215  {
216  cellSet selectedCellSet
217  (
218  mesh,
219  "cellSet",
220  mesh.nCells()/10+1 // Reasonable size estimate.
221  );
222 
223  source->applyToSet
224  (
226  selectedCellSet
227  );
228 
229  PtrList<setCellField> fieldValues
230  (
231  region.dict().lookup("fieldValues"),
232  setCellField::iNew(mesh, selectedCellSet.toc())
233  );
234  }
235  else if (source().setType() == topoSetSource::FACESETSOURCE)
236  {
237  faceSet selectedFaceSet
238  (
239  mesh,
240  "faceSet",
241  (mesh.nFaces()-mesh.nInternalFaces())/10+1
242  );
243 
244  source->applyToSet
245  (
247  selectedFaceSet
248  );
249 
250  PtrList<setFaceField> fieldValues
251  (
252  region.dict().lookup("fieldValues"),
253  setFaceField::iNew(mesh, selectedFaceSet.toc())
254  );
255  }
256  }
257  }
258 
259 
260  Info<< "\nEnd\n" << endl;
261 
262  return 0;
263 }
264 
265 
266 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
static const List< label > & null()
Return a null List.
Definition: ListI.H:118
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A collection of cell labels.
Definition: cellSet.H:51
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:740
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:849
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
const keyType & keyword() const
Return keyword.
Definition: entry.H:136
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
A list of face labels.
Definition: faceSet.H:51
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:96
wordList toc(const word &className) const
Return the list of names of IOobjects of given class name.
label nInternalFaces() const
label nCells() const
label nFaces() const
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
static autoPtr< topoSetSource > New(const word &topoSetSourceType, const polyMesh &mesh, const dictionary &dict)
Return a reference to the selected topoSetSource.
Definition: topoSetSource.C:61
static autoPtr< zoneGenerator > New(const word &name, const polyMesh &mesh, const dictionary &dict)
Select constructed from name, mesh and dictionary.
Definition: zoneGenerator.C:66
Zone container returned by zoneGenerator::generate.
Definition: zoneSet.H:94
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
int main(int argc, char *argv[])
Definition: financialFoam.C:44
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
messageStream Info
IOdictionary systemDict(const word &dictName, const argList &args, const objectRegistry &ob, const word &regionName=polyMesh::defaultRegion, const fileName &path=fileName::null)
Definition: systemDict.C:93
static const char nl
Definition: Ostream.H:267
Foam::argList args(argc, argv)