ENH: Add functionEntry '#calc' to dictionary

- handles basic operations, references to other dictionary entries
  (with '$name' syntax) and assorted mathematical functions:

      pi(), degToRad, radToDeg, asin, acos, atan, sin, cos, tan, log,
      log10, mag, atan2, pow

The basic syntax: #calc{ ... };

NOTE the trailing ';' is required for the primitiveEntry to be
properly defined.
This commit is contained in:
Mark Olesen
2010-02-07 13:58:10 +01:00
parent 03956d46be
commit 00616b72ff
15 changed files with 153 additions and 102 deletions

View File

@ -1,7 +1,3 @@
dictionaryTest.C dictionaryTest.C
calcEntry/calcEntryParser.atg
calcEntry/calcEntryInternal.C
calcEntry/calcEntry.C
EXE = $(FOAM_USER_APPBIN)/dictionaryTest EXE = $(FOAM_USER_APPBIN)/dictionaryTest

View File

@ -1 +1 @@
EXE_INC = -IcalcEntry -I$(OBJECTS_DIR) EXE_INC =

View File

@ -10,7 +10,7 @@ FoamFile
version 2.0; version 2.0;
format ascii; format ascii;
class dictionary; class dictionary;
object testDictTest; object testDictCalc;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -19,28 +19,13 @@ flowRatePerHour 720;
x 10; x 10;
y 20; y 20;
z t s v; z t s v;
// z #test{ // this p #calc{ 1 + 2 + 10 * 15 + $x - $y };
// 123 - 456
// // comments // are
// /* stripped
// * 10
// * {}
// */
// + 1 /*100 */ 10
// };
p this calculation #calc{ // this calculation is in-place, but does not work inside a string:
1 + 2 + 10 * 15 + flowRate "The flow rate " #calc{ $flowRatePerHour / 3600 } "kg/s";
$x - $y
// $x + $y
}
is done inplace;
// this is also okay
x #calc{ $x * 1E-3 };
flowRate #calc{ $flowRatePerHour / 3600};
xxx yyy;
foo 30;
bar 15;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1,46 @@
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: Any |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object testDictCalcError;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
flowRatePerHour 720;
x 10;
y 20;
z t s v;
// z #test{ // this
// 123 - 456
// // comments // are
// /* stripped
// * 10
// * {}
// */
// + 1 /*100 */ 10
// };
p this calculation #calc{
1xxx1 + 2 + 10 * 15 +
$x - $y
// $x + $y
}
is done inplace;
flowRate #calc{ $flowRatePerHour / 3600};
xxx yyy;
foo 30;
bar 15;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -142,6 +142,12 @@ $(functionEntries)/includeIfPresentEntry/includeIfPresentEntry.C
$(functionEntries)/inputModeEntry/inputModeEntry.C $(functionEntries)/inputModeEntry/inputModeEntry.C
$(functionEntries)/removeEntry/removeEntry.C $(functionEntries)/removeEntry/removeEntry.C
calcEntry = $(functionEntries)/calcEntry
$(calcEntry)/calcEntryParser.atg
$(calcEntry)/calcEntryInternal.C
$(calcEntry)/calcEntry.C
IOdictionary = db/IOobjects/IOdictionary IOdictionary = db/IOobjects/IOdictionary
$(IOdictionary)/IOdictionary.C $(IOdictionary)/IOdictionary.C
$(IOdictionary)/IOdictionaryIO.C $(IOdictionary)/IOdictionaryIO.C

View File

@ -1,4 +1,4 @@
EXE_INC = EXE_INC = -I$(OBJECTS_DIR)
LIB_LIBS = \ LIB_LIBS = \
$(FOAM_LIBBIN)/libOSspecific.o \ $(FOAM_LIBBIN)/libOSspecific.o \

View File

@ -1,3 +1,8 @@
/*---------------------------------------------------------------------------*\
Attributed Grammar for Coco/R (-*- C++ -*- version)
compile with:
coco-cpp calcEntryParser.atg
\*---------------------------------------------------------------------------*/
[copy] [copy]
/*---------------------------------*- C++ -*---------------------------------*\ /*---------------------------------*- C++ -*---------------------------------*\
========= | ========= |
@ -36,13 +41,6 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
[/copy] [/copy]
/*---------------------------------------------------------------------------*\
compile with:
Coco \
[-single] -frames $WM_THIRD_PARTY_DIR/coco-r \
calcEntryParser.atg
\*---------------------------------------------------------------------------*/
#include "dictionary.H" #include "dictionary.H"
#include "wchar.H" #include "wchar.H"
#include "calcEntryInternal.H" #include "calcEntryInternal.H"
@ -52,7 +50,9 @@ COMPILER calcEntry
// grammar pragmas: // grammar pragmas:
$prefix=calcEntry $prefix=calcEntry
$namespace=Foam::functionEntries::calcEntryInternal $namespace=Foam::functionEntries::calcEntryInternal
$explicitEOF=true // grammar handles eof itself $define=EXPLICIT_EOF // grammar handles eof itself
$define=STREAMS_ONLY // only use STL streams
$define=NO_UTF8 // disable UTF8 on input
private: private:
//- The parent dictionary //- The parent dictionary
@ -62,56 +62,7 @@ private:
scalar val; scalar val;
//- lookup dictionary entry //- lookup dictionary entry
scalar getDictLookup() const scalar getDictLookup(const word&) const;
{
if (!dict_)
{
FatalErrorIn
(
"calcEntry::getDictEntry() const"
) << "No dictionary attached!"
<< exit(FatalError);
return 0;
}
char* str = coco_string_create_char
(
t->val,
1,
(coco_string_length(t->val) - 1)
);
word keyword(str);
coco_string_delete(str);
scalar dictValue = 0;
entry* entryPtr = dict_->lookupEntryPtr(keyword, true, false);
if (entryPtr && !entryPtr->isDict())
{
if (entryPtr->stream().size() != 1)
{
FatalErrorIn
(
"calcEntry::getDictEntry() const"
) << "keyword " << keyword << " has "
<< entryPtr->stream().size() << " values in dictionary "
<< exit(FatalError);
}
entryPtr->stream() >> dictValue;
}
else
{
FatalErrorIn
(
"calcEntry::getDictEntry() const"
) << "keyword " << keyword << " is undefined in dictionary "
<< exit(FatalError);
}
return dictValue;
}
public: public:
@ -128,11 +79,57 @@ public:
} }
[initialize] /*---------------------------------------------------------------------------*/
[initialize] // add to Parser constructor
dict_ = 0; dict_ = 0;
val = 0; val = 0;
[/initialize] [/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 CHARACTERS
@ -226,7 +223,10 @@ Factor<scalar& val> (. bool negative = false; .)
] ]
( (
Func<val> | '(' Expr<val> ')' Func<val> | '(' Expr<val> ')'
| variable (. val = getDictLookup(); .) | variable (.
// skip leading '$' for the keyword
val = getDictLookup(t->toString(1, t->length()-1));
.)
| number (. val = coco_string_toDouble(t->val); .) | number (. val = coco_string_toDouble(t->val); .)
) (. if (negative) { val = -val; } .) ) (. if (negative) { val = -val; } .)
. .
@ -238,9 +238,7 @@ Factor<scalar& val> (. bool negative = false; .)
Func<scalar& val> Func<scalar& val>
= =
ident (. ident (.
char* str = coco_string_create_char(t->val); word funcName(t->toString());
word funcName(str);
coco_string_delete(str);
DynamicList<scalar> stack(4); DynamicList<scalar> stack(4);
.) .)
'(' '('

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2009-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -45,7 +45,7 @@ namespace Foam
Class CocoParserErrors Declaration Class CocoParserErrors Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
template<class BaseClass, class CharClass=wchar_t> template<class BaseClass, class StringClass=std::wstring>
class CocoParserErrors class CocoParserErrors
: :
public BaseClass public BaseClass
@ -90,14 +90,14 @@ public:
// Error Handling // Error Handling
//- Handle a general warning 'msg' //- Handle a general warning 'msg'
virtual void Warning(const CharClass* msg) virtual void Warning(const StringClass& msg)
{ {
WarningIn(name_) WarningIn(name_)
<< msg << endl; << msg << endl;
} }
//- Handle a general warning 'msg' //- Handle a general warning 'msg'
virtual void Warning(int line, int col, const CharClass* msg) virtual void Warning(int line, int col, const StringClass& msg)
{ {
WarningIn(name_) WarningIn(name_)
<<"line " << line << " col " << col << ": " <<"line " << line << " col " << col << ": "
@ -105,7 +105,7 @@ public:
} }
//- Handle general error 'msg' (eg, a semantic error) //- Handle general error 'msg' (eg, a semantic error)
virtual void Error(int line, int col, const CharClass* msg) virtual void Error(int line, int col, const StringClass& msg)
{ {
FatalErrorIn(name_) FatalErrorIn(name_)
<< "line " << line << " col " << col <<": " << msg << endl << "line " << line << " col " << col <<": " << msg << endl
@ -113,7 +113,7 @@ public:
} }
//- Handle general error 'msg' (eg, a semantic error) //- Handle general error 'msg' (eg, a semantic error)
virtual void Error(const CharClass* msg) virtual void Error(const StringClass& msg)
{ {
FatalErrorIn(name_) FatalErrorIn(name_)
<< msg << endl << msg << endl
@ -121,7 +121,7 @@ public:
} }
//- Handle a general exception 'msg' //- Handle a general exception 'msg'
virtual void Exception(const CharClass* msg) virtual void Exception(const StringClass& msg)
{ {
this->Error(msg); this->Error(msg);
} }

View File

@ -41,6 +41,7 @@ SeeAlso
#define wchar_H #define wchar_H
#include <cwchar> #include <cwchar>
#include <string>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -58,6 +59,9 @@ Ostream& operator<<(Ostream&, const wchar_t);
//- Output wide character (Unicode) string as UTF-8 //- Output wide character (Unicode) string as UTF-8
Ostream& operator<<(Ostream&, const wchar_t*); Ostream& operator<<(Ostream&, const wchar_t*);
//- Output wide character (Unicode) string as UTF-8
Ostream& operator<<(Ostream&, const std::wstring&);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -101,13 +101,13 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const wchar_t wc)
} }
Foam::Ostream& Foam::operator<<(Ostream& os, const wchar_t* ws) Foam::Ostream& Foam::operator<<(Ostream& os, const wchar_t* wstr)
{ {
if (ws) if (wstr)
{ {
for (const wchar_t* p = ws; *p; ++p) for (const wchar_t* iter = wstr; *iter; ++iter)
{ {
os << *p; os << *iter;
} }
} }
@ -115,4 +115,20 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const wchar_t* ws)
} }
Foam::Ostream& Foam::operator<<(Ostream& os, const std::wstring& wstr)
{
for
(
std::wstring::const_iterator iter = wstr.begin();
iter != wstr.end();
++iter
)
{
os << *iter;
}
return os;
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -3,7 +3,7 @@
.SUFFIXES: .atg .SUFFIXES: .atg
atgtoo = \ atgtoo = \
$(WM_THIRD_PARTY_DIR)/coco-cpp/platforms/$(WM_ARCH)/bin/coco-cpp \ $(WM_THIRD_PARTY_DIR)/coco-cpp/platforms/$(WM_ARCH)$(WM_COMPILER)/bin/coco-cpp \
-single \ -single \
-frames $(WM_THIRD_PARTY_DIR)/coco-cpp/platforms/share/coco-cpp \ -frames $(WM_THIRD_PARTY_DIR)/coco-cpp/platforms/share/coco-cpp \
$$SOURCE -o $(OBJECTS_DIR) && \ $$SOURCE -o $(OBJECTS_DIR) && \