Function1Expression.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) 2020 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 "Function1Expression.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  const word& entryName,
36  const dictionary& dict
37 )
38 :
39  Function1<Type>(entryName, dict),
40  dict_(dict),
41  valueExpr_(),
42  driver_(1, dict_)
43 {
44  if (dict.getOrDefault("debug", false))
45  {
46  debug |= 1;
47  }
48 
49  string expr;
50  dict.readEntry("expression", expr);
51  valueExpr_ = expressions::exprString(expr, dict);
52 
53  // Basic sanity
54  if (valueExpr_.empty())
55  {
57  << "The expression was not defined!" << nl
58  << exit(FatalIOError);
59  }
60 
61  driver_.readDict(dict_);
62 }
63 
64 
65 template<class Type>
67 (
68  const Function1Expression<Type>& rhs
69 )
70 :
71  Function1<Type>(rhs),
72  dict_(rhs.dict_),
73  valueExpr_(rhs.valueExpr_),
74  driver_(1, rhs.driver_)
75 {}
76 
77 
78 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
79 
80 template<class Type>
82 (
83  const scalar x
84 ) const
85 {
86  // Expression evaluation
87  driver_.clearVariables();
88 
89  driver_.setArgument(x);
90 
91  driver_.parse(this->valueExpr_);
92 
93  expressions::exprResult result(driver_.result());
94 
95  DebugInfo
96  << "Evaluated: " << result << nl;
97 
98  if (!result.hasValue() || !result.size() || !result.isType<Type>())
99  {
101  << "Could not evaluate: " << this->valueExpr_
102  << exit(FatalError);
103  }
104 
105  return result.cref<Type>().first();
106 }
107 
108 
109 template<class Type>
111 (
112  const scalar x1,
113  const scalar x2
114 ) const
115 {
117  return Zero;
118 }
119 
120 
121 template<class Type>
123 (
124  Ostream& os
125 ) const
126 {
127  // Function1-from-subdict so out dictionary contains
128  // only the relevant entries.
129  dict_.writeEntry(this->name(), os);
130 }
131 
132 
133 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::FatalIOError
IOerror FatalIOError
Foam::Function1Types::Function1Expression::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1Expression.C:123
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:86
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:445
Foam::expressions::exprResult
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:128
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::Function1Types::Function1Expression::Function1Expression
Function1Expression(const word &entryName, const dictionary &dict)
Construct from entry name and dictionary.
Definition: Function1Expression.C:34
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::Function1Types::Function1Expression::integrate
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two values.
Definition: Function1Expression.C:111
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::Function1Types::Function1Expression::value
virtual Type value(const scalar x) const
Return value.
Definition: Function1Expression.C:82
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:359
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::expressions::exprString
Definition: exprString.H:60
x
x
Definition: LISASMDCalcMethod2.H:52
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Function1Expression.H
Foam::Function1Types::Function1Expression
Function1 with values supplied by a parsed expression.
Definition: Function1Expression.H:85