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  field.add
147  (
148  "#include",
149  "<system>"/regionName/"caseProperties"
150  );
151 
152  field.add("dimensions", fieldDimensions[i]);
153 
154  string iField("${:initialConditions." + fieldNames[i] + '}');
155  field.add("internalField", iField.c_str());
156 
157  dictionary boundaryField =
159  (
160  regionPrefix,
161  fieldNames[i],
162  bcTemplates
163  );
164 
165  field.add("boundaryField", boundaryField);
166 
167  // Expand all of the dictionary redirections and remove unnecessary
168  // entries
169 
170  dictionary field2;
171 
172  {
173  OCharStream os;
174  os << field;
175  ISpanStream is(os.view());
176 
178  is >> field2;
180 
181  field2.remove("#include");
182  field2.remove("initialConditions");
183  field2.remove("boundaryConditions");
184  }
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,
198  ),
199  field2
200  );
201 
202  fieldOut.regIOobject::writeObject
203  (
205  true
206  );
207  }
208 }
209 
210 
211 // Main program:
212 int main(int argc, char *argv[])
213 {
215  (
216  "Create a 0/ directory with fields appropriate for the chosen"
217  " solver and turbulence model."
218  );
219 
221  (
222  "templateDir",
223  "dir",
224  "Read case set-up templates from specified location"
225  );
226 
227  #include "setRootCase.H"
228  #include "createTime.H"
229 
230  Info<< "Reading controlDict" << nl << endl;
231 
233  (
234  IOobject
235  (
236  "controlDict",
237  runTime.system(),
238  runTime,
242  )
243  );
244 
245  // Template directory: default is from PROJECT/etc directory
246  //
247  // Can use
248  // - foamEtcDir("caseDicts/createZeroDirectory", 0007);
249  // - expand "<etc:o>/caseDicts/createZeroDirectory"
250  // - expand "${WM_PROJECT_DIR}/etc/caseDicts/createZeroDirectory"
251  //
252  // Use "${WM_PROJECT_DIR}/" version for nicer error message
253 
254  fileName baseDir
255  (
257  (
258  "templateDir",
259  "${WM_PROJECT_DIR}/etc/caseDicts/createZeroDirectoryTemplates"
260  )
261  );
262 
263  baseDir.expand();
264  baseDir.toAbsolute();
265 
266  if (!Foam::isDir(baseDir))
267  {
269  << "templateDir " << baseDir << nl
270  << "Does not point to a folder containing case set-up templates"
271  << nl
272  << exit(FatalError);
273  }
274 
275  // Keep variable substitutions - delay until after creation of controlDict
276  // to allow #include files to be processed
278 
279  // Read the solver
280  const word solverName(controlDict.get<word>("application"));
281 
282  // Generate solver template
283  const solverTemplate solver(baseDir, runTime, solverName);
284 
285  // Read the boundary condition templates
286  const boundaryTemplates bcTemplates
287  (
288  baseDir,
289  runTime,
290  solver.type()
291  );
292 
293  Info<< endl;
294 
295  const label nRegion = solver.nRegion();
296  for (label regionI = 0; regionI < nRegion; regionI++)
297  {
298  const word& regionName = solver.regionName(regionI);
299 
300  if (regionName.empty())
301  {
302  Info<< "Region: " << polyMesh::defaultRegion << " (default)"
303  << endl;
304  }
305  else
306  {
307  Info<< "Region: " << regionName << endl;
308  }
309 
310  // Read the case set-up info for the current region
311  const caseInfo cInfo(runTime, regionName);
312 
313  cInfo.checkPatches(solver.regionType(regionI), bcTemplates);
314 
315  createFieldFiles
316  (
317  runTime,
318  regionName,
319  solver.regionType(regionI),
320  solver.fieldNames(regionI),
321  solver.fieldTypes(regionI),
322  solver.fieldDimensions(regionI),
323  cInfo,
324  bcTemplates
325  );
326  }
327 
328  Info<< "End\n" << endl;
329 
330  return 0;
331 }
332 
333 
334 // ************************************************************************* //
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:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Base solver class.
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:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
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:531
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
Generic GeometricField class.
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:421
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
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:406
const word & system() const noexcept
Return system name.
Definition: TimePathsI.H:118
An OSstream with internal List storage.
Definition: OCharStream.H:231
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
Foam::word regionName(args.getOrDefault< word >("region", Foam::polyMesh::defaultRegion))
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
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:180
void checkPatches(const word &regionPrefix, const boundaryTemplates &bcTemplates) const
Check patches.
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: ISpanStream.H:251
Do not request registration (bool: false)
Namespace for OpenFOAM.