createZeroDirectory.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015 OpenFOAM Foundation
9  Copyright (C) 2015-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  createZeroDirectory
29 
30 Group
31  grpPreProcessingUtilities
32 
33 Description
34  Creates a zero directory with fields appropriate for the chosen solver and
35  turbulence model. Operates on both single and multi-region cases.
36 
37 Usage
38  The set-up is configured using a 'caseProperties' dictionary, located under
39  the $FOAM_CASE/system (or system/regionName if multi-region) directory.
40  This consists of a lists of initial and boundary conditions, e.g.
41 
42  \verbatim
43  initialConditions
44  {
45  U uniform (0 0 0);
46  p uniform 0;
47  }
48 
49  boundaryConditions
50  {
51  topWall
52  {
53  category wall;
54  patches (movingWall);
55  type noSlip;
56  options
57  {
58  wallFunction highReynolds;
59  motion moving;
60  };
61  values
62  {
63  U uniform (1 0 0);
64  }
65  }
66 
67  walls
68  {
69  category wall;
70  patches (fixedWalls);
71  type noSlip;
72  options
73  {
74  wallFunction highReynolds;
75  motion stationary;
76  };
77  }
78  }
79  \endverbatim
80 
81 \*---------------------------------------------------------------------------*/
82 
83 #include "argList.H"
84 #include "volFields.H"
85 #include "IOdictionary.H"
86 #include "caseInfo.H"
87 #include "boundaryTemplates.H"
88 #include "solverTemplate.H"
89 
90 using namespace Foam;
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
94 const word getClassType(const word& pType)
95 {
96  if (pType == pTraits<scalar>::typeName)
97  {
99  }
100  else if (pType == pTraits<vector>::typeName)
101  {
103  }
104  else if (pType == pTraits<sphericalTensor>::typeName)
105  {
107  }
108  else if (pType == pTraits<symmTensor>::typeName)
109  {
111  }
112  else if (pType == pTraits<tensor>::typeName)
113  {
115  }
116  else
117  {
118  // Error
119  return word::null;
120  }
121 }
122 
123 
124 void createFieldFiles
125 (
126  const Time& runTime,
127  const word& regionName,
128  const word& regionPrefix,
129  const wordList& fieldNames,
130  const wordList& fieldTypes,
131  const PtrList<dimensionSet>& fieldDimensions,
132  const caseInfo& cInfo,
133  const boundaryTemplates& bcTemplates
134 )
135 {
136  Info<< " Generating field files" << nl << endl;
137 
138  // Create files
139  forAll(fieldNames, i)
140  {
141  const_cast<word&>(IOdictionary::typeName) =
142  getClassType(fieldTypes[i]);
143 
145 
146  fileName regionPath = "/";
147 
148  if (!regionName.empty())
149  {
150  regionPath += regionName + '/';
151  }
152 
153  field.add
154  (
155  "#include",
156  "$FOAM_CASE/system" + regionPath + "caseProperties"
157  );
158 
159  field.add("dimensions", fieldDimensions[i]);
160 
161  string iField("${:initialConditions." + fieldNames[i] + '}');
162  field.add("internalField", iField.c_str());
163 
164  dictionary boundaryField =
166  (
167  regionPrefix,
168  fieldNames[i],
169  bcTemplates
170  );
171 
172  field.add("boundaryField", boundaryField);
173 
174  // Expand all of the dictionary redirections and remove unnecessary
175  // entries
177  os << field;
178 
180  dictionary field2(IStringStream(os.str())());
182  field2.remove("#include");
183  field2.remove("initialConditions");
184  field2.remove("boundaryConditions");
185 
186  // Construct and write field dictionary. Note use of localIOdictionary
187  localIOdictionary fieldOut
188  (
189  IOobject
190  (
191  fieldNames[i],
192  "0",
193  regionName,
194  runTime,
196  ),
197  field2
198  );
199 
200  fieldOut.regIOobject::writeObject
201  (
203  true
204  );
205  }
206 }
207 
208 
209 // Main program:
210 int main(int argc, char *argv[])
211 {
213  (
214  "Create a 0/ directory with fields appropriate for the chosen"
215  " solver and turbulence model."
216  );
217 
219  (
220  "templateDir",
221  "dir",
222  "Read case set-up templates from specified location"
223  );
224 
225  #include "setRootCase.H"
226  #include "createTime.H"
227 
228  Info<< "Reading controlDict" << nl << endl;
229 
231  (
232  IOobject
233  (
234  "controlDict",
235  runTime.system(),
236  runTime,
239  )
240  );
241 
242  // Template directory: default is from PROJECT/etc directory
243  //
244  // Can use
245  // - foamEtcDir("caseDicts/createZeroDirectory", 0007);
246  // - expand "<etc:o>/caseDicts/createZeroDirectory"
247  // - expand "${WM_PROJECT_DIR}/etc/caseDicts/createZeroDirectory"
248  //
249  // Use "${WM_PROJECT_DIR}/" version for nicer error message
250 
251  fileName baseDir
252  (
254  (
255  "templateDir",
256  "${WM_PROJECT_DIR}/etc/caseDicts/createZeroDirectoryTemplates"
257  )
258  );
259 
260  baseDir.expand();
261  baseDir.toAbsolute();
262 
263  if (!Foam::isDir(baseDir))
264  {
266  << "templateDir " << baseDir << nl
267  << "Does not point to a folder containing case set-up templates"
268  << nl
269  << exit(FatalError);
270  }
271 
272  // Keep variable substitutions - delay until after creation of controlDict
273  // to allow #include files to be processed
275 
276  // Read the solver
277  const word solverName(controlDict.get<word>("application"));
278 
279  // Generate solver template
280  const solverTemplate solver(baseDir, runTime, solverName);
281 
282  // Read the boundary condition templates
283  const boundaryTemplates bcTemplates
284  (
285  baseDir,
286  runTime,
287  solver.type()
288  );
289 
290  Info<< endl;
291 
292  const label nRegion = solver.nRegion();
293  for (label regionI = 0; regionI < nRegion; regionI++)
294  {
295  const word& regionName = solver.regionName(regionI);
296 
297  if (regionName.empty())
298  {
299  Info<< "Region: " << polyMesh::defaultRegion << " (default)"
300  << endl;
301  }
302  else
303  {
304  Info<< "Region: " << regionName << endl;
305  }
306 
307  // Read the case set-up info for the current region
308  const caseInfo cInfo(runTime, regionName);
309 
310  cInfo.checkPatches(solver.regionType(regionI), bcTemplates);
311 
312  createFieldFiles
313  (
314  runTime,
315  regionName,
316  solver.regionType(regionI),
317  solver.fieldNames(regionI),
318  solver.fieldTypes(regionI),
319  solver.fieldDimensions(regionI),
320  cInfo,
321  bcTemplates
322  );
323  }
324 
325  Info<< "End\n" << endl;
326 
327  return 0;
328 }
329 
330 
331 // ************************************************************************* //
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
rDeltaTY field()
A class for handling file names.
Definition: fileName.H:71
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Base class for solution control classes.
Definition: solver.H:45
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
dictionary generateBoundaryField(const word &regionPrefix, const word &fieldName, const boundaryTemplates &bcTemplates) const
Generate boundary field (dictionary)
"ascii" (normal default)
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
A traits class, which is primarily used for primitives.
Definition: pTraits.H:51
Generic GeometricField class.
Definition: areaFieldsFwd.H:50
A simple container for options an IOstream can normally have.
Ignore writing from objectRegistry::writeObject()
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:300
Class to store boundary template specifications.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:414
localIOdictionary is derived from IOdictionary but excludes parallel master reading.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
Foam::word regionName(Foam::polyMesh::defaultRegion)
static int disableFunctionEntries
Enable or disable use of function entries and variable expansions.
Definition: entry.H:139
A class for handling words, derived from Foam::string.
Definition: word.H:63
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:397
const word & system() const noexcept
Return system name.
Definition: TimePathsI.H:95
static const word null
An empty word.
Definition: word.H:84
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:385
runTime controlDict().readEntry("adjustTimeStep"
The central control dictionary, the contents of which are either taken directly from the FOAM_CONTROL...
Definition: debug.C:142
OBJstream os(runTime.globalPath()/outputName)
Class to hold information related to the simaulation case.
Definition: caseInfo.H:50
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:120
Nothing to be read.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Class to store solver template specifications.
Foam::argList args(argc, argv)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:171
void checkPatches(const word &regionPrefix, const boundaryTemplates &bcTemplates) const
Check patches.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:256
Namespace for OpenFOAM.