solverTemplate.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 \*---------------------------------------------------------------------------*/
28 
29 #include "solverTemplate.H"
30 #include "Time.H"
31 #include "IOPtrList.H"
32 #include "polyMesh.H"
33 #include "regionProperties.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 const Foam::Enum
38 <
40 >
42 ({
43  { solverType::stCompressible, "compressible" },
44  { solverType::stIncompressible, "incompressible" },
45  { solverType::stBuoyant, "buoyant" },
46  { solverType::stUnknown, "unknown" },
47 });
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 Foam::word Foam::solverTemplate::readFromDict
53 (
54  IOobject& dictHeader,
55  const word& entryName
56 ) const
57 {
58  if (!dictHeader.typeHeaderOk<IOdictionary>(true))
59  {
61  << "Unable to open file "
62  << dictHeader.objectPath()
63  << exit(FatalError);
64  }
65 
66  IOdictionary dict(dictHeader);
67  return dict.get<word>(entryName);
68 }
69 
70 
71 Foam::dictionary Foam::solverTemplate::readFluidFieldTemplates
72 (
73  const word& regionName,
74  const fileName& baseDir,
75  const dictionary& solverDict,
76  const Time& runTime
77 ) const
78 {
79  Info<< " Reading fluid field templates";
80 
81  if (regionName == word::null)
82  {
83  Info<< endl;
84  }
85  else
86  {
87  Info<< " for region " << regionName << endl;
88  }
89 
90  dictionary fieldTemplates = solverDict.subDict("fluidFields");
91 
92  const fileName turbModelDir(baseDir/"models"/"turbulence");
93 
94  const dictionary fieldModels(solverDict.subDict("fluidModels"));
95 
96  word turbModel("laminar"); // default to laminar
97  word turbType("none");
98 
99  if (fieldModels.readIfPresent("turbulenceModel", turbType))
100  {
101  if (turbType == "turbulenceModel")
102  {
103  IOdictionary turbPropDict
104  (
105  IOobject
106  (
107  // turbulenceModel::propertiesName
108  "turbulenceProperties",
109  runTime.constant(),
110  regionName,
111  runTime,
114  false
115  )
116  );
117 
118  const word modelType(turbPropDict.get<word>("simulationType"));
119 
120  if (modelType == "laminar")
121  {
122  // Leave as laminar
123  }
124  else if (modelType == "RAS")
125  {
126  // "RASModel" for v2006 and earlier
127  turbPropDict.subDict(modelType)
128  .readCompat("model", {{"RASModel", -2006}}, turbModel);
129  }
130  else if (modelType == "LES")
131  {
132  // "LESModel" for v2006 and earlier
133  turbPropDict.subDict(modelType)
134  .readCompat("model", {{"LESModel", -2006}}, turbModel);
135  }
136  else
137  {
139  << "Unhandled turbulence model option " << modelType
140  << ". Valid options are laminar, RAS, LES"
141  << exit(FatalError);
142  }
143  }
144  else
145  {
147  << "Unhandled turbulence model option " << turbType
148  << ". Valid options are turbulenceModel"
149  << exit(FatalError);
150  }
151  }
152 
153  Info<< " Selecting " << turbType << ": " << turbModel << endl;
154 
155  IOdictionary turbModelDict
156  (
157  IOobject
158  (
159  fileName(turbModelDir/turbModel),
160  runTime,
162  )
163  );
164 
165  // Merge common fluid fields
166  fieldTemplates.merge(turbModelDict.subDict("fluidFields"));
167 
168  // Merge specific compressible or incompressible fluid fields
169  switch (solverType_)
170  {
171  case stIncompressible:
172  {
173  fieldTemplates.merge(turbModelDict.subDict("incompressibleFields"));
174  break;
175  }
176  case stCompressible:
177  case stBuoyant:
178  {
179  fieldTemplates.merge(turbModelDict.subDict("compressibleFields"));
180  break;
181  }
182  default:
183  {
184  // do nothing
185  }
186  }
187 
188  return fieldTemplates;
189 }
190 
191 
192 Foam::dictionary Foam::solverTemplate::readSolidFieldTemplates
193 (
194  const word& regionName,
195  const dictionary& solverDict
196 ) const
197 {
198  Info<< " Reading solid field templates for region " << regionName
199  << endl;
200 
201  // nothing more to do for solids
202  return solverDict.subDict("solidFields");
203 }
204 
205 
206 void Foam::solverTemplate::setRegionProperties
207 (
208  const dictionary& fieldDict,
209  const word& regionType,
210  const word& regionName,
211  const label regionI
212 )
213 {
214  regionTypes_[regionI] = regionType,
215  regionNames_[regionI] = regionName,
216  fieldNames_[regionI] = fieldDict.toc();
217  fieldTypes_[regionI].setSize(fieldNames_[regionI].size());
218  fieldDimensions_[regionI].setSize(fieldNames_[regionI].size());
219 
220  forAll(fieldNames_[regionI], i)
221  {
222  const word& fieldName = fieldNames_[regionI][i];
223  const dictionary& dict = fieldDict.subDict(fieldName);
224 
225  dict.readEntry("type", fieldTypes_[regionI][i]);
226  fieldDimensions_[regionI].set
227  (
228  i,
229  new dimensionSet(dict, "dimensions")
230  );
231  }
232 }
233 
234 
235 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
236 
238 (
239  const fileName& baseDir,
240  const Time& runTime,
241  const word& solverName
242 )
243 :
244  solverType_(stUnknown),
245  multiRegion_(false),
246  regionTypes_(),
247  fieldNames_(),
248  fieldTypes_(),
249  fieldDimensions_()
250 {
251  IOdictionary solverDict
252  (
253  IOobject
254  (
255  fileName(baseDir/"solvers"/solverName),
256  runTime,
258  )
259  );
260 
261  Info<< "Selecting " << solverName << ": ";
262 
263  solverType_ = solverTypeNames_.get("solverType", solverDict);
264  multiRegion_ = solverDict.get<bool>("multiRegion");
265 
266  Info<< solverTypeNames_[solverType_];
267  if (multiRegion_)
268  {
269  Info<< ", multi-region";
270  }
271  else
272  {
273  Info<< ", single-region";
274  }
275 
276  Info<< " case" << endl;
277 
278  if (multiRegion_)
279  {
280  // read regionProperties
281  regionProperties rp(runTime);
282 
283  const wordList fluidNames(rp["fluid"]);
284  const wordList solidNames(rp["solid"]);
285 
286  const label nRegion = fluidNames.size() + solidNames.size();
287 
288  regionTypes_.setSize(nRegion);
289  regionNames_.setSize(nRegion);
290  fieldNames_.setSize(nRegion);
291  fieldTypes_.setSize(nRegion);
292  fieldDimensions_.setSize(nRegion);
293 
294  // read templates for solver lists of available
295  // - fields and dimensions required for the solver
296  // - models
297  label regionI = 0;
298  forAll(fluidNames, i)
299  {
300  const dictionary fieldDict
301  (
302  readFluidFieldTemplates
303  (
304  fluidNames[i],
305  baseDir,
306  solverDict,
307  runTime
308  )
309  );
310 
311  setRegionProperties(fieldDict, "fluid", fluidNames[i], regionI++);
312  }
313 
314  forAll(solidNames, i)
315  {
316  const dictionary fieldDict
317  (
318  readSolidFieldTemplates
319  (
320  solidNames[i],
321  solverDict
322  )
323  );
324  setRegionProperties(fieldDict, "solid", solidNames[i], regionI++);
325  }
326  }
327  else
328  {
329  regionTypes_.setSize(1);
330  regionNames_.setSize(1);
331  fieldNames_.setSize(1);
332  fieldTypes_.setSize(1);
333  fieldDimensions_.setSize(1);
334 
335  // read templates for solver lists of available
336  // - fields and dimensions required for the solver
337  // - models
338  const dictionary fieldDict
339  (
340  readFluidFieldTemplates
341  (
342  word::null, // use region = null for single-region cases
343  baseDir,
344  solverDict,
345  runTime
346  )
347  );
348 
349  setRegionProperties(fieldDict, "fluid", word::null, 0);
350  }
351 }
352 
353 
354 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
355 
357 {
358  return solverTypeNames_[solverType_];
359 }
360 
361 
363 {
364  return multiRegion_;
365 }
366 
367 
368 Foam::label Foam::solverTemplate::nRegion() const
369 {
370  return regionTypes_.size();
371 }
372 
373 
374 const Foam::word& Foam::solverTemplate::regionType(const label regionI) const
375 {
376  return regionTypes_[regionI];
377 }
378 
379 
380 const Foam::word& Foam::solverTemplate::regionName(const label regionI) const
381 {
382  return regionNames_[regionI];
383 }
384 
385 
387 (
388  const label regionI
389 ) const
390 {
391  return fieldNames_[regionI];
392 }
393 
394 
396 (
397  const label regionI
398 ) const
399 {
400  return fieldTypes_[regionI];
401 }
402 
403 
405 (
406  const label regionI
407 ) const
408 {
409  return fieldDimensions_[regionI];
410 }
411 
412 
413 // ************************************************************************* //
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:191
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:53
rp
regionProperties rp(runTime)
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
regionProperties.H
solverTemplate.H
Foam::solverTemplate::fieldNames
const wordList & fieldNames(const label regionI) const
Foam::solverTemplate::solverType
solverType
Definition: solverTemplate.H:54
Foam::solverTemplate::solverTypeNames_
static const Enum< solverType > solverTypeNames_
Definition: solverTemplate.H:63
Foam::solverTemplate::fieldDimensions
const PtrList< dimensionSet > & fieldDimensions(const label regionI) const
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
polyMesh.H
Foam::solverTemplate::regionName
const word & regionName(const label regionI) const
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:58
regionName
Foam::word regionName
Definition: createNamedDynamicFvMesh.H:1
IOPtrList.H
Foam::Info
messageStream Info
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.C:453
Foam::solverTemplate::solverTemplate
solverTemplate(const fileName &baseDir, const Time &runTime, const word &regionName)
Foam::dictionary::readEntry
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
Definition: dictionaryTemplates.C:295
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:55
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:119
Foam::solverTemplate::fieldTypes
const wordList & fieldTypes(const label regionI) const
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::solverTemplate::nRegion
label nRegion() const
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
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::word::null
static const word null
Definition: word.H:78
Foam::solverTemplate::type
word type() const
Foam::solverTemplate::multiRegion
bool multiRegion() const
Foam::solverTemplate::regionType
const word & regionType(const label regionI) const
Foam::TimePaths::constant
const word & constant() const
Definition: TimePathsI.H:89
solidNames
const wordList solidNames(rp["solid"])
fluidNames
const wordList fluidNames(rp["fluid"])
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181