stateFunctionObject.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) 2015 OpenFOAM Foundation
9  Copyright (C) 2015-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 "stateFunctionObject.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const Foam::word Foam::functionObjects::stateFunctionObject::resultsName_ =
35  "results";
36 
37 
38 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
39 
40 const Foam::IOdictionary&
42 {
43  return time_.functionObjects().stateDict();
44 }
45 
46 
48 {
49  return const_cast<IOdictionary&>(time_.functionObjects().stateDict());
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
56 (
57  const word& name,
58  const Time& runTime
59 )
60 :
62 {}
63 
64 
65 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
66 
68 {
69  IOdictionary& stateDict = this->stateDict();
70 
71  if (!stateDict.found(name()))
72  {
73  stateDict.add(name(), dictionary());
74  }
75 
76  return stateDict.subDict(name());
77 }
78 
79 
81 (
82  const label triggeri
83 )
84 {
85  IOdictionary& stateDict = this->stateDict();
86 
87  label oldTriggeri =
88  stateDict.getOrDefault<label>("triggerIndex", labelMin);
89 
90  if (triggeri > oldTriggeri)
91  {
92  stateDict.set("triggerIndex", triggeri);
93  return true;
94  }
95 
96  return false;
97 }
98 
99 
101 {
102  const IOdictionary& stateDict = this->stateDict();
103 
104  return stateDict.getOrDefault<label>("triggerIndex", labelMin);
105 }
106 
107 
109 (
110  const word& entryName
111 ) const
112 {
113  const IOdictionary& stateDict = this->stateDict();
114 
115  if (stateDict.found(name()))
116  {
117  const dictionary& baseDict = stateDict.subDict(name());
118  return baseDict.found(entryName);
119  }
120 
121  return false;
122 }
123 
124 
126 (
127  const word& entryName,
129 ) const
130 {
131  return getObjectDict(name(), entryName, dict);
132 }
133 
134 
136 (
137  const word& objectName,
138  const word& entryName,
140 ) const
141 {
142  const IOdictionary& stateDict = this->stateDict();
143 
144  if (stateDict.found(objectName))
145  {
146  const dictionary& baseDict = stateDict.subDict(objectName);
147  if (baseDict.found(entryName) && baseDict.isDict(entryName))
148  {
149  dict = baseDict.subDict(entryName);
150  return true;
151  }
152  }
153 
154  return false;
155 }
156 
157 
159 (
160  const word& entryName
161 ) const
162 {
163  return objectResultType(name(), entryName);
164 }
165 
166 
168 (
169  const word& objectName,
170  const word& entryName
171 ) const
172 {
173  word result = word::null;
174  const IOdictionary& stateDict = this->stateDict();
175 
176  if (stateDict.found(resultsName_))
177  {
178  const dictionary& resultsDict = stateDict.subDict(resultsName_);
179 
180  if (resultsDict.found(objectName))
181  {
182  const dictionary& objectDict = resultsDict.subDict(objectName);
183 
184  for (const entry& dEntry : objectDict)
185  {
186  const dictionary& dict = dEntry.dict();
187 
188  if (dict.found(entryName))
189  {
190  return dict.dictName();
191  }
192  }
193  }
194  }
195 
196  return result;
197 }
198 
199 
202 {
203  return objectResultEntries(name());
204 }
205 
206 
209 (
210  const word& objectName
211 ) const
212 {
213  DynamicList<word> result(2);
214 
215  const IOdictionary& stateDict = this->stateDict();
216 
217  if (stateDict.found(resultsName_))
218  {
219  const dictionary& resultsDict = stateDict.subDict(resultsName_);
220 
221  if (resultsDict.found(objectName))
222  {
223  const dictionary& objectDict = resultsDict.subDict(objectName);
224 
225  for (const entry& dEntry : objectDict)
226  {
227  const dictionary& dict = dEntry.dict();
228 
229  result.append(dict.toc());
230  }
231  }
232  }
233 
234  wordList entries;
235  entries.transfer(result);
236 
237  return entries;
238 }
239 
241 (
242  Ostream& os
243 ) const
244 {
245  writeResultEntries(name(), os);
246 }
247 
248 
250 (
251  const word& objectName,
252  Ostream& os
253 ) const
254 {
255  const IOdictionary& stateDict = this->stateDict();
256 
257  if (stateDict.found(resultsName_))
258  {
259  const dictionary& resultsDict = stateDict.subDict(resultsName_);
260 
261  if (resultsDict.found(objectName))
262  {
263  const dictionary& objectDict = resultsDict.subDict(objectName);
264 
265  for (const word& dataFormat : objectDict.sortedToc())
266  {
267  os << " Type: " << dataFormat << nl;
268 
269  const dictionary& resultDict = objectDict.subDict(dataFormat);
270 
271  for (const word& result : resultDict.sortedToc())
272  {
273  os << " " << result << nl;
274  }
275  }
276  }
277  }
278 }
279 
280 
282 (
283  Ostream& os
284 ) const
285 {
286  const IOdictionary& stateDict = this->stateDict();
287 
288  if (stateDict.found(resultsName_))
289  {
290  const dictionary& resultsDict = stateDict.subDict(resultsName_);
291 
292  const wordList allObjectNames = resultsDict.sortedToc();
293 
294  for (const word& objectName : allObjectNames)
295  {
296  os << "Object: " << objectName << endl;
297 
298  writeResultEntries(objectName, os);
299  }
300  }
301 }
302 
303 
304 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
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::functionObjects::stateFunctionObject::propertyDict
dictionary & propertyDict()
Return access to the property dictionary.
Definition: stateFunctionObject.C:67
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::DynamicList< word >
Foam::dictionary::found
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionary.C:364
Foam::functionObjects::timeFunctionObject::time_
const Time & time_
Reference to the time database.
Definition: timeFunctionObject.H:65
Foam::functionObjectList::stateDict
IOdictionary & stateDict()
Definition: functionObjectList.C:573
Foam::functionObjects::timeFunctionObject
Virtual base class for function objects with a reference to Time.
Definition: timeFunctionObject.H:56
Foam::Time::functionObjects
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:499
Foam::functionObjects::stateFunctionObject::setTrigger
bool setTrigger(const label triggeri)
Set the current trigger index.
Definition: stateFunctionObject.C:81
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::functionObjects::stateFunctionObject::resultType
word resultType(const word &entryName) const
Retrieve the result type.
Definition: stateFunctionObject.C:159
Foam::functionObjects::stateFunctionObject::stateFunctionObject
stateFunctionObject(const stateFunctionObject &)=delete
No copy construct.
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:847
Foam::functionObjects::stateFunctionObject::getTrigger
label getTrigger() const
Get the current trigger index.
Definition: stateFunctionObject.C:100
Foam::functionObjects::stateFunctionObject::getObjectDict
bool getObjectDict(const word &objectName, const word &entryName, dictionary &dict) const
Set dictionary from named object, return true if set.
Definition: stateFunctionObject.C:136
Foam::functionObjects::stateFunctionObject::writeAllResultEntries
void writeAllResultEntries(Ostream &os) const
Write the results entries for all objects to stream.
Definition: stateFunctionObject.C:282
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:474
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:459
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:528
Foam::functionObjects::stateFunctionObject::foundProperty
bool foundProperty(const word &entryName) const
Return true if the property exists.
Definition: stateFunctionObject.C:109
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:121
Foam::functionObjects::stateFunctionObject::getDict
bool getDict(const word &entryName, dictionary &dict) const
Set dictionary, return true if set.
Definition: stateFunctionObject.C:126
Foam::functionObjects::stateFunctionObject::stateDict
const IOdictionary & stateDict() const
Return a const reference to the state dictionary.
Definition: stateFunctionObject.C:41
Foam::functionObjects::stateFunctionObject::objectResultType
word objectResultType(const word &objectName, const word &entryName) const
Return the type of result.
Definition: stateFunctionObject.C:168
Foam::dictionary::isDict
bool isDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Check if entry is found and is a sub-dictionary.
Definition: dictionary.C:498
Time.H
Foam::functionObjects::stateFunctionObject::objectResultEntries
List< word > objectResultEntries() const
Retrieve the result entries.
Definition: stateFunctionObject.C:201
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< Foam::word >
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
stateFunctionObject.H
Foam::labelMin
constexpr label labelMin
Definition: label.H:60
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:708
Foam::functionObjects::stateFunctionObject::writeResultEntries
void writeResultEntries(Ostream &os) const
Write the results entries for all objects to stream.
Definition: stateFunctionObject.C:241
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:122
Foam::dictionary::sortedToc
wordList sortedToc() const
Return the sorted table of contents.
Definition: dictionary.C:684