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-2023 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 {
39 
41  (
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, runTime),
108  obr_
109  (
110  runTime.lookupObject<objectRegistry>
111  (
112  dict.lookupOrDefault("region", polyMesh::defaultRegion)
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, time_.name(), firstDict);
194 
195  if (!processed)
196  {
197  processed = tryDirectory(i, time_.constant(), firstDict);
198  }
199 
200  if (!processed)
201  {
202  processed = tryDirectory(i, 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 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
A HashTable with keys but without contents.
Definition: HashSet.H:62
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:202
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:93
void setSize(const label)
Reset size of List.
Definition: List.C:281
The SHA1 message digest.
Definition: SHA1Digest.H:63
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
Abstract base-class for Time/database functionObjects.
virtual const word & type() const =0
Runtime type information.
const word & name() const
Return the name of this functionObject.
Writes dictionaries on start-up and on change.
writeDictionary(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
virtual bool execute()
Execute, currently does nothing.
virtual bool write()
Write the selected dictionaries.
virtual bool read(const dictionary &)
Read the writeDictionary data.
Registry of regIOobjects.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
A class for handling words, derived from string.
Definition: word.H:62
defineTypeNameAndDebug(adjustTimeStepToCombustion, 0)
addToRunTimeSelectionTable(functionObject, adjustTimeStepToCombustion, dictionary)
Namespace for OpenFOAM.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const char nl
Definition: Ostream.H:260
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict