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-2026 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  typeIOobject<IOdictionary> io
46  (
47  typeName,
48  mesh.time().constant(),
49  mesh,
52  );
53 
54  if (io.headerOk())
55  {
56  Info<< indentOrNl << "Constructing " << typeName << " from "
57  << io.relativeObjectPath().c_str() << endl;
58 
59  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
60  return io;
61  }
62  else
63  {
64  const fileName preferredPath =
65  mesh.time().constant()/io.db().dbDir()/io.local()/io.name();
66 
67  // For backward-compatibility
68  // check if the fvOptions file is in constant
69  io.rename("fvOptions");
70 
71  if (io.headerOk())
72  {
73  Warning
74  << indentOrNl << "Constructing " << typeName << " from "
75  << io.relativeObjectPath() << " rather than "
76  << preferredPath << endl;
77 
78  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
79  return io;
80  }
81  else
82  {
83  // For backward-compatibility
84  // check if the fvOptions file is in system
85  io.instance() = mesh.time().system();
86 
87  if (io.headerOk())
88  {
89  Warning
90  << indentOrNl << "Constructing " << typeName << " from "
91  << io.relativeObjectPath() << " rather than "
92  << preferredPath << endl;
93 
94  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
95  return io;
96  }
97  else
98  {
99  io.rename(typeName);
100  io.readOpt() = IOobject::NO_READ;
101  return io;
102  }
103  }
104  }
105 }
106 
107 
108 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
109 
110 void Foam::fvModels::checkApplied() const
111 {
112  const label timeIndex =
113  mesh().time().subCycling()
114  ? mesh().time().prevTimeState().timeIndex()
115  : mesh().time().timeIndex();
116 
117  if (timeIndex > checkTimeIndex_)
118  {
119  const PtrListDictionary<fvModel>& modelList(*this);
120 
121  forAll(modelList, i)
122  {
123  const fvModel& model = modelList[i];
124 
125  wordHashSet notAddSupFields(model.addSupFields());
126  notAddSupFields -= addSupFields_[i];
127 
128  forAllConstIter(wordHashSet, notAddSupFields, iter)
129  {
131  << "Model " << model.name()
132  << " defined for field " << iter.key()
133  << " but never used" << endl;
134  }
135  }
136 
137  checkTimeIndex_ = timeIndex;
138  }
139 }
140 
141 
142 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
143 
145 (
146  const fvMesh& mesh
147 )
148 :
150  <
151  fvMesh,
153  fvModels,
155  >
156  (
157  createIOobject(mesh),
158  mesh
159  ),
161  checkTimeIndex_(mesh.time().timeIndex() + 1),
162  addSupFields_()
163 {
164  const bool readFromFvModels = IOobject::name() == typeName;
165 
166  const dictionary& dict = *this;
167 
168  // Count number of active fvModels
169  label count = 0;
171  {
172  if (iter().isDict())
173  {
174  count++;
175  }
176  }
177 
179 
180  addSupFields_.setSize(count);
181 
182  printDictionary print(*this);
183 
184  label i = 0;
186  {
187  if (iter().isDict())
188  {
189  const word& name = iter().keyword();
190  const dictionary& modelDict = iter().dict();
191 
192  const word modelType(modelDict.lookup("type"));
193 
194  if
195  (
196  readFromFvModels
197  || !fvConstraint::dictionaryConstructorTablePtr_->found
198  (
199  modelType
200  )
201  )
202  {
204  (
205  i,
206  name,
207  fvModel::New(name, mesh, modelDict).ptr()
208  );
209 
210  addSupFields_.set(i, new wordHashSet());
211 
212  i++;
213  }
214  }
215  }
216 
218  addSupFields_.setSize(i);
219 
220  if (!readFromFvModels)
221  {
222  // If the fvOptions file was read re-name to fvModels
223  rename(typeName);
224  }
225 }
226 
227 
228 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
229 
230 bool Foam::fvModels::addsSupToField(const word& fieldName) const
231 {
232  const PtrListDictionary<fvModel>& modelList(*this);
233 
234  forAll(modelList, i)
235  {
236  if (modelList[i].addsSupToField(fieldName))
237  {
238  return true;
239  }
240  }
241 
242  return false;
243 }
244 
245 
246 Foam::scalar Foam::fvModels::maxDeltaT() const
247 {
248  const PtrListDictionary<fvModel>& modelList(*this);
249 
250  scalar maxDeltaT = vGreat;
251 
252  forAll(modelList, i)
253  {
254  maxDeltaT = min(maxDeltaT, modelList[i].maxDeltaT());
255  }
256 
257  return maxDeltaT;
258 }
259 
260 
262 {
263  PtrListDictionary<fvModel>& modelList(*this);
264 
265  forAll(modelList, i)
266  {
267  modelList[i].preUpdateMesh();
268  }
269 }
270 
271 
273 {
274  bool allOk = true;
275 
276  PtrListDictionary<fvModel>& modelList(*this);
277 
278  forAll(modelList, i)
279  {
280  allOk = allOk && modelList[i].movePoints();
281  }
282 
283  return allOk;
284 }
285 
286 
288 {
289  PtrListDictionary<fvModel>& modelList(*this);
290 
291  forAll(modelList, i)
292  {
293  modelList[i].topoChange(map);
294  }
295 }
296 
297 
299 {
300  PtrListDictionary<fvModel>& modelList(*this);
301 
302  forAll(modelList, i)
303  {
304  modelList[i].mapMesh(map);
305  }
306 }
307 
308 
310 {
311  PtrListDictionary<fvModel>& modelList(*this);
312 
313  forAll(modelList, i)
314  {
315  modelList[i].distribute(map);
316  }
317 }
318 
319 
321 {
322  if (regIOobject::read())
323  {
324  checkTimeIndex_ = mesh().time().timeIndex() + 1;
325 
326  bool allOk = true;
327 
328  PtrListDictionary<fvModel>& modelList(*this);
329 
330  forAll(modelList, i)
331  {
332  fvModel& model = modelList[i];
333  bool ok = model.read(subDict(model.name()));
334  allOk = (allOk && ok);
335  }
336  return allOk;
337  }
338  else
339  {
340  return false;
341  }
342 }
343 
344 
346 (
350  const bool write
351 ) const
352 {
353  bool allOk = true;
354 
355  const PtrListDictionary<fvModel>& modelList(*this);
356 
357  forAll(modelList, i)
358  {
359  const bool ok = modelList[i].write(write);
360  allOk = (allOk && ok);
361  }
362 
363  return allOk;
364 }
365 
366 
368 {
369  PtrListDictionary<fvModel>& modelList(*this);
370 
371  forAll(modelList, i)
372  {
373  modelList[i].correct();
374  }
375 }
376 
377 
378 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
379 
380 Foam::Ostream& Foam::operator<<
381 (
382  Ostream& os,
383  const fvModels& models
384 )
385 {
386  models.dictionary::write(os, false);
387  return os;
388 }
389 
390 
391 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
Templated abstract base-class for demand-driven mesh objects used to automate their allocation to the...
static bool found(const word &name, const fvMesh &mesh)
Return true if the DemandDrivenMeshObject with the given name.
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:57
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:119
const word & name() const
Return name.
Definition: IOobject.H:307
Version number type.
Definition: IOstream.H:97
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Template dictionary class which manages the storage associated with it.
autoPtr< T > set(const label, const word &key, T *)
Set element to pointer provided and return old element.
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
static const word & constant()
Return constant name.
Definition: TimePaths.H:122
static const word & system()
Return system name.
Definition: TimePaths.H:112
label timeIndex() const
Return current time index.
Definition: TimeStateI.H:28
const TimeState & prevTimeState() const
Return previous TimeState if time is being sub-cycled.
Definition: Time.H:449
bool subCycling() const
Return true if time currently being sub-cycled, otherwise false.
Definition: Time.H:443
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:111
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:669
bool isDict(const word &) const
Check if entry is a sub-dictionary.
Definition: dictionary.C:732
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:98
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:433
Finite volume model abstract base class.
Definition: fvModel.H:60
static autoPtr< fvModel > New(const word &name, const fvMesh &mesh, const dictionary &dict)
Return a reference to the selected fvModel.
Definition: fvModel.C:92
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvModel.C:196
const word & name() const
Return const access to the source name.
Definition: fvModelI.H:57
Finite volume models.
Definition: fvModels.H:69
virtual bool movePoints()
Update for mesh motion.
Definition: fvModels.C:272
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the fvModels.
Definition: fvModels.C:346
virtual void correct()
Correct the fvModels.
Definition: fvModels.C:367
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: fvModels.C:287
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: fvModels.C:309
virtual scalar maxDeltaT() const
Return the maximum time-step for stable operation.
Definition: fvModels.C:246
virtual void preUpdateMesh()
Prepare for mesh update.
Definition: fvModels.C:261
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: fvModels.C:298
virtual bool addsSupToField(const word &fieldName) const
Return true if an fvModel adds a source term to the given.
Definition: fvModels.C:230
fvModels(const fvMesh &mesh)
Construct from fvMesh.
Definition: fvModels.C:145
virtual bool read()
Read the fvModels dictionary if it has changed.
Definition: fvModels.C:320
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Enables the printing of a dictionary and subsequently looked-up defaulted entries.
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:393
virtual bool read()
Read object.
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
scalar maxDeltaT
#define WarningInFunction
Report a warning using Foam::Warning.
const dimensionSet time
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
Namespace for OpenFOAM.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
messageStream Info
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:210
Ostream & indentOrNl(Ostream &os)
Indent stream or add newline if indent level == 0.
Definition: Ostream.H:250
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)
messageStream Warning
label timeIndex
Definition: getTimeIndex.H:4
dictionary dict