Function1New.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Constant.H"
27 
28 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
29 
30 template<class Type>
32 (
33  const word& name,
34  const Function1s::unitSets& units,
35  const dictionary& dict
36 )
37 {
38  // If the function is a dictionary (preferred) then read straightforwardly
39  if (dict.isDict(name))
40  {
41  const dictionary& coeffDict(dict.subDict(name));
42 
43  const word Function1Type(coeffDict.lookup("type"));
44 
45  if (printDictionary::prints(coeffDict))
46  {
47  Info<< indent << "Selecting " << typeName << " "
48  << Function1Type << endl;
49  }
50 
51  typename dictionaryConstructorTable::iterator cstrIter =
52  dictionaryConstructorTablePtr_->find(Function1Type);
53 
54  if (cstrIter == dictionaryConstructorTablePtr_->end())
55  {
57  << "Unknown Function1 type "
58  << Function1Type << " for Function1 "
59  << name << nl << nl
60  << "Valid Function1 types are:" << nl
61  << dictionaryConstructorTablePtr_->sortedToc() << nl
62  << exit(FatalIOError);
63  }
64 
65  printDictionary print(coeffDict);
66 
67  return cstrIter()(name, units, coeffDict);
68  }
69 
70  // Find the entry
71  Istream& is(dict.lookup(name));
72 
73  // Peek at the first token
74  token firstToken(is);
75  is.putBack(firstToken);
76 
77  // Read the type, or assume constant
78  const word Function1Type =
80 
81  // If the entry is not a type followed by a end statement then
82  // construct the function from the stream
83  if (!firstToken.isWord() || !is.eof())
84  {
85  return New(name, units, Function1Type, is);
86  }
87 
88  // Otherwise, construct from the current or coeffs dictionary
89  typename dictionaryConstructorTable::iterator dictCstrIter =
90  dictionaryConstructorTablePtr_->find(Function1Type);
91 
92  if (dictCstrIter == dictionaryConstructorTablePtr_->end())
93  {
95  << "Unknown Function1 type "
96  << Function1Type << " for Function1 "
97  << name << nl << nl
98  << "Valid Function1 types are:" << nl
99  << dictionaryConstructorTablePtr_->sortedToc() << nl
100  << exit(FatalIOError);
101  }
102 
103  return dictCstrIter()(name, units, dict);
104 }
105 
106 
107 template<class Type>
109 (
110  const word& name,
111  const unitSet& xUnits,
112  const unitSet& valueUnits,
113  const dictionary& dict
114 )
115 {
116  return New(name, {xUnits, valueUnits}, dict);
117 }
118 
119 
120 template<class Type>
122 (
123  const Function1s::unitSets& units,
124  const entry& e
125 )
126 {
127  // If the function is a dictionary (preferred) then read straightforwardly
128  if (e.isDict())
129  {
130  const dictionary& coeffDict(e.dict());
131 
132  const word Function1Type(coeffDict.lookup("type"));
133 
134  if (printDictionary::prints(coeffDict))
135  {
136  Info<< indent << "Selecting " << typeName << " "
137  << Function1Type << endl;
138  }
139 
140  typename dictionaryConstructorTable::iterator cstrIter =
141  dictionaryConstructorTablePtr_->find(Function1Type);
142 
143  if (cstrIter == dictionaryConstructorTablePtr_->end())
144  {
145  FatalIOErrorInFunction(e.dict())
146  << "Unknown Function1 type "
147  << Function1Type << " for Function1 "
148  << e.keyword() << nl << nl
149  << "Valid Function1 types are:" << nl
150  << dictionaryConstructorTablePtr_->sortedToc() << nl
151  << exit(FatalIOError);
152  }
153 
154  printDictionary print(coeffDict);
155 
156  return cstrIter()(e.keyword(), units, coeffDict);
157  }
158 
159  // Get the stream
160  Istream& is(e.stream());
161 
162  // Peek at the first token
163  token firstToken(is);
164  is.putBack(firstToken);
165 
166  // Read the type, or assume constant
167  const word Function1Type =
168  firstToken.isWord() ? word(is) : Function1s::Constant<Type>::typeName;
169 
170  // If the entry is not a type followed by a end statement then
171  // construct the function from the stream
172  if (!firstToken.isWord() || !is.eof())
173  {
174  return New(e.keyword(), units, Function1Type, is);
175  }
176 
177  FatalIOErrorInFunction(e.stream())
178  << "Unable to construct Function1 for " << e.keyword()
179  << exit(FatalIOError);
180 
181  return autoPtr<Function1<Type>>(nullptr);
182 }
183 
184 
185 template<class Type>
187 (
188  const unitSet& xUnits,
189  const unitSet& valueUnits,
190  const entry& e
191 )
192 {
193  return New({xUnits, valueUnits}, e);
194 }
195 
196 
197 template<class Type>
199 (
200  const word& name,
201  const Function1s::unitSets& units,
202  const word& Function1Type,
203  Istream& is
204 )
205 {
206  typename dictionaryConstructorTable::iterator dictCstrIter =
207  dictionaryConstructorTablePtr_->find(Function1Type);
208  const bool haveDictCstrIter =
209  dictCstrIter != dictionaryConstructorTablePtr_->end();
210 
211  typename IstreamConstructorTable::iterator isCstrIter =
212  IstreamConstructorTablePtr_->find(Function1Type);
213  const bool haveIstreamCstrIter =
214  isCstrIter != IstreamConstructorTablePtr_->end();
215 
216  if (!haveDictCstrIter && !haveIstreamCstrIter)
217  {
219  << "Unknown Function1 type "
220  << Function1Type << " for Function1 "
221  << name << nl << nl
222  << "Valid Function1 types are:" << nl
223  << dictionaryConstructorTablePtr_->sortedToc() << nl
224  << exit(FatalError);
225  }
226 
227  if (!haveIstreamCstrIter)
228  {
230  << "Function1 type "
231  << Function1Type << " for Function1 "
232  << name << " cannot be specified inline" << nl << nl
233  << "Make " << name << " a sub-dictionary"
234  << exit(FatalError);
235  }
236 
237  return isCstrIter()(name, units, is);
238 }
239 
240 
241 template<class Type>
243 (
244  const word& name,
245  const unitSet& xUnits,
246  const unitSet& valueUnits,
247  const word& Function1Type,
248  Istream& is
249 )
250 {
251  return New(name, {xUnits, valueUnits}, Function1Type, is);
252 }
253 
254 
255 // ************************************************************************* //
static autoPtr< Function1< Type > > New(const word &name, const Function1s::unitSets &units, const dictionary &dict)
Select from dictionary.
Definition: Function1New.C:32
Templated function that returns a constant value.
Definition: Constant.H:60
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
void putBack(const token &)
Put back token.
Definition: Istream.C:30
bool eof() const
Return true if end of input seen.
Definition: Istream.H:107
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:669
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
Enables the printing of a dictionary and subsequently looked-up defaulted entries.
A token holds items read from Istream.
Definition: token.H:74
bool isWord() const
Definition: tokenI.H:342
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
Definition: unitSet.H:68
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
const dimensionedScalar e
Elementary charge.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
messageStream Info
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
static const char nl
Definition: Ostream.H:297
dictionary dict
Struct containing two unitSets for use in converting both the argument and the value of a Function1.