writeDictionary.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2013-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "writeDictionary.H"
30 #include "Time.H"
31 #include "polyMesh.H"
33 #include "IOdictionary.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace functionObjects
40 {
41  defineTypeNameAndDebug(writeDictionary, 0);
42 
44  (
45  functionObject,
46  writeDictionary,
47  dictionary
48  );
49 }
50 }
51 
52 
53 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
54 
55 void Foam::functionObjects::writeDictionary::writeHeader()
56 {
57  if (firstChange_)
58  {
59  firstChange_ = false;
60 
61  Info<< type() << ' ' << name() << " write:" << nl << nl;
63  }
64 }
65 
66 
67 void Foam::functionObjects::writeDictionary::checkDictionary
68 (
69  const dictionary& dict,
70  const label dicti
71 )
72 {
73  const auto digest(dict.digest());
74 
75  if (digests_[dicti] != digest)
76  {
77  digests_[dicti] = digest;
78 
79  writeHeader();
80  dict.writeEntry(Info);
81  Info<< nl;
83  }
84 }
85 
86 
87 bool Foam::functionObjects::writeDictionary::tryDirectory
88 (
89  const word& location,
90  const label dicti
91 )
92 {
93  IOobject dictIO
94  (
95  dictNames_[dicti],
96  location,
97  obr_,
101  );
102 
103  if (dictIO.typeHeaderOk<IOdictionary>(true))
104  {
105  checkDictionary(IOdictionary(dictIO), dicti);
106 
107  return true;
108  }
109 
110  return false;
111 }
112 
113 
114 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
115 
116 Foam::functionObjects::writeDictionary::writeDictionary
117 (
118  const word& name,
119  const Time& runTime,
120  const dictionary& dict
121 )
122 :
123  functionObjects::regionFunctionObject(name, runTime, dict),
124  dictNames_(),
125  digests_(),
126  firstChange_(true)
127 {
128  read(dict);
129 }
130 
131 
132 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 
135 {
137 
138  // Make unique
139  dictNames_ = wordHashSet(dict.get<wordList>("dictNames")).sortedToc();
140 
141  digests_.resize(dictNames_.size());
142  digests_ = SHA1Digest();
143 
144  Info<< type() << ' ' << name() << ": monitoring dictionaries:" << nl;
145  for (const word& dictName : dictNames_)
146  {
147  Info<< " " << dictName << nl;
148  }
149  if (dictNames_.empty())
150  {
151  Info<< " none" << nl;
152  }
153  Info<< endl;
154 
155  return true;
156 }
157 
158 
159 bool Foam::functionObjects::writeDictionary::performCheck()
160 {
161  // Restart reporting cycle
162  firstChange_ = true;
163 
164  forAll(dictNames_, dicti)
165  {
166  // Also search parent (eg, Time) as too
167  const auto* dictptr =
168  obr_.cfindObject<IOdictionary>(dictNames_[dicti], true);
169 
170  if (dictptr)
171  {
172  checkDictionary(*dictptr, dicti);
173  }
174  else if (dictNames_[dicti] == Time::controlDictName)
175  {
176  // Slight hack. controlDict an unwatchedIOdictionary
177  // (not IOdictionary) and not registered on Time either
178  // - grab directly from Time
179  checkDictionary(obr_.time().controlDict(), dicti);
180  }
181  else
182  {
183  const bool ok
184  (
185  tryDirectory(obr_.time().timeName(), dicti)
186  || tryDirectory(obr_.time().constant(), dicti)
187  || tryDirectory(obr_.time().system(), dicti)
188  );
189 
190  if (!ok)
191  {
192  writeHeader();
193 
194  Info<< " Unable to locate dictionary "
195  << dictNames_[dicti] << nl << nl;
196 
198  }
199  }
200  }
201 
202  return true;
203 }
204 
207 {
208  return true;
209 }
210 
211 
213 {
214  performCheck();
215  return true;
216 }
217 
218 
219 // ************************************************************************* //
Specialization of Foam::functionObject for a region and providing a reference to the region Foam::obj...
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
virtual bool read(const dictionary &)
Read the writeDictionary data.
static void writeHeader(Ostream &os, const word &fieldName)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
const word dictName("faMeshDefinition")
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
The SHA1 message digest.
Definition: SHA1Digest.H:56
Ignore writing from objectRegistry::writeObject()
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
const word & name() const noexcept
Return the name of this functionObject.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual const word & type() const =0
Runtime type information.
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:73
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:268
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
virtual bool write()
Write the selected dictionaries.
virtual bool execute()
Execute, currently does nothing.
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:139
messageStream Info
Information stream (stdout output on master, null elsewhere)
virtual bool read(const dictionary &dict)
Read optional controls.
IOobject dictIO
Do not request registration (bool: false)
Namespace for OpenFOAM.