cellModels.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) 2017 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "cellModel.H"
29 #include "etcFiles.H"
30 #include "IFstream.H"
31 #include "HashSet.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 Foam::PtrList<Foam::cellModel> Foam::cellModel::models_;
36 
37 Foam::List<const Foam::cellModel*> Foam::cellModel::modelPtrs_;
38 
40 ({
41  { modelType::UNKNOWN, "unknown" },
42  { modelType::HEX, "hex" },
43  { modelType::WEDGE, "wedge" },
44  { modelType::PRISM, "prism" },
45  { modelType::PYR, "pyr" },
46  { modelType::TET, "tet" },
47  { modelType::TETWEDGE, "tetWedge" },
48  { modelType::SPLITHEX, "splitHex" },
49 });
50 
51 
52 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
53 
54 void Foam::cellModel::constructModels()
55 {
56  if (models_.size())
57  {
59  << "attempt to re-construct cellModeller when it already exists"
60  << exit(FatalError);
61  }
62 
63  IFstream is(findEtcFile("cellModels", true)); // Mandatory file
64 
65  PtrList<cellModel> newPtrs(is);
66  models_.swap(newPtrs);
67 
70 
71 
72  // Build two lookups: by index, by name
73  // Since there are relatively few models, use straight lookup for the index
74  // and a linear (non-hashed) search for the name.
75  // Lookup by name is less likely than lookup by enum anyhow.
76 
77  label maxIndex = 0;
78  forAll(models_, i)
79  {
80  if (maxIndex < models_[i].index())
81  {
82  maxIndex = models_[i].index();
83  }
84  }
85 
86  modelPtrs_.clear();
87  modelPtrs_.setSize(maxIndex+1, nullptr);
88 
89  wordHashSet used(2*maxIndex);
90 
91  forAll(models_, i)
92  {
93  const label modelIndex = models_[i].index();
94  const word& modelName = models_[i].name();
95  const cellModel* ptr = &models_[i];
96 
97  if (used.insert(modelName))
98  {
99  if (modelPtrs_[modelIndex])
100  {
102  << "more than one model share the index "
103  << modelIndex
104  << exit(FatalError);
105  }
106 
107  modelPtrs_[modelIndex] = ptr;
108  }
109  else
110  {
112  << "more than one model share the name "
113  << modelName
114  << exit(FatalError);
115  }
116  }
117 }
118 
119 
121 {
122  return ptr(label(model));
123 }
124 
125 
126 const Foam::cellModel* Foam::cellModel::ptr(const word& modelName)
127 {
128  if (models_.empty())
129  {
130  constructModels();
131  }
132 
133  const label n = models_.size();
134  for (label i = 0; i < n; ++i)
135  {
136  if (models_[i].name() == modelName)
137  {
138  return &(models_[i]);
139  }
140  }
141 
142  return nullptr;
143 }
144 
145 
146 const Foam::cellModel* Foam::cellModel::ptr(const label modelIndex)
147 {
148  if (models_.empty())
149  {
150  constructModels();
151  }
152 
153  return (modelIndex < modelPtrs_.size() ? modelPtrs_[modelIndex] : nullptr);
154 }
155 
156 
158 {
159  const cellModel* p = ptr(model);
160 
161  if (!p)
162  {
164  << "No such cellModel: " << modelNames[model]
165  << exit(FatalError);
166  }
167 
168  return *p;
169 }
170 
171 
172 const Foam::cellModel& Foam::cellModel::ref(const word& modelName)
173 {
174  const cellModel* p = ptr(modelName);
175 
176  if (!p)
177  {
179  << "No such cellModel: " << modelName
180  << exit(FatalError);
181  }
182 
183  return *p;
184 }
185 
186 
187 const Foam::cellModel& Foam::cellModel::ref(const label modelIndex)
188 {
189  const cellModel* p = ptr(modelIndex);
190 
191  if (!p)
192  {
194  << "No such cellModel: " << modelIndex
195  << exit(FatalError);
196  }
197 
198  return *p;
199 }
200 
201 
202 // ************************************************************************* //
Foam::cellModel::index
label index() const noexcept
Return index of model in the model list.
Definition: cellModelI.H:37
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::cellModel::modelNames
static const Enum< modelType > modelNames
Names of commonly used cellModels corresponding to modelType.
Definition: cellModel.H:92
Foam::cellModel::ptr
static const cellModel * ptr(const modelType model)
Look up pointer to cellModel by enumeration, or nullptr on failure.
Definition: cellModels.C:120
Foam::cellModel::cellModel
cellModel(Istream &is)
Construct from Istream.
Definition: cellModelIO.C:34
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::findEtcFile
fileName findEtcFile(const fileName &name, const bool mandatory=false, unsigned short location=0777)
Search for a single FILE within the etc directories.
Definition: etcFiles.C:446
cellModel.H
Foam::PtrList< Foam::cellModel >
Foam::cellModel::ref
static const cellModel & ref(const modelType model)
Look up reference to cellModel by enumeration. Fatal on failure.
Definition: cellModels.C:157
IFstream.H
HashSet.H
Foam::FatalError
error FatalError
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
etcFiles.H
Functions to search 'etc' directories for configuration files etc.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::List< const Foam::cellModel * >
Foam::HashSetOps::used
labelHashSet used(const bitSet &select)
Convert a bitset to a labelHashSet of the indices used.
Definition: HashOps.C:35
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
Foam::wordHashSet
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:77
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::cellModel::modelType
modelType
Enumeration of commonly used cellModel types.
Definition: cellModel.H:78
Foam::cellModel
Maps a geometry to a set of cell primitives.
Definition: cellModel.H:72