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 | Copyright (C) 2013-2016 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 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace functionObjects
36 {
37  defineTypeNameAndDebug(writeDictionary, 0);
38 
40  (
41  functionObject,
42  writeDictionary,
43  dictionary
44  );
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
50 
51 bool Foam::functionObjects::writeDictionary::tryDirectory
52 (
53  const label dictI,
54  const word& location,
55  bool& firstDict
56 )
57 {
58  IOobject dictIO
59  (
60  dictNames_[dictI],
61  location,
62  obr_,
65  false
66  );
67 
68  if (dictIO.headerOk())
69  {
70  IOdictionary dict(dictIO);
71 
72  if (dict.digest() != digests_[dictI])
73  {
74  if (firstDict)
75  {
76  Info<< type() << " " << name() << " write:" << nl << endl;
77 
79  Info<< endl;
80  firstDict = false;
81  }
82 
83  Info<< dict.dictName() << dict << nl;
84 
86 
87  digests_[dictI] = dict.digest();
88  }
89 
90  return true;
91  }
92 
93  return false;
94 }
95 
96 
97 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98 
99 Foam::functionObjects::writeDictionary::writeDictionary
100 (
101  const word& name,
102  const Time& runTime,
103  const dictionary& dict
104 )
105 :
106  functionObject(name),
107  obr_
108  (
110  (
112  )
113  ),
114  dictNames_(),
115  digests_()
116 {
117  read(dict);
118  execute();
119 }
120 
121 
122 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
123 
125 {}
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
131 {
132  wordList dictNames(dict.lookup("dictNames"));
133  HashSet<word> uniqueNames(dictNames);
134  dictNames_ = uniqueNames.toc();
135 
136  digests_.setSize(dictNames_.size(), SHA1Digest());
137 
138  Info<< type() << " " << name() << ": monitoring dictionaries:" << nl;
139  if (dictNames_.size())
140  {
141  forAll(dictNames_, i)
142  {
143  Info<< " " << dictNames_[i] << endl;
144  }
145  }
146  else
147  {
148  Info<< " none" << nl;
149  }
150  Info<< endl;
151 
152  return true;
153 }
154 
155 
157 {
158  return true;
159 }
160 
161 
163 {
164  bool firstDict = true;
165  forAll(dictNames_, i)
166  {
167  if (obr_.foundObject<dictionary>(dictNames_[i]))
168  {
169  const dictionary& dict =
170  obr_.lookupObject<dictionary>(dictNames_[i]);
171 
172  if (dict.digest() != digests_[i])
173  {
174  if (firstDict)
175  {
176  Info<< type() << " " << name() << " write:" << nl << endl;
177 
179  Info<< endl;
180  firstDict = false;
181  }
182 
183  digests_[i] = dict.digest();
184 
185  Info<< dict.dictName() << dict << nl;
187  Info<< endl;
188  }
189  }
190  else
191  {
192  bool processed = tryDirectory(i, obr_.time().timeName(), firstDict);
193 
194  if (!processed)
195  {
196  processed = tryDirectory(i, obr_.time().constant(), firstDict);
197  }
198 
199  if (!processed)
200  {
201  processed = tryDirectory(i, obr_.time().system(), firstDict);
202  }
203 
204  if (!processed)
205  {
206  Info<< " Unable to locate dictionary " << dictNames_[i]
207  << nl << endl;
208  }
209  else
210  {
211  Info<< endl;
212  }
213  }
214  }
215 
216  return true;
217 }
218 
219 
220 // ************************************************************************* //
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:428
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
virtual bool read(const dictionary &)
Read the writeDictionary data.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:98
const word dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:115
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
The SHA1 message digest.
Definition: SHA1Digest.H:62
Abstract base-class for Time/database function objects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Macros for easy insertion into run-time selection tables.
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type.
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:262
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
IOobject dictIO(dictName, runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE)
virtual bool write()
Write the selected dictionaries.
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:461
virtual bool execute()
Execute, currently does nothing.
addToRunTimeSelectionTable(functionObject, blendingFactor, dictionary)
SHA1Digest digest() const
Return the SHA1 digest of the dictionary contents.
Definition: dictionary.C:272
messageStream Info
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
Registry of regIOobjects.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451