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-2024 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,
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& coeffsDict(dict.subDict(name));
42 
43  const word Function2Type(coeffsDict.lookup("type"));
44 
45  typename dictionaryConstructorTable::iterator cstrIter =
46  dictionaryConstructorTablePtr_->find(Function2Type);
47 
48  if (cstrIter == dictionaryConstructorTablePtr_->end())
49  {
51  << "Unknown Function2 type "
52  << Function2Type << " for Function2 "
53  << name << nl << nl
54  << "Valid Function2 types are:" << nl
55  << dictionaryConstructorTablePtr_->sortedToc() << nl
56  << exit(FatalError);
57  }
58 
59  return cstrIter()(name, units, coeffsDict);
60  }
61 
62  // Find the entry
63  Istream& is(dict.lookup(name));
64 
65  // Peek at the first token
66  token firstToken(is);
67  is.putBack(firstToken);
68 
69  // Read the type, or assume constant
70  const word Function2Type =
72 
73  // If the entry is not a type followed by a end statement then
74  // construct the function from the stream
75  if (!firstToken.isWord() || !is.eof())
76  {
77  return New(name, units, Function2Type, is);
78  }
79 
80  // Otherwise, construct from the current dictionary
81  typename dictionaryConstructorTable::iterator dictCstrIter =
82  dictionaryConstructorTablePtr_->find(Function2Type);
83 
84  if (dictCstrIter == dictionaryConstructorTablePtr_->end())
85  {
87  << "Unknown Function2 type "
88  << Function2Type << " for Function2 "
89  << name << nl << nl
90  << "Valid Function2 types are:" << nl
91  << dictionaryConstructorTablePtr_->sortedToc() << nl
92  << exit(FatalError);
93  }
94 
95  return dictCstrIter()(name, units, dict);
96 }
97 
98 
99 template<class Type>
101 (
102  const word& name,
103  const unitConversion& xUnits,
104  const unitConversion& yUnits,
105  const unitConversion& valueUnits,
106  const dictionary& dict
107 )
108 {
109  return New(name, {xUnits, yUnits, valueUnits}, dict);
110 }
111 
112 
113 template<class Type>
115 (
116  const word& name,
118  const word& Function2Type,
119  Istream& is
120 )
121 {
122  typename dictionaryConstructorTable::iterator dictCstrIter =
123  dictionaryConstructorTablePtr_->find(Function2Type);
124  const bool haveDictCstrIter =
125  dictCstrIter != dictionaryConstructorTablePtr_->end();
126 
127  typename IstreamConstructorTable::iterator isCstrIter =
128  IstreamConstructorTablePtr_->find(Function2Type);
129  const bool haveIstreamCstrIter =
130  isCstrIter != IstreamConstructorTablePtr_->end();
131 
132  if (!haveDictCstrIter && !haveIstreamCstrIter)
133  {
135  << "Unknown Function2 type "
136  << Function2Type << " for Function2 "
137  << name << nl << nl
138  << "Valid Function2 types are:" << nl
139  << dictionaryConstructorTablePtr_->sortedToc() << nl
140  << exit(FatalError);
141  }
142 
143  if (!haveIstreamCstrIter)
144  {
146  << "Function2 type "
147  << name << " cannot be specified inline" << nl << nl
148  << "Make " << name << " a sub-dictionary"
149  << exit(FatalError);
150  }
151 
152  return isCstrIter()(name, units, is);
153 }
154 
155 
156 template<class Type>
158 (
159  const word& name,
160  const unitConversion& xUnits,
161  const unitConversion& yUnits,
162  const unitConversion& valueUnits,
163  const word& Function2Type,
164  Istream& is
165 )
166 {
167  return New(name, {xUnits, yUnits, valueUnits}, Function2Type, is);
168 }
169 
170 
171 // ************************************************************************* //
static autoPtr< Function2< Type > > New(const word &name, const Function2s::unitConversions &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
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:336
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
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 keyword definitions, which are a keyword followed by any number of values (e....
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:710
A token holds items read from Istream.
Definition: token.H:73
bool isWord() const
Definition: tokenI.H:298
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
autoPtr< CompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const viscosity &viscosity)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
const HashTable< unitConversion > & units()
Get the table of unit conversions.
error FatalError
static const char nl
Definition: Ostream.H:266
dictionary dict