Function2New.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) 2020-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 "Constant2.H"
27 
28 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
29 
30 template<class Type>
32 (
33  const word& name,
34  const Function2s::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 Function2Type(coeffDict.lookup("type"));
44 
45  if (printDictionary::prints(coeffDict))
46  {
47  Info<< indent << "Selecting " << typeName << " "
48  << Function2Type << endl;
49  }
50 
51  typename dictionaryConstructorTable::iterator cstrIter =
52  dictionaryConstructorTablePtr_->find(Function2Type);
53 
54  if (cstrIter == dictionaryConstructorTablePtr_->end())
55  {
57  << "Unknown Function2 type "
58  << Function2Type << " for Function2 "
59  << name << nl << nl
60  << "Valid Function2 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 Function2Type =
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, Function2Type, is);
86  }
87 
88  // Otherwise, construct from the current dictionary
89  typename dictionaryConstructorTable::iterator dictCstrIter =
90  dictionaryConstructorTablePtr_->find(Function2Type);
91 
92  if (dictCstrIter == dictionaryConstructorTablePtr_->end())
93  {
95  << "Unknown Function2 type "
96  << Function2Type << " for Function2 "
97  << name << nl << nl
98  << "Valid Function2 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& yUnits,
113  const unitSet& valueUnits,
114  const dictionary& dict
115 )
116 {
117  return New(name, {xUnits, yUnits, valueUnits}, dict);
118 }
119 
120 
121 template<class Type>
123 (
124  const word& name,
125  const Function2s::unitSets& units,
126  const word& Function2Type,
127  Istream& is
128 )
129 {
130  typename dictionaryConstructorTable::iterator dictCstrIter =
131  dictionaryConstructorTablePtr_->find(Function2Type);
132  const bool haveDictCstrIter =
133  dictCstrIter != dictionaryConstructorTablePtr_->end();
134 
135  typename IstreamConstructorTable::iterator isCstrIter =
136  IstreamConstructorTablePtr_->find(Function2Type);
137  const bool haveIstreamCstrIter =
138  isCstrIter != IstreamConstructorTablePtr_->end();
139 
140  if (!haveDictCstrIter && !haveIstreamCstrIter)
141  {
143  << "Unknown Function2 type "
144  << Function2Type << " for Function2 "
145  << name << nl << nl
146  << "Valid Function2 types are:" << nl
147  << dictionaryConstructorTablePtr_->sortedToc() << nl
148  << exit(FatalError);
149  }
150 
151  if (!haveIstreamCstrIter)
152  {
154  << "Function2 type "
155  << name << " cannot be specified inline" << nl << nl
156  << "Make " << name << " a sub-dictionary"
157  << exit(FatalError);
158  }
159 
160  return isCstrIter()(name, units, is);
161 }
162 
163 
164 template<class Type>
166 (
167  const word& name,
168  const unitSet& xUnits,
169  const unitSet& yUnits,
170  const unitSet& valueUnits,
171  const word& Function2Type,
172  Istream& is
173 )
174 {
175  return New(name, {xUnits, yUnits, valueUnits}, Function2Type, is);
176 }
177 
178 
179 // ************************************************************************* //
static autoPtr< Function2< Type > > New(const word &name, const Function2s::unitSets &units, const dictionary &dict)
Select from dictionary.
Definition: Function2New.C:32
Templated function of two variables that returns a constant value.
Definition: Constant2.H:66
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
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
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