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(const fvMesh& mesh)
41 {
42  typeIOobject<IOdictionary> io
43  (
44  typeName,
45  mesh.time().system(),
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().system()/io.db().dbDir()/io.local()/io.name();
63 
64  // For backward-compatibility
65  // check if the fvOptions file is in system
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 constant
82  io.instance() = mesh.time().constant();
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::fvConstraints::checkApplied() const
108 {
109  if (mesh().time().timeIndex() > checkTimeIndex_)
110  {
111  const PtrListDictionary<fvConstraint>& constraintList(*this);
112 
113  forAll(constraintList, i)
114  {
115  const fvConstraint& constraint = constraintList[i];
116 
117  wordHashSet notConstrainedFields(constraint.constrainedFields());
118  notConstrainedFields -= constrainedFields_[i];
119 
120  forAllConstIter(wordHashSet, notConstrainedFields, iter)
121  {
123  << "Constraint " << constraint.name()
124  << " defined for field " << iter.key()
125  << " but never used" << endl;
126  }
127  }
128 
129  checkTimeIndex_ = mesh().time().timeIndex();
130  }
131 }
132 
133 
134 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
135 
137 (
138  const fvMesh& mesh
139 )
140 :
142  <
143  fvMesh,
147  >
148  (
149  createIOobject(mesh),
150  mesh
151  ),
153  checkTimeIndex_(mesh.time().timeIndex() + 1),
154  constrainedFields_()
155 {
156  const bool readFromFvConstraints = IOobject::name() == typeName;
157 
158  const dictionary& dict = *this;
159 
160  // Count number of active fvConstraints
161  label count = 0;
163  {
164  if (iter().isDict())
165  {
166  count++;
167  }
168  }
169 
171 
172  constrainedFields_.setSize(count);
173 
174  printDictionary print(*this);
175 
176  label i = 0;
178  {
179  if (iter().isDict())
180  {
181  const word& name = iter().keyword();
182  const dictionary& constraintDict = iter().dict();
183 
184  const word constraintType(constraintDict.lookup("type"));
185 
186  if
187  (
188  readFromFvConstraints
189  || !fvModel::dictionaryConstructorTablePtr_->found
190  (
191  constraintType
192  )
193  )
194  {
196  (
197  i,
198  name,
199  fvConstraint::New(name, mesh, constraintDict).ptr()
200  );
201 
202  constrainedFields_.set(i, new wordHashSet());
203 
204  i++;
205  }
206  }
207  }
208 
210  constrainedFields_.setSize(i);
211 
212  if (!readFromFvConstraints)
213  {
214  // If the fvOptions file was read re-name to fvConstraints
215  rename(typeName);
216  }
217 }
218 
219 
220 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
221 
222 bool Foam::fvConstraints::constrainsField(const word& fieldName) const
223 {
224  const PtrListDictionary<fvConstraint>& constraintList(*this);
225 
226  forAll(constraintList, i)
227  {
228  if (constraintList[i].constrainsField(fieldName))
229  {
230  return true;
231  }
232  }
233 
234  return false;
235 }
236 
237 
239 {
240  bool allOk = true;
241 
242  PtrListDictionary<fvConstraint>& constraintList(*this);
243 
244  forAll(constraintList, i)
245  {
246  allOk = allOk && constraintList[i].movePoints();
247  }
248 
249  return allOk;
250 }
251 
252 
254 {
255  PtrListDictionary<fvConstraint>& constraintList(*this);
256 
257  forAll(constraintList, i)
258  {
259  constraintList[i].topoChange(map);
260  }
261 }
262 
263 
265 {
266  PtrListDictionary<fvConstraint>& constraintList(*this);
267 
268  forAll(constraintList, i)
269  {
270  constraintList[i].mapMesh(map);
271  }
272 }
273 
274 
276 {
277  PtrListDictionary<fvConstraint>& constraintList(*this);
278 
279  forAll(constraintList, i)
280  {
281  constraintList[i].distribute(map);
282  }
283 }
284 
285 
287 {
288  if (regIOobject::read())
289  {
290  checkTimeIndex_ = mesh().time().timeIndex() + 1;
291 
292  bool allOk = true;
293 
294  PtrListDictionary<fvConstraint>& constraintList(*this);
295 
296  forAll(constraintList, i)
297  {
298  fvConstraint& constraint = constraintList[i];
299  bool ok = constraint.read(subDict(constraint.name()));
300  allOk = (allOk && ok);
301  }
302  return allOk;
303  }
304  else
305  {
306  return false;
307  }
308 }
309 
310 
311 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
312 
313 Foam::Ostream& Foam::operator<<
314 (
315  Ostream& os,
316  const fvConstraints& constraints
317 )
318 {
319  constraints.writeData(os);
320  return os;
321 }
322 
323 
324 // ************************************************************************* //
#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