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 -------------------------------------------------------------------------------
10 License
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 
32 namespace 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
103 template<class Type>
104 static 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
150 template<class Type>
151 static 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 
206 void 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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::dictionary::findDict
dictionary * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary pointer if present.
Definition: dictionaryI.H:127
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::keyType::isPattern
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:104
Foam::expressions::exprDriver::vectorFuncs_
HashTable< refPtr< Function1< vector > > > vectorFuncs_
Definition: exprDriver.H:204
Foam::Ostream::beginBlock
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:91
Foam::expressions::exprDriver::dict_
const dictionary & dict_
The dictionary with all input data/specification.
Definition: exprDriver.H:187
Foam::HashSet
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:77
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
exprDriver.H
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:68
Foam::writeFuncsImpl
static void writeFuncsImpl(Ostream &os, const word &subDictName, const dictionary &topDict, const HashTable< refPtr< Function1< Type >>> &tbl)
Definition: exprDriverFunctions.C:152
Foam::expressions::exprDriver::writeFunctions
void writeFunctions(Ostream &os) const
Write scalar/vector Function1 entries in dictionary format.
Definition: exprDriverFunctions.C:221
Foam::func
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::Ostream::endBlock
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:109
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::getAcceptableFunctionKeys
static wordHashSet getAcceptableFunctionKeys(const dictionary *dictPtr, const bool acceptPrimitiveEntry, const bool report=false)
Definition: exprDriverFunctions.C:39
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::expressions::exprDriver::scalarFuncs_
HashTable< refPtr< Function1< scalar > > > scalarFuncs_
Definition: exprDriver.H:200
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::expressions::exprDriver::obrPtr_
const objectRegistry * obrPtr_
Pointer to an object registry (for functions etc).
Definition: exprDriver.H:216
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
Foam::wordHashSet
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:77
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:81
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::resetFuncsImpl
static void resetFuncsImpl(const word &subDictName, const dictionary &topDict, HashTable< refPtr< Function1< Type >>> &tbl, const objectRegistry *obrPtr)
Definition: exprDriverFunctions.C:105
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60