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-2022 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<< "Creating fvConstraints 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 system
67  io.rename("fvOptions");
68 
69  if (io.headerOk())
70  {
71  Warning
72  << "Creating fvConstraints 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 constant
83  io.instance() = mesh.time().constant();
84 
85  if (io.headerOk())
86  {
87  Warning
88  << "Creating fvConstraints from "
89  << io.instance()/io.name()
90  << " rather than system/fvConstraints"
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::fvConstraints::checkApplied() const
110 {
111  if (mesh().time().timeIndex() > checkTimeIndex_)
112  {
113  const PtrListDictionary<fvConstraint>& constraintList(*this);
114 
115  forAll(constraintList, i)
116  {
117  const fvConstraint& constraint = constraintList[i];
118 
119  wordHashSet notConstrainedFields(constraint.constrainedFields());
120  notConstrainedFields -= constrainedFields_[i];
121 
122  forAllConstIter(wordHashSet, notConstrainedFields, iter)
123  {
125  << "Constraint " << constraint.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  constrainedFields_()
151 {
152  readHeaderOk(IOstream::ASCII, typeName);
153 
154  const bool readFromFvConstraints(IOobject::name() == typeName);
155 
156  const dictionary& dict(*this);
157 
158  // Count number of active fvConstraints
159  label count = 0;
160  forAllConstIter(dictionary, dict, iter)
161  {
162  if (iter().isDict())
163  {
164  count++;
165  }
166  }
167 
169 
170  constrainedFields_.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& constraintDict = iter().dict();
179 
180  const word constraintType(constraintDict.lookup("type"));
181 
182  if
183  (
184  readFromFvConstraints
185  || !fvModel::dictionaryConstructorTablePtr_->found
186  (
187  constraintType
188  )
189  )
190  {
192  (
193  i,
194  name,
195  fvConstraint::New(name, constraintDict, mesh).ptr()
196  );
197 
198  constrainedFields_.set(i, new wordHashSet());
199 
200  i++;
201  }
202  }
203  }
204 
206  constrainedFields_.setSize(i);
207 
208  if (readFromFvConstraints)
209  {
210  // Add file watch on the fvConstraints dictionary for
211  // MUST_READ_IF_MODIFIED
212  addWatch();
213  }
214  else
215  {
216  // If the fvOptions file was read re-name to fvConstraints
217  rename(typeName);
218  }
219 }
220 
221 
222 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
223 
224 bool Foam::fvConstraints::constrainsField(const word& fieldName) const
225 {
226  const PtrListDictionary<fvConstraint>& constraintList(*this);
227 
228  forAll(constraintList, i)
229  {
230  if (constraintList[i].constrainsField(fieldName))
231  {
232  return true;
233  }
234  }
235 
236  return false;
237 }
238 
239 
241 {
242  bool allOk = true;
243 
244  PtrListDictionary<fvConstraint>& constraintList(*this);
245 
246  forAll(constraintList, i)
247  {
248  allOk = allOk && constraintList[i].movePoints();
249  }
250 
251  return allOk;
252 }
253 
254 
256 {
257  PtrListDictionary<fvConstraint>& constraintList(*this);
258 
259  forAll(constraintList, i)
260  {
261  constraintList[i].topoChange(map);
262  }
263 }
264 
265 
267 {
268  PtrListDictionary<fvConstraint>& constraintList(*this);
269 
270  forAll(constraintList, i)
271  {
272  constraintList[i].mapMesh(map);
273  }
274 }
275 
276 
278 {
279  PtrListDictionary<fvConstraint>& constraintList(*this);
280 
281  forAll(constraintList, i)
282  {
283  constraintList[i].distribute(map);
284  }
285 }
286 
287 
289 {
290  is >> *this;
291  return !is.bad();
292 }
293 
294 
296 {
297  dictionary::write(os, false);
298  return os.good();
299 }
300 
301 
303 {
305  {
306  checkTimeIndex_ = mesh().time().timeIndex() + 1;
307 
308  bool allOk = true;
309 
310  PtrListDictionary<fvConstraint>& constraintList(*this);
311 
312  forAll(constraintList, i)
313  {
314  fvConstraint& constraint = constraintList[i];
315  bool ok = constraint.read(subDict(constraint.name()));
316  allOk = (allOk && ok);
317  }
318  return allOk;
319  }
320  else
321  {
322  return false;
323  }
324 }
325 
326 
327 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
328 
329 Foam::Ostream& Foam::operator<<
330 (
331  Ostream& os,
332  const fvConstraints& constraints
333 )
334 {
335  constraints.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
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
const word & name() const
Return name.
Definition: IOobject.H:315
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
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
static autoPtr< fvConstraint > New(const word &name, const dictionary &dict, const fvMesh &mesh)
Return a reference to the selected fvConstraint.
Definition: fvConstraint.C:82
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
virtual bool constrainsField(const word &fieldName) const
Return true if an fvConstraint constrains the given field.
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
virtual bool movePoints()
Update for mesh motion.
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:372
fvMesh & mesh
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
Templated abstract base-class for optional mesh objects used to automate their allocation to the mesh...
Definition: MeshObject.H:87
const word & name() const
Return const access to the source name.
Definition: fvConstraintI.H:28
virtual bool read()
Read the fvConstraints dictionary if it has changed.
virtual bool writeData(Ostream &os) const
Write data to Ostream.
Foam::fvConstraints & fvConstraints
A class for handling words, derived from string.
Definition: word.H:59
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:208
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
fvConstraints(const fvMesh &mesh)
Construct from components with list of field names.
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
label timeIndex() const
Return current time index.
Definition: TimeStateI.H:28
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
#define WarningInFunction
Report a warning using Foam::Warning.
Finite volume constraints.
Definition: fvConstraints.H:57
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:95
label timeIndex
Definition: getTimeIndex.H:4
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
messageStream Info
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvConstraint.C:143
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:50
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
bool found
Finite volume options abstract base class.
Definition: fvConstraint.H:56
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864
virtual bool readData(Istream &)
ReadData function which reads the fvConstraints dictionary.