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-2020 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  Info<< type() << " " << name() << " write:" << nl << endl;
60 
62  Info<< endl;
63  firstChange_ = false;
64  }
65 }
66 
67 
68 void Foam::functionObjects::writeDictionary::checkDictionary
69 (
70  const dictionary& dict,
71  const label dicti
72 )
73 {
74  if (dict.digest() != digests_[dicti])
75  {
76  writeHeader();
77 
78  digests_[dicti] = dict.digest();
79 
80  Info<< dict.dictName() << dict << nl;
81 
83  Info<< endl;
84  }
85 }
86 
87 
88 bool Foam::functionObjects::writeDictionary::tryDirectory
89 (
90  const word& location,
91  const label dicti
92 )
93 {
94  IOobject dictIO
95  (
96  dictNames_[dicti],
97  location,
98  obr_,
101  false
102  );
103 
104  if (dictIO.typeHeaderOk<IOdictionary>(true))
105  {
106  checkDictionary(IOdictionary(dictIO), dicti);
107 
108  return true;
109  }
110 
111  return false;
112 }
113 
114 
115 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
116 
117 Foam::functionObjects::writeDictionary::writeDictionary
118 (
119  const word& name,
120  const Time& runTime,
121  const dictionary& dict
122 )
123 :
125  dictNames_(),
126  digests_(),
127  firstChange_(true)
128 {
129  read(dict);
130  execute();
131 }
132 
133 
134 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135 
137 {
139 
140  wordList dictNames(dict.get<wordList>("dictNames"));
141  wordHashSet uniqueNames(dictNames);
142  dictNames_ = uniqueNames.toc();
143 
144  digests_.setSize(dictNames_.size(), SHA1Digest());
145 
146  Info<< type() << " " << name() << ": monitoring dictionaries:" << nl;
147  if (dictNames_.size())
148  {
149  for (const word & dictName : dictNames_)
150  {
151  Info<< " " << dictName << endl;
152  }
153  }
154  else
155  {
156  Info<< " none" << nl;
157  }
158  Info<< endl;
159 
160  return true;
161 }
162 
163 
165 {
166  return true;
167 }
168 
169 
171 {
172  firstChange_ = true;
173 
174  forAll(dictNames_, dicti)
175  {
176  const IOdictionary* dictptr =
177  obr_.cfindObject<IOdictionary>(dictNames_[dicti]);
178 
179  if (dictptr)
180  {
181  checkDictionary(*dictptr, dicti);
182  }
183  else
184  {
185  bool processed = tryDirectory(obr_.time().timeName(), dicti);
186 
187  if (!processed)
188  {
189  processed = tryDirectory(obr_.time().constant(), dicti);
190  }
191 
192  if (!processed)
193  {
194  processed = tryDirectory(obr_.time().system(), dicti);
195  }
196 
197  if (!processed)
198  {
199  writeHeader();
200 
201  Info<< " Unable to locate dictionary " << dictNames_[dicti]
202  << nl << endl;
203 
205  Info<< endl;
206  }
207  }
208  }
209 
210  return true;
211 }
212 
213 
214 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::writeDictionary::execute
virtual bool execute()
Execute, currently does nothing.
Definition: writeDictionary.C:164
Foam::functionObjects::writeDictionary::write
virtual bool write()
Write the selected dictionaries.
Definition: writeDictionary.C:170
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
dictName
const word dictName("faMeshDefinition")
Foam::functionObjects::writeDictionary::read
virtual bool read(const dictionary &)
Read the writeDictionary data.
Definition: writeDictionary.C:136
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:66
polyMesh.H
Foam::HashSet< word, Hash< word > >
writeDictionary.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:173
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
dictIO
IOobject dictIO
Definition: setConstantMeshDictionaryIO.H:1
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::SHA1Digest
The SHA1 message digest.
Definition: SHA1Digest.H:60
IOdictionary.H
Time.H
Foam::functionObject::name
const word & name() const noexcept
Return the name of this functionObject.
Definition: functionObject.C:143
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::IOobject::writeDivider
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
Definition: IOobjectWriteHeader.C:132
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::List< word >
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::regionFunctionObject
Specialization of Foam::functionObject for a region and providing a reference to the region Foam::obj...
Definition: regionFunctionObject.H:90
Foam::IOobject::MUST_READ
Definition: IOobject.H:185