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(const fvMesh& mesh)
41 {
42  typeIOobject<IOdictionary> io
43  (
44  typeName,
45  mesh.time().constant(),
46  mesh,
49  );
50 
51  if (io.headerOk())
52  {
53  Info<< indentOrNl << "Constructing " << typeName << " from "
54  << io.relativeObjectPath().c_str() << endl;
55 
56  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
57  return io;
58  }
59  else
60  {
61  const fileName preferredPath =
62  mesh.time().constant()/io.db().dbDir()/io.local()/io.name();
63 
64  // For backward-compatibility
65  // check if the fvOptions file is in constant
66  io.rename("fvOptions");
67 
68  if (io.headerOk())
69  {
70  Warning
71  << indentOrNl << "Constructing " << typeName << " from "
72  << io.relativeObjectPath() << " rather than "
73  << preferredPath << endl;
74 
75  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
76  return io;
77  }
78  else
79  {
80  // For backward-compatibility
81  // check if the fvOptions file is in system
82  io.instance() = mesh.time().system();
83 
84  if (io.headerOk())
85  {
86  Warning
87  << indentOrNl << "Constructing " << typeName << " from "
88  << io.relativeObjectPath() << " rather than "
89  << preferredPath << endl;
90 
91  io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
92  return io;
93  }
94  else
95  {
96  io.rename(typeName);
97  io.readOpt() = IOobject::NO_READ;
98  return io;
99  }
100  }
101  }
102 }
103 
104 
105 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
106 
107 void Foam::fvModels::checkApplied() const
108 {
109  const label timeIndex =
110  mesh().time().subCycling()
111  ? mesh().time().prevTimeState().timeIndex()
112  : mesh().time().timeIndex();
113 
114  if (timeIndex > checkTimeIndex_)
115  {
116  const PtrListDictionary<fvModel>& modelList(*this);
117 
118  forAll(modelList, i)
119  {
120  const fvModel& model = modelList[i];
121 
122  wordHashSet notAddSupFields(model.addSupFields());
123  notAddSupFields -= addSupFields_[i];
124 
125  forAllConstIter(wordHashSet, notAddSupFields, iter)
126  {
128  << "Model " << model.name()
129  << " defined for field " << iter.key()
130  << " but never used" << endl;
131  }
132  }
133 
134  checkTimeIndex_ = timeIndex;
135  }
136 }
137 
138 
139 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
140 
142 (
143  const fvMesh& mesh
144 )
145 :
147  <
148  fvMesh,
150  fvModels,
152  >
153  (
154  createIOobject(mesh),
155  mesh
156  ),
158  checkTimeIndex_(mesh.time().timeIndex() + 1),
159  addSupFields_()
160 {
161  const bool readFromFvModels = IOobject::name() == typeName;
162 
163  const dictionary& dict = *this;
164 
165  // Count number of active fvModels
166  label count = 0;
168  {
169  if (iter().isDict())
170  {
171  count++;
172  }
173  }
174 
176 
177  addSupFields_.setSize(count);
178 
179  printDictionary print(*this);
180 
181  label i = 0;
183  {
184  if (iter().isDict())
185  {
186  const word& name = iter().keyword();
187  const dictionary& modelDict = iter().dict();
188 
189  const word modelType(modelDict.lookup("type"));
190 
191  if
192  (
193  readFromFvModels
194  || !fvConstraint::dictionaryConstructorTablePtr_->found
195  (
196  modelType
197  )
198  )
199  {
201  (
202  i,
203  name,
204  fvModel::New(name, mesh, modelDict).ptr()
205  );
206 
207  addSupFields_.set(i, new wordHashSet());
208 
209  i++;
210  }
211  }
212  }
213 
215  addSupFields_.setSize(i);
216 
217  if (!readFromFvModels)
218  {
219  // If the fvOptions file was read re-name to fvModels
220  rename(typeName);
221  }
222 }
223 
224 
225 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
226 
227 bool Foam::fvModels::addsSupToField(const word& fieldName) const
228 {
229  const PtrListDictionary<fvModel>& modelList(*this);
230 
231  forAll(modelList, i)
232  {
233  if (modelList[i].addsSupToField(fieldName))
234  {
235  return true;
236  }
237  }
238 
239  return false;
240 }
241 
242 
243 Foam::scalar Foam::fvModels::maxDeltaT() const
244 {
245  const PtrListDictionary<fvModel>& modelList(*this);
246 
247  scalar maxDeltaT = vGreat;
248 
249  forAll(modelList, i)
250  {
251  maxDeltaT = min(maxDeltaT, modelList[i].maxDeltaT());
252  }
253 
254  return maxDeltaT;
255 }
256 
257 
259 {
260  PtrListDictionary<fvModel>& modelList(*this);
261 
262  forAll(modelList, i)
263  {
264  modelList[i].preUpdateMesh();
265  }
266 }
267 
268 
270 {
271  bool allOk = true;
272 
273  PtrListDictionary<fvModel>& modelList(*this);
274 
275  forAll(modelList, i)
276  {
277  allOk = allOk && modelList[i].movePoints();
278  }
279 
280  return allOk;
281 }
282 
283 
285 {
286  PtrListDictionary<fvModel>& modelList(*this);
287 
288  forAll(modelList, i)
289  {
290  modelList[i].topoChange(map);
291  }
292 }
293 
294 
296 {
297  PtrListDictionary<fvModel>& modelList(*this);
298 
299  forAll(modelList, i)
300  {
301  modelList[i].mapMesh(map);
302  }
303 }
304 
305 
307 {
308  PtrListDictionary<fvModel>& modelList(*this);
309 
310  forAll(modelList, i)
311  {
312  modelList[i].distribute(map);
313  }
314 }
315 
316 
318 {
319  if (regIOobject::read())
320  {
321  checkTimeIndex_ = mesh().time().timeIndex() + 1;
322 
323  bool allOk = true;
324 
325  PtrListDictionary<fvModel>& modelList(*this);
326 
327  forAll(modelList, i)
328  {
329  fvModel& model = modelList[i];
330  bool ok = model.read(subDict(model.name()));
331  allOk = (allOk && ok);
332  }
333  return allOk;
334  }
335  else
336  {
337  return false;
338  }
339 }
340 
341 
343 (
347  const bool write
348 ) const
349 {
350  bool allOk = true;
351 
352  const PtrListDictionary<fvModel>& modelList(*this);
353 
354  forAll(modelList, i)
355  {
356  const bool ok = modelList[i].write(write);
357  allOk = (allOk && ok);
358  }
359 
360  return allOk;
361 }
362 
363 
365 {
366  PtrListDictionary<fvModel>& modelList(*this);
367 
368  forAll(modelList, i)
369  {
370  modelList[i].correct();
371  }
372 }
373 
374 
375 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
376 
377 Foam::Ostream& Foam::operator<<
378 (
379  Ostream& os,
380  const fvModels& models
381 )
382 {
383  models.dictionary::write(os, false);
384  return os;
385 }
386 
387 
388 // ************************************************************************* //
#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:269
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the fvModels.
Definition: fvModels.C:343
virtual void correct()
Correct the fvModels.
Definition: fvModels.C:364
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: fvModels.C:284
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: fvModels.C:306
virtual scalar maxDeltaT() const
Return the maximum time-step for stable operation.
Definition: fvModels.C:243
virtual void preUpdateMesh()
Prepare for mesh update.
Definition: fvModels.C:258
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: fvModels.C:295
virtual bool addsSupToField(const word &fieldName) const
Return true if an fvModel adds a source term to the given.
Definition: fvModels.C:227
fvModels(const fvMesh &mesh)
Construct from fvMesh.
Definition: fvModels.C:142
virtual bool read()
Read the fvModels dictionary if it has changed.
Definition: fvModels.C:317
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