diff --git a/applications/test/dictionary/calcEntry/addToGlobalFunctionSelectionTable.H b/applications/test/dictionary/calcEntry/addToGlobalFunctionSelectionTable.H new file mode 100644 index 0000000000..d6eadfdd74 --- /dev/null +++ b/applications/test/dictionary/calcEntry/addToGlobalFunctionSelectionTable.H @@ -0,0 +1,53 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +@file Foam::addToGlobalFunctionSelectionTable + +Description + Macros for easy insertion into global function selection tables + +\*---------------------------------------------------------------------------*/ + +#ifndef addToGlobalFunctionSelectionTable_H +#define addToGlobalFunctionSelectionTable_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + + +// add to hash-table of functions with 'lookup' as the key +#define addNamedToGlobalFunctionSelectionTable\ +(memberFunction,argNames,lookup,functionPtr) \ + \ + /* Add to the table, find by lookup name */ \ + add##memberFunction##argNames##GlobalMemberFunctionToTable \ + add_##lookup##_##memberFunction##argNames##GlobalMemberFunctionTo##Table_\ + (#lookup, functionPtr) + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + + +#endif + +// ************************************************************************* // diff --git a/applications/test/dictionary/calcEntry/calcEntry.atg b/applications/test/dictionary/calcEntry/calcEntry.atg index 57646c3cad..ac6766aae4 100644 --- a/applications/test/dictionary/calcEntry/calcEntry.atg +++ b/applications/test/dictionary/calcEntry/calcEntry.atg @@ -39,10 +39,7 @@ SourceFiles \*---------------------------------------------------------------------------*/ #include "dictionary.H" -#include "scalar.H" -#include "error.H" #include "wchar.H" -#include "DynamicList.H" #include "calcEntryInternal.H" @@ -56,7 +53,7 @@ COMPILER calcEntry private: //- The parent dictionary - mutable dictionary* dict_; + dictionary* dict_; //- The calculation result scalar val; @@ -196,8 +193,8 @@ Expr (. scalar val2 = 0; .) = Term { - "+" Term (. val += val2; .) - | "-" Term (. val -= val2; .) + '+' Term (. val += val2; .) + | '-' Term (. val -= val2; .) } . @@ -208,20 +205,26 @@ Term (. scalar val2 = 0; .) = Factor { - "*" Factor (. val *= val2; .) - | "/" Factor (. val /= val2; .) + '*' Factor (. val *= val2; .) + | '/' Factor (. val /= val2; .) } . /*---------------------------------------------------------------------------*/ -Factor + +// Note the treatment of the leading signs is fairly generous +// eg, "10 + - 10" is treated like "10 + -10" +// +Factor (. bool negative = false; .) = - Func - | variable (. val = getDictLookup(); .) - | number (. val = coco_string_toDouble(t->val); .) - | '-' '(' Expr ')' (. val = -val; .) - | '(' Expr ')' + ['+' | '-' (. negative = true; .) + ] + ( + Func | '(' Expr ')' + | variable (. val = getDictLookup(); .) + | number (. val = coco_string_toDouble(t->val); .) + ) (. if (negative) { val = -val; } .) . @@ -242,7 +245,7 @@ Func { ',' Expr (. param.append(x); .) } ] - ')' (. val = scalarFunctions::dispatch(funcName, param); .) + ')' (. val = dispatch(funcName, param); .) . diff --git a/applications/test/dictionary/calcEntry/calcEntryInternal.C b/applications/test/dictionary/calcEntry/calcEntryInternal.C index 888475cee6..086238d9a3 100644 --- a/applications/test/dictionary/calcEntry/calcEntryInternal.C +++ b/applications/test/dictionary/calcEntry/calcEntryInternal.C @@ -25,8 +25,7 @@ License \*---------------------------------------------------------------------------*/ #include "calcEntryInternal.H" -#include "addToMemberFunctionSelectionTable.H" -#include "addToStaticMemberFunctionSelectionTable.H" +#include "addToGlobalFunctionSelectionTable.H" #include "unitConversion.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -38,14 +37,66 @@ namespace functionEntries namespace calcEntryInternal { -defineStaticMemberFunctionSelectionTable(scalarFunctions,dispatch,ParamList); +defineGlobalFunctionSelectionTable(dispatch,ParamList); -scalar scalarFunctions::dispatch -( - const word& name, - const UList& param -) +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#define globalConstant0(Name, Constant)\ +scalar Name##_0(const UList& param) \ +{ \ + return Constant; \ +} \ +addNamedToGlobalFunctionSelectionTable(dispatch,ParamList,Name##_0,&Name##_0) + + +#define globalFunction0(Name, Function)\ +scalar Name##_0(const UList& param) \ +{ \ + return Function(); \ +} \ +addNamedToGlobalFunctionSelectionTable(dispatch,ParamList,Name##_0,&Name##_0) + + +#define globalFunction1(Name, Function)\ +scalar Name##_1(const UList& param) \ +{ \ + return Function(param[0]); \ +} \ +addNamedToGlobalFunctionSelectionTable(dispatch,ParamList,Name##_1,&Name##_1) + + +#define globalFunction2(Name, Function)\ +scalar Name##_2(const UList& param) \ +{ \ + return Function(param[0], param[1]); \ +} \ +addNamedToGlobalFunctionSelectionTable(dispatch,ParamList,Name##_2,&Name##_2) + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +globalConstant0(pi, constant::mathematical::pi); + +globalFunction1(degToRad, degToRad); +globalFunction1(radToDeg, radToDeg); +globalFunction1(asin, Foam::asin); +globalFunction1(acos, Foam::acos); +globalFunction1(atan, Foam::atan); +globalFunction1(sin, Foam::sin); +globalFunction1(cos, Foam::cos); +globalFunction1(tan, Foam::tan); +globalFunction1(log, Foam::log); +globalFunction1(log10, Foam::log10); +globalFunction1(mag, Foam::mag); + +globalFunction2(atan2, Foam::atan2); +globalFunction2(pow, Foam::pow); + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +scalar dispatch(const word& name, const UList& param) { // create lookup name with parameter count const word lookupName = name + '_' + Foam::name(param.size()); @@ -69,114 +120,10 @@ scalar scalarFunctions::dispatch } -scalar scalarFunctions::pi_0(const UList& param) -{ - return constant::mathematical::pi; -} - -scalar scalarFunctions::degToRad_1(const UList& param) -{ - return degToRad(param[0]); -} - -scalar scalarFunctions::radToDeg_1(const UList& param) -{ - return radToDeg(param[0]); -} - -scalar scalarFunctions::sin_1(const UList& param) -{ - return Foam::sin(param[0]); -} - -scalar scalarFunctions::cos_1(const UList& param) -{ - return Foam::cos(param[0]); -} - -scalar scalarFunctions::pow_2(const UList& param) -{ - return Foam::pow(param[0], param[1]); -} - -scalar scalarFunctions::log_1(const UList& param) -{ - return Foam::log(param[0]); -} - -scalar scalarFunctions::log10_1(const UList& param) -{ - return Foam::log10(param[0]); -} - - - -addNamedToStaticMemberFunctionSelectionTable -( - scalarFunctions,scalarFunctions,dispatch,ParamList, - pi_0, - &scalarFunctions::pi_0 -); - -addNamedToStaticMemberFunctionSelectionTable -( - scalarFunctions,scalarFunctions,dispatch,ParamList, - degToRad_1, - &scalarFunctions::degToRad_1 -); - -addNamedToStaticMemberFunctionSelectionTable -( - scalarFunctions,scalarFunctions,dispatch,ParamList, - radToDeg_1, - &scalarFunctions::radToDeg_1 -); - -addNamedToStaticMemberFunctionSelectionTable -( - scalarFunctions,scalarFunctions,dispatch,ParamList, - sin_1, - &scalarFunctions::sin_1 -); - -addNamedToStaticMemberFunctionSelectionTable -( - scalarFunctions,scalarFunctions,dispatch,ParamList, - cos_1, - &scalarFunctions::cos_1 -); - -addNamedToStaticMemberFunctionSelectionTable -( - scalarFunctions,scalarFunctions,dispatch,ParamList, - pow_2, - &scalarFunctions::pow_2 -); - -addNamedToStaticMemberFunctionSelectionTable -( - scalarFunctions,scalarFunctions,dispatch,ParamList, - log_1, - &scalarFunctions::log_1 -); - - - -addNamedToStaticMemberFunctionSelectionTable -( - scalarFunctions,scalarFunctions,dispatch,ParamList, - log10_1, - &scalarFunctions::log10_1 -); - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace calcEntryInternal } // End namespace functionEntries } // End namespace Foam -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - - // ************************************************************************* // diff --git a/applications/test/dictionary/calcEntry/calcEntryInternal.H b/applications/test/dictionary/calcEntry/calcEntryInternal.H index 73ff08c7e4..2dd3a03056 100644 --- a/applications/test/dictionary/calcEntry/calcEntryInternal.H +++ b/applications/test/dictionary/calcEntry/calcEntryInternal.H @@ -26,8 +26,7 @@ Namespace Foam::functionEntries::calcEntryInternal Description - This dictionary function entry may or may not do anything particularly - useful - depending upon what is currently being used to test. + Contains global functions and classes for the calcEntry. SourceFiles calcEntryInternal.C @@ -37,9 +36,10 @@ SourceFiles #ifndef calcEntryInternal_H #define calcEntryInternal_H -#include "functionEntry.H" -#include "memberFunctionSelectionTables.H" -#include "staticMemberFunctionSelectionTables.H" +#include "error.H" +#include "scalar.H" +#include "DynamicList.H" +#include "globalFunctionSelectionTables.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -50,42 +50,22 @@ namespace functionEntries namespace calcEntryInternal { -/*---------------------------------------------------------------------------*\ - Class calcEntryFunctions Declaration -\*---------------------------------------------------------------------------*/ + // Global Function Selectors -class scalarFunctions -{ -public: - - // Member Function Selectors - - declareStaticMemberFunctionSelectionTable + declareGlobalFunctionSelectionTable + ( + scalar, + dispatch, + ParamList, ( - scalar, - scalarFunctions, - dispatch, - ParamList, - ( - const UList& param - ), - (param) - ); - - //- Calculate - static scalar dispatch(const word&, const UList&); + const UList& param + ), + (param) + ); - static scalar pi_0(const UList&); - static scalar degToRad_1(const UList&); - static scalar radToDeg_1(const UList&); - static scalar cos_1(const UList&); - static scalar sin_1(const UList&); - static scalar pow_2(const UList&); - static scalar log_1(const UList&); - static scalar log10_1(const UList&); - -}; + //- Dispatch calculation to the named function + scalar dispatch(const word&, const UList&); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/applications/test/dictionary/calcEntry/calcEntryParser.cpp b/applications/test/dictionary/calcEntry/calcEntryParser.cpp index 4fc4016207..e1a3937823 100644 --- a/applications/test/dictionary/calcEntry/calcEntryParser.cpp +++ b/applications/test/dictionary/calcEntry/calcEntryParser.cpp @@ -137,25 +137,29 @@ void Parser::Term(scalar& val) { } void Parser::Factor(scalar& val) { + bool negative = false; + if (la->kind == 7 || la->kind == 8) { + if (la->kind == 7) { + Get(); + } else { + Get(); + negative = true; + } + } if (la->kind == 1) { Func(val); + } else if (la->kind == 11) { + Get(); + Expr(val); + Expect(12); } else if (la->kind == 3) { Get(); val = getDictLookup(); } else if (la->kind == 4) { Get(); val = coco_string_toDouble(t->val); - } else if (la->kind == 8) { - Get(); - Expect(11); - Expr(val); - Expect(12); - val = -val; - } else if (la->kind == 11) { - Get(); - Expr(val); - Expect(12); } else SynErr(16); + if (negative) { val = -val; } } void Parser::Func(scalar& val) { @@ -177,7 +181,7 @@ void Parser::Func(scalar& val) { } } Expect(12); - val = scalarFunctions::dispatch(funcName, param); + val = dispatch(funcName, param); } @@ -225,7 +229,7 @@ bool Parser::StartOf(int s) { static const bool set[2][16] = { {T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x}, - {x,T,x,T, T,x,x,x, T,x,x,T, x,x,x,x} + {x,T,x,T, T,x,x,T, T,x,x,T, x,x,x,x} }; diff --git a/applications/test/dictionary/calcEntry/calcEntryParser.h b/applications/test/dictionary/calcEntry/calcEntryParser.h index afa25782a7..cfc036ccec 100644 --- a/applications/test/dictionary/calcEntry/calcEntryParser.h +++ b/applications/test/dictionary/calcEntry/calcEntryParser.h @@ -4,10 +4,7 @@ #define COCO_calcEntryPARSER_H__ #include "dictionary.H" -#include "scalar.H" -#include "error.H" #include "wchar.H" -#include "DynamicList.H" #include "calcEntryInternal.H" @@ -81,7 +78,7 @@ public: private: //- The parent dictionary - mutable dictionary* dict_; + dictionary* dict_; //- The calculation result scalar val; diff --git a/applications/test/dictionary/calcEntry/globalFunctionSelectionTables.H b/applications/test/dictionary/calcEntry/globalFunctionSelectionTables.H new file mode 100644 index 0000000000..4a7dfb4ac4 --- /dev/null +++ b/applications/test/dictionary/calcEntry/globalFunctionSelectionTables.H @@ -0,0 +1,145 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +@file Foam::globalFunctionSelectionTables + +Description + Macros to enable the easy declaration of global function selection tables. + +\*---------------------------------------------------------------------------*/ + +#ifndef globalMemberFunctionSelectionTables_H +#define globalMemberFunctionSelectionTables_H + +#include "memberFunctionSelectionTables.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + + +// external use: +// ~~~~~~~~~~~~~ +// declare a run-time selection: +#define declareGlobalFunctionSelectionTable\ +(returnType,memberFunction,argNames,argList,parList) \ + \ + /* Construct from argList function pointer type */ \ + typedef returnType (*memberFunction##argNames##MemberFunctionPtr)argList; \ + \ + /* Construct from argList function table type */ \ + typedef HashTable \ + \ + memberFunction##argNames##MemberFunctionTable; \ + \ + /* Construct from argList function pointer table pointer */ \ + extern memberFunction##argNames##MemberFunctionTable* \ + memberFunction##argNames##MemberFunctionTablePtr_; \ + \ + /* Table memberFunction called from the table add function */ \ + void construct##memberFunction##argNames##MemberFunctionTables(); \ + \ + /* Table destructor called from the table add function destructor */ \ + void destroy##memberFunction##argNames##MemberFunctionTables(); \ + \ + /* Class to add constructor from argList to table */ \ + class add##memberFunction##argNames##GlobalMemberFunctionToTable \ + { \ + public: \ + \ + add##memberFunction##argNames##GlobalMemberFunctionToTable \ + ( \ + const word& lookup, \ + memberFunction##argNames##MemberFunctionPtr function \ + ) \ + { \ + construct##memberFunction##argNames##MemberFunctionTables(); \ + memberFunction##argNames##MemberFunctionTablePtr_->insert \ + ( \ + lookup, \ + function \ + ); \ + } \ + \ + ~add##memberFunction##argNames##GlobalMemberFunctionToTable() \ + { \ + destroy##memberFunction##argNames##MemberFunctionTables(); \ + } \ + } + +// internal use: +// constructor/destructor aid +#define defineGlobalFunctionSelectionTableConstructDestruct\ +(memberFunction,argNames) \ + \ + /* Table constructor called from the table add function */ \ + void construct##memberFunction##argNames##MemberFunctionTables()\ + { \ + static bool constructed = false; \ + if (!constructed) \ + { \ + constructed = true; \ + memberFunction##argNames##MemberFunctionTablePtr_ \ + = new memberFunction##argNames##MemberFunctionTable; \ + } \ + } \ + \ + /* Table destructor called from the table add function destructor */ \ + void destroy##memberFunction##argNames##MemberFunctionTables()\ + { \ + if (memberFunction##argNames##MemberFunctionTablePtr_) \ + { \ + delete memberFunction##argNames##MemberFunctionTablePtr_; \ + memberFunction##argNames##MemberFunctionTablePtr_ = NULL; \ + } \ + } + + +// internal use: +// create pointer to hash-table of functions +#define defineGlobalFunctionSelectionTablePtr\ +(memberFunction,argNames) \ + \ + /* Define the memberFunction table */ \ + memberFunction##argNames##MemberFunctionTable* \ + memberFunction##argNames##MemberFunctionTablePtr_ = NULL + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// external use: +// ~~~~~~~~~~~~~ +// define run-time selection table +#define defineGlobalFunctionSelectionTable\ +(memberFunction,argNames) \ + \ + defineGlobalFunctionSelectionTablePtr \ + (memberFunction,argNames); \ + defineGlobalFunctionSelectionTableConstructDestruct \ + (memberFunction,argNames) + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + + +#endif + +// ************************************************************************* // diff --git a/applications/test/dictionary/testDictCalc b/applications/test/dictionary/testDictCalc index 6f2c38a51c..092a933a29 100644 --- a/applications/test/dictionary/testDictCalc +++ b/applications/test/dictionary/testDictCalc @@ -39,8 +39,11 @@ is done inplace; flowRate #calc{ $flowRatePerHour / 3600 }; -sin45 #calc{ sin( 45*pi() / 180 ) }; -cos45 #calc{ cos( degToRad(45) ) }; +// inplace redefine +flowRate #calc{ $flowRate * 0.1 }; + +sin45 #calc{ sin( 45.0 * pi() / 180 ) }; +cos45 #calc{ cos( degToRad(15- -15+ +15) ) }; pow #calc{ pow( $x, $y ) }; log10 #calc{ log( 1e6 ) }; // list #calc{ list() };