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-2020 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 != word::null)
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 == word::null)
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 // ************************************************************************* //
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:191
volFields.H
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:165
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
solverTemplate.H
Foam::polyMesh::defaultRegion
static word defaultRegion
Definition: polyMesh.H:314
Foam::argList::getOrDefault
T getOrDefault(const word &optName, const T &deflt) const
Definition: argListI.H:300
caseInfo.H
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::solverTemplate
Class to store solver template specifications.
Definition: solverTemplate.H:47
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
regionName
Foam::word regionName
Definition: createNamedDynamicFvMesh.H:1
Foam::caseInfo::generateBoundaryField
dictionary generateBoundaryField(const word &regionPrefix, const word &fieldName, const boundaryTemplates &bcTemplates) const
Foam::solver
Base class for solution control classes.
Definition: solver.H:45
Foam::Info
messageStream Info
argList.H
controlDict
runTime controlDict().readEntry("adjustTimeStep"
Definition: debug.C:139
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:59
field
rDeltaTY field()
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:55
Foam::FatalError
error FatalError
Foam::caseInfo::checkPatches
void checkPatches(const word &regionPrefix, const boundaryTemplates &bcTemplates) const
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:119
fieldNames
const wordRes fieldNames(propsDict.getOrDefault< wordRes >("fields", wordRes()))
os
OBJstream os(runTime.globalPath()/outputName)
boundaryTemplates.H
Foam::IStringStream
Definition: StringStream.H:108
Foam
Definition: atmBoundaryLayer.C:26
Foam::entry::disableFunctionEntries
static int disableFunctionEntries
Definition: entry.H:123
Foam::caseInfo
Class to hold information related to the simaulation case.
Definition: caseInfo.H:50
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
IOdictionary.H
setRootCase.H
Foam::TimePaths::system
const word & system() const
Definition: TimePathsI.H:95
Foam::IOstreamOption::ASCII
@ ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:68
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:50
Foam::OStringStream
Definition: StringStream.H:229
Foam::word::null
static const word null
Definition: word.H:78
createTime.H
Foam::GeometricField
Generic GeometricField class.
Definition: areaFieldsFwd.H:49
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Definition: argList.C:328
Foam::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:184
args
Foam::argList args(argc, argv)
Foam::boundaryTemplates
Class to store boundary template specifications.
Definition: boundaryTemplates.H:47
Foam::localIOdictionary
localIOdictionary is derived from IOdictionary but excludes parallel master reading.
Definition: localIOdictionary.H:48
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Definition: POSIX.C:775
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181