Commit b4dae1a5 authored by andy's avatar andy
Browse files

ENH: removed calcEntry

parent f4a84af4
......@@ -167,17 +167,6 @@ $(functionEntries)/includeIfPresentEntry/includeIfPresentEntry.C
$(functionEntries)/inputModeEntry/inputModeEntry.C
$(functionEntries)/removeEntry/removeEntry.C
/*
* Requires customized coco-cpp
* could be dropped or activated in the future
*/
/*
calcEntry = $(functionEntries)/calcEntry
$(calcEntry)/calcEntryParser.atg
$(calcEntry)/calcEntryInternal.C
$(calcEntry)/calcEntry.C
*/
IOdictionary = db/IOobjects/IOdictionary
$(IOdictionary)/IOdictionary.C
$(IOdictionary)/IOdictionaryIO.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2009-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 3 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, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "calcEntry.H"
#include "dictionary.H"
#include "addToMemberFunctionSelectionTable.H"
#include "ISstream.H"
#include "CocoParserErrors.H"
#include "calcEntryParser.h"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionEntries
{
defineTypeNameAndDebug(calcEntry, 0);
addToMemberFunctionSelectionTable
(
functionEntry,
calcEntry,
execute,
primitiveEntryIstream
);
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionEntries::calcEntry::execute
(
const dictionary& parentDict,
primitiveEntry& entry,
Istream& is
)
{
std::istream& iss = dynamicCast<ISstream>(is).stdStream();
// define parser error handler
CocoParserErrors<calcEntryInternal::Errors>
myErrorHandler("calcEntryInternal::Parser");
calcEntryInternal::Scanner scanner(iss);
// set the starting line
scanner.Line(is.lineNumber());
calcEntryInternal::Parser parser(&scanner, &myErrorHandler);
// Attach dictionary context
parser.dict(parentDict);
parser.Parse();
// mostly have an extra newline in the lookahead token
// so subtract 1 to keep things vaguely in sync
// (this is still far from perfect)
is.lineNumber() = scanner.Line() - 1;
// a small input list to contain the answer
tokenList tokens(2);
tokens[0] = parser.Result();
tokens[1] = token::END_STATEMENT;
entry.read(parentDict, ITstream("ParserResult", tokens)());
return true;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2009-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 3 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, see <http://www.gnu.org/licenses/>.
Class
Foam::functionEntries::calcEntry
Description
This dictionary function entry may or may not do anything particularly
useful - depending upon what is currently being used to test.
SourceFiles
calcEntry.C
\*---------------------------------------------------------------------------*/
#ifndef calcEntry_H
#define calcEntry_H
#include "functionEntry.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace functionEntries
{
/*---------------------------------------------------------------------------*\
Class calcEntry Declaration
\*---------------------------------------------------------------------------*/
class calcEntry
:
public functionEntry
{
// Private Member Functions
//- Disallow default bitwise copy construct
calcEntry(const calcEntry&);
//- Disallow default bitwise assignment
void operator=(const calcEntry&);
public:
//- Runtime type information
ClassName("calc");
// Member Functions
static bool execute
(
const dictionary& parentDict,
primitiveEntry& entry,
Istream& is
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace functionEntries
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 3 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, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "calcEntryInternal.H"
#include "addToGlobalFunctionSelectionTable.H"
#include "unitConversion.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace functionEntries
{
namespace calcEntryInternal
{
defineGlobalFunctionSelectionTable(dispatch,ParamList);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#define globalConstant0(Name, Constant)\
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
const word lookupName = name + '_' + Foam::name(param.size());
dispatchParamListMemberFunctionTable::iterator mfIter =
dispatchParamListMemberFunctionTablePtr_->find(lookupName);
if (mfIter == dispatchParamListMemberFunctionTablePtr_->end())
{
FatalErrorIn
(
"calcEntryInternal::scalarFunctions::dispatch"
"(const word&, const UList<scalar>&) : "
) << "Unknown function " << name << nl << nl
<< "Valid types are :" << endl
<< dispatchParamListMemberFunctionTablePtr_->sortedToc()
<< exit(FatalError);
}
return mfIter()(param);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace calcEntryInternal
} // End namespace functionEntries
} // End namespace Foam
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 3 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, see <http://www.gnu.org/licenses/>.
Namespace
Foam::functionEntries::calcEntryInternal
Description
Contains global functions and classes for the calcEntry.
SourceFiles
calcEntryInternal.C
\*---------------------------------------------------------------------------*/
#ifndef calcEntryInternal_H
#define calcEntryInternal_H
#include "error.H"
#include "scalar.H"
#include "DynamicList.H"
#include "globalFunctionSelectionTables.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace functionEntries
{
namespace calcEntryInternal
{
// Global Function Selectors
declareGlobalFunctionSelectionTable
(
scalar,
dispatch,
ParamList,
(
const UList<scalar>& param
),
(param)
);
//- Dispatch calculation to the named function
scalar dispatch(const word&, const UList<scalar>&);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace calcEntryInternal
} // End namespace functionEntries
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
Attributed Grammar for Coco/R (-*- C++ -*- version)
compile with:
coco-cpp calcEntryParser.atg
\*---------------------------------------------------------------------------*/
[copy]
/*---------------------------------*- C++ -*---------------------------------*\
========= |
\\ / 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 3 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, see <http://www.gnu.org/licenses/>.
@file calcEntryParser.atg
Description
An attributed Coco/R grammar to parse simple arithmetic expressions
Includes support for dictionary $variables and some scalar functions
(eg, sin, pow, ...)
SourceFiles
generated
\*---------------------------------------------------------------------------*/
[/copy]
#include "dictionary.H"
#include "wchar.H"
#include "calcEntryInternal.H"
COMPILER calcEntry
// grammar pragmas:
$prefix=calcEntry
$namespace=Foam::functionEntries::calcEntryInternal
$define=EXPLICIT_EOF // grammar handles eof itself
$define=STREAMS_ONLY // only use STL streams
$define=NO_UTF8 // disable UTF8 on input
private:
//- The parent dictionary
dictionary* dict_;
//- The calculation result
scalar val;
//- lookup dictionary entry
scalar getDictLookup(const word&) const;
public:
//- attach a dictionary
void dict(const dictionary& dict)
{
dict_ = const_cast<dictionary*>(&dict);
}
//- Return the calculated result
scalar Result() const
{
return val;
}
/*---------------------------------------------------------------------------*/
[initialize] // add to Parser constructor
dict_ = 0;
val = 0;
[/initialize]
/*---------------------------------------------------------------------------*/
[code]
Foam::scalar Parser::getDictLookup(const word& keyword) const
{
if (!dict_)
{
FatalErrorIn
(
"calcEntry::getDictEntry(const word&) const"
) << "No dictionary attached!"
<< exit(FatalError);
return 0;
}
scalar dictValue = 0;
entry* entryPtr = dict_->lookupEntryPtr(keyword, true, false);
if (entryPtr && !entryPtr->isDict())
{
if (entryPtr->stream().size() != 1)
{
FatalErrorIn
(
"calcEntry::getDictEntry(const word&) const"
) << "keyword " << keyword << " has "
<< entryPtr->stream().size() << " values in dictionary "
<< exit(FatalError);
}
entryPtr->stream() >> dictValue;
}
else
{
FatalErrorIn
(
"calcEntry::getDictEntry(const word&) const"
) << "keyword " << keyword << " is undefined in dictionary "
<< exit(FatalError);
}
return dictValue;
}
[/code]
/*---------------------------------------------------------------------------*/
CHARACTERS
letter = 'A'..'Z' + 'a'..'z' + '_'.
digit = "0123456789".
alphanum = letter + digit.
sign = '+' + '-'.
cr = '\r'.
lf = '\n'.
tab = '\t'.
stringCh = ANY - '"' - '\\' - cr - lf.
printable = '\u0020' .. '\u007e'.
// * * * * * * * * * * * * * * * * TOKENS * * * * * * * * * * * * * * * * * //
TOKENS
// identifier
ident =
letter { alphanum }.
// string
string =
'"' { stringCh | '\\' printable } '"'.
// dictionary lookup identifier
// starts with '$' and otherwise limited to a normal identifier
variable =
'$' letter { alphanum }.
// floating point and integer numbers
number =
[sign] ('.' digit { digit } ) | ( digit { digit } [ '.' { digit } ])
[ ('E' | 'e') [sign] digit { digit } ].
// * * * * * * * * * * * PRAGMAS / COMMENTS / IGNORE * * * * * * * * * * * //
COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO lf
IGNORE cr + lf + tab
// * * * * * * * * * * * * * * * PRODUCTIONS * * * * * * * * * * * * * * * //
PRODUCTIONS
calcEntry (. val = 0; .)
=
'{' Expr<val> '}' (. // reposition to immediately after the closing '}'
scanner->buffer->SetPos(t->pos + 1);
.)
| ( Expr<val> EOF )
.
/*---------------------------------------------------------------------------*/
Expr<scalar& val> (. scalar val2 = 0; .)