Loading...
Searching...
No Matches
exprResultGlobals.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) 2012-2018 Bernhard Gschaider
9 Copyright (C) 2019-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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 "exprResultGlobals.H"
30#include "Time.H"
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
36namespace expressions
37{
38
40
41} // End namespace expressions
42} // End namespace Foam
43
44
45// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46
47Foam::expressions::exprResultGlobals::exprResultGlobals
48(
49 const objectRegistry& obr
50)
51:
53 (
55 (
56 exprResultGlobals::typeName,
57 obr.time().timeName(), // instance
58 "expressions", // local
59 obr.time(),
60 IOobject::READ_IF_PRESENT,
61 IOobject::AUTO_WRITE,
62 IOobject::REGISTER
63 )
64 ),
65 variables_(),
66 timeIndex_(obr.time().timeIndex())
67{
68 if (headerOk())
69 {
70 readData
71 (
72 readStream(exprResultGlobals::typeName, true)
73 );
74 }
75}
76
77
79:
80 HashPtrTable<exprResult>(tbl.capacity())
81{
82 for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
83 {
84 this->set(iter.key(), (*iter)->clone());
85 }
86}
87
92{}
93
94
96:
98{}
99
100
101// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
102
103void Foam::expressions::exprResultGlobals::reset()
104{
105 forAllIters(variables_, tablesIter)
106 {
107 forAllIters((*tablesIter), iter)
108 {
109 (*iter)->reset();
110 }
112}
113
114
115// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
116
119(
120 const objectRegistry& obr
121)
122{
124
125 auto* ptr = obr.time().getObjectPtr<Type>(Type::typeName);
126
127 if (!ptr)
128 {
129 ptr = new Type(obr);
130 ptr->store();
131 }
132 else if (ptr->timeIndex_ != obr.time().timeIndex())
133 {
134 // If time changes, reset variables
135
136 ptr->timeIndex_ = obr.time().timeIndex();
137 ptr->reset();
138 }
140 return *ptr;
141}
142
143
144// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
145
147{
149
150 auto* ptr = obr.time().getObjectPtr<Type>(Type::typeName);
151
152 if (ptr)
153 {
154 return obr.time().checkOut(ptr);
155 }
157 return false;
158}
159
160
161// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
162
164{
165 // Enforce ASCII to avoid any potential binary issues
166 const auto oldFmt = os.format(IOstreamOption::ASCII);
167
168 os << variables_;
170 os.format(oldFmt);
171
172 return os.good();
173}
174
175
177{
178 // Enforce ASCII to avoid any potential binary issues
179 const auto oldFmt = is.format(IOstreamOption::ASCII);
180
181 is >> variables_;
182
183 is.format(oldFmt);
184
185 return !is.bad();
186}
187
188
191{
192 return variables_[name];
193}
194
195
198(
199 const word& name,
200 const wordUList& scopes
201) const
202{
203 for (const word& scopeName : scopes)
204 {
205 const auto tableIter = variables_.cfind(scopeName);
206
207 if (tableIter.good())
208 {
209 const auto resultIter = (*tableIter).cfind(name);
210
211 if (resultIter.good())
212 {
213 return *(*resultIter);
214 }
215 }
216 #ifdef FULLDEBUG
217 else
218 {
220 << "No scope " << scopeName << " for " << name << nl
221 << "Known global scopes: " << variables_.sortedToc() << nl;
222 }
223 #endif
225
226 return exprResult::null;
227}
228
229
232(
233 const word& name,
234 const word& scope,
235 const exprResult& value,
236 const bool overwrite
237)
238{
239 Table& tbl = getOrCreateScope(scope);
240
241 auto iter = tbl.find(name);
242
243 if (!iter.good())
244 {
245 tbl.set(name, new exprResult(value));
246 iter = tbl.find(name);
247 }
248 else if (overwrite)
249 {
250 *(*iter) = value;
252
253 return *(*iter);
254}
255
256
259(
260 const word& name,
261 const word& scope,
262 autoPtr<exprResult>&& value,
263 const bool overwrite
264)
265{
266 Table& tbl = getOrCreateScope(scope);
267
268 if (overwrite || !tbl.found(name))
269 {
270 tbl.set(name, std::move(value));
272
273 return *tbl[name];
274}
275
276
279(
280 const dictionary& dict,
281 const word& scope,
282 const bool overwrite
283)
284{
285 word scopeName(scope);
286
287 const word name(dict.get<word>("globalName"));
288
289 if (scopeName.empty())
290 {
291 scopeName = dict.get<word>("globalScope");
292 }
293
294 if (dict.found("resultType"))
295 {
296 return addValue
297 (
298 name,
299 scopeName,
301 overwrite
302 );
303 }
304 else
305 {
306 return addValue
307 (
308 name,
309 scopeName,
311 overwrite
312 );
313 }
314}
315
316
318(
319 const word& name,
320 const word& scope
321)
322{
323 auto iter = variables_.find(scope);
324
325 return (iter.good() && iter.val().erase(name));
326}
327
328
329// ************************************************************************* //
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
constexpr HashPtrTable() noexcept=default
bool set(const Key &key, T *ptr)
Assign a new entry, overwrites existing.
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
bool found(const Key &key) const
Same as contains().
Definition HashTable.H:1370
label capacity() const noexcept
The size of the underlying table (the number of buckets).
Definition HashTable.H:363
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition HashTableI.H:86
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
streamFormat format() const noexcept
Get the current stream format.
@ ASCII
"ascii" (normal default)
bool bad() const noexcept
True if stream is corrupted.
Definition IOstream.H:305
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
label timeIndex() const noexcept
Return the current time index.
Definition TimeStateI.H:43
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A globally available registry of expression results. These are currently registered on Time (may chan...
const exprResult & get(const word &name, const wordUList &scopes) const
Return a global variable, if it exists, or a exprResult::null.
virtual bool writeData(Ostream &os) const
Write variables.
static exprResultGlobals & New(const objectRegistry &obr)
Static constructor for singleton.
exprResult & addValue(const word &name, const word &scope, const exprResult &value, const bool overwrite=true)
Add named result to specified scope.
virtual bool readData(Istream &os)
Read variables.
Table & getNamespace(const word &name)
Get an existing table for the namespace.
static bool Delete(const objectRegistry &obr)
Static destructor for singleton.
bool removeValue(const word &name, const word &scope)
Remove named result from specified scope.
A polymorphic field/result from evaluating an expression.
Definition exprResult.H:122
static const exprResult null
An empty result.
Definition exprResult.H:332
static autoPtr< exprResult > New(const dictionary &dict)
Return a reference to the selected value driver.
Definition exprResult.C:272
bool reset(bool force=false)
Reset at new timestep according to type.
Definition exprResult.C:352
Registry of regIOobjects.
const Time & time() const noexcept
Return time registry.
bool checkOut(regIOobject *io) const
Remove a regIOobject from registry and free memory if the object is ownedByRegistry....
Type * getObjectPtr(const word &name, const bool recursive=false) const
Return non-const pointer to the object of the given Type, using a const-cast to have it behave like a...
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeName(Type)
Define the typeName.
Definition className.H:113
OBJstream os(runTime.globalPath()/outputName)
auto & name
word timeName
Definition getTimeIndex.H:3
#define WarningInFunction
Report a warning using Foam::Warning.
A namespace for expression-related classes/traits etc.
Namespace for OpenFOAM.
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
UList< word > wordUList
UList of word.
Definition wordList.H:34
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
label timeIndex
dictionary dict
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition stdFoam.H:214