writeDictionary.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) 2013-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 "writeDictionary.H"
27 #include "Time.H"
28 #include "polyMesh.H"
30 #include "IOdictionary.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionObjects
37 {
38  defineTypeNameAndDebug(writeDictionary, 0);
39 
41  (
42  functionObject,
43  writeDictionary,
44  dictionary
45  );
46 }
47 }
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 bool Foam::functionObjects::writeDictionary::tryDirectory
53 (
54  const label dictI,
55  const word& location,
56  bool& firstDict
57 )
58 {
59  typeIOobject<IOdictionary> dictIO
60  (
61  dictNames_[dictI],
62  location,
63  obr_,
66  false
67  );
68 
69  if (dictIO.headerOk())
70  {
71  IOdictionary dict(dictIO);
72 
73  if (dict.digest() != digests_[dictI])
74  {
75  if (firstDict)
76  {
77  Info<< type() << " " << name() << " write:" << nl << endl;
78 
80  Info<< endl;
81  firstDict = false;
82  }
83 
84  Info<< dict.dictName() << dict << nl;
85 
87 
88  digests_[dictI] = dict.digest();
89  }
90 
91  return true;
92  }
93 
94  return false;
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
101 (
102  const word& name,
103  const Time& runTime,
104  const dictionary& dict
105 )
106 :
107  functionObject(name),
108  obr_
109  (
111  (
113  )
114  ),
115  dictNames_(),
116  digests_()
117 {
118  read(dict);
119  execute();
120 }
121 
122 
123 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
124 
126 {}
127 
128 
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130 
132 {
133  wordList dictNames(dict.lookup("dictNames"));
134  HashSet<word> uniqueNames(dictNames);
135  dictNames_ = uniqueNames.toc();
136 
137  digests_.setSize(dictNames_.size(), SHA1Digest());
138 
139  Info<< type() << " " << name() << ": monitoring dictionaries:" << nl;
140  if (dictNames_.size())
141  {
142  forAll(dictNames_, i)
143  {
144  Info<< " " << dictNames_[i] << endl;
145  }
146  }
147  else
148  {
149  Info<< " none" << nl;
150  }
151  Info<< endl;
152 
153  return true;
154 }
155 
156 
158 {
159  return true;
160 }
161 
162 
164 {
165  bool firstDict = true;
166  forAll(dictNames_, i)
167  {
168  if (obr_.foundObject<dictionary>(dictNames_[i]))
169  {
170  const dictionary& dict =
171  obr_.lookupObject<dictionary>(dictNames_[i]);
172 
173  if (dict.digest() != digests_[i])
174  {
175  if (firstDict)
176  {
177  Info<< type() << " " << name() << " write:" << nl << endl;
178 
180  Info<< endl;
181  firstDict = false;
182  }
183 
184  digests_[i] = dict.digest();
185 
186  Info<< dict.dictName() << dict << nl;
188  Info<< endl;
189  }
190  }
191  else
192  {
193  bool processed = tryDirectory(i, obr_.time().timeName(), firstDict);
194 
195  if (!processed)
196  {
197  processed = tryDirectory(i, obr_.time().constant(), firstDict);
198  }
199 
200  if (!processed)
201  {
202  processed = tryDirectory(i, obr_.time().system(), firstDict);
203  }
204 
205  if (!processed)
206  {
207  Info<< " Unable to locate dictionary " << dictNames_[i]
208  << nl << endl;
209  }
210  else
211  {
212  Info<< endl;
213  }
214  }
215  }
216 
217  return true;
218 }
219 
220 
221 // ************************************************************************* //
A HashTable with keys but without contents.
Definition: HashSet.H:59
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
virtual bool read(const dictionary &)
Read the writeDictionary data.
SHA1Digest digest() const
Return the SHA1 digest of the dictionary contents.
Definition: dictionary.C:629
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
writeDictionary(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:113
addToRunTimeSelectionTable(functionObject, Qdot, dictionary)
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:325
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
The SHA1 message digest.
Definition: SHA1Digest.H:62
Abstract base-class for Time/database functionObjects.
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type.
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 dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:121
bool read(const char *, int32_t &)
Definition: int32IO.C:85
A class for handling words, derived from string.
Definition: word.H:59
static const char nl
Definition: Ostream.H:260
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
defineTypeNameAndDebug(Qdot, 0)
virtual bool write()
Write the selected dictionaries.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
virtual bool execute()
Execute, currently does nothing.
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
messageStream Info
Registry of regIOobjects.
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