Loading...
Searching...
No Matches
exprDriverFunctions.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) 2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "exprDriver.H"
29
30// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
32namespace Foam
33{
34
35// Check for acceptable Function1 keywords in the given dictionary,
36// with special handling to avoid accidental inclusion of coeffs
37// dictionaries etc
39(
40 const dictionary* dictPtr,
41 const bool acceptPrimitiveEntry,
42 const bool report = false
43)
44{
45 wordHashSet acceptKeys;
46 wordHashSet rejectKeys;
47
48 if (!dictPtr)
49 {
50 return acceptKeys;
51 }
52
53 const dictionary& dict = *dictPtr;
54
55 acceptKeys.reserve(dict.size());
56 rejectKeys.reserve(dict.size());
57
58 for (const entry& dEntry : dict)
59 {
60 const keyType& kw = dEntry.keyword();
61
62 bool ok = true;
63
64 if (kw.isPattern())
65 {
66 ok = false;
67 }
68 else if (dEntry.isDict())
69 {
70 // Dictionary entry - require "type", which should eliminate
71 // any *Coeffs dictionaries
72
73 ok = dEntry.dict().found("type", keyType::LITERAL);
74 }
75 else
76 {
77 // Primitive entry. Trust that it is okay?
78 ok = acceptPrimitiveEntry;
79 }
80
81 if (ok)
82 {
83 acceptKeys.insert(kw);
84 }
85 else
86 {
87 rejectKeys.insert(kw);
88 }
89 }
90
91 if (report && rejectKeys.size())
92 {
94 << "Dropped invalid/redundant entries: "
95 << flatOutput(rejectKeys.sortedToc()) << nl;
96 }
97
98 return acceptKeys;
99}
100
101
102// Read and reset Function1 for given dictionary.
103// Uses getAcceptableFunctionKeys
104template<class Type>
105static void resetFuncsImpl
106(
107 const word& subDictName,
108 const dictionary& topDict,
109 HashTable<refPtr<Function1<Type>>>& tbl,
110 const objectRegistry* obrPtr
111)
112{
113 tbl.clear();
114
115 const dictionary* dictPtr =
116 topDict.findDict(subDictName, keyType::LITERAL);
117
118 if (!dictPtr)
119 {
120 return;
121 }
122
123 wordHashSet acceptKeys
124 (
126 (
127 dictPtr,
128 true // Accept primitive entries, hope for the best
129 )
130 );
131
132 const dictionary& dict = *dictPtr;
133
134 for (const word& entryName : acceptKeys)
135 {
136 // From autoPtr -> refPtr
138 (
139 Function1<Type>::New(entryName, dict, obrPtr)
140 );
141
142 if (func)
143 {
144 tbl.insert(entryName, std::move(func));
146 }
147}
148
149
150// Write out entries, if they originated from dictionary
151template<class Type>
152static void writeFuncsImpl
153(
154 Ostream& os,
155 const word& subDictName,
156 const dictionary& topDict,
157 const HashTable<refPtr<Function1<Type>>>& tbl
158)
159{
160 const dictionary* dictPtr =
161 topDict.findDict(subDictName, keyType::LITERAL);
162
163 if (!dictPtr || tbl.empty())
164 {
165 return;
166 }
167
168 label nwrote = 0;
169
170 const dictionary& dict = *dictPtr;
171
172 for (const entry& dEntry : dict)
173 {
174 const word& entryName = dEntry.keyword();
175
176 const auto iter = tbl.cfind(entryName);
177
178 if (!iter.good())
179 {
180 continue;
181 }
182
183 const auto& funcPtr = iter.val();
184
185 if (funcPtr)
186 {
187 if (!nwrote++)
188 {
189 os.beginBlock(subDictName);
190 }
191
192 (*funcPtr).writeData(os);
193 }
194 }
195
196 if (nwrote)
197 {
198 os.endBlock();
199 }
200}
201
202} // End namespace Foam
203
204
205// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
206
207void Foam::expressions::exprDriver::resetFunctions
208(
209 const dictionary& dict
210)
211{
212 resetFuncsImpl<scalar>("functions<scalar>", dict_, scalarFuncs_, obrPtr_);
213 resetFuncsImpl<vector>("functions<vector>", dict_, vectorFuncs_, obrPtr_);
214
215 if (debug)
216 {
218 }
219}
220
221
223{
224 writeFuncsImpl<scalar>(os, "functions<scalar>", dict_, scalarFuncs_);
225 writeFuncsImpl<vector>(os, "functions<vector>", dict_, vectorFuncs_);
226}
227
228
229// ************************************************************************* //
bool empty() const noexcept
True if the list is empty.
Definition DLListBase.H:189
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition Function1.H:92
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition HashTable.C:156
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
Definition HashTable.C:729
label size() const noexcept
The number of elements in table.
Definition HashTable.H:358
void clear()
Remove all entries from table.
Definition HashTable.C:742
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
virtual Ostream & endBlock()
Write end block group.
Definition Ostream.C:108
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition Ostream.C:90
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
const dictionary * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary pointer if present (and it is a dictionary) otherwise return nullptr...
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
void writeFunctions(Ostream &os) const
Write scalar/vector Function1 entries in dictionary format.
HashTable< refPtr< Function1< scalar > > > scalarFuncs_
Function1 mappings/timelines (scalar), evaluated at the simulation time or with arbitrary scalars.
Definition exprDriver.H:217
HashTable< refPtr< Function1< vector > > > vectorFuncs_
Function1 mappings/timelines (vector), evaluated at the simulation time or with arbitrary scalars.
Definition exprDriver.H:223
const objectRegistry * obrPtr_
Pointer to an object registry (for functions etc).
Definition exprDriver.H:243
const dictionary & dict_
The dictionary with all input data/specification.
Definition exprDriver.H:196
A class for handling keywords in dictionaries.
Definition keyType.H:69
@ LITERAL
String literal.
Definition keyType.H:82
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition keyTypeI.H:97
Registry of regIOobjects.
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
#define InfoInFunction
Report an information message using Foam::Info.
Namespace for handling debugging switches.
Definition debug.C:45
Namespace for OpenFOAM.
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition HashSet.H:80
static wordHashSet getAcceptableFunctionKeys(const dictionary *dictPtr, const bool acceptPrimitiveEntry, const bool report=false)
static void resetFuncsImpl(const word &subDictName, const dictionary &topDict, HashTable< refPtr< Function1< Type > > > &tbl, const objectRegistry *obrPtr)
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
static void writeFuncsImpl(Ostream &os, const word &subDictName, const dictionary &topDict, const HashTable< refPtr< Function1< Type > > > &tbl)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict