Use global lookup tables for the calcEntry scalar functions.

Handle leading signs on functions etc.
This commit is contained in:
Mark Olesen
2010-01-02 15:09:17 +01:00
parent f8147f5bb5
commit cf91174880
8 changed files with 314 additions and 182 deletions

View File

@ -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
// ************************************************************************* //

View File

@ -39,10 +39,7 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "dictionary.H" #include "dictionary.H"
#include "scalar.H"
#include "error.H"
#include "wchar.H" #include "wchar.H"
#include "DynamicList.H"
#include "calcEntryInternal.H" #include "calcEntryInternal.H"
@ -56,7 +53,7 @@ COMPILER calcEntry
private: private:
//- The parent dictionary //- The parent dictionary
mutable dictionary* dict_; dictionary* dict_;
//- The calculation result //- The calculation result
scalar val; scalar val;
@ -196,8 +193,8 @@ Expr<scalar& val> (. scalar val2 = 0; .)
= =
Term<val> Term<val>
{ {
"+" Term<val2> (. val += val2; .) '+' Term<val2> (. val += val2; .)
| "-" Term<val2> (. val -= val2; .) | '-' Term<val2> (. val -= val2; .)
} }
. .
@ -208,20 +205,26 @@ Term<scalar& val> (. scalar val2 = 0; .)
= =
Factor<val> Factor<val>
{ {
"*" Factor<val2> (. val *= val2; .) '*' Factor<val2> (. val *= val2; .)
| "/" Factor<val2> (. val /= val2; .) | '/' Factor<val2> (. val /= val2; .)
} }
. .
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
Factor<scalar& val>
// Note the treatment of the leading signs is fairly generous
// eg, "10 + - 10" is treated like "10 + -10"
//
Factor<scalar& val> (. bool negative = false; .)
= =
Func<val> ['+' | '-' (. negative = true; .)
| variable (. val = getDictLookup(); .) ]
| number (. val = coco_string_toDouble(t->val); .) (
| '-' '(' Expr<val> ')' (. val = -val; .) Func<val> | '(' Expr<val> ')'
| '(' Expr<val> ')' | variable (. val = getDictLookup(); .)
| number (. val = coco_string_toDouble(t->val); .)
) (. if (negative) { val = -val; } .)
. .
@ -242,7 +245,7 @@ Func<scalar& val>
{ ',' Expr<x> (. param.append(x); .) { ',' Expr<x> (. param.append(x); .)
} }
] ]
')' (. val = scalarFunctions::dispatch(funcName, param); .) ')' (. val = dispatch(funcName, param); .)
. .

View File

@ -25,8 +25,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "calcEntryInternal.H" #include "calcEntryInternal.H"
#include "addToMemberFunctionSelectionTable.H" #include "addToGlobalFunctionSelectionTable.H"
#include "addToStaticMemberFunctionSelectionTable.H"
#include "unitConversion.H" #include "unitConversion.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -38,14 +37,66 @@ namespace functionEntries
namespace calcEntryInternal namespace calcEntryInternal
{ {
defineStaticMemberFunctionSelectionTable(scalarFunctions,dispatch,ParamList); defineGlobalFunctionSelectionTable(dispatch,ParamList);
scalar scalarFunctions::dispatch // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
(
const word& name, #define globalConstant0(Name, Constant)\
const UList<scalar>& param scalar Name##_0(const UList<scalar>& param) \
) { \
return Constant; \
} \
addNamedToGlobalFunctionSelectionTable(dispatch,ParamList,Name##_0,&Name##_0)
#define globalFunction0(Name, Function)\
scalar Name##_0(const UList<scalar>& param) \
{ \
return Function(); \
} \
addNamedToGlobalFunctionSelectionTable(dispatch,ParamList,Name##_0,&Name##_0)
#define globalFunction1(Name, Function)\
scalar Name##_1(const UList<scalar>& param) \
{ \
return Function(param[0]); \
} \
addNamedToGlobalFunctionSelectionTable(dispatch,ParamList,Name##_1,&Name##_1)
#define globalFunction2(Name, Function)\
scalar Name##_2(const UList<scalar>& 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<scalar>& param)
{ {
// create lookup name with parameter count // create lookup name with parameter count
const word lookupName = name + '_' + Foam::name(param.size()); const word lookupName = name + '_' + Foam::name(param.size());
@ -69,114 +120,10 @@ scalar scalarFunctions::dispatch
} }
scalar scalarFunctions::pi_0(const UList<scalar>& param)
{
return constant::mathematical::pi;
}
scalar scalarFunctions::degToRad_1(const UList<scalar>& param)
{
return degToRad(param[0]);
}
scalar scalarFunctions::radToDeg_1(const UList<scalar>& param)
{
return radToDeg(param[0]);
}
scalar scalarFunctions::sin_1(const UList<scalar>& param)
{
return Foam::sin(param[0]);
}
scalar scalarFunctions::cos_1(const UList<scalar>& param)
{
return Foam::cos(param[0]);
}
scalar scalarFunctions::pow_2(const UList<scalar>& param)
{
return Foam::pow(param[0], param[1]);
}
scalar scalarFunctions::log_1(const UList<scalar>& param)
{
return Foam::log(param[0]);
}
scalar scalarFunctions::log10_1(const UList<scalar>& 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 calcEntryInternal
} // End namespace functionEntries } // End namespace functionEntries
} // End namespace Foam } // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// ************************************************************************* // // ************************************************************************* //

View File

@ -26,8 +26,7 @@ Namespace
Foam::functionEntries::calcEntryInternal Foam::functionEntries::calcEntryInternal
Description Description
This dictionary function entry may or may not do anything particularly Contains global functions and classes for the calcEntry.
useful - depending upon what is currently being used to test.
SourceFiles SourceFiles
calcEntryInternal.C calcEntryInternal.C
@ -37,9 +36,10 @@ SourceFiles
#ifndef calcEntryInternal_H #ifndef calcEntryInternal_H
#define calcEntryInternal_H #define calcEntryInternal_H
#include "functionEntry.H" #include "error.H"
#include "memberFunctionSelectionTables.H" #include "scalar.H"
#include "staticMemberFunctionSelectionTables.H" #include "DynamicList.H"
#include "globalFunctionSelectionTables.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -50,42 +50,22 @@ namespace functionEntries
namespace calcEntryInternal namespace calcEntryInternal
{ {
/*---------------------------------------------------------------------------*\ // Global Function Selectors
Class calcEntryFunctions Declaration
\*---------------------------------------------------------------------------*/
class scalarFunctions declareGlobalFunctionSelectionTable
{ (
public: scalar,
dispatch,
// Member Function Selectors ParamList,
declareStaticMemberFunctionSelectionTable
( (
scalar, const UList<scalar>& param
scalarFunctions, ),
dispatch, (param)
ParamList, );
(
const UList<scalar>& param
),
(param)
);
//- Calculate
static scalar dispatch(const word&, const UList<scalar>&);
static scalar pi_0(const UList<scalar>&); //- Dispatch calculation to the named function
static scalar degToRad_1(const UList<scalar>&); scalar dispatch(const word&, const UList<scalar>&);
static scalar radToDeg_1(const UList<scalar>&);
static scalar cos_1(const UList<scalar>&);
static scalar sin_1(const UList<scalar>&);
static scalar pow_2(const UList<scalar>&);
static scalar log_1(const UList<scalar>&);
static scalar log10_1(const UList<scalar>&);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -137,25 +137,29 @@ void Parser::Term(scalar& val) {
} }
void Parser::Factor(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) { if (la->kind == 1) {
Func(val); Func(val);
} else if (la->kind == 11) {
Get();
Expr(val);
Expect(12);
} else if (la->kind == 3) { } else if (la->kind == 3) {
Get(); Get();
val = getDictLookup(); val = getDictLookup();
} else if (la->kind == 4) { } else if (la->kind == 4) {
Get(); Get();
val = coco_string_toDouble(t->val); 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); } else SynErr(16);
if (negative) { val = -val; }
} }
void Parser::Func(scalar& val) { void Parser::Func(scalar& val) {
@ -177,7 +181,7 @@ void Parser::Func(scalar& val) {
} }
} }
Expect(12); 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] = { static const bool set[2][16] = {
{T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x}, {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}
}; };

View File

@ -4,10 +4,7 @@
#define COCO_calcEntryPARSER_H__ #define COCO_calcEntryPARSER_H__
#include "dictionary.H" #include "dictionary.H"
#include "scalar.H"
#include "error.H"
#include "wchar.H" #include "wchar.H"
#include "DynamicList.H"
#include "calcEntryInternal.H" #include "calcEntryInternal.H"
@ -81,7 +78,7 @@ public:
private: private:
//- The parent dictionary //- The parent dictionary
mutable dictionary* dict_; dictionary* dict_;
//- The calculation result //- The calculation result
scalar val; scalar val;

View File

@ -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##MemberFunctionPtr, word, string::hash> \
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
// ************************************************************************* //

View File

@ -39,8 +39,11 @@ is done inplace;
flowRate #calc{ $flowRatePerHour / 3600 }; flowRate #calc{ $flowRatePerHour / 3600 };
sin45 #calc{ sin( 45*pi() / 180 ) }; // inplace redefine
cos45 #calc{ cos( degToRad(45) ) }; flowRate #calc{ $flowRate * 0.1 };
sin45 #calc{ sin( 45.0 * pi() / 180 ) };
cos45 #calc{ cos( degToRad(15- -15+ +15) ) };
pow #calc{ pow( $x, $y ) }; pow #calc{ pow( $x, $y ) };
log10 #calc{ log( 1e6 ) }; log10 #calc{ log( 1e6 ) };
// list #calc{ list() }; // list #calc{ list() };