mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
@ -1,7 +1,3 @@
|
||||
dictionaryTest.C
|
||||
|
||||
calcEntry/calcEntryParser.atg
|
||||
calcEntry/calcEntryInternal.C
|
||||
calcEntry/calcEntry.C
|
||||
|
||||
EXE = $(FOAM_USER_APPBIN)/dictionaryTest
|
||||
|
||||
@ -1 +1 @@
|
||||
EXE_INC = -IcalcEntry -I$(OBJECTS_DIR)
|
||||
EXE_INC =
|
||||
|
||||
@ -10,7 +10,7 @@ FoamFile
|
||||
version 2.0;
|
||||
format ascii;
|
||||
class dictionary;
|
||||
object testDictTest;
|
||||
object testDictCalc;
|
||||
}
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -19,28 +19,13 @@ flowRatePerHour 720;
|
||||
x 10;
|
||||
y 20;
|
||||
z t s v;
|
||||
// z #test{ // this
|
||||
// 123 - 456
|
||||
// // comments // are
|
||||
// /* stripped
|
||||
// * 10
|
||||
// * {}
|
||||
// */
|
||||
// + 1 /*100 */ 10
|
||||
// };
|
||||
p #calc{ 1 + 2 + 10 * 15 + $x - $y };
|
||||
|
||||
p this calculation #calc{
|
||||
1 + 2 + 10 * 15 +
|
||||
$x - $y
|
||||
// $x + $y
|
||||
}
|
||||
is done inplace;
|
||||
// this calculation is in-place, but does not work inside a string:
|
||||
flowRate "The flow rate " #calc{ $flowRatePerHour / 3600 } "kg/s";
|
||||
|
||||
// this is also okay
|
||||
x #calc{ $x * 1E-3 };
|
||||
|
||||
flowRate #calc{ $flowRatePerHour / 3600};
|
||||
|
||||
xxx yyy;
|
||||
foo 30;
|
||||
bar 15;
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
46
applications/test/dictionary/testDictCalcError
Normal file
46
applications/test/dictionary/testDictCalcError
Normal 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;
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
@ -142,6 +142,12 @@ $(functionEntries)/includeIfPresentEntry/includeIfPresentEntry.C
|
||||
$(functionEntries)/inputModeEntry/inputModeEntry.C
|
||||
$(functionEntries)/removeEntry/removeEntry.C
|
||||
|
||||
calcEntry = $(functionEntries)/calcEntry
|
||||
$(calcEntry)/calcEntryParser.atg
|
||||
$(calcEntry)/calcEntryInternal.C
|
||||
$(calcEntry)/calcEntry.C
|
||||
|
||||
|
||||
IOdictionary = db/IOobjects/IOdictionary
|
||||
$(IOdictionary)/IOdictionary.C
|
||||
$(IOdictionary)/IOdictionaryIO.C
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
EXE_INC =
|
||||
EXE_INC = -I$(OBJECTS_DIR)
|
||||
|
||||
LIB_LIBS = \
|
||||
$(FOAM_LIBBIN)/libOSspecific.o \
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Attributed Grammar for Coco/R (-*- C++ -*- version)
|
||||
compile with:
|
||||
coco-cpp calcEntryParser.atg
|
||||
\*---------------------------------------------------------------------------*/
|
||||
[copy]
|
||||
/*---------------------------------*- C++ -*---------------------------------*\
|
||||
========= |
|
||||
@ -36,13 +41,6 @@ SourceFiles
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
[/copy]
|
||||
/*---------------------------------------------------------------------------*\
|
||||
compile with:
|
||||
Coco \
|
||||
[-single] -frames $WM_THIRD_PARTY_DIR/coco-r \
|
||||
calcEntryParser.atg
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "dictionary.H"
|
||||
#include "wchar.H"
|
||||
#include "calcEntryInternal.H"
|
||||
@ -52,7 +50,9 @@ COMPILER calcEntry
|
||||
// grammar pragmas:
|
||||
$prefix=calcEntry
|
||||
$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:
|
||||
//- The parent dictionary
|
||||
@ -62,56 +62,7 @@ private:
|
||||
scalar val;
|
||||
|
||||
//- lookup dictionary entry
|
||||
scalar getDictLookup() 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;
|
||||
}
|
||||
|
||||
scalar getDictLookup(const word&) const;
|
||||
|
||||
public:
|
||||
|
||||
@ -128,11 +79,57 @@ public:
|
||||
}
|
||||
|
||||
|
||||
[initialize]
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
[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
|
||||
@ -226,7 +223,10 @@ Factor<scalar& val> (. bool negative = false; .)
|
||||
]
|
||||
(
|
||||
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); .)
|
||||
) (. if (negative) { val = -val; } .)
|
||||
.
|
||||
@ -238,9 +238,7 @@ Factor<scalar& val> (. bool negative = false; .)
|
||||
Func<scalar& val>
|
||||
=
|
||||
ident (.
|
||||
char* str = coco_string_create_char(t->val);
|
||||
word funcName(str);
|
||||
coco_string_delete(str);
|
||||
word funcName(t->toString());
|
||||
DynamicList<scalar> stack(4);
|
||||
.)
|
||||
'('
|
||||
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2009-2010 OpenCFD Ltd.
|
||||
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
@ -45,7 +45,7 @@ namespace Foam
|
||||
Class CocoParserErrors Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
template<class BaseClass, class CharClass=wchar_t>
|
||||
template<class BaseClass, class StringClass=std::wstring>
|
||||
class CocoParserErrors
|
||||
:
|
||||
public BaseClass
|
||||
@ -90,14 +90,14 @@ public:
|
||||
// Error Handling
|
||||
|
||||
//- Handle a general warning 'msg'
|
||||
virtual void Warning(const CharClass* msg)
|
||||
virtual void Warning(const StringClass& msg)
|
||||
{
|
||||
WarningIn(name_)
|
||||
<< msg << endl;
|
||||
}
|
||||
|
||||
//- 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_)
|
||||
<<"line " << line << " col " << col << ": "
|
||||
@ -105,7 +105,7 @@ public:
|
||||
}
|
||||
|
||||
//- 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_)
|
||||
<< "line " << line << " col " << col <<": " << msg << endl
|
||||
@ -113,7 +113,7 @@ public:
|
||||
}
|
||||
|
||||
//- Handle general error 'msg' (eg, a semantic error)
|
||||
virtual void Error(const CharClass* msg)
|
||||
virtual void Error(const StringClass& msg)
|
||||
{
|
||||
FatalErrorIn(name_)
|
||||
<< msg << endl
|
||||
@ -121,7 +121,7 @@ public:
|
||||
}
|
||||
|
||||
//- Handle a general exception 'msg'
|
||||
virtual void Exception(const CharClass* msg)
|
||||
virtual void Exception(const StringClass& msg)
|
||||
{
|
||||
this->Error(msg);
|
||||
}
|
||||
|
||||
@ -41,6 +41,7 @@ SeeAlso
|
||||
#define wchar_H
|
||||
|
||||
#include <cwchar>
|
||||
#include <string>
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -58,6 +59,9 @@ Ostream& operator<<(Ostream&, const wchar_t);
|
||||
//- Output wide character (Unicode) string as UTF-8
|
||||
Ostream& operator<<(Ostream&, const wchar_t*);
|
||||
|
||||
//- Output wide character (Unicode) string as UTF-8
|
||||
Ostream& operator<<(Ostream&, const std::wstring&);
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
.SUFFIXES: .atg
|
||||
|
||||
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 \
|
||||
-frames $(WM_THIRD_PARTY_DIR)/coco-cpp/platforms/share/coco-cpp \
|
||||
$$SOURCE -o $(OBJECTS_DIR) && \
|
||||
|
||||
Reference in New Issue
Block a user