fvConstraints.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 "fvConstraints.H"
27 #include "fvModel.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::fvConstraints::createIOobject
41 (
42  const fvMesh& mesh
43 ) const
44 {
45  typeIOobject<IOdictionary> io
46  (
47  typeName,
48  mesh.time().system(),
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().system()/io.db().dbDir()/io.local()/io.name();
66 
67  // For backward-compatibility
68  // check if the fvOptions file is in system
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 constant
85  io.instance() = mesh.time().constant();
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::fvConstraints::checkApplied() const
111 {
112  if (mesh().time().timeIndex() > checkTimeIndex_)
113  {
114  const PtrListDictionary<fvConstraint>& constraintList(*this);
115 
116  forAll(constraintList, i)
117  {
118  const fvConstraint& constraint = constraintList[i];
119 
120  wordHashSet notConstrainedFields(constraint.constrainedFields());
121  notConstrainedFields -= constrainedFields_[i];
122 
123  forAllConstIter(wordHashSet, notConstrainedFields, iter)
124  {
126  << "Constraint " << constraint.name()
127  << " defined for field " << iter.key()
128  << " but never used" << endl;
129  }
130  }
131 
132  checkTimeIndex_ = mesh().time().timeIndex();
133  }
134 }
135 
136 
137 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
138 
140 (
141  const fvMesh& mesh
142 )
143 :
145  <
146  fvMesh,
150  >
151  (
152  createIOobject(mesh),
153  mesh
154  ),
156  checkTimeIndex_(mesh.time().timeIndex() + 1),
157  constrainedFields_()
158 {
159  const bool readFromFvConstraints = IOobject::name() == typeName;
160 
161  const dictionary& dict = *this;
162 
163  // Count number of active fvConstraints
164  label count = 0;
166  {
167  if (iter().isDict())
168  {
169  count++;
170  }
171  }
172 
174 
175  constrainedFields_.setSize(count);
176 
177  printDictionary print(*this);
178 
179  label i = 0;
181  {
182  if (iter().isDict())
183  {
184  const word& name = iter().keyword();
185  const dictionary& constraintDict = iter().dict();
186 
187  const word constraintType(constraintDict.lookup("type"));
188 
189  if
190  (
191  readFromFvConstraints
192  || !fvModel::dictionaryConstructorTablePtr_->found
193  (
194  constraintType
195  )
196  )
197  {
199  (
200  i,
201  name,
202  fvConstraint::New(name, mesh, constraintDict).ptr()
203  );
204 
205  constrainedFields_.set(i, new wordHashSet());
206 
207  i++;
208  }
209  }
210  }
211 
213  constrainedFields_.setSize(i);
214 
215  if (!readFromFvConstraints)
216  {
217  // If the fvOptions file was read re-name to fvConstraints
218  rename(typeName);
219  }
220 }
221 
222 
223 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
224 
225 bool Foam::fvConstraints::constrainsField(const word& fieldName) const
226 {
227  const PtrListDictionary<fvConstraint>& constraintList(*this);
228 
229  forAll(constraintList, i)
230  {
231  if (constraintList[i].constrainsField(fieldName))
232  {
233  return true;
234  }
235  }
236 
237  return false;
238 }
239 
240 
242 {
243  bool allOk = true;
244 
245  PtrListDictionary<fvConstraint>& constraintList(*this);
246 
247  forAll(constraintList, i)
248  {
249  allOk = allOk && constraintList[i].movePoints();
250  }
251 
252  return allOk;
253 }
254 
255 
257 {
258  PtrListDictionary<fvConstraint>& constraintList(*this);
259 
260  forAll(constraintList, i)
261  {
262  constraintList[i].topoChange(map);
263  }
264 }
265 
266 
268 {
269  PtrListDictionary<fvConstraint>& constraintList(*this);
270 
271  forAll(constraintList, i)
272  {
273  constraintList[i].mapMesh(map);
274  }
275 }
276 
277 
279 {
280  PtrListDictionary<fvConstraint>& constraintList(*this);
281 
282  forAll(constraintList, i)
283  {
284  constraintList[i].distribute(map);
285  }
286 }
287 
288 
290 {
291  if (regIOobject::read())
292  {
293  checkTimeIndex_ = mesh().time().timeIndex() + 1;
294 
295  bool allOk = true;
296 
297  PtrListDictionary<fvConstraint>& constraintList(*this);
298 
299  forAll(constraintList, i)
300  {
301  fvConstraint& constraint = constraintList[i];
302  bool ok = constraint.read(subDict(constraint.name()));
303  allOk = (allOk && ok);
304  }
305  return allOk;
306  }
307  else
308  {
309  return false;
310  }
311 }
312 
313 
314 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
315 
316 Foam::Ostream& Foam::operator<<
317 (
318  Ostream& os,
319  const fvConstraints& constraints
320 )
321 {
322  constraints.writeData(os);
323  return os;
324 }
325 
326 
327 // ************************************************************************* //
#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
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 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
Finite volume options abstract base class.
Definition: fvConstraint.H:57
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvConstraint.C:163
const word & name() const
Return const access to the source name.
Definition: fvConstraintI.H:28
static autoPtr< fvConstraint > New(const word &name, const fvMesh &mesh, const dictionary &dict)
Return a reference to the selected fvConstraint.
Definition: fvConstraint.C:77
Finite volume constraints.
Definition: fvConstraints.H:68
virtual bool movePoints()
Update for mesh motion.
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
fvConstraints(const fvMesh &mesh)
Construct from fvMesh.
virtual bool constrainsField(const word &fieldName) const
Return true if an fvConstraint constrains the given field.
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
virtual bool read()
Read the fvConstraints dictionary if it has changed.
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
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)
#define WarningInFunction
Report a warning using Foam::Warning.
const dimensionSet time
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
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