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 * * * * * * * * * * * * * * //
31
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(0);
46
47 if (!dictPtr)
48 {
49 return acceptKeys;
50 }
51
52 const dictionary& dict = *dictPtr;
53
54 acceptKeys.resize(2*dict.size());
55 wordHashSet rejectKeys(2*dict.size());
56
57 for (const entry& dEntry : dict)
58 {
59 const keyType& kw = dEntry.keyword();
60
61 bool ok = true;
62
63 if (kw.isPattern())
64 {
65 ok = false;
66 }
67 else if (dEntry.isDict())
68 {
69 // Dictionary entry - require "type", which should eliminate
70 // any *Coeffs dictionaries
71
72 ok = dEntry.dict().found("type", keyType::LITERAL);
73 }
74 else
75 {
76 // Primitive entry. Trust that it is okay?
77 ok = acceptPrimitiveEntry;
78 }
79
80 if (ok)
81 {
82 acceptKeys.insert(kw);
83 }
84 else
85 {
86 rejectKeys.insert(kw);
87 }
88 }
89
90 if (report && rejectKeys.size())
91 {
93 << "Dropped invalid/redundant entries: "
94 << flatOutput(rejectKeys.sortedToc()) << nl;
95 }
96
97 return acceptKeys;
98}
99
100
101// Read and reset Function1 for given dictionary.
102// Uses getAcceptableFunctionKeys
103template<class Type>
104static void resetFuncsImpl
105(
106 const word& subDictName,
107 const dictionary& topDict,
109 const objectRegistry* obrPtr
110)
111{
112 tbl.clear();
113
114 const dictionary* dictPtr =
115 topDict.findDict(subDictName, keyType::LITERAL);
116
117 if (!dictPtr)
118 {
119 return;
120 }
121
122 wordHashSet acceptKeys
123 (
125 (
126 dictPtr,
127 true // Accept primitive entries, hope for the best
128 )
129 );
130
131 const dictionary& dict = *dictPtr;
132
133 for (const word& entryName : acceptKeys)
134 {
135 // From autoPtr -> refPtr
137 (
138 Function1<Type>::New(entryName, dict, obrPtr)
139 );
140
141 if (func)
142 {
143 tbl.insert(entryName, std::move(func));
144 }
145 }
146}
147
148
149// Write out entries, if they originated from dictionary
150template<class Type>
151static void writeFuncsImpl
152(
153 Ostream& os,
154 const word& subDictName,
155 const dictionary& topDict,
156 const HashTable<refPtr<Function1<Type>>>& tbl
157)
158{
159 const dictionary* dictPtr =
160 topDict.findDict(subDictName, keyType::LITERAL);
161
162 if (!dictPtr || tbl.empty())
163 {
164 return;
165 }
166
167 label nwrote = 0;
168
169 const dictionary& dict = *dictPtr;
170
171 for (const entry& dEntry : dict)
172 {
173 const word& entryName = dEntry.keyword();
174
175 const auto iter = tbl.cfind(entryName);
176
177 if (!iter.found())
178 {
179 continue;
180 }
181
182 const auto& funcPtr = iter.val();
183
184 if (funcPtr)
185 {
186 if (!nwrote++)
187 {
188 os.beginBlock(subDictName);
189 }
190
191 (*funcPtr).writeData(os);
192 }
193 }
194
195 if (nwrote)
196 {
197 os.endBlock();
198 }
199}
200
201} // End namespace Foam
202
203
204// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
205
206void Foam::expressions::exprDriver::resetFunctions
207(
208 const dictionary& dict
209)
210{
211 resetFuncsImpl<scalar>("functions<scalar>", dict_, scalarFuncs_, obrPtr_);
212 resetFuncsImpl<vector>("functions<vector>", dict_, vectorFuncs_, obrPtr_);
213
214 if (debug)
215 {
217 }
218}
219
220
222{
223 writeFuncsImpl<scalar>(os, "functions<scalar>", dict_, scalarFuncs_);
224 writeFuncsImpl<vector>(os, "functions<vector>", dict_, vectorFuncs_);
225}
226
227
228// ************************************************************************* //
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:96
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:137
void resize(const label sz)
Resize the hash table for efficiency.
Definition: HashTable.C:601
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:105
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:87
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
dictionary * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary pointer if present.
Definition: dictionaryI.H:127
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
void writeFunctions(Ostream &os) const
Write scalar/vector Function1 entries in dictionary format.
HashTable< refPtr< Function1< scalar > > > scalarFuncs_
Definition: exprDriver.H:201
HashTable< refPtr< Function1< vector > > > vectorFuncs_
Definition: exprDriver.H:205
const objectRegistry * obrPtr_
Pointer to an object registry (for functions etc).
Definition: exprDriver.H:217
const dictionary & dict_
The dictionary with all input data/specification.
Definition: exprDriver.H:188
A class for handling keywords in dictionaries.
Definition: keyType.H:71
@ LITERAL
String literal.
Definition: keyType.H:81
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:104
Registry of regIOobjects.
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
#define InfoInFunction
Report an information message using Foam::Info.
Namespace for OpenFOAM.
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
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:215
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:53
dictionary dict