All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fvModels.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2021 OpenFOAM Foundation
6  \\/ M anipulation |
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 \*---------------------------------------------------------------------------*/
25 
26 #include "fvModels.H"
27 #include "fvConstraint.H"
28 #include "fvMesh.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
35 }
36 
37 
38 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
39 
40 Foam::IOobject Foam::fvModels::createIOobject
41 (
42  const fvMesh& mesh
43 ) const
44 {
45  IOobject io
46  (
47  typeName,
48  mesh.time().constant(),
49  mesh,
52  );
53 
54  if (io.typeHeaderOk<IOdictionary>(true))
55  {
56  Info<< "Creating fvModels from "
57  << io.instance()/io.name() << nl
58  << endl;
59 
60  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
61  return io;
62  }
63  else
64  {
65  // For backward-compatibility
66  // check if the fvOptions file is in constant
67  io.rename("fvOptions");
68 
69  if (io.typeHeaderOk<IOdictionary>(true))
70  {
71  Warning
72  << "Creating fvModels from "
73  << io.instance()/io.name() << nl
74  << endl;
75 
76  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
77  return io;
78  }
79  else
80  {
81  // For backward-compatibility
82  // check if the fvOptions file is in system
83  io.instance() = mesh.time().system();
84 
85  if (io.typeHeaderOk<IOdictionary>(true))
86  {
87  Warning
88  << "Creating fvModels from "
89  << io.instance()/io.name()
90  << " rather than constant/fvModels"
91  << endl;
92 
93  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
94  return io;
95  }
96  else
97  {
98  io.rename(typeName);
99  io.readOpt() = IOobject::NO_READ;
100  return io;
101  }
102  }
103  }
104 }
105 
106 
107 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
108 
109 void Foam::fvModels::checkApplied() const
110 {
111  if (mesh().time().timeIndex() > checkTimeIndex_)
112  {
113  const PtrListDictionary<fvModel>& modelList(*this);
114 
115  forAll(modelList, i)
116  {
117  const fvModel& model = modelList[i];
118 
119  wordHashSet notAddSupFields(model.addSupFields());
120  notAddSupFields -= addSupFields_[i];
121 
122  forAllConstIter(wordHashSet, notAddSupFields, iter)
123  {
125  << "Model " << model.name()
126  << " defined for field " << iter.key()
127  << " but never used" << endl;
128  }
129  }
130 
131  checkTimeIndex_ = mesh().time().timeIndex();
132  }
133 }
134 
135 
136 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
137 
139 (
140  const fvMesh& mesh
141 )
142 :
144  (
145  mesh,
146  createIOobject(mesh)
147  ),
149  checkTimeIndex_(mesh.time().timeIndex() + 1),
150  addSupFields_()
151 {
152  readHeaderOk(IOstream::ASCII, typeName);
153 
154  const bool readFromFvModels(IOobject::name() == typeName);
155 
156  const dictionary& dict(*this);
157 
158  // Count number of active fvModels
159  label count = 0;
160  forAllConstIter(dictionary, dict, iter)
161  {
162  if (iter().isDict())
163  {
164  count++;
165  }
166  }
167 
169 
170  addSupFields_.setSize(count);
171 
172  label i = 0;
173  forAllConstIter(dictionary, dict, iter)
174  {
175  if (iter().isDict())
176  {
177  const word& name = iter().keyword();
178  const dictionary& modelDict = iter().dict();
179 
180  const word modelType(modelDict.lookup("type"));
181 
182  if
183  (
184  readFromFvModels
185  || !fvConstraint::dictionaryConstructorTablePtr_->found
186  (
187  modelType
188  )
189  )
190  {
192  (
193  i,
194  name,
195  fvModel::New(name, modelDict, mesh).ptr()
196  );
197 
198  addSupFields_.set(i, new wordHashSet());
199 
200  i++;
201  }
202  }
203  }
204 
206  addSupFields_.setSize(i);
207 
208  if (readFromFvModels)
209  {
210  // Add file watch on the fvModels dictionary for
211  // MUST_READ_IF_MODIFIED
212  addWatch();
213  }
214  else
215  {
216  // If the fvOptions file was read re-name to fvModels
217  rename(typeName);
218  }
219 }
220 
221 
222 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
223 
224 bool Foam::fvModels::addsSupToField(const word& fieldName) const
225 {
226  const PtrListDictionary<fvModel>& modelList(*this);
227 
228  forAll(modelList, i)
229  {
230  if (modelList[i].addsSupToField(fieldName))
231  {
232  return true;
233  }
234  }
235 
236  return false;
237 }
238 
239 
241 {
242  PtrListDictionary<fvModel>& modelList(*this);
243 
244  forAll(modelList, i)
245  {
246  modelList[i].preUpdateMesh();
247  }
248 }
249 
250 
252 {
253  PtrListDictionary<fvModel>& modelList(*this);
254 
255  forAll(modelList, i)
256  {
257  modelList[i].updateMesh(mpm);
258  }
259 }
260 
261 
263 {
264  bool allOk = true;
265 
266  PtrListDictionary<fvModel>& modelList(*this);
267 
268  forAll(modelList, i)
269  {
270  allOk = allOk && modelList[i].movePoints();
271  }
272 
273  return allOk;
274 }
275 
276 
278 {
279  is >> *this;
280  return !is.bad();
281 }
282 
283 
285 {
286  dictionary::write(os, false);
287  return os.good();
288 }
289 
290 
292 {
294  {
295  checkTimeIndex_ = mesh().time().timeIndex() + 1;
296 
297  bool allOk = true;
298 
299  PtrListDictionary<fvModel>& modelList(*this);
300 
301  forAll(modelList, i)
302  {
303  fvModel& model = modelList[i];
304  bool ok = model.read(subDict(model.name()));
305  allOk = (allOk && ok);
306  }
307  return allOk;
308  }
309  else
310  {
311  return false;
312  }
313 }
314 
315 
317 {
318  PtrListDictionary<fvModel>& modelList(*this);
319 
320  forAll(modelList, i)
321  {
322  modelList[i].correct();
323  }
324 }
325 
326 
327 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
328 
329 Foam::Ostream& Foam::operator<<
330 (
331  Ostream& os,
332  const fvModels& models
333 )
334 {
335  models.writeData(os);
336  return os;
337 }
338 
339 
340 // ************************************************************************* //
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:207
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
const word & name() const
Return name.
Definition: IOobject.H:303
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
const word & name() const
Return const access to the source name.
Definition: fvModelI.H:28
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
autoPtr< T > set(const label, const word &key, T *)
Set element to pointer provided and return old element.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvModel.C:158
virtual void correct()
Correct the fvModels.
Definition: fvModels.C:316
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
virtual void preUpdateMesh()
Prepare for mesh update.
Definition: fvModels.C:240
Finite volume model abstract base class.
Definition: fvModel.H:55
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:239
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Templated abstract base-class for optional mesh objects used to automate their allocation to the mesh...
Definition: MeshObject.H:85
dynamicFvMesh & mesh
A class for handling words, derived from string.
Definition: word.H:59
static autoPtr< fvModel > New(const word &name, const dictionary &dict, const fvMesh &mesh)
Return a reference to the selected fvModel.
Definition: fvModel.C:94
Finite volume models.
Definition: fvModels.H:60
virtual void updateMesh(const mapPolyMesh &)
Update for mesh changes.
Definition: fvModels.C:251
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:208
virtual bool readData(Istream &)
ReadData function which reads the fvModels dictionary.
Definition: fvModels.C:277
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
defineTypeNameAndDebug(combustionModel, 0)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
messageStream Warning
virtual bool writeData(Ostream &os) const
Write data to Ostream.
Definition: fvModels.C:284
Foam::fvModels & fvModels
virtual bool movePoints()
Update for mesh motion.
Definition: fvModels.C:262
label timeIndex() const
Return current time index.
Definition: TimeStateI.H:35
#define WarningInFunction
Report a warning using Foam::Warning.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
label timeIndex
Definition: getTimeIndex.H:4
messageStream Info
virtual bool addsSupToField(const word &fieldName) const
Return true if an fvModel adds a source term to the given.
Definition: fvModels.C:224
fvModels(const fvMesh &mesh)
Construct from components with list of field names.
Definition: fvModels.C:139
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
bool found
virtual bool read()
Read the fvModels dictionary if it has changed.
Definition: fvModels.C:291
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:844