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 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  IOobject io
46  (
47  typeName,
48  mesh.time().system(),
49  mesh,
52  );
53 
54  if (io.typeHeaderOk<IOdictionary>(true))
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.typeHeaderOk<IOdictionary>(true))
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.typeHeaderOk<IOdictionary>(true))
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  PtrListDictionary<fvConstraint>& constraintList(*this);
243 
244  forAll(constraintList, i)
245  {
246  constraintList[i].updateMesh(mpm);
247  }
248 }
249 
250 
252 {
253  bool allOk = true;
254 
255  PtrListDictionary<fvConstraint>& constraintList(*this);
256 
257  forAll(constraintList, i)
258  {
259  allOk = allOk && constraintList[i].movePoints();
260  }
261 
262  return allOk;
263 }
264 
265 
267 {
268  is >> *this;
269  return !is.bad();
270 }
271 
272 
274 {
275  dictionary::write(os, false);
276  return os.good();
277 }
278 
279 
281 {
283  {
284  checkTimeIndex_ = mesh().time().timeIndex() + 1;
285 
286  bool allOk = true;
287 
288  PtrListDictionary<fvConstraint>& constraintList(*this);
289 
290  forAll(constraintList, i)
291  {
292  fvConstraint& constraint = constraintList[i];
293  bool ok = constraint.read(subDict(constraint.name()));
294  allOk = (allOk && ok);
295  }
296  return allOk;
297  }
298  else
299  {
300  return false;
301  }
302 }
303 
304 
305 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
306 
307 Foam::Ostream& Foam::operator<<
308 (
309  Ostream& os,
310  const fvConstraints& constraints
311 )
312 {
313  constraints.writeData(os);
314  return os;
315 }
316 
317 
318 // ************************************************************************* //
virtual void updateMesh(const mapPolyMesh &)
Update for mesh changes.
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
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
const word & name() const
Return name.
Definition: IOobject.H:303
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
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:239
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Templated abstract base-class for optional mesh objects used to automate their allocation to the mesh...
Definition: MeshObject.H:85
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.
dynamicFvMesh & mesh
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
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:208
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
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:35
#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:78
label timeIndex
Definition: getTimeIndex.H:4
messageStream Info
virtual bool read(const dictionary &dict)
Read source dictionary.
Definition: fvConstraint.C:143
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
bool found
Finite volume options abstract base class.
Definition: fvConstraint.H:54
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:844
virtual bool readData(Istream &)
ReadData function which reads the fvConstraints dictionary.