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 using namespace Foam;
143 
144 void setVolFields
145 (
146  const fvMesh& mesh,
147  const dictionary& fieldsDict,
148  const labelList& selectedCells,
149  const PtrDictionary<wordReList>& extrapolatePatches
150 );
151 
152 void setPatchFields
153 (
154  const fvMesh& mesh,
155  const dictionary& fieldsDict,
156  const labelList& selectedCells
157 );
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 int main(int argc, char *argv[])
163 {
165  #include "addDictOption.H"
166  #include "addRegionOption.H"
167  #include "setRootCase.H"
168  #include "createTime.H"
169  timeSelector::select0(runTime, args);
171 
172  const dictionary setFieldsDict(systemDict("setFieldsDict", args, mesh));
173 
174  PtrDictionary<wordReList> extrapolatePatches;
175 
176  if (setFieldsDict.isDict("extrapolatePatches"))
177  {
178  const dictionary& extrapolatePatchesDict =
179  setFieldsDict.subDict("extrapolatePatches");
180 
182  {
183  if (extrapolatePatchesDict.found(mesh.boundary()[patchi].name()))
184  {
185  extrapolatePatches.insert
186  (
187  mesh.boundary()[patchi].name(),
188  new wordReList
189  (
190  extrapolatePatchesDict.lookup
191  (
192  mesh.boundary()[patchi].name()
193  )
194  )
195  );
196  }
197  }
198  }
199 
200  if (setFieldsDict.found("defaultValues"))
201  {
202  Info<< "Setting field default values" << endl;
203  setVolFields
204  (
205  mesh,
206  setFieldsDict.subDict("defaultValues"),
207  labelList::null(),
208  extrapolatePatches
209  );
210  Info<< endl;
211  }
212 
213  if (setFieldsDict.found("zones"))
214  {
215  Info<< "Setting field zone values" << endl;
216 
217  const dictionary& zonesDict = setFieldsDict.subDict("zones");
218 
219  forAllConstIter(dictionary, zonesDict, iter)
220  {
221  Info<< "Zone: " << iter().keyword() << endl;
222 
223  const dictionary& zoneDict = iter().dict();
224 
226 
227  if (zoneDict.found("zoneType"))
228  {
229  zg = zoneGenerator::New(iter().keyword(), mesh, zoneDict);
230  }
231  else
232  {
233  zg = zoneGenerator::New
234  (
235  iter().keyword(),
237  mesh,
238  zoneDict
239  );
240  }
241 
242  const zoneSet zs(zg->generate());
243 
244  if (zs.cValid())
245  {
246  setVolFields
247  (
248  mesh,
249  zoneDict.subDict("values"),
250  zs.cZone(),
251  extrapolatePatches
252  );
253  }
254 
255  if (zs.fValid())
256  {
257  setPatchFields(mesh, zoneDict.subDict("values"), zs.fZone());
258  }
259  }
260  }
261 
262  Info<< "\nEnd\n" << endl;
263 
264  return 0;
265 }
266 
267 
268 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
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
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 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:669
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:778
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:98
const fvBoundaryMesh & boundary() const
Return reference to boundary mesh.
Definition: fvMesh.C:932
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< 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:288
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
Foam::argList args(argc, argv)