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-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 "Constant.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 Function1Type(coeffsDict.lookup("type"));
44 
45  typename dictionaryConstructorTable::iterator cstrIter =
46  dictionaryConstructorTablePtr_->find(Function1Type);
47 
48  if (cstrIter == dictionaryConstructorTablePtr_->end())
49  {
51  << "Unknown Function1 type "
52  << Function1Type << " for Function1 "
53  << name << nl << nl
54  << "Valid Function1 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 Function1Type =
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, Function1Type, is);
78  }
79 
80  // Otherwise, construct from the current or coeffs dictionary
81  typename dictionaryConstructorTable::iterator dictCstrIter =
82  dictionaryConstructorTablePtr_->find(Function1Type);
83 
84  if (dictCstrIter == dictionaryConstructorTablePtr_->end())
85  {
87  << "Unknown Function1 type "
88  << Function1Type << " for Function1 "
89  << name << nl << nl
90  << "Valid Function1 types are:" << nl
91  << dictionaryConstructorTablePtr_->sortedToc() << nl
92  << exit(FatalError);
93  }
94 
95  const bool haveCoeffsDict = dict.found(name + "Coeffs");
96 
98  (
99  dictCstrIter()
100  (
101  name,
102  units,
103  haveCoeffsDict ? dict.subDict(name + "Coeffs") : dict
104  )
105  );
106 
107  if (haveCoeffsDict)
108  {
110  << "Using deprecated "
111  << (name + "Coeffs") << " sub-dictionary."<< nl
112  << " Please use the simpler form" << endl;
113  funcPtr->write(Info, units);
114  }
115 
116  return funcPtr;
117 }
118 
119 
120 template<class Type>
122 (
123  const word& name,
124  const unitConversion& xUnits,
125  const unitConversion& valueUnits,
126  const dictionary& dict
127 )
128 {
129  return New(name, {xUnits, valueUnits}, dict);
130 }
131 
132 
133 template<class Type>
135 (
136  const word& name,
138  const word& Function1Type,
139  Istream& is
140 )
141 {
142  typename dictionaryConstructorTable::iterator dictCstrIter =
143  dictionaryConstructorTablePtr_->find(Function1Type);
144  const bool haveDictCstrIter =
145  dictCstrIter != dictionaryConstructorTablePtr_->end();
146 
147  typename IstreamConstructorTable::iterator isCstrIter =
148  IstreamConstructorTablePtr_->find(Function1Type);
149  const bool haveIstreamCstrIter =
150  isCstrIter != IstreamConstructorTablePtr_->end();
151 
152  if (!haveDictCstrIter && !haveIstreamCstrIter)
153  {
155  << "Unknown Function1 type "
156  << Function1Type << " for Function1 "
157  << name << nl << nl
158  << "Valid Function1 types are:" << nl
159  << dictionaryConstructorTablePtr_->sortedToc() << nl
160  << exit(FatalError);
161  }
162 
163  if (!haveIstreamCstrIter)
164  {
166  << "Function1 type "
167  << Function1Type << " for Function1 "
168  << name << " cannot be specified inline" << nl << nl
169  << "Make " << name << " a sub-dictionary"
170  << exit(FatalError);
171  }
172 
173  return isCstrIter()(name, units, is);
174 }
175 
176 
177 template<class Type>
179 (
180  const word& name,
181  const unitConversion& xUnits,
182  const unitConversion& valueUnits,
183  const word& Function1Type,
184  Istream& is
185 )
186 {
187  return New(name, {xUnits, valueUnits}, Function1Type, is);
188 }
189 
190 
191 // ************************************************************************* //
static autoPtr< Function1< Type > > New(const word &name, const Function1s::unitConversions &units, const dictionary &dict)
Select from dictionary.
Definition: Function1New.C:32
Templated function that returns a constant value.
Definition: Constant.H:60
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
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
const HashTable< unitConversion > & units()
Get the table of unit conversions.
error FatalError
static const char nl
Definition: Ostream.H:266
dictionary dict