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 | Copyright (C) 2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 Application
25  createZeroDirectory
26 
27 Description
28  Creates a zero directory with fields appropriate for the chosen solver and
29  turbulence model. Operates on both single and multi-region cases.
30 
31 Usage
32  The set-up is configured using a 'caseProperties' dictionary, located under
33  the $FOAM_CASE/system (or system/regionName if multi-region) directory.
34  This consists of a lists of initial and boundary conditions, e.g.
35 
36  \verbatim
37  initialConditions
38  {
39  U uniform (0 0 0);
40  p uniform 0;
41  }
42 
43  boundaryConditions
44  {
45  topWall
46  {
47  category wall;
48  patches (movingWall);
49  type noSlip;
50  options
51  {
52  wallFunction highReynolds;
53  motion moving;
54  };
55  values
56  {
57  U uniform (1 0 0);
58  }
59  }
60 
61  walls
62  {
63  category wall;
64  patches (fixedWalls);
65  type noSlip;
66  options
67  {
68  wallFunction highReynolds;
69  motion stationary;
70  };
71  }
72  }
73  \endverbatim
74 
75 \*---------------------------------------------------------------------------*/
76 
77 #include "argList.H"
78 #include "volFields.H"
79 #include "IOdictionary.H"
80 #include "caseInfo.H"
81 #include "boundaryTemplates.H"
82 #include "solverTemplate.H"
83 
84 using namespace Foam;
85 
86 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
87 
88 const word getClassType(const word& pType)
89 {
90  if (pType == pTraits<scalar>::typeName)
91  {
93  }
94  else if (pType == pTraits<vector>::typeName)
95  {
97  }
98  else if (pType == pTraits<sphericalTensor>::typeName)
99  {
101  }
102  else if (pType == pTraits<symmTensor>::typeName)
103  {
105  }
106  else if (pType == pTraits<tensor>::typeName)
107  {
109  }
110  else
111  {
112  // Error
113  return word::null;
114  }
115 }
116 
117 
118 void createFieldFiles
119 (
120  const Time& runTime,
121  const word& regionName,
122  const word& regionPrefix,
123  const wordList& fieldNames,
124  const wordList& fieldTypes,
125  const PtrList<dimensionSet>& fieldDimensions,
126  const caseInfo& cInfo,
127  const boundaryTemplates& bcTemplates
128 )
129 {
130  Info<< " Generating field files" << nl << endl;
131 
132  // Create files
133  mkDir(runTime.path()/runTime.timeName()/regionName);
134  forAll(fieldNames, i)
135  {
136  const_cast<word&>(IOdictionary::typeName) =
137  getClassType(fieldTypes[i]);
138 
139  dictionary field;
140 
141  fileName regionPath = "/";
142 
143  if (regionName != word::null)
144  {
145  regionPath += regionName + '/';
146  }
147 
148  field.add
149  (
150  "#include",
151  "${FOAM_CASE}/system" + regionPath + "caseProperties"
152  );
153 
154  field.add("dimensions", fieldDimensions[i]);
155 
156  string iField("${:initialConditions." + fieldNames[i] + '}');
157  field.add("internalField", iField.c_str());
158 
161  (
162  regionPrefix,
163  fieldNames[i],
164  bcTemplates
165  );
166 
167  field.add("boundaryField", boundaryField);
168 
169  // Expand all of the dictionary redirections and remove unnecessary
170  // entries
171  OStringStream os;
172  os << field;
173 
175  dictionary field2(IStringStream(os.str())());
177  field2.remove("#include");
178  field2.remove("initialConditions");
179  field2.remove("boundaryConditions");
180 
181  // Construct and write field dictionary
182  IOdictionary fieldOut
183  (
184  IOobject
185  (
186  fieldNames[i],
187  "0",
188  regionName,
189  runTime,
191  ),
192  field2
193  );
194 
195  fieldOut.regIOobject::writeObject
196  (
200  );
201  }
202 }
203 
204 
205 // Main program:
206 int main(int argc, char *argv[])
207 {
209  (
210  "templateDir",
211  "file",
212  "read case set-up templates from specified location"
213  );
214 
215  #include "setRootCase.H"
216  #include "createTime.H"
217 
218  Info<< "Reading controlDict" << nl << endl;
219 
221  (
222  IOobject
223  (
224  "controlDict",
225  runTime.system(),
226  runTime,
229  )
230  );
231 
232  fileName baseDir
233  (
234  "${WM_PROJECT_DIR}/etc/caseDicts/createZeroDirectoryTemplates"
235  );
236  if (args.optionFound("templateDir"))
237  {
238  baseDir = args["templateDir"];
239  }
240 
241  baseDir.expand();
242  baseDir.toAbsolute();
243 
244  if (!isDir(baseDir))
245  {
247  << "templateDir " << baseDir
248  << " should point to the folder containing the "
249  << "case set-up templates" << exit(FatalError);
250  }
251 
252  // Keep variable substitutions - delay until after creation of controlDict
253  // to allow #include files to be processed
255 
256  // Read the solver
257  const word& solverName = controlDict.lookup("application");
258 
259  // Generate solver template
260  const solverTemplate solver(baseDir, runTime, solverName);
261 
262  // Read the boundary condition templates
263  const boundaryTemplates bcTemplates
264  (
265  baseDir,
266  runTime,
267  solver.type()
268  );
269 
270  Info<< endl;
271 
272  const label nRegion = solver.nRegion();
273  for (label regionI = 0; regionI < nRegion; regionI++)
274  {
275  const word& regionName = solver.regionName(regionI);
276 
277  if (regionName == word::null)
278  {
279  Info<< "Region: " << polyMesh::defaultRegion << " (default)"
280  << endl;
281  }
282  else
283  {
284  Info<< "Region: " << regionName << endl;
285  }
286 
287  // Read the case set-up info for the current region
288  const caseInfo cInfo(runTime, regionName);
289 
290  cInfo.checkPatches(solver.regionType(regionI), bcTemplates);
291 
292  createFieldFiles
293  (
294  runTime,
295  regionName,
296  solver.regionType(regionI),
297  solver.fieldNames(regionI),
298  solver.fieldTypes(regionI),
299  solver.fieldDimensions(regionI),
300  cInfo,
301  bcTemplates
302  );
303  }
304 
305  Info<< "End\n" << endl;
306 
307  return 0;
308 }
309 
310 
311 // ************************************************************************* //
volFields.H
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
solverTemplate.H
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
caseInfo.H
Foam::solverTemplate
Class to store solver template specifications.
Definition: solverTemplate.H:51
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:108
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOstream::currentVersion
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
Foam::OStringStream::str
string str() const
Return the string.
Definition: OStringStream.H:107
regionName
Foam::word regionName
Definition: createNamedDynamicFvMesh.H:1
Foam::IOstream::ASCII
@ ASCII
Definition: IOstream.H:88
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::caseInfo::generateBoundaryField
dictionary generateBoundaryField(const word &regionPrefix, const word &fieldName, const boundaryTemplates &bcTemplates) const
Generate boundary field (dictionary)
Foam::TimePaths::system
const word & system() const
Return system name.
Definition: TimePaths.H:120
controlDict
runTime controlDict().lookup("adjustTimeStep") >> adjustTimeStep
Foam::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:111
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
argList.H
main
int main(int argc, char *argv[])
Definition: postCalc.C:54
Foam::PtrList
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
Foam::FatalError
error FatalError
Foam::caseInfo::checkPatches
void checkPatches(const word &regionPrefix, const boundaryTemplates &bcTemplates) const
Check patches.
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
boundaryField
cellIbMask *cellIbMaskExt *faceIbMask *cellIbMask boundaryField().evaluateCoupled()
boundaryTemplates.H
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::entry::disableFunctionEntries
static int disableFunctionEntries
Definition: entry.H:83
Foam::isDir
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:615
Foam::caseInfo
Class to hold information related to the simaulation case.
Definition: caseInfo.H:54
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
IOdictionary.H
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::Time::path
fileName path() const
Return path.
Definition: Time.H:281
Foam::Time::timeName
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:741
Foam::fieldNames
wordList fieldNames(const IOobjectList &objects, const bool syncPar)
Get sorted names of fields of type. If syncPar and running in parallel.
Definition: ReadFields.C:36
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:50
Foam::OStringStream
Output to memory buffer stream.
Definition: OStringStream.H:49
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
createTime.H
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::IOstream::UNCOMPRESSED
@ UNCOMPRESSED
Definition: IOstream.H:195
Foam::GeometricField
Generic GeometricField class.
Definition: surfaceFieldsFwd.H:52
args
Foam::argList args(argc, argv)
Foam::mkDir
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:419
Foam::boundaryTemplates
Class to store boundary template specifications.
Definition: boundaryTemplates.H:53
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729