diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C index 24829271e0..656a74c53c 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C @@ -65,7 +65,7 @@ Foam::string Foam::functionEntries::calcEntry::calc Istream& is ) { - Info<< "Using #calcEntry at line " << is.lineNumber() + Info<< "Expanding #calc at line " << is.lineNumber() << " in file " << dict.name() << endl; dynamicCode::checkSecurity @@ -74,16 +74,37 @@ Foam::string Foam::functionEntries::calcEntry::calc dict ); - // Read string delimited by either '"..."' or '#{...#}' - verbatimString s(is); - // Construct codeDict for codeStream // with dict as parent dictionary for string expansion // and variable substitution dictionary codeDict(dict, dictionary()); - calcIncludeEntry::codeInclude(codeDict); - codeDict.add("code", "os << (" + s + ");"); + // Read the code expression string delimited by either '"..."' or '#{...#}' + token t(is); + + if (t.isVerbatimString()) + { + const verbatimString& s = t.verbatimStringToken(); + + calcIncludeEntry::codeInclude(codeDict); + codeDict.add("code", s); + } + else if (t.isString()) + { + const string& s = t.stringToken(); + + calcIncludeEntry::codeInclude(codeDict); + codeDict.add("code", "os << (" + s + ");"); + } + else + { + FatalIOErrorInFunction(is) + << "Wrong string type for #calc" << nl + << " Expected either a string delimited by '\"...\"' " + "or a verbatim string delimited by '#{...#}' " << nl + << " found token " << t + << exit(FatalIOError); + } codeStream::streamingFunctionType function = codeStream::getFunction ( diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H index 70db464a59..4019191f83 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H @@ -51,18 +51,9 @@ Usage c #calc "($a)/$b"; \endverbatim - The code string can be delimited either by '"..."' in which newlines must be - escaped or '#{...#}' which directly supports multi-line strings and is more - convenient when evaluating string expressions by avoiding the need to - escape the quotes, e.g. - - \verbatim - c #calc #{ $a*sqrt($b) #}; - \endverbatim - \verbatim s "field"; - fieldName #calc #{ $s + "Name" #}; + fieldName #calc "$s + \"Name\" "; \endverbatim Additional include files for the #calc code compilation can be specified @@ -77,12 +68,31 @@ Usage dragDir #calc "transform(Ry($angle), vector(1, 0, 0))"; \endverbatim - Note: - Internally this is just a wrapper around codeStream functionality - the - #calc string is used to construct a dictionary for codeStream. + If the code string is delimited by '#{...#}' multiple lines and multiple + code statements can be used to generate the entry using 'os << ...;'. + This is equivalent to #codeStream but with a more compact syntax. + \verbatim + #calcInclude "transform.H" + maxAngle 30; + nAngles 7; + Us #calc + #{ + const vector U($testCalc2!U); + const int nAngles = $nAngles; + const scalar angleStep = ($maxAngle)/(nAngles - 1); + List Us(nAngles); + for(int i=0; i($nCellsByL*($max - $min) + vector::one/2) #}; +nCells #calc + "Vector