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 or by extrapolation from the
30  internal field.
31 
32 Usage
33  Any number of fields can be initialised on any number of zones, for
34  example the 1D shock tube is initialised by
35  \verbatim
36  defaultValues
37  {
38  U (0 0 0);
39  T 348.432;
40  p 100000;
41  }
42 
43  zones
44  {
45  lowPressure
46  {
47  type box;
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 
72  box (-1e300 -1e300 -1e300) (1e300 0 1e300);
73 
74  values
75  {
76  alpha.water 1;
77  }
78  }
79  }
80 
81  extrapolatePatches
82  {
83  "inlet|outlet" (alpha.water);
84  }
85  \endverbatim
86  which sets the internal values of phase-fraction field and inlet and outlet
87  patch values by extrapolation from the internal to provide consistent
88  boundary distribution.
89 
90  Alternatively the inlet and outlet patch values can be set explicitly on a
91  faceZone constructed from the corresponding patches:
92  \verbatim
93  defaultValues
94  {
95  alpha.water 0;
96  }
97 
98  zones
99  {
100  cells
101  {
102  type box;
103 
104  box (-1e300 -1e300 -1e300) (1e300 0 1e300);
105 
106  values
107  {
108  alpha.water 1;
109  }
110  }
111 
112  patchFaces
113  {
114  type box;
115  zoneType face;
116 
117  box (-1e300 -1e300 -1e300) (1e300 0 1e300);
118 
119  zone
120  {
121  type patch;
122  patches (inlet outlet);
123  }
124 
125  values
126  {
127  alpha.water 1;
128  }
129  }
130  }
131  \endverbatim
132 
133 \*---------------------------------------------------------------------------*/
134 
135 #include "argList.H"
136 #include "timeSelector.H"
137 #include "PtrDictionary.H"
138 #include "volFields.H"
139 #include "zoneGenerator.H"
140 #include "systemDict.H"
141 
142 // Backward compatibility
143 #include "topoSetSource.H"
144 #include "cellSet.H"
145 #include "faceSet.H"
146 #include "setCellField.H"
147 
148 using namespace Foam;
149 
150 void setVolFields
151 (
152  const fvMesh& mesh,
153  const dictionary& fieldsDict,
154  const labelList& selectedCells,
155  const PtrDictionary<wordReList>& extrapolatePatches
156 );
157 
158 void setPatchFields
159 (
160  const fvMesh& mesh,
161  const dictionary& fieldsDict,
162  const labelList& selectedCells
163 );
164 
165 
166 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
167 
168 int main(int argc, char *argv[])
169 {
171  #include "addDictOption.H"
172  #include "addRegionOption.H"
173  #include "setRootCase.H"
174  #include "createTime.H"
175  timeSelector::select0(runTime, args);
177 
178  const dictionary setFieldsDict(systemDict("setFieldsDict", args, mesh));
179 
180  PtrDictionary<wordReList> extrapolatePatches;
181 
182  if (setFieldsDict.isDict("extrapolatePatches"))
183  {
184  const dictionary& extrapolatePatchesDict =
185  setFieldsDict.subDict("extrapolatePatches");
186 
188  {
189  if (extrapolatePatchesDict.found(mesh.boundary()[patchi].name()))
190  {
191  extrapolatePatches.insert
192  (
193  mesh.boundary()[patchi].name(),
194  new wordReList
195  (
196  extrapolatePatchesDict.lookup
197  (
198  mesh.boundary()[patchi].name()
199  )
200  )
201  );
202  }
203  }
204  }
205 
206  if (setFieldsDict.found("defaultValues"))
207  {
208  Info<< "Setting field default values" << endl;
209  setVolFields
210  (
211  mesh,
212  setFieldsDict.subDict("defaultValues"),
213  labelList::null(),
214  extrapolatePatches
215  );
216  Info<< endl;
217  }
218  else if (setFieldsDict.found("defaultFieldValues"))
219  {
220  Info<< "Setting field default values" << nl
221  << " The 'defaultFieldValues' entry is deprecated, "
222  "please use 'default'" << endl;
223 
224  PtrList<setCellField> defaultFieldValues
225  (
226  setFieldsDict.lookup("defaultFieldValues"),
228  );
229  Info<< endl;
230  }
231 
232  if (setFieldsDict.found("zones"))
233  {
234  Info<< "Setting field zone values" << endl;
235 
236  const dictionary& zonesDict = setFieldsDict.subDict("zones");
237 
238  forAllConstIter(dictionary, zonesDict, iter)
239  {
240  Info<< "Zone: " << iter().keyword() << endl;
241 
242  const dictionary& zoneDict = iter().dict();
243 
245 
246  if (zoneDict.found("zoneType"))
247  {
248  zg = zoneGenerator::New(iter().keyword(), mesh, zoneDict);
249  }
250  else
251  {
252  zg = zoneGenerator::New
253  (
254  iter().keyword(),
256  mesh,
257  zoneDict
258  );
259  }
260 
261  const zoneSet zs(zg->generate());
262 
263  if (zs.cValid())
264  {
265  setVolFields
266  (
267  mesh,
268  zoneDict.subDict("values"),
269  zs.cZone(),
270  extrapolatePatches
271  );
272  }
273 
274  if (zs.fValid())
275  {
276  setPatchFields(mesh, zoneDict.subDict("values"), zs.fZone());
277  }
278  }
279  }
280 
281  if (setFieldsDict.found("regions"))
282  {
283  PtrList<entry> regions(setFieldsDict.lookup("regions"));
284 
285  Info<< "Setting field region values" << nl
286  << " The 'regions' entry is deprecated, "
287  "please use 'zones'" << endl;
288 
289  forAll(regions, ri)
290  {
291  const entry& region = regions[ri];
292 
293  autoPtr<topoSetSource> source =
294  topoSetSource::New(region.keyword(), mesh, region.dict());
295 
296  if (source().setType() == topoSetSource::CELLSETSOURCE)
297  {
298  cellSet selectedCellSet
299  (
300  mesh,
301  "cellSet",
302  mesh.nCells()/10+1 // Reasonable size estimate.
303  );
304 
305  source->applyToSet
306  (
308  selectedCellSet
309  );
310 
311  PtrList<setCellField> fieldValues
312  (
313  region.dict().lookup("fieldValues"),
314  setCellField::iNew(mesh, selectedCellSet.toc())
315  );
316  }
317  else if (source().setType() == topoSetSource::FACESETSOURCE)
318  {
319  faceSet selectedFaceSet
320  (
321  mesh,
322  "faceSet",
323  (mesh.nFaces()-mesh.nInternalFaces())/10+1
324  );
325 
326  source->applyToSet
327  (
329  selectedFaceSet
330  );
331 
332  PtrList<setFaceField> fieldValues
333  (
334  region.dict().lookup("fieldValues"),
335  setFaceField::iNew(mesh, selectedFaceSet.toc())
336  );
337  }
338  }
339  }
340 
341 
342  Info<< "\nEnd\n" << endl;
343 
344  return 0;
345 }
346 
347 
348 // ************************************************************************* //
#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
void insert(const word &, T *)
Add at head of dictionary.
static const List< label > & null()
Return a null List.
Definition: ListI.H:118
Template dictionary class which manages the storage associated with it.
Definition: PtrDictionary.H:56
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
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:539
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
const fvBoundaryMesh & boundary() const
Return reference to boundary mesh.
Definition: fvMesh.C:957
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
label patchi
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)